- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- language and scripting
Operating System - HP-UX
1752538
Members
5012
Online
108788
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-09-2010 05:30 AM
тАО07-09-2010 05:30 AM
Re: language and scripting
the only way I see is using getopt with the while loop I gave you just above.
char * v2;
char *allcparms[256]; /* if we suppose the max param is 256.
int indexcparms=0;
......
case 'c':
if (indexcparms==256) errflg++;
else
allcparms[indexcparms++]=optarg;
while(indexcparms<256 && (v2=argv[optind]))
{
if ( v2[0]=='-' ) break;
allcparms[indexcparms++]=v2;
optind++;
}
break;
my allcparms is your optargs.
Now there is an other way which overload the argv
case 'c':
argv[optind-1]=optarg;
params=&argv[optind-1];
while((v2=argv[optind]) && (v2[0]!='-')) optind++;
break;
but then your argv will be changed.
optarg is always a pointer in argv[optind-1] string
but depends on if the option is like
getopt("c:x") will give:
-c arg1 -> optarg=optarg[optind-1]
or
-carg1 -> optarg=optarg[optind-1]+1
-xcarg1 -> optarg=optarg[optind-1]+2
So all getopt() is based on pointing in argv to avoid any other allocations.
The only problem of my last proposition is it modifies argv[] string array, so you can't parse it again.
now If you really want dynamic allocation
it can also be made .
but there is no std api.
char * v2;
char *allcparms[256]; /* if we suppose the max param is 256.
int indexcparms=0;
......
case 'c':
if (indexcparms==256) errflg++;
else
allcparms[indexcparms++]=optarg;
while(indexcparms<256 && (v2=argv[optind]))
{
if ( v2[0]=='-' ) break;
allcparms[indexcparms++]=v2;
optind++;
}
break;
my allcparms is your optargs.
Now there is an other way which overload the argv
case 'c':
argv[optind-1]=optarg;
params=&argv[optind-1];
while((v2=argv[optind]) && (v2[0]!='-')) optind++;
break;
but then your argv will be changed.
optarg is always a pointer in argv[optind-1] string
but depends on if the option is like
getopt("c:x") will give:
-c arg1 -> optarg=optarg[optind-1]
or
-carg1 -> optarg=optarg[optind-1]+1
-xcarg1 -> optarg=optarg[optind-1]+2
So all getopt() is based on pointing in argv to avoid any other allocations.
The only problem of my last proposition is it modifies argv[] string array, so you can't parse it again.
now If you really want dynamic allocation
it can also be made .
but there is no std api.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-09-2010 05:43 PM
тАО07-09-2010 05:43 PM
Re: language and scripting
>that is there any standard function is there like getopt using which get my variable number of arguments using some standard variable like optarg in getopt
No, you'll have to design your own. Or take Bill's and JRF's suggestion to merge the argument components into just one. Splitting those on spaces, commas or colons is easy, you could use strtok(3).
Also, I don't think Laurent's example where he changes optind will work on Linux?
I recently tried to port a HP-UX program that used getopt(3) to Linux that allowed files and options to be in any order and I wasn't successful.
>Laurent:
extern char *optarg;
extern int optind, optopt;
You really should NOT declare these but take them from their header.
But perhaps you copied this bad code from the man page. :-)
No, you'll have to design your own. Or take Bill's and JRF's suggestion to merge the argument components into just one. Splitting those on spaces, commas or colons is easy, you could use strtok(3).
Also, I don't think Laurent's example where he changes optind will work on Linux?
I recently tried to port a HP-UX program that used getopt(3) to Linux that allowed files and options to be in any order and I wasn't successful.
>Laurent:
extern char *optarg;
extern int optind, optopt;
You really should NOT declare these but take them from their header
But perhaps you copied this bad code from the man page. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-11-2010 09:57 AM
тАО07-11-2010 09:57 AM
Re: language and scripting
yes indeed you guess well , I took the skleton, without reading at it from the manpage.
here is the full example tested on
hpux & debian 5 ia64
- I fixed the test I made a little quickly on -f option.
$ ./a.out -afaa bb -c aa bb cc -o 1 2 3 4 5555 -- z t u
f= aa..bb..1
allcparms:aa
allcparms:bb
allcparms:cc
parms:1
parms:2
parms:3
parms:4
parms:5555
14 z
15 t
16 u
#include
#include
int i;
main (int argc, char *argv[])
{
int c;
int bflg, aflg=0, errflg=0;
char *t;
char *ifile,*ofile;
char *allcparms[256];
char **parms=0;
int nparms=0;
int icparms=0;
while ((c = getopt(argc, argv,
":abf:o:c:")) != -1)
switch (c) {
case 'a':
aflg++;
break;
case 'f':
ifile = optarg;
if (optind+1>argc)
errflg++;/*not enough parms with -f ( 2 parms) */
else
ofile = argv[ optind++];
break;
case 'c':
if(icparms==256)
{
errflg++; break;
}
allcparms[icparms++]=optarg;
while ( icparms<256 &&
(t=argv[optind]))
{
if (t[0] == '-' ) break;
allcparms[icparms++]=t;
optind++;
}
break;
case 'o':
argv[optind-1]=optarg;
parms=&argv[optind-1];
nparms=1;
while ((t=argv[optind]) &&
(t[0]!='-'))
{
optind++; nparms++;
}
break;
case ':':
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 (i=0;i printf("allcparms:%s\n",allcparms[i]);
while (nparms--)
printf("parms:%s\n",parms++[0]);
for ( ; optind < argc; optind++)
printf("%d %s\n",optind,argv[optind]);
}
here is the full example tested on
hpux & debian 5 ia64
- I fixed the test I made a little quickly on -f option.
$ ./a.out -afaa bb -c aa bb cc -o 1 2 3 4 5555 -- z t u
f= aa..bb..1
allcparms:aa
allcparms:bb
allcparms:cc
parms:1
parms:2
parms:3
parms:4
parms:5555
14 z
15 t
16 u
#include
#include
int i;
main (int argc, char *argv[])
{
int c;
int bflg, aflg=0, errflg=0;
char *t;
char *ifile,*ofile;
char *allcparms[256];
char **parms=0;
int nparms=0;
int icparms=0;
while ((c = getopt(argc, argv,
":abf:o:c:")) != -1)
switch (c) {
case 'a':
aflg++;
break;
case 'f':
ifile = optarg;
if (optind+1>argc)
errflg++;/*not enough parms with -f ( 2 parms) */
else
ofile = argv[ optind++];
break;
case 'c':
if(icparms==256)
{
errflg++; break;
}
allcparms[icparms++]=optarg;
while ( icparms<256 &&
(t=argv[optind]))
{
if (t[0] == '-' ) break;
allcparms[icparms++]=t;
optind++;
}
break;
case 'o':
argv[optind-1]=optarg;
parms=&argv[optind-1];
nparms=1;
while ((t=argv[optind]) &&
(t[0]!='-'))
{
optind++; nparms++;
}
break;
case ':':
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 (i=0;i
while (nparms--)
printf("parms:%s\n",parms++[0]);
for ( ; optind < argc; optind++)
printf("%d %s\n",optind,argv[optind]);
}
- « Previous
- Next »
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP