From: Paul Eggert <eggert@cs.ucla.edu>
To: bug-gnulib@gnu.org
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: [PATCH 03/13] dirname: prefer idx_t for some indexes
Date: Fri, 11 Jun 2021 17:25:43 -0700 [thread overview]
Message-ID: <20210612002553.1105537-3-eggert@cs.ucla.edu> (raw)
In-Reply-To: <20210612002553.1105537-1-eggert@cs.ucla.edu>
* 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
next prev parent reply other threads:[~2021-06-12 0:26 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
2021-06-12 0:25 ` [PATCH 02/13] xalloc: new idx_t-based allocators Paul Eggert
2021-06-12 0:25 ` Paul Eggert [this message]
2021-06-12 0:25 ` [PATCH 04/13] dfa: prefer idx_t for indexes Paul Eggert
2021-06-12 0:25 ` [PATCH 05/13] exclude: prefer idx_t for most indexes Paul Eggert
2021-06-12 0:25 ` [PATCH 06/13] getusershell: prefer idx_t for indexes Paul Eggert
2021-06-12 0:25 ` [PATCH 07/13] linebuffer: " Paul Eggert
2021-06-12 0:25 ` [PATCH 08/13] readtokens: " Paul Eggert
2021-06-12 0:25 ` [PATCH 09/13] readutmp: " Paul Eggert
2021-06-12 0:25 ` [PATCH 10/13] savedir: " Paul Eggert
2021-06-12 0:25 ` [PATCH 11/13] stack: " Paul Eggert
2021-06-13 10:19 ` Bruno Haible
2021-06-13 17:47 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 12/13] userspec: " Paul Eggert
2021-06-12 0:25 ` [PATCH 13/13] xgethostname: " Paul Eggert
2021-06-13 10:05 ` [PATCH 01/13] ialloc: new module Bruno Haible
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210612002553.1105537-3-eggert@cs.ucla.edu \
--to=eggert@cs.ucla.edu \
--cc=bug-gnulib@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).