1

I'm implementing cd functionality using system calls in a C program that acts as a simple shell, and am having a bit of an issue with directory names. I have a feeling it's trivially string related, but I haven't had any luck so far.

I have a function like the following:

static void parse_args(int argc, char **argv) {

    // Special conditions:
    if (strcmp(argv[1], "return") == 0) {
        exit(0);
    }
    else if (strcmp(argv[1], "cd") == 0) {

        if (strcmp(argv[2], "mydir") == 0) {
            printf("Directory name is 'mydir'!\n");
        } else {
            printf("Directory name is NOT 'mydir'!\n");
        }

        int ret = chdir(argv[2]);
        if (ret == 0) { // success
            printf("Able to change directory.\n");
        } else {
            printf("UNABLE to change directory.\n");
        }
    }

    // Handling piping for shell ....
}

I know up front that argv[2] is a string containing my new desired directory.

I wish to cd into an existing directory called mydir, and call changeDirectory with argv[2] equal to "mydir". However, as you can see, inside changeDirectory, I verify that argv[2] is in fact exactly equal to "mydir".

The results of my simple shell call is:

$> pwd
/foo/bar
$> cd mydir
Directory name is 'mydir'!
UNABLE to change directory.
/usr/bin/cd: line 4: cd: mydir: No such file or directory
$> pwd
/foo/bar/mydir

So it appears the command succeeds, however I don't know why I'm receiving the error message. Interestingly, this error message doesn't occur if I call int ret = chdir("mydir"); Can I cast/sanitize this parameter somehow? Any pointers would be greatly appreciated. Can I san

4

1 回答 1

1

原来我在更改目录后忘记退出该功能。稍后在函数中,我将cd其作为参数传递execvp给执行它,就好像它是一个程序一样。只需将后面的“处理管道”代码封装在一个else解决了这个问题的地方。

于 2014-10-04T22:30:11.117 回答