home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Python / getopt.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  3KB  |  99 lines

  1. /*---------------------------------------------------------------------------*
  2.  * <RCS keywords>
  3.  *
  4.  * C++ Library
  5.  *
  6.  * Copyright 1992-1994, David Gottner
  7.  *
  8.  *                    All Rights Reserved
  9.  *
  10.  * Permission to use, copy, modify, and distribute this software and its 
  11.  * documentation for any purpose and without fee is hereby granted, 
  12.  * provided that the above copyright notice, this permission notice and
  13.  * the following disclaimer notice appear unmodified in all copies.
  14.  *
  15.  * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL I
  17.  * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
  18.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
  19.  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  20.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Nevertheless, I would like to know about bugs in this library or
  23.  * suggestions for improvment.  Send bug reports and feedback to
  24.  * davegottner@delphi.com.
  25.  *---------------------------------------------------------------------------*/
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. #define bool    int
  31. #ifndef TRUE
  32. #define TRUE    1
  33. #endif
  34. #ifndef FALSE
  35. #define FALSE    0
  36. #endif
  37.  
  38. bool    opterr = TRUE;          /* generate error messages */
  39. int     optind = 1;             /* index into argv array   */
  40. char *  optarg = NULL;          /* optional argument       */
  41.  
  42.  
  43. #ifndef __BEOS__
  44. int getopt(argc,argv,optstring)
  45. int argc; 
  46. char *argv[]; 
  47. char optstring[];
  48. #else
  49. int getopt( int argc, char *const *argv, const char *optstring )
  50. #endif
  51. {
  52.     static   char *opt_ptr = "";
  53.     register char *ptr;
  54.              int   option;
  55.  
  56.     if (*opt_ptr == '\0') {
  57.  
  58.         if (optind >= argc || argv[optind][0] != '-')
  59.             return -1;
  60.  
  61.         else if (strcmp(argv[optind], "--") == 0) {
  62.             ++optind;
  63.             return -1;
  64.         }
  65.  
  66.         opt_ptr = &argv[optind++][1]; 
  67.     }
  68.  
  69.     if ( (option = *opt_ptr++) == '\0')
  70.       return -1;
  71.     
  72.     if ((ptr = strchr(optstring, option)) == NULL) {
  73.         if (opterr)
  74.             fprintf(stderr, "Unknown option: -%c\n", option);
  75.  
  76.         return '?';
  77.     }
  78.  
  79.     if (*(ptr + 1) == ':') {
  80.         if (*opt_ptr != '\0') {
  81.             optarg  = opt_ptr;
  82.             opt_ptr = "";
  83.         }
  84.  
  85.         else {
  86.             if (optind >= argc) {
  87.                 if (opterr)
  88.                     fprintf(stderr,
  89.                 "Argument expected for the -%c option\n", option);
  90.                 return '?';
  91.             }
  92.  
  93.             optarg = argv[optind++];
  94.         }
  95.     }
  96.  
  97.     return option;
  98. }
  99.