]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
copyfilerange: simply report "too few arguments", not misleading messages
authorBenno Schulenberg <bensberg@telfort.nl>
Mon, 16 Mar 2026 11:09:38 +0000 (12:09 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 17 Mar 2026 11:28:53 +0000 (12:28 +0100)
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 <dick@mrns.nl>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
misc-utils/copyfilerange.c

index 03b9e10015089960a557fa2f7f7f3f744ac85000..f28cacd105e3c0febab7b528bae17f51c9840974 100644 (file)
@@ -242,27 +242,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);