home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / graphics / conversion / giftrans / getopt.c next >
Encoding:
C/C++ Source or Header  |  1997-11-20  |  2.4 KB  |  84 lines

  1. /*
  2.  * Copyright (c) 1987 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef ST
  19. #if defined(LIBC_SCCS) && !defined(lint)
  20. static char sccsid[] = "@(#)getopt.c    4.7 (Berkeley) 6/27/88";
  21. #endif /* LIBC_SCCS and not lint */
  22. #endif
  23.  
  24. /*
  25.  * get option letter from argument vector
  26.  */
  27. int    opterr = 1,        /* if error message should be printed */
  28.     optind = 1,        /* index into parent argv vector */
  29.     optopt;         /* character checked for validity */
  30. char    *optarg;        /* argument associated with option */
  31.  
  32. #define BADCH    (int)'?'
  33. #define EMSG    ""
  34. #define tell(s) { \
  35.     if (opterr) { \
  36.         fputs(*nargv, stderr); \
  37.         fputs(s, stderr); \
  38.         fputc(optopt, stderr); \
  39.         fputc((int)'\n', stderr); \
  40.     } \
  41.     return(BADCH); \
  42. }
  43.  
  44. getopt(nargc, nargv, ostr)
  45.     int nargc;
  46.     char **nargv, *ostr;
  47. {
  48.     static char *place = EMSG;        /* option letter processing */
  49.     register char *oli;            /* option letter list index */
  50.  
  51.     if (!*place) {                /* update scanning pointer */
  52.         if (optind >= nargc || *(place = nargv[optind]) != '-')
  53.             return(EOF);
  54.         if (place[1] && *++place == '-') {      /* found "--" */
  55.             ++optind;
  56.             return(EOF);
  57.         }
  58.     }                    /* option letter okay? */
  59.     if ((optopt = (int)*place++) == (int)':' ||
  60.         !(oli = strchr(ostr, optopt))) {
  61.         if (!*place)
  62.             ++optind;
  63.         tell(": illegal option -- ");
  64.     }
  65.     if (*++oli != ':') {                    /* don't need argument */
  66.         optarg = NULL;
  67.         if (!*place)
  68.             ++optind;
  69.     }
  70.     else {                    /* need an argument */
  71.         if (*place)            /* no white space */
  72.             optarg = place;
  73.         else if (nargc <= ++optind) {    /* no arg */
  74.             place = EMSG;
  75.             tell(": option requires an argument -- ");
  76.         }
  77.         else                /* white space */
  78.             optarg = nargv[optind];
  79.         place = EMSG;
  80.         ++optind;
  81.     }
  82.     return(optopt);             /* dump back option letter */
  83. }
  84.