Commit 2fe0ed35 2fe0ed35e470dfe7f8983d1dbaf50644b86f3e4d by Sergey Poznyakoff

Ignore trailing whitespace and terminate output array with a NULL entry.

1 parent 54dac6a0
...@@ -52,10 +52,10 @@ argcv_scan (int len, const char *command, const char *delim, ...@@ -52,10 +52,10 @@ argcv_scan (int len, const char *command, const char *delim,
52 if (isdelim (command [i], delim)) 52 if (isdelim (command [i], delim))
53 break; 53 break;
54 /* Skip until next whitespace character or end of line */ 54 /* Skip until next whitespace character or end of line */
55 while (++i < len && !(isws (command [i]) || isdelim (command [i], delim))) 55 while (++i < len &&
56 !(isws (command [i]) || isdelim (command [i], delim)))
56 ; 57 ;
57 if (i < len) 58 i--;
58 i--;
59 break; 59 break;
60 } 60 }
61 61
...@@ -68,11 +68,14 @@ int ...@@ -68,11 +68,14 @@ int
68 argcv_get (const char *command, const char *delim, int *argc, char ***argv) 68 argcv_get (const char *command, const char *delim, int *argc, char ***argv)
69 { 69 {
70 int len = strlen (command); 70 int len = strlen (command);
71 int i = 0, j = 0; 71 int i = 0;
72 int start, end, save; 72 int start, end, save;
73 73
74 *argc = 0; 74 *argc = 0;
75 *argv = NULL; 75 *argv = NULL;
76
77 while (len > 0 && isspace (command[len-1]))
78 len--;
76 if (len < 1) 79 if (len < 1)
77 return 1; 80 return 1;
78 81
...@@ -86,7 +89,7 @@ argcv_get (const char *command, const char *delim, int *argc, char ***argv) ...@@ -86,7 +89,7 @@ argcv_get (const char *command, const char *delim, int *argc, char ***argv)
86 89
87 i = 0; 90 i = 0;
88 save = 0; 91 save = 0;
89 do 92 for (i = 0; i < *argc; i++)
90 { 93 {
91 int n; 94 int n;
92 argcv_scan (len, command, delim, &start, &end, &save); 95 argcv_scan (len, command, delim, &start, &end, &save);
...@@ -107,9 +110,8 @@ argcv_get (const char *command, const char *delim, int *argc, char ***argv) ...@@ -107,9 +110,8 @@ argcv_get (const char *command, const char *delim, int *argc, char ***argv)
107 return 1; 110 return 1;
108 memcpy ((*argv)[i], &command[start], n); 111 memcpy ((*argv)[i], &command[start], n);
109 (*argv)[i][n] = 0; 112 (*argv)[i][n] = 0;
110 i++;
111 } 113 }
112 while (end < len); 114 (*argv)[i] = NULL;
113 return 0; 115 return 0;
114 } 116 }
115 117
......