home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / getopt1.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  4KB  |  181 lines

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