From fefff8660daa86834673fdcc284ebe5d02c1d37b Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 11 Mar 2022 19:32:24 +0100 Subject: [PATCH] Remove para_dirname() and para_basename(). The former has only a single caller, the second only two, open-coding these is actually simpler and more performant because we no longer scan each path twice and avoid the temporary copy of the path. --- aft.c | 15 +++++++-------- string.c | 50 -------------------------------------------------- string.h | 2 -- 3 files changed, 7 insertions(+), 60 deletions(-) diff --git a/aft.c b/aft.c index 5f9098aa..e0bde7fc 100644 --- a/aft.c +++ b/aft.c @@ -779,18 +779,17 @@ static void write_image_items(struct para_buffer *b, struct afs_info *afsi) static void write_filename_items(struct para_buffer *b, const char *path, bool basename) { - char *val; + const char *slash; if (basename) { WRITE_STATUS_ITEM(b, SI_basename, "%s\n", path); return; } WRITE_STATUS_ITEM(b, SI_path, "%s\n", path); - val = para_basename(path); - WRITE_STATUS_ITEM(b, SI_basename, "%s\n", val? val : ""); - val = para_dirname(path); - WRITE_STATUS_ITEM(b, SI_directory, "%s\n", val? val : ""); - free(val); + slash = strrchr(path, '/'); + WRITE_STATUS_ITEM(b, SI_basename, "%s\n", slash? slash + 1 : path); + WRITE_STATUS_ITEM(b, SI_directory, "%.*s\n", + slash? (int)(slash - path) : (int)strlen(path), path); } static int print_chunk_table(struct ls_data *d, struct para_buffer *b) @@ -897,13 +896,13 @@ static int print_list_item(struct ls_data *d, struct ls_options *opts, goto out; } if (opts->mode == LS_MODE_MBOX) { - const char *bn = para_basename(d->path); + const char *slash = strrchr(d->path, '/'); para_printf(b, "From foo@localhost %s\n" "Received: from\nTo: bar\nFrom: a\n" "Subject: %s\n\n", last_played_time, - bn? bn : "?"); + slash? slash + 1 : "?"); } write_filename_items(b, d->path, lls_opt_given(r_b)); if (lls_opt_given(r_a)) diff --git a/string.c b/string.c index 198e9f1d..f8033190 100644 --- a/string.c +++ b/string.c @@ -245,56 +245,6 @@ __must_check __malloc char *para_strcat(char *a, const char *b) return tmp; } -/** - * Paraslash's version of dirname(). - * - * \param name Pointer to the full path. - * - * Compute the directory component of \p name. - * - * \return If \a name is \p NULL or the empty string, return \p NULL. - * Otherwise, Make a copy of \a name and return its directory component. Caller - * is responsible to free the result. - */ -__must_check __malloc char *para_dirname(const char *name) -{ - char *p, *ret; - - if (!name || !*name) - return NULL; - ret = para_strdup(name); - p = strrchr(ret, '/'); - if (!p) - *ret = '\0'; - else - *p = '\0'; - return ret; -} - -/** - * Paraslash's version of basename(). - * - * \param name Pointer to the full path. - * - * Compute the filename component of \a name. - * - * \return \p NULL if (a) \a name is the empty string or \p NULL, or (b) name - * ends with a slash. Otherwise, a pointer within \a name is returned. Caller - * must not free the result. - */ -__must_check char *para_basename(const char *name) -{ - char *ret; - - if (!name || !*name) - return NULL; - ret = strrchr(name, '/'); - if (!ret) - return (char *)name; - ret++; - return ret; -} - /** * Get the logname of the current user. * diff --git a/string.h b/string.h index 10379a0e..08b9965f 100644 --- a/string.h +++ b/string.h @@ -76,8 +76,6 @@ __printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap); __printf_2_3 unsigned xasprintf(char **result, const char *fmt, ...); __must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...); __must_check __malloc char *para_strcat(char *a, const char *b); -__must_check __malloc char *para_dirname(const char *name); -__must_check char *para_basename(const char *name); __must_check __malloc char *para_logname(void); __must_check __malloc char *para_homedir(void); __malloc char *para_hostname(void); -- 2.39.2