]> Savannah Git Hosting - gnulib.git/commitdiff
dirname: prefer idx_t for some indexes
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Jun 2021 00:18:57 +0000 (17:18 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Jun 2021 00:20:11 +0000 (17:20 -0700)
* 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
lib/basename.c
modules/dirname

index 42d748a388804748ab9d92ecdeb9c1ace3462ad2..0fdbfe60b483d54ecc7b2b4713526e7b7709ef9b 100644 (file)
--- 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.
index 0ba28780c8f0c05e01bfb07f4ccd52fe5e9a8c93..1181134ed13a0b223a5312b9150c997831920259 100644 (file)
 
 #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;
 }
index e3ffbe04d7993a4c0f5a4104403c81ce42cde23c..483103eba5f6423bf6bdb09381ade3bf20242f24 100644 (file)
@@ -9,7 +9,6 @@ lib/stripslash.c
 Depends-on:
 dirname-lgpl
 xalloc
-xstrndup
 
 configure.ac:
 gl_MODULE_INDICATOR([dirname])