On 12/12/2012 07:05 PM, Aaron Davies wrote:
Is there a reason the interface for readlink(1) is “FILE” instead of “FILE...”?
I’ve often wanted to do e.g. “find -type l|xargs readlink” or (in zsh)
“readlink **/*(@)”, and having to do a shell loop or use “xargs -n1” seems
inelegant.
Note the newer more general realpath(1)
supports multiple files.
Though there is no reason I see that readlink(1)
can't do so too. I also see the BSD version
can accept multiple args, so I'll probably add
something along the lines of the following
unless there are objections.
thanks,
Pádraig.
diff --git a/src/readlink.c b/src/readlink.c
index e025bf9..0db0c32 100644
--- a/src/readlink.c
+++ b/src/readlink.c
@@ -94,13 +94,7 @@ main (int argc, char **argv)
{
/* If not -1, use this method to canonicalize. */
int can_mode = -1;
-
- /* File name to canonicalize. */
- const char *fname;
-
- /* Result of canonicalize. */
- char *value;
-
+ int status = EXIT_SUCCESS;
int optc;
initialize_main (&argc, &argv);
@@ -147,26 +141,28 @@ main (int argc, char **argv)
usage (EXIT_FAILURE);
}
- fname = argv[optind++];
-
- if (optind < argc)
+ for (; optind < argc; ++optind)
{
- error (0, 0, _("extra operand %s"), quote (argv[optind]));
- usage (EXIT_FAILURE);
- }
+ const char *fname;
+ char *value;
- value = (can_mode != -1
- ? canonicalize_filename_mode (fname, can_mode)
- : areadlink_with_size (fname, 63));
- if (value)
- {
- printf ("%s%s", value, (no_newline ? "" : "\n"));
- free (value);
- return EXIT_SUCCESS;
- }
+ fname = argv[optind];
- if (verbose)
- error (EXIT_FAILURE, errno, "%s", fname);
+ value = (can_mode != -1
+ ? canonicalize_filename_mode (fname, can_mode)
+ : areadlink_with_size (fname, 63));
+ if (value)
+ {
+ printf ("%s%s", value, (no_newline ? "" : "\n"));
+ free (value);
+ }
+ else
+ {
+ status = EXIT_FAILURE;
+ if (verbose)
+ error (0, errno, "%s", fname);
+ }
+ }
- return EXIT_FAILURE;
+ return status;
}