Создание простой оболочки в Linux - PullRequest
0 голосов
/ 23 октября 2018

Я работаю над заданием по созданию чрезвычайно простой оболочки Linux на C, и она работает почти так же, как я этого хочу.

Если пользователь вводит простую команду Linux, программа запустит ееи цикл, чтобы разрешить другую команду.Если пользователь вводит «quit», программа завершает работу.

Моя проблема в том, что команды работают только в первый раз.После этого они как-то неправильно форматируются.Есть ли способ, которым я могу повторно инициализировать мой массив args, чтобы он правильно получил новый ввод?

int main() {
    char* args[50];          // Argument array.
    char userInput[200];     // User input.
    char* userQuit = "quit"; // String to be compared to user input to quit program.
    int pid;                 // Process ID for fork().
    int i = 0;               // Counter.

    while(1) {
        // Promt and get input from user.
        printf("minor5> ");
        fgets(userInput, sizeof(userInput), stdin);

        // Pass userInput into args array.
        args[0] = strtok(userInput, " \n\0");

        // Loop to separate args into individual arguments, delimited by either space, newline, or NULL.
        while(args[i] != NULL) {
            i++;
            args[i] = strtok(NULL, " \n\0");
        }

        // If the first argument is "quit", exit the program.
        if(strcmp(args[0], userQuit) == 0) {
            printf("Exiting Minor5 Shell...\n");
            exit(EXIT_SUCCESS);
        }

        // Create child process.
        pid = fork();

        // Parent process will wait for child to execute.
        // Child process will execute the command given in userInput.
        if(pid > 0) {
            // Parent //
            wait( (int *) 0 );
        } else {
            // Child //
            int errChk;
            errChk = execvp(args[0], args);

            if(errChk == -1) {
                printf("%s: Command not found\n", userInput);
            }
        }
    }

    return 0;
}

1 Ответ

0 голосов
/ 23 октября 2018

Вам необходимо убедиться, что , что args имеет последнее значение NULL.Вероятно, он был один по команде first , случайно, но без гарантии

Вот переработанный фрагмент вашего цикла синтаксического анализа [прошу прощения за чистую стирку]:

// Pass userInput into args array.
char *uptr = userInput;

i = 0;
while (1) {
    char *token = strtok(uptr, " \n");
    uptr = NULL;

    if (token == NULL)
        break;

    args[i++] = token;
}

// NOTE: this is the key missing ingredient from your code
args[i] = NULL;
...