]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Remove para_dirname() and para_basename().
authorAndre Noll <maan@tuebingen.mpg.de>
Fri, 11 Mar 2022 18:32:24 +0000 (19:32 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 21:01:05 +0000 (23:01 +0200)
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
string.c
string.h

diff --git a/aft.c b/aft.c
index 5f9098aa458955d1338bec6183c67493ce798cad..e0bde7fc78c8365f33aa8e5aae9f751a3dabb751 100644 (file)
--- 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))
index 198e9f1d286ee2228638a2a7994fe9e65f41ccac..f80331900de09fccf573c18666c3fa007f8ca6bf 100644 (file)
--- 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.
  *
index 10379a0e83098f392e8653467b826925376eda15..08b9965f57d94b52a19dd231c38b2d53aabb824a 100644 (file)
--- 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);