Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/util-linux/getopt.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/util-linux/getopt.c
r2725 r3232 26 26 * <misiek@misiek.eu.org>) 27 27 * Ported to Busybox - Alfred M. Szmidt <ams@trillian.itslinux.org> 28 * Removed --version/-V and --help/-h in28 * Removed --version/-V and --help/-h 29 29 * Removed parse_error(), using bb_error_msg() from Busybox instead 30 30 * Replaced our_malloc with xmalloc and our_realloc with xrealloc … … 32 32 */ 33 33 34 #include <getopt.h> 34 //usage:#define getopt_trivial_usage 35 //usage: "[OPTIONS] [--] OPTSTRING PARAMS" 36 //usage:#define getopt_full_usage "\n\n" 37 //usage: IF_LONG_OPTS( 38 //usage: " -a,--alternative Allow long options starting with single -" 39 //usage: "\n -l,--longoptions=LOPT[,...] Long options to be recognized" 40 //usage: "\n -n,--name=PROGNAME The name under which errors are reported" 41 //usage: "\n -o,--options=OPTSTRING Short options to be recognized" 42 //usage: "\n -q,--quiet Disable error reporting by getopt(3)" 43 //usage: "\n -Q,--quiet-output No normal output" 44 //usage: "\n -s,--shell=SHELL Set shell quoting conventions" 45 //usage: "\n -T,--test Test for getopt(1) version" 46 //usage: "\n -u,--unquoted Don't quote the output" 47 //usage: ) 48 //usage: IF_NOT_LONG_OPTS( 49 //usage: " -a Allow long options starting with single -" 50 //usage: "\n -l LOPT[,...] Long options to be recognized" 51 //usage: "\n -n PROGNAME The name under which errors are reported" 52 //usage: "\n -o OPTSTRING Short options to be recognized" 53 //usage: "\n -q Disable error reporting by getopt(3)" 54 //usage: "\n -Q No normal output" 55 //usage: "\n -s SHELL Set shell quoting conventions" 56 //usage: "\n -T Test for getopt(1) version" 57 //usage: "\n -u Don't quote the output" 58 //usage: ) 59 //usage: "\n" 60 //usage: "\nExample:" 61 //usage: "\n" 62 //usage: "\nO=`getopt -l bb: -- ab:c:: \"$@\"` || exit 1" 63 //usage: "\neval set -- \"$O\"" 64 //usage: "\nwhile true; do" 65 //usage: "\n case \"$1\" in" 66 //usage: "\n -a) echo A; shift;;" 67 //usage: "\n -b|--bb) echo \"B:'$2'\"; shift 2;;" 68 //usage: "\n -c) case \"$2\" in" 69 //usage: "\n \"\") echo C; shift 2;;" 70 //usage: "\n *) echo \"C:'$2'\"; shift 2;;" 71 //usage: "\n esac;;" 72 //usage: "\n --) shift; break;;" 73 //usage: "\n *) echo Error; exit 1;;" 74 //usage: "\n esac" 75 //usage: "\ndone" 76 //usage: 77 //usage:#define getopt_example_usage 78 //usage: "$ cat getopt.test\n" 79 //usage: "#!/bin/sh\n" 80 //usage: "GETOPT=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \\\n" 81 //usage: " -n 'example.busybox' -- \"$@\"`\n" 82 //usage: "if [ $? != 0 ]; then exit 1; fi\n" 83 //usage: "eval set -- \"$GETOPT\"\n" 84 //usage: "while true; do\n" 85 //usage: " case $1 in\n" 86 //usage: " -a|--a-long) echo \"Option a\"; shift;;\n" 87 //usage: " -b|--b-long) echo \"Option b, argument '$2'\"; shift 2;;\n" 88 //usage: " -c|--c-long)\n" 89 //usage: " case \"$2\" in\n" 90 //usage: " \"\") echo \"Option c, no argument\"; shift 2;;\n" 91 //usage: " *) echo \"Option c, argument '$2'\"; shift 2;;\n" 92 //usage: " esac;;\n" 93 //usage: " --) shift; break;;\n" 94 //usage: " *) echo \"Internal error!\"; exit 1;;\n" 95 //usage: " esac\n" 96 //usage: "done\n" 97 98 #if ENABLE_FEATURE_GETOPT_LONG 99 # include <getopt.h> 100 #endif 35 101 #include "libbb.h" 36 102 … … 291 357 int getopt_main(int argc, char **argv) 292 358 { 359 int n; 293 360 char *optstr = NULL; 294 361 char *name = NULL; … … 303 370 compatible = getenv("GETOPT_COMPATIBLE"); /* used as yes/no flag */ 304 371 305 if ( argc == 1) {372 if (!argv[1]) { 306 373 if (compatible) { 307 374 /* For some reason, the original getopt gave no error 308 375 * when there were no arguments. */ 309 376 printf(" --\n"); 310 377 return 0; … … 314 381 315 382 if (argv[1][0] != '-' || compatible) { 316 char *s ;383 char *s = argv[1]; 317 384 318 385 option_mask32 |= OPT_u; /* quoting off */ 319 s = xstrdup( argv[1] + strspn(argv[1], "-+"));386 s = xstrdup(s + strspn(s, "-+")); 320 387 argv[1] = argv[0]; 321 388 return generate_output(argv+1, argc-1, s, long_options); … … 344 411 345 412 /* All options controlling the applet have now been parsed */ 413 n = optind - 1; 346 414 if (!optstr) { 347 if (optind >= argc) 415 optstr = argv[++n]; 416 if (!optstr) 348 417 bb_error_msg_and_die("missing optstring argument"); 349 optstr = argv[optind++]; 350 } 351 352 argv[optind-1] = name ? name : argv[0]; 353 return generate_output(argv+optind-1, argc-optind+1, optstr, long_options); 418 } 419 420 argv[n] = name ? name : argv[0]; 421 return generate_output(argv + n, argc - n, optstr, long_options); 354 422 }
Note:
See TracChangeset
for help on using the changeset viewer.