From 878e51ebfaf130f494957e7e6b5bce96d6e1eb13 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 15 May 2008 11:19:44 +0200 Subject: [PATCH] du, wc: merge improved --files0-from=F-related diagnostics du gave a better diagnostic for one unusual case, and wc gave a better diagnostic for a different one. Now each diagnoses both unusual cases. * src/du.c (main): Disallow '-' as file name when reading from stdin. * src/wc.c (main): Give a better diagnostic for a zero-length file name. --- src/du.c | 14 +++++++++++++- src/wc.c | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/du.c b/src/du.c index 6681079..55aa1fe 100644 --- a/src/du.c +++ b/src/du.c @@ -1,5 +1,5 @@ /* du -- summarize disk usage - Copyright (C) 1988-1991, 1995-2007 Free Software Foundation, Inc. + Copyright (C) 1988-1991, 1995-2008 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 @@ -977,10 +977,22 @@ main (int argc, char **argv) if ( ! files[i]) break; + if (files_from && STREQ (files_from, "-") && STREQ (files[i], "-")) + { + /* Give a better diagnostic in an unusual case: + printf - | du --files0-from=- */ + error (0, 0, _("when reading file names from stdin, " + "no file name of %s allowed"), + quote ("-")); + continue; + } + if (files[i][0]) i++; else { + /* Diagnose a zero-length file name. When it's one + among many, knowing the record number may help. */ if (files_from) { /* Using the standard `filename:line-number:' prefix here is diff --git a/src/wc.c b/src/wc.c index 1945504..ebbb5b3 100644 --- a/src/wc.c +++ b/src/wc.c @@ -30,6 +30,7 @@ #include "inttostr.h" #include "mbchar.h" #include "quote.h" +#include "quotearg.h" #include "readtokens0.h" #include "safe-read.h" @@ -683,15 +684,39 @@ main (int argc, char **argv) ok = true; for (i = 0; i < nfiles; i++) { - if (files_from && STREQ (files_from, "-") && STREQ (files[i], "-")) + if (files[i]) { - ok = false; - error (0, 0, - _("when reading file names from stdin, " - "no file name of %s allowed"), - quote ("-")); - continue; + if (files_from && STREQ (files_from, "-") && STREQ (files[i], "-")) + { + ok = false; + /* Give a better diagnostic in an unusual case: + printf - | wc --files0-from=- */ + error (0, 0, _("when reading file names from stdin, " + "no file name of %s allowed"), + quote ("-")); + continue; + } + + /* Diagnose a zero-length file name. When it's one + among many, knowing the record number may help. */ + if (files[i][0] == '\0') + { + ok = false; + if (files_from) + { + /* Using the standard `filename:line-number:' prefix here is + not totally appropriate, since NUL is the separator, not NL, + but it might be better than nothing. */ + unsigned long int file_number = i + 1; + error (0, 0, "%s:%lu: %s", quotearg_colon (files_from), + file_number, _("invalid zero-length file name")); + } + else + error (0, 0, "%s", _("invalid zero-length file name")); + continue; + } } + ok &= wc_file (files[i], &fstatus[i]); } -- 2.7.4