Back to index

lightning-sunbird  0.9+nobinonly
getopt.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  * Copyright (c) 1987 Regents of the University of California.
00039  * All rights reserved.
00040  *
00041  * Redistribution and use in source and binary forms are permitted
00042  * provided that: (1) source distributions retain this entire copyright
00043  * notice and comment, and (2) distributions including binaries display
00044  * the following acknowledgement:  ``This product includes software
00045  * developed by the University of California, Berkeley and its contributors''
00046  * in the documentation or other materials provided with the distribution
00047  * and in all advertising materials mentioning features or use of this
00048  * software. Neither the name of the University nor the names of its
00049  * contributors may be used to endorse or promote products derived
00050  * from this software without specific prior written permission.
00051  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00052  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00053  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00054  */
00055 
00056 #if defined(LIBC_SCCS) && !defined(lint)
00057 static char sccsid[] = "@(#)getopt.c    4.12 (Berkeley) 6/1/90";
00058 #endif /* LIBC_SCCS and not lint */
00059 
00060 #include <stdio.h>
00061 #include <string.h>
00062 #include "ldap-macos.h"
00063 #define index strchr
00064 #define rindex strrchr
00065 
00066 /*
00067  * get option letter from argument vector
00068  */
00069 int     opterr = 1,             /* if error message should be printed */
00070        optind = 1,             /* index into parent argv vector */
00071        optopt;                 /* character checked for validity */
00072 char    *optarg;                /* argument associated with option */
00073 
00074 #define BADCH   (int)'?'
00075 #define EMSG    ""
00076 
00077 getopt(int nargc, char **nargv, char *ostr)
00078 {
00079        static char *place = EMSG;              /* option letter processing */
00080        register char *oli;                     /* option letter list index */
00081        char *p;
00082 
00083        if (!*place) {                          /* update scanning pointer */
00084               if (optind >= nargc || *(place = nargv[optind]) != '-') {
00085                      place = EMSG;
00086                      return(EOF);
00087               }
00088               if (place[1] && *++place == '-') {      /* found "--" */
00089                      ++optind;
00090                      place = EMSG;
00091                      return(EOF);
00092               }
00093        }                                       /* option letter okay? */
00094        if ((optopt = (int)*place++) == (int)':' ||
00095            !(oli = index(ostr, optopt))) {
00096               /*
00097                * if the user didn't specify '-' as an option,
00098                * assume it means EOF.
00099                */
00100               if (optopt == (int)'-')
00101                      return(EOF);
00102               if (!*place)
00103                      ++optind;
00104               if (opterr) {
00105                      if (!(p = rindex(*nargv, '/')))
00106                             p = *nargv;
00107                      else
00108                             ++p;
00109                      (void)fprintf(stderr, "%s: illegal option -- %c\n",
00110                          p, optopt);
00111               }
00112               return(BADCH);
00113        }
00114        if (*++oli != ':') {                    /* don't need argument */
00115               optarg = NULL;
00116               if (!*place)
00117                      ++optind;
00118        }
00119        else {                                  /* need an argument */
00120               if (*place)                     /* no white space */
00121                      optarg = place;
00122               else if (nargc <= ++optind) {   /* no arg */
00123                      place = EMSG;
00124                      if (!(p = rindex(*nargv, '/')))
00125                             p = *nargv;
00126                      else
00127                             ++p;
00128                      if (opterr)
00129                             (void)fprintf(stderr,
00130                                 "%s: option requires an argument -- %c\n",
00131                                 p, optopt);
00132                      return(BADCH);
00133               }
00134               else                            /* white space */
00135                      optarg = nargv[optind];
00136               place = EMSG;
00137               ++optind;
00138        }
00139        return(optopt);                         /* dump back option letter */
00140 }