Updated man page to document new behaviour of -Z, -z and -Q flags.
[unix-history] / gnu / usr.bin / sort / long-options.c
CommitLineData
c4698cbb
NW
1/* Utility to accept --help and --version options as unobtrusively as possible.
2 Copyright (C) 1993 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any 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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18/* Jim Meyering (meyering@comco.com) */
19
20#ifdef HAVE_CONFIG_H
21#if defined (CONFIG_BROKETS)
22/* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
25#include <config.h>
26#else
27#include "config.h"
28#endif
29#endif
30
31#include <stdio.h>
32#include <getopt.h>
33#include <sys/types.h>
34#include "system.h"
35#include "version.h"
36#include "long-options.h"
37
38static struct option const long_options[] =
39{
40 {"help", no_argument, 0, 'h'},
41 {"version", no_argument, 0, 'v'},
42 {0, 0, 0, 0}
43};
44
45/* Process long options --help and --version, but only if argc == 2.
46 Be careful not to gobble up `--'. */
47
48void
49parse_long_options (argc, argv, usage)
50 int argc;
51 char **argv;
52 void (*usage)();
53{
54 int c;
55 int saved_opterr;
56 int saved_optind;
57
58 saved_opterr = opterr;
59 saved_optind = optind;
60
61 /* Don't print an error message for unrecognized options. */
62 opterr = 0;
63
64 if (argc == 2
65 && (c = getopt_long (argc, argv, "+", long_options, (int *) 0)) != EOF)
66 {
67 switch (c)
68 {
69 case 'h':
70 usage (0);
71
72 case 'v':
73 printf ("%s\n", version_string);
74 exit (0);
75
76 default:
77 /* Don't process any other long-named options. */
78 break;
79 }
80 }
81
82 /* Restore previous value. */
83 opterr = saved_opterr;
84
85 /* Restore optind in case it has advanced past a leading `--'. */
86 optind = saved_optind;
87}