1 /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2 file accompanying popt source distributions, available from
3 ftp://ftp.redhat.com/pub/code/popt */
7 * \brief Source: popt parser
14 #include "poptalloca.h"
21 #define POPT_ARGV_ARRAY_GROW_DELTA 5
23 int poptParseArgvString(const char * s
, int * argcPtr
, char *** argvPtr
) {
24 char * buf
, * bufStart
, * dst
;
27 int argvAlloced
= POPT_ARGV_ARRAY_GROW_DELTA
;
28 char ** argv
= malloc(sizeof(*argv
) * argvAlloced
);
33 buflen
= strlen(s
) + 1;
34 bufStart
= buf
= alloca(buflen
);
35 memset(buf
, '\0', buflen
);
48 return POPT_ERROR_BADQUOTE
;
50 if (*src
!= quote
) *buf
++ = '\\';
53 } else if (isspace((unsigned char) *src
)) {
56 if (argc
== argvAlloced
) {
57 argvAlloced
+= POPT_ARGV_ARRAY_GROW_DELTA
;
58 argv
= realloc(argv
, sizeof(*argv
) * argvAlloced
);
62 } else switch (*src
) {
71 return POPT_ERROR_BADQUOTE
;
81 if (strlen(argv
[argc
])) {
85 dst
= malloc(argc
* sizeof(*argv
) + (buf
- bufStart
));
87 dst
+= argc
* sizeof(*argv
);
88 memcpy(argv2
, argv
, argc
* sizeof(*argv
));
89 memcpy(dst
, bufStart
, buf
- bufStart
);
91 for (i
= 0; i
< argc
; i
++) {
92 argv2
[i
] = dst
+ (argv
[i
] - bufStart
);