#include #include #include #include /* TODO */ static int run_cmd(const char *cmd, int argc, char *argv[]) { puts(cmd); for (int i = 0; i < argc; i++) { puts(argv[i]); } return 0; } /* Tricks with pointers */ int parse(char *str) { char *cmd = str; str += strcspn(str, separator); if (*str) { *str++ = 0; } char *str_counter = str; int i = 0; for (; *str_counter; i++) { str_counter += strspn(str, separator); if (!*str_counter) { break; } *str_counter++ = 0; str_counter += strcspn(str_counter, separator); if(!str) { return EINVAL; } *str++ = 0; } char *args[i]; for (int j = 0; *str; j++) { str += strspn(str, separator); if (!*str) { break; } *str++ = 0; args[j] = str; str += strcspn(str, separator); if(!str) { return EINVAL; } *str++ = 0; /* TODO: parse the string left to an array of arguments and pass command * itself and arguments to the run_cmd() right after end of this loop */ } int response = run_cmd(cmd, i, args); return response; }