Call wildmatch(), not fnmatch().
[rsync.git] / popt / poptparse.c
blob2ffb7dd293d8c2c58c54695d8cd0e778541386cb
1 /** \ingroup popt
2 * \file popt/poptparse.c
3 */
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. */
9 #include "system.h"
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);
17 const char ** argv2;
18 char * dst;
19 int i;
21 if (argc <= 0 || argv == NULL) /* XXX can't happen */
22 return POPT_ERROR_NOARG;
23 for (i = 0; i < argc; i++) {
24 if (argv[i] == NULL)
25 return POPT_ERROR_NOARG;
26 nb += strlen(argv[i]) + 1;
29 dst = malloc(nb);
30 if (dst == NULL) /* XXX can't happen */
31 return POPT_ERROR_MALLOC;
32 argv2 = (void *) dst;
33 dst += (argc + 1) * sizeof(*argv);
35 /*@-branchstate@*/
36 for (i = 0; i < argc; i++) {
37 argv2[i] = dst;
38 dst += strlen(strcpy(dst, argv[i])) + 1;
40 /*@=branchstate@*/
41 argv2[argc] = NULL;
43 if (argvPtr) {
44 *argvPtr = argv2;
45 } else {
46 free(argv2);
47 argv2 = NULL;
49 if (argcPtr)
50 *argcPtr = argc;
51 return 0;
54 int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
56 const char * src;
57 char quote = '\0';
58 int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
59 const char ** argv = malloc(sizeof(*argv) * argvAlloced);
60 int argc = 0;
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;
66 argv[argc] = buf;
68 for (src = s; *src != '\0'; src++) {
69 if (quote == *src) {
70 quote = '\0';
71 } else if (quote != '\0') {
72 if (*src == '\\') {
73 src++;
74 if (!*src) {
75 rc = POPT_ERROR_BADQUOTE;
76 goto exit;
78 if (*src != quote) *buf++ = '\\';
80 *buf++ = *src;
81 } else if (isspace(*src)) {
82 if (*argv[argc] != '\0') {
83 buf++, argc++;
84 if (argc == argvAlloced) {
85 argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
86 argv = realloc(argv, sizeof(*argv) * argvAlloced);
87 if (argv == NULL) goto exit;
89 argv[argc] = buf;
91 } else switch (*src) {
92 case '"':
93 case '\'':
94 quote = *src;
95 /*@switchbreak@*/ break;
96 case '\\':
97 src++;
98 if (!*src) {
99 rc = POPT_ERROR_BADQUOTE;
100 goto exit;
102 /*@fallthrough@*/
103 default:
104 *buf++ = *src;
105 /*@switchbreak@*/ break;
109 if (strlen(argv[argc])) {
110 argc++, buf++;
113 rc = poptDupArgv(argc, argv, argcPtr, argvPtr);
115 exit:
116 if (argv) free(argv);
117 return rc;