我已经阅读了几个论坛,但找不到解决方案。
int sIndex = 3;
char serverArgs[serverCommandCount + 3][20];
strcpy(serverArgs[0], "ant");
strcpy(serverArgs[1], "-f");
strcpy(serverArgs[2], "/dev/server1/trunk/build.xml");
if(serverStop){strcpy(serverArgs[sIndex], "jboss-stop"); sIndex++;}
if(serverClean){strcpy(serverArgs[sIndex], "clean"); sIndex++;}
if(serverDeploy){strcpy(serverArgs[sIndex], "deploy"); sIndex++;}
if(releaseDB){strcpy(serverArgs[sIndex], "releasedb"); sIndex++;}
if(createDB){strcpy(serverArgs[sIndex], "createdb"); sIndex++;}
if(serverStart){strcpy(serverArgs[sIndex], "jboss-start"); sIndex++;}
if(serverDebug){strcpy(serverArgs[sIndex], "jboss-start-debug"); sIndex++;}
execv(antEx, serverArgs);
在这个解决方案中,问题是 execv 想要一个 char *[ ] 而不是 char[ ]。
int sIndex = 3;
char *serverArgs[serverCommandCount + 3];
for(index = 0; index < serverCommandCount + 3; index++)
serverArgs[index] = malloc(20);
strcpy(serverArgs[0], "ant");
strcpy(serverArgs[1], "-f");
strcpy(serverArgs[2], "/dev/server1/trunk/build.xml");
if(serverStop){strcpy(serverArgs[sIndex], "jboss-stop"); sIndex++;}
if(serverClean){strcpy(serverArgs[sIndex], "clean"); sIndex++;}
if(serverDeploy){strcpy(serverArgs[sIndex], "deploy"); sIndex++;}
if(releaseDB){strcpy(serverArgs[sIndex], "releasedb"); sIndex++;}
if(createDB){strcpy(serverArgs[sIndex], "createdb"); sIndex++;}
if(serverStart){strcpy(serverArgs[sIndex], "jboss-start"); sIndex++;}
if(serverDebug){strcpy(serverArgs[sIndex], "jboss-start-debug"); sIndex++;}
execv(antEx, serverArgs);
当我以这种方式尝试时,当它尝试执行时出现分段错误
strcpy(serverArgs[1], "-f");
我错过了什么?