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 /*
00039  * Copyright (c) 1987 Regents of the University of California.
00040  * All rights reserved.
00041  *
00042  * Redistribution and use in source and binary forms are permitted
00043  * provided that: (1) source distributions retain this entire copyright
00044  * notice and comment, and (2) distributions including binaries display
00045  * the following acknowledgement:  ``This product includes software
00046  * developed by the University of California, Berkeley and its contributors''
00047  * in the documentation or other materials provided with the distribution
00048  * and in all advertising materials mentioning features or use of this
00049  * software. Neither the name of the University nor the names of its
00050  * contributors may be used to endorse or promote products derived
00051  * from this software without specific prior written permission.
00052  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00053  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00054  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00055  */
00056 
00057 #ifdef _WINDOWS
00058 
00059 #if defined(LIBC_SCCS) && !defined(lint)
00060 static char sccsid[] = "@(#)getopt.c    4.12 (Berkeley) 6/1/90";
00061 #endif /* LIBC_SCCS and not lint */
00062 
00063 #include <windows.h>
00064 #include <stdio.h>
00065 #include <string.h>
00066 #include "lber.h"
00067 #define index strchr
00068 #define rindex strrchr
00069 
00070 /*
00071  * get option letter from argument vector
00072  */
00073 int     opterr = 1,             /* if error message should be printed */
00074        optind = 1,             /* index into parent argv vector */
00075        optopt;                 /* character checked for validity */
00076 char    *optarg;                /* argument associated with option */
00077 
00078 #define BADCH   (int)'?'
00079 #define EMSG    ""
00080 
00081 int getopt(int nargc, char *const *nargv, const char *ostr)
00082 {
00083        static char *place = EMSG;              /* option letter processing */
00084        register char *oli;                     /* option letter list index */
00085        char *p;
00086 
00087        if (!*place) {                          /* update scanning pointer */
00088               if (optind >= nargc || *(place = nargv[optind]) != '-') {
00089                      place = EMSG;
00090                      return(EOF);
00091               }
00092               if (place[1] && *++place == '-') {      /* found "--" */
00093                      ++optind;
00094                      place = EMSG;
00095                      return(EOF);
00096               }
00097        }                                       /* option letter okay? */
00098        if ((optopt = (int)*place++) == (int)':' ||
00099            !(oli = index(ostr, optopt))) {
00100               /*
00101                * if the user didn't specify '-' as an option,
00102                * assume it means EOF.
00103                */
00104               if (optopt == (int)'-')
00105                      return(EOF);
00106               if (!*place)
00107                      ++optind;
00108               if (opterr) {
00109                      if (!(p = rindex(*nargv, '/')))
00110                             p = *nargv;
00111                      else
00112                             ++p;
00113                      (void)fprintf(stderr, "%s: illegal option -- %c\n",
00114                          p, optopt);
00115               }
00116               return(BADCH);
00117        }
00118        if (*++oli != ':') {                    /* don't need argument */
00119               optarg = NULL;
00120               if (!*place)
00121                      ++optind;
00122        }
00123        else {                                  /* need an argument */
00124               if (*place)                     /* no white space */
00125                      optarg = place;
00126               else if (nargc <= ++optind) {   /* no arg */
00127                      place = EMSG;
00128                      if (!(p = rindex(*nargv, '/')))
00129                             p = *nargv;
00130                      else
00131                             ++p;
00132                      if (opterr)
00133                             (void)fprintf(stderr,
00134                                 "%s: option requires an argument -- %c\n",
00135                                 p, optopt);
00136                      return(BADCH);
00137               }
00138               else                            /* white space */
00139                      optarg = nargv[optind];
00140               place = EMSG;
00141               ++optind;
00142        }
00143        return(optopt);                         /* dump back option letter */
00144 }
00145 
00146 #endif