#include #include #include #include #include #include #include #include static Result> split_command_into_argv(const char* cmd) { char* str = strdup(cmd); Vector result; char* segment = strtok(str, " \n"); TRY(result.try_append(segment)); if (segment == NULL) return result; for (;;) { segment = strtok(NULL, " \n"); TRY(result.try_append(segment)); if (segment == NULL) return result; } return result; } int main() { while (1) { fputs("sh$ ", stdout); char command[4096]; char* rc = fgets(command, sizeof(command), stdin); if (!rc) return 0; if (strspn(command, " \n") == strlen(command)) continue; pid_t child = fork(); if (child < 0) { perror("fork"); return 1; } if (child == 0) { Vector argv; bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno); if (!ok) { perror("failed to parse command"); return 1; } if (argv[0] == NULL) return 0; execvp(argv[0], argv.data()); perror(argv[0]); return 1; } if (waitpid(child, NULL, 0) < 0) { perror("waitpid"); return 1; } } }