+/* 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;
/* 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);
free(BUFFER);
if (!quote) { /* Just copy arg */
- BUFFER=bb_xstrdup(arg);
+ BUFFER=xstrdup(arg);
return BUFFER;
}
* 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;
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;
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 {
charptr = strchr(optstr,opt);
if (charptr != NULL && *++charptr == ':')
printf(" %s",
- normalize(optarg?optarg:""));
+ normalize(optarg?optarg:""));
}
}
printf(" --");
while (optind < argc)
printf(" %s",normalize(argv[optind++]));
- printf("\n");
+ puts("");
}
return exit_code;
}
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);
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++;
}
/* 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");
}
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)
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;
set_shell(optarg);
break;
case 'T':
- return 4;
+ return 4;
case 'u':
quote=0;
break;
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:
-*/