execvp - ls: fts_open: No such file or directory

前端 未结 1 475
甜味超标
甜味超标 2021-01-07 02:28

I\'m currently struggling with this error. I\'m writing a shell emulator, using fork() for executing a command using execvp();. Almost every command I try to parse to my she

相关标签:
1条回答
  • 2021-01-07 02:38

    If there are no more arguments, the pointer should be null, not a non-null pointer to a zero length string.

    pid = fork();
    if (pid==0)
    {
        puts("CHILD");
        puts(args[0]);
        fflush(stdout);
        args[1] = 0;
        execvp(args[0], args);
        fprintf(stderr, "Failed to exec %s\n", args[0]);
        exit(1);
    }
    else
        wait(NULL);
    puts("BACK TO PARENT");
    

    Just out of pure curiosity, I tried this at my command line:

    $ ls ''
    ls: fts_open: No such file or directory
    $
    

    Are you running on a Mac too? (To say I was surprised to see the same message doesn't begin to describe my reaction!) What's more intriguing is that creating a file fts_open doesn't seem to get rid of the error message. Weird behaviour by ls, but in response to an invalid request (there are no file names that are the empty string).

    0 讨论(0)
提交回复
热议问题