]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
stdbuf: improve path search
authorEric Blake <ebb9@byu.net>
Thu, 24 Sep 2009 23:18:47 +0000 (17:18 -0600)
committerEric Blake <ebb9@byu.net>
Thu, 8 Oct 2009 12:52:18 +0000 (06:52 -0600)
* src/stdbuf.c (set_program_path): Use gnulib methods for better
file name handling.
* bootstrap.conf (gnulib_modules): Add xreadlink.

bootstrap.conf
src/stdbuf.c

index e523273fc4d05680d648f2e2204699247b42268a..9cf3746a2a9c43ceb73cf6788ac89ae1371346da 100644 (file)
@@ -238,6 +238,7 @@ gnulib_modules="
   xnanosleep
   xprintf
   xprintf-posix
+  xreadlink
   xstrtod
   xstrtoimax
   xstrtol
index afb7821f7f9642a315b3bbd6864a6dee4176093f..05a6b9fe8e5b62a007d95df4222ec78d44596c57 100644 (file)
 
 #include "system.h"
 #include "error.h"
+#include "filenamecat.h"
 #include "posixver.h"
 #include "quote.h"
+#include "xreadlink.h"
 #include "xstrtol.h"
 #include "c-ctype.h"
 
@@ -145,34 +147,26 @@ set_program_path (const char *arg)
     }
   else
     {
-      char *path;
-      char tmppath[PATH_MAX + 1];
-      ssize_t len = readlink ("/proc/self/exe", tmppath, sizeof (tmppath) - 1);
-      if (len > 0)
-        {
-          tmppath[len] = '\0';
-          program_path = dir_name (tmppath);
-        }
+      char *path = xreadlink ("/proc/self/exe");
+      if (path)
+        program_path = dir_name (path);
       else if ((path = getenv ("PATH")))
         {
           char *dir;
           path = xstrdup (path);
           for (dir = strtok (path, ":"); dir != NULL; dir = strtok (NULL, ":"))
             {
-              int req = snprintf (tmppath, sizeof (tmppath), "%s/%s", dir, arg);
-              if (req >= sizeof (tmppath))
-                {
-                  error (0, 0, _("path truncated when looking for %s"),
-                         quote (arg));
-                }
-              else if (access (tmppath, X_OK) == 0)
+              char *candidate = file_name_concat (dir, arg, NULL);
+              if (access (candidate, X_OK) == 0)
                 {
-                  program_path = dir_name (tmppath);
+                  program_path = dir_name (candidate);
+                  free (candidate);
                   break;
                 }
+              free (candidate);
             }
-          free (path);
         }
+      free (path);
     }
 }