Executing command line options

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;
}

Hi Safia,

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.

Hope this hints you to the solution.

There's a function called getopt(), check to see if this function is supported in your compiler, this will help you.

Can you try this one ??
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void main(int argc, char **argv)
{
char str[30];
if (argv[2][0] == '-')
{
strcpy(str,argv[1]);
strcat(str," ");
strcat(str,argv[2]);
system (str);
}

}//end of main

or

void main(int argc, char **argv)
{
system(argv[1]);
}

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.,....

bye,
sharath

>void main(int argc, char **argv)
void main() is wrong. main() should return a value.

If main is not intended to return anything.......we must have its return type "void".

You can compile the source code using ansi compliance,

bye,
sharath

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.

See this

Hi,
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

thanks,
SHARATH CHANDRA

>Thanks yaar for opening my eyes on the very first line of my everyday code.
:wink:

>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.