Ignore:
Timestamp:
Mar 30, 2018, 11:03:28 PM (7 years ago)
Author:
bird
Message:

kmk: Forked getopt and created a reentrant variant getopt_r.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/getopt1_r.c

    r3212 r3213  
    1 /* getopt_long and getopt_long_only entry points for GNU getopt.
     1/*  Reentrant version of getopt_long and getopt_long_only.
     2
     3Based on ../getopt*.*:
     4
     5   getopt_long and getopt_long_only entry points for GNU getopt.
    26Copyright (C) 1987-1994, 1996-2016 Free Software Foundation, Inc.
    37
     
    1519
    1620You should have received a copy of the GNU General Public License along with
    17 this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21this program.  If not, see <http://www.gnu.org/licenses/>.
     22
     23Modifications:
     24  Copyright (c) 2018 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
     25*/
    1826
    1927
     28#define FAKES_NO_GETOPT_H /* bird */
    2029#ifdef HAVE_CONFIG_H
    2130#include <config.h>
    2231#endif
    2332
    24 #include "getopt.h"
     33#include "getopt_r.h"
    2534
    2635#if !defined __STDC__ || !__STDC__
     
    3443#include <stdio.h>
    3544
     45#if 0
    3646/* Comment out all this code if we are using the GNU C Library, and are not
    3747   actually compiling the library itself.  This code is part of the GNU C
     
    4959#endif
    5060#endif
     61#endif
    5162
    52 #ifndef ELIDE_CODE
     63#if 1 //ndef ELIDE_CODE
    5364
    5465
     
    6475
    6576int
    66 getopt_long (int argc, char *const *argv, const char *options,
    67              const struct option *long_options, int *opt_index)
     77getopt_long_r (struct getopt_state_r *gos, int *opt_index)
    6878{
    69   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
     79  return _getopt_internal_r (gos, gos->long_options, opt_index, 0);
    7080}
    7181
     
    7686
    7787int
    78 getopt_long_only (int argc, char *const *argv, const char *options,
    79                   const struct option *long_options, int *opt_index)
     88getopt_long_only_r (struct getopt_state_r *gos, int *opt_index)
    8089{
    81   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
     90  return _getopt_internal_r (gos, gos->long_options, opt_index, 0);
    8291}
    8392
    8493
    85 #endif  /* Not ELIDE_CODE.  */
     94#endif /* #if 1 */ /* Not ELIDE_CODE.  */
    8695
    8796
     
    100109      int this_option_optind = optind ? optind : 1;
    101110      int option_index = 0;
     111      struct getopt_state_r gos;
    102112      static struct option long_options[] =
    103113      {
     
    111121      };
    112122
    113       c = getopt_long (argc, argv, "abc:d:0123456789",
    114                        long_options, &option_index);
     123      getopt_initialize_r (&gos, argc, argv, "abc:d:0123456789", long_options, NULL, NULL);
     124      c = getopt_long_r (&geo, &option_index);
    115125      if (c == -1)
    116126        break;
     
    120130        case 0:
    121131          printf ("option %s", long_options[option_index].name);
    122           if (optarg)
    123             printf (" with arg %s", optarg);
     132          if (geo.optarg)
     133            printf (" with arg %s", geo.optarg);
    124134          printf ("\n");
    125135          break;
     
    150160
    151161        case 'c':
    152           printf ("option c with value '%s'\n", optarg);
     162          printf ("option c with value '%s'\n", geo.optarg);
    153163          break;
    154164
    155165        case 'd':
    156           printf ("option d with value '%s'\n", optarg);
     166          printf ("option d with value '%s'\n", geo.optarg);
    157167          break;
    158168
     
    165175    }
    166176
    167   if (optind < argc)
     177  if (geo.optind < argc)
    168178    {
    169179      printf ("non-option ARGV-elements: ");
    170180      while (optind < argc)
    171         printf ("%s ", argv[optind++]);
     181        printf ("%s ", argv[geo.optind++]);
    172182      printf ("\n");
    173183    }
Note: See TracChangeset for help on using the changeset viewer.