argv[0] 什么时候可以为空? [英] When can argv[0] have null?

查看:37
本文介绍了argv[0] 什么时候可以为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从命令行向 main() 传递参数的理解是 argc 的最小值为 1,而 argv[0] 将始终包含程序名称及其路径.

What I have understand about passing arguments to main() from command line is that argc has a minimum value of 1 and argv[0] will always have the program name with its path in it.

如果在命令行提供参数,则 argc 的值将大于 1 并且 argv1 到 argv[argc-1] 将有这些参数.

If arguments are provided at the command line, then argc will have a value greater than one and argv1 to argv[argc-1] will have those arguments.

现在这个链接上的一个段落说

argv[0] 将是一个包含程序名称的字符串或一个空字符串(如果它不可用).

argv[0] will be a string containing the program's name or a null string if that is not available.

现在,argv[0] 如何以及何时可以有空字符串?我的意思是程序名称及其路径将始终可用,所以什么时候可以为空?

Now, how and when can argv[0] have null string? I mean program name with its path will always be available so when can it be null?

作者说如果那不可用"但是程序名称何时以及如何可能不可用?

Writer says that "if that is not available" but when and how it is possible that program name will not be available?

推荐答案

使用 exec 类调用,您指定程序 name 和程序 executable 分开,这样你就可以把它设置为 NULL.

With the exec class of calls, you specify the program name and program executable separately so you can set it to NULL then.

但这句话实际上来自 ISO 标准(可能是转述),该标准涵盖了从最小的微控制器到最新的 z10 企业级大型机的范围非常广泛的执行环境.

But that quote is actually from the ISO standard (possibly paraphrased) and that standard covers a awfully large range of execution environments from the smallest micro-controller to the latest z10 Enterprise-class mainframe.

许多嵌入式系统可能会遇到可执行文件名称毫无意义的情况.

Many of those embedded systems would be in the situation where an executable name makes little sense.

来自最新的 c1x 草案:

From the latest c1x draft:

argc 的值应该是非负的.

argv[argc] 应为空指针.

如果 argc 的值大于零,数组成员 argv[0]argv[argc-1] 包括包含指向字符串的指针,这些字符串在程序启动之前由宿主环境赋予实现定义的值.

If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program start up.

这意味着,如果 argc 为零(并且可能为零),则 argv[0] 为 NULL.

This means that, if argc is zero (and it can be), argv[0] is NULL.

但是,即使 argcnot 0,您也可能无法获得程序名称,因为标准还指出:

But, even when argc is not 0, you may not get the program name, since the standard also states:

如果argc的值大于零,则argv[0]指向的字符串代表程序名;argv[0][0] 如果程序名称在宿主环境中不可用,则应为空字符.如果argc的值大于1,则argv[1]argv[argc-1]指向的字符串代表程序参数.

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

因此,标准下没有要求提供程序名称.我已经看到程序为此值使用了多种选项:

So, there is no requirement under the standard that a program name be provided. I've seen programs use a wide selection of options for this value:

  • 根本没有价值(为了所谓的安全).
  • 明目张胆的谎言(例如恶意代码的sleep).
  • 实际程序名称(例如sleep).
  • 稍作修改(例如用于登录 shell 的 -ksh).
  • 一个描述性名称(例如,progname - 一个程序).
  • no value at all (for supposed security).
  • a blatant lie (such as sleep for a malicious piece of code).
  • the actual program name (such as sleep).
  • a slightly modified one (such as -ksh for the login shell).
  • a descriptive name (e.g., progname - a program for something).

这篇关于argv[0] 什么时候可以为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆