[PATCH 2/2] copyfilerange: simply report "too few arguments", not misleading messages

Benno Schulenberg <[email protected]> Mon, 16 Mar 2026 12:09:38 +0100
Newsgroups org.kernel.vger.util-linux
Message-ID <[email protected]>
Running ./copyfilerange without any arguments would report:

  copyfilerange: source file is required

giving the impression that only a source file is required.
But running ./copyfilerange with one argument would report:

  copyfilerange: destination file is required

giving the impression that specifying two files would be enough.
But running ./copyfilerange with two arguments would report:

  copyfilerange: nothing to do, no ranges supplied

Instead of these custom messages, let's report what other tools
report when given too few arguments: "too few arguments".

This change also prevents `copyfilerange` from creating an empty
destination file when given just two arguments, when it reported
that there was nothing to do.

Furthermore, correct a parameter of a call of err(),
from `argv[2]` to `range.out_filename`.

CC: Dick Marinus <[email protected]>
Signed-off-by: Benno Schulenberg <[email protected]>
---
 misc-utils/copyfilerange.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/misc-utils/copyfilerange.c b/misc-utils/copyfilerange.c
index 71b21bb28..2ebe43b3b 100644
--- a/misc-utils/copyfilerange.c
+++ b/misc-utils/copyfilerange.c
@@ -244,27 +244,20 @@ int main(const int argc, char **argv)
 			range.out_filename = argv[rem_optind];
 	}
 
-	if (!range.in_filename)
-		errx(EXIT_FAILURE, _("source file is required"));
-
-	if (!range.out_filename)
-		errx(EXIT_FAILURE, _("destination file is required"));
+	if (!range.out_filename || (rem_optind == argc && !nrange_files))
+		errx(EXIT_FAILURE, _("too few arguments"));
 
 	range.in_fd = open(range.in_filename, O_RDONLY);
 	if (range.in_fd < 0)
 		err(EXIT_FAILURE, _("cannot open source %s"), range.in_filename);
 
-
 	if (fstat(range.in_fd, &sb) == -1)
 		err(EXIT_FAILURE, _("cannot determine size of source file %s"), range.in_filename);
 	range.in_st_size = sb.st_size;
 
 	range.out_fd = open(range.out_filename, O_WRONLY | O_CREAT, 0666);
 	if (range.out_fd < 0)
-		err(EXIT_FAILURE, _("cannot open destination %s"), argv[2]);
-
-	if (rem_optind == argc && !nrange_files)
-		errx(EXIT_FAILURE, _("nothing to do, no ranges supplied"));
+		err(EXIT_FAILURE, _("cannot open destination %s"), range.out_filename);
 
 	if (nrange_files)
 		handle_range_files(&range, nrange_files, range_files);
-- 
2.53.0