简体   繁体   English

ls: 无法访问 'PATH=./': 没有这样的文件或目录

[英]ls: cannot access 'PATH=./': No such file or directory

I'm new to using linux and the bash command line but I was trying to see whether I could execute basic bash commands (or program) from a c file. I'm new to using linux and the bash command line but I was trying to see whether I could execute basic bash commands (or program) from a c file. My code is really simple and I could've sworn that it was working but suddenly it is not acknowledging that the path even exists.我的代码非常简单,我可以发誓它正在工作,但突然间它甚至不承认路径存在。

char* arg[] = {"ls", "-l"};
char* environ[] = {"PATH=./", (char*)0};
execve("/bin/ls", arg, environ);/*execute the command 'ls -l' in curdir*/

I've tried to set as PATH=/bin but it's not even executing the command in that directory.我试图设置为PATH=/bin但它甚至没有执行该目录中的命令。 it just returns a similar error.它只是返回一个类似的错误。

NOTE: I have tried注意:我已经尝试过

char* environ[] = {"PATH=./", NULL};

I've even tried using the envp from main() and that STILL doesn't work.我什至尝试过使用 main() 中的envp ,但仍然无法正常工作。

Put NULL in the end of arg array.将 NULL 放在 arg 数组的末尾。

char* arg[] = {"ls", "-l", NULL};

The NULL is used to mark the end of the array. NULL 用于标记数组的结束。

This error message...此错误消息...

 ls: cannot access 'PATH=./': No such file or directory

... indicates that the ls utility is attempting to access a file named "PATH=./", which it does not find. ... 表示ls实用程序正在尝试访问名为“PATH=./”的文件,但它没有找到该文件。 That is a manifestation of the undefined behavior arising from your code...这是您的代码引起的未定义行为的表现......

 char* arg[] = {"ls", "-l"}; char* environ[] = {"PATH=./", (char*)0}; execve("/bin/ls", arg, environ);/*execute the command 'ls -l' in curdir*/

... on account of execve() expecting and relying upon the argument list to which arg points being terminated by a null pointer. ...由于execve()期望并依赖于arg指向的参数列表,该参数列表被 null 指针终止。

Although it is somewhat fraught to try to rationalize or interpret undefined behavior, you can imagine that the contents of arrays arg and environ are laid out one immediately after the other in memory, so that the combined representation of these two is the same as the representation of a four-element array of char * .虽然试图合理化或解释未定义的行为有点令人担忧,但您可以想象 arrays argenviron的内容在 memory 中紧挨着一个接一个地布置,因此这两者的组合表示与表示相同char *的四元素数组。 This view is perhaps useful for understanding why the arg and env arrays must be terminated by null pointers in the first place.这个视图可能有助于理解为什么argenv arrays 首先必须由 null 指针终止。

The fix is to append a null pointer to the value of arg :修复是 append 指向arg值的 null 指针:

char* arg[] = {"ls", "-l", NULL};

Note also that in this particular case there is no apparent advantage to specifying an environment, so you could simplify by using execv() instead.另请注意,在这种特殊情况下,指定环境没有明显优势,因此您可以改用execv()来简化。

Note, too, that path elements other than / itself should not contain a trailing / character.还要注意,除/本身之外的路径元素不应包含尾随/字符。 This is not part of a correct name for a directory, and although you can often get away with it, it is poor form.这不是正确的目录名称的一部分,尽管您通常可以侥幸逃脱,但它的形式很差。

Please always post a complete program.请始终发布完整的程序。

As said in the man page, and as said in the comments above, you need a NULL at the end of the arguments list.如手册页中所述,以及上述评论中所述,您需要NULL列表末尾的 NULL。

You do not need to pass anything in envp[] since you are running just ls您不需要在envp[]中传递任何内容,因为您只运行ls

By convention you should pass the full path of the executable in argv[0]按照惯例,您应该在argv[0]中传递可执行文件的完整路径

In short, this works简而言之,这行得通

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    char* arg[] = { "/bin/ls", "-l", NULL };
    execve("/bin/ls", arg, NULL);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM