I want to execute the command ls -a
using execv()
on a Linux machine as follows:
我想在Linux机器上执行命令ls -a使用execv():
char *const ptr={"/bin/sh","-c","ls","-a" ,NULL};
execv("/bin/sh",ptr);
However, this command does not list hidden files. What am I doing wrong?
但是,该命令不列出隐藏的文件。我做错了什么?
2 个解决方案
#1
9
I'm not sure why you're passing this via /bin/sh
... but since you are, you need to pass all the arguments after -c
as a single value because these are now to be interpreted by /bin/sh
.
我不知道你为什么要通过/bin/ sh…但是由于您是这样的,所以您需要将-c之后的所有参数作为单个值传递,因为这些参数现在将由/bin/ sh来解释。
The example is to compare the shell syntax of
例子是比较shell语法
/bin/sh -c ls -a
to
来
/bin/sh -c 'ls -a'
The second works, but the first doesn't.
第二个行得通,但第一个行不通。
So your ptr
should be defined as
所以ptr应该被定义为
char * const ptr[]={"/bin/sh","-c","ls -a" ,NULL};
#2
5
If you need to get the contents of a directory from a c program, then this is not the best way - you will effectively have to parse the output of ls
, which is generally considered a bad idea.
如果您需要从c程序获取目录的内容,那么这不是最好的方法——您必须有效地解析ls的输出,这通常被认为是一个坏主意。
Instead you can use the libc
functions opendir()
and readdir()
to achieve this.
相反,您可以使用libc函数opendir()和readdir()来实现这一点。
Here is a small example program that will iterate over (and list) all files in the current directory:
下面是一个小示例程序,它将遍历(并列出)当前目录中的所有文件:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
int main (int argc, char **argv) {
DIR *dirp;
struct dirent *dp;
dirp = opendir(".");
if (!dirp) {
perror("opendir()");
exit(1);
}
while ((dp = readdir(dirp))) {
puts(dp->d_name);
}
if (errno) {
perror("readdir()");
exit(1);
}
return 0;
}
Note the listing will not be sorted, unlike the default ls -a
output.
注意,与默认的ls -a输出不同,清单不会被排序。
#1
9
I'm not sure why you're passing this via /bin/sh
... but since you are, you need to pass all the arguments after -c
as a single value because these are now to be interpreted by /bin/sh
.
我不知道你为什么要通过/bin/ sh…但是由于您是这样的,所以您需要将-c之后的所有参数作为单个值传递,因为这些参数现在将由/bin/ sh来解释。
The example is to compare the shell syntax of
例子是比较shell语法
/bin/sh -c ls -a
to
来
/bin/sh -c 'ls -a'
The second works, but the first doesn't.
第二个行得通,但第一个行不通。
So your ptr
should be defined as
所以ptr应该被定义为
char * const ptr[]={"/bin/sh","-c","ls -a" ,NULL};
#2
5
If you need to get the contents of a directory from a c program, then this is not the best way - you will effectively have to parse the output of ls
, which is generally considered a bad idea.
如果您需要从c程序获取目录的内容,那么这不是最好的方法——您必须有效地解析ls的输出,这通常被认为是一个坏主意。
Instead you can use the libc
functions opendir()
and readdir()
to achieve this.
相反,您可以使用libc函数opendir()和readdir()来实现这一点。
Here is a small example program that will iterate over (and list) all files in the current directory:
下面是一个小示例程序,它将遍历(并列出)当前目录中的所有文件:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
int main (int argc, char **argv) {
DIR *dirp;
struct dirent *dp;
dirp = opendir(".");
if (!dirp) {
perror("opendir()");
exit(1);
}
while ((dp = readdir(dirp))) {
puts(dp->d_name);
}
if (errno) {
perror("readdir()");
exit(1);
}
return 0;
}
Note the listing will not be sorted, unlike the default ls -a
output.
注意,与默认的ls -a输出不同,清单不会被排序。