bug-gnulib
[Top][All Lists]
Advanced

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

[Bug-gnulib] gnulib path-concat merge from coreutils


From: Paul Eggert
Subject: [Bug-gnulib] gnulib path-concat merge from coreutils
Date: Thu, 05 Aug 2004 21:51:27 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this:

        * lib/path-concat.c (mempcpy): Don't define if a system header
        defines it.
        Don't include stdio.h, stdlib.h, unistd.h, strdup.h.
        (longest_relative_suffix): New function.
        (path_concat): Use it.  Assume first argument is not NULL.
        Port to DOS.  Omit redundant separators.
        Report an error instead of returning NULL.
        Use mempcpy instead of memcpy.
        (xpath_concat): Remove: not declared or used.
        * m4/path-concat.m4 (gl_PATH_CONCAT): Don't require gl_AC_DOS, the
        prerequisite modules now handle the DOS stuff.
        Don't check for unistd.h.
        * modules/path-concat: Don't depend on strdup.

Index: lib/path-concat.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/path-concat.c,v
retrieving revision 1.18
diff -p -u -r1.18 path-concat.c
--- lib/path-concat.c   25 Feb 2004 19:45:35 -0000      1.18
+++ lib/path-concat.c   6 Aug 2004 04:29:36 -0000
@@ -26,85 +26,62 @@
 /* Specification.  */
 #include "path-concat.h"
 
-#ifndef HAVE_MEMPCPY
-# define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
-#if HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
-#include "strdup.h"
 #include "dirname.h"
 #include "xalloc.h"
 
-/* Concatenate two pathname components, DIR and BASE, in
-   newly-allocated storage and return the result.  Return 0 if out of
-   memory.  Add a slash between DIR and BASE in the result if neither
-   would contribute one.  If each would contribute at least one, elide
-   one from the end of DIR.  Otherwise, simply concatenate DIR and
-   BASE.  In any case, if BASE_IN_RESULT is non-NULL, set
+#if ! HAVE_MEMPCPY && ! defined mempcpy
+# define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
+#endif
+
+/* Return the longest suffix of F that is a relative file name.
+   If it has no such suffix, return the empty string.  */
+
+static char const *
+longest_relative_suffix (char const *f)
+{
+  for (f += FILE_SYSTEM_PREFIX_LEN (f); ISSLASH (*f); f++)
+    continue;
+  return f;
+}
+
+/* Concatenate two pathname components, DIR and ABASE, in
+   newly-allocated storage and return the result.
+   The resulting file name F is such that the commands "ls F" and "(cd
+   DIR; ls BASE)" refer to the same file, where BASE is ABASE with any
+   file system prefixes and leading separators removed.
+   Arrange for a directory separator if necessary between DIR and BASE
+   in the result, removing any redundant separators.
+   In any case, if BASE_IN_RESULT is non-NULL, set
    *BASE_IN_RESULT to point to the copy of BASE in the returned
    concatenation.
 
-   DIR may be NULL, BASE must not be.
-
-   Return NULL if memory is exhausted.  */
+   Report an error if memory is exhausted.  */
 
 char *
-path_concat (const char *dir, const char *base, char **base_in_result)
+path_concat (char const *dir, char const *abase, char **base_in_result)
 {
+  char const *dirbase = base_name (dir);
+  size_t dirbaselen = base_len (dirbase);
+  size_t dirlen = dirbase - dir + dirbaselen;
+  size_t needs_separator = (dirbaselen && ! ISSLASH (dirbase[dirbaselen - 1]));
+
+  char const *base = longest_relative_suffix (abase);
+  size_t baselen = strlen (base);
+
+  char *p_concat = xmalloc (dirlen + needs_separator + baselen + 1);
   char *p;
-  char *p_concat;
-  size_t baselen;
-  size_t dirlen;
-
-  if (!dir)
-    {
-      p_concat = strdup (base);
-      if (base_in_result)
-        *base_in_result = p_concat;
-      return p_concat;
-    }
-
-  /* DIR is not empty. */
-  baselen = base_len (base);
-  dirlen = strlen (dir);
-
-  p_concat = malloc (dirlen + baselen + 2);
-  if (!p_concat)
-    return 0;
 
   p = mempcpy (p_concat, dir, dirlen);
-
-  if (FILESYSTEM_PREFIX_LEN (dir) < dirlen)
-    {
-      if (ISSLASH (*(p - 1)) && ISSLASH (*base))
-       --p;
-      else if (!ISSLASH (*(p - 1)) && !ISSLASH (*base))
-       *p++ = DIRECTORY_SEPARATOR;
-    }
+  *p = DIRECTORY_SEPARATOR;
+  p += needs_separator;
 
   if (base_in_result)
     *base_in_result = p;
 
-  memcpy (p, base, baselen);
-  p[baselen] = '\0';
+  p = mempcpy (p, base, baselen);
+  *p = '\0';
 
   return p_concat;
 }
-
-/* Same, but die when memory is exhausted. */
-
-char *
-xpath_concat (const char *dir, const char *base, char **base_in_result)
-{
-  char *res = path_concat (dir, base, base_in_result);
-  if (! res)
-    xalloc_die ();
-  return res;
-}
Index: m4/path-concat.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/path-concat.m4,v
retrieving revision 1.4
diff -p -u -r1.4 path-concat.m4
--- m4/path-concat.m4   1 Jun 2004 20:27:22 -0000       1.4
+++ m4/path-concat.m4   6 Aug 2004 04:29:36 -0000
@@ -1,4 +1,4 @@
-# path-concat.m4 serial 3
+# path-concat.m4 serial 4
 dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
@@ -9,7 +9,5 @@ dnl the same distribution terms as the r
 AC_DEFUN([gl_PATH_CONCAT],
 [
   dnl Prerequisites of lib/path-concat.c.
-  AC_REQUIRE([gl_AC_DOS])
-  AC_CHECK_HEADERS_ONCE(unistd.h)
   AC_CHECK_FUNCS_ONCE(mempcpy)
 ])
Index: modules/path-concat
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/path-concat,v
retrieving revision 1.3
diff -p -u -r1.3 path-concat
--- modules/path-concat 20 Jan 2003 10:02:38 -0000      1.3
+++ modules/path-concat 6 Aug 2004 04:49:39 -0000
@@ -9,7 +9,6 @@ m4/dos.m4
 m4/path-concat.m4
 
 Depends-on:
-strdup
 xalloc
 dirname
 




reply via email to

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