I am trying to write a program for executing multiple programs from command line arguments. I start with concatenating the argv[x] strings I pass through calling the program into bigger strings that are separated by a semicolon.
I later want to execute this strings as separate child processes within a parent process.
But I am having trouble concatenating the arguments correctly.
my code:
int main(int argc, char **argv) {
char *wp = " ";
for(int i=1; i < argc; i++){
// if next argument is not a semicolon and is not null
if((strcmp(argv[i],";") != 0) && (argv[i+1] != NULL) ){
// concat this argument with whitespace
strcat(argv[i],wp);
// concat this argument with the next
strcat(argv[i],argv[i+1]);
}
// go on with concatenating next arguments after semicolon if any, into new string ...
}
}
// test results
printf("\n%s",argv[1]);
// go on with executing argv as a child process..
}
I call the above program with ./program ls -l -a . \; date
and the output is: ls -a .
Could someone explain why the complete series of arguments up until the semicolon is not shown? (ls -l -a .) Thank you
Problems include
Insufficient buffer size
Code fail to properly concatenate as the destination buffer is not specified to be large enough.
OP's code instead experiences undefined behavior (UB).
What should be done instead depends on OP's larger unstated goal.