Commit Graph

36 Commits

Author SHA1 Message Date
Đoàn Trần Công Danh 27fd7278a1 getopt: explicitly ask for POSIX mode on POSIXLY_CORRECT
GNU libc's getopt_long(3) have the tradition of not shuffling arguments
to find options when either POSIXLY_CORRECT is defined in environment
variables or '+' prepended in short options. Hence, the current code
base is fine as is fine as is for util-linux built with GNU libc.

However, musl libc only honour POSIX convention when short options
prepended with '+'. musl libc doesn't care about POSIXLY_CORRECT.
Thus, the behaviour of util-linux's getopt(1) that linked with musl-libc
doesn't match with its own documentation.

Let's make sure a '+' is always prepended to short options if
POSIXLY_CORRECT is defined.

Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
2021-01-06 20:57:57 +07:00
Rosen Penev 042f62dfc5
[clang-tidy] do not use else after return
Found with readability-else-after-return

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:20:59 -07:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Ruediger Meier f45f3ec34a misc: consolidate macro style USAGE_HELP_OPTIONS
changed in include/c.h and applied via sed:

  sed -i 's/fprintf.*\(USAGE_MAN_TAIL.*\)/printf(\1/' $(git ls-files -- "*.c")
  sed -i 's/print_usage_help_options\(.*\);/printf(USAGE_HELP_OPTIONS\1);/' $(git ls-files -- "*.c")

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 16:54:33 +02:00
Ruediger Meier b1a294c448 misc: introduce print_usage_help_options()
Consolidate --help and --version descriptions. We are
now able to align them to the other options.

We changed include/c.h. The rest of this patch was
generated by sed, plus manually setting the right
alignment numbers. We do not change anything but
white spaces in the --help output.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-27 12:26:19 +02:00
Ruediger Meier 6d7bee26bc flock, getopt: write --help to stdout and return 0
... and use errtryhelp() instead of usage().

Note in past "getopt --help" returned 2. But it is otherwise
documented and was just a mistake IMO. See the unreachable exit(0)
which was removed here: d1d03b54

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-26 14:38:24 +02:00
Sami Kerola 2ba641e5f3 misc: add static keyword to where needed [smatch scan]
text-utils/rev.c:68:9: warning: symbol 'buf' was not declared. Should it be
static?

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-20 12:58:49 +01:00
Karel Zak e1164591f7 getopt: be sure that options array is terminated
Reported-by: Anders Kaseorg <andersk@mit.edu>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-10-21 13:51:48 +02:00
Karel Zak 090d8c763a include/closestream: define exit codes
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-16 13:35:06 +02:00
Karel Zak e402d6d3b1 getopt: keep pointer to 'name' in control struct
It's seems more readable than call free(argv[0]).

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-04-22 11:29:46 +02:00
Sami Kerola b13fedd485
getopt: fix memory leaks and integer overflows [ASAN & valgrind]
The getopt(1) is short living command, and one could argue ensuring all
allocations are freed at end of execution is waste of time.  There is a
point in that, but making test-suite runs to be less noisy with ASAN is also
nice as it encourages reading the errors when/if they happen.

Reviewed-by: Yuriy M. Kaminskiy <yumkam@gmail.com>
Reviewed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-04-20 22:04:34 +01:00
Karel Zak 30fbf2f679 getopt: make sure setprogname provided by non-Linux system
based on suggestion from Ruediger Meier.

Reported-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-04-04 10:41:37 +02:00
Ruediger Meier 5c78d493c0 getopt: fix -n name for BSD
BSD gets the program name for warnings from getprogname() and not
from argv. Thus we use setprogname() there.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-07 23:36:46 +01:00
Benno Schulenberg 451dbcfae1 textual: add a docstring to most of the utilities
This adds a concise description of a tool to its usage text.

A first form of this patch was proposed by Steven Honeyman
(see http://www.spinics.net/lists/util-linux-ng/msg09994.html).

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-06 11:27:38 +01:00
Benno Schulenberg ae76917250 textual: do not start option descriptions with a capital
This normalizes the help text of getopt, clarifies one description,
and also adjust two comments.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2014-12-19 09:57:21 +01:00
Karel Zak 4b4e1b10f3 getopt: cleanup add_log_options()
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-12-09 12:21:33 +01:00
Karel Zak c29b090fc5 getopt: rename function to keep code readable
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-12-09 12:17:57 +01:00
Karel Zak f7b38b8733 getopt: fix compiler warning
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-12-09 12:17:22 +01:00
Sami Kerola 3aa141879f getopt: avoid re-terminating long_option list at every update
Terminating the ctl->long_options list once when adding options is
completed is enough.  This also allows moving ctl->long_options_nr
increment to more appropriate location.

CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:02 +00:00
Sami Kerola 80dd38e9fb getopt: change --shell argument parsing function
Avoid passing getopt_control stucture in when returning a value is enough.

CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:01 +00:00
Sami Kerola bb742d7352 getopt: prefer switch-case rather than long if statement
And avoid testing same thing time after time.

CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:01 +00:00
Sami Kerola 0640689c20 getopt: remove unnecessary code
The control structure is initialized in main().

CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:01 +00:00
Sami Kerola eba520d824 getopt: make normalize() print strings
A change towards less indirection, and a change to free allocated memory.

CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:01 +00:00
Sami Kerola 05ac43f5f8 getopt: use xstrdup rather than malloc + strcpy
CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:01 +00:00
Sami Kerola 1dacc901ec getopt: add struct getopt_control and remove global variables
CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:00 +00:00
Sami Kerola 5f69b38c5e getopt: remove function prototypes
Functions are in right order so they do not need prototypes, with effect
of not needing to keep function argument changes in sync in two locations.

CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:00 +00:00
Sami Kerola d01f47f6f5 getopt: make nonoptions optstring comment correct
From manual page: 'If the first character of optstring is '-', then each
nonoption argv-element is handled as if it were the argument of an option
with character code 1.'

Reference: http://man7.org/linux/man-pages/man3/getopt.3.html
CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:00 +00:00
Sami Kerola b5f3078e14 getopt: sync with 1.1.6 release
Reference: http://www.spinics.net/lists/util-linux-ng/msg10556.html
CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-12-08 20:07:00 +00:00
Sami Kerola db433bf737 textual: use usage() text element macros
Translating these text elements should happen only once, which is
more likely when the text macros are used properly.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-10-01 12:33:26 +02:00
Sami Kerola a587cc5520 textual: use manual tail usage() macro
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-10-01 12:33:24 +02:00
Karel Zak 3706db7c08 getopt: fix usage() typo unquote -> unquoted
Signed-off-by: Karel Zak <kzak@redhat.com>
2013-04-05 11:21:17 +02:00
Karel Zak 31bfda07a4 getopt: fix normalize() usage [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2013-03-27 14:38:07 +01:00
Benno Schulenberg b50945d4ac textual: spell and encode the name of Arkadiusz Miśkiewicz correctly
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2013-02-06 11:15:02 +01:00
Karel Zak e421313dc2 textual: use UTIL_LINUX_VERSION everywhere
Signed-off-by: Karel Zak <kzak@redhat.com>
2013-01-25 12:05:26 +01:00
Karel Zak f7fff0096a getopt: make the code more readable for analysers
Signed-off-by: Karel Zak <kzak@redhat.com>
2012-09-07 11:42:34 +02:00
Karel Zak f9a20f0b58 build-sys: move getopt to misc-utils/
Signed-off-by: Karel Zak <kzak@redhat.com>
2012-06-26 20:48:23 +02:00