From the commends, i saw that your real problem is, how to make a string from 2 given strings.
What you can do is: Write a function that concatenate 2 strings in to one.
For that you need to get the length of both strings, then add this lengths (also add 1 for the '\0'-Byte and check for overflows), then use malloc()
to reserve buffer space for the new string and copy both strings to this buffer.
You can do it like this (do not just use this, it is not very well testet, and the error handling is not great):
void die(const char *msg)
{
fprintf(stderr,"[ERROR] %s\n",msg);
exit(EXIT_FAILURE);
}
char *catString(const char *a, const char *b)
{
//calculate the buffer length we need
size_t lena = strlen(a);
size_t lenb = strlen(b);
size_t lenTot = lena+lenb+1; //need 1 extra for the end-of-string '\0'
if(lenTot<lena) //check for overflow
{
die("size_t overflow");
}
//reseve memory
char *buffer = malloc(lenTot);
if(!buffer) //check if malloc fail
{
die("malloc fail");
}
strcpy(buffer,a); //copy string a to the buffer
strcpy(&buffer[lena],b);//copy string b to the buffer
return buffer;
}
After this you can use this function to create the string you need from your static string "python2.7 ./myScript "
and argv[1]
int main(int argc, char **argv)
{
//without a argument we should not call the python script
if(argc<2)
{
die("need at least one argument");
}
//make one string to call system()
char *combined = catString("python2.7 ./myScript ",argv[1]);
printf("DEBUG complete string is '%s'\n",combined);
int i = system(combined);
//we must free the buffer after use it or we generate memory leaks
free(combined);
if(i<0)
{
die("system()-call failed");
}
printf("DEBUG returned from system()-call\n");
return EXIT_SUCCESS;
}
You need the extra space in "python2.7 ./myScript "
, without you would get "python2.7 ./myScriptArgumentToMain"
.
And with this your caller can execute any code he like because we do not escape argv[1]
, so a call to your program with yourProgram "argumentToPython ; badProgram argumentToBadProgram"
will execute badProgram
also which you do not want (in the most cases)