一个简单的函数,popen正常,执行完代码后pclose返回-1,提示错误:No child processes
static char *Display(int argc, char **argv, int *retlen)
{
//cmd handle func, and save the result into the sReturnBuffer
char *retbuf = NULL;
char tmpbuff[1024];
char command[1024];
int len = 0;
int rc = 0;
FILE *fp = NULL;
int i = 0;
//check valid
if (NULL == argv || NULL == retlen || 0 == argc)
{
return NULL;
}
*retlen = 0;
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s \r\n",i, argv[i]);
strcat(command,argv[i]);
strcat(command," ");
}
printf("cmd show = \r\n%s \r\n",command);
//popen
fp = popen(command, "r");
if (NULL == fp)
{
perror("popen error");
return NULL;
}
//sReturnBuffer init
memset(sReturnBuffer, 0, sizeof(sReturnBuffer));
// memcpy sReturnBuffer, get sReturnBuffer len
while(fgets(tmpbuff, sizeof(tmpbuff), fp) != NULL)
{
strcat(sReturnBuffer, tmpbuff);
}
len = (int)strlen(sReturnBuffer);
printf("len = %d \r\n",len);
if (len > 0)
{
//use retbuf as return value
retbuf = (char *)malloc(len);
memcpy(retbuf, sReturnBuffer, len);
}
printf("\r\nsReturnBuffer = \r\n%s \r\n", sReturnBuffer);
//pclose
rc = pclose(fp);
printf("rc = %d\n",rc);
if (-1 == rc)
{
perror("pclose error");
return NULL;
}
*retlen = len;
return retbuf;
}
检查过command,sReturnBuffer都是对的打印出来的信息也是对的,就是pclose时,rc的值是-1,错误信息:pclose error: No child processes
网上查了一下,没有什么有效的办法,不知道有什么地方错了,没有检查到,希望有懂的给个意见。