~ubuntu-branches/ubuntu/intrepid/popt/intrepid

« back to all changes in this revision

Viewing changes to poptparse.c

  • Committer: Bazaar Package Importer
  • Author(s): Paul Martin
  • Date: 2008-06-25 07:25:34 UTC
  • mfrom: (4.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080625072534-sc91ls37wk5gqrbq
Tags: 1.14-4
Remove locale data from the udeb, and change the package 
description. The debian-installer developers are sure that they 
don't need the localization information, mainly error messages.
(Thanks to Frans Pop for bringing this to my attention.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
#define POPT_ARGV_ARRAY_GROW_DELTA 5
12
12
 
13
 
/*@-boundswrite@*/
14
13
int poptDupArgv(int argc, const char **argv,
15
14
                int * argcPtr, const char *** argvPtr)
16
15
{
32
31
        return POPT_ERROR_MALLOC;
33
32
    argv2 = (void *) dst;
34
33
    dst += (argc + 1) * sizeof(*argv);
 
34
    *dst = '\0';
35
35
 
36
 
    /*@-branchstate@*/
37
36
    for (i = 0; i < argc; i++) {
38
37
        argv2[i] = dst;
39
 
        dst += strlen(strcpy(dst, argv[i])) + 1;
 
38
        dst = stpcpy(dst, argv[i]);
 
39
        dst++;  /* trailing NUL */
40
40
    }
41
 
    /*@=branchstate@*/
42
41
    argv2[argc] = NULL;
43
42
 
44
43
    if (argvPtr) {
51
50
        *argcPtr = argc;
52
51
    return 0;
53
52
}
54
 
/*@=boundswrite@*/
55
53
 
56
 
/*@-bounds@*/
57
54
int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
58
55
{
59
56
    const char * src;
61
58
    int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
62
59
    const char ** argv = malloc(sizeof(*argv) * argvAlloced);
63
60
    int argc = 0;
64
 
    int buflen = strlen(s) + 1;
65
 
    char * buf = memset(alloca(buflen), 0, buflen);
 
61
    size_t buflen = strlen(s) + 1;
 
62
    char * buf, * bufOrig = NULL;
66
63
    int rc = POPT_ERROR_MALLOC;
67
64
 
68
65
    if (argv == NULL) return rc;
 
66
    buf = bufOrig = calloc((size_t)1, buflen);
 
67
    if (buf == NULL) {
 
68
        free(argv);
 
69
        return rc;
 
70
    }
69
71
    argv[argc] = buf;
70
72
 
71
73
    for (src = s; *src != '\0'; src++) {
81
83
                if (*src != quote) *buf++ = '\\';
82
84
            }
83
85
            *buf++ = *src;
84
 
        } else if (isspace(*src)) {
 
86
        } else if (_isspaceptr(src)) {
85
87
            if (*argv[argc] != '\0') {
86
88
                buf++, argc++;
87
89
                if (argc == argvAlloced) {
116
118
    rc = poptDupArgv(argc, argv, argcPtr, argvPtr);
117
119
 
118
120
exit:
 
121
    if (bufOrig) free(bufOrig);
119
122
    if (argv) free(argv);
120
123
    return rc;
121
124
}
122
 
/*@=bounds@*/
123
125
 
124
126
/* still in the dev stage.
125
127
 * return values, perhaps 1== file erro
126
128
 * 2== line to long
127
129
 * 3== umm.... more?
128
130
 */
129
 
int poptConfigFileToString(FILE *fp, char ** argstrp, /*@unused@*/ int flags)
 
131
int poptConfigFileToString(FILE *fp, char ** argstrp,
 
132
                /*@unused@*/ UNUSED(int flags))
130
133
{
131
134
    char line[999];
132
135
    char * argstr;
133
136
    char * p;
134
137
    char * q;
135
138
    char * x;
136
 
    int t;
137
 
    int argvlen = 0;
 
139
    size_t t;
 
140
    size_t argvlen = 0;
138
141
    size_t maxlinelen = sizeof(line);
139
142
    size_t linelen;
140
 
    int maxargvlen = 480;
141
 
    int linenum = 0;
 
143
    size_t maxargvlen = (size_t)480;
142
144
 
143
145
    *argstrp = NULL;
144
146
 
153
155
    if (argstr == NULL) return POPT_ERROR_MALLOC;
154
156
 
155
157
    while (fgets(line, (int)maxlinelen, fp) != NULL) {
156
 
        linenum++;
157
158
        p = line;
158
159
 
159
160
        /* loop until first non-space char or EOL */
160
 
        while( *p != '\0' && isspace(*p) )
 
161
        while( *p != '\0' && _isspaceptr(p) )
161
162
            p++;
162
163
 
163
164
        linelen = strlen(p);
164
 
        if (linelen >= maxlinelen-1)
 
165
        if (linelen >= maxlinelen-1) {
 
166
            free(argstr);
165
167
            return POPT_ERROR_OVERFLOW; /* XXX line too long */
 
168
        }
166
169
 
167
170
        if (*p == '\0' || *p == '\n') continue; /* line is empty */
168
171
        if (*p == '#') continue;                /* comment line */
169
172
 
170
173
        q = p;
171
174
 
172
 
        while (*q != '\0' && (!isspace(*q)) && *q != '=')
 
175
        while (*q != '\0' && (!_isspaceptr(q)) && *q != '=')
173
176
            q++;
174
177
 
175
 
        if (isspace(*q)) {
 
178
        if (_isspaceptr(q)) {
176
179
            /* a space after the name, find next non space */
177
180
            *q++='\0';
178
 
            while( *q != '\0' && isspace((int)*q) ) q++;
 
181
            while( *q != '\0' && _isspaceptr(q) ) q++;
179
182
        }
180
183
        if (*q == '\0') {
181
184
            /* single command line option (ie, no name=val, just name) */
182
185
            q[-1] = '\0';               /* kill off newline from fgets() call */
183
 
            argvlen += (t = q - p) + (sizeof(" --")-1);
 
186
            argvlen += (t = (size_t)(q - p)) + (sizeof(" --")-1);
184
187
            if (argvlen >= maxargvlen) {
185
188
                maxargvlen = (t > maxargvlen) ? t*2 : maxargvlen*2;
186
189
                argstr = realloc(argstr, maxargvlen);
197
200
        *q++ = '\0';
198
201
 
199
202
        /* find next non-space letter of value */
200
 
        while (*q != '\0' && isspace(*q))
 
203
        while (*q != '\0' && _isspaceptr(q))
201
204
            q++;
202
205
        if (*q == '\0')
203
206
            continue;   /* XXX silently ignore missing value */
204
207
 
205
208
        /* now, loop and strip all ending whitespace */
206
209
        x = p + linelen;
207
 
        while (isspace(*--x))
208
 
            *x = 0;     /* null out last char if space (including fgets() NL) */
 
210
        while (_isspaceptr(--x))
 
211
            *x = '\0';  /* null out last char if space (including fgets() NL) */
209
212
 
210
213
        /* rest of line accept */
211
 
        t = x - p;
 
214
        t = (size_t)(x - p);
212
215
        argvlen += t + (sizeof("' --='")-1);
213
216
        if (argvlen >= maxargvlen) {
214
217
            maxargvlen = (t > maxargvlen) ? t*2 : maxargvlen*2;