1 /* Borrowed from Apache NT Port */
8 #define OPTERRCOLON (1)
15 static int ap_opterr
= 1;
19 ap_optiserr(int argc
, char * const *argv
, int oint
, const char *optstr
,
24 fprintf(stderr
, "Error in argument %d, char %d: ", oint
, optchr
+1);
28 fprintf(stderr
, ": in flags\n");
31 fprintf(stderr
, "option not found %c\n", argv
[oint
][optchr
]);
34 fprintf(stderr
, "no argument for option %c\n", argv
[oint
][optchr
]);
37 fprintf(stderr
, "unknown\n");
41 ap_optopt
= argv
[oint
][optchr
];
45 int ap_getopt(int argc
, char* const *argv
, const char *optstr
)
47 static int optchr
= 0;
48 static int dash
= 0; /* have already seen the - */
52 if (ap_optind
>= argc
)
54 if (!dash
&& (argv
[ap_optind
][0] != '-'))
56 if (!dash
&& (argv
[ap_optind
][0] == '-') && !argv
[ap_optind
][1])
59 * use to specify stdin. Need to let pgm process this and
64 if ((argv
[ap_optind
][0] == '-') && (argv
[ap_optind
][1] == '-'))
66 /* -- indicates end of args */
72 assert((argv
[ap_optind
][0] == '-') && argv
[ap_optind
][1]);
77 /* Check if the guy tries to do a -: kind of flag */
79 if (argv
[ap_optind
][optchr
] == ':')
83 return(ap_optiserr(argc
, argv
, ap_optind
-1, optstr
, optchr
, OPTERRCOLON
));
85 if (!(cp
= strchr(optstr
, argv
[ap_optind
][optchr
])))
87 int errind
= ap_optind
;
90 if (!argv
[ap_optind
][optchr
+1])
97 return(ap_optiserr(argc
, argv
, errind
, optstr
, errchr
, OPTERRNF
));
101 /* Check for cases where the value of the argument
102 is in the form -<arg> <val> or in the form -<arg><val> */
104 if(!argv
[ap_optind
][2]) {
106 if (ap_optind
== argc
)
107 return(ap_optiserr(argc
, argv
, ap_optind
-1, optstr
, optchr
, OPTERRARG
));
108 ap_optarg
= argv
[ap_optind
++];
112 ap_optarg
= &argv
[ap_optind
][2];
119 if (!argv
[ap_optind
][optchr
+1])
134 main (int argc
, char **argv
)
137 extern char *ap_optarg
;
138 extern int ap_optind
;
144 while ((c
= ap_getopt(argc
, argv
, "abo:")) != EOF
)
160 (void)printf("ofile = %s\n", ofile
);
166 (void)fprintf(stderr
,
167 "usage: cmd [-a|-b] [-o <filename>] files...\n");
170 for ( ; ap_optind
< argc
; ap_optind
++)
171 (void)printf("%s\n", argv
[ap_optind
]);
175 #endif /* TESTGETOPT */