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