Can someone please tell me how to modify/add to this code so that it recognizes UNIX command options (all beginning with "-") and executes the command with options?
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int i;
system("stty -echo");
for(i=1; i<argc; i++)
{
printf("The command %s is executed.\n", argv);
system(argv);
}
system("stty echo");
return 0;
}
I think you will have to prepare a string of the unix command first and then pass this string to the system function. This string manipulation should take care of the "-" sign. I think you can also try ftok.
for second case, you need to give input as say "ls -l" within double quotes.
You can have any combination for this.
Like let us assume "a.out" is the binary then
./a.out "who | wc -l" also works for the second option.,....
ANSI C says 'int main()'. Ofcourse, to avoid compiler warnings you can use 'void main()', but that is incorrect. The OS might be wanting a return value from the terminated program, and when you are not returning any value to it, a junk value might be returned to your OS, and the results could be unpredictable.
>Thanks yaar for opening my eyes on the very first line of my everyday code.
>If you have some more good links please do send to following address : sharath.c@shiparatech.com
It's against the rules of this board, displaying your email id like that, and why not! It's very right that this board is for the benefit of all.
About the links, what kind of links are you looking for? You can always search them here and if you have trouble finding them, post the query here, there will always be someone here to help you out.