nfs-ganesha 1.4

Getopt.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  * 
00023  * ---------------------------------------
00024  */
00025 
00047 #ifdef HAVE_CONFIG_H
00048 #include "config.h"
00049 #endif
00050 
00051 #include "Getopt.h"
00052 
00053 #include <string.h>
00054 
00055 /* LINTLIBRARY */
00056 
00057 #ifndef NULL
00058 #define NULL    0
00059 #endif
00060 
00061 #define EOF    (-1)
00062 #define ERR(s, c)    if(Opterr){\
00063     extern int write(int, void *, unsigned);\
00064     char errbuf[2];\
00065     errbuf[0] = (char)c; errbuf[1] = '\n';\
00066     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
00067     (void) write(2, s, (unsigned)strlen(s));\
00068     (void) write(2, errbuf, 2);}
00069 
00070 int Opterr = 1;
00071 int Optind = 1;
00072 int Optopt;
00073 char *Optarg;
00074 
00075 int Getopt(int argc, char *argv[], char *opts)
00076 {
00077   static int sp = 1;
00078   register int c;
00079   register char *cp;
00080 
00081   if(sp == 1)
00082     {
00083       if(Optind >= argc || argv[Optind][0] != '-' || argv[Optind][1] == '\0')
00084         return (EOF);
00085       else if(strcmp(argv[Optind], "--") == 0)
00086         {
00087           Optind++;
00088           return (EOF);
00089         }
00090     }
00091   Optopt = c = argv[Optind][sp];
00092   if(c == ':' || (cp = index(opts, c)) == NULL)
00093     {
00094       ERR(": illegal option -- ", c);
00095       if(argv[Optind][++sp] == '\0')
00096         {
00097           Optind++;
00098           sp = 1;
00099         }
00100       return ('?');
00101     }
00102   if(*++cp == ':')
00103     {
00104       if(argv[Optind][sp + 1] != '\0')
00105         Optarg = &argv[Optind++][sp + 1];
00106       else if(++Optind >= argc)
00107         {
00108           ERR(": option requires an argument -- ", c);
00109           sp = 1;
00110           return ('?');
00111         }
00112       else
00113         Optarg = argv[Optind++];
00114       sp = 1;
00115     }
00116   else
00117     {
00118       if(argv[Optind][++sp] == '\0')
00119         {
00120           sp = 1;
00121           Optind++;
00122         }
00123       Optarg = NULL;
00124     }
00125   return (c);
00126 }