coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: readlink(1) of more than one file?


From: Pádraig Brady
Subject: Re: readlink(1) of more than one file?
Date: Wed, 12 Dec 2012 19:54:02 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1

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;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]