home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / lib / librpc / etc / getopt.c next >
Encoding:
C/C++ Source or Header  |  1989-07-11  |  1.3 KB  |  76 lines

  1. /* @(#)getopt.c    2.1 88/08/01 4.0 RPCSRC */
  2.  
  3. /* this is a public domain version of getopt */
  4.  
  5. /*LINTLIBRARY*/
  6. #ifndef NULL
  7. #define NULL    0
  8. #endif NULL
  9. #ifndef EOF
  10. #define EOF    (-1)
  11. #endif EOF
  12.  
  13. #define ERR(s, c)    if(opterr){\
  14.     extern int strlen(), write();\
  15.     char errbuf[2];\
  16.     errbuf[0] = c; errbuf[1] = '\n';\
  17.     (void) write(2, argv[0], strlen(argv[0]));\
  18.     (void) write(2, s, strlen(s));\
  19.     (void) write(2, errbuf, 2);}
  20.  
  21. #define strchr index
  22.  
  23. extern int strcmp();
  24. extern char *strchr();
  25.  
  26. int    opterr = 1;
  27. int    optind = 1;
  28. int    optopt;
  29. char    *optarg;
  30.  
  31. int
  32. getopt(argc, argv, opts)
  33. int    argc;
  34. char    **argv, *opts;
  35. {
  36.     static int sp = 1;
  37.     register int c;
  38.     register char *cp;
  39.  
  40.     if(sp == 1)
  41.         if(optind >= argc ||
  42.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  43.             return(EOF);
  44.         else if(strcmp(argv[optind], "--") == NULL) {
  45.             optind++;
  46.             return(EOF);
  47.         }
  48.     optopt = c = argv[optind][sp];
  49.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  50.         ERR(": unknown option, -", c);
  51.         if(argv[optind][++sp] == '\0') {
  52.             optind++;
  53.             sp = 1;
  54.         }
  55.         return('?');
  56.     }
  57.     if(*++cp == ':') {
  58.         if(argv[optind][sp+1] != '\0')
  59.             optarg = &argv[optind++][sp+1];
  60.         else if(++optind >= argc) {
  61.             ERR(": argument missing for -", c);
  62.             sp = 1;
  63.             return('?');
  64.         } else
  65.             optarg = argv[optind++];
  66.         sp = 1;
  67.     } else {
  68.         if(argv[optind][++sp] == '\0') {
  69.             sp = 1;
  70.             optind++;
  71.         }
  72.         optarg = NULL;
  73.     }
  74.     return(c);
  75. }
  76.