diff --git a/archival/ar.c b/archival/ar.c index 46c10aad4..027cd6b5a 100644 --- a/archival/ar.c +++ b/archival/ar.c @@ -243,10 +243,13 @@ int ar_main(int argc UNUSED_PARAM, char **argv) /* prepend '-' to the first argument if required */ if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') argv[1] = xasprintf("-%s", argv[1]); - /* -1: at least one param is reqd */ - /* one of p,t,x[,r] is required */ - opt_complementary = "-1:p:t:x"IF_FEATURE_AR_CREATE(":r"); - opt = getopt32(argv, "voc""ptx"IF_FEATURE_AR_CREATE("r")); + opt = getopt32(argv, "^" + "voc""ptx"IF_FEATURE_AR_CREATE("r") + "\0" + /* -1: at least one arg is reqd */ + /* one of p,t,x[,r] is required */ + "-1:p:t:x"IF_FEATURE_AR_CREATE(":r") + ); argv += optind; t = opt / FIRST_CMD; diff --git a/archival/bzip2.c b/archival/bzip2.c index 0b9c508df..d578eb7ad 100644 --- a/archival/bzip2.c +++ b/archival/bzip2.c @@ -195,9 +195,11 @@ int bzip2_main(int argc UNUSED_PARAM, char **argv) * --best alias for -9 */ - opt_complementary = "s2"; /* -s means -2 (compatibility) */ - /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */ - opt = getopt32(argv, "cfkv" IF_FEATURE_BZIP2_DECOMPRESS("dt") "123456789qzs"); + opt = getopt32(argv, "^" + /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */ + "cfkv" IF_FEATURE_BZIP2_DECOMPRESS("dt") "123456789qzs" + "\0" "s2" /* -s means -2 (compatibility) */ + ); #if ENABLE_FEATURE_BZIP2_DECOMPRESS /* bunzip2_main may not be visible... */ if (opt & 0x30) // -d and/or -t return bunzip2_main(argc, argv); diff --git a/archival/dpkg_deb.c b/archival/dpkg_deb.c index 029bc4af1..f6bf9eb04 100644 --- a/archival/dpkg_deb.c +++ b/archival/dpkg_deb.c @@ -80,8 +80,9 @@ int dpkg_deb_main(int argc UNUSED_PARAM, char **argv) #endif /* Must have 1 or 2 args */ - opt_complementary = "-1:?2:c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX"; - opt = getopt32(argv, "cefXx"); + opt = getopt32(argv, "^" "cefXx" + "\0" "-1:?2:c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX" + ); argv += optind; //argc -= optind; diff --git a/archival/lzop.c b/archival/lzop.c index df18ff170..1bf954f4f 100644 --- a/archival/lzop.c +++ b/archival/lzop.c @@ -1138,7 +1138,7 @@ int lzop_main(int argc UNUSED_PARAM, char **argv) /* -U is "anti -k", invert bit for bbunpack(): */ option_mask32 ^= OPT_KEEP; /* -k disables -U (if any): */ - /* opt_complementary = "k-U"; - nope, only handles -Uk, not -kU */ + /* opt_complementary "k-U"? - nope, only handles -Uk, not -kU */ if (option_mask32 & OPT_k) option_mask32 |= OPT_KEEP; diff --git a/archival/tar.c b/archival/tar.c index 44ab246c0..73c14ca81 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -973,18 +973,6 @@ int tar_main(int argc UNUSED_PARAM, char **argv) /* Prepend '-' to the first argument if required */ if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') argv[1] = xasprintf("-%s", argv[1]); - opt_complementary = - "tt:vv:" // count -t,-v -#if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM - "\xff::" // --exclude=PATTERN is a list -#endif - IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd - IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive - IF_NOT_FEATURE_TAR_CREATE("t--x:x--t") // mutually exclusive -#if ENABLE_FEATURE_TAR_LONG_OPTIONS - ":\xf9+" // --strip-components=NUM -#endif - ; #if ENABLE_DESKTOP /* Lie to buildroot when it starts asking stupid questions. */ if (argv[1] && strcmp(argv[1], "--version") == 0) { @@ -1021,7 +1009,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv) } } #endif - opt = GETOPT32(argv, + opt = GETOPT32(argv, "^" "txC:f:Oopvk" IF_FEATURE_TAR_CREATE( "ch" ) IF_FEATURE_SEAMLESS_BZ2( "j" ) @@ -1032,6 +1020,17 @@ int tar_main(int argc UNUSED_PARAM, char **argv) IF_FEATURE_SEAMLESS_Z( "Z" ) IF_FEATURE_TAR_NOPRESERVE_TIME("m") IF_FEATURE_TAR_LONG_OPTIONS("\xf9:") // --strip-components + "\0" + "tt:vv:" // count -t,-v +#if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM + "\xff::" // --exclude=PATTERN is a list +#endif + IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd + IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive + IF_NOT_FEATURE_TAR_CREATE("t--x:x--t") // mutually exclusive +#if ENABLE_FEATURE_TAR_LONG_OPTIONS + ":\xf9+" // --strip-components=NUM +#endif LONGOPTS , &base_dir // -C dir , &tar_filename // -f filename diff --git a/console-tools/loadfont.c b/console-tools/loadfont.c index 623d98175..81d0c3db4 100644 --- a/console-tools/loadfont.c +++ b/console-tools/loadfont.c @@ -348,8 +348,7 @@ int loadfont_main(int argc UNUSED_PARAM, char **argv) unsigned char *buffer; // no arguments allowed! - opt_complementary = "=0"; - getopt32(argv, ""); + getopt32(argv, "^" "" "\0" "=0"); /* * We used to look at the length of the input file @@ -437,8 +436,7 @@ int setfont_main(int argc UNUSED_PARAM, char **argv) char *mapfilename; const char *tty_name = CURRENT_TTY; - opt_complementary = "=1"; - opts = getopt32(argv, "m:C:", &mapfilename, &tty_name); + opts = getopt32(argv, "^" "m:C:" "\0" "=1", &mapfilename, &tty_name); argv += optind; fd = xopen_nonblocking(tty_name); diff --git a/console-tools/setconsole.c b/console-tools/setconsole.c index 8f4b7b7a6..bad2b76e4 100644 --- a/console-tools/setconsole.c +++ b/console-tools/setconsole.c @@ -47,8 +47,7 @@ int setconsole_main(int argc UNUSED_PARAM, char **argv) int reset; /* at most one non-option argument */ - opt_complementary = "?1"; - reset = getopt32(argv, "r"); + reset = getopt32(argv, "^" "r" "\0" "?1"); argv += 1 + reset; if (*argv) { diff --git a/coreutils/cat.c b/coreutils/cat.c index 390254512..7e35fa5ee 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c @@ -170,9 +170,11 @@ int cat_main(int argc UNUSED_PARAM, char **argv) { unsigned opts; - IF_FEATURE_CATV(opt_complementary = "Aetv"; /* -A == -vet */) - /* -u is ignored ("unbuffered") */ - opts = getopt32(argv, IF_FEATURE_CATV("etvA") IF_FEATURE_CATN("nb") "u"); + opts = getopt32(argv, IF_FEATURE_CATV("^") + /* -u is ignored ("unbuffered") */ + IF_FEATURE_CATV("etvA")IF_FEATURE_CATN("nb")"u" + IF_FEATURE_CATV("\0" "Aetv" /* -A == -vet */) + ); argv += optind; /* Read from stdin if there's nothing else to do. */ diff --git a/coreutils/chmod.c b/coreutils/chmod.c index 2174334d1..88989bf67 100644 --- a/coreutils/chmod.c +++ b/coreutils/chmod.c @@ -123,8 +123,7 @@ int chmod_main(int argc UNUSED_PARAM, char **argv) } /* Parse options */ - opt_complementary = "-2"; - getopt32(argv, ("-"OPT_STR) + 1); /* Reuse string */ + getopt32(argv, "^" OPT_STR "\0" "-2"); argv += optind; /* Restore option-like mode if needed */ diff --git a/coreutils/chown.c b/coreutils/chown.c index 0c77529ec..985d18d6f 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c @@ -55,7 +55,7 @@ /* This is a NOEXEC applet. Be very careful! */ -#define OPT_STR ("Rh" IF_DESKTOP("vcfLHP")) +#define OPT_STR "Rh" IF_DESKTOP("vcfLHP") #define BIT_RECURSE 1 #define OPT_RECURSE (opt & 1) #define OPT_NODEREF (opt & 2) @@ -127,11 +127,10 @@ int chown_main(int argc UNUSED_PARAM, char **argv) int opt, flags; struct param_t param; - opt_complementary = "-2"; #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS - opt = getopt32long(argv, OPT_STR, chown_longopts); + opt = getopt32long(argv, "^" OPT_STR "\0" "=2", chown_longopts); #else - opt = getopt32(argv, OPT_STR); + opt = getopt32(argv, "^" OPT_STR "\0" "=2"); #endif argv += optind; diff --git a/coreutils/comm.c b/coreutils/comm.c index 5be11468c..4bee77677 100644 --- a/coreutils/comm.c +++ b/coreutils/comm.c @@ -62,8 +62,7 @@ int comm_main(int argc UNUSED_PARAM, char **argv) int i; int order; - opt_complementary = "=2"; - getopt32(argv, "123"); + getopt32(argv, "^" "123" "\0" "=2"); argv += optind; for (i = 0; i < 2; ++i) { diff --git a/coreutils/cp.c b/coreutils/cp.c index fe408950a..5b34c27e7 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c @@ -73,15 +73,17 @@ int cp_main(int argc, char **argv) #endif }; - // Need at least two arguments - // Soft- and hardlinking doesn't mix - // -P and -d are the same (-P is POSIX, -d is GNU) - // -r and -R are the same - // -R (and therefore -r) turns on -d (coreutils does this) - // -a = -pdR - opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR"; #if ENABLE_FEATURE_CP_LONG_OPTIONS - flags = getopt32long(argv, FILEUTILS_CP_OPTSTR, + flags = getopt32long(argv, "^" + FILEUTILS_CP_OPTSTR + "\0" + // Need at least two arguments + // Soft- and hardlinking doesn't mix + // -P and -d are the same (-P is POSIX, -d is GNU) + // -r and -R are the same + // -R (and therefore -r) turns on -d (coreutils does this) + // -a = -pdR + "-2:l--s:s--l:Pd:rRd:Rd:apdR", "archive\0" No_argument "a" "force\0" No_argument "f" "interactive\0" No_argument "i" diff --git a/coreutils/cut.c b/coreutils/cut.c index 6578ce8ce..cdd90ab44 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c @@ -42,7 +42,7 @@ /* option vars */ -static const char optstring[] ALIGN1 = "b:c:f:d:sn"; +#define OPT_STR "b:c:f:d:sn" #define CUT_OPT_BYTE_FLGS (1 << 0) #define CUT_OPT_CHAR_FLGS (1 << 1) #define CUT_OPT_FIELDS_FLGS (1 << 2) @@ -201,8 +201,11 @@ int cut_main(int argc UNUSED_PARAM, char **argv) char *sopt, *ltok; unsigned opt; - opt_complementary = "b--bcf:c--bcf:f--bcf"; - opt = getopt32(argv, optstring, &sopt, &sopt, &sopt, <ok); + opt = getopt32(argv, "^" + OPT_STR + "\0" "b--bcf:c--bcf:f--bcf", + &sopt, &sopt, &sopt, <ok + ); // argc -= optind; argv += optind; if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS))) diff --git a/coreutils/date.c b/coreutils/date.c index 33f210434..5b15ce778 100644 --- a/coreutils/date.c +++ b/coreutils/date.c @@ -192,12 +192,16 @@ int date_main(int argc UNUSED_PARAM, char **argv) char *filename; char *isofmt_arg = NULL; - opt_complementary = "d--s:s--d" - IF_FEATURE_DATE_ISOFMT(":R--I:I--R"); - opt = getopt32long(argv, "Rs:ud:r:" - IF_FEATURE_DATE_ISOFMT("I::D:"), date_longopts, + opt = getopt32long(argv, "^" + "Rs:ud:r:" + IF_FEATURE_DATE_ISOFMT("I::D:") + "\0" + "d--s:s--d" + IF_FEATURE_DATE_ISOFMT(":R--I:I--R"), + date_longopts, &date_str, &date_str, &filename - IF_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt)); + IF_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt) + ); argv += optind; maybe_set_utc(opt); diff --git a/coreutils/df.c b/coreutils/df.c index 4d6534bc2..121da970b 100644 --- a/coreutils/df.c +++ b/coreutils/df.c @@ -115,15 +115,18 @@ int df_main(int argc UNUSED_PARAM, char **argv) init_unicode(); -#if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY - opt_complementary = "k-mB:m-Bk:B-km"; -#elif ENABLE_FEATURE_HUMAN_READABLE - opt_complementary = "k-m:m-k"; -#endif - opt = getopt32(argv, "kPT" + opt = getopt32(argv, "^" + "kPT" IF_FEATURE_DF_FANCY("aiB:") IF_FEATURE_HUMAN_READABLE("hm") - IF_FEATURE_DF_FANCY(, &chp)); + "\0" +#if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY + "k-mB:m-Bk:B-km" +#elif ENABLE_FEATURE_HUMAN_READABLE + "k-m:m-k" +#endif + IF_FEATURE_DF_FANCY(, &chp) + ); if (opt & OPT_MEGA) df_disp_hr = 1024*1024; diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index a3d008051..9f098e41e 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c @@ -120,8 +120,7 @@ int dos2unix_main(int argc UNUSED_PARAM, char **argv) } /* -u convert to unix, -d convert to dos */ - opt_complementary = "u--d:d--u"; /* mutually exclusive */ - o = getopt32(argv, "du"); + o = getopt32(argv, "^" "du" "\0" "u--d:d--u"); /* mutually exclusive */ /* Do the conversion requested by an argument else do the default * conversion depending on our name. */ diff --git a/coreutils/du.c b/coreutils/du.c index 947c46e74..d5ce46cf2 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -242,8 +242,11 @@ int du_main(int argc UNUSED_PARAM, char **argv) * ignore -a. This is consistent with -s being equivalent to -d 0. */ #if ENABLE_FEATURE_HUMAN_READABLE - opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s"; - opt = getopt32(argv, "aHkLsx" "d:+" "lc" "hm", &G.max_print_depth); + opt = getopt32(argv, "^" + "aHkLsxd:+lchm" + "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s", + &G.max_print_depth + ); argv += optind; if (opt & OPT_h_for_humans) { G.disp_unit = 0; @@ -255,8 +258,11 @@ int du_main(int argc UNUSED_PARAM, char **argv) G.disp_unit = 1024; } #else - opt_complementary = "H-L:L-H:s-d:d-s"; - opt = getopt32(argv, "aHkLsx" "d:+" "lc", &G.max_print_depth); + opt = getopt32(argv, "^" + "aHkLsxd:+lc" + "\0" "H-L:L-H:s-d:d-s", + &G.max_print_depth + ); argv += optind; #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K if (opt & OPT_k_kbytes) { diff --git a/coreutils/expand.c b/coreutils/expand.c index fa3ff18f4..91084b80b 100644 --- a/coreutils/expand.c +++ b/coreutils/expand.c @@ -174,9 +174,10 @@ int expand_main(int argc UNUSED_PARAM, char **argv) , &opt_t ); } else { - /* -t NUM sets also -a */ - opt_complementary = "ta"; - opt = getopt32long(argv, "ft:a", + opt = getopt32long(argv, "^" + "ft:a" + "\0" + "ta" /* -t NUM sets -a */, "first-only\0" No_argument "i" "tabs\0" Required_argument "t" "all\0" No_argument "a" diff --git a/coreutils/id.c b/coreutils/id.c index 6043bca61..5a7fb9aaf 100644 --- a/coreutils/id.c +++ b/coreutils/id.c @@ -170,9 +170,12 @@ int id_main(int argc UNUSED_PARAM, char **argv) } else { /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/ /* Don't allow more than one username */ - opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" - IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G"); - opt = getopt32(argv, "rnugG" IF_SELINUX("Z")); + opt = getopt32(argv, "^" + "rnugG" IF_SELINUX("Z") + "\0" + "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" + IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G") + ); } username = argv[optind]; diff --git a/coreutils/install.c b/coreutils/install.c index c01750f81..2e4dc257f 100644 --- a/coreutils/install.c +++ b/coreutils/install.c @@ -140,13 +140,16 @@ int install_main(int argc, char **argv) #endif }; - opt_complementary = "t--d:d--t:s--d:d--s" IF_FEATURE_INSTALL_LONG_OPTIONS(IF_SELINUX(":Z--\xff:\xff--Z")); /* -c exists for backwards compatibility, it's needed */ /* -b is ignored ("make a backup of each existing destination file") */ - opts = GETOPT32(argv, "cvb" "Ddpsg:m:o:t:" IF_SELINUX("Z:"), - LONGOPTS - &gid_str, &mode_str, &uid_str, &last - IF_SELINUX(, &scontext) + opts = GETOPT32(argv, "^" + "cvb" "Ddpsg:m:o:t:" IF_SELINUX("Z:") + "\0" + "t--d:d--t:s--d:d--s" + IF_FEATURE_INSTALL_LONG_OPTIONS(IF_SELINUX(":Z--\xff:\xff--Z")), + LONGOPTS + &gid_str, &mode_str, &uid_str, &last + IF_SELINUX(, &scontext) ); argc -= optind; argv += optind; diff --git a/coreutils/link.c b/coreutils/link.c index 6e20dafe3..81808b778 100644 --- a/coreutils/link.c +++ b/coreutils/link.c @@ -27,8 +27,7 @@ int link_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int link_main(int argc UNUSED_PARAM, char **argv) { - opt_complementary = "=2"; /* exactly 2 params */ - getopt32(argv, ""); + getopt32(argv, "^" "" "\0" "=2"); argv += optind; if (link(argv[0], argv[1]) != 0) { /* shared message */ diff --git a/coreutils/ln.c b/coreutils/ln.c index fed96af42..2dda5dae9 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c @@ -62,8 +62,7 @@ int ln_main(int argc, char **argv) struct stat statbuf; int (*link_func)(const char *, const char *); - opt_complementary = "-1"; /* min one arg */ - opts = getopt32(argv, "sfnbS:vT", &suffix); + opts = getopt32(argv, "^" "sfnbS:vT" "\0" "-1", &suffix); last = argv[argc - 1]; argv += optind; diff --git a/coreutils/ls.c b/coreutils/ls.c index 0834cdc63..af5e6cb51 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -206,18 +206,18 @@ SPLIT_SUBDIR = 2, /* -SXvhTw GNU options, busybox optionally supports */ /* -T WIDTH Ignored (we don't use tabs on output) */ /* -Z SELinux mandated option, busybox optionally supports */ -static const char ls_options[] ALIGN1 = - "Cadi1lgnsxAk" /* 12 opts, total 12 */ - IF_FEATURE_LS_FILETYPES("Fp") /* 2, 14 */ - IF_FEATURE_LS_RECURSIVE("R") /* 1, 15 */ - IF_SELINUX("Z") /* 1, 16 */ - "Q" /* 1, 17 */ - IF_FEATURE_LS_TIMESTAMPS("ctu") /* 3, 20 */ - IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 24 */ - IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 26 */ - IF_FEATURE_HUMAN_READABLE("h") /* 1, 27 */ +#define ls_options \ + "Cadi1lgnsxAk" /* 12 opts, total 12 */ \ + IF_FEATURE_LS_FILETYPES("Fp") /* 2, 14 */ \ + IF_FEATURE_LS_RECURSIVE("R") /* 1, 15 */ \ + IF_SELINUX("Z") /* 1, 16 */ \ + "Q" /* 1, 17 */ \ + IF_FEATURE_LS_TIMESTAMPS("ctu") /* 3, 20 */ \ + IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 24 */ \ + IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 26 */ \ + IF_FEATURE_HUMAN_READABLE("h") /* 1, 27 */ \ IF_FEATURE_LS_WIDTH("T:w:") /* 2, 29 */ -; + enum { OPT_C = (1 << 0), OPT_a = (1 << 1), @@ -1093,24 +1093,26 @@ int ls_main(int argc UNUSED_PARAM, char **argv) #endif /* process options */ - opt_complementary = - /* -n and -g imply -l */ - "nl:gl" - /* --full-time implies -l */ - IF_FEATURE_LS_TIMESTAMPS(IF_LONG_OPTS(":\xff""l")) - /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html: - * in some pairs of opts, only last one takes effect: - */ - IF_FEATURE_LS_TIMESTAMPS(IF_FEATURE_LS_SORTFILES(":t-S:S-t")) /* time/size */ - // ":m-l:l-m" - we don't have -m - IF_FEATURE_LS_FOLLOWLINKS(":H-L:L-H") - ":C-xl:x-Cl:l-xC" /* bycols/bylines/long */ - ":C-1:1-C" /* bycols/oneline */ - ":x-1:1-x" /* bylines/oneline (not in SuS, but in GNU coreutils 8.4) */ - IF_FEATURE_LS_TIMESTAMPS(":c-u:u-c") /* mtime/atime */ - /* -w NUM: */ - IF_FEATURE_LS_WIDTH(":w+"); - opt = getopt32long(argv, ls_options, ls_longopts + opt = getopt32long(argv, "^" + ls_options + "\0" + /* -n and -g imply -l */ + "nl:gl" + /* --full-time implies -l */ + IF_FEATURE_LS_TIMESTAMPS(IF_LONG_OPTS(":\xff""l")) + /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html: + * in some pairs of opts, only last one takes effect: + */ + IF_FEATURE_LS_TIMESTAMPS(IF_FEATURE_LS_SORTFILES(":t-S:S-t")) /* time/size */ + // ":m-l:l-m" - we don't have -m + IF_FEATURE_LS_FOLLOWLINKS(":H-L:L-H") + ":C-xl:x-Cl:l-xC" /* bycols/bylines/long */ + ":C-1:1-C" /* bycols/oneline */ + ":x-1:1-x" /* bylines/oneline (not in SuS, but in GNU coreutils 8.4) */ + IF_FEATURE_LS_TIMESTAMPS(":c-u:u-c") /* mtime/atime */ + /* -w NUM: */ + IF_FEATURE_LS_WIDTH(":w+") + , ls_longopts IF_FEATURE_LS_WIDTH(, /*-T*/ NULL, /*-w*/ &G_terminal_width) IF_FEATURE_LS_COLOR(, &color_opt) ); diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c index bcccdd64f..89d6cec0b 100644 --- a/coreutils/md5_sha1_sum.c +++ b/coreutils/md5_sha1_sum.c @@ -258,15 +258,14 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) #endif if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) { - /* -s and -w require -c */ - opt_complementary = "s?c:w?c"; /* -b "binary", -t "text" are ignored (shaNNNsum compat) */ + /* -s and -w require -c */ #if ENABLE_SHA3SUM if (applet_name[3] == HASH_SHA3) - flags = getopt32(argv, "scwbta:+", &sha3_width); + flags = getopt32(argv, "^" "scwbta:+" "\0" "s?c:w?c", &sha3_width); else #endif - flags = getopt32(argv, "scwbt"); + flags = getopt32(argv, "^" "scwbt" "\0" "s?c:w?c"); } else { #if ENABLE_SHA3SUM if (applet_name[3] == HASH_SHA3) diff --git a/coreutils/mktemp.c b/coreutils/mktemp.c index 944eb0e06..d4ff883fa 100644 --- a/coreutils/mktemp.c +++ b/coreutils/mktemp.c @@ -80,8 +80,7 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv) if (!path || path[0] == '\0') path = "/tmp"; - opt_complementary = "?1"; /* 1 argument max */ - opts = getopt32(argv, "dqtp:u", &path); + opts = getopt32(argv, "^" "dqtp:u" "\0" "?1"/*1 arg max*/, &path); chp = argv[optind]; if (!chp) { diff --git a/coreutils/mv.c b/coreutils/mv.c index 7f6e9fef5..10cbc506f 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c @@ -55,8 +55,10 @@ int mv_main(int argc, char **argv) * If more than one of -f, -i, -n is specified , only the final one * takes effect (it unsets previous options). */ - opt_complementary = "-2:f-in:i-fn:n-fi"; - flags = getopt32long(argv, "finv", + flags = getopt32long(argv, "^" + "finv" + "\0" + "-2:f-in:i-fn:n-fi", "interactive\0" No_argument "i" "force\0" No_argument "f" "no-clobber\0" No_argument "n" diff --git a/coreutils/readlink.c b/coreutils/readlink.c index 7f8d6b239..b8e327d11 100644 --- a/coreutils/readlink.c +++ b/coreutils/readlink.c @@ -71,8 +71,7 @@ int readlink_main(int argc UNUSED_PARAM, char **argv) IF_FEATURE_READLINK_FOLLOW( unsigned opt; /* We need exactly one non-option argument. */ - opt_complementary = "=1"; - opt = getopt32(argv, "fnvsq"); + opt = getopt32(argv, "^" "fnvsq" "\0" "=1"); fname = argv[optind]; ) IF_NOT_FEATURE_READLINK_FOLLOW( diff --git a/coreutils/rm.c b/coreutils/rm.c index 5e4acab8c..b68a82dc4 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -46,8 +46,7 @@ int rm_main(int argc UNUSED_PARAM, char **argv) int flags = 0; unsigned opt; - opt_complementary = "f-i:i-f"; - opt = getopt32(argv, "fiRrv"); + opt = getopt32(argv, "^" "fiRrv" "\0" "f-i:i-f"); argv += optind; if (opt & 1) flags |= FILEUTILS_FORCE; diff --git a/coreutils/shuf.c b/coreutils/shuf.c index 403041534..d0caaa2ce 100644 --- a/coreutils/shuf.c +++ b/coreutils/shuf.c @@ -70,8 +70,11 @@ int shuf_main(int argc, char **argv) unsigned numlines; char eol; - opt_complementary = "e--i:i--e"; /* mutually exclusive */ - opts = getopt32(argv, OPT_STR, &opt_i_str, &opt_n_str, &opt_o_str); + opts = getopt32(argv, "^" + OPT_STR + "\0" "e--i:i--e"/* mutually exclusive */, + &opt_i_str, &opt_n_str, &opt_o_str + ); argc -= optind; argv += optind; diff --git a/coreutils/sort.c b/coreutils/sort.c index 9860dca64..ceea24491 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c @@ -94,7 +94,7 @@ */ /* These are sort types */ -static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:*t:"; +#define OPT_STR "ngMucszbrdfimS:T:o:k:*t:" enum { FLAG_n = 1, /* Numeric sort */ FLAG_g = 2, /* Sort using strtod() */ @@ -378,9 +378,11 @@ int sort_main(int argc UNUSED_PARAM, char **argv) xfunc_error_retval = 2; /* Parse command line options */ - /* -o and -t can be given at most once */ - opt_complementary = "o--o:t--t"; /* -t, -o: at most one of each */ - opts = getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t); + opts = getopt32(argv, "^" + OPT_STR + "\0" "o--o:t--t"/*-t, -o: at most one of each*/, + &str_ignored, &str_ignored, &str_o, &lst_k, &str_t + ); /* global b strips leading and trailing spaces */ if (opts & FLAG_b) option_mask32 |= FLAG_bb; diff --git a/coreutils/split.c b/coreutils/split.c index d0c63573a..4e1db190c 100644 --- a/coreutils/split.c +++ b/coreutils/split.c @@ -100,8 +100,11 @@ int split_main(int argc UNUSED_PARAM, char **argv) setup_common_bufsiz(); - opt_complementary = "?2"; /* max 2 args; -a N */ - opt = getopt32(argv, "l:b:a:+", &count_p, &count_p, &suffix_len); + opt = getopt32(argv, "^" + "l:b:a:+" /* -a N */ + "\0" "?2"/*max 2 args*/, + &count_p, &count_p, &suffix_len + ); if (opt & SPLIT_OPT_l) cnt = XATOOFF(count_p); diff --git a/coreutils/stat.c b/coreutils/stat.c index 4e926a908..dafbd4e9f 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -762,11 +762,13 @@ int stat_main(int argc UNUSED_PARAM, char **argv) unsigned opts; statfunc_ptr statfunc = do_stat; - opt_complementary = "-1"; /* min one arg */ - opts = getopt32(argv, "tL" + opts = getopt32(argv, "^" + "tL" IF_FEATURE_STAT_FILESYSTEM("f") IF_SELINUX("Z") - IF_FEATURE_STAT_FORMAT("c:", &format) + IF_FEATURE_STAT_FORMAT("c:") + "\0" "-1" /* min one arg */ + IF_FEATURE_STAT_FORMAT(,&format) ); #if ENABLE_FEATURE_STAT_FILESYSTEM if (opts & OPT_FILESYS) /* -f */ diff --git a/coreutils/sync.c b/coreutils/sync.c index 66445281a..9be47ab64 100644 --- a/coreutils/sync.c +++ b/coreutils/sync.c @@ -59,8 +59,7 @@ int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) OPT_SYNCFS = (1 << 1), }; - opt_complementary = "d--f:f--d"; - opts = getopt32(argv, "df"); + opts = getopt32(argv, "^" "df" "\0" "d--f:f--d"); argv += optind; /* Handle the no-argument case. */ diff --git a/coreutils/tail.c b/coreutils/tail.c index fd310f422..7335ba11e 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -140,9 +140,11 @@ int tail_main(int argc, char **argv) #endif /* -s NUM, -F imlies -f */ - IF_FEATURE_FANCY_TAIL(opt_complementary = "Ff";) - opt = getopt32(argv, "fc:n:" IF_FEATURE_FANCY_TAIL("qs:+vF"), - &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period)); + opt = getopt32(argv, IF_FEATURE_FANCY_TAIL("^") + "fc:n:"IF_FEATURE_FANCY_TAIL("qs:+vF") + IF_FEATURE_FANCY_TAIL("\0" "Ff"), + &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period) + ); #define FOLLOW (opt & 0x1) #define COUNT_BYTES (opt & 0x2) //if (opt & 0x1) // -f diff --git a/coreutils/tr.c b/coreutils/tr.c index 64e4efc91..c5872434a 100644 --- a/coreutils/tr.c +++ b/coreutils/tr.c @@ -298,8 +298,8 @@ int tr_main(int argc UNUSED_PARAM, char **argv) * In POSIX locale, these are the same. */ - opt_complementary = "-1"; - opts = getopt32(argv, "+Ccds"); /* '+': stop at first non-option */ + /* '+': stop at first non-option */ + opts = getopt32(argv, "^+" "Ccds" "\0" "-1"); argv += optind; str1_length = expand(*argv++, &str1); diff --git a/coreutils/truncate.c b/coreutils/truncate.c index f67abaf40..f693570aa 100644 --- a/coreutils/truncate.c +++ b/coreutils/truncate.c @@ -50,8 +50,7 @@ int truncate_main(int argc UNUSED_PARAM, char **argv) OPT_SIZE = (1 << 1), }; - opt_complementary = "s:-1"; - opts = getopt32(argv, "cs:", &size_str); + opts = getopt32(argv, "^" "cs:" "\0" "s:-1", &size_str); if (!(opts & OPT_NOCREATE)) flags |= O_CREAT; diff --git a/coreutils/unlink.c b/coreutils/unlink.c index e32a9743c..56309b1c7 100644 --- a/coreutils/unlink.c +++ b/coreutils/unlink.c @@ -25,8 +25,7 @@ int unlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int unlink_main(int argc UNUSED_PARAM, char **argv) { - opt_complementary = "=1"; /* must have exactly 1 param */ - getopt32(argv, ""); + getopt32(argv, "^" "" "\0" "=1"); argv += optind; xunlink(argv[0]); return 0; diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 4e72e86ee..5ef05ee4d 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c @@ -120,8 +120,7 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) char *outname = NULL; char *line; - opt_complementary = "?1"; /* 1 argument max */ - getopt32(argv, "o:", &outname); + getopt32(argv, "^" "o:" "\0" "?1"/* 1 arg max*/, &outname); argv += optind; if (!argv[0]) @@ -196,8 +195,7 @@ int base64_main(int argc UNUSED_PARAM, char **argv) FILE *src_stream; unsigned opts; - opt_complementary = "?1"; /* 1 argument max */ - opts = getopt32(argv, "d"); + opts = getopt32(argv, "^" "d" "\0" "?1"/* 1 arg max*/); argv += optind; if (!argv[0]) diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c index 7164f838a..d6e077430 100644 --- a/coreutils/uuencode.c +++ b/coreutils/uuencode.c @@ -49,8 +49,7 @@ int uuencode_main(int argc UNUSED_PARAM, char **argv) tbl = bb_uuenc_tbl_std; mode = 0666 & ~umask(0666); - opt_complementary = "-1:?2"; /* must have 1 or 2 args */ - if (getopt32(argv, "m")) { + if (getopt32(argv, "^" "m" "\0" "-1:?2"/*must have 1 or 2 args*/)) { tbl = bb_uuenc_tbl_base64; } argv += optind; diff --git a/coreutils/who.c b/coreutils/who.c index 6be3d692e..cfe0c921e 100644 --- a/coreutils/who.c +++ b/coreutils/who.c @@ -117,8 +117,7 @@ int who_main(int argc UNUSED_PARAM, char **argv) unsigned opt; const char *fmt = "%s"; - opt_complementary = "=0"; - opt = getopt32(argv, do_who ? "aH" : ""); + opt = getopt32(argv, do_who ? "^" "aH" "\0" "=0": "^" "" "\0" "=0"); if ((opt & 2) || do_w) /* -H or we are w */ puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST"); diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c index b770383c4..e4d61df35 100644 --- a/debianutils/run_parts.c +++ b/debianutils/run_parts.c @@ -181,8 +181,9 @@ int run_parts_main(int argc UNUSED_PARAM, char **argv) INIT_G(); /* We require exactly one argument: the directory name */ - opt_complementary = "=1"; - GETOPT32(argv, "a:*u:" LONGOPTS, &arg_list, &umask_p); + GETOPT32(argv, "^" "a:*u:" "\0" "=1" LONGOPTS, + &arg_list, &umask_p + ); umask(xstrtou_range(umask_p, 8, 0, 07777)); diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index 45c277a53..12cf6c3a5 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c @@ -451,15 +451,17 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv) INIT_G(); - /* -K or -S is required; they are mutually exclusive */ - /* -p is required if -m is given */ - /* -xpun (at least one) is required if -K is given */ - /* -xa (at least one) is required if -S is given */ - /* -q turns off -v */ - opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa" - IF_FEATURE_START_STOP_DAEMON_FANCY("q-v"); - opt = GETOPT32(argv, "KSbqtma:n:s:u:c:x:p:" - IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"), + opt = GETOPT32(argv, "^" + "KSbqtma:n:s:u:c:x:p:" + IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:") + /* -K or -S is required; they are mutually exclusive */ + /* -p is required if -m is given */ + /* -xpun (at least one) is required if -K is given */ + /* -xa (at least one) is required if -S is given */ + /* -q turns off -v */ + "\0" + "K:S:K--S:S--K:m?p:K?xpun:S?xa" + IF_FEATURE_START_STOP_DAEMON_FANCY("q-v"), LONGOPTS &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) diff --git a/debianutils/which.c b/debianutils/which.c index b31d61871..3bd54ac42 100644 --- a/debianutils/which.c +++ b/debianutils/which.c @@ -37,8 +37,7 @@ int which_main(int argc UNUSED_PARAM, char **argv) if (!env_path) env_path = bb_default_root_path; - opt_complementary = "-1"; /* at least one argument */ - getopt32(argv, "a"); + getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/); argv += optind; do { diff --git a/e2fsprogs/tune2fs.c b/e2fsprogs/tune2fs.c index 9f14b26ec..a1caf011c 100644 --- a/e2fsprogs/tune2fs.c +++ b/e2fsprogs/tune2fs.c @@ -71,8 +71,7 @@ int tune2fs_main(int argc UNUSED_PARAM, char **argv) struct ext2_super_block *sb; int fd; - opt_complementary = "=1"; - opts = getopt32(argv, "L:c:i:C:", &label, &str_c, &str_i, &str_C); + opts = getopt32(argv, "^" "L:c:i:C:" "\0" "=1", &label, &str_c, &str_i, &str_C); if (!opts) bb_show_usage(); argv += optind; // argv[0] -- device diff --git a/editors/cmp.c b/editors/cmp.c index f53d9603c..ec86c0ce2 100644 --- a/editors/cmp.c +++ b/editors/cmp.c @@ -36,7 +36,7 @@ static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"u, line %u // This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"u %o %o\n" static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"u %3o %3o\n"; -static const char opt_chars[] ALIGN1 = "sl"; +#define OPT_STR "sl" #define CMP_OPT_s (1<<0) #define CMP_OPT_l (1<<1) @@ -52,11 +52,13 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) unsigned opt; int retval = 0; - opt_complementary = "-1" + opt = getopt32(argv, "^" + OPT_STR + "\0" "-1" IF_DESKTOP(":?4") IF_NOT_DESKTOP(":?2") - ":l--s:s--l"; - opt = getopt32(argv, opt_chars); + ":l--s:s--l" + ); argv += optind; filename1 = *argv; diff --git a/editors/diff.c b/editors/diff.c index d90ac8f94..2f254575c 100644 --- a/editors/diff.c +++ b/editors/diff.c @@ -984,8 +984,8 @@ int diff_main(int argc UNUSED_PARAM, char **argv) INIT_G(); /* exactly 2 params; collect multiple -L