** Changes in behavior
+ cksum, dd, hostid, hostname, link, logname, sleep, tsort, unlink,
+ uptime, users, whoami, yes: now always process --help and --version options,
+ regardless of any other arguments present before any optional '--'
+ end-of-options marker.
+
+ nohup: now process --help and --version as first options even if other
+ parameters follow.
+
+ 'yes a -- b' now outputs 'a b' instead of including the end-of-options
+ marker as before: 'a -- b'.
+
echo now always processes backslash escapes when the POSIXLY_CORRECT
environment variable is set.
#else /* !CRCTAB */
-# include <getopt.h>
# include "long-options.h"
# include "die.h"
# include "error.h"
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
/* Number of bytes to read at once. */
# define BUFLEN (1 << 16)
so that processes running in parallel do not intersperse their output. */
setvbuf (stdout, NULL, _IOLBF, 0);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE, Version,
+ true, usage, AUTHORS, (char const *) NULL);
have_read_stdin = false;
#include <sys/types.h>
#include <signal.h>
-#include <getopt.h>
#include "system.h"
#include "close-stream.h"
proper_name ("David MacKenzie"), \
proper_name ("Stuart Kemp")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
/* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
present. */
#ifndef SA_NOCLDSTOP
page_size = getpagesize ();
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
- usage, AUTHORS, (char const *) NULL);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE, Version,
+ true, usage, AUTHORS, (char const *) NULL);
close_stdout_required = false;
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
-
/* Initialize translation table to identity translation. */
for (i = 0; i < 256; i++)
trans_table[i] = i;
/* Written by Jim Meyering. */
#include <config.h>
-#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
#define AUTHORS proper_name ("Jim Meyering")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
void
usage (int status)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (optind < argc)
{
/* Written by Jim Meyering. */
#include <config.h>
-#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
#define AUTHORS proper_name ("Jim Meyering")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
#if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
defined HAVE_SYS_SYSTEMINFO_H
# include <sys/systeminfo.h>
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (argc == optind + 1)
{
#include <config.h>
#include <stdio.h>
-#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#define AUTHORS proper_name ("Michael Stone")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
void
usage (int status)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (argc < optind + 2)
{
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
-#include <getopt.h>
#include "system.h"
#include "die.h"
#define AUTHORS proper_name ("FIXME: unknown")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
void
usage (int status)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (optind < argc)
{
/* Written by Jim Meyering */
#include <config.h>
-#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#define AUTHORS proper_name ("Jim Meyering")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
/* Exit statuses. */
enum
{
initialize_exit_failure (exit_internal_failure);
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "+", long_options, NULL) != -1)
- usage (exit_internal_failure);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, false, usage, AUTHORS,
+ (char const *) NULL);
if (argc <= optind)
{
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
-#include <getopt.h>
#include "system.h"
#include "cl-strtod.h"
proper_name ("Jim Meyering"), \
proper_name ("Paul Eggert")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
void
usage (int status)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (argc == 1)
{
#include <config.h>
#include <assert.h>
-#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#define AUTHORS proper_name ("Mark Kettenis")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
/* Token delimiters when reading from a file. */
#define DELIM " \t\n"
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (1 < argc - optind)
{
#include <config.h>
#include <stdio.h>
-#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#define AUTHORS proper_name ("Michael Stone")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
void
usage (int status)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (argc < optind + 1)
{
/* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu. */
#include <config.h>
-#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
proper_name ("David MacKenzie"), \
proper_name ("Kaveh Ghazi")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
static void
print_uptime (size_t n, const STRUCT_UTMP *this)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
switch (argc - optind)
{
/* Written by jla; revised by djm */
#include <config.h>
-#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
proper_name ("Joseph Arceneaux"), \
proper_name ("David MacKenzie")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
static int
userid_compare (const void *v_a, const void *v_b)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
switch (argc - optind)
{
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
-#include <getopt.h>
#include "system.h"
#include "die.h"
#define AUTHORS proper_name ("Richard Mlynarik")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
void
usage (int status)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
if (optind != argc)
{
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
-#include <getopt.h>
#include "system.h"
#define AUTHORS proper_name ("David MacKenzie")
-static struct option const long_options[] =
-{
- {NULL, 0, NULL, 0}
-};
-
void
usage (int status)
{
atexit (close_stdout);
- parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
- usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "+", long_options, NULL) != -1)
- usage (EXIT_FAILURE);
+ parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+ Version, true, usage, AUTHORS,
+ (char const *) NULL);
char **operands = argv + optind;
char **operand_lim = argv + argc;
all_tests = \
tests/misc/help-version.sh \
+ tests/misc/help-version-getopt.sh \
tests/tail-2/inotify-race.sh \
tests/tail-2/inotify-race2.sh \
tests/misc/invalid-opt.pl \
--- /dev/null
+#!/bin/sh
+# Ensure --version and --help options are processed before
+# any other options by some programs.
+
+# Copyright (C) 2019 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ yes
+
+programs="cksum dd hostid hostname link logname nohup
+sleep tsort unlink uptime users whoami yes"
+
+# All these variations should show the help/version screen
+# regardless of their position on the command line arguments.
+for prog in $programs; do
+ # skip if the program is not built (e.g., hostname)
+ case " $built_programs " in
+ *" $prog "*) ;;
+ *) continue;;
+ esac
+
+ for opt in --help --version; do
+ rm -f exp out1 out2 out3 || framework_failure_
+
+ # Get the reference output
+ env $prog $opt >exp || fail=1
+
+ # Test: some other argument AFTER --help/--version.
+ env $prog $opt AFTER > out1 || fail=1
+ compare exp out1 || fail=1
+
+ # nohup is an exception: stops processing after first non-option parameter.
+ # E.g., "nohup df --help" should run "df --help", not "df --help".
+ if [ "$prog" = nohup ]; then
+ rm -f exp || framework_failure_
+ df $opt > exp || framework_failure_
+ BEFORE=df
+ else
+ BEFORE=BEFORE
+ fi
+
+ # Test: some other argument BEFORE --help/--version.
+ env $prog $BEFORE $opt > out2 || fail=1
+ compare exp out2 || fail=1
+
+ # Test: some other arguments BEFORE and AFTER --help/--version.
+ env $prog $BEFORE $opt AFTER > out3 || fail=1
+ compare exp out3 || fail=1
+ done
+done
+
+# After end-of-options marker (--), the options should not be parsed.
+# Test with 'yes', and assume the common code will work the
+# same for the other programs.
+cat >exp <<\EOF || framework_failure_
+--help
+EOF
+env yes -- --help | head -n1 > out
+compare exp out || fail=1
+
+Exit $fail