C语言 C: execvp() 和命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21896000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:47:49  来源:igfitidea点击:

C: execvp() and command line arguments

cexecvp

提问by user3295674

So I'm writing a program where the arguments are as follows:

所以我正在编写一个程序,其中的参数如下

program start emacs file.c

or even

甚至

program wait

In essence, the first argument (argv[0]) is the program name, followed by user inputs.

本质上,第一个参数 (argv[0]) 是程序名称,然后是用户输入。

Inside my code, I invoke execvp. Thing is, I'm not entirely sure I'm invoking the right arguments.

在我的代码中,我调用了 execvp。问题是,我并不完全确定我是否在引用正确的论点。

if (pid == 0) { 
                    execvp(argv[1], argv); //Line of interest
                    exit(1);
                    }

are argv[1] and argv the correct arguments to pass for the functionality described above? I looked at the man page and they make sense but might not be correct for this case. Thank you!

argv[1] 和 argv 是为上述功能传递的正确参数吗?我查看了手册页,它们很有意义,但对于这种情况可能不正确。谢谢!

回答by Evans

In your main, argv will be like this in the first example:

在您的主文件中,第一个示例中的 argv 将如下所示:

argv[0] = "program";
argv[1] = "start";
argv[2] = "emacs";
argv[3] = "file.c";
argv[4] = NULL;

In execv you want to execute the program "start" with args "emacs file.c", right?. Then the first parameter should be argv[1] - "start" and the second one an array with this strings: {"start", "emacs", "file.c", NULL}. If you use argv, you include the "program" string in argv[0].

在 execv 中,您想使用 args “emacs file.c” 执行程序“start”,对吗?那么第一个参数应该是 argv[1] - "start",第二个参数是一个带有这个字符串的数组:{"start", "emacs", "file.c", NULL}。如果使用 argv,则在 argv[0] 中包含“程序”字符串。

You can create a new array and copy these parameters or use the address of argv[1] like this:

您可以创建一个新数组并复制这些参数或使用 argv[1] 的地址,如下所示:

execvp(argv[1], &argv[1]); //Line of interest

回答by Guilherme Salazar

My understanding is that you want to take a specific action based on the second command-line argument (argv[1]). If the second argument is 'start', your program should start the executable named argv[2]with the arguments provided thereafter (right?). In this case, you should provide execvpwith the executable name (argv[2]) [1] and a list of arguments, which by convention starts with the name of the executable (argv[2]).

我的理解是您希望根据第二个命令行参数 ( argv[1])执行特定操作。如果第二个参数是“start”,你的程序应该启动以后面argv[2]提供的参数命名的可执行文件(对吗?)。在这种情况下,您应该提供execvp可执行文件名称 ( argv[2]) [1] 和参数列表,按照惯例,参数列表以可执行文件的名称 ( ) 开头argv[2]

execvp(argv[2], &argv[2])would implement what we have described in the last paragraph (assuming this is what you intended to do).

execvp(argv[2], &argv[2])将实现我们在上一段中描述的内容(假设这是您打算做的)。

[1] execvpexpects 2 arguments as you know. The first is a filename; if the specified filename does not contain a slash character (/), execvpwill do a lookup in the PATH environment variable (which contains a list of directories where executable files reside) to find the executable's fully-qualified name. The second argument is a list of command-line arguments that will be available to the program when it starts.

[1]execvp如您所知,需要 2 个参数。第一个是文件名;如果指定的文件名不包含斜杠字符 (/),execvp将在 PATH 环境变量(其中包含可执行文件所在的目录列表)中查找可执行文件的完全限定名称。第二个参数是程序启动时可用的命令行参数列表。

回答by millinon

The only thing that might be an issue is that argv[0]in argvpassed to execvpwon't match argv[1](the first argument). Otherwise, it looks okay.

这可能是一个问题的唯一的事情就是argv[0]argv传递给execvp不匹配argv[1](第一个参数)。否则,它看起来没问题。

Imagine calling program cat file.txt. In your program, argv will be {"program", "cat", "file.txt", NULL}. Then, in cat, even though the binary called will be cat, argv will still be {"program", "cat", "file.txt", NULL}.

想象一下调用program cat file.txt. 在您的程序中, argv 将为{"program", "cat", "file.txt", NULL}. 然后,在 cat 中,即使调用的二进制文件是 cat,argv 仍然是{"program", "cat", "file.txt", NULL}.

Since cattries to open and read each argument as a file, the first file it'll try to open is cat(argv[1]), which isn't the desired behavior.

由于cat尝试将每个参数作为文件打开和读取,它将尝试打开的第一个文件是cat( argv[1]),这不是所需的行为。

The simple solution is to use execvp(argv[1], argv+1)- this essentially shifts the argument array to the left by one element.

简单的解决方案是使用execvp(argv[1], argv+1)- 这实质上是将参数数组向左移动一个元素。