home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / thesrc15.zip / getopt.c < prev    next >
C/C++ Source or Header  |  1992-10-14  |  3KB  |  109 lines

  1. /* This is file GETOPT.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1987 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted
  12.  * provided that: (1) source distributions retain this entire copyright
  13.  * notice and comment, and (2) distributions including binaries display
  14.  * the following acknowledgement:  ``This product includes software
  15.  * developed by the University of California, Berkeley and its contributors''
  16.  * in the documentation or other materials provided with the distribution
  17.  * and in all advertising materials mentioning features or use of this
  18.  * software. Neither the name of the University nor the names of its
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  */
  25.  
  26. #if defined(LIBC_SCCS) && !defined(lint)
  27. static char sccsid[] = "@(#)getopt.c    4.12 (Berkeley) 6/1/90";
  28. #endif /* LIBC_SCCS and not lint */
  29.  
  30. #include <stdio.h>
  31.  
  32. /*
  33.  * get option letter from argument vector
  34.  */
  35. int    opterr = 1,        /* if error message should be printed */
  36.     optind = 1,        /* index into parent argv vector */
  37.     optopt;            /* character checked for validity */
  38. char    *optarg;        /* argument associated with option */
  39.  
  40. #define    BADCH    (int)'?'
  41. #define    EMSG    ""
  42.  
  43. getopt(nargc, nargv, ostr)
  44.     int nargc;
  45.     char **nargv, *ostr;
  46. {
  47.     static char *place = EMSG;        /* option letter processing */
  48.     register char *oli;            /* option letter list index */
  49.     char *p, *strchr(), *strrchr();
  50.  
  51.     if (!*place) {                /* update scanning pointer */
  52.         if (optind >= nargc || *(place = nargv[optind]) != '-') {
  53.             place = EMSG;
  54.             return(EOF);
  55.         }
  56.         if (place[1] && *++place == '-') {    /* found "--" */
  57.             ++optind;
  58.             place = EMSG;
  59.             return(EOF);
  60.         }
  61.     }                    /* option letter okay? */
  62.     if ((optopt = (int)*place++) == (int)':' ||
  63.         !(oli = strchr(ostr, optopt))) {
  64.         /*
  65.          * if the user didn't specify '-' as an option,
  66.          * assume it means EOF.
  67.          */
  68.         if (optopt == (int)'-')
  69.             return(EOF);
  70.         if (!*place)
  71.             ++optind;
  72.         if (opterr) {
  73.             if (!(p = strrchr(*nargv, '/')))
  74.                 p = *nargv;
  75.             else
  76.                 ++p;
  77.             (void)fprintf(stderr, "%s: illegal option -- %c\n",
  78.                 p, optopt);
  79.         }
  80.         return(BADCH);
  81.     }
  82.     if (*++oli != ':') {            /* don't need argument */
  83.         optarg = NULL;
  84.         if (!*place)
  85.             ++optind;
  86.     }
  87.     else {                    /* need an argument */
  88.         if (*place)            /* no white space */
  89.             optarg = place;
  90.         else if (nargc <= ++optind) {    /* no arg */
  91.             place = EMSG;
  92.             if (!(p = strrchr(*nargv, '/')))
  93.                 p = *nargv;
  94.             else
  95.                 ++p;
  96.             if (opterr)
  97.                 (void)fprintf(stderr,
  98.                     "%s: option requires an argument -- %c\n",
  99.                     p, optopt);
  100.             return(BADCH);
  101.         }
  102.          else                /* white space */
  103.             optarg = nargv[optind];
  104.         place = EMSG;
  105.         ++optind;
  106.     }
  107.     return(optopt);                /* dump back option letter */
  108. }
  109.