使用execvp()的问题

时间:2013-03-17 12:08:18

标签: c parsing arguments execvp

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

extern int errno;

void parseArgs(char *path, char *argv[]) {
    while (*path != '\0') {
        while (*path == ' ' || *path == '\t' || *path == '\n') {
            *path++ = '\0';
        }
        *argv++ = path;
        while (*path != '\0' && *path != ' ' && *path != '\t' && *path != '\n') {
            path++;
        }
    }
    *argv = '\0';
}

int execArgs(char *argv[], int amp) {
    pid_t process;
    int check;

    if ((process = fork()) < 0) {
        fprintf(stderr, "Forking child failed\n%s\n", strerror(errno));
        errno = 0;
        return EXIT_FAILURE;
    } else if (process == 0) {
        if (execvp(*argv, argv) < 0) {
            fprintf(stderr, "Execution failed\n%s\n", strerror(errno));
            errno = 0;
            return EXIT_FAILURE;
        }
    } else {
        while (wait(&check) != process) {
            //do nothing
        }
    }
    return EXIT_SUCCESS;
}

int main (void) {
    char path[1024];
    char *argv[64];
    //int amp = 0;

    while (1) {
        printf("[root@localhost]$ ");
        fgets(path, 1024, stdin);
        puts("");
        parseArgs(path, argv);
        if (strcmp(argv[0], "exit") == 0) {
            return EXIT_SUCCESS;
        }
        execArgs(argv, 0);
    }
}

我在这里遇到问题。这个问题似乎是一个解析错误,因为每当我尝试ls一个目录时,我都会得到ls: cannot access : No such file or directory。奇怪的是,如果我去ls ..我得到:

ls: cannot access : No such file or directory
..:
cat  cat.c  cat.c~  m4

注意:以上是dir级别的内容!

此外 - 我怀疑这些问题是相互关联的 - 我不能cd进入目录,但是我不能100%确定这一点,因为它不会抛出像目录那样不存在的东西;我只是得到一个空白区域(但我的cwd没有更新)。

任何帮助将不胜感激!

参考,一些测试输入

[thanasi@localhost m4]$ ./cli
[root@localhost /home/thanasi/Systems Programming/m4]$ ls

ls: cannot access : No such file or directory
[root@localhost /home/thanasi/Systems Programming/m4]$ ls ..

ls: cannot access : No such file or directory
..:
cat  cat.c  cat.c~  m4
[root@localhost /home/thanasi/Systems Programming/m4]$ cd test

[root@localhost /home/thanasi/Systems Programming/m4]$ cd /test           

/usr/bin/cd: line 2: cd: /test: No such file or directory
[root@localhost /home/thanasi/Systems Programming/m4]$ ^C

确认目录'测试'确实存在:

[thanasi@localhost m4]$ ls -al
total 36
drwxrwxr-x. 3 thanasi thanasi  4096 Mar 17 22:00 .
drwxrwxr-x. 3 thanasi thanasi  4096 Mar 17 20:36 ..
-rwxrwxr-x. 1 thanasi thanasi 13175 Mar 17 22:00 cli
-rw-rw-r--. 1 thanasi thanasi  1222 Mar 17 22:00 cli.c
-rw-rw-r--. 1 thanasi thanasi  1221 Mar 17 22:00 cli.c~
drwxr-xr-x. 2 root    root     4096 Mar 17 21:32 test

1 个答案:

答案 0 :(得分:3)

通常,您从fgets获得的输入以换行符结尾。假设您{'l', 's', '\n', '\0'}path中的前四个字节。然后,当parseArgsargv[0]设置为指向path中的第一个字符时,下一次迭代将使用'\0'覆盖换行符,并设置argv[1] = &path[2];:< / p>

void parseArgs(char *path, char *argv[]) {
    while (*path != '\0') {
        while (*path == ' ' || *path == '\t' || *path == '\n') {
            *path++ = '\0';
        }
        *argv++ = path;
        while (*path != '\0' && *path != ' ' && *path != '\t' && *path != '\n') {
            path++;
        }
    }
    *argv = '\0';
}

所以你的argv数组包含一个指向最后一个真实参数和终止数组的NULL之间的空字符串的指针。因此ls会尝试列出""的内容 - 这些内容不存在。

由于您无法确定从fgets获得的输入是否以换行结尾,因此请保护作业

if (*path) *argv++ = path;

要避免这种情况。

相关问题