GetCommandLine的Linux *真实*相当于 [英] GetCommandLine linux *true* equivalent

查看:547
本文介绍了GetCommandLine的Linux *真实*相当于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题到Linux相当于GetCommandLine和CommandLineToArgv的

是否有可能得到的原始的命令在Linux行?文件/ proc /自/ CMDLINE是destroyd。

  ./ a.out的文件=文件1,2档参数=2

打印

  ./ a.outfiles =文件1,文件2param = 2

这是垃圾

避灾命令行不工作的所有参数,但第一次。

  ./ a.out的文件= \\FIL 1 \\,\\FIL 2 \\\\参数= \\2 \\

打印

  ./ a.outfiles =FIL1,FIL2参数=2


解决方案

您不能这样做。命令行参数实际上是传递给新的进程作为单独的字符串。看到Linux内核源代码:
kernel_execve

注意 kernel_execve(...)需要为const char * argv的[] - 所以不存在在Linux的一个长字符串命令行这样的东西 - 这是上面的一层需要的参数划分为单独的组件。

编辑:实际上,系统调用是在这里:

excve系统调用

不过,上述声明仍然适用。为argv的参数已经由内核得到它从C库调用exec时间分割。

它是的节目的启动器的责任(通常是壳,但不必是),以产生所述的argv []数组。它会做通配(扩展文件名通配符来它匹配的实际文件)和报价,变量替换的剥离等。

我还要指出的是,虽然有在C库中的执行几个变种,只有一种方式进入内核。所有变种最终会在的execve 系统调用,我联系到上面。其他的变种,只是因为主叫方可以不花哨分裂参数到invdividual元素,所以C库,帮助去做,对于程序员。同样,对于传递一个环境数组的新方案 - 如果程序员不需要特定的环境中,他/她可以叫自动取父进程ENV的变体。

Similar question to Linux equivalent of GetCommandLine and CommandLineToArgv

Is it possible to get the raw command line in linux? The file /proc/self/cmdline is destroyd.

./a.out files="file 1","file 2" param="2"

prints

./a.outfiles=file 1,file 2param=2

which is junk

Escaping command line does work for all arguments but the first.

./a.out files=\"fil 1\",\"fil 2\"\ param=\"2\"

prints

./a.outfiles="fil1","fil2" param="2"

解决方案

You can't do that. The command line arguments are actually passed to the new process as individual strings. See the linux kernel source: kernel_execve

Note that kernel_execve(...) takes a const char *argv[] - so there is no such thing as a long string commandline in Linux - it's the layer above that needs to split the arguments into separate components.

Edit: actually, the system call is here:

excve system call

But the statement above still applies. The parameter for argv is already split by the time the kernel gets it from the C-library call to exec.

It is the responsibility of the "starter of the program" (typically a shell, but doesn't have to be) to produce the argv[] array. It will do the "globbing" (expansion of wildcard filenames to the actual files that it matches) and stripping of quotations, variable replacement and so on.

I would also point out that although there are several variants of "exec" in the C library, there is only one way into the kernel. All variants end up in the execve system call that I linked to above. The other variants are simply because the caller may not fancy splitting arguments into invdividual elements, so the C library "helps out" by doing that for the programmer. Similarly for passing an environment array to the new program - if the programmer don't need specific environment, he/she can just call the variant that automatically take the parent process env.

这篇关于GetCommandLine的Linux *真实*相当于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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