Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:44:47 UTC

view on githubraw file Latest commit ec6cf3b0 on 2003-08-26 20:45:25 UTC
ec6cf3b09d Ed H*0001 /* (C) Copyright 1993,1994 by Carnegie Mellon University
                0002  * All Rights Reserved.
                0003  *
                0004  * Permission to use, copy, modify, distribute, and sell this software
                0005  * and its documentation for any purpose is hereby granted without
                0006  * fee, provided that the above copyright notice appear in all copies
                0007  * and that both that copyright notice and this permission notice
                0008  * appear in supporting documentation, and that the name of Carnegie
                0009  * Mellon University not be used in advertising or publicity
                0010  * pertaining to distribution of the software without specific,
                0011  * written prior permission.  Carnegie Mellon University makes no
                0012  * representations about the suitability of this software for any
                0013  * purpose.  It is provided "as is" without express or implied
                0014  * warranty.
                0015  *
                0016  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
                0017  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                0018  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
                0019  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                0020  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
                0021  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                0022  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                0023  * SOFTWARE.
                0024  */
                0025 #include <stdio.h>
                0026 
                0027 #ifdef __OS2__ 
                0028 # include <io.h>
                0029 #endif
                0030 
                0031 #if defined(__MSDOS__) || defined(__OS2__)
                0032 #define ERR(s, c)                   \
                0033     if (opterr) {                   \
                0034     char buff[3];                   \
                0035     buff[0] = c; buff[1] = '\r'; buff[2] = '\n';    \
                0036     (void)write(2, av[0], strlen(av[0]));       \
                0037     (void)write(2, s, strlen(s));           \
                0038     (void)write(2, buff, 3);            \
                0039     }
                0040 #else /* __MSDOS__ */
                0041 #define ERR(s, c)                   \
                0042     if (opterr) {                   \
                0043     char buff[2];                   \
                0044     buff[0] = c; buff[1] = '\n';            \
                0045     (void)write(2, av[0], strlen(av[0]));       \
                0046     (void)write(2, s, strlen(s));           \
                0047     (void)write(2, buff, 2);            \
                0048     }
                0049 #endif
                0050 
                0051 int opterr = 1;
                0052 int optind = 1;
                0053 int optopt;
                0054 char    *optarg;
                0055 
                0056 
                0057 /*
                0058 **  Return options and their values from the command line.
                0059 **  This comes from the AT&T public-domain getopt published in mod.sources
                0060 **  (i.e., comp.sources.unix before the great Usenet renaming).
                0061 */
                0062 int
                0063 getopt(int ac, char **av, char *opts)
                0064 {
                0065     extern char *strchr(const char *, int);
                0066     static int  i = 1;
                0067     char    *p;
                0068 
                0069     /* Move to next value from argv? */
                0070     if (i == 1) {
                0071     if (optind >= ac ||
                0072 #if defined(__MSDOS__) || defined(__OS2__)
                0073         (av[optind][0] != '-' && av[optind][0] != '/')
                0074 #else
                0075         av[optind][0] != '-'
                0076 #endif
                0077         || av[optind][1] == '\0')
                0078         return EOF;
                0079     if (strcmp(av[optind], "--") == 0) {
                0080         optind++;
                0081         return EOF;
                0082     }
                0083     }
                0084 
                0085     /* Get next option character. */
                0086     if ((optopt = av[optind][i]) == ':'
                0087      || (p = strchr(opts,  optopt)) == NULL) {
                0088     ERR(": illegal option -- ", optopt);
                0089     if (av[optind][++i] == '\0') {
                0090         optind++;
                0091         i = 1;
                0092     }
                0093     return '?';
                0094     }
                0095 
                0096     /* Snarf argument? */
                0097     if (*++p == ':') {
                0098     if (av[optind][i + 1] != '\0')
                0099         optarg = &av[optind++][i + 1];
                0100     else {
                0101         if (++optind >= ac) {
                0102         ERR(": option requires an argument -- ", optopt);
                0103         i = 1;
                0104         return '?';
                0105         }
                0106         optarg = av[optind++];
                0107     }
                0108     i = 1;
                0109     }
                0110     else {
                0111     if (av[optind][++i] == '\0') {
                0112         i = 1;
                0113         optind++;
                0114     }
                0115     optarg = NULL;
                0116     }
                0117 
                0118     return optopt;
                0119 }