aboutsummaryrefslogtreecommitdiff
path: root/util-linux/getopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/getopt.c')
-rw-r--r--util-linux/getopt.c51
1 files changed, 35 insertions, 16 deletions
diff --git a/util-linux/getopt.c b/util-linux/getopt.c
index c45edf8ca..d662c813a 100644
--- a/util-linux/getopt.c
+++ b/util-linux/getopt.c
@@ -32,30 +32,47 @@
32 */ 32 */
33 33
34//usage:#define getopt_trivial_usage 34//usage:#define getopt_trivial_usage
35//usage: "[OPTIONS]" 35//usage: "[OPTIONS] [--] OPTSTRING PARAMS"
36//usage:#define getopt_full_usage "\n\n" 36//usage:#define getopt_full_usage "\n\n"
37//usage: IF_LONG_OPTS( 37//usage: IF_LONG_OPTS(
38//usage: " -a,--alternative Allow long options starting with single -" 38//usage: " -a,--alternative Allow long options starting with single -"
39//usage: "\n -l,--longoptions=longopts Long options to be recognized" 39//usage: "\n -l,--longoptions=LOPT[,...] Long options to be recognized"
40//usage: "\n -n,--name=progname The name under which errors are reported" 40//usage: "\n -n,--name=PROGNAME The name under which errors are reported"
41//usage: "\n -o,--options=optstring Short options to be recognized" 41//usage: "\n -o,--options=OPTSTRING Short options to be recognized"
42//usage: "\n -q,--quiet Disable error reporting by getopt(3)" 42//usage: "\n -q,--quiet Disable error reporting by getopt(3)"
43//usage: "\n -Q,--quiet-output No normal output" 43//usage: "\n -Q,--quiet-output No normal output"
44//usage: "\n -s,--shell=shell Set shell quoting conventions" 44//usage: "\n -s,--shell=SHELL Set shell quoting conventions"
45//usage: "\n -T,--test Test for getopt(1) version" 45//usage: "\n -T,--test Test for getopt(1) version"
46//usage: "\n -u,--unquoted Don't quote the output" 46//usage: "\n -u,--unquoted Don't quote the output"
47//usage: ) 47//usage: )
48//usage: IF_NOT_LONG_OPTS( 48//usage: IF_NOT_LONG_OPTS(
49//usage: " -a Allow long options starting with single -" 49//usage: " -a Allow long options starting with single -"
50//usage: "\n -l longopts Long options to be recognized" 50//usage: "\n -l LOPT[,...] Long options to be recognized"
51//usage: "\n -n progname The name under which errors are reported" 51//usage: "\n -n PROGNAME The name under which errors are reported"
52//usage: "\n -o optstring Short options to be recognized" 52//usage: "\n -o OPTSTRING Short options to be recognized"
53//usage: "\n -q Disable error reporting by getopt(3)" 53//usage: "\n -q Disable error reporting by getopt(3)"
54//usage: "\n -Q No normal output" 54//usage: "\n -Q No normal output"
55//usage: "\n -s shell Set shell quoting conventions" 55//usage: "\n -s SHELL Set shell quoting conventions"
56//usage: "\n -T Test for getopt(1) version" 56//usage: "\n -T Test for getopt(1) version"
57//usage: "\n -u Don't quote the output" 57//usage: "\n -u Don't quote the output"
58//usage: ) 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"
59//usage: 76//usage:
60//usage:#define getopt_example_usage 77//usage:#define getopt_example_usage
61//usage: "$ cat getopt.test\n" 78//usage: "$ cat getopt.test\n"
@@ -339,6 +356,7 @@ static const char getopt_longopts[] ALIGN1 =
339int getopt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 356int getopt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
340int getopt_main(int argc, char **argv) 357int getopt_main(int argc, char **argv)
341{ 358{
359 int n;
342 char *optstr = NULL; 360 char *optstr = NULL;
343 char *name = NULL; 361 char *name = NULL;
344 unsigned opt; 362 unsigned opt;
@@ -351,7 +369,7 @@ int getopt_main(int argc, char **argv)
351 369
352 compatible = getenv("GETOPT_COMPATIBLE"); /* used as yes/no flag */ 370 compatible = getenv("GETOPT_COMPATIBLE"); /* used as yes/no flag */
353 371
354 if (argc == 1) { 372 if (!argv[1]) {
355 if (compatible) { 373 if (compatible) {
356 /* For some reason, the original getopt gave no error 374 /* For some reason, the original getopt gave no error
357 when there were no arguments. */ 375 when there were no arguments. */
@@ -362,10 +380,10 @@ int getopt_main(int argc, char **argv)
362 } 380 }
363 381
364 if (argv[1][0] != '-' || compatible) { 382 if (argv[1][0] != '-' || compatible) {
365 char *s; 383 char *s = argv[1];
366 384
367 option_mask32 |= OPT_u; /* quoting off */ 385 option_mask32 |= OPT_u; /* quoting off */
368 s = xstrdup(argv[1] + strspn(argv[1], "-+")); 386 s = xstrdup(s + strspn(s, "-+"));
369 argv[1] = argv[0]; 387 argv[1] = argv[0];
370 return generate_output(argv+1, argc-1, s, long_options); 388 return generate_output(argv+1, argc-1, s, long_options);
371 } 389 }
@@ -392,12 +410,13 @@ int getopt_main(int argc, char **argv)
392 } 410 }
393 411
394 /* All options controlling the applet have now been parsed */ 412 /* All options controlling the applet have now been parsed */
413 n = optind - 1;
395 if (!optstr) { 414 if (!optstr) {
396 if (optind >= argc) 415 optstr = argv[++n];
416 if (!optstr)
397 bb_error_msg_and_die("missing optstring argument"); 417 bb_error_msg_and_die("missing optstring argument");
398 optstr = argv[optind++];
399 } 418 }
400 419
401 argv[optind-1] = name ? name : argv[0]; 420 argv[n] = name ? name : argv[0];
402 return generate_output(argv+optind-1, argc-optind+1, optstr, long_options); 421 return generate_output(argv + n, argc - n, optstr, long_options);
403} 422}