| 294 | } |
| 295 | |
| 296 | int handle_line(char *line) |
| 297 | { |
| 298 | if (strcmp(line, "exit") == 0) |
| 299 | { |
| 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | char *buffer = NULL; |
| 304 | char **cmds = (char **)malloc(sizeof(char *) * 2); |
| 305 | |
| 306 | buffer = strtok(line, "&"); |
| 307 | size_t cmds_c = 0; |
| 308 | |
| 309 | while (buffer != NULL) |
| 310 | { |
| 311 | |
| 312 | if (buffer[strlen(buffer) - 1] == ' ') // If last char is a space remove it |
| 313 | { |
| 314 | buffer[strlen(buffer) - 1] = 0; |
| 315 | } |
| 316 | |
| 317 | if (buffer[0] == ' ') // If first char is a space remove it |
| 318 | { |
| 319 | buffer++; |
| 320 | } |
| 321 | |
| 322 | // Put string containing cmd and parameters into array |
| 323 | cmds[cmds_c] = (char *)malloc(sizeof(char) * strlen(buffer) + 1); |
| 324 | strcpy(cmds[cmds_c++], buffer); |
| 325 | cmds = (char **)realloc(cmds, (cmds_c + 1) * sizeof(char *)); |
| 326 | buffer = strtok(NULL, "&"); |
| 327 | } |
| 328 | |
| 329 | // Start all processes with `handle_cmd` function |
| 330 | for (size_t i = 0; i < cmds_c; i++) |
| 331 | { |
| 332 | handle_cmd(cmds[i]); |
| 333 | } |
| 334 | |
| 335 | int status; |
| 336 | pid_t wpid; |
| 337 | while ((wpid = wait(&status)) > 0) // Wait for all child processes to finish before continuing |
| 338 | { |
| 339 | } |
| 340 | |
| 341 | free_char_arr(cmds, cmds_c); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | void interactive_mode() |
| 346 | { |
no test coverage detected