1
0
mirror of https://https.git.savannah.gnu.org/git/gnulib.git synced 2026-09-01 02:34:55 +00:00

dirname: prefer idx_t for some indexes

* 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.
This commit is contained in:
Paul Eggert
2021-06-11 17:18:57 -07:00
parent 61d866244d
commit 40a031df08
3 changed files with 33 additions and 23 deletions
+5
View File
@@ -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.
+28 -22
View File
@@ -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;
}
-1
View File
@@ -9,7 +9,6 @@ lib/stripslash.c
Depends-on:
dirname-lgpl
xalloc
xstrndup
configure.ac:
gl_MODULE_INDICATOR([dirname])