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).