home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / osi / isode / vmsisode / vmsisode80_tar.Z / vmsisode80_tar / sockit / source / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-25  |  1.0 KB  |  59 lines

  1. #include <stdio.h>
  2.  
  3. extern char    *index();
  4.  
  5. /*
  6.  *    getopt - parse the arguments given.
  7.  *    retrieved from net.sources
  8.  */
  9. int    opterr = 1;
  10. int    optind = 1;
  11. int    optopt;
  12. char    *optarg;
  13.  
  14. #define BADCH    (int)'?'
  15. #define EMSG    ""
  16. #define TELL(s)    fputs(*nargv, stderr); fputs(s, stderr);\
  17.     fputc(optopt, stderr); fputc('\n', stderr);\
  18.     return(BADCH);
  19.  
  20. int getopt(nargc, nargv, ostr)
  21. int    nargc;
  22. char    **nargv;
  23. char    *ostr;
  24. {
  25.   register char    *oli;
  26.   static char    *place = EMSG;
  27.  
  28.   if (!*place) {
  29.     if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
  30.         return(EOF);
  31.     if (*place == '-') {
  32.         ++optind;
  33.         return(EOF);
  34.     }
  35.   }
  36.   if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr, optopt))) {
  37.     if (!*place)
  38.         ++optind;
  39.     TELL(": illegal option -- ");
  40.   }
  41.   if (*++oli != ':') {
  42.     optarg = NULL;
  43.     if (!*place)
  44.         ++optind;
  45.   } else {
  46.     if (*place)
  47.         optarg = place;
  48.     else if (nargc <= ++optind) {
  49.         place = EMSG;
  50.         TELL(": option requires an argument -- ");
  51.     } else
  52.         optarg = nargv[optind];
  53.     place = EMSG;
  54.     ++optind;
  55.   }
  56.   return(optopt);
  57. }
  58.  
  59.