int getopt_main(int argc, char **argv) { int n; char *optstr = NULL; char *name = NULL; unsigned opt; const char *compatible; char *s_arg; #if ENABLE_FEATURE_GETOPT_LONG struct option *long_options = NULL; llist_t *l_arg = NULL; #endif compatible = getenv("GETOPT_COMPATIBLE"); /* used as yes/no flag */ if (!argv[1]) { if (compatible) { /* For some reason, the original getopt gave no error when there were no arguments. */ printf(" --\n"); return 0; } bb_error_msg_and_die("missing optstring argument"); } if (argv[1][0] != '-' || compatible) { char *s = argv[1]; option_mask32 |= OPT_u; /* quoting off */ s = xstrdup(s + strspn(s, "-+")); argv[1] = argv[0]; return generate_output(argv+1, argc-1, s, long_options); } #if !ENABLE_FEATURE_GETOPT_LONG opt = getopt32(argv, "+o:n:qQs:Tu", &optstr, &name, &s_arg); #else applet_long_options = getopt_longopts; opt_complementary = "l::"; opt = getopt32(argv, "+o:n:qQs:Tual:", &optstr, &name, &s_arg, &l_arg); /* Effectuate the read options for the applet itself */ while (l_arg) { long_options = add_long_options(long_options, llist_pop(&l_arg)); } #endif if (opt & OPT_s) { set_shell(s_arg); } if (opt & OPT_T) { return 4; } /* All options controlling the applet have now been parsed */ n = optind - 1; if (!optstr) { optstr = argv[++n]; if (!optstr) bb_error_msg_and_die("missing optstring argument"); } argv[n] = name ? name : argv[0]; return generate_output(argv + n, argc - n, optstr, long_options); }
int getopt_main(int argc, char *argv[]) { char *optstr=NULL; char *name=NULL; int opt; int compatible=0; init_longopt(); if (getenv("GETOPT_COMPATIBLE")) compatible=1; if (argc == 1) { if (compatible) { /* For some reason, the original getopt gave no error when there were no arguments. */ printf(" --\n"); exit(0); } else error_msg_and_die("missing optstring argument"); } if (argv[1][0] != '-' || compatible) { quote=0; optstr=xmalloc(strlen(argv[1])+1); strcpy(optstr,argv[1]+strspn(argv[1],"-+")); argv[1]=argv[0]; exit(generate_output(argv+1,argc-1,optstr,long_options)); } while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) switch (opt) { case 'a': alternative=1; break; case 'o': if (optstr) free(optstr); optstr=xmalloc(strlen(optarg)+1); strcpy(optstr,optarg); break; case 'l': add_long_options(optarg); break; case 'n': if (name) free(name); name=xmalloc(strlen(optarg)+1); strcpy(name,optarg); break; case 'q': quiet_errors=1; break; case 'Q': quiet_output=1; break; case 's': set_shell(optarg); break; case 'T': exit(4); case 'u': quote=0; break; default: show_usage(); } if (!optstr) { if (optind >= argc) error_msg_and_die("missing optstring argument"); else { optstr=xmalloc(strlen(argv[optind])+1); strcpy(optstr,argv[optind]); optind++; } } if (name) argv[optind-1]=name; else argv[optind-1]=argv[0]; exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options)); }
int main(int argc, char *argv[]) { char *optstr=NULL; char *name=NULL; int opt; int compatible=0; #if WITHOUT_GETTEXT #else setlocale(LC_ALL,""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); #endif init_longopt(); if (getenv("GETOPT_COMPATIBLE")) compatible=1; if (argc == 1) { if (compatible) { /* For some reason, the original getopt gave no error when there were no arguments. */ printf(" --\n"); exit(0); } else parse_error(_("missing optstring argument")); } if (argv[1][0] != '-' || compatible) { quote=0; optstr=our_malloc(strlen(argv[1])+1); strcpy(optstr,argv[1]+strspn(argv[1],"-+")); argv[1]=argv[0]; exit(generate_output(argv+1,argc-1,optstr,long_options)); } while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) switch (opt) { case 'a': alternative=1; break; case 'h': print_help(); exit(0); case 'o': if (optstr) free(optstr); optstr=our_malloc(strlen(optarg)+1); strcpy(optstr,optarg); break; case 'l': add_long_options(optarg); break; case 'n': if (name) free(name); name=our_malloc(strlen(optarg)+1); strcpy(name,optarg); break; case 'q': quiet_errors=1; break; case 'Q': quiet_output=1; break; case 's': set_shell(optarg); break; case 'T': exit(4); case 'u': quote=0; break; case 'V': printf(_("getopt (enhanced) 1.1.3\n")); exit(0); case '?': case ':': parse_error(NULL); default: parse_error(_("internal error, contact the author.")); } if (!optstr) { if (optind >= argc) parse_error(_("missing optstring argument")); else { optstr=our_malloc(strlen(argv[optind])+1); strcpy(optstr,argv[optind]); optind++; } } if (name) argv[optind-1]=name; else argv[optind-1]=argv[0]; exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options)); }
int main(int argc, char *argv[]) { char *optstr = NULL; char *name = NULL; int opt; int compatible = 0; /* Stop scanning as soon as a non-option argument is found! */ static const char *shortopts = "+ao:l:n:qQs:TuhV"; static const struct option longopts[] = { {"options", required_argument, NULL, 'o'}, {"longoptions", required_argument, NULL, 'l'}, {"quiet", no_argument, NULL, 'q'}, {"quiet-output", no_argument, NULL, 'Q'}, {"shell", required_argument, NULL, 's'}, {"test", no_argument, NULL, 'T'}, {"unquoted", no_argument, NULL, 'u'}, {"help", no_argument, NULL, 'h'}, {"alternative", no_argument, NULL, 'a'}, {"name", required_argument, NULL, 'n'}, {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); init_longopt(); getopt_long_fp = getopt_long; if (getenv("GETOPT_COMPATIBLE")) compatible = 1; if (argc == 1) { if (compatible) { /* * For some reason, the original getopt gave no * error when there were no arguments. */ printf(" --\n"); return EXIT_SUCCESS; } else parse_error(_("missing optstring argument")); } if (argv[1][0] != '-' || compatible) { quote = 0; optstr = xmalloc(strlen(argv[1]) + 1); strcpy(optstr, argv[1] + strspn(argv[1], "-+")); argv[1] = argv[0]; return generate_output(argv + 1, argc - 1, optstr, long_options); } while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) != EOF) switch (opt) { case 'a': getopt_long_fp = getopt_long_only; break; case 'h': print_help(); case 'o': free(optstr); optstr = xmalloc(strlen(optarg) + 1); strcpy(optstr, optarg); break; case 'l': add_long_options(optarg); break; case 'n': free(name); name = xmalloc(strlen(optarg) + 1); strcpy(name, optarg); break; case 'q': quiet_errors = 1; break; case 'Q': quiet_output = 1; break; case 's': set_shell(optarg); break; case 'T': return TEST_EXIT_CODE; case 'u': quote = 0; break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case '?': case ':': parse_error(NULL); default: parse_error(_("internal error, contact the author.")); } if (!optstr) { if (optind >= argc) parse_error(_("missing optstring argument")); else { optstr = xmalloc(strlen(argv[optind]) + 1); strcpy(optstr, argv[optind]); optind++; } } if (name) argv[optind - 1] = name; else argv[optind - 1] = argv[0]; return generate_output(argv + optind - 1, argc-optind + 1, optstr, long_options); }