是“argv[0] = 可执行文件名称”吗?一个公认的标准还是一个共同的约定?
在 C 或 C++ 应用程序中将参数传递给 main()
时,argv[0]
是否始终是可执行文件的名称?或者这只是一个常见的约定,并不能保证 100% 正确?
When passing argument to main()
in a C or C++ application, will argv[0]
always be the name of the executable? Or is this just a common convention and not guaranteed to be true 100% of the time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
此页面指出:
然而,其他页面似乎支持这样一个事实:它始终是可执行文件的名称。 此内容指出:
This page states:
However, other pages seem to back up the fact that it is always the name of the executable. This one states:
我不确定这是否是一个几乎通用的约定或标准,但无论哪种方式你都应该遵守它。不过,我从未见过它在 Unix 和类 Unix 系统之外被利用。在 Unix 环境中(尤其是在过去),程序可能会根据调用它们的名称而具有显着不同的行为。
编辑:我从与我同时发表的其他帖子中看到,有人已将其识别为来自特定标准,但我确信该约定早于该标准。
I'm not sure whether it is a nearly universal convention or a standard, but either way you should abide by it. I've never seen it exploited outside of Unix and Unix-like systems, though. In Unix environments - and maybe particularly in the old days - programs might have significantly different behaviors depending on the name under which they are invoked.
EDITED: I see from other posts at the same time as mine that someone has identified it as coming from a particular standard, but I'm sure the convention long predates the standard.
如果您通过 Workbench 启动 Amiga 程序,则不会设置 argv[0],只能通过 CLI 进行设置。
If you start an Amiga program by Workbench argv[0] will not be set, only by CLI.
猜测(甚至是有根据的猜测)很有趣,但您确实需要查看标准文档才能确定。例如,ISO C11 规定(我的重点):
所以不,如果该名称可用,它只是程序名称。并且它“代表”程序名称,不一定是程序名称。前面的部分指出:
这与之前的标准 C99 没有变化,这意味着即使是值也不是由标准规定的 - 它完全取决于实现。
这意味着,如果主机环境不提供程序名称,则程序名称可以为空;如果主机环境提供提供程序名称,则程序名称可以为空,前提是“任何其他名称”以某种方式代表程序名称。在我更施虐的时刻,我会考虑将其翻译成斯瓦希里语,通过替换密码运行它,然后以相反的字节顺序存储它:-)。
然而,实现定义确实在 ISO 标准中具有特定含义 - 实现必须记录其工作方式。因此,即使 UNIX 可以通过
exec
系列调用将任何它喜欢的东西放入argv[0]
中,也必须(并且确实)记录它。Guesswork (even educated guesswork) is fun but you really need to go to the standards documents to be sure. For example, ISO C11 states (my emphasis):
So no, it's only the program name if that name is available. And it "represents" the program name, not necessarily is the program name. The section before that states:
This is unchanged from C99, the previous standard, and means that even the values are not dictated by the standard - it's up to the implementation entirely.
This means that the program name can be empty if the host environment doesn't provide it, and anything else if the host environment does provide it, provided that "anything else" somehow represents the program name. In my more sadistic moments, I would consider translating it into Swahili, running it through a substitution cipher then storing it in reverse byte order :-).
However, implementation-defined does have a specific meaning in the ISO standards - the implementation must document how it works. So even UNIX, which can put anything it likes into
argv[0]
with theexec
family of calls, has to (and does) document it.在具有
exec*()
调用的*nix
类型系统下,argv[0]
将是调用者放入argv0< 中的任何内容/code> 位于
exec*()
调用中。shell 使用的约定是程序名称,并且大多数其他程序也遵循相同的约定,因此
argv[0]
通常是程序名称。但是流氓 Unix 程序可以调用
exec()
并生成argv[0]
任何它喜欢的内容,所以无论 C 标准怎么说,你都不能指望这一点100% 的时间。Under
*nix
type systems withexec*()
calls,argv[0]
will be whatever the caller puts into theargv0
spot in theexec*()
call.The shell uses the convention that this is the program name, and most other programs follow the same convention, so
argv[0]
usually the program name.But a rogue Unix program can call
exec()
and makeargv[0]
anything it likes, so no matter what the C standard says, you can't count on this 100% of the time.根据 C++ 标准,第 3.6.1 节:
所以不,至少标准不能保证。
According to the C++ Standard, section 3.6.1:
So no, it is not guaranteed, at least by the Standard.
ISO-IEC 9899 规定:
我还使用过:
然后您只需解析字符串即可从路径中提取可执行文件名称。
ISO-IEC 9899 states:
I've also used:
And then you just have to parse the string to extract the executable name from the path.
具有
argv[0] !=
可执行文件名称的应用程序许多 shell 通过检查
argv[0][0] = 来确定它们是否是登录 shell ='-'
。登录 shell 具有不同的属性,特别是它们获取一些默认文件,例如/etc/profile
。通常是 init 本身或
getty
添加前导-
,另请参阅:https://unix.stackexchange.com/questions/299408/how- to-login-automatically-without-typing-the-root-username-or-password-in-build/300152#300152多调用二进制文件,也许最值得注意的是Busybox。这些符号将多个名称(例如
/bin/sh
和/bin/ls
)链接到单个可执行文件/bin/busybox
,该可执行文件识别要使用的工具来自argv[0]
。这使得拥有一个代表多个工具的小型静态链接可执行文件成为可能,并且基本上可以在任何 Linux 环境上工作。
另请参阅:https://unix.stackexchange.com/questions /315812/why-does-argv-include-the-program-name/315817
可运行的 POSIX
execve
示例,其中argv[0] !=
可执行文件名称其他人提到了
exec
,但这里是一个可运行的示例。ac
bc
然后:
给出:
是的,
argv[0]
也可以是:在 Ubuntu 16.10 上测试。
Applications of having
argv[0] !=
executable namemany shells determine if they are a login shell by checking
argv[0][0] == '-'
. Login shells have different properties, notably that they source some default files such as/etc/profile
.It is typically the init itself or
getty
that adds the leading-
, see also: https://unix.stackexchange.com/questions/299408/how-to-login-automatically-without-typing-the-root-username-or-password-in-build/300152#300152multi-call binaries, perhaps most notably Busybox. These symlink multiple names e.g.
/bin/sh
and/bin/ls
to a single exebutable/bin/busybox
, which recognizes which tool to use fromargv[0]
.This makes it possible to have a single small statically linked executable that represents multiple tools, and will work on basically on any Linux environment.
See also: https://unix.stackexchange.com/questions/315812/why-does-argv-include-the-program-name/315817
Runnable POSIX
execve
example whereargv[0] !=
executable nameOthers mentioned
exec
, but here is a runnable example.a.c
b.c
Then:
Gives:
Yes,
argv[0]
could also be:Tested on Ubuntu 16.10.