summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2007-08-04 13:36:59 +0200
committerJim Meyering <jim@meyering.net>2007-08-04 13:36:59 +0200
commit8188c3b48ded8b53d88440f0736690d2e30508ed (patch)
treeb36bbe8ed28eaa4dc37c9876cc5f71a520bb9fdd /src
parent33f95a9ab62968a6b13eb1a6a71f180f61721f71 (diff)
downloadcoreutils-8188c3b48ded8b53d88440f0736690d2e30508ed.tar.xz
Adapt to new human and xstrtol API.
* src/df.c (long_options): Prepend "--" to long options that OPT_STR might decode. * src/du.c (long_options): Likewise. * src/od.c (long_options): Likewise. * src/sort.c (long_options): Likewise. * src/df.c (main): Adjust to new human and xstrtol API. * src/du.c (main): Likewise. * src/ls.c (decode_switches): Likewise. * src/od.c (main): Likewise. * src/pr.c (first_last_page): Likewise. New argument OPTION. All callers changed. * src/sort.c (specify_sort_size): New arg OPTION. All callers changed. Adjust to new xstrtol API. * src/system.h (opt_str_storage): New static var. (OPT_STR, LONG_OPT_STR, OPT_STR_INIT): New macros.
Diffstat (limited to 'src')
-rw-r--r--src/df.c23
-rw-r--r--src/du.c23
-rw-r--r--src/ls.c13
-rw-r--r--src/od.c28
-rw-r--r--src/pr.c10
-rw-r--r--src/sort.c11
-rw-r--r--src/system.h17
7 files changed, 85 insertions, 40 deletions
diff --git a/src/df.c b/src/df.c
index 41bda8705..c2d1180b7 100644
--- a/src/df.c
+++ b/src/df.c
@@ -121,7 +121,7 @@ enum
static struct option const long_options[] =
{
{"all", no_argument, NULL, 'a'},
- {"block-size", required_argument, NULL, 'B'},
+ {OPT_STR_INIT ("block-size"), required_argument, NULL, 'B'},
{"inodes", no_argument, NULL, 'i'},
{"human-readable", no_argument, NULL, 'h'},
{"si", no_argument, NULL, 'H'},
@@ -776,7 +776,6 @@ kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
int
main (int argc, char **argv)
{
- int c;
struct stat *stats IF_LINT (= 0);
initialize_main (&argc, &argv);
@@ -798,16 +797,26 @@ main (int argc, char **argv)
posix_format = false;
exit_status = EXIT_SUCCESS;
- while ((c = getopt_long (argc, argv, "aB:iF:hHklmPTt:vx:", long_options, NULL))
- != -1)
+ for (;;)
{
+ int oi = -1;
+ int c = getopt_long (argc, argv, "aB:iF:hHklmPTt:vx:", long_options,
+ &oi);
+ if (c == -1)
+ break;
+
switch (c)
{
case 'a':
show_all_fs = true;
break;
case 'B':
- human_output_opts = human_options (optarg, true, &output_block_size);
+ {
+ enum strtol_error e = human_options (optarg, &human_output_opts,
+ &output_block_size);
+ if (e != LONGINT_OK)
+ STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, e);
+ }
break;
case 'i':
inode_format = true;
@@ -873,8 +882,8 @@ main (int argc, char **argv)
output_block_size = (getenv ("POSIXLY_CORRECT") ? 512 : 1024);
}
else
- human_output_opts = human_options (getenv ("DF_BLOCK_SIZE"), false,
- &output_block_size);
+ human_options (getenv ("DF_BLOCK_SIZE"),
+ &human_output_opts, &output_block_size);
}
/* Fail if the same file system type was both selected and excluded. */
diff --git a/src/du.c b/src/du.c
index f9bd2e364..caacbb0bd 100644
--- a/src/du.c
+++ b/src/du.c
@@ -204,7 +204,7 @@ static struct option const long_options[] =
{
{"all", no_argument, NULL, 'a'},
{"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
- {"block-size", required_argument, NULL, 'B'},
+ {OPT_STR_INIT ("block-size"), required_argument, NULL, 'B'},
{"bytes", no_argument, NULL, 'b'},
{"count-links", no_argument, NULL, 'l'},
{"dereference", no_argument, NULL, 'L'},
@@ -661,7 +661,6 @@ du_files (char **files, int bit_flags)
int
main (int argc, char **argv)
{
- int c;
char *cwd_only[2];
bool max_depth_specified = false;
char **files;
@@ -692,12 +691,17 @@ main (int argc, char **argv)
exclude = new_exclude ();
- human_output_opts = human_options (getenv ("DU_BLOCK_SIZE"), false,
- &output_block_size);
+ human_options (getenv ("DU_BLOCK_SIZE"),
+ &human_output_opts, &output_block_size);
- while ((c = getopt_long (argc, argv, DEBUG_OPT "0abchHklmsxB:DLPSX:",
- long_options, NULL)) != -1)
+ for (;;)
{
+ int oi = -1;
+ int c = getopt_long (argc, argv, DEBUG_OPT "0abchHklmsxB:DLPSX:",
+ long_options, &oi);
+ if (c == -1)
+ break;
+
switch (c)
{
#if DU_DEBUG
@@ -788,7 +792,12 @@ main (int argc, char **argv)
break;
case 'B':
- human_output_opts = human_options (optarg, true, &output_block_size);
+ {
+ enum strtol_error e = human_options (optarg, &human_output_opts,
+ &output_block_size);
+ if (e != LONGINT_OK)
+ STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, e);
+ }
break;
case 'D': /* This will eventually be 'H' (-H), too. */
diff --git a/src/ls.c b/src/ls.c
index ee736227e..064a51f74 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -1450,8 +1450,8 @@ decode_switches (int argc, char **argv)
{
char const *ls_block_size = getenv ("LS_BLOCK_SIZE");
- human_output_opts = human_options (ls_block_size, false,
- &output_block_size);
+ human_options (ls_block_size,
+ &human_output_opts, &output_block_size);
if (ls_block_size || getenv ("BLOCK_SIZE"))
file_output_block_size = output_block_size;
}
@@ -1793,8 +1793,13 @@ decode_switches (int argc, char **argv)
break;
case BLOCK_SIZE_OPTION:
- human_output_opts = human_options (optarg, true, &output_block_size);
- file_output_block_size = output_block_size;
+ {
+ enum strtol_error e = human_options (optarg, &human_output_opts,
+ &output_block_size);
+ if (e != LONGINT_OK)
+ STRTOL_FATAL_ERROR ("--block-size", optarg, e);
+ file_output_block_size = output_block_size;
+ }
break;
case SI_OPTION:
diff --git a/src/od.c b/src/od.c
index 3f2159ee8..472c513e3 100644
--- a/src/od.c
+++ b/src/od.c
@@ -1,5 +1,5 @@
/* od -- dump files in octal and other formats
- Copyright (C) 92, 1995-2006 Free Software Foundation, Inc.
+ Copyright (C) 92, 1995-2007 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
@@ -281,14 +281,14 @@ enum
static struct option const long_options[] =
{
- {"skip-bytes", required_argument, NULL, 'j'},
+ {OPT_STR_INIT ("skip-bytes"), required_argument, NULL, 'j'},
{"address-radix", required_argument, NULL, 'A'},
- {"read-bytes", required_argument, NULL, 'N'},
+ {OPT_STR_INIT ("read-bytes"), required_argument, NULL, 'N'},
{"format", required_argument, NULL, 't'},
{"output-duplicates", no_argument, NULL, 'v'},
- {"strings", optional_argument, NULL, 'S'},
+ {OPT_STR_INIT ("strings"), optional_argument, NULL, 'S'},
{"traditional", no_argument, NULL, TRADITIONAL_OPTION},
- {"width", optional_argument, NULL, 'w'},
+ {OPT_STR_INIT ("width"), optional_argument, NULL, 'w'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
@@ -1555,7 +1555,6 @@ dump_strings (void)
int
main (int argc, char **argv)
{
- int c;
int n_files;
size_t i;
int l_c_m;
@@ -1609,11 +1608,14 @@ main (int argc, char **argv)
address_pad_len = 7;
flag_dump_strings = false;
- while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
- != -1)
+ for (;;)
{
uintmax_t tmp;
enum strtol_error s_err;
+ int oi = -1;
+ int c = getopt_long (argc, argv, short_options, long_options, &oi);
+ if (c == -1)
+ break;
switch (c)
{
@@ -1653,7 +1655,7 @@ it must be one character from [doxn]"),
modern = true;
s_err = xstrtoumax (optarg, NULL, 0, &n_bytes_to_skip, multipliers);
if (s_err != LONGINT_OK)
- STRTOL_FATAL_ERROR (optarg, _("skip argument"), s_err);
+ STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, s_err);
break;
case 'N':
@@ -1663,7 +1665,7 @@ it must be one character from [doxn]"),
s_err = xstrtoumax (optarg, NULL, 0, &max_bytes_to_format,
multipliers);
if (s_err != LONGINT_OK)
- STRTOL_FATAL_ERROR (optarg, _("limit argument"), s_err);
+ STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, s_err);
break;
case 'S':
@@ -1674,7 +1676,8 @@ it must be one character from [doxn]"),
{
s_err = xstrtoumax (optarg, NULL, 0, &tmp, multipliers);
if (s_err != LONGINT_OK)
- STRTOL_FATAL_ERROR (optarg, _("minimum string length"), s_err);
+ STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg,
+ s_err);
/* The minimum string length may be no larger than SIZE_MAX,
since we may allocate a buffer of this size. */
@@ -1746,7 +1749,8 @@ it must be one character from [doxn]"),
uintmax_t w_tmp;
s_err = xstrtoumax (optarg, NULL, 10, &w_tmp, "");
if (s_err != LONGINT_OK)
- STRTOL_FATAL_ERROR (optarg, _("width specification"), s_err);
+ STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg,
+ s_err);
if (SIZE_MAX < w_tmp)
error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
desired_width = w_tmp;
diff --git a/src/pr.c b/src/pr.c
index f7ae01512..329183b99 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -796,14 +796,14 @@ cols_ready_to_print (void)
using option +FIRST_PAGE:LAST_PAGE */
static bool
-first_last_page (char const *pages)
+first_last_page (char const *option, char const *pages)
{
char *p;
uintmax_t first;
uintmax_t last = UINTMAX_MAX;
strtol_error err = xstrtoumax (pages, &p, 10, &first, "");
if (err != LONGINT_OK && err != LONGINT_INVALID_SUFFIX_CHAR)
- _STRTOL_ERROR (EXIT_FAILURE, pages, _("page range"), err);
+ STRTOL_FATAL_ERROR (option, pages, err);
if (p == pages || !first)
return false;
@@ -813,7 +813,7 @@ first_last_page (char const *pages)
char const *p1 = p + 1;
err = xstrtoumax (p1, &p, 10, &last, "");
if (err != LONGINT_OK)
- _STRTOL_ERROR (EXIT_FAILURE, pages, _("page range"), err);
+ STRTOL_FATAL_ERROR (option, pages, err);
if (p1 == p || last < first)
return false;
}
@@ -902,7 +902,7 @@ main (int argc, char **argv)
case 1: /* Non-option argument. */
/* long option --page dominates old `+FIRST_PAGE ...'. */
if (! (first_page_number == 0
- && *optarg == '+' && first_last_page (optarg + 1)))
+ && *optarg == '+' && first_last_page ("+", optarg + 1)))
file_names[n_files++] = optarg;
break;
@@ -911,7 +911,7 @@ main (int argc, char **argv)
if (! optarg)
error (EXIT_FAILURE, 0,
_("`--pages=FIRST_PAGE[:LAST_PAGE]' missing argument"));
- else if (! first_last_page (optarg))
+ else if (! first_last_page ("--pages", optarg))
error (EXIT_FAILURE, 0, _("Invalid page range %s"),
quote (optarg));
break;
diff --git a/src/sort.c b/src/sort.c
index af23e844f..dc7874a9e 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -414,7 +414,7 @@ static struct option const long_options[] =
{"output", required_argument, NULL, 'o'},
{"reverse", no_argument, NULL, 'r'},
{"stable", no_argument, NULL, 's'},
- {"buffer-size", required_argument, NULL, 'S'},
+ {OPT_STR_INIT ("buffer-size"), required_argument, NULL, 'S'},
{"field-separator", required_argument, NULL, 't'},
{"temporary-directory", required_argument, NULL, 'T'},
{"unique", no_argument, NULL, 'u'},
@@ -1032,7 +1032,7 @@ inittables (void)
/* Specify the amount of main memory to use when sorting. */
static void
-specify_sort_size (char const *s)
+specify_sort_size (char const *option, char const *s)
{
uintmax_t n;
char *suffix;
@@ -1088,7 +1088,7 @@ specify_sort_size (char const *s)
e = LONGINT_OVERFLOW;
}
- STRTOL_FATAL_ERROR (s, _("sort size"), e);
+ STRTOL_FATAL_ERROR (option, s, e);
}
/* Return the default sort size. */
@@ -2842,6 +2842,7 @@ main (int argc, char **argv)
pedantic and a file was seen, unless the POSIX version
predates 1003.1-2001 and -c was not seen and the operand is
"-o FILE" or "-oFILE". */
+ int oi = -1;
if (c == -1
|| (posixly_correct && nfiles != 0
@@ -2851,7 +2852,7 @@ main (int argc, char **argv)
&& argv[optind][0] == '-' && argv[optind][1] == 'o'
&& (argv[optind][2] || optind + 1 != argc)))
|| ((c = getopt_long (argc, argv, short_options,
- long_options, NULL))
+ long_options, &oi))
== -1))
{
if (argc <= optind)
@@ -3011,7 +3012,7 @@ main (int argc, char **argv)
break;
case 'S':
- specify_sort_size (optarg);
+ specify_sort_size (OPT_STR (oi, c, long_options), optarg);
break;
case 't':
diff --git a/src/system.h b/src/system.h
index 3c7f49d10..cc97f2fec 100644
--- a/src/system.h
+++ b/src/system.h
@@ -592,3 +592,20 @@ emit_bug_reporting_address (void)
bugs (typically your translation team's web or email address). */
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
+
+/* Use OPT_IDX to decide whether to return either a short option
+ string "-C", or a long option string derived from LONG_OPTION.
+ OPT_IDX is -1 if the short option C was used; otherwise it is an
+ index into LONG_OPTIONS, which should have a name preceded by two
+ '-' characters. */
+static char opt_str_storage[3] = {'-', 0, 0};
+#define OPT_STR(opt_idx, c, long_options) \
+ ((opt_idx) < 0 \
+ ? (opt_str_storage[1] = c, opt_str_storage) \
+ : LONG_OPT_STR (opt_idx, long_options))
+
+/* Likewise, but assume OPT_IDX is nonnegative. */
+#define LONG_OPT_STR(opt_idx, long_options) ((long_options)[opt_idx].name - 2)
+
+/* Define an option string that will be used with OPT_STR or LONG_OPT_STR. */
+#define OPT_STR_INIT(name) ("--" name + 2)