]> Savannah Git Hosting - gnulib.git/commitdiff
relpath: do not depend on xalloc.h. akim/relpath
authorAkim Demaille <akim@lrde.epita.fr>
Mon, 18 Jun 2012 09:41:32 +0000 (11:41 +0200)
committerAkim Demaille <akim@lrde.epita.fr>
Mon, 18 Jun 2012 11:35:59 +0000 (13:35 +0200)
Suggested by Bruno Haible.
* lib/relpath.c (convert_abs_rel): Embrace malloc failures.
Simplify some conditionals.
* lib/relpath.h: Adjust the documentation.
* modules/relpath (Depends-on): Remove xalloc.

lib/relpath.c
lib/relpath.h
modules/relpath

index 18691782d38eeec992eaa747bd6d2010fcf93dd5..36a17e07c9c94544c970d55d80e7b22e91d42107 100644 (file)
@@ -31,7 +31,6 @@
 #include "dirname.h"
 #include "error.h"
 #include "relpath.h"
-#include "xalloc.h"
 
 #include "pathmax.h"
 #ifndef PATH_MAX
@@ -98,8 +97,7 @@ buffer_or_output (const char* str, char **pbuf, size_t *plen)
   return false;
 }
 
-/* Output the relative representation if possible.
-   If BUF is non NULL, write to that buffer rather than to stdout.  */
+
 bool
 relpath (const char *can_fname, const char *can_reldir, char *buf, size_t len)
 {
@@ -138,8 +136,8 @@ relpath (const char *can_fname, const char *can_reldir, char *buf, size_t len)
     }
   else
     {
-        buf_err |= buffer_or_output (*fname_suffix ? fname_suffix : ".",
-                                     &buf, &len);
+      buf_err |= buffer_or_output (*fname_suffix ? fname_suffix : ".",
+                                   &buf, &len);
     }
 
   if (buf_err)
@@ -154,11 +152,27 @@ relpath (const char *can_fname, const char *can_reldir, char *buf, size_t len)
 char *
 convert_abs_rel (const char *from, const char *target)
 {
-  char *realtarget = canonicalize_filename_mode (target, CAN_MISSING);
-  char *realfrom = canonicalize_filename_mode (from, CAN_MISSING);
+  char *realtarget = NULL;
+  char *realfrom = NULL;
+  char *relative_from = NULL;
+  char *res = NULL;
+
+  realtarget = canonicalize_filename_mode (target, CAN_MISSING);
+  if (!realtarget)
+    goto end;
+  realfrom = canonicalize_filename_mode (from, CAN_MISSING);
+  if (!realfrom)
+    goto end;
 
   /* Write to a PATH_MAX buffer.  */
-  char *relative_from = xmalloc (PATH_MAX);
+  relative_from = malloc (PATH_MAX);
+  if (!relative_from)
+    {
+      /* It's easier to set errno to ENOMEM than to rely on the
+         'malloc-posix' gnulib module.  */
+      errno = ENOMEM;
+      goto end;
+    }
 
   /* Get dirname to generate paths relative to.  */
   realtarget[dir_len (realtarget)] = '\0';
@@ -168,9 +182,14 @@ convert_abs_rel (const char *from, const char *target)
       free (relative_from);
       relative_from = NULL;
     }
-
-  free (realtarget);
-  free (realfrom);
-
-  return relative_from ? relative_from : xstrdup (from);
+  res = relative_from ? relative_from : xstrdup (from);
+
+ end:
+  {
+    int saved_errno = errno;
+    free (realtarget);
+    free (realfrom);
+    errno = saved_errno;
+  }
+  return res;
 }
index ad499769c38307bd1adeac1c9a56d3ca1e1cb56b..17b07de943f15dce37038a7a8650dd485cd79df3 100644 (file)
 # define _RELPATH_H
 
 /* Output the relative representation if possible.
-   If BUF is non NULL, write to that buffer rather than to stdout.  */
+   If BUF is non NULL, write to that buffer rather than to stdout.
+   Return true iff success.  */
 
 bool
 relpath (const char *can_fname, const char *can_reldir, char *buf, size_t len);
 
-/* Return FROM represented as relative to the dir of TARGET.
-   The result is malloced.  */
+/* Return FROM represented as relative to the dir of TARGET.  The
+   result is malloced, or NULL upon error (described by errno).  */
 
 char *
 convert_abs_rel (const char *from, const char *target);
index a250296b7816fdcbaf8349b8ddd9abdb2276551f..814d169c8ed6e67029e7b6cda8fc9e72bc75e8c9 100644 (file)
@@ -11,7 +11,6 @@ dirname
 error
 pathmax
 stdbool
-xalloc
 
 configure.ac: