1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/* Copyright (C) 2002, 2005 by Red Hat, Incorporated. All rights reserved.
*
* Permission to use, copy, modify, and distribute this software
* is freely granted, provided that this notice is preserved.
*/
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <argz.h>
#define __need_ptrdiff_t
#include <stddef.h>
error_t
argz_insert (char **argz,
size_t *argz_len,
char *before,
const char *entry)
{
int len = 0;
ptrdiff_t delta;
if (before == NULL)
return argz_add(argz, argz_len, entry);
if (before < *argz || before >= *argz + *argz_len)
return EINVAL;
while (before != *argz && before[-1])
before--;
/* delta will always be non-negative, and < *argz_len */
delta = before - *argz;
len = strlen(entry) + 1;
if(!(*argz = (char *)realloc(*argz, *argz_len + len)))
return ENOMEM;
memmove(*argz + delta + len, *argz + delta, *argz_len - delta);
memcpy(*argz + delta, entry, len);
*argz_len += len;
return 0;
}
|