Yesterday's fix makes argcv_get include trailing
whitespace to the argv array. To fix: (argcv_scan): Return len+1 if called on or after the terminating null character. (argcv_get): Initialize *argc to zero and continue scanning until argcv_scan return is > len. Thanks Sam for noticing.
Showing
1 changed file
with
6 additions
and
8 deletions
... | @@ -19,8 +19,6 @@ | ... | @@ -19,8 +19,6 @@ |
19 | 19 | ||
20 | #include "argcv.h" | 20 | #include "argcv.h" |
21 | 21 | ||
22 | char srtime[] = __TIME__; | ||
23 | |||
24 | /* | 22 | /* |
25 | * takes a string and splits it into several strings, breaking at ' ' | 23 | * takes a string and splits it into several strings, breaking at ' ' |
26 | * command is the string to split | 24 | * command is the string to split |
... | @@ -43,7 +41,7 @@ argcv_scan (int len, const char *command, const char *delim, const char* cmnt, | ... | @@ -43,7 +41,7 @@ argcv_scan (int len, const char *command, const char *delim, const char* cmnt, |
43 | i = *save; | 41 | i = *save; |
44 | 42 | ||
45 | if (i >= len) | 43 | if (i >= len) |
46 | return i; | 44 | return i + 1; |
47 | 45 | ||
48 | /* Skip initial whitespace */ | 46 | /* Skip initial whitespace */ |
49 | while (i < len && isws (command[i])) | 47 | while (i < len && isws (command[i])) |
... | @@ -102,10 +100,10 @@ argcv_get (const char *command, const char *delim, const char* cmnt, | ... | @@ -102,10 +100,10 @@ argcv_get (const char *command, const char *delim, const char* cmnt, |
102 | *argv = NULL; | 100 | *argv = NULL; |
103 | 101 | ||
104 | /* Count number of arguments */ | 102 | /* Count number of arguments */ |
105 | *argc = 1; | 103 | *argc = 0; |
106 | save = 0; | 104 | save = 0; |
107 | 105 | ||
108 | while (argcv_scan (len, command, delim, cmnt, &start, &end, &save) < len) | 106 | while (argcv_scan (len, command, delim, cmnt, &start, &end, &save) <= len) |
109 | (*argc)++; | 107 | (*argc)++; |
110 | 108 | ||
111 | *argv = calloc ((*argc + 1), sizeof (char *)); | 109 | *argv = calloc ((*argc + 1), sizeof (char *)); |
... | @@ -198,14 +196,14 @@ argcv_string (int argc, char **argv, char **pstring) | ... | @@ -198,14 +196,14 @@ argcv_string (int argc, char **argv, char **pstring) |
198 | } | 196 | } |
199 | 197 | ||
200 | #if 0 | 198 | #if 0 |
201 | char *command = "set prompt=\"& \""; | 199 | char *command = "set prompt=\"& \" "; |
202 | 200 | ||
203 | main() | 201 | main(int xargc, char **xargv) |
204 | { | 202 | { |
205 | int i, argc; | 203 | int i, argc; |
206 | char **argv; | 204 | char **argv; |
207 | 205 | ||
208 | argcv_get (command, "=", NULL, &argc, &argv); | 206 | argcv_get (xargv[1] ? xargv[1]:command, "=", "#", &argc, &argv); |
209 | printf ("%d args:\n", argc); | 207 | printf ("%d args:\n", argc); |
210 | for (i = 0; i < argc; i++) | 208 | for (i = 0; i < argc; i++) |
211 | printf ("%s\n", argv[i]); | 209 | printf ("%s\n", argv[i]); | ... | ... |
-
Please register or sign in to post a comment