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)
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))
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.
*
__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);