summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libcruft/getopt_long.c
diff options
context:
space:
mode:
authorGwenolé Beauchesne <gbeauchesne@mandriva.org>2003-06-04 18:44:09 +0000
committerGwenolé Beauchesne <gbeauchesne@mandriva.org>2003-06-04 18:44:09 +0000
commit4cd6a4a5d7e49d54d53dcf4a6f3393d50bd88e8b (patch)
treeacd4001a266a8713495af7f1b2102b61e67113b0 /mdk-stage1/dietlibc/libcruft/getopt_long.c
parent71b111ec6c4671667a19c6fbe0023d33422535d7 (diff)
downloaddrakx-4cd6a4a5d7e49d54d53dcf4a6f3393d50bd88e8b.tar
drakx-4cd6a4a5d7e49d54d53dcf4a6f3393d50bd88e8b.tar.gz
drakx-4cd6a4a5d7e49d54d53dcf4a6f3393d50bd88e8b.tar.bz2
drakx-4cd6a4a5d7e49d54d53dcf4a6f3393d50bd88e8b.tar.xz
drakx-4cd6a4a5d7e49d54d53dcf4a6f3393d50bd88e8b.zip
Import dietlibc 0.22 + other fixes for AMD64
Diffstat (limited to 'mdk-stage1/dietlibc/libcruft/getopt_long.c')
-rw-r--r--mdk-stage1/dietlibc/libcruft/getopt_long.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/mdk-stage1/dietlibc/libcruft/getopt_long.c b/mdk-stage1/dietlibc/libcruft/getopt_long.c
new file mode 100644
index 000000000..713da6579
--- /dev/null
+++ b/mdk-stage1/dietlibc/libcruft/getopt_long.c
@@ -0,0 +1,103 @@
+#include <string.h>
+#include <getopt.h>
+
+static void getopterror(int which) {
+ static char error1[]="Unknown option `-x'.\n";
+ static char error2[]="Missing argument for `-x'.\n";
+ if (opterr) {
+ if (which) {
+ error2[23]=optopt;
+ write(2,error2,28);
+ } else {
+ error1[17]=optopt;
+ write(2,error1,22);
+ }
+ }
+}
+
+int getopt_long(int argc, char * const argv[], const char *optstring,
+ const struct option *longopts, int *longindex) {
+ static int lastidx=0,lastofs=0;
+ char *tmp;
+ if (optind==0) optind=1; /* whoever started setting optind to 0 should be shot */
+again:
+ if (optind>argc || !argv[optind] || *argv[optind]!='-' || argv[optind][1]==0)
+ return -1;
+ if (argv[optind][1]=='-' && argv[optind][2]==0) {
+ ++optind;
+ return -1;
+ }
+ if (argv[optind][1]=='-') { /* long option */
+ char* arg=argv[optind]+2;
+ char* max=strchr(arg,'=');
+ const struct option* o;
+ if (!max) max=arg+strlen(arg);
+ for (o=longopts; o->name; ++o) {
+ if (!strncmp(o->name,arg,(size_t)(max-arg))) { /* match */
+ if (longindex) *longindex=o-longopts;
+ if (o->has_arg>0) {
+ if (*max=='=')
+ optarg=max+1;
+ else {
+ optarg=argv[optind+1];
+ if (!optarg && o->has_arg==1) { /* no argument there */
+ if (*optstring==':') return ':';
+ write(2,"argument required: `",20);
+ write(2,arg,(size_t)(max-arg));
+ write(2,"'.\n",3);
+ ++optind;
+ return '?';
+ }
+ ++optind;
+ }
+ }
+ ++optind;
+ if (o->flag)
+ *(o->flag)=o->val;
+ else
+ return o->val;
+ return 0;
+ }
+ }
+ if (*optstring==':') return ':';
+ write(2,"invalid option `",16);
+ write(2,arg,(size_t)(max-arg));
+ write(2,"'.\n",3);
+ ++optind;
+ return '?';
+ }
+ if (lastidx!=optind) {
+ lastidx=optind; lastofs=0;
+ }
+ optopt=argv[optind][lastofs+1];
+ if ((tmp=strchr(optstring,optopt))) {
+ if (*tmp==0) { /* apparently, we looked for \0, i.e. end of argument */
+ ++optind;
+ goto again;
+ }
+ if (tmp[1]==':') { /* argument expected */
+ if (tmp[2]==':' || argv[optind][lastofs+2]) { /* "-foo", return "oo" as optarg */
+ if (!*(optarg=argv[optind]+lastofs+2)) optarg=0;
+ goto found;
+ }
+ optarg=argv[optind+1];
+ if (!optarg) { /* missing argument */
+ ++optind;
+ if (*optstring==':') return ':';
+ getopterror(1);
+ return ':';
+ }
+ ++optind;
+ } else {
+ ++lastofs;
+ return optopt;
+ }
+found:
+ ++optind;
+ return optopt;
+ } else { /* not found */
+ getopterror(0);
+ ++optind;
+ return '?';
+ }
+}