[英]execvp - ls: fts_open: No such file or directory
I'm currently struggling with this error. 我目前正在努力解决此错误。 I'm writing a shell emulator, using fork() for executing a command using execvp();.
我正在编写一个shell模拟器,使用fork()使用execvp();执行命令。 Almost every command I try to parse to my shell is working perfectly, except for ls without arguments.
除了没有参数的ls之外,几乎所有我尝试解析到我的shell的命令都运行良好。 If I try to execute ls -lah, everything works, but a simple ls won't, receiving the following error:
如果我尝试执行ls -lah,则一切正常,但是一个简单的ls不会,并收到以下错误:
ls: fts_open: No such file or directory
Here is a snippet of my code: (just the essential) 这是我的代码段:(仅此而已)
pid = fork();
if (pid==0)
{
puts("CHILD");
puts(args[0]);
puts(args[1]);
printf("%d\n", strlen(args[1]));
args[2] = NULL;
execvp(args[0], args);
}
else wait(NULL);
puts("BACK TO PARENT");
}
and here is the output for ls: 这是ls的输出:
ls
CHILD
ls
0
ls: fts_open: No such file or directory
BACK TO PARENT
as you can see, args[0] contains ls, args[1] is empty, as there are no arguments, the length of args[1] is 0, but i keep getting the error. 如您所见,args [0]包含ls,args [1]为空,因为没有参数,args [1]的长度为0,但我一直收到错误消息。
Any idea on what it could be? 有什么想法吗?
EDIT: 编辑:
Kudos for Jonathan Leffler for finding it out: (also, it seems like it is just an issue on Mac) 乔纳森·莱夫勒(Jonathan Leffler)的荣誉:(同样,这似乎在Mac上只是一个问题)
The point is that args[1] is not really empty, so, the OS tries to open the '' file, which, obviously, does not exists, and, by what is looks, can't be created, since it is not really a name. 关键是args [1]并非真正为空,因此,操作系统尝试打开''文件,该文件显然不存在,并且从外观上看无法创建,因为它不是真是个名字。
So, Here is what I did: check the len of args[1]. 所以,这就是我所做的:检查args [1]的len。 If it is 0, set it to NULL.
如果为0,则将其设置为NULL。 (just freeing the memory did not really helped)
(只是释放内存并没有真正帮助)
pid = fork();
if (pid==0)
{
puts("CHILD");
puts(args[0]);
puts(args[1]);
if (strlen(args[1]) == 0)
args[1] = 0;
args[2] = NULL;
execvp(args[0], args);
}
else wait(NULL);
puts("BACK TO PARENT");
}
If there are no more arguments, the pointer should be null, not a non-null pointer to a zero length string. 如果没有更多参数,则指针应为null,而不是指向零长度字符串的非null指针。
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? 您也在Mac上运行吗? (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. (要说我惊讶地看到同一条消息没有开始描述我的反应!)更令人着迷的是,创建文件
fts_open
似乎并没有摆脱错误消息。 Weird behaviour by ls
, but in response to an invalid request (there are no file names that are the empty string). ls
奇怪的行为,但是响应无效的请求(没有文件名是空字符串)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.