home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / time14.zip / time-1.4 / getopt1.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  3KB  |  154 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 88, 89, 90, 91, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify it
  5.    under the terms of the GNU General Public License as published by the
  6.    Free Software Foundation; either version 2, or (at your option) any
  7.    later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "getopt.h"
  19.  
  20. #ifndef __STDC__
  21. #define const
  22. #endif
  23.  
  24. #if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__) || defined (LIBC)
  25. #include <stdlib.h>
  26. #else /* STDC_HEADERS or __GNU_LIBRARY__ */
  27. char *getenv ();
  28. #endif /* STDC_HEADERS or __GNU_LIBRARY__ */
  29.  
  30. #ifndef    NULL
  31. #define NULL 0
  32. #endif
  33.  
  34. int
  35. getopt_long (argc, argv, options, long_options, opt_index)
  36.      int argc;
  37.      char *const *argv;
  38.      const char *options;
  39.      const struct option *long_options;
  40.      int *opt_index;
  41. {
  42.   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  43. }
  44.  
  45. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  46.    If an option that starts with '-' (not '--') doesn't match a long option,
  47.    but does match a short option, it is parsed as a short option
  48.    instead.  */
  49.  
  50. int 
  51. getopt_long_only (argc, argv, options, long_options, opt_index)
  52.      int argc;
  53.      char *const *argv;
  54.      const char *options;
  55.      const struct option *long_options;
  56.      int *opt_index;
  57. {
  58.   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  59. }
  60.  
  61. #ifdef TEST
  62.  
  63. #include <stdio.h>
  64.  
  65. int
  66. main (argc, argv)
  67.      int argc;
  68.      char **argv;
  69. {
  70.   int c;
  71.   int digit_optind = 0;
  72.  
  73.   while (1)
  74.     {
  75.       int this_option_optind = optind ? optind : 1;
  76.       int option_index = 0;
  77.       static struct option long_options[] =
  78.       {
  79.     {"add", 1, 0, 0},
  80.     {"append", 0, 0, 0},
  81.     {"delete", 1, 0, 0},
  82.     {"verbose", 0, 0, 0},
  83.     {"create", 0, 0, 0},
  84.     {"file", 1, 0, 0},
  85.     {0, 0, 0, 0}
  86.       };
  87.  
  88.       c = getopt_long (argc, argv, "abc:d:0123456789",
  89.                long_options, &option_index);
  90.       if (c == EOF)
  91.     break;
  92.  
  93.       switch (c)
  94.     {
  95.     case 0:
  96.       printf ("option %s", long_options[option_index].name);
  97.       if (optarg)
  98.         printf (" with arg %s", optarg);
  99.       printf ("\n");
  100.       break;
  101.  
  102.     case '0':
  103.     case '1':
  104.     case '2':
  105.     case '3':
  106.     case '4':
  107.     case '5':
  108.     case '6':
  109.     case '7':
  110.     case '8':
  111.     case '9':
  112.       if (digit_optind != 0 && digit_optind != this_option_optind)
  113.         printf ("digits occur in two different argv-elements.\n");
  114.       digit_optind = this_option_optind;
  115.       printf ("option %c\n", c);
  116.       break;
  117.  
  118.     case 'a':
  119.       printf ("option a\n");
  120.       break;
  121.  
  122.     case 'b':
  123.       printf ("option b\n");
  124.       break;
  125.  
  126.     case 'c':
  127.       printf ("option c with value `%s'\n", optarg);
  128.       break;
  129.  
  130.     case 'd':
  131.       printf ("option d with value `%s'\n", optarg);
  132.       break;
  133.  
  134.     case '?':
  135.       break;
  136.  
  137.     default:
  138.       printf ("?? getopt returned character code 0%o ??\n", c);
  139.     }
  140.     }
  141.  
  142.   if (optind < argc)
  143.     {
  144.       printf ("non-option ARGV-elements: ");
  145.       while (optind < argc)
  146.     printf ("%s ", argv[optind++]);
  147.       printf ("\n");
  148.     }
  149.  
  150.   exit (0);
  151. }
  152.  
  153. #endif /* TEST */
  154.