home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / delivery / deliver.tz / deliver / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-24  |  1.7 KB  |  101 lines

  1. /* $Header: getopt.c,v 2.1 89/06/09 12:25:28 network Exp $
  2.  *
  3.  * A version of the public-domain getopt() function, as found
  4.  * in the SVID and fine Unix manuals everywhere.
  5.  *
  6.  * $Log:    getopt.c,v $
  7.  * Revision 2.1  89/06/09  12:25:28  network
  8.  * Update RCS revisions.
  9.  * 
  10.  * Revision 1.4  89/06/09  12:23:50  network
  11.  * Baseline for 2.0 release.
  12.  * 
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "config.h"
  17. #include "misc.h"
  18.  
  19. /*----------------------------------------------------------------------
  20.  * Get command line options.
  21.  * This is essentially the public domain version, just reformatted to
  22.  * match the rest of the deliver program.
  23.  */
  24.  
  25. #ifndef HAS_GETOPT
  26.  
  27. int     opterr = 1;
  28. int     optind = 1;
  29. int     optopt = 0;
  30. char    *optarg = NULL;
  31.  
  32. #define ERR(what,c) \
  33.     if (!opterr) {} else fprintf(stderr,"%s: %s -- %c\n", argv[0], what, c);
  34.  
  35. int
  36. getopt(argc, argv, opts)
  37. int argc;
  38. char **argv;
  39. char *opts;
  40. {
  41.     static int sp = 1;
  42.     int c;
  43.     char *cp;
  44.  
  45.     if (sp == 1)
  46.     {
  47.         if (optind >= argc
  48.          || argv[optind][0] != '-' || argv[optind][1] == '\0')
  49.             return EOF;
  50.  
  51.         if (strcmp(argv[optind], "--") == NULL)
  52.         {
  53.             optind++;
  54.             return EOF;
  55.         }
  56.     }
  57.  
  58.     optopt = c = argv[optind][sp];
  59.  
  60.     if (c == ':' || (cp = strchr(opts, c)) == NULL)
  61.     {
  62.         ERR("illegal option", c);
  63.         if (argv[optind][++sp] == '\0')
  64.         {
  65.             optind++;
  66.             sp = 1;
  67.         }
  68.         return '?';
  69.     }
  70.  
  71.     if (*++cp == ':')
  72.     {
  73.         if (argv[optind][sp + 1] != '\0')
  74.             optarg = &argv[optind++][sp + 1];
  75.         else if (++optind >= argc)
  76.         {
  77.             ERR("option requires an argument", c);
  78.             sp = 1;
  79.             return '?';
  80.         }
  81.         else
  82.             optarg = argv[optind++];
  83.  
  84.         sp = 1;
  85.     }
  86.     else
  87.     {
  88.         if (argv[optind][++sp] == '\0')
  89.         {
  90.             sp = 1;
  91.             optind++;
  92.         }
  93.  
  94.         optarg = NULL;
  95.     }
  96.  
  97.     return c;
  98. }
  99.  
  100. #endif  /* !HAS_GETOPT */
  101.