home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / time / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  2.4 KB  |  94 lines

  1. #ifndef lint
  2. #ifndef NOID
  3. static char    elsieid[] = "@(#)getopt.c    7.5";
  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. extern int    opterr;        /* if error message should be printed */
  34. extern int    optind;     /* index into parent argv vector */
  35. extern int    optopt;        /* character checked for validity */
  36. extern char *    optarg;        /* argument associated with option */
  37.  
  38. #define BADCH    (int)'?'
  39. static char    EMSG[1];
  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. extern char *    strchr();
  51.  
  52. int
  53. getopt(nargc, nargv, ostr)
  54.     int    nargc;
  55.     char    **nargv, *ostr;
  56. {
  57.     static char    *place = EMSG;        /* option letter processing */
  58.     register char    *oli;            /* option letter list index */
  59.  
  60.     if (!*place) {                /* update scanning pointer */
  61.         if (optind >= nargc || *(place = nargv[optind]) != '-' ||
  62.             !*++place)
  63.                 return(EOF);
  64.         if (*place == '-') {        /* found "--" */
  65.             ++optind;
  66.             return(EOF);
  67.         }
  68.     }                    /* option letter okay? */
  69.     if ((optopt = (int)*place++) == (int)':' ||
  70.         !(oli = strchr(ostr, optopt))) {
  71.             if (!*place)
  72.                 ++optind;
  73.             tell(": illegal option -- ");
  74.     }
  75.     if (*++oli != ':') {            /* don't need argument */
  76.         optarg = NULL;
  77.         if (!*place)
  78.             ++optind;
  79.     }
  80.     else {                    /* need an argument */
  81.         if (*place)            /* no white space */
  82.             optarg = place;
  83.         else if (nargc <= ++optind) {    /* no arg */
  84.             place = EMSG;
  85.             tell(": option requires an argument -- ");
  86.         }
  87.          else                /* white space */
  88.             optarg = nargv[optind];
  89.         place = EMSG;
  90.         ++optind;
  91.     }
  92.     return(optopt);                /* dump back option letter */
  93. }
  94.