I am trying to wrap a shell script so that it may be called from a Windows program inside Cygwin. Following up on a previous question, I thought it was enough to pass the arguments of the script to the wrapper as follows:
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char *argv[])
{
/* WARNING: Only use an absolute path to the script to execute,
* a malicious user might fool the binary and execute
* arbitary commands if not.
* */
system ("~/bin/script.sh \"$*\"");
return 0;
}
The script runs, but somehow cannot pass the arguments to the wrapper (I guess?). Coming from a DOS/Win background, this shell scripting thing is somewhat mysterious to me. What is wrong with this approach?
I am trying to wrap a shell script so that it may be called from a Windows program inside Cygwin. Following up on a previous question, I thought it was enough to pass the arguments of the script to the wrapper as follows:
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char *argv[])
{
/* WARNING: Only use an absolute path to the script to execute,
* a malicious user might fool the binary and execute
* arbitary commands if not.
* */
system ("~/bin/script.sh \"$*\"");
return 0;
}
The script runs, but somehow cannot pass the arguments to the wrapper (I guess?). Coming from a DOS/Win background, this shell scripting thing is somewhat mysterious to me. What is wrong with this approach?
Share Improve this question edited Feb 2 at 19:52 jarnowic asked Jan 31 at 0:20 jarnowicjarnowic 2651 silver badge19 bronze badges 4 |1 Answer
Reset to default 1Firstly, "$*" doesn't work in C programs. You'll need to iterate through argv, and append all the arguments to a string.
Secondly, "$*" isn't even that great in shell, you should usually use "$@" because it preserves arguments separation.
Thirdly, what system() does might depend what compiler and library you are using. If it's a windows compiler, I'm not sure its libraries would know how to execute either "~" tilde, or the shell script. I know you said you're using cygwin, but you didn't say if you were using the cywin compiler or a windows compiler.
Fourthly, after all, why would you want to to write a C program to wrap cygwin? I don't know what a "windows process" is... but windows can execute c:/cygwin/bin/bash script.sh arg arg arg without wrapping it. If I really needed a windowsy thing to call cygwin, I'd probably write a .bat script calling bash.
argv
array that was passed tomain
, and add each parameter from there (separated by spaces) to the string you pass tosystem()
. – Chad Commented Jan 31 at 0:41system()
function simply passes the argument to the OS, missing some capabilites of the shell, if any... – jarnowic Commented Jan 31 at 23:44