When I execute the program ./test -debug 7 -m player,I use C language how to get the parameter values of -m -debug?
当我执行程序./test -debug 7 -m player时,我使用C语言如何获取-m -debug的参数值?
I have tried:
我努力了:
char* name = (char*)calloc(1024,sizeof(char));
if(name){
sprintf(name, "/proc/%d/cmdline",pid);
FILE* f = fopen(name,"r");
if(f){
size_t size;
size = fread(name, sizeof(char), 1024, f);
if(size>0){
if('\n'==name[size-1])
name[size-1]='\0';
}
fclose(f);
}
}
But it only returns the name of the process.exec "xargs -0 < /proc/pid/cmdline" can return the right value(mytest -debug 7 -m player),I want to get in another process, rather than in the main method of the process.such as,in process mytest2,I want to get mytest process debug value with pid(via pid = getpid() and via pid get mytest process info,and than get debug value ).
但它只返回process.exec的名称“xargs -0
1 个解决方案
#1
2
From proc(5):
来自proc(5):
The command-line arguments appear in this file as a set of strings separated by null bytes ('\0'), with a further null byte after the last string.
命令行参数在此文件中显示为由空字节('\ 0')分隔的一组字符串,在最后一个字符串后面还有一个空字节。
So, this code should work:
所以,这段代码应该工作:
for (i = 0; i < size; i++) {
if (!i)
printf("%s\n", name);
else if (!name[i - 1])
printf("%s\n", name + i);
}
#1
2
From proc(5):
来自proc(5):
The command-line arguments appear in this file as a set of strings separated by null bytes ('\0'), with a further null byte after the last string.
命令行参数在此文件中显示为由空字节('\ 0')分隔的一组字符串,在最后一个字符串后面还有一个空字节。
So, this code should work:
所以,这段代码应该工作:
for (i = 0; i < size; i++) {
if (!i)
printf("%s\n", name);
else if (!name[i - 1])
printf("%s\n", name + i);
}