Rework option handling to save space.
[oweals/busybox.git] / util-linux / getopt.c
index 625263184321b25c510d8da57913b97fcc44e59a..95ecba6e6645dbbfd69ac9eb3fd3af57e88770e2 100644 (file)
@@ -62,20 +62,19 @@ typedef enum {BASH,TCSH} shell_t;
 
 
 /* Some global variables that tells us how to parse. */
-shell_t shell=BASH; /* The shell we generate output for. */
-int quiet_errors=0; /* 0 is not quiet. */
-int quiet_output=0; /* 0 is not quiet. */
-int quote=1; /* 1 is do quote. */
-int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
+static shell_t shell=BASH; /* The shell we generate output for. */
+static int quiet_errors=0; /* 0 is not quiet. */
+static int quiet_output=0; /* 0 is not quiet. */
+static int quote=1; /* 1 is do quote. */
+static int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
 
 /* Function prototypes */
-const char *normalize(const char *arg);
-int generate_output(char * argv[],int argc,const char *optstr,
+static const char *normalize(const char *arg);
+static int generate_output(char * argv[],int argc,const char *optstr,
                     const struct option *longopts);
-void add_long_options(char *options);
-void add_longopt(const char *name,int has_arg);
-void set_shell(const char *new_shell);
-void set_initial_shell(void);
+static void add_long_options(char *options);
+static void add_longopt(const char *name,int has_arg);
+static void set_shell(const char *new_shell);
 
 
 /*
@@ -244,20 +243,23 @@ void add_longopt(const char *name,int has_arg)
  */
 void add_long_options(char *options)
 {
-        int arg_opt;
+        int arg_opt, tlen;
         char *tokptr=strtok(options,", \t\n");
         while (tokptr) {
                 arg_opt=no_argument;
-                if (strlen(tokptr) > 0) {
-                        if (tokptr[strlen(tokptr)-1] == ':') {
-                                if (tokptr[strlen(tokptr)-2] == ':') {
-                                        tokptr[strlen(tokptr)-2]='\0';
+               tlen=strlen(tokptr);
+                if (tlen > 0) {
+                        if (tokptr[tlen-1] == ':') {
+                                if (tlen > 1 && tokptr[tlen-2] == ':') {
+                                        tokptr[tlen-2]='\0';
+                                       tlen -= 2;
                                         arg_opt=optional_argument;
                                 } else {
-                                        tokptr[strlen(tokptr)-1]='\0';
+                                        tokptr[tlen-1]='\0';
+                                       tlen -= 1;
                                         arg_opt=required_argument;
                                 }
-                                if (strlen(tokptr) == 0)
+                                if (tlen == 0)
                                         error_msg("empty long option after -l or --long argument");
                         }
                         add_longopt(tokptr,arg_opt);
@@ -372,7 +374,7 @@ int getopt_main(int argc, char *argv[])
                         quote=0;
                         break;
                 default:
-                        usage(getopt_usage);
+                        show_usage();
                 }
 
         if (!optstr) {