I have read several forums but can't find the solution to this.
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);
In this solution the problem is the execv wanting a char *[ ] rather than a 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);
When I try it this way, I get a segmentation fault when it tries to execute
strcpy(serverArgs[1], "-f");
What am I missing?
strcpy(serverArgs[2], "/dev/server1/trunk/build.xml");is probably no good -- that string is larger than twenty characters.strcpyis generally frowned upon, for exactly this reason – it's much too easy to create buffer overflows. Usestrncpyand provide an explicit length instead.strncpyis probably more "frowned upon" thanstrcpy, because you have to watch that the result is always nul terminated.