# HG changeset patch # User Akim Demaille # Date 1340012492 -7200 # Node ID 6b45aca5c4b22c7077fe911c0cdd103c712dd7ec # Parent bb4ca9725d0a31cf4a8bf976a6206a11851e3f8e relpath: do not depend on xalloc.h. 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. diff -r bb4ca9725d0a -r 6b45aca5c4b2 lib/relpath.c --- a/lib/relpath.c Wed Jun 13 22:21:57 2012 +0200 +++ b/lib/relpath.c Mon Jun 18 11:41:32 2012 +0200 @@ -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 @@ 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 @@ } 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 @@ 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 @@ free (relative_from); relative_from = NULL; } + res = relative_from ? relative_from : xstrdup (from); - free (realtarget); - free (realfrom); - - return relative_from ? relative_from : xstrdup (from); + end: + { + int saved_errno = errno; + free (realtarget); + free (realfrom); + errno = saved_errno; + } + return res; } diff -r bb4ca9725d0a -r 6b45aca5c4b2 lib/relpath.h --- a/lib/relpath.h Wed Jun 13 22:21:57 2012 +0200 +++ b/lib/relpath.h Mon Jun 18 11:41:32 2012 +0200 @@ -20,13 +20,14 @@ # 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); diff -r bb4ca9725d0a -r 6b45aca5c4b2 modules/relpath --- a/modules/relpath Wed Jun 13 22:21:57 2012 +0200 +++ b/modules/relpath Mon Jun 18 11:41:32 2012 +0200 @@ -11,7 +11,6 @@ error pathmax stdbool -xalloc configure.ac: