1754368 Members
4769 Online
108813 Solutions
New Discussion юеВ

language and scripting

 
SOLVED
Go to solution
Soumya Poddar
Advisor

language and scripting

I am new in Unix
please forgive me if my question looks
very simple to you.


I am writing this

program in c
.....
while((c=getopt(..,..,..))!=-1)
{
...
...
......
}

but my two of my options in my program need more than two arguments
how do I do it please help.
22 REPLIES 22
Laurent Menase
Honored Contributor
Solution

Re: language and scripting


#include
#include
main (int argc, char *argv[])
{
int c;
int bflg, aflg=0, errflg=0;
extern char *optarg;
char *ifile,*ofile;

extern int optind, optopt;
while ((c = getopt(argc, argv, ":abf:o:")) != -1)
switch (c) {
case 'a':
aflg++;
break;
case 'f':
ifile = optarg++;
if (optarg[0]=='-')
errflg++;
else
{
ofile = ++optarg;
optind++;
}
break;
case ':': /* -f or -o without arguments */
fprintf(stderr, "Option -%c requires an argument\n",
optopt);
errflg++;
break;
case '?':
fprintf(stderr, "Unrecognized option: - %c\n",
optopt);
errflg++;
}
if (errflg) {
fprintf(stderr, "usage: . . . ");
exit (2);
}
printf("f= %s..%s..%d\n",ifile,ofile,aflg);
for ( ; optind < argc; optind++)
printf("%d %s\n",optind,argv[optind]);
}
Soumya Poddar
Advisor

Re: language and scripting

Thank you
Soumya Poddar
Advisor

Re: language and scripting

sir
your program is not taking two arguments
i takes first arguments properly but for the second one the program takes it as a string except the first two charecters of the first argument..
I want to mean that

if input is

./a.out -f 4567 7890

the output gets
f= 4567..67..0

so it does not taking the second argument


please help sir
Laurent Menase
Honored Contributor

Re: language and scripting

indeed it is
case 'f':
ifile = optarg;
if (optarg[0]=='-')
errflg++;
else
{
ofile = argv[ optind++];
}
break;


sorry
Soumya Poddar
Advisor

Re: language and scripting

thank you
sir
it helps me a lot
Soumya Poddar
Advisor

Re: language and scripting

actually here the getopt does not parse the argument by it-self fully
but our intervention is needed there
is there any way to avoid this
is there any function of technique is there

plese help me sir
Laurent Menase
Honored Contributor

Re: language and scripting

can you clarify?
I am not sure I understand what you are trying to do.

just an example could help

Soumya Poddar
Advisor

Re: language and scripting

actually

we write
while((c=getopt(...,...,":abc:")!=-1)
{
....
....
....
}

here the getopt can easily understand that the option c should need a argument with it
hence, it searchs for the argument when it sees the option -c in the string of one of it's strings stored in argv[] vector.

just like that do we have any other kind of option where the function it self searches for the two arguments or the argument list.


please help
sir

if u need any more clarification please reply me
Laurent Menase
Honored Contributor

Re: language and scripting

I am sorry I don't follow

./a.out -f toto tata -c arg1 arg2 arg3 arg4 ... -z
you want to get the list of -c args?