1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
15 *******************************************************************************
23 * global variables, types & constants
24 * may be removed if already defined
25 *******************************************************************************
33 * internal values needed by getopt
34 * DO NOT CHANGE or REMOVE
43 * variables needed by getopt & getopt_long!
46 static char *optstart = NULL;
49 getopt(int argc, char **argv, const char *options)
59 * reset used global values
65 * reset getopt if a new argv pointer is passed
67 if (optstart != argv[0]) {
75 * return if no more arguments are available
82 * start parsing argv[optind]
87 * return if the option does not begin with a '-' or has more than 2 characters
89 if (argv[optind][idx] != '-') {
92 printf("unknown option \'%s\', expecting \'-\'\n",
96 optopt = (int) argv[optind][idx];
103 * continue to the next character in argv[optind]
108 * identify the option
109 * make sure if an option contains a ':' to invalidate the option
111 optptr = strchr(argv[optind], ':');
113 if (optptr == NULL) {
114 optptr = strchr(options, (int) argv[optind][idx]);
120 * check whether the option is present
122 if (optptr == NULL) {
124 * unknown option detected
127 printf("unknown option \'%s\'\n", argv[optind]);
130 optopt = (int) argv[optind][idx];
137 * the option is present in the option string
143 * get option argument if needed
148 * determine between mandatory and optional argument
152 if (*optptr == ':') {
153 optman--; // now set to MANDATORY_ARG
156 if (optman == MANDATORY_ARG) {
159 if (*optptr == ':') {
160 optman--; // now set to OPTIONAL_ARG
166 * if strlen( argv[optind ) is greater than 2,
167 * the argument is in the same argv
169 if (strlen(argv[optind]) > 2) {
170 argptr = &argv[optind][2];
173 * do not allow '-' in an argument
175 if (strchr(argptr, '-') != NULL) {
179 ("illegal argument value \'%s\' for option \'-%c\'\n",
190 * move on to the next argv
191 * it now either contains an argument or the next option
196 * make sure not to overflow
199 argptr = argv[optind];
207 * do the needed actions for the argument state
212 if (argptr == NULL) {
216 if (*argptr != '-') {
229 argpresent = (argptr != NULL);
232 argpresent = (*argptr != '-');
243 * mandatory argument missing
247 ("missing argument for option \'-%c\'\n",
254 * if the first character of options is a ':'
255 * return a ':' instead of a '?' in case of
258 if (*options == ':') {
271 if (strlen(argv[optind - 1]) > 2) {
275 ("too many arguments for option \'-%c\'\n",
292 getopt_long(int argc, char **argv, const char *shortopts,
293 const struct option *longopts, int *indexptr)
295 struct option *optptr = (struct option *) longopts;
302 * reset used global values
313 * reset getopt if a new argv pointer is passed
315 if (optstart != argv[0]) {
323 * return if no more arguments are available
325 if (optind >= argc) {
330 * start parsing argv[optind]
335 * return if the option does not begin with a '-'
337 if (argv[optind][idx] != '-') {
338 printf("unknown option \'%s\', expecting \'-\'\n",
347 * move on to the next character in argv[optind]
352 * return getopt() in case of a short option
354 if (argv[optind][idx] != '-') {
355 return getopt(argc, argv, shortopts);
359 * handle a long option
363 while (optptr->name != NULL) {
365 if (strcmp(&argv[optind][idx], optptr->name) == 0) {
374 * no matching option found
376 if (optptr->name == NULL) {
377 printf("unknown option \'%s\'\n", argv[optind]);
385 * option was found, set up index pointer
394 switch (optptr->has_arg) {
402 case required_argument:
403 argpresent = (optind != argc);
406 argpresent = (argv[optind][0] != '-');
413 optarg = argv[optind];
417 * mandatory argument missing
419 printf("missing argument for option \'%s\'\n",
428 case optional_argument:
430 if (optind == argc) {
434 if (argv[optind][0] != '-') {
438 optarg = argv[optind];
446 printf("unknown argument option for option \'%s\'\n",
456 * setup return values
460 if (optptr->flag == NULL) {
463 *optptr->flag = optptr->val;