home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / comm / mail / smail / src / rcs / getopt.c,v < prev    next >
Text File  |  1993-12-21  |  3KB  |  150 lines

  1. head    1.2;
  2. access;
  3. symbols
  4.     C_1:1.2;
  5. locks; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    93.09.18.16.47.47;    author Aussem;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    93.09.08.16.27.13;    author Aussem;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @get cmdline options
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @insert GNU license text in the header
  28. @
  29. text
  30. @/*
  31.  *  getopt.c
  32.  *
  33.  *  Routines to parse options from argv
  34.  *
  35.  * This program is free software; you can redistribute it and/or
  36.  * modify it under the terms of the GNU General Public License as
  37.  * published by the Free Software Foundation; either version 2 of
  38.  * the License, or (at your option) any later version.
  39.  *
  40.  * This program is distributed in the hope that it will be useful,
  41.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  43.  * General Public License for more details.
  44.  *
  45.  * You should have received a copy of the GNU General Public License
  46.  * along with this program; if not, write to the Free Software
  47.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  48.  *
  49.  * $Log: getopt.c,v $
  50.  * Revision 1.1  1993/09/08  16:27:13  Aussem
  51.  * Initial revision
  52.  *
  53.  *
  54.  */
  55.  
  56. static char     *rcsid="$Id: getopt.c,v 1.1 1993/09/08 16:27:13 Aussem Exp Aussem $";
  57.  
  58. /*
  59.  * Here's something you've all been waiting for:  the AT&T public domain
  60.  * source for getopt(3).  It is the code which was given out at the 1985
  61.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  62.  * directly from AT&T.  The people there assure me that it is indeed
  63.  * in the public domain.
  64.  * 
  65.  * There is no manual page.  That is because the one they gave out at
  66.  * UNIFORUM was slightly different from the current System V Release 2
  67.  * manual page.  The difference apparently involved a note about the
  68.  * famous rules 5 and 6, recommending using white space between an option
  69.  * and its first argument, and not grouping options that have arguments.
  70.  * Getopt itself is currently lenient about both of these things White
  71.  * space is allowed, but not mandatory, and the last option in a group can
  72.  * have an argument.  That particular version of the man page evidently
  73.  * has no official existence, and my source at AT&T did not send a copy.
  74.  * The current SVR2 man page reflects the actual behavor of this getopt.
  75.  * However, I am not about to post a copy of anything licensed by AT&T.
  76.  */
  77.  
  78. #include <stdio.h>
  79. #include "defs.h"
  80.  
  81. #define ERR(s, c)    if(opterr){\
  82.     (void) printf("%s%s%c\n",argv[0],s,c);}
  83.  
  84. int    opterr = 1;
  85. int    optind = 1;
  86. int    optopt;
  87. char    *optarg;
  88.  
  89. int
  90. getopt(argc, argv, opts)
  91. int    argc;
  92. char    **argv, *opts;
  93. {
  94.     static int sp = 1;
  95.     register int c;
  96.     register char *cp;
  97.  
  98.     if(sp == 1)
  99.         if(optind >= argc ||
  100.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  101.             return(EOF);
  102.         else if(strcmp(argv[optind], "--") == NULL) {
  103.             optind++;
  104.             return(EOF);
  105.         }
  106.     optopt = c = argv[optind][sp];
  107.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  108.         ERR(": illegal option -- ", c);
  109.         if(argv[optind][++sp] == '\0') {
  110.             optind++;
  111.             sp = 1;
  112.         }
  113.         return('?');
  114.     }
  115.     if(*++cp == ':') {
  116.         if(argv[optind][sp+1] != '\0')
  117.             optarg = &argv[optind++][sp+1];
  118.         else if(++optind >= argc) {
  119.             ERR(": option requires an argument -- ", c);
  120.             sp = 1;
  121.             return('?');
  122.         } else
  123.             optarg = argv[optind++];
  124.         sp = 1;
  125.     } else {
  126.         if(argv[optind][++sp] == '\0') {
  127.             sp = 1;
  128.             optind++;
  129.         }
  130.         optarg = NULL;
  131.     }
  132.     return(c);
  133. }
  134. @
  135.  
  136.  
  137. 1.1
  138. log
  139. @Initial revision
  140. @
  141. text
  142. @d6 4
  143. a9 1
  144.  * $Log$
  145. d11 14
  146. d27 1
  147. a27 1
  148. static char     *rcsid="$Id$";
  149. @
  150.