ifupdown: save some 100+ bytes of code in addstr()
[oweals/busybox.git] / util-linux / getopt.c
index 16dcbbca0fe0d897e7843c18251627a3116fa7d2..17f3c4297b987e869bf610a1b6850da4b543e1cb 100644 (file)
@@ -1,20 +1,9 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getopt.c - Enhanced implementation of BSD getopt(1)
  *   Copyright (c) 1997, 1998, 1999, 2000  Frodo Looijaard <frodol@dds.nl>
  *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 /*
  *
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <ctype.h>
-#include <getopt.h>
-
 #include "busybox.h"
+#include <getopt.h>
 
 /* NON_OPT is the code that is returned when a non-option is found in '+'
    mode */
-static const int NON_OPT = 1;
+enum {
+       NON_OPT = 1,
 /* LONG_OPT is the code that is returned when a long option is found. */
-static const int LONG_OPT = 2;
+       LONG_OPT = 2
+};
 
 /* The shells recognized. */
 typedef enum {BASH,TCSH} shell_t;
@@ -71,7 +56,7 @@ static int alternative; /* 0 is getopt_long, 1 is getopt_long_only */
 /* Function prototypes */
 static const char *normalize(const char *arg);
 static int generate_output(char * argv[],int argc,const char *optstr,
-                   const struct option *longopts);
+               const struct option *longopts);
 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);
@@ -95,7 +80,7 @@ const char *normalize(const char *arg)
        free(BUFFER);
 
        if (!quote) { /* Just copy arg */
-              BUFFER=bb_xstrdup(arg);
+               BUFFER=xstrdup(arg);
                return BUFFER;
        }
 
@@ -149,7 +134,7 @@ const char *normalize(const char *arg)
  * Other settings are found in global variables.
  */
 int generate_output(char * argv[],int argc,const char *optstr,
-                   const struct option *longopts)
+               const struct option *longopts)
 {
        int exit_code = 0; /* We assume everything will be OK */
        int opt;
@@ -161,8 +146,8 @@ int generate_output(char * argv[],int argc,const char *optstr,
        optind=0; /* Reset getopt(3) */
 
        while ((opt = (alternative?
-                      getopt_long_only(argc,argv,optstr,longopts,&longindex):
-                      getopt_long(argc,argv,optstr,longopts,&longindex)))
+                       getopt_long_only(argc,argv,optstr,longopts,&longindex):
+                       getopt_long(argc,argv,optstr,longopts,&longindex)))
               != EOF)
                if (opt == '?' || opt == ':' )
                        exit_code = 1;
@@ -171,7 +156,7 @@ int generate_output(char * argv[],int argc,const char *optstr,
                                printf(" --%s",longopts[longindex].name);
                                if (longopts[longindex].has_arg)
                                        printf(" %s",
-                                              normalize(optarg?optarg:""));
+                                               normalize(optarg?optarg:""));
                        } else if (opt == NON_OPT)
                                printf(" %s",normalize(optarg));
                        else {
@@ -179,7 +164,7 @@ int generate_output(char * argv[],int argc,const char *optstr,
                                charptr = strchr(optstr,opt);
                                if (charptr != NULL && *++charptr == ':')
                                        printf(" %s",
-                                              normalize(optarg?optarg:""));
+                                               normalize(optarg?optarg:""));
                        }
                }
 
@@ -187,7 +172,7 @@ int generate_output(char * argv[],int argc,const char *optstr,
                printf(" --");
                while (optind < argc)
                        printf(" %s",normalize(argv[optind++]));
-               printf("\n");
+               puts("");
        }
        return exit_code;
 }
@@ -195,11 +180,11 @@ int generate_output(char * argv[],int argc,const char *optstr,
 static struct option *long_options;
 static int long_options_length; /* Length of array */
 static int long_options_nr; /* Nr of used elements in array */
-static const int LONG_OPTIONS_INCR = 10;
+enum { LONG_OPTIONS_INCR = 10 };
 #define init_longopt() add_longopt(NULL,0)
 
 /* Register a long option. The contents of name is copied. */
-void add_longopt(const char *name,int has_arg)
+void add_longopt(const char *name, int has_arg)
 {
        if (!name) { /* init */
                free(long_options);
@@ -224,7 +209,7 @@ void add_longopt(const char *name,int has_arg)
                long_options[long_options_nr-1].has_arg=has_arg;
                long_options[long_options_nr-1].flag=NULL;
                long_options[long_options_nr-1].val=LONG_OPT;
-              long_options[long_options_nr-1].name=bb_xstrdup(name);
+               long_options[long_options_nr-1].name=xstrdup(name);
        }
        long_options_nr++;
 }
@@ -320,7 +305,7 @@ int getopt_main(int argc, char *argv[])
                        /* For some reason, the original getopt gave no error
                           when there were no arguments. */
                        printf(" --\n");
-                      return 0;
+                       return 0;
                } else
                        bb_error_msg_and_die("missing optstring argument");
        }
@@ -332,7 +317,7 @@ int getopt_main(int argc, char *argv[])
                s=xmalloc(strlen(argv[1])+1);
                strcpy(s,argv[1]+strspn(argv[1],"-+"));
                argv[1]=argv[0];
-              return (generate_output(argv+1,argc-1,s,long_options));
+               return (generate_output(argv+1,argc-1,s,long_options));
        }
 
        while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF)
@@ -341,13 +326,13 @@ int getopt_main(int argc, char *argv[])
                        alternative=1;
                        break;
                case 'o':
-                      optstr = optarg;
+                       optstr = optarg;
                        break;
                case 'l':
                        add_long_options(optarg);
                        break;
                case 'n':
-                      name = optarg;
+                       name = optarg;
                        break;
                case 'q':
                        quiet_errors=1;
@@ -359,7 +344,7 @@ int getopt_main(int argc, char *argv[])
                        set_shell(optarg);
                        break;
                case 'T':
-                      return 4;
+                       return 4;
                case 'u':
                        quote=0;
                        break;
@@ -376,13 +361,5 @@ int getopt_main(int argc, char *argv[])
                argv[optind-1]=name;
        else
                argv[optind-1]=argv[0];
-       return (generate_output(argv+optind-1,argc-optind+1,optstr,long_options));
+       return generate_output(argv+optind-1,argc-optind+1,optstr,long_options);
 }
-
-/*
-  Local Variables:
-  c-file-style: "linux"
-  c-basic-offset: 4
-  tab-width: 4
-  End:
-*/