1 /* Copyright (C) 2002 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 <argz.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <stdlib.h>
12 
13 error_t
argz_create(char * const argv[],char ** argz,size_t * argz_len)14 argz_create (char *const argv[],
15        char **argz,
16        size_t *argz_len)
17 {
18   int argc = 0;
19   int i = 0;
20   size_t len = 0;
21   char *iter;
22 
23   *argz_len = 0;
24 
25   if (*argv == NULL)
26     {
27       *argz = NULL;
28       return 0;
29     }
30 
31   while (argv[argc])
32     {
33       *argz_len += (strlen(argv[argc]) + 1);
34       argc++;
35     }
36 
37   /* There are argc strings to copy into argz. */
38   if(!(*argz = (char *)malloc(*argz_len)))
39     return ENOMEM;
40 
41   iter = *argz;
42   for(i = 0; i < argc; i++)
43     {
44       len = strlen(argv[i]) + 1;
45       memcpy(iter, argv[i], len);
46       iter += len;
47     }
48   return 0;
49 }
50