remove unused files
[oweals/busybox.git] / util-linux / getopt.c
index 95ecba6e6645dbbfd69ac9eb3fd3af57e88770e2..032d0dc6be5146827ed0ae7174271113783d4e45 100644 (file)
@@ -37,7 +37,7 @@
  *     <misiek@misiek.eu.org>)
  * Ported to Busybox - Alfred M. Szmidt <ams@trillian.itslinux.org>
  *  Removed --version/-V and --help/-h in
- *  Removed prase_error(), using error_msg() from Busybox instead
+ *  Removed parse_error(), using bb_error_msg() from Busybox instead
  *  Replaced our_malloc with xmalloc and our_realloc with xrealloc
  *
  */
@@ -92,17 +92,14 @@ const char *normalize(const char *arg)
         const char *argptr=arg;
         char *bufptr;
 
-        if (BUFFER != NULL)
-                free(BUFFER);
+        free(BUFFER);
 
         if (!quote) { /* Just copy arg */
-                BUFFER=xmalloc(strlen(arg)+1);
-
-                strcpy(BUFFER,arg);
+               BUFFER=bb_xstrdup(arg);
                 return BUFFER;
         }
 
-        /* Each character in arg may take upto four characters in the result:
+        /* Each character in arg may take up to four characters in the result:
            For a quote we need a closing quote, a backslash, a quote and an
            opening quote! We need also the global opening and closing quote,
            and one extra character for '\0'. */
@@ -204,7 +201,6 @@ static const int LONG_OPTIONS_INCR = 10;
 /* Register a long option. The contents of name is copied. */
 void add_longopt(const char *name,int has_arg)
 {
-        char *tmp;
         if (!name) { /* init */
                 free(long_options);
                 long_options=NULL;
@@ -228,9 +224,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;
-                tmp = xmalloc(strlen(name)+1);
-                strcpy(tmp,name);
-                long_options[long_options_nr-1].name=tmp;
+               long_options[long_options_nr-1].name=bb_xstrdup(name);
         }
         long_options_nr++;
 }
@@ -260,7 +254,7 @@ void add_long_options(char *options)
                                         arg_opt=required_argument;
                                 }
                                 if (tlen == 0)
-                                        error_msg("empty long option after -l or --long argument");
+                                        bb_error_msg("empty long option after -l or --long argument");
                         }
                         add_longopt(tokptr,arg_opt);
                 }
@@ -279,12 +273,12 @@ void set_shell(const char *new_shell)
         else if (!strcmp(new_shell,"csh"))
                 shell=TCSH;
         else
-                error_msg("unknown shell after -s or --shell argument");
+                bb_error_msg("unknown shell after -s or --shell argument");
 }
 
 
 /* Exit codes:
- *   0) No errors, succesful operation.
+ *   0) No errors, successful operation.
  *   1) getopt(3) returned an error.
  *   2) A problem with parameter parsing for getopt(1).
  *   3) Internal error, out of memory
@@ -311,8 +305,8 @@ static const char *shortopts="+ao:l:n:qQs:Tu";
 
 int getopt_main(int argc, char *argv[])
 {
-        char *optstr=NULL;
-        char *name=NULL;
+        const char *optstr = NULL;
+        const char *name = NULL;
         int opt;
         int compatible=0;
 
@@ -326,9 +320,9 @@ int getopt_main(int argc, char *argv[])
                         /* For some reason, the original getopt gave no error
                            when there were no arguments. */
                         printf(" --\n");
-                        exit(0);
+                       return 0;
                 } else
-                        error_msg_and_die("missing optstring argument");
+                        bb_error_msg_and_die("missing optstring argument");
         }
 
         if (argv[1][0] != '-' || compatible) {
@@ -336,7 +330,7 @@ int getopt_main(int argc, char *argv[])
                 optstr=xmalloc(strlen(argv[1])+1);
                 strcpy(optstr,argv[1]+strspn(argv[1],"-+"));
                 argv[1]=argv[0];
-                exit(generate_output(argv+1,argc-1,optstr,long_options));
+               return (generate_output(argv+1,argc-1,optstr,long_options));
         }
 
         while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF)
@@ -345,19 +339,15 @@ int getopt_main(int argc, char *argv[])
                         alternative=1;
                         break;
                 case 'o':
-                        if (optstr)
-                                free(optstr);
-                        optstr=xmalloc(strlen(optarg)+1);
-                        strcpy(optstr,optarg);
+                       free(optstr);
+                       optstr = optarg;
                         break;
                 case 'l':
                         add_long_options(optarg);
                         break;
                 case 'n':
-                        if (name)
-                                free(name);
-                        name=xmalloc(strlen(optarg)+1);
-                        strcpy(name,optarg);
+                       free(name);
+                       name = optarg;
                         break;
                 case 'q':
                         quiet_errors=1;
@@ -369,20 +359,19 @@ int getopt_main(int argc, char *argv[])
                         set_shell(optarg);
                         break;
                 case 'T':
-                        exit(4);
+                       return 4;
                 case 'u':
                         quote=0;
                         break;
                 default:
-                        show_usage();
+                        bb_show_usage();
                 }
 
         if (!optstr) {
                 if (optind >= argc)
-                        error_msg_and_die("missing optstring argument");
+                        bb_error_msg_and_die("missing optstring argument");
                 else {
-                        optstr=xmalloc(strlen(argv[optind])+1);
-                        strcpy(optstr,argv[optind]);
+                       optstr=bb_xstrdup(argv[optind]);
                         optind++;
                 }
         }
@@ -390,7 +379,7 @@ int getopt_main(int argc, char *argv[])
                 argv[optind-1]=name;
         else
                 argv[optind-1]=argv[0];
-        exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options));
+       return (generate_output(argv+optind-1,argc-optind+1,optstr,long_options));
 }
 
 /*