home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctcoll95.zip / BASTELST / PAPI020.ZIP / GETOPT.C < prev    next >
C/C++ Source or Header  |  1993-10-06  |  1KB  |  60 lines

  1. /*
  2.     getopt.c -- Turbo C
  3.  
  4.     Copyright (c) 1986,87,88 by Borland International Inc.
  5.     All Rights Reserved.
  6. */
  7.  
  8. #include <string.h>
  9. #include <dos.h>
  10.  
  11. int    optind    = 1;    /* index of which argument is next    */
  12. char   *optarg;        /* pointer to argument of current option */
  13.  
  14. static    char   *letP = NULL;    /* remember next option char's location */
  15. #define SW     '-'        /* DOS switch character, either '-' or '/' */
  16.  
  17. int    getopt(int argc, char *argv[], char *optionS)
  18. {
  19.     unsigned char ch;
  20.     char *optP;
  21.  
  22.     if (argc > optind) {
  23.         if (letP == NULL) {
  24.             if ((letP = argv[optind]) == NULL || 
  25.                 *(letP++) != SW)  goto gopEOF;
  26.             if (*letP == SW) {
  27.                 optind++;  goto gopEOF;
  28.             }
  29.         }
  30.         if (0 == (ch = *(letP++))) {
  31.             optind++;  goto gopEOF;
  32.         }
  33.         if (':' == ch  ||  (optP = strchr(optionS, ch)) == NULL)  
  34.             goto gopError;
  35.         if (':' == *(++optP)) {
  36.             optind++;
  37.             if (0 == *letP) {
  38.                 if (argc <= optind)  goto  gopError;
  39.                 letP = argv[optind++];
  40.             }
  41.             optarg = letP;
  42.             letP = NULL;
  43.         } else {
  44.             if (0 == *letP) {
  45.                 optind++;
  46.                 letP = NULL;
  47.             }
  48.             optarg = NULL;
  49.         }
  50.         return ch;
  51.     }
  52. gopEOF:
  53.     optarg = letP = NULL;  
  54.     return -1;
  55.  
  56. gopError:
  57.     optarg = NULL;
  58.     return ('?');
  59. }
  60.