Reputation: 2228
I'm writing in C langage a program that contain these lines:
void main(int argc, char *argv[]) {
char* file=argv[1];
char* arguments[] = { "sh", argv[2], argv[3], argv[4], file, NULL };
execv("/bin/sh", arguments);
}
The file is prog.sh
which contain a simple sum of arguments:
expr $1 + $2 + $3
When I run the program by ./main prog.sh 1 2 3
I obtain an error which is
/bin/sh: 0: Can't open 1
While I expect the output 6 (sum of 1 2 3)
Upvotes: 0
Views: 9096
Reputation: 123470
Look at your arguments:
char* arguments[] = { "sh", argv[2],argv[3],argv[4],file, NULL };
When you run ./main prog.sh 1 2 3
, you end up calling:
sh 1 2 3 prog.sh
You should instead make the script the first argument:
char* arguments[] = { "sh", file, argv[2],argv[3],argv[4], NULL };
thereby calling
sh prog.sh 1 2 3
Upvotes: 2