Я пишу оболочку с командой «эхо». Например, если пользователь вводит «echo hello world», оболочка выводит «hello world».
Мой код ниже.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int MAX_INPUT_SIZE = 200;
char input[MAX_INPUT_SIZE];
char *command;
printf("shell> ");
fgets(input, MAX_INPUT_SIZE, stdin);
//find first word
char *space;
space = strtok(input, " ");
command = space;
// printf("command: %s\n",command);
//echo command
if (strncmp(command, "echo", MAX_INPUT_SIZE) == 0) {
while (space != NULL) {
space = strtok(NULL, " ");
printf("%s ", space);
}
}
return (EXIT_SUCCESS);
}
Когда я запускаю это, с вводом
echo hello world
оболочка распечатывает
hello world
(null)
Я не понимаю, почему печатается (null). Любые идеи?
Спасибо заранее за ваше время!