home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / misc / sci / units / source / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-28  |  2.0 KB  |  78 lines

  1. /* optarg - parse command-line arguments */
  2. /* Author: AT&T */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <strings.h>
  7.  
  8. #define ERR(s, c)                if(opterr){\
  9.     char errbuf[2];\
  10.     errbuf[0] = c; errbuf[1] = '\n';\
  11.     fwrite(argv[0], 1, (unsigned)strlen(argv[0]),stderr);\
  12.     fwrite(s, 1, (unsigned)strlen(s),stderr);\
  13.     fwrite(errbuf, 1, 2, stderr);}
  14.  
  15. extern int opterr;
  16.  
  17. /* prototypes as desired by SAS/C 6.51 for Amiga (Ron Charlton) */
  18. #include "getopt_protos.h"
  19. #include "units_protos.h"
  20.  
  21. int    opterr = 1;        /* getopt prints errors if this is on */
  22. int    optind = 1;        /* token pointer */
  23. int    optopt;            /* option character passed back to user */
  24. char    *optarg;        /* flag argument (or value) */
  25.  
  26. int    /* return option character, EOF if no more or ? if problem */
  27. getopt(argc, argv, opts)
  28. int    argc;
  29. char    **argv;
  30. char    *opts;                /* option string */
  31. {
  32.     static int sp = 1;        /* character index in current token */
  33.     register char *cp;        /* pointer into current token */
  34.  
  35.     if(sp == 1)
  36.         /* check for more flag-like tokens */
  37.         if(optind >= argc ||
  38.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  39.             return(EOF);
  40.         else if(strcmp(argv[optind], "--") == 0) {
  41.             optind++;
  42.             return(EOF);
  43.         }
  44.     optopt = argv[optind][sp];
  45.     if(optopt == ':' || (cp=strchr(opts, optopt)) == 0) {
  46.         ERR(": illegal option -- ", optopt);
  47.         /* if no chars left in this token, move to next token */
  48.         if(argv[optind][++sp] == '\0') {
  49.             optind++;
  50.             sp = 1;
  51.         }
  52.         return('?');
  53.     }
  54.  
  55.     if(*++cp == ':') {    /* if a value is expected, get it */
  56.         if(argv[optind][sp+1] != '\0')
  57.             /* flag value is rest of current token */
  58.             optarg = &argv[optind++][sp+1];
  59.         else if(++optind >= argc) {
  60.             ERR(": option requires an argument -- ", optopt);
  61.             sp = 1;
  62.             return('?');
  63.         } else
  64.             /* flag value is next token */
  65.             optarg = argv[optind++];
  66.         sp = 1;
  67.     } else {
  68.         /* set up to look at next char in token, next time */
  69.         if(argv[optind][++sp] == '\0') {
  70.             /* no more in current token, so setup next token */
  71.             sp = 1;
  72.             optind++;
  73.         }
  74.         optarg = 0;
  75.     }
  76.     return(optopt);/* return the current flag character found */
  77. }
  78.