2 * \file popt/poptparse.c
5 /* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
6 file accompanying popt source distributions, available from
7 ftp://ftp.rpm.org/pub/rpm/dist. */
11 #define POPT_ARGV_ARRAY_GROW_DELTA 5
13 int poptDupArgv(int argc
, const char **argv
,
14 int * argcPtr
, const char *** argvPtr
)
16 size_t nb
= (argc
+ 1) * sizeof(*argv
);
21 if (argc
<= 0 || argv
== NULL
) /* XXX can't happen */
22 return POPT_ERROR_NOARG
;
23 for (i
= 0; i
< argc
; i
++) {
25 return POPT_ERROR_NOARG
;
26 nb
+= strlen(argv
[i
]) + 1;
30 if (dst
== NULL
) /* XXX can't happen */
31 return POPT_ERROR_MALLOC
;
33 dst
+= (argc
+ 1) * sizeof(*argv
);
36 for (i
= 0; i
< argc
; i
++) {
38 dst
+= strlen(strcpy(dst
, argv
[i
])) + 1;
54 int poptParseArgvString(const char * s
, int * argcPtr
, const char *** argvPtr
)
58 int argvAlloced
= POPT_ARGV_ARRAY_GROW_DELTA
;
59 const char ** argv
= malloc(sizeof(*argv
) * argvAlloced
);
61 int buflen
= strlen(s
) + 1;
62 char * buf
= memset(alloca(buflen
), 0, buflen
);
63 int rc
= POPT_ERROR_MALLOC
;
65 if (argv
== NULL
) return rc
;
68 for (src
= s
; *src
!= '\0'; src
++) {
71 } else if (quote
!= '\0') {
75 rc
= POPT_ERROR_BADQUOTE
;
78 if (*src
!= quote
) *buf
++ = '\\';
81 } else if (isspace(*src
)) {
82 if (*argv
[argc
] != '\0') {
84 if (argc
== argvAlloced
) {
85 argvAlloced
+= POPT_ARGV_ARRAY_GROW_DELTA
;
86 argv
= realloc(argv
, sizeof(*argv
) * argvAlloced
);
87 if (argv
== NULL
) goto exit
;
91 } else switch (*src
) {
95 /*@switchbreak@*/ break;
99 rc
= POPT_ERROR_BADQUOTE
;
105 /*@switchbreak@*/ break;
109 if (strlen(argv
[argc
])) {
113 rc
= poptDupArgv(argc
, argv
, argcPtr
, argvPtr
);
116 if (argv
) free(argv
);