execvp - LS:fts_open:没有这样的文件或目录 [英] execvp - ls: fts_open: No such file or directory
问题描述
我目前正在与此错误挣扎。我在写一个shell模拟器,使用fork()的使用execvp执行命令();.
我几乎尝试解析到我的壳的每一个命令正在完美,除了不带参数LS。
如果我尝试执行ls -lah,一切正常,但一个简单的LS不会,收到以下错误:
LS:fts_open:没有这样的文件或目录
下面是我的code的一个片段:(只是必需的)
PID =叉();
如果(PID == 0)
{
看跌期权(子);
看跌期权(参数[0]);
看跌期权(参数[1]);
的printf(%d个\\ N的strlen(参数[1]));
ARGS [2] = NULL;
execvp(参数[0],参数);
}
否则等待(NULL);
看跌期权(返回上一级);
}
和这里是输出LS:
LS
儿童
LS0
LS:fts_open:没有这样的文件或目录
返回上一级
你可以看到,ARGS [0]包含LS,ARGS [1]是空的,因为没有参数,args来长[1]为0,但我不断收到错误。
这可能是什么你知道吗?
编辑:
荣誉乔纳森莱弗勒寻找出来:(还,现在看来似乎只是在Mac上一个问题)
的一点是,ARGS [1]不是真正空的,所以,在OS试图打开''文件,其中,显然,不存在,并且,由是什么样子,不能创建,因为这是不是一个真正的名字。
所以,这里是我所做的:检查args来LEN [1]。如果是0,将其设置为NULL。 (只是释放内存并没有真正帮助)
PID =叉();
如果(PID == 0)
{
看跌期权(子);
看跌期权(参数[0]);
看跌期权(参数[1]);
如果(strlen的(参数[1])== 0)
ARGS [1] = 0;
ARGS [2] = NULL;
execvp(参数[0],参数);
}
否则等待(NULL);
看跌期权(返回上一级);
}
如果没有更多的参数,指针为空,而不是一个非空指针为零长度的字符串。
PID =叉();
如果(PID == 0)
{
看跌期权(子);
看跌期权(参数[0]);
fflush(标准输出);
ARGS [1] = 0;
execvp(参数[0],参数);
fprintf中(标准错误,无法给exec%S \\ n,ARGS [0]);
出口(1);
}
其他
等待(NULL);
看跌期权(返回上一级);
只是出于好奇纯正,我想这在我的命令行:
$ ls'的'
LS:fts_open:没有这样的文件或目录
$
您在Mac上运行吗? (说我很惊讶地看到相同的消息没有开始来形容我的反应!)什么是更耐人寻味的是,创建一个文件 fts_open
似乎并没有摆脱的错误消息。通过 LS怪异的行为
,但在应对一个无效的请求(有一些是空字符串没有文件名)。
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 shell is working perfectly, except for ls without arguments. If I try to execute ls -lah, everything works, but a simple ls won't, receiving the following error:
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
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.
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)
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.
So, Here is what I did: check the len of args[1]. If it is 0, set it to 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.
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).
这篇关于execvp - LS:fts_open:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!