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 */
9 #include "poptalloca.h"
16 #define POPT_ARGV_ARRAY_GROW_DELTA 5
18 int poptParseArgvString(const char * s
, int * argcPtr
, char *** argvPtr
) {
19 char * buf
, * bufStart
, * dst
;
22 int argvAlloced
= POPT_ARGV_ARRAY_GROW_DELTA
;
23 char ** argv
= malloc(sizeof(*argv
) * argvAlloced
);
28 buflen
= strlen(s
) + 1;
29 bufStart
= buf
= alloca(buflen
);
30 memset(buf
, '\0', buflen
);
43 return POPT_ERROR_BADQUOTE
;
45 if (*src
!= quote
) *buf
++ = '\\';
48 } else if (isspace((unsigned char) *src
)) {
51 if (argc
== argvAlloced
) {
52 argvAlloced
+= POPT_ARGV_ARRAY_GROW_DELTA
;
53 argv
= realloc(argv
, sizeof(*argv
) * argvAlloced
);
57 } else switch (*src
) {
66 return POPT_ERROR_BADQUOTE
;
76 if (strlen(argv
[argc
])) {
80 dst
= malloc(argc
* sizeof(*argv
) + (buf
- bufStart
));
82 dst
+= argc
* sizeof(*argv
);
83 memcpy(argv2
, argv
, argc
* sizeof(*argv
));
84 memcpy(dst
, bufStart
, buf
- bufStart
);
86 for (i
= 0; i
< argc
; i
++) {
87 argv2
[i
] = dst
+ (argv
[i
] - bufStart
);