home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum16.lzh / SOFTWARE / C / DEVPRC / getopt.c < prev    next >
Text File  |  1991-01-05  |  3KB  |  123 lines

  1. /*
  2.     I got this off net.sources from Henry Spencer.
  3.     It is a public domain getopt(3) like in System V.
  4.     I have made the following modifications:
  5.  
  6.     index(s,c) was added because too many people could
  7.     not compile getopt without it.
  8.  
  9.     A test main program was added, ifdeffed by GETOPT.
  10.     This main program is a public domain implementation
  11.     of the getopt(1) program like in System V.  The getopt
  12.     program can be used to standardize shell option handling.
  13.         e.g.  cc -DGETOPT getopt.c -o getopt
  14. */
  15. #include <stdio.h>
  16.  
  17. /*
  18. #ifdef OSK
  19. #define fputc putc
  20. #endif
  21. */
  22.  
  23. #ifndef lint
  24. static    char    sccsfid[] = "@(#) getopt.c 5.0 (UTZoo) 1985";
  25. #endif
  26.  
  27. #define    ARGCH    (int)':'
  28. #define BADCH     (int)'?'
  29. #define EMSG     ""
  30. #define    ENDARGS  "--"
  31.  
  32. /*
  33.  * get option letter from argument vector
  34.  */
  35. int    opterr = 1,        /* useless, never set or used */
  36.     optind = 1,        /* index into parent argv vector */
  37.     optopt;            /* character checked for validity */
  38. char    *optarg;        /* argument associated with option */
  39.  
  40. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  41.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  42.  
  43.  
  44. getopt(nargc,nargv,ostr)
  45. int    nargc;
  46. char    **nargv,
  47.     *ostr;
  48. {
  49.     static char    *place = EMSG;    /* option letter processing */
  50.     register char    *oli;        /* option letter list index */
  51.     char    *index();
  52.  
  53.     if(!*place) {            /* update scanning pointer */
  54.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  55.         if (*place == '-') {    /* found "--" */
  56.             ++optind;
  57.             return(EOF);
  58.         }
  59.     }                /* option letter okay? */
  60.     if ((optopt = (int)*place++) == ARGCH || !(oli = index(ostr,optopt))) {
  61.         if(!*place) ++optind;
  62.         tell(": illegal option -- ");
  63.     }
  64.     if (*++oli != ARGCH) {        /* don't need argument */
  65.         optarg = NULL;
  66.         if (!*place) ++optind;
  67.     }
  68.     else {                /* need an argument */
  69.         if (*place) optarg = place;    /* no white space */
  70.         else if (nargc <= ++optind) {    /* no arg */
  71.             place = EMSG;
  72.             tell(": option requires an argument -- ");
  73.         }
  74.          else optarg = nargv[optind];    /* white space */
  75.         place = EMSG;
  76.         ++optind;
  77.     }
  78.     return(optopt);            /* dump back option letter */
  79. }
  80.  
  81.  
  82. #ifdef GETOPT
  83.  
  84. #ifndef lint
  85. static    char    sccspid[] = "@(#) getopt.c 5.1 (WangInst) 6/15/85";
  86. #endif
  87.  
  88. main (argc, argv) char **argv;
  89.     {
  90.     char    *optstring = argv[1];
  91.     char    *argv0 = argv[0];
  92.     extern    int     optind;
  93.     extern    char    *optarg;
  94.     int     opterr = 0;
  95.     int     C;
  96.     char    *opi;
  97.     if (argc == 1)
  98.         {
  99.         fprintf (stderr, "Usage: %s optstring args\n", argv0);
  100.         exit (1);
  101.         }
  102.     argv++;
  103.     argc--;
  104.     argv[0] = argv0;
  105.     while ((C = getopt (argc, argv, optstring)) != EOF)
  106.         {
  107.         if (C == BADCH) opterr++;
  108.         printf ("-%c ", C);
  109.         opi = index (optstring, C);
  110.         if (opi && opi[1] == ARGCH)
  111.             if (optarg)
  112.                 printf ("\"%s\" ", optarg);
  113.             else opterr++;
  114.         }
  115.     printf ("%s", ENDARGS);
  116.     while (optind < argc)
  117.         printf (" \"%s\"", argv[optind++]);
  118.     putchar ('\n');
  119.     exit (opterr);
  120.     }
  121.  
  122. #endif
  123.