getopt32: remove applet_long_options
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 8 Aug 2017 14:38:18 +0000 (16:38 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 8 Aug 2017 15:09:40 +0000 (17:09 +0200)
FEATURE_GETOPT_LONG made dependent on LONG_OPTS.

The folloving options are removed, now LONG_OPTS enables long options
for affected applets:
FEATURE_ENV_LONG_OPTIONS FEATURE_EXPAND_LONG_OPTIONS
FEATURE_UNEXPAND_LONG_OPTIONS FEATURE_MKDIR_LONG_OPTIONS
FEATURE_MV_LONG_OPTIONS FEATURE_RMDIR_LONG_OPTIONS
FEATURE_ADDGROUP_LONG_OPTIONS FEATURE_ADDUSER_LONG_OPTIONS
FEATURE_HWCLOCK_LONG_OPTIONS FEATURE_NSENTER_LONG_OPTS
FEATURE_CHCON_LONG_OPTIONS FEATURE_RUNCON_LONG_OPTIONS

They either had a small number of long options, or their long options are
essential.

Example: upstream addgroup and adduser have ONLY longopts,
we should probably go further and get rid
of non-standard short options.

To this end, make addgroup and adduser "select LONG_OPTS".
We had this breakage caused by us even in our own package!
#if ENABLE_LONG_OPTS || !ENABLE_ADDGROUP
                /* We try to use --gid, not -g, because "standard" addgroup
                 * has no short option -g, it has only long --gid.
                 */
                argv[1] = (char*)"--gid";
#else
                /* Breaks if system in fact does NOT use busybox addgroup */
                argv[1] = (char*)"-g";
#endif

xargs: its lone longopt no longer depends on DESKTOP, only on LONG_OPTS.

hwclock TODO: get rid of incompatible -t, -l aliases to --systz, --localtime

Shorten help texts by omitting long option when short opt alternative exists.

Reduction of size comes from the fact that store of an immediate
(an address of longopts) to a fixed address (global variable)
is a longer insn than pushing that immediate or passing it in a register.
This effect is CPU-agnostic.

function                                             old     new   delta
getopt32                                            1350      22   -1328
vgetopt32                                              -    1318   +1318
getopt32long                                           -      24     +24
tftpd_main                                           562     567      +5
scan_recursive                                       376     380      +4
collect_cpu                                          545     546      +1
date_main                                           1096    1095      -1
hostname_main                                        262     259      -3
uname_main                                           259     255      -4
setpriv_main                                         362     358      -4
rmdir_main                                           191     187      -4
mv_main                                              562     558      -4
ipcalc_main                                          548     544      -4
ifenslave_main                                       641     637      -4
gzip_main                                            192     188      -4
gunzip_main                                           77      73      -4
fsfreeze_main                                         81      77      -4
flock_main                                           318     314      -4
deluser_main                                         337     333      -4
cp_main                                              374     370      -4
chown_main                                           175     171      -4
applet_long_options                                    4       -      -4
xargs_main                                           894     889      -5
wget_main                                           2540    2535      -5
udhcpc_main                                         2767    2762      -5
touch_main                                           436     431      -5
tar_main                                            1014    1009      -5
start_stop_daemon_main                              1033    1028      -5
sed_main                                             682     677      -5
script_main                                         1082    1077      -5
run_parts_main                                       330     325      -5
rtcwake_main                                         459     454      -5
od_main                                             2169    2164      -5
nl_main                                              201     196      -5
modprobe_main                                        773     768      -5
mkdir_main                                           160     155      -5
ls_main                                              568     563      -5
install_main                                         773     768      -5
hwclock_main                                         411     406      -5
getopt_main                                          622     617      -5
fstrim_main                                          256     251      -5
env_main                                             198     193      -5
dumpleases_main                                      635     630      -5
dpkg_main                                           3991    3986      -5
diff_main                                           1355    1350      -5
cryptpw_main                                         233     228      -5
cpio_main                                            593     588      -5
conspy_main                                         1135    1130      -5
chpasswd_main                                        313     308      -5
adduser_main                                         887     882      -5
addgroup_main                                        416     411      -5
ftpgetput_main                                       351     345      -6
get_terminal_width_height                            242     234      -8
expand_main                                          690     680     -10
static.expand_longopts                                18       -     -18
static.unexpand_longopts                              27       -     -27
mkdir_longopts                                        28       -     -28
env_longopts                                          30       -     -30
static.ifenslave_longopts                             34       -     -34
mv_longopts                                           46       -     -46
static.rmdir_longopts                                 48       -     -48
packed_usage                                       31739   31687     -52
------------------------------------------------------------------------------
(add/remove: 2/8 grow/shrink: 3/49 up/down: 1352/-1840)      Total: -488 bytes
   text    data     bss     dec     hex filename
 915681     485    6880  923046   e15a6 busybox_old
 915428     485    6876  922789   e14a5 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
57 files changed:
archival/bbunzip.c
archival/cpio.c
archival/dpkg.c
archival/gzip.c
archival/tar.c
coreutils/chown.c
coreutils/cp.c
coreutils/date.c
coreutils/env.c
coreutils/expand.c
coreutils/install.c
coreutils/ls.c
coreutils/mkdir.c
coreutils/mv.c
coreutils/nl.c
coreutils/nproc.c
coreutils/od_bloaty.c
coreutils/rmdir.c
coreutils/touch.c
coreutils/uname.c
debianutils/run_parts.c
debianutils/start_stop_daemon.c
editors/diff.c
editors/patch_bbox.c
editors/sed.c
findutils/xargs.c
include/libbb.h
libbb/getopt32.c
libbb/vfork_daemon_rexec.c
loginutils/addgroup.c
loginutils/adduser.c
loginutils/chpasswd.c
loginutils/cryptpw.c
loginutils/deluser.c
miscutils/conspy.c
miscutils/nandwrite.c
modutils/modprobe.c
networking/ftpgetput.c
networking/hostname.c
networking/ifenslave.c
networking/ipcalc.c
networking/udhcp/d6_dhcpc.c
networking/udhcp/dhcpc.c
networking/udhcp/dumpleases.c
networking/wget.c
selinux/chcon.c
selinux/runcon.c
util-linux/flock.c
util-linux/fsfreeze.c
util-linux/fstrim.c
util-linux/getopt.c
util-linux/hwclock.c
util-linux/nsenter.c
util-linux/rtcwake.c
util-linux/script.c
util-linux/setpriv.c
util-linux/unshare.c

index 9b9fdc87b594baa19a0374d62df9e7bc6c50fee3..20ab893dacda07172897bd2da0aecfac86d37724 100644 (file)
@@ -389,9 +389,10 @@ int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int gunzip_main(int argc UNUSED_PARAM, char **argv)
 {
 #if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
-       applet_long_options = gunzip_longopts;
-#endif
+       getopt32long(argv, "cfkvqdtn", gunzip_longopts);
+#else
        getopt32(argv, "cfkvqdtn");
+#endif
        argv += optind;
 
        /* If called as zcat...
index 38bab8257192696c428fa51038b650d36110b731..f2165be3a6303f2da8273b3313061cd3dcc6e85e 100644 (file)
@@ -360,9 +360,8 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
        char *cpio_owner;
        IF_FEATURE_CPIO_O(const char *cpio_fmt = "";)
        unsigned opt;
-
 #if ENABLE_LONG_OPTS
-       applet_long_options =
+       const char *long_opts =
                "extract\0"      No_argument       "i"
                "list\0"         No_argument       "t"
 #if ENABLE_FEATURE_CPIO_O
@@ -390,9 +389,9 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
        /* -L makes sense only with -o or -p */
 
 #if !ENABLE_FEATURE_CPIO_O
-       opt = getopt32(argv, OPTION_STR, &cpio_filename, &cpio_owner);
+       opt = getopt32long(argv, OPTION_STR, long_opts, &cpio_filename, &cpio_owner);
 #else
-       opt = getopt32(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"),
+       opt = getopt32long(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), long_opts,
                       &cpio_filename, &cpio_owner, &cpio_fmt);
 #endif
        argv += optind;
index 90ad8766cb4e6c6f7dff2b55a96964ed015a8d0d..852e0cac28d895703506afe220d8bd1d870c69fe 100644 (file)
@@ -1766,8 +1766,7 @@ int dpkg_main(int argc UNUSED_PARAM, char **argv)
 
        INIT_G();
 
-       IF_LONG_OPTS(applet_long_options = dpkg_longopts);
-       opt = getopt32(argv, "CilPruF:", &str_f);
+       opt = getopt32long(argv, "CilPruF:", dpkg_longopts, &str_f);
        argv += optind;
        //if (opt & OPT_configure) ... // -C
        if (opt & OPT_force) { // -F (--force in official dpkg)
index 4cf34ac284065612df28163f2b35d89d2d2c963d..9c53895e90856dacff3066029439986f15ac255a 100644 (file)
@@ -2216,11 +2216,12 @@ int gzip_main(int argc UNUSED_PARAM, char **argv)
        SET_PTR_TO_GLOBALS((char *)xzalloc(sizeof(struct globals)+sizeof(struct globals2))
                        + sizeof(struct globals));
 
-#if ENABLE_FEATURE_GZIP_LONG_OPTIONS
-       applet_long_options = gzip_longopts;
-#endif
        /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
+#if ENABLE_FEATURE_GZIP_LONG_OPTIONS
+       opt = getopt32long(argv, "cfkv" IF_FEATURE_GZIP_DECOMPRESS("dt") "qn123456789", gzip_longopts);
+#else
        opt = getopt32(argv, "cfkv" IF_FEATURE_GZIP_DECOMPRESS("dt") "qn123456789");
+#endif
 #if ENABLE_FEATURE_GZIP_DECOMPRESS /* gunzip_main may not be visible... */
        if (opt & 0x30) // -d and/or -t
                return gunzip_main(argc, argv);
index f62b330056277c976fa7e38892d30b4db7de1b2b..44ab246c04e053f4f4c7041f6c455445a24564da 100644 (file)
@@ -940,6 +940,11 @@ static const char tar_longopts[] ALIGN1 =
        "exclude\0"             Required_argument "\xff"
 # endif
        ;
+# define GETOPT32 getopt32long
+# define LONGOPTS ,tar_longopts
+#else
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
 int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -980,9 +985,6 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
                ":\xf9+" // --strip-components=NUM
 #endif
        ;
-#if ENABLE_FEATURE_TAR_LONG_OPTIONS
-       applet_long_options = tar_longopts;
-#endif
 #if ENABLE_DESKTOP
        /* Lie to buildroot when it starts asking stupid questions. */
        if (argv[1] && strcmp(argv[1], "--version") == 0) {
@@ -1019,7 +1021,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"     )
@@ -1030,6 +1032,7 @@ 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
+               LONGOPTS
                , &base_dir // -C dir
                , &tar_filename // -f filename
                IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T
index 1bfc725cc2c4a4dff0e291cc13f1825fbb0fea4d..0c77529ecaaff1202ff1a4326cb1d152ab69dcaf 100644 (file)
@@ -127,11 +127,12 @@ int chown_main(int argc UNUSED_PARAM, char **argv)
        int opt, flags;
        struct param_t param;
 
-#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
-       applet_long_options = chown_longopts;
-#endif
        opt_complementary = "-2";
+#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
+       opt = getopt32long(argv, OPT_STR, chown_longopts);
+#else
        opt = getopt32(argv, OPT_STR);
+#endif
        argv += optind;
 
        /* This matches coreutils behavior (almost - see below) */
index 092e39583b709317fcde2aa1ceb19d03079ded84..fe408950af05645d602b054eac161949858aef99 100644 (file)
@@ -81,7 +81,7 @@ int cp_main(int argc, char **argv)
        // -a = -pdR
        opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR";
 #if ENABLE_FEATURE_CP_LONG_OPTIONS
-       applet_long_options =
+       flags = getopt32long(argv, FILEUTILS_CP_OPTSTR,
                "archive\0"        No_argument "a"
                "force\0"          No_argument "f"
                "interactive\0"    No_argument "i"
@@ -94,9 +94,10 @@ int cp_main(int argc, char **argv)
                "update\0"         No_argument "u"
                "remove-destination\0" No_argument "\xff"
                "parents\0"        No_argument "\xfe"
-               ;
-#endif
+       );
+#else
        flags = getopt32(argv, FILEUTILS_CP_OPTSTR);
+#endif
        /* Options of cp from GNU coreutils 6.10:
         * -a, --archive
         * -f, --force
index 5a4ad5fe540e70f4b0f2ef1b69d57c0fbabcdb20..33f210434b17ced232c82f67b7217a2497315bfa 100644 (file)
@@ -194,9 +194,8 @@ int date_main(int argc UNUSED_PARAM, char **argv)
 
        opt_complementary = "d--s:s--d"
                IF_FEATURE_DATE_ISOFMT(":R--I:I--R");
-       IF_LONG_OPTS(applet_long_options = date_longopts;)
-       opt = getopt32(argv, "Rs:ud:r:"
-                       IF_FEATURE_DATE_ISOFMT("I::D:"),
+       opt = getopt32long(argv, "Rs:ud:r:"
+                       IF_FEATURE_DATE_ISOFMT("I::D:"), date_longopts,
                        &date_str, &date_str, &filename
                        IF_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt));
        argv += optind;
index 3242446f59f666ebfc48e06c40f44eaff3762c17..20453e871e138bac633d56ac9648b5210fc15ff9 100644 (file)
 //config:      env is used to set an environment variable and run
 //config:      a command; without options it displays the current
 //config:      environment.
-//config:
-//config:config FEATURE_ENV_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on ENV && LONG_OPTS
 
 //applet:IF_ENV(APPLET_NOEXEC(env, env, BB_DIR_USR_BIN, BB_SUID_DROP, env))
 
 
 #include "libbb.h"
 
-#if ENABLE_FEATURE_ENV_LONG_OPTIONS
-static const char env_longopts[] ALIGN1 =
-       "ignore-environment\0" No_argument       "i"
-       "unset\0"              Required_argument "u"
-       ;
-#endif
-
 int env_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int env_main(int argc UNUSED_PARAM, char **argv)
 {
        unsigned opts;
        llist_t *unset_env = NULL;
 
-#if ENABLE_FEATURE_ENV_LONG_OPTIONS
-       applet_long_options = env_longopts;
-#endif
-       opts = getopt32(argv, "+iu:+", &unset_env);
+       opts = getopt32long(argv, "+iu:+",
+                       "ignore-environment\0" No_argument       "i"
+                       "unset\0"              Required_argument "u"
+                       , &unset_env
+       );
        argv += optind;
        if (argv[0] && LONE_DASH(argv[0])) {
                opts |= 1;
index 64f2a539dabd277d29c6e73fc21d91fb334be03c..fa3ff18f4b20febc1b94cb67cf902b4145187b33 100644 (file)
 //config:      help
 //config:      By default, convert all tabs to spaces.
 //config:
-//config:config FEATURE_EXPAND_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on EXPAND && LONG_OPTS
-//config:
 //config:config UNEXPAND
 //config:      bool "unexpand (6 kb)"
 //config:      default y
 //config:      help
 //config:      By default, convert only leading sequences of blanks to tabs.
-//config:
-//config:config FEATURE_UNEXPAND_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on UNEXPAND && LONG_OPTS
 
 //applet:IF_EXPAND(APPLET(expand, BB_DIR_USR_BIN, BB_SUID_DROP))
 //                   APPLET_ODDNAME:name      main    location        suid_type     help
 //usage:       "[-i] [-t N] [FILE]..."
 //usage:#define expand_full_usage "\n\n"
 //usage:       "Convert tabs to spaces, writing to stdout\n"
-//usage:       IF_FEATURE_EXPAND_LONG_OPTIONS(
-//usage:     "\n       -i,--initial    Don't convert tabs after non blanks"
-//usage:     "\n       -t,--tabs N     Tabstops every N chars"
-//usage:       )
-//usage:       IF_NOT_FEATURE_EXPAND_LONG_OPTIONS(
 //usage:     "\n       -i      Don't convert tabs after non blanks"
 //usage:     "\n       -t      Tabstops every N chars"
-//usage:       )
 
 //usage:#define unexpand_trivial_usage
 //usage:       "[-fa][-t N] [FILE]..."
 //usage:#define unexpand_full_usage "\n\n"
 //usage:       "Convert spaces to tabs, writing to stdout\n"
-//usage:       IF_FEATURE_UNEXPAND_LONG_OPTIONS(
-//usage:     "\n       -a,--all        Convert all blanks"
-//usage:     "\n       -f,--first-only Convert only leading blanks"
-//usage:     "\n       -t,--tabs N     Tabstops every N chars"
-//usage:       )
-//usage:       IF_NOT_FEATURE_UNEXPAND_LONG_OPTIONS(
 //usage:     "\n       -a      Convert all blanks"
 //usage:     "\n       -f      Convert only leading blanks"
 //usage:     "\n       -t N    Tabstops every N chars"
-//usage:       )
 
 #include "libbb.h"
 #include "unicode.h"
@@ -188,31 +165,23 @@ int expand_main(int argc UNUSED_PARAM, char **argv)
        unsigned opt;
        int exit_status = EXIT_SUCCESS;
 
-#if ENABLE_FEATURE_EXPAND_LONG_OPTIONS
-       static const char expand_longopts[] ALIGN1 =
-               /* name, has_arg, val */
-               "initial\0"          No_argument       "i"
-               "tabs\0"             Required_argument "t"
-       ;
-#endif
-#if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS
-       static const char unexpand_longopts[] ALIGN1 =
-               /* name, has_arg, val */
-               "first-only\0"       No_argument       "i"
-               "tabs\0"             Required_argument "t"
-               "all\0"              No_argument       "a"
-       ;
-#endif
        init_unicode();
 
        if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
-               IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
-               opt = getopt32(argv, "it:", &opt_t);
+               opt = getopt32long(argv, "it:",
+                               "initial\0"          No_argument       "i"
+                               "tabs\0"             Required_argument "t"
+                               , &opt_t
+               );
        } else {
-               IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
                /* -t NUM sets also -a */
                opt_complementary = "ta";
-               opt = getopt32(argv, "ft:a", &opt_t);
+               opt = getopt32long(argv, "ft:a",
+                               "first-only\0"       No_argument       "i"
+                               "tabs\0"             Required_argument "t"
+                               "all\0"              No_argument       "a"
+                               , &opt_t
+               );
                /* -f --first-only is the default */
                if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
        }
index a1342bb13e706d09689fe676d78cb87f547052e1..c01750f81c9d89cfe4abcb5dd87a950164bdcf76 100644 (file)
@@ -55,12 +55,17 @@ static const char install_longopts[] ALIGN1 =
        "target-directory\0"    Required_argument "t"
 /* autofs build insists of using -b --suffix=.orig */
 /* TODO? (short option for --suffix is -S) */
-#if ENABLE_SELINUX
+# if ENABLE_SELINUX
        "context\0"             Required_argument "Z"
        "preserve_context\0"    No_argument       "\xff"
        "preserve-context\0"    No_argument       "\xff"
-#endif
+# endif
        ;
+# define GETOPT32 getopt32long
+# define LONGOPTS install_longopts,
+#else
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
 
@@ -135,15 +140,14 @@ int install_main(int argc, char **argv)
 #endif
        };
 
-#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
-       applet_long_options = install_longopts;
-#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:"),
+       opts = GETOPT32(argv, "cvb" "Ddpsg:m:o:t:" IF_SELINUX("Z:"),
+                       LONGOPTS
                        &gid_str, &mode_str, &uid_str, &last
-                       IF_SELINUX(, &scontext));
+                       IF_SELINUX(, &scontext)
+       );
        argc -= optind;
        argv += optind;
 
index 0fe0345b317a02857fb07eb07d7989b9ba94c80a..0834cdc63f6276c868a30d779bae58461cab5317 100644 (file)
@@ -1093,7 +1093,6 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
 #endif
 
        /* process options */
-       IF_LONG_OPTS(applet_long_options = ls_longopts;)
        opt_complementary =
                /* -n and -g imply -l */
                "nl:gl"
@@ -1111,7 +1110,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
                IF_FEATURE_LS_TIMESTAMPS(":c-u:u-c") /* mtime/atime */
                /* -w NUM: */
                IF_FEATURE_LS_WIDTH(":w+");
-       opt = getopt32(argv, ls_options
+       opt = getopt32long(argv, ls_options, ls_longopts
                IF_FEATURE_LS_WIDTH(, /*-T*/ NULL, /*-w*/ &G_terminal_width)
                IF_FEATURE_LS_COLOR(, &color_opt)
        );
index 22851187c1b2ce26c0b252ec66a132d8a45d80ce..986353dc6ef352bc83ab8574dc1c9d4689d1b722 100644 (file)
 //config:      default y
 //config:      help
 //config:      mkdir is used to create directories with the specified names.
-//config:
-//config:config FEATURE_MKDIR_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on MKDIR && LONG_OPTS
 
 //applet:IF_MKDIR(APPLET_NOFORK(mkdir, mkdir, BB_DIR_BIN, BB_SUID_DROP, mkdir))
 
 
 /* This is a NOFORK applet. Be very careful! */
 
-#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
-static const char mkdir_longopts[] ALIGN1 =
-       "mode\0"    Required_argument "m"
-       "parents\0" No_argument       "p"
-#if ENABLE_SELINUX
-       "context\0" Required_argument "Z"
-#endif
-#if ENABLE_FEATURE_VERBOSE
-       "verbose\0" No_argument       "v"
-#endif
-       ;
-#endif
-
 int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int mkdir_main(int argc UNUSED_PARAM, char **argv)
 {
@@ -78,10 +60,17 @@ int mkdir_main(int argc UNUSED_PARAM, char **argv)
        security_context_t scontext;
 #endif
 
-#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
-       applet_long_options = mkdir_longopts;
-#endif
-       opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
+       opt = getopt32long(argv, "m:pv" IF_SELINUX("Z:"),
+                       "mode\0"    Required_argument "m"
+                       "parents\0" No_argument       "p"
+# if ENABLE_SELINUX
+                       "context\0" Required_argument "Z"
+# endif
+# if ENABLE_FEATURE_VERBOSE
+                       "verbose\0" No_argument       "v"
+# endif
+                       , &smode IF_SELINUX(,&scontext)
+       );
        if (opt & 1) {
                mode_t mmode = bb_parse_mode(smode, 0777);
                if (mmode == (mode_t)-1) {
index 147dd3bb23f722e0063075c5b913f3b17a396212..7f6e9fef56648c24e566985e9baea488a6538876 100644 (file)
 //config:      default y
 //config:      help
 //config:      mv is used to move or rename files or directories.
-//config:
-//config:config FEATURE_MV_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on MV && LONG_OPTS
 
 //applet:IF_MV(APPLET(mv, BB_DIR_BIN, BB_SUID_DROP))
 
 #include "libbb.h"
 #include "libcoreutils/coreutils.h"
 
-#if ENABLE_FEATURE_MV_LONG_OPTIONS
-static const char mv_longopts[] ALIGN1 =
-       "interactive\0" No_argument "i"
-       "force\0"       No_argument "f"
-       "no-clobber\0"  No_argument "n"
-       IF_FEATURE_VERBOSE(
-       "verbose\0"     No_argument "v"
-       )
-       ;
-#endif
-
-#define OPT_FORCE       (1 << 0)
-#define OPT_INTERACTIVE (1 << 1)
-#define OPT_NOCLOBBER   (1 << 2)
-#define OPT_VERBOSE     ((1 << 3) * ENABLE_FEATURE_VERBOSE)
-
-
 int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int mv_main(int argc, char **argv)
 {
@@ -69,15 +47,23 @@ int mv_main(int argc, char **argv)
        int status = 0;
        int copy_flag = 0;
 
-#if ENABLE_FEATURE_MV_LONG_OPTIONS
-       applet_long_options = mv_longopts;
-#endif
+#define OPT_FORCE       (1 << 0)
+#define OPT_INTERACTIVE (1 << 1)
+#define OPT_NOCLOBBER   (1 << 2)
+#define OPT_VERBOSE     ((1 << 3) * ENABLE_FEATURE_VERBOSE)
        /* Need at least two arguments.
         * 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 = getopt32(argv, "finv");
+       flags = getopt32long(argv, "finv",
+                       "interactive\0" No_argument "i"
+                       "force\0"       No_argument "f"
+                       "no-clobber\0"  No_argument "n"
+                       IF_FEATURE_VERBOSE(
+                       "verbose\0"     No_argument "v"
+                       )
+       );
        argc -= optind;
        argv += optind;
        last = argv[argc - 1];
index 93e78c490fa8c63490bd7550e7e545c76cd5da42..c2f8b10423be1867a58c0b4792840934063fc745 100644 (file)
@@ -57,14 +57,13 @@ int nl_main(int argc UNUSED_PARAM, char **argv)
                "starting-line-number\0"Required_argument "v"
                "number-width\0"        Required_argument "w"
        ;
-
-       applet_long_options = nl_longopts;
 #endif
        ns.width = 6;
        ns.start = 1;
        ns.inc = 1;
        ns.sep = "\t";
-       getopt32(argv, "pw:+s:v:+i:+b:", &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b);
+       getopt32long(argv, "pw:+s:v:+i:+b:", nl_longopts,
+                       &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b);
        ns.all = (opt_b[0] == 'a');
        ns.nonempty = (opt_b[0] == 't');
        ns.empty_str = xasprintf("%*s\n", ns.width + (int)strlen(ns.sep), "");
index 0ae55e70a827744fca1ab93d600431747fdac17a..336b176cad263ccc3e0a16f8c91f37f4e0852ef8 100644 (file)
@@ -28,7 +28,6 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
        unsigned long mask[1024];
        unsigned i, count = 0;
 
-       //applet_long_options = ...;
        //getopt32(argv, "");
 
        //if --all, count /sys/devices/system/cpu/cpuN dirs, else:
index fa0196ca4fba398106962eabf7ce62dbe39c213b..f19875c427784b7a0d0cb0430da9f6a8d46a462b 100644 (file)
@@ -61,8 +61,8 @@ enum {
        OPT_traditional = (1 << 18) * ENABLE_LONG_OPTS,
 };
 
-#define OD_GETOPT32() getopt32(argv, \
-       "A:N:abcdfhij:lot:*vxsS:w:+:", \
+#define OD_GETOPT32() getopt32long(argv, \
+       "A:N:abcdfhij:lot:*vxsS:w:+:", od_longopts, \
        /* -w with optional param */ \
        /* -S was -s and also had optional parameter */ \
        /* but in coreutils 6.3 it was renamed and now has */ \
@@ -1213,9 +1213,6 @@ int od_main(int argc UNUSED_PARAM, char **argv)
        address_pad_len_char = '7';
 
        /* Parse command line */
-#if ENABLE_LONG_OPTS
-       applet_long_options = od_longopts;
-#endif
        opt = OD_GETOPT32();
        argv += optind;
        if (opt & OPT_A) {
index c04ce78f846ac66bd19ccb05f6f505493385b377..955740494955666e7e4bf8c21861f5fbc41e70c1 100644 (file)
 //config:      default y
 //config:      help
 //config:      rmdir is used to remove empty directories.
-//config:
-//config:config FEATURE_RMDIR_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on RMDIR && LONG_OPTS
-//config:      help
-//config:      Support long options for the rmdir applet, including
-//config:      --ignore-fail-on-non-empty for compatibility with GNU rmdir.
 
 //applet:IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, BB_DIR_BIN, BB_SUID_DROP, rmdir))
 
 //usage:       "[OPTIONS] DIRECTORY..."
 //usage:#define rmdir_full_usage "\n\n"
 //usage:       "Remove DIRECTORY if it is empty\n"
-//usage:       IF_FEATURE_RMDIR_LONG_OPTIONS(
-//usage:     "\n       -p|--parents    Include parents"
-//usage:     "\n       --ignore-fail-on-non-empty"
-//usage:       )
-//usage:       IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
 //usage:     "\n       -p      Include parents"
+//usage:       IF_LONG_OPTS(
+//usage:     "\n       --ignore-fail-on-non-empty"
 //usage:       )
 //usage:
 //usage:#define rmdir_example_usage
@@ -49,7 +38,7 @@
 
 #define PARENTS          (1 << 0)
 #define VERBOSE          ((1 << 1) * ENABLE_FEATURE_VERBOSE)
-#define IGNORE_NON_EMPTY (1 << 2)
+#define IGNORE_NON_EMPTY ((1 << 2) * ENABLE_LONG_OPTS)
 
 int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int rmdir_main(int argc UNUSED_PARAM, char **argv)
@@ -58,8 +47,7 @@ int rmdir_main(int argc UNUSED_PARAM, char **argv)
        int flags;
        char *path;
 
-#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
-       static const char rmdir_longopts[] ALIGN1 =
+       flags = getopt32long(argv, "pv",
                "parents\0"                  No_argument "p"
                /* Debian etch: many packages fail to be purged or installed
                 * because they desperately want this option: */
@@ -67,10 +55,7 @@ int rmdir_main(int argc UNUSED_PARAM, char **argv)
                IF_FEATURE_VERBOSE(
                "verbose\0"                  No_argument "v"
                )
-               ;
-       applet_long_options = rmdir_longopts;
-#endif
-       flags = getopt32(argv, "pv");
+       );
        argv += optind;
 
        if (!*argv) {
@@ -86,7 +71,7 @@ int rmdir_main(int argc UNUSED_PARAM, char **argv)
                        }
 
                        if (rmdir(path) < 0) {
-#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
+#if ENABLE_LONG_OPTS
                                if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
                                        break;
 #endif
index 11b40d4276ccc7d0d5eec1af270d6fe30cea0c73..8577615781284b38e7403610a43ca4282be1a901 100644 (file)
@@ -103,6 +103,11 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
                "date\0"              Required_argument "d"
                IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h")
        ;
+#  define GETOPT32 getopt32long
+#  define LONGOPTS ,touch_longopts
+# else
+#  define GETOPT32 getopt32
+#  define LONGOPTS
 # endif
        char *reference_file = NULL;
        char *date_str = NULL;
@@ -112,17 +117,17 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
 # define reference_file NULL
 # define date_str       NULL
 # define timebuf        ((struct timeval*)NULL)
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
-#if ENABLE_FEATURE_TOUCH_SUSV3 && ENABLE_LONG_OPTS
-       applet_long_options = touch_longopts;
-#endif
        /* -d and -t both set time. In coreutils,
         * accepted data format differs a bit between -d and -t.
         * We accept the same formats for both */
-       opts = getopt32(argv, "c" IF_FEATURE_TOUCH_SUSV3("r:d:t:")
+       opts = GETOPT32(argv, "c" IF_FEATURE_TOUCH_SUSV3("r:d:t:")
                                IF_FEATURE_TOUCH_NODEREF("h")
                                /*ignored:*/ "fma"
+                               LONGOPTS
                                IF_FEATURE_TOUCH_SUSV3(, &reference_file)
                                IF_FEATURE_TOUCH_SUSV3(, &date_str)
                                IF_FEATURE_TOUCH_SUSV3(, &date_str)
index be9a3f90d4703dde0a44bb5b85566c699f4f2a38..bb2d1fe8dc8d8c079b35e2f183d99800176fc799 100644 (file)
@@ -147,8 +147,7 @@ int uname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
                        "operating-system\0"  No_argument       "o"
                ;
 # endif
-               IF_LONG_OPTS(applet_long_options = uname_longopts);
-               toprint = getopt32(argv, options);
+               toprint = getopt32long(argv, options, uname_longopts);
                if (argv[optind]) { /* coreutils-6.9 compat */
                        bb_show_usage();
                }
index c6a90a4865e6e66b520d852c539688824a635123..b770383c47cc42ed1b22087a878d7cdfeb7f3a69 100644 (file)
@@ -159,10 +159,15 @@ static const char runparts_longopts[] ALIGN1 =
        "reverse\0" No_argument       "\xf0"
        "test\0"    No_argument       "\xf1"
        "exit-on-error\0" No_argument "\xf2"
-#if ENABLE_FEATURE_RUN_PARTS_FANCY
+# if ENABLE_FEATURE_RUN_PARTS_FANCY
        "list\0"    No_argument       "\xf3"
-#endif
+# endif
        ;
+# define GETOPT32 getopt32long
+# define LONGOPTS ,runparts_longopts
+#else
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
 int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -175,12 +180,9 @@ int run_parts_main(int argc UNUSED_PARAM, char **argv)
 
        INIT_G();
 
-#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
-       applet_long_options = runparts_longopts;
-#endif
        /* We require exactly one argument: the directory name */
        opt_complementary = "=1";
-       getopt32(argv, "a:*u:", &arg_list, &umask_p);
+       GETOPT32(argv, "a:*u:" LONGOPTS, &arg_list, &umask_p);
 
        umask(xstrtou_range(umask_p, 8, 0, 07777));
 
index 9effdc80b3dc683f98e821340956e85d77fcb87d..45c277a5316545717302a0aab7ea624c5d1eacdd 100644 (file)
@@ -426,6 +426,11 @@ static const char start_stop_daemon_longopts[] ALIGN1 =
        "retry\0"        Required_argument "R"
 # endif
        ;
+# define GETOPT32 getopt32long
+# define LONGOPTS start_stop_daemon_longopts,
+#else
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
 int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -446,10 +451,6 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
 
        INIT_G();
 
-#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
-       applet_long_options = start_stop_daemon_longopts;
-#endif
-
        /* -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 */
@@ -457,8 +458,9 @@ int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
        /* -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:"
+       opt = GETOPT32(argv, "KSbqtma:n:s:u:c:x:p:"
                IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
+               LONGOPTS
                &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
                IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
                /* We accept and ignore -R <param> / --retry <param> */
index 03c13908e940726a2d71e4389665013ec18577f1..d90ac8f941449feafd350835decee018cc8ab5a4 100644 (file)
@@ -967,6 +967,11 @@ static const char diff_longopts[] ALIGN1 =
        "starting-file\0"            Required_argument "S"
        "minimal\0"                  No_argument       "d"
        ;
+# define GETOPT32 getopt32long
+# define LONGOPTS ,diff_longopts
+#else
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
 int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -980,10 +985,7 @@ int diff_main(int argc UNUSED_PARAM, char **argv)
 
        /* exactly 2 params; collect multiple -L <label>; -U N */
        opt_complementary = "=2";
-#if ENABLE_FEATURE_DIFF_LONG_OPTIONS
-       applet_long_options = diff_longopts;
-#endif
-       getopt32(argv, "abdiL:*NqrsS:tTU:+wupBE",
+       GETOPT32(argv, "abdiL:*NqrsS:tTU:+wupBE" LONGOPTS,
                        &L_arg, &s_start, &opt_U_context);
        argv += optind;
        while (L_arg)
index aae7b79878e575183aa37f59e1c17c1f9007721c..8e09ef4888b667b307fdf30df85d5d8812f3d11b 100644 (file)
@@ -111,10 +111,9 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
                        "no-backup-if-mismatch\0" No_argument       "\xff" /*ignored*/
 # endif
                        ;
-               applet_long_options = patch_longopts;
 #endif
                /* -f,-E,-g are ignored */
-               opt = getopt32(argv, "p:i:RN""fEg:", &p, &i, NULL);
+               opt = getopt32long(argv, "p:i:RN""fEg:", patch_longopts, &p, &i, NULL);
                if (opt & OPT_R)
                        plus = '-';
                patch_level = xatoi(p); /* can be negative! */
index 22580cf71e2fa564827972a9a418c59dc9920117..1a1098859c55e85208d02ddad0397f7c70595e72 100644 (file)
@@ -1508,16 +1508,14 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
        opt_e = opt_f = NULL;
        opt_i = NULL;
        opt_complementary = "nn"; /* count -n */
-
-       IF_LONG_OPTS(applet_long_options = sed_longopts);
-
        /* -i must be first, to match OPT_in_place definition */
        /* -E is a synonym of -r:
         * GNU sed 4.2.1 mentions it in neither --help
         * nor manpage, but does recognize it.
         */
-       opt = getopt32(argv, "i::rEne:*f:*", &opt_i, &opt_e, &opt_f,
-                           &G.be_quiet); /* counter for -n */
+       opt = getopt32long(argv, "i::rEne:*f:*", sed_longopts,
+                       &opt_i, &opt_e, &opt_f,
+                       &G.be_quiet); /* counter for -n */
        //argc -= optind;
        argv += optind;
        if (opt & OPT_in_place) { // -i
index 2d0d1c8b99cb31349510969c3adc6814fc9a475d..c3d37a64da70574c4bcb0824c58e665a66eb819a 100644 (file)
@@ -495,13 +495,8 @@ int xargs_main(int argc, char **argv)
 
        INIT_G();
 
-#if ENABLE_DESKTOP && ENABLE_LONG_OPTS
-       /* For example, Fedora's build system uses --no-run-if-empty */
-       applet_long_options =
-               "no-run-if-empty\0" No_argument "r"
-               ;
-#endif
-       opt = getopt32(argv, OPTION_STR,
+       opt = getopt32long(argv, OPTION_STR,
+               "no-run-if-empty\0" No_argument "r",
                &max_args, &max_chars, &G.eof_str, &G.eof_str
                IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
        );
index 3f3e033fe65c5ac61cfde64cc20bfad7dcbec0ff..64d1f2fcb9ba767804913bf7ec85ca78c46f70d7 100644 (file)
@@ -1180,19 +1180,24 @@ void bb_sanitize_stdio(void) FAST_FUNC;
 int sanitize_env_if_suid(void) FAST_FUNC;
 
 
+/* For top, ps. Some argv[i] are replaced by malloced "-opt" strings */
+void make_all_argv_opts(char **argv) FAST_FUNC;
 char* single_argv(char **argv) FAST_FUNC;
-extern const char *const bb_argv_dash[]; /* "-", NULL */
-extern const char *opt_complementary;
-#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
-#define No_argument "\0"
-#define Required_argument "\001"
-#define Optional_argument "\002"
-extern const char *applet_long_options;
-#endif
+extern const char *const bb_argv_dash[]; /* { "-", NULL } */
 extern uint32_t option_mask32;
+//TODO: get rid of this global variable. How about a trick where optstring can be
+// "^optchars""\0""complementary" (the leading "^" is an indicator)?
+extern const char *opt_complementary;
 uint32_t getopt32(char **argv, const char *applet_opts, ...) FAST_FUNC;
-/* For top, ps. Some argv[i] are replaced by malloced "-opt" strings */
-void make_all_argv_opts(char **argv) FAST_FUNC;
+# define No_argument "\0"
+# define Required_argument "\001"
+# define Optional_argument "\002"
+#if ENABLE_LONG_OPTS
+uint32_t getopt32long(char **argv, const char *optstring, const char *longopts, ...) FAST_FUNC;
+#else
+#define getopt32long(argv,optstring,longopts,...) \
+       getopt32(argv,optstring,##__VA_ARGS__)
+#endif
 /* BSD-derived getopt() functions require that optind be set to 1 in
  * order to reset getopt() state.  This used to be generally accepted
  * way of resetting getopt().  However, glibc's getopt()
index 513415894ebd6aa5393106a46868c591e66f91ea..b2b4de8cbde5e947fa895f78b0716e3d42419fcf 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
 # include <getopt.h>
 #endif
 #include "libbb.h"
@@ -99,17 +99,18 @@ getopt32(char **argv, const char *applet_opts, ...)
  "!"    Report bad option, missing required options,
         inconsistent options with all-ones return value (instead of abort).
 
-const char *applet_long_options
+uint32_t
+getopt32long(char **argv, const char *applet_opts, const char *logopts...)
 
-        This struct allows you to define long options:
+        This allows you to define long options:
 
         static const char applet_longopts[] ALIGN1 =
                 //"name\0"  has_arg     val
                 "verbose\0" No_argument "v"
                 ;
-        applet_long_options = applet_longopts;
+        opt = getopt32long(argv, applet_opts, applet_longopts, ...);
 
-        The last member of struct option (val) typically is set to
+        The last element (val) typically is set to
         matching short option from applet_opts. If there is no matching
         char in applet_opts, then:
         - return bit has next position after short options
@@ -317,20 +318,21 @@ typedef struct {
        int *counter;
 } t_complementary;
 
-/* You can set applet_long_options for parse called long options */
-#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
+uint32_t option_mask32;
+
+#if ENABLE_LONG_OPTS
 static const struct option bb_null_long_options[1] = {
        { 0, 0, 0, 0 }
 };
-const char *applet_long_options;
+#else
+#define vgetopt32(argv,applet_opts,applet_long_options,p) \
+       vgetopt32(argv,applet_opts,p)
 #endif
 
-uint32_t option_mask32;
-
 /* Please keep getopt32 free from xmalloc */
 
-uint32_t FAST_FUNC
-getopt32(char **argv, const char *applet_opts, ...)
+static uint32_t
+vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options, va_list p)
 {
        int argc;
        unsigned flags = 0;
@@ -340,8 +342,7 @@ getopt32(char **argv, const char *applet_opts, ...)
        int c;
        const unsigned char *s;
        t_complementary *on_off;
-       va_list p;
-#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
        const struct option *l_o;
        struct option *long_options = (struct option *) &bb_null_long_options;
 #endif
@@ -356,8 +357,6 @@ getopt32(char **argv, const char *applet_opts, ...)
        /* skip 0: some applets cheat: they do not actually HAVE argv[0] */
        argc = 1 + string_array_len(argv + 1);
 
-       va_start(p, applet_opts);
-
        on_off = complementary;
        memset(on_off, 0, sizeof(complementary));
 
@@ -394,7 +393,7 @@ getopt32(char **argv, const char *applet_opts, ...)
                c++;
        }
 
-#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
        if (applet_long_options) {
                const char *optstr;
                unsigned i, count;
@@ -433,12 +432,8 @@ getopt32(char **argv, const char *applet_opts, ...)
                        c++;
  next_long: ;
                }
-               /* Make it unnecessary to clear applet_long_options
-                * by hand after each call to getopt32
-                */
-               applet_long_options = NULL;
        }
-#endif /* ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG */
+#endif /* ENABLE_LONG_OPTS */
 
        for (s = (const unsigned char *)opt_complementary; s && *s; s++) {
                t_complementary *pair;
@@ -517,9 +512,9 @@ getopt32(char **argv, const char *applet_opts, ...)
                }
                s--;
        }
-       opt_complementary = NULL;
-       va_end(p);
 
+       /* Make it unnecessary to clear it by hand after each getopt32 call */
+       opt_complementary = NULL;
        /* In case getopt32 was already called:
         * reset the libc getopt() function, which keeps internal state.
         * run_nofork_applet() does this, but we might end up here
@@ -531,7 +526,7 @@ getopt32(char **argv, const char *applet_opts, ...)
         * "fake" short options, like this one:
         * wget $'-\203' "Test: test" http://kernel.org/
         * (supposed to act as --header, but doesn't) */
-#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
        while ((c = getopt_long(argc, argv, applet_opts,
                        long_options, NULL)) != -1) {
 #else
@@ -592,3 +587,29 @@ getopt32(char **argv, const char *applet_opts, ...)
                bb_show_usage();
        return (int32_t)-1;
 }
+
+uint32_t FAST_FUNC
+getopt32(char **argv, const char *applet_opts, ...)
+{
+       uint32_t opt;
+       va_list p;
+
+       va_start(p, applet_opts);
+       opt = vgetopt32(argv, applet_opts, NULL, p);
+       va_end(p);
+       return opt;
+}
+
+#if ENABLE_LONG_OPTS
+uint32_t FAST_FUNC
+getopt32long(char **argv, const char *applet_opts, const char *longopts, ...)
+{
+       uint32_t opt;
+       va_list p;
+
+       va_start(p, longopts);
+       opt = vgetopt32(argv, applet_opts, longopts, p);
+       va_end(p);
+       return opt;
+}
+#endif
index e55847f9307657fb409896a848e6508880dc362b..15c92a7cdcf267d5aab64ca125cf266ead15fac4 100644 (file)
@@ -45,14 +45,15 @@ static void jump(void)
 {
        /* Special case. We arrive here if NOFORK applet
         * calls xfunc, which then decides to die.
-        * We don't die, but jump instead back to caller.
+        * We don't die, but instead jump back to caller.
         * NOFORK applets still cannot carelessly call xfuncs:
         * p = xmalloc(10);
         * q = xmalloc(10); // BUG! if this dies, we leak p!
         */
        /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0).
         * This works because exitcodes are bytes,
-        * run_nofork_applet() ensures that by "& 0xff" */
+        * run_nofork_applet() ensures that by "& 0xff"
+        */
        longjmp(die_jmp, xfunc_error_retval | 0x100);
 }
 
@@ -92,12 +93,12 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
 
        logmode = LOGMODE_STDIO;
        xfunc_error_retval = EXIT_FAILURE;
-       /* In case getopt() or getopt32() was already called:
+       /* In case getopt() was already called:
         * reset the libc getopt() function, which keeps internal state.
+        * (getopt32() does it itself, but getopt() doesn't (and can't))
         */
        GETOPT_RESET();
-//?    applet_long_options = NULL;
-//?    opt_complementary = NULL;
+       /* opt_complementary = NULL; - cleared by each getopt32() call anyway */
 
        argc = string_array_len(argv);
 
@@ -122,8 +123,7 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
        restore_nofork_data(&old);
        /* Other globals can be simply reset to defaults */
        GETOPT_RESET();
-//?    applet_long_options = NULL;
-//?    opt_complementary = NULL;
+       /* opt_complementary = NULL; - cleared by each getopt32() call anyway */
 
        return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
 }
@@ -138,8 +138,7 @@ void FAST_FUNC run_noexec_applet_and_exit(int a, const char *name, char **argv)
        xfunc_error_retval = EXIT_FAILURE;
        die_func = NULL;
        GETOPT_RESET();
-//?    applet_long_options = NULL;
-//?    opt_complementary = NULL;
+       /* opt_complementary = NULL; - cleared by each getopt32() call anyway */
 
 //TODO: think pidof, pgrep, pkill!
 //set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
index 30f7e72dc7c5208cfd9ce5a6d28c52d4b25ee421..5a2b0435248fc0229183973d48db31577fda3fef 100644 (file)
 //config:config ADDGROUP
 //config:      bool "addgroup (8.2 kb)"
 //config:      default y
+//config:      select LONG_OPTS
 //config:      help
 //config:      Utility for creating a new group account.
 //config:
-//config:config FEATURE_ADDGROUP_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on ADDGROUP && LONG_OPTS
-//config:
 //config:config FEATURE_ADDUSER_TO_GROUP
 //config:      bool "Support adding users to groups"
 //config:      default y
@@ -131,12 +127,11 @@ static void new_group(char *group, gid_t gid)
 #endif
 }
 
-#if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
+//FIXME: upstream addgroup has no short options! NOT COMPATIBLE!
 static const char addgroup_longopts[] ALIGN1 =
                "gid\0"                 Required_argument "g"
                "system\0"              No_argument       "S"
                ;
-#endif
 
 /*
  * addgroup will take a login_name as its first parameter.
@@ -155,16 +150,13 @@ int addgroup_main(int argc UNUSED_PARAM, char **argv)
        if (geteuid()) {
                bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
        }
-#if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
-       applet_long_options = addgroup_longopts;
-#endif
        /* Syntax:
         *  addgroup group
         *  addgroup -g num group
         *  addgroup user group
         * Check for min, max and missing args */
        opt_complementary = "-1:?2";
-       opts = getopt32(argv, "g:S", &gid);
+       opts = getopt32long(argv, "g:S", addgroup_longopts, &gid);
        /* move past the commandline options */
        argv += optind;
        //argc -= optind;
index 913dbaf8345709f2b786de6f1409f2c3cd9e7048..8b92df9234805db134db8cd7bc7ceb380e00a66f 100644 (file)
 //config:config ADDUSER
 //config:      bool "adduser (15 kb)"
 //config:      default y
+//config:      select LONG_OPTS
 //config:      help
 //config:      Utility for creating a new user account.
 //config:
-//config:config FEATURE_ADDUSER_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on ADDUSER && LONG_OPTS
-//config:
 //config:config FEATURE_CHECK_NAMES
 //config:      bool "Enable sanity check on user/group names in adduser and addgroup"
 //config:      default n
@@ -148,15 +144,7 @@ static int addgroup_wrapper(struct passwd *p, const char *group_name)
                /* Add user to his own group with the first free gid
                 * found in passwd_study.
                 */
-#if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS || !ENABLE_ADDGROUP
-               /* We try to use --gid, not -g, because "standard" addgroup
-                * has no short option -g, it has only long --gid.
-                */
                argv[1] = (char*)"--gid";
-#else
-               /* Breaks if system in fact does NOT use busybox addgroup */
-               argv[1] = (char*)"-g";
-#endif
                argv[2] = utoa(p->pw_gid);
                argv[3] = (char*)"--";
                argv[4] = p->pw_name;
@@ -174,7 +162,7 @@ static void passwd_wrapper(const char *login_name)
        bb_error_msg_and_die("can't execute passwd, you must set password manually");
 }
 
-#if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
+//FIXME: upstream adduser has no short options! NOT COMPATIBLE!
 static const char adduser_longopts[] ALIGN1 =
                "home\0"                Required_argument "h"
                "gecos\0"               Required_argument "g"
@@ -187,7 +175,6 @@ static const char adduser_longopts[] ALIGN1 =
                "uid\0"                 Required_argument "u"
                "skel\0"                Required_argument "k"
                ;
-#endif
 
 /*
  * adduser will take a login_name as its first parameter.
@@ -204,10 +191,6 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
        char *uid;
        const char *skel = "/etc/skel";
 
-#if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
-       applet_long_options = adduser_longopts;
-#endif
-
        /* got root? */
        if (geteuid()) {
                bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
@@ -221,7 +204,9 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
        /* at least one and at most two non-option args */
        /* disable interactive passwd for system accounts */
        opt_complementary = "-1:?2:SD";
-       opts = getopt32(argv, "h:g:s:G:DSHu:k:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid, &skel);
+       opts = getopt32long(argv, "h:g:s:G:DSHu:k:", adduser_longopts,
+                       &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell,
+                       &usegroup, &uid, &skel);
        if (opts & OPT_UID)
                pw.pw_uid = xatou_range(uid, 0, CONFIG_LAST_ID);
 
index e390da647dda7c2e690d1e6c5caa47f9547a2681..3c9ed68b958f66ba7adb929ee7f60d58badb95ac 100644 (file)
@@ -62,8 +62,7 @@ int chpasswd_main(int argc UNUSED_PARAM, char **argv)
                bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
 
        opt_complementary = "m--ec:e--mc:c--em";
-       IF_LONG_OPTS(applet_long_options = chpasswd_longopts;)
-       opt = getopt32(argv, "emc:", &algo);
+       opt = getopt32long(argv, "emc:", chpasswd_longopts, &algo);
 
        while ((name = xmalloc_fgetline(stdin)) != NULL) {
                char *free_me;
index 136c619bb054712a0f0aa187532fa86ce2b0ee34..c10a0c8bdd39ad02f711fd9d93ca51f99e2f86eb 100644 (file)
@@ -106,14 +106,14 @@ int cryptpw_main(int argc UNUSED_PARAM, char **argv)
                "salt\0"        Required_argument "S"
                "method\0"      Required_argument "m"
        ;
-       applet_long_options = mkpasswd_longopts;
 #endif
        fd = STDIN_FILENO;
        opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
        opt_S = NULL;
        /* at most two non-option arguments; -P NUM */
        opt_complementary = "?2";
-       getopt32(argv, "sP:+S:m:a:", &fd, &opt_S, &opt_m, &opt_m);
+       getopt32long(argv, "sP:+S:m:a:", mkpasswd_longopts,
+                       &fd, &opt_S, &opt_m, &opt_m);
        argv += optind;
 
        /* have no idea how to handle -s... */
index f5bc3c28abb74dcc0f8b511e778e01c088e2b688..5ee22d4f1df32a8c7920b2dca16c2ab09927fc4d 100644 (file)
@@ -68,9 +68,8 @@ int deluser_main(int argc, char **argv)
 #else
        int opt_delhome = 0;
        if (do_deluser) {
-               applet_long_options =
-                       "remove-home\0" No_argument "\xff";
-               opt_delhome = getopt32(argv, "");
+               opt_delhome = getopt32long(argv, "",
+                               "remove-home\0" No_argument "\xff");
                argv += opt_delhome;
                argc -= opt_delhome;
        }
index 22b29cbf34c025e045a15f1931b71c7a7a07f2c7..a0e0d4e4b5c6f3cb9a01ffa0e1fba41b2fe22acf 100644 (file)
@@ -377,8 +377,6 @@ int conspy_main(int argc UNUSED_PARAM, char **argv)
                "follow\0"       No_argument "f"
                "framebuffer\0"  No_argument "F"
                ;
-
-       applet_long_options = conspy_longopts;
 #endif
 #define keybuf bb_common_bufsiz1
        setup_common_bufsiz();
@@ -387,7 +385,7 @@ int conspy_main(int argc UNUSED_PARAM, char **argv)
        strcpy(G.vcsa_name, DEV_VCSA);
 
        // numeric params
-       opts = getopt32(argv, "vcQsndfFx:+y:+", &G.x, &G.y);
+       opts = getopt32long(argv, "vcQsndfFx:+y:+", conspy_longopts, &G.x, &G.y);
        argv += optind;
        ttynum = 0;
        if (argv[0]) {
index 14b1ed056fb7bf69069e1960770e7dc33b911bc8..5986ab27205f722b3295bb0cc76aee253f9fc294 100644 (file)
@@ -124,11 +124,9 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
 
        if (IS_NANDDUMP) {
                opt_complementary = "=1";
-#if ENABLE_LONG_OPTS
-               applet_long_options =
-                       "bb\0" Required_argument "\xff"; /* no short equivalent */
-#endif
-               opts = getopt32(argv, "ons:f:l:", &opt_s, &opt_f, &opt_l, &opt_bb);
+               opts = getopt32long(argv, "ons:f:l:",
+                               "bb\0" Required_argument "\xff", /* no short equivalent */
+                               &opt_s, &opt_f, &opt_l, &opt_bb);
        } else { /* nandwrite */
                opt_complementary = "-1:?2";
                opts = getopt32(argv, "pns:", &opt_s);
index e899935c4eb164f8ca769c15b636a835d8120e37..2dac8a8953cf4bafe254d42cca0da727d3e04a82 100644 (file)
@@ -566,9 +566,8 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv)
 
        INIT_G();
 
-       IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
        opt_complementary = MODPROBE_COMPLEMENTARY;
-       opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
+       opt = getopt32long(argv, INSMOD_OPTS MODPROBE_OPTS, modprobe_longopts INSMOD_ARGS);
        argv += optind;
 
        /* Goto modules location */
index d70f4ca77e957e4f21c3b85968963d8d97d6e4ba..1fc20364f78fefedc3050591fefd36ad0529a3fd 100644 (file)
@@ -361,12 +361,14 @@ int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
        /*
         * Decipher the command line
         */
+       opt_complementary = "-2:vv:cc"; /* must have 2 to 3 params; -v and -c count */
 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
-       applet_long_options = ftpgetput_longopts;
+       getopt32long(argv, "cvu:p:P:", ftpgetput_longopts,
+#else
+       getopt32(argv, "cvu:p:P:",
 #endif
-       opt_complementary = "-2:vv:cc"; /* must have 2 to 3 params; -v and -c count */
-       getopt32(argv, "cvu:p:P:", &user, &password, &port,
-                                       &verbose_flag, &do_continue);
+                       &user, &password, &port, &verbose_flag, &do_continue
+       );
        argv += optind;
 
        /* We want to do exactly _one_ DNS lookup, since some
index ea0ff95b78243b78e65bf15c8eab80e2f7c1d792..d87f6562f4aefb27ba814add2c0d71495bf958cf 100644 (file)
@@ -132,8 +132,9 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
        char *buf;
        char *hostname_str;
 
-#if ENABLE_LONG_OPTS
-       applet_long_options =
+       /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14),
+        * supports hostname's options too (not just -v as manpage says) */
+       opts = getopt32(argv, "dfisF:v", &hostname_str,
                "domain\0"     No_argument "d"
                "fqdn\0"       No_argument "f"
        //Enable if seen in active use in some distro:
@@ -142,12 +143,7 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
        //      "short\0"      No_argument "s"
        //      "verbose\0"    No_argument "v"
                "file\0"       No_argument "F"
-               ;
-
-#endif
-       /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14),
-        * supports hostname's options too (not just -v as manpage says) */
-       opts = getopt32(argv, "dfisF:v", &hostname_str);
+       );
        argv += optind;
        buf = safe_gethostname();
        if (ENABLE_DNSDOMAINNAME) {
index a845c4e7e24b6baa841861f92cf7d9d900dfc5a3..5e769b61d57f688c0805838839074cfd702d31ad 100644 (file)
@@ -493,19 +493,15 @@ int ifenslave_main(int argc UNUSED_PARAM, char **argv)
                OPT_d = (1 << 1),
                OPT_f = (1 << 2),
        };
-#if ENABLE_LONG_OPTS
-       static const char ifenslave_longopts[] ALIGN1 =
+
+       INIT_G();
+
+       opt = getopt32long(argv, "cdfa",
                "change-active\0"  No_argument "c"
                "detach\0"         No_argument "d"
                "force\0"          No_argument "f"
                /* "all-interfaces\0" No_argument "a" */
-               ;
-
-       applet_long_options = ifenslave_longopts;
-#endif
-       INIT_G();
-
-       opt = getopt32(argv, "cdfa");
+       );
        argv += optind;
        if (opt & (opt-1)) /* Only one option can be given */
                bb_show_usage();
index 83937828f1d49cd093213d4f096965472d5b3251..1d5db969c3ef54552759fd2d575ae05678acb2b3 100644 (file)
@@ -108,6 +108,11 @@ int get_prefix(unsigned long netmask);
                "silent\0"    No_argument "s" // don’t ever display error messages
 # endif
                ;
+# define GETOPT32 getopt32long
+# define LONGOPTS ,ipcalc_longopts
+#else
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
 int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -125,11 +130,8 @@ int ipcalc_main(int argc UNUSED_PARAM, char **argv)
 #define ipaddr    (s_ipaddr.s_addr)
        char *ipstr;
 
-#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
-       applet_long_options = ipcalc_longopts;
-#endif
        opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
-       opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
+       opt = GETOPT32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs") LONGOPTS);
        argv += optind;
        if (opt & SILENT)
                logmode = LOGMODE_NONE; /* suppress error_msg() output */
index 43081efca116e7390888b11ab51be8213ab257dc..84969aa81abaf7345ace2fd91c2490e26fb59348 100644 (file)
@@ -1103,12 +1103,12 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
        /* Parse command line */
        /* O,x: list; -T,-t,-A take numeric param */
        IF_UDHCP_VERBOSE(opt_complementary = "vv";)
-       IF_LONG_OPTS(applet_long_options = udhcpc6_longopts;)
-       opt = getopt32(argv, "i:np:qRr:s:T:+t:+SA:+O:*ox:*f"
+       opt = getopt32long(argv, "i:np:qRr:s:T:+t:+SA:+O:*ox:*f"
                USE_FOR_MMU("b")
                ///IF_FEATURE_UDHCPC_ARPING("a")
                IF_FEATURE_UDHCP_PORT("P:")
                "v"
+               , udhcpc6_longopts
                , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
                , &client_config.script /* s */
                , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
index ccf04993d80d6a11e6aba627e6b7fa2865296555..5f87f8586577727b86ef98c014232f2ebb66c45d 100644 (file)
@@ -1297,12 +1297,12 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
        /* Parse command line */
        /* O,x: list; -T,-t,-A take numeric param */
        IF_UDHCP_VERBOSE(opt_complementary = "vv";)
-       IF_LONG_OPTS(applet_long_options = udhcpc_longopts;)
-       opt = getopt32(argv, "CV:H:h:F:i:np:qRr:s:T:+t:+SA:+O:*ox:*fB"
+       opt = getopt32long(argv, "CV:H:h:F:i:np:qRr:s:T:+t:+SA:+O:*ox:*fB"
                USE_FOR_MMU("b")
                IF_FEATURE_UDHCPC_ARPING("a::")
                IF_FEATURE_UDHCP_PORT("P:")
                "v"
+               , udhcpc_longopts
                , &str_V, &str_h, &str_h, &str_F
                , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
                , &client_config.script /* s */
index 8aafc0d571168b12ce54815295a80cee362dee5c..fb1860ff6ecf1eba4a7a47110cf852d0894ff2b6 100644 (file)
@@ -51,12 +51,11 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv)
                "decimal\0"   No_argument       "d"
                ;
 
-       applet_long_options = dumpleases_longopts;
 #endif
        init_unicode();
 
        opt_complementary = "=0:a--r:r--a";
-       opt = getopt32(argv, "arf:d", &file);
+       opt = getopt32long(argv, "arf:d", dumpleases_longopts, &file);
 
        fd = xopen(file, O_RDONLY);
 
index e47c9a51b5bfe913d61f9f49a4b70c7a83fa518a..b661f727ba5cdcb3153c157af4923b9cf5c69556 100644 (file)
@@ -1365,6 +1365,11 @@ IF_DESKTOP(      "no-clobber\0"       No_argument       "\xf0")
 IF_DESKTOP(    "no-host-directories\0" No_argument    "\xf0")
 IF_DESKTOP(    "no-parent\0"        No_argument       "\xf0")
                ;
+# define GETOPT32 getopt32long
+# define LONGOPTS ,wget_longopts
+#else
+# define GETOPT32 getopt32
+# define LONGOPTS
 #endif
 
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
@@ -1381,11 +1386,10 @@ IF_DESKTOP(     "no-parent\0"        No_argument       "\xf0")
        G.user_agent = "Wget"; /* "User-Agent" header field */
 
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
-       applet_long_options = wget_longopts;
 #endif
        opt_complementary = "-1" /* at least one URL */
                IF_FEATURE_WGET_LONG_OPTIONS(":\xff::"); /* --header is a list */
-       getopt32(argv, "cqSO:P:Y:U:T:+"
+       GETOPT32(argv, "cqSO:P:Y:U:T:+"
                /*ignored:*/ "t:"
                /*ignored:*/ "n::"
                /* wget has exactly four -n<letter> opts, all of which we can ignore:
@@ -1396,6 +1400,7 @@ IF_DESKTOP(       "no-parent\0"        No_argument       "\xf0")
                 * "n::" above says that we accept -n[ARG].
                 * Specifying "n:" would be a bug: "-n ARG" would eat ARG!
                 */
+               LONGOPTS
                , &G.fname_out, &G.dir_prefix,
                &G.proxy_flag, &G.user_agent,
                IF_FEATURE_WGET_TIMEOUT(&G.timeout_seconds) IF_NOT_FEATURE_WGET_TIMEOUT(NULL),
index ae87fb5545d469c54d63ddfad157397d997390a5..3cf9e928a9202a2d9995d3be2278155d01983f48 100644 (file)
 //config:      depends on SELINUX
 //config:      help
 //config:      Enable support to change the security context of file.
-//config:
-//config:config FEATURE_CHCON_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on CHCON && LONG_OPTS
 
 //applet:IF_CHCON(APPLET(chcon, BB_DIR_USR_BIN, BB_SUID_DROP))
 
 //usage:#define chcon_trivial_usage
 //usage:       "[OPTIONS] CONTEXT FILE..."
 //usage:       "\n     chcon [OPTIONS] [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE..."
-//usage:       IF_FEATURE_CHCON_LONG_OPTIONS(
 //usage:       "\n     chcon [OPTIONS] --reference=RFILE FILE..."
-//usage:       )
+//usage:
 //usage:#define chcon_full_usage "\n\n"
 //usage:       "Change the security context of each FILE to CONTEXT\n"
-//usage:       IF_FEATURE_CHCON_LONG_OPTIONS(
-//usage:     "\n       -v,--verbose            Verbose"
-//usage:     "\n       -c,--changes            Report changes made"
-//usage:     "\n       -h,--no-dereference     Affect symlinks instead of their targets"
-//usage:     "\n       -f,--silent,--quiet     Suppress most error messages"
-//usage:     "\n       --reference RFILE       Use RFILE's group instead of using a CONTEXT value"
-//usage:     "\n       -u,--user USER          Set user/role/type/range in the target"
-//usage:     "\n       -r,--role ROLE          security context"
-//usage:     "\n       -t,--type TYPE"
-//usage:     "\n       -l,--range RANGE"
-//usage:     "\n       -R,--recursive          Recurse"
-//usage:       )
-//usage:       IF_NOT_FEATURE_CHCON_LONG_OPTIONS(
 //usage:     "\n       -v      Verbose"
 //usage:     "\n       -c      Report changes made"
 //usage:     "\n       -h      Affect symlinks instead of their targets"
 //usage:     "\n       -f      Suppress most error messages"
+//usage:       IF_LONG_OPTS(
+//usage:     "\n       --reference RFILE Use RFILE's group instead of using a CONTEXT value"
+//usage:       )
 //usage:     "\n       -u USER Set user/role/type/range in the target security context"
 //usage:     "\n       -r ROLE"
 //usage:     "\n       -t TYPE"
 //usage:     "\n       -l RNG"
 //usage:     "\n       -R      Recurse"
-//usage:       )
 
 #include <selinux/context.h>
 
@@ -68,7 +51,7 @@
 #define OPT_TYPE               (1<<6)  /* 't' */
 #define OPT_RANGE              (1<<7)  /* 'l' */
 #define OPT_VERBOSE            (1<<8)  /* 'v' */
-#define OPT_REFERENCE          ((1<<9) * ENABLE_FEATURE_CHCON_LONG_OPTIONS)
+#define OPT_REFERENCE          ((1<<9) * ENABLE_LONG_OPTS)
 #define OPT_COMPONENT_SPECIFIED        (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE)
 
 static char *user = NULL;
@@ -157,7 +140,7 @@ skip:
        return rc;
 }
 
-#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
+#if ENABLE_LONG_OPTS
 static const char chcon_longopts[] ALIGN1 =
        "recursive\0"      No_argument       "R"
        "changes\0"        No_argument       "c"
@@ -180,20 +163,18 @@ int chcon_main(int argc UNUSED_PARAM, char **argv)
        char *fname;
        int i, errors = 0;
 
-#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
-       applet_long_options = chcon_longopts;
-#endif
        opt_complementary = "-1"  /* at least 1 param */
                ":?"  /* error if exclusivity constraints are violated */
-#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
+#if ENABLE_LONG_OPTS
                ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
 #endif
                ":f--v:v--f";  /* 'verbose' and 'quiet' are exclusive */
-       getopt32(argv, "Rchfu:r:t:l:v",
-               &user, &role, &type, &range, &reference_file);
+       getopt32long(argv, "Rchfu:r:t:l:v", chcon_longopts,
+               &user, &role, &type, &range, &reference_file
+       );
        argv += optind;
 
-#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
+#if ENABLE_LONG_OPTS
        if (option_mask32 & OPT_REFERENCE) {
                /* FIXME: lgetfilecon() should be used when '-h' is specified.
                 * But current implementation follows the original one. */
index 09082d6c21356158b4065941e008bccea75fb804..199da25c6fe396f3b9e44fd9b3d97e479942af70 100644 (file)
 //config:      depends on SELINUX
 //config:      help
 //config:      Enable support to run command in specified security context.
-//config:
-//config:config FEATURE_RUNCON_LONG_OPTIONS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on RUNCON && LONG_OPTS
 
 //applet:IF_RUNCON(APPLET(runcon, BB_DIR_USR_BIN, BB_SUID_DROP))
 
 //usage:#define runcon_full_usage "\n\n"
 //usage:       "Run PROG in a different security context\n"
 //usage:     "\n       CONTEXT         Complete security context\n"
-//usage:       IF_FEATURE_RUNCON_LONG_OPTIONS(
-//usage:     "\n       -c,--compute    Compute process transition context before modifying"
-//usage:     "\n       -t,--type TYPE  Type (for same role as parent)"
-//usage:     "\n       -u,--user USER  User identity"
-//usage:     "\n       -r,--role ROLE  Role"
-//usage:     "\n       -l,--range RNG  Levelrange"
-//usage:       )
-//usage:       IF_NOT_FEATURE_RUNCON_LONG_OPTIONS(
 //usage:     "\n       -c      Compute process transition context before modifying"
 //usage:     "\n       -t TYPE Type (for same role as parent)"
 //usage:     "\n       -u USER User identity"
 //usage:     "\n       -r ROLE Role"
 //usage:     "\n       -l RNG  Levelrange"
-//usage:       )
 
 #include <selinux/context.h>
 /* from deprecated <selinux/flask.h>: */
@@ -108,7 +94,7 @@ static context_t runcon_compute_new_context(char *user, char *role, char *type,
        return con;
 }
 
-#if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
+#if ENABLE_LONG_OPTS
 static const char runcon_longopts[] ALIGN1 =
        "user\0"    Required_argument "u"
        "role\0"    Required_argument "r"
@@ -140,11 +126,9 @@ int runcon_main(int argc UNUSED_PARAM, char **argv)
 
        selinux_or_die();
 
-#if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
-       applet_long_options = runcon_longopts;
-#endif
        opt_complementary = "-1";
-       opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range);
+       opts = getopt32long(argv, "r:t:u:l:ch", runcon_longopts,
+                               &role, &type, &user, &range);
        argv += optind;
 
        if (!(opts & OPTS_CONTEXT_COMPONENT)) {
index b55e07adb866ddd7325d14a8acc9987a5f1eff0d..0c9158508734fe9e633c6fb0f9f39dbb020f7150 100644 (file)
@@ -44,11 +44,10 @@ int flock_main(int argc UNUSED_PARAM, char **argv)
                "unlock\0"      No_argument       "u"
                "nonblock\0"    No_argument       "n"
                ;
-       applet_long_options = flock_longopts;
 #endif
        opt_complementary = "-1";
 
-       opt = getopt32(argv, "+sxnu");
+       opt = getopt32long(argv, "+sxnu", flock_longopts);
        argv += optind;
 
        if (argv[1]) {
index c1f31569fb0f0981a25b30bf2eca82927256df81..af715da5e74add3d4e5310b57355e6ca225f61d8 100644 (file)
@@ -36,15 +36,14 @@ int fsfreeze_main(int argc UNUSED_PARAM, char **argv)
        unsigned opts;
        int fd;
 
-       applet_long_options =
-               "freeze\0"   No_argument "\xff"
-               "unfreeze\0" No_argument "\xfe"
-       ;
        /* exactly one non-option arg: the mountpoint */
        /* one of opts is required */
        /* opts are mutually exclusive */
        opt_complementary = "=1:""\xff:\xfe:""\xff--\xfe:\xfe--\xff";
-       opts = getopt32(argv, "");
+       opts = getopt32long(argv, "",
+               "freeze\0"   No_argument "\xff"
+               "unfreeze\0" No_argument "\xfe"
+       );
 
        fd = xopen(argv[optind], O_RDONLY);
        /* Works with NULL arg on linux-4.8.0 */
index 1fbf0c857679be9de61b2da82d44b4618dd2dea2..8f0a0538fde968d56f96e426ae084048915bf4c6 100644 (file)
@@ -69,11 +69,10 @@ int fstrim_main(int argc UNUSED_PARAM, char **argv)
                "minimum\0"   Required_argument    "m"
                "verbose\0"   No_argument          "v"
                ;
-       applet_long_options = fstrim_longopts;
 #endif
 
        opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */
-       opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m);
+       opts = getopt32long(argv, "o:l:m:v", fstrim_longopts, &arg_o, &arg_l, &arg_m);
 
        memset(&range, 0, sizeof(range));
        range.len = ULLONG_MAX;
index 5fe0da7cdca8cc0c93e5830444ae747b92b23d4c..a151b7e560c629292bc6abe0c50e873b6144d0de 100644 (file)
@@ -42,8 +42,8 @@
 //config:
 //config:config FEATURE_GETOPT_LONG
 //config:      bool "Support -l LONGOPTs"
-//config:      default y if LONG_OPTS
-//config:      depends on GETOPT
+//config:      default y
+//config:      depends on GETOPT && LONG_OPTS
 //config:      help
 //config:      Enable support for long options (option -l).
 
 //usage:#define getopt_trivial_usage
 //usage:       "[OPTIONS] [--] OPTSTRING PARAMS"
 //usage:#define getopt_full_usage "\n\n"
-//usage:       IF_LONG_OPTS(
-//usage:       IF_FEATURE_GETOPT_LONG(
-//usage:       "       -a,--alternative                Allow long options starting with single -\n"
-//usage:       "       -l,--longoptions LOPT[,...]     Long options to recognize\n"
-//usage:       )
-//usage:       "       -n,--name PROGNAME              The name under which errors are reported"
-//usage:     "\n       -o,--options OPTSTRING          Short options to recognize"
-//usage:     "\n       -q,--quiet                      No error messages on unrecognized options"
-//usage:     "\n       -Q,--quiet-output               No normal output"
-//usage:     "\n       -s,--shell SHELL                Set shell quoting conventions"
-//usage:     "\n       -T,--test                       Version test (exits with 4)"
-//usage:     "\n       -u,--unquoted                   Don't quote output"
-//usage:       )
-//usage:       IF_NOT_LONG_OPTS(
 //usage:       IF_FEATURE_GETOPT_LONG(
 //usage:       "       -a              Allow long options starting with single -\n"
 //usage:       "       -l LOPT[,...]   Long options to recognize\n"
@@ -79,7 +65,6 @@
 //usage:     "\n       -s SHELL        Set shell quoting conventions"
 //usage:     "\n       -T              Version test (exits with 4)"
 //usage:     "\n       -u              Don't quote output"
-//usage:       )
 //usage:       IF_FEATURE_GETOPT_LONG( /* example uses -l, needs FEATURE_GETOPT_LONG */
 //usage:     "\n"
 //usage:     "\nExample:"
@@ -411,8 +396,7 @@ int getopt_main(int argc, char **argv)
 #if !ENABLE_FEATURE_GETOPT_LONG
        opt = getopt32(argv, "+o:n:qQs:Tu", &optstr, &name, &s_arg);
 #else
-       applet_long_options = getopt_longopts;
-       opt = getopt32(argv, "+o:n:qQs:Tual:*",
+       opt = getopt32long(argv, "+o:n:qQs:Tual:*", getopt_longopts,
                                        &optstr, &name, &s_arg, &l_arg);
        /* Effectuate the read options for the applet itself */
        while (l_arg) {
index 5df56de153f0fcc4890cee474f364fd6884e8935..50f83d8c484c7aceb33809732aa7785d5daf13a4 100644 (file)
 //config:      shutdown in the hardware clock, so the hardware will keep the
 //config:      correct time when Linux is _not_ running.
 //config:
-//config:config FEATURE_HWCLOCK_LONG_OPTIONS
-//config:      bool "Support long options (--hctosys,...)"
-//config:      default y
-//config:      depends on HWCLOCK && LONG_OPTS
-//config:
 //config:config FEATURE_HWCLOCK_ADJTIME_FHS
 //config:      bool "Use FHS /var/lib/hwclock/adjtime"
 //config:      default n  # util-linux-ng in Fedora 13 still uses /etc/adjtime
@@ -293,12 +288,12 @@ static void set_system_clock_timezone(int utc)
 }
 
 //usage:#define hwclock_trivial_usage
-//usage:       IF_FEATURE_HWCLOCK_LONG_OPTIONS(
-//usage:       "[-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz]"
-//usage:       " [-l|--localtime] [-u|--utc]"
+//usage:       IF_LONG_OPTS(
+//usage:       "[-r|--show] [-s|--hctosys] [-w|--systohc] [--systz]"
+//usage:       " [--localtime] [-u|--utc]"
 //usage:       " [-f|--rtc FILE]"
 //usage:       )
-//usage:       IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS(
+//usage:       IF_NOT_LONG_OPTS(
 //usage:       "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]"
 //usage:       )
 //usage:#define hwclock_full_usage "\n\n"
@@ -306,12 +301,18 @@ static void set_system_clock_timezone(int utc)
 //usage:     "\n       -r      Show hardware clock time"
 //usage:     "\n       -s      Set system time from hardware clock"
 //usage:     "\n       -w      Set hardware clock from system time"
-//usage:     "\n       -t      Set in-kernel timezone, correct system time"
+//usage:       IF_LONG_OPTS(
+//usage:     "\n       --systz Set in-kernel timezone, correct system time"
+//usage:       )
 //usage:     "\n               if hardware clock is in local time"
 //usage:     "\n       -u      Assume hardware clock is kept in UTC"
-//usage:     "\n       -l      Assume hardware clock is kept in local time"
+//usage:       IF_LONG_OPTS(
+//usage:     "\n       --localtime     Assume hardware clock is kept in local time"
+//usage:       )
 //usage:     "\n       -f FILE Use specified device (e.g. /dev/rtc2)"
 
+//TODO: get rid of incompatible -t and -l aliases to --systz and --localtime
+
 #define HWCLOCK_OPT_LOCALTIME   0x01
 #define HWCLOCK_OPT_UTC         0x02
 #define HWCLOCK_OPT_SHOW        0x04
@@ -327,7 +328,7 @@ int hwclock_main(int argc UNUSED_PARAM, char **argv)
        unsigned opt;
        int utc;
 
-#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
+#if ENABLE_LONG_OPTS
        static const char hwclock_longopts[] ALIGN1 =
                "localtime\0" No_argument "l" /* short opt is non-standard */
                "utc\0"       No_argument "u"
@@ -337,14 +338,13 @@ int hwclock_main(int argc UNUSED_PARAM, char **argv)
                "systz\0"     No_argument "t" /* short opt is non-standard */
                "rtc\0"       Required_argument "f"
                ;
-       applet_long_options = hwclock_longopts;
 #endif
 
        /* Initialize "timezone" (libc global variable) */
        tzset();
 
        opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l";
-       opt = getopt32(argv, "lurswtf:", &rtcname);
+       opt = getopt32long(argv, "lurswtf:", hwclock_longopts, &rtcname);
 
        /* If -u or -l wasn't given check if we are using utc */
        if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
index 12c86b0ed1cf1353bb19ec3af34ac220008ed60c..d91b0b5090755dda5bd57deb6b7eaccba439504b 100644 (file)
 //config:      select PLATFORM_LINUX
 //config:      help
 //config:      Run program with namespaces of other processes.
-//config:
-//config:config FEATURE_NSENTER_LONG_OPTS
-//config:      bool "Enable long options"
-//config:      default y
-//config:      depends on NSENTER && LONG_OPTS
-//config:      help
-//config:      Support long options for the nsenter applet. This makes
-//config:      the busybox implementation more compatible with upstream.
 
 //applet:IF_NSENTER(APPLET(nsenter, BB_DIR_USR_BIN, BB_SUID_DROP))
 
 
 //usage:#define nsenter_trivial_usage
 //usage:       "[OPTIONS] [PROG [ARGS]]"
-//usage:#if ENABLE_FEATURE_NSENTER_LONG_OPTS
-//usage:#define nsenter_full_usage "\n"
-//usage:     "\n       -t,--target PID         Target process to get namespaces from"
-//usage:     "\n       -m,--mount[=FILE]       Enter mount namespace"
-//usage:     "\n       -u,--uts[=FILE]         Enter UTS namespace (hostname etc)"
-//usage:     "\n       -i,--ipc[=FILE]         Enter System V IPC namespace"
-//usage:     "\n       -n,--net[=FILE]         Enter network namespace"
-//usage:     "\n       -p,--pid[=FILE]         Enter pid namespace"
-//usage:     "\n       -U,--user[=FILE]        Enter user namespace"
-//usage:     "\n       -S,--setuid UID         Set uid in entered namespace"
-//usage:     "\n       -G,--setgid GID         Set gid in entered namespace"
-//usage:     "\n       --preserve-credentials  Don't touch uids or gids"
-//usage:     "\n       -r,--root[=DIR]         Set root directory"
-//usage:     "\n       -w,--wd[=DIR]           Set working directory"
-//usage:     "\n       -F,--no-fork            Don't fork before exec'ing PROG"
-//usage:#else
 //usage:#define nsenter_full_usage "\n"
 //usage:     "\n       -t PID          Target process to get namespaces from"
 //usage:     "\n       -m[FILE]        Enter mount namespace"
 //usage:     "\n       -U[FILE]        Enter user namespace"
 //usage:     "\n       -S UID          Set uid in entered namespace"
 //usage:     "\n       -G GID          Set gid in entered namespace"
+//usage:       IF_LONG_OPTS(
+//usage:     "\n       --preserve-credentials  Don't touch uids or gids"
+//usage:       )
 //usage:     "\n       -r[DIR]         Set root directory"
 //usage:     "\n       -w[DIR]         Set working directory"
 //usage:     "\n       -F              Don't fork before exec'ing PROG"
-//usage:#endif
 
 #include <sched.h>
 #ifndef CLONE_NEWUTS
@@ -101,7 +79,7 @@ enum {
        OPT_root        = 1 << 9,
        OPT_wd          = 1 << 10,
        OPT_nofork      = 1 << 11,
-       OPT_prescred    = (1 << 12) * ENABLE_FEATURE_NSENTER_LONG_OPTS,
+       OPT_prescred    = (1 << 12) * ENABLE_LONG_OPTS,
 };
 enum {
        NS_USR_POS = 0,
@@ -130,7 +108,7 @@ static const struct namespace_descr ns_list[] = {
  */
 static const char opt_str[] ALIGN1 = "U::i::u::n::p::m::""t+S+G+r::w::F";
 
-#if ENABLE_FEATURE_NSENTER_LONG_OPTS
+#if ENABLE_LONG_OPTS
 static const char nsenter_longopts[] ALIGN1 =
        "user\0"                        Optional_argument       "U"
        "ipc\0"                         Optional_argument       "i"
@@ -190,8 +168,7 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv)
 
        memset(ns_ctx_list, 0, sizeof(ns_ctx_list));
 
-       IF_FEATURE_NSENTER_LONG_OPTS(applet_long_options = nsenter_longopts);
-       opts = getopt32(argv, opt_str,
+       opts = getopt32long(argv, opt_str, nsenter_longopts,
                        &ns_ctx_list[NS_USR_POS].path,
                        &ns_ctx_list[NS_IPC_POS].path,
                        &ns_ctx_list[NS_UTS_POS].path,
index 4c47c5369ade8e2922d8ff4464d19b76f6d5cd59..2a3d61f21ef0543a0a24b52b8d57f834c9f86619 100644 (file)
@@ -154,11 +154,11 @@ int rtcwake_main(int argc UNUSED_PARAM, char **argv)
                "seconds\0" Required_argument "s"
                "time\0"    Required_argument "t"
                ;
-       applet_long_options = rtcwake_longopts;
 #endif
        /* Must have -s or -t, exclusive */
        opt_complementary = "s:t:s--t:t--s";
-       opt = getopt32(argv, "alud:m:s:t:", &rtcname, &suspend, &opt_seconds, &opt_time);
+       opt = getopt32long(argv, "alud:m:s:t:", rtcwake_longopts,
+                       &rtcname, &suspend, &opt_seconds, &opt_time);
 
        /* this is the default
        if (opt & RTCWAKE_OPT_AUTO)
index 4cb9842a0f56bf11695d6fe4b629f08f1ce8ffb0..8174c65bffe40a75073a3c5040a509cf64d4826b 100644 (file)
@@ -78,12 +78,10 @@ int script_main(int argc UNUSED_PARAM, char **argv)
                "quiet\0"   No_argument       "q"
                "timing\0"  Optional_argument "t"
                ;
-
-       applet_long_options = script_longopts;
 #endif
 
        opt_complementary = "?1"; /* max one arg */
-       opt = getopt32(argv, "ac:fqt::", &shell_arg, &str_t);
+       opt = getopt32long(argv, "ac:fqt::", script_longopts, &shell_arg, &str_t);
        //argc -= optind;
        argv += optind;
        if (argv[0]) {
index 0f85428abfda3e916d80f6487023ab7ef15ed186..5c532d6377a306c1e1b91db353cea4198f3f5c36 100644 (file)
@@ -421,9 +421,12 @@ int setpriv_main(int argc UNUSED_PARAM, char **argv)
        int opts;
        IF_FEATURE_SETPRIV_CAPABILITIES(char *inh_caps, *ambient_caps;)
 
-       applet_long_options = setpriv_longopts;
-       opts = getopt32(argv, "+"IF_FEATURE_SETPRIV_DUMP("d")
-                       IF_FEATURE_SETPRIV_CAPABILITIES("\xfe:\xfd:", &inh_caps, &ambient_caps));
+       opts = getopt32long(argv,
+               "+"IF_FEATURE_SETPRIV_DUMP("d")
+               IF_FEATURE_SETPRIV_CAPABILITIES("\xfe:\xfd:"),
+               setpriv_longopts
+               IF_FEATURE_SETPRIV_CAPABILITIES(, &inh_caps, &ambient_caps)
+       );
        argv += optind;
 
 #if ENABLE_FEATURE_SETPRIV_DUMP
index e7b95c72b57f4d49145eda8b157f5dfad7d23809..df377478f5e570761cd000031c3ae90ecc90f99f 100644 (file)
@@ -220,8 +220,7 @@ int unshare_main(int argc UNUSED_PARAM, char **argv)
                ":ru"      /* --map-root-user or -r implies -u */
                ":\xfd""m" /* --mount-proc implies -m */
        ;
-       applet_long_options = unshare_longopts;
-       opts = getopt32(argv, opt_str,
+       opts = getopt32long(argv, opt_str, unshare_longopts,
                        &proc_mnt_target, &prop_str, &setgrp_str,
                        &ns_ctx_list[NS_MNT_POS].path,
                        &ns_ctx_list[NS_UTS_POS].path,