分析execve()的命令行时出错。普通C
我不熟悉C指针,尝试使用命令行执行进程并获取其pid。任何使用命令行的system()、sh()等都不会返回pid,所以我决定使用execve()。但它需要将命令行解析为字符串数组 我找到了一些现有的解决方案,但我无法使任何方案起作用。所以,我自己写了。它在我的测试程序中工作,但在实际程序中会导致分段错误。 谁能告诉我,我错在哪里? 代码是:分析execve()的命令行时出错。普通C,c,pointers,segmentation-fault,argv,C,Pointers,Segmentation Fault,Argv,我不熟悉C指针,尝试使用命令行执行进程并获取其pid。任何使用命令行的system()、sh()等都不会返回pid,所以我决定使用execve()。但它需要将命令行解析为字符串数组 我找到了一些现有的解决方案,但我无法使任何方案起作用。所以,我自己写了。它在我的测试程序中工作,但在实际程序中会导致分段错误。 谁能告诉我,我错在哪里? 代码是: 我想这里所有的问题都是指针操作,我分配数组并写入它。如何纠正?你看这里了吗?我相信你想要的是getpid(),尽管你可能要求更多。你看这里了吗?我相信你想
我想这里所有的问题都是指针操作,我分配数组并写入它。如何纠正?你看这里了吗?我相信你想要的是getpid(),尽管你可能要求更多。你看这里了吗?我相信你想要的是getpid(),尽管你可能要求更多。问题结束了。找到了解决方案。解决了棘手的指针声明语法。这是有效的 谢谢大家-|
void cmdline_to_argv(const char* cmd_line, char ***argv, int* argc){
int i, spc;
size_t len;
char *s,*d;
/* make a copy to play with */
char *cmd_line1 = strdup(cmd_line);
/* count items to deal with and trim multiple spaces */
d = s = cmd_line1; spc = 1; *argc = 0; len=strlen(cmd_line1);
for (i=0; i<len;i++,s++) {
switch (*s) {
case ' ':
if (spc) continue;
*d++ = '\0'; /* replace spaces with zeroes */
spc = 1;
break;
default:
if (spc) { (*argc)++; spc = 0; }
*d++ = *s;
}
}
(*d++) = '\0'; /* line termination */
/* calc actual size */
len = d - cmd_line1;
/* allocate array of poiters */
*argv = (char**) malloc(sizeof(char*) * ((*argc)+1) + len);
(*argv)[*argc] = (char*) NULL;
d = (char*) &(*argv)[(*argc)+1];
memmove(d, cmd_line1, len);
free(cmd_line1);
cmd_line1 = d;
/* scan line again to find all lines starts and register */
for (i=0; i<*argc; i++) {
(*argv)[i] = d;
while (*(d++)) ;
}
}
/* deallocate array and strings */
void free_argv(char ***argv) {
free(*argv); /* strings laying together at once after pointers array */
}
void cmdline_to_argv(const char*cmd_line,char***argv,int*argc){
int i,spc;
尺寸透镜;
字符*s,*d;
/*复制一份来玩*/
char*cmd_line 1=strdup(cmd_line);
/*计算要处理的项目并修剪多个空间*/
d=s=cmd_line1;spc=1;*argc=0;len=strlen(cmd_line1);
对于(i=0;i问题已结束。找到解决方案。棘手的指针声明语法已解决。以下是有效的解决方案
谢谢大家-|
void cmdline_to_argv(const char* cmd_line, char ***argv, int* argc){
int i, spc;
size_t len;
char *s,*d;
/* make a copy to play with */
char *cmd_line1 = strdup(cmd_line);
/* count items to deal with and trim multiple spaces */
d = s = cmd_line1; spc = 1; *argc = 0; len=strlen(cmd_line1);
for (i=0; i<len;i++,s++) {
switch (*s) {
case ' ':
if (spc) continue;
*d++ = '\0'; /* replace spaces with zeroes */
spc = 1;
break;
default:
if (spc) { (*argc)++; spc = 0; }
*d++ = *s;
}
}
(*d++) = '\0'; /* line termination */
/* calc actual size */
len = d - cmd_line1;
/* allocate array of poiters */
*argv = (char**) malloc(sizeof(char*) * ((*argc)+1) + len);
(*argv)[*argc] = (char*) NULL;
d = (char*) &(*argv)[(*argc)+1];
memmove(d, cmd_line1, len);
free(cmd_line1);
cmd_line1 = d;
/* scan line again to find all lines starts and register */
for (i=0; i<*argc; i++) {
(*argv)[i] = d;
while (*(d++)) ;
}
}
/* deallocate array and strings */
void free_argv(char ***argv) {
free(*argv); /* strings laying together at once after pointers array */
}
void cmdline_to_argv(const char*cmd_line,char***argv,int*argc){
int i,spc;
尺寸透镜;
字符*s,*d;
/*复制一份来玩*/
char*cmd_line 1=strdup(cmd_line);
/*计算要处理的项目并修剪多个空间*/
d=s=cmd_line1;spc=1;*argc=0;len=strlen(cmd_line1);
对于(i=0;iThanks.But getpid()仅在当前进程中工作,并且子进程不是我自己的,因此我不能在其中使用getpid()。子进程的Pid必须在父进程启动子进程后在父进程中已知。也许,在system()之后还有另一种获取Pid的方法调用或类似的调用,不需要数组,只需要一行?因此,您正在尝试在fork之后获取父级中子级的pid?如果是,则fork会将子级的pid返回给父级。是的!我确实使用fork()获取pid,然后用另一个进程映像用execve()覆盖fork子级调用。这就是为什么我需要将execve()的行转换为数组。上面显示的转换例程是我的问题。它会出现故障,我需要知道原因。谢谢。但是getpid()只在当前进程中工作,并且子进程不是我自己的,所以我不能使用getpid()在父进程启动子进程后,子进程的Pid必须在父进程中已知。也许,在system()之后还有另一种获取Pid的方法调用或类似的调用,不需要数组,只需要一行?因此,您正在尝试在fork之后获取父级中子级的pid?如果是,则fork会将子级的pid返回给父级。是的!我确实使用fork()获取pid,然后用另一个进程映像用execve()覆盖fork子级调用。这就是为什么我需要将execve()的行转换为数组。上面显示的转换例程是我的问题。它会出现故障,我需要知道原因。
void cmdline_to_argv(const char* cmd_line, char ***argv, int* argc){
int i, spc;
size_t len;
char *s,*d;
/* make a copy to play with */
char *cmd_line1 = strdup(cmd_line);
/* count items to deal with and trim multiple spaces */
d = s = cmd_line1; spc = 1; *argc = 0; len=strlen(cmd_line1);
for (i=0; i<len;i++,s++) {
switch (*s) {
case ' ':
if (spc) continue;
*d++ = '\0'; /* replace spaces with zeroes */
spc = 1;
break;
default:
if (spc) { (*argc)++; spc = 0; }
*d++ = *s;
}
}
(*d++) = '\0'; /* line termination */
/* calc actual size */
len = d - cmd_line1;
/* allocate array of poiters */
*argv = (char**) malloc(sizeof(char*) * ((*argc)+1) + len);
(*argv)[*argc] = (char*) NULL;
d = (char*) &(*argv)[(*argc)+1];
memmove(d, cmd_line1, len);
free(cmd_line1);
cmd_line1 = d;
/* scan line again to find all lines starts and register */
for (i=0; i<*argc; i++) {
(*argv)[i] = d;
while (*(d++)) ;
}
}
/* deallocate array and strings */
void free_argv(char ***argv) {
free(*argv); /* strings laying together at once after pointers array */
}