Changeset 2781 in kBuild for trunk/src/kash
- Timestamp:
- May 9, 2015 6:52:15 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/shinstance.c
r2652 r2781 1135 1135 *p = '\0'; 1136 1136 1137 /* Create the command line. */ 1137 /* Figure the size of the command line. Double quotes makes this 1138 tedious and we overestimate to simplify. */ 1138 1139 cmdline_size = 2; 1139 1140 for (i = 0; argv[i]; i++) 1140 cmdline_size += strlen(argv[i]) + 3; 1141 { 1142 const char *arg = argv[i]; 1143 cmdline_size += strlen(arg) + 3; 1144 arg = strchr(arg, '"'); 1145 if (arg) 1146 { 1147 do 1148 cmdline_size++; 1149 while ((arg = strchr(arg + 1, '"')) != NULL); 1150 arg = argv[i] - 1; 1151 while ((arg = strchr(arg + 1, '\\')) != NULL); 1152 cmdline_size++; 1153 } 1154 } 1155 1156 /* Create the command line. */ 1141 1157 cmdline = p = sh_malloc(psh, cmdline_size); 1142 1158 for (i = 0; argv[i]; i++) 1143 1159 { 1144 size_t len = strlen(argv[i]); 1145 int quoted = !!strpbrk(argv[i], " \t"); /** @todo Do this quoting business right. */ 1160 const char *arg = argv[i]; 1161 const char *cur = arg; 1162 size_t len = strlen(arg); 1163 int quoted = 0; 1164 char ch; 1165 while ((ch = *cur++) != '\0') 1166 if (ch <= 0x20 || strchr("&><|%", ch) != NULL) 1167 { 1168 quoted = 1; 1169 break; 1170 } 1171 1146 1172 if (i != 0) 1147 1173 *(p++) = ' '; 1148 1174 if (quoted) 1149 1175 *(p++) = '"'; 1150 memcpy(p, argv[i], len); 1151 p += len; 1176 if (memchr(arg, '"', len) == NULL) 1177 { 1178 memcpy(p, arg, len); 1179 p += len; 1180 } 1181 else 1182 { /* MS CRT style: double quotes must be escaped; backslashes 1183 must be escaped if followed by double quotes. */ 1184 while ((ch = *arg++) != '\0') 1185 if (ch != '\\' && ch != '"') 1186 *p++ = ch; 1187 else if (ch == '"') 1188 { 1189 *p++ = '\\'; 1190 *p++ = '"'; 1191 } 1192 else 1193 { 1194 unsigned slashes = 1; 1195 *p++ = '\\'; 1196 while (*arg == '\\') 1197 { 1198 *p++ = '\\'; 1199 slashes++; 1200 arg++; 1201 } 1202 if (*arg == '"') 1203 { 1204 while (slashes-- > 0) 1205 *p++ = '\\'; 1206 *p++ = '\\'; 1207 *p++ = '"'; 1208 arg++; 1209 } 1210 } 1211 } 1152 1212 if (quoted) 1153 1213 *(p++) = '"';
Note:
See TracChangeset
for help on using the changeset viewer.