【问题标题】:Parsing input with strtok in C在 C 中使用 strtok 解析输入
【发布时间】:2012-10-01 21:03:26
【问题描述】:

我有一个项目到期,需要我在 C 中构建一个简单的 shell。我是 C 新手,我现在面临的问题是在发送命令执行之前正确解析命令.我知道有几种不同的方法可以做到这一点,但我们需要使用 strtok,而且我似乎遇到了一些问题。

这是我的全部代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

int parseCommands(char *command, char **args){
    int pos = 0;
    printf("%s\n", command);
    char *readCommand = NULL;
    char delims[] = {" \n"};
    readCommand = strtok(command, delims);
    while(readCommand != '\0'){
        printf("%s\n", readCommand); 
        strcpy(args[pos], readCommand);
        printf("%s\n", args[pos]);
        readCommand = strtok(NULL, delims);
        pos++;
    }
    return pos;
}

int executeCommand(char **args){
    pid_t pID;
    switch(pID = fork()){
        case 0:
            execvp(args[0], args);
            perror("Some sort of exec error");
            return -1;
        case -1:
            perror("Could not even fork");
            return -2;
        default:
            wait(NULL);
            return 0;
    }
}

void main(){

    char wd[256];
    char input[256];
    char *args[15];

    char strDelims[] = ";";
    char *readInput = NULL;

    while(1){

        getcwd(wd, sizeof wd);
        printf("mysh: %s> ", wd);
        fgets(input, sizeof input, stdin);

        readInput = strtok(input, strDelims);

        int numArgs;
        numArgs = parseCommands(readInput, args);
            if(numArgs < 1)
                printf("There was a problem parsing the command\n");

            if(strcmp(args[0], "cd") == 0){
                printf("%d\n", numArgs);
                if(numArgs > 1){
                    if((chdir(args[1])) < 0){
                        perror("I'm afraid I can't let you do that Dave\n");
                    }
                }
                else{
                    if((chdir(getenv("HOME"))) < 0){
                        perror("Can't go home\n");
                    }
                }
            }
            else if(strcmp(readInput, "quit") == 0){
                break;
            }
            else{
                if((executeCommand(args)) != 0)
                    printf("Problem executing the command\n");
            }
            readInput = strtok(input, strDelims);
    }

}

这是几个命令的输出:

mysh: /path/to/stuff> cd
cd

cd
cd
1
mysh: /path/to/home> cd /bin
cd /bin

cd
cd
/bin
/bin
2
mysh: /bin> ls
ls

ls
ls
Segmentation fault
mysh: /path/to/stuff> ps aux
ps aux

ps
ps
aux
aux
Segmentation fault

我只是觉得奇怪的是cd 似乎工作得相当好,但它并不真正喜欢其他任何东西。这让我认为稍后会出现问题(但在 numArgs 的 printf 之前?)。仅供参考,我们被告知每个命令不会超过 15 个参数或 256 个字符。

这让我沮丧了一段时间,所以对于这个特定问题的任何帮助都会很棒(我意识到那里还有其他错误或糟糕的代码部分,但我想自己解决/修复这些问题) .非常感谢! :)

【问题讨论】:

  • 作业标签已弃用。请不要使用它。

标签: c parsing strtok execvp


【解决方案1】:

一些提示

通常 strtok 共享一个静态缓冲区,所以当你先调用 strtok 之前 您的 parse 函数,然后在 parse 函数中,您可能会搞砸 之前缓冲区中的内容。

您也没有为 args[] 分配空间,您只声明了一个指针数组(char *args[15]) 但是然后在您的 parse 函数中,您对指针指向的任何内容执行 strcpy 。 您需要分配一个缓冲区,分配给 args[i],然后将字符串复制到其中。

所以而不是

strcpy(args[pos], readCommand);

args[pos] = strdup(readCommand);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多