* lib/basename.c (base_name):
Prefer idx_t to size_t for indexes, and use idx_t-related allocators.
* lib/basename.c: Do not include xstrndup.h.
(basename): Simplify by always using memcpy.
* modules/dirname (Depends-on): Removbe xstrndup.
---
 ChangeLog       |  5 +++++
 lib/basename.c  | 50 +++++++++++++++++++++++++++----------------------
 modules/dirname |  1 -
 3 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 42d748a38..0fdbfe60b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,10 @@
 
        xalloc: new idx_t-based allocators
        This is for code that prefers to use idx_t for sizes.
+       * lib/basename.c (base_name):
+       Prefer idx_t to size_t for indexes, and use idx_t-related allocators.
+       * lib/basename.c: Do not include xstrndup.h.
+       (basename): Simplify by always using memcpy.
        * lib/xalloc.h (ximalloc, xizalloc, xicalloc, xirealloc)
        (xireallocarray, ximemdup, ximemdup0) [GNULIB_XALLOC]:
        New decls.
@@ -14,6 +18,7 @@
        (ximemdup, ximemdup0): New functions.
        (x2nrealloc): Moved here from xalloc.h.
        * modules/xalloc (Depends-on): Add ialloc.
+       * modules/dirname (Depends-on): Removbe xstrndup.
 
        ialloc: new module
        * lib/ialloc.c, lib/ialloc.h, modules/ialloc: New files.
diff --git a/lib/basename.c b/lib/basename.c
index 0ba28780c..1181134ed 100644
--- a/lib/basename.c
+++ b/lib/basename.c
@@ -22,37 +22,43 @@
 
 #include <string.h>
 #include "xalloc.h"
-#include "xstrndup.h"
 
 char *
 base_name (char const *name)
 {
   char const *base = last_component (name);
-  size_t length;
-
-  /* If there is no last component, then name is a file system root or the
-     empty string.  */
-  if (! *base)
-    return xstrndup (name, base_len (name));
-
-  /* Collapse a sequence of trailing slashes into one.  */
-  length = base_len (base);
-  if (ISSLASH (base[length]))
-    length++;
-
-  /* On systems with drive letters, "a/b:c" must return "./b:c" rather
-     than "b:c" to avoid confusion with a drive letter.  On systems
-     with pure POSIX semantics, this is not an issue.  */
-  if (FILE_SYSTEM_PREFIX_LEN (base))
+  idx_t length;
+  int dotslash_len;
+  if (*base)
+    {
+      length = base_len (base);
+
+      /* Collapse a sequence of trailing slashes into one.  */
+      length += ISSLASH (base[length]);
+
+      /* On systems with drive letters, "a/b:c" must return "./b:c" rather
+         than "b:c" to avoid confusion with a drive letter.  On systems
+         with pure POSIX semantics, this is not an issue.  */
+      dotslash_len = FILE_SYSTEM_PREFIX_LEN (base) != 0 ? 2 : 0;
+    }
+  else
+    {
+      /* There is no last component, so NAME is a file system root or
+         the empty string.  */
+      base = name;
+      length = base_len (base);
+      dotslash_len = 0;
+    }
+
+  char *p = ximalloc (dotslash_len + length + 1);
+  if (dotslash_len)
     {
-      char *p = xmalloc (length + 3);
       p[0] = '.';
       p[1] = '/';
-      memcpy (p + 2, base, length);
-      p[length + 2] = '\0';
-      return p;
     }
 
   /* Finally, copy the basename.  */
-  return xstrndup (base, length);
+  memcpy (p + dotslash_len, base, length);
+  p[dotslash_len + length] = '\0';
+  return p;
 }
diff --git a/modules/dirname b/modules/dirname
index e3ffbe04d..483103eba 100644
--- a/modules/dirname
+++ b/modules/dirname
@@ -9,7 +9,6 @@ lib/stripslash.c
 Depends-on:
 dirname-lgpl
 xalloc
-xstrndup
 
 configure.ac:
 gl_MODULE_INDICATOR([dirname])
-- 
2.30.2


Reply via email to