- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试编写一个小型实用程序,将其参数列表中继到一个exec'd进程,除了在构建新进程的参数列表时会重复一些传入的参数。
以下是我要执行的操作的非常简化的版本,该版本仅重复了每个参数一次:
#include <stdlib.h>
#include <unistd.h>
#define PROG "ls"
int main(int argc, char* argv[] ) {
int progArgCount = (argc-1)*2;
char** execArgv = malloc(sizeof(char*)*(progArgCount+2)); // +2 for PROG and final 0
execArgv[0] = PROG;
for (int i = 0; i<progArgCount; ++i)
execArgv[i+1] = argv[i/2+1];
execArgv[progArgCount+1] = 0;
execvp(PROG, execArgv );
} // end main()
execArgv
的元素不是唯一的。具体来说,每个重复项中的两个元素是相同的,这意味着它们指向内存中的相同地址。
argv
元素的唯一性?如果我错了,请纠正我,但是由于程序不是const,程序是否可能无法直接修改其
argv
元素?这样做会冒着风险,使执行程序错误地修改
argv[1]
(例如)然后访问
argv[2]
,错误地假设这两个元素指向独立的字符串吗?我可以肯定,几年前我开始学习C/C++时就自己做了,而且我认为当时
argv
元素可能不是唯一的。
argv
参数(在上面的示例中为
execArgv
)深复制到新的内存分配中,这可能会使事情变得唯一,但我对exec函数的内部知识并不了解。而且,这至少是浪费的,至少是如果可以在“替换”操作中保留原始数据结构,那么这就是我怀疑它发生的原因。也许不同的平台/实现在这方面表现不同?回答者可以对此发表意见吗?
The arguments specified by a program with one of the exec functions shall be passed on to the new process image in the corresponding main() arguments.
The argument argv is an array of character pointers to null-terminated strings. The application shall ensure that the last member of this array is a null pointer. These strings shall constitute the argument list available to the new process image. The value in argv[0] should point to a filename that is associated with the process being started by one of the exec functions.
The argv[] and envp[] arrays of pointers and the strings to which those arrays point shall not be modified by a call to one of the exec functions, except as a consequence of replacing the process image.
argv
。此摘录只是加剧了我的困惑。
The statement about argv[] and envp[] being constants is included to make explicit to future writers of language bindings that these objects are completely constant. Due to a limitation of the ISO C standard, it is not possible to state that idea in standard C. Specifying two levels of const-qualification for the argv[] and envp[] parameters for the exec functions may seem to be the natural choice, given that these functions do not modify either the array of pointers or the characters to which the function points, but this would disallow existing correct code. Instead, only the array of pointers is noted as constant. The table of assignment compatibility for dst= src derived from the ISO C standard summarizes the compatibility:
char
数据,因此很难明确指出“这些对象是完全恒定的”。其次,我不知道为什么该段提到“语言绑定(bind)的作者”。绑定(bind)到什么?这与exec函数上的常规文档页面有何关系?第三,该段的主要目的似乎是说,为了向后兼容已建立的ISO C标准,我们不得不将
char
元素所指向的字符串的实际
argv
内容保留为非常量。现有的正确代码”。文档页面上的下表对此进行了确认,在此不再赘述。尽管在摘录的中间确实很清楚地表明exec函数本身不会以任何方式修改给定的
argv
对象,但是这些都不能果断地回答我的主要问题。
最佳答案
Does Standard C say anything about this usage? Is it incorrect, or undefined behavior?
If not, is it still inadvisable, since the exec'd program might depend on the uniqueness of its argv elements?
Please correct me if I'm wrong, but isn't it possible for programs to modify their argv elements directly, since they're non-const?
The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
Wouldn't that create a risk of the exec'd program blithely modifying argv
[1]
(say) and then accessing argv[2]
, falsely assuming that the two elements point to independent strings?
exec
是操作系统的功能,该操作系统在现有进程的上下文中运行可执行文件,以替换先前的可执行文件。
exec
家族系统调用时,参数中给出的程序将被加载到调用者的地址空间中,并在那里覆盖该程序。结果,一旦指定的程序文件开始执行,调用者地址空间中的原始程序就消失了,并由新程序和存储在新替换的地址空间中的参数列表
argv
替换。
The number of bytes available for the new process' combined argument and environment lists is {ARG_MAX}. It is implementation-defined whether null terminators, pointers, and/or any alignment bytes are included in this total.
{ARG_MAX}
Maximum length of argument to the exec functions including environment data.
I know that exec'ing involves "replacement of the process image", but I'm not sure what that entails exactly.
And perhaps different platforms/implementations behave differently in this respect? Can answerers please speak to this?
关于c - 在调用exec *()系列函数时,argv的char *元素是否必须唯一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815506/
目前我正在学习 C 并试图理解这些说明。它们真的不同吗? ++*argv *argv++ *(++argv) *(argv++) 谢谢! 最佳答案 后缀递增运算符的优先级高于指针取消引用运算符,而不是
我正在与在 Hostgator 共享主机上运行的 PHP CLI 的一个(也许)简单问题作斗争。一个简单的代码: 在我的本地机器上运行时以及其他几个 php 实例: php test.php arg
谁能解释一下原因 int main(int argc, const char * argv[]) { while (* argv) puts(* argv++); return 0 ;
从 K&R 的 C 书的第 5.10 章开始,argv 的概念被引入以允许命令行参数。 argv 是指向字符指针数组的指针。据此,以下代码如何检查提供的参数是否以连字符开头? (*++argv)[0]
我不明白 sys.argv 和 argv 之间的区别,网上没有任何东西给我我理解的概念。如果两者相同!我们什么时候使用 sys.argv 什么时候使用 argv ? 如果不是,sys.argv 是什么
我正在编写一个接受参数的 C 程序,char *argv[] 来决定运行哪个 exec()。我有 execlp(argv[1], argv[1], NULL) 可以正常工作。出于某种原因,execvp
char** argv 和 char* argv[] 有什么区别?在 int main(int argc, char** argv) 和 int main(int argc, char* argv[]
我目前有一个 python 脚本,可以在大型网络中完成一项非常有用的任务。我所做的是使用 lsof -iTCP -F 和其他一些选项来转储所有监听的 TCP 套接字。我能够得到 argv[0],这不是
为什么 argvs 的行为很奇怪?示例: 这将按预期工作,打印第一个参数的第一个字符。 printf("%c", *argv[1]); 但是,这将打印 ascii 表中的字符(又名“更大”,表示为数字
我做了一个小的重复异或加密器。您可以使用您选择的键对您选择的字符串进行异或运算,它可以选择将其输出到文件中。 无论何时使用单个单词(无空格)字符串和键运行程序,都没有问题。但是,只要字符串或键中有空格
我刚开始学习 C,想知道我应该在我的主要方法中使用其中的哪一个。有什么不同吗?哪个更常见? 最佳答案 由于您刚开始学习 C,我建议您首先真正尝试了解数组和指针之间的区别,而不是共同的东西。 在参数和数
所以我在编译器转换的函数中传递数组时在幕后阅读过 int myArray(int arr[]) 进入 int myArray(int *arr) 例如,大多数时候数组也会衰减为指针 arr[0] 与相
我正在编写一个小的 shell 程序,它接受一个命令并执行它。如果用户输入无效命令,则 if 语句返回 -1。如果命令正确,它就会执行命令,但是一旦它执行了命令,程序就会结束。我做错了什么是不执行它之
是否可以传入自定义参数列表? 与现实生活中的用例相比,更多是出于好奇,但例如,我可能希望在让 argparse 完成其工作之前将所有参数转换为小写。 最佳答案 Yes .只需传入要parse_args
我知道这个问题很基础,我也是新手,所以请帮我解决这个问题: 我有这个代码: int wmain(int argc, wchar_t *argv[]) { if (*argv[1] == L'-
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 3 个月前。 我写了这段代码: #!/usr/bin/env python import sys i
我的程序获取主要参数,我的任务是检查用户是否在执行文件和参数之间输入了空格。 这是一个没有在 argv[0] 为 HW01 和 argv[1] 之间输入空格的人的例子/n: HW01/n 但是如果有人
我的 c 技能很生疏,所以如果这是一个愚蠢的问题,我深表歉意,但我什至没想过要寻找这个简单问题的答案。此代码编译时没有任何警告: #include int run_script( int argc,
这个问题在这里已经有了答案: Should I use char** argv or char* argv[]? (10 个答案) What does the 'array name' mean i
我有以下代码片段: int main(int argc, char *argv[]) { char line[MAXLINE]; long lineno = 0; i
我是一名优秀的程序员,十分优秀!