1 /*
2    getopt_long.c - implementation of getopt_long() for systems that lack it
3 
4    Copyright (C) 2001, 2002 Arthur de Jong
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 
25 #include "getopt_long.h"
26 
27 
28 /* this is a (poor) getopt_long() replacement for systems that don't have it
29    (getopt_long() is generaly a GNU extention)
30    this implementation is by no meens flawless, especialy the optional arguments
31    to options and options following filenames is not quite right, allso
32    minimal error checking is provided
33    */
getopt_long(int argc,char * const argv[],const char * optstring,const struct option * longopts,int * longindex)34 int getopt_long(int argc,char * const argv[],
35                 const char *optstring,
36                 const struct option *longopts,int *longindex)
37 {
38   int i;   /* for looping through options */
39   int l;   /* for length */
40 
41   /* first check if there realy is a -- option */
42   if ( (optind>0)&&(optind<argc) &&
43        (strncmp(argv[optind],"--",2)==0) &&
44        (argv[optind][2]!='\0') )
45   {
46     /* check the longopts list for a valid option */
47     for (i=0;longopts[i].name!=NULL;i++)
48     {
49       /* save the length for later */
50       l=strlen(longopts[i].name);
51       if (strncmp(argv[optind]+2,longopts[i].name,l)==0)
52       {
53         /* we have a match */
54         if ( (longopts[i].has_arg==no_argument) &&
55              (argv[optind][2+l]=='\0') )
56         {
57           optind++;
58           return longopts[i].val;
59         }
60         else if ( (longopts[i].has_arg==required_argument) &&
61                   (argv[optind][2+l]=='=') )
62         {
63           optarg=argv[optind]+3+l;
64           optind++;
65           return longopts[i].val;
66         }
67         else if ( (longopts[i].has_arg==required_argument) &&
68                   (argv[optind][2+l]=='\0') )
69         {
70           optarg=argv[optind+1];
71           optind+=2;
72           return longopts[i].val;
73         }
74         else if ( (longopts[i].has_arg==optional_argument) &&
75                   (argv[optind][2+l]=='=') )
76         {
77           optarg=argv[optind]+3+l;
78           optind++;
79           return longopts[i].val;
80         }
81         else if ( (longopts[i].has_arg==optional_argument) &&
82                   (argv[optind][2+l]=='\0') )
83         {
84           optind++;
85           return longopts[i].val;
86         }
87       }
88     }
89   }
90   /* if all else fails use plain getopt() */
91   return getopt(argc,argv,optstring);
92 }
93