home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.10 / util-lin / util-linux-1.10 / time / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-30  |  2.4 KB  |  93 lines

  1. #ifndef lint
  2. #ifndef NOID
  3. static char    elsieid[] = "@(#)getopt.c    7.3";
  4. /* Modified from the UCB version with the SCCS ID appearing below. */
  5. #endif /* !defined NOID */
  6. #endif /* !defined lint */
  7.  
  8. /*LINTLIBRARY*/
  9.  
  10. /*
  11.  * Copyright (c) 1987 Regents of the University of California.
  12.  * All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted
  15.  * provided that this notice is preserved and that due credit is given
  16.  * to the University of California at Berkeley. The name of the University
  17.  * may not be used to endorse or promote products derived from this
  18.  * software without specific written prior permission. This software
  19.  * is provided ``as is'' without express or implied warranty.
  20.  */
  21.  
  22. #ifdef LIBC_SCCS
  23. #ifndef lint
  24. static char sccsid[] = "@(#)getopt.c    4.5 (Berkeley) 11/24/87";
  25. #endif /* !defined lint */
  26. #endif /* defined LIBC_SCCS */
  27.  
  28. #include <stdio.h>
  29.  
  30. /*
  31.  * get option letter from argument vector
  32.  */
  33. int    opterr = 1,        /* if error message should be printed */
  34.     optind = 1,        /* index into parent argv vector */
  35.     optopt;            /* character checked for validity */
  36. char    *optarg;        /* argument associated with option */
  37.  
  38. #define BADCH    (int)'?'
  39. #define EMSG    ""
  40. #define tell(s)    { \
  41.     if (opterr) { \
  42.         (void) fputs(*nargv, stderr); \
  43.         (void) fputs(s, stderr); \
  44.         (void) fputc(optopt, stderr); \
  45.         (void) fputc((int)'\n', stderr); \
  46.     } \
  47.     return(BADCH); \
  48. }
  49.  
  50. int
  51. getopt(nargc, nargv, ostr)
  52.     int    nargc;
  53.     char    **nargv, *ostr;
  54. {
  55.     static char    *place = EMSG;        /* option letter processing */
  56.     register char    *oli;            /* option letter list index */
  57.     char    *strchr();
  58.  
  59.     if (!*place) {                /* update scanning pointer */
  60.         if (optind >= nargc || *(place = nargv[optind]) != '-' ||
  61.             !*++place)
  62.                 return(EOF);
  63.         if (*place == '-') {        /* found "--" */
  64.             ++optind;
  65.             return(EOF);
  66.         }
  67.     }                    /* option letter okay? */
  68.     if ((optopt = (int)*place++) == (int)':' ||
  69.         !(oli = strchr(ostr, optopt))) {
  70.             if (!*place)
  71.                 ++optind;
  72.             tell(": illegal option -- ");
  73.     }
  74.     if (*++oli != ':') {            /* don't need argument */
  75.         optarg = NULL;
  76.         if (!*place)
  77.             ++optind;
  78.     }
  79.     else {                    /* need an argument */
  80.         if (*place)            /* no white space */
  81.             optarg = place;
  82.         else if (nargc <= ++optind) {    /* no arg */
  83.             place = EMSG;
  84.             tell(": option requires an argument -- ");
  85.         }
  86.          else                /* white space */
  87.             optarg = nargv[optind];
  88.         place = EMSG;
  89.         ++optind;
  90.     }
  91.     return(optopt);                /* dump back option letter */
  92. }
  93.