From: Andre Noll Date: Sat, 29 Dec 2007 20:43:13 +0000 (+0100) Subject: Simplify para_basename(). X-Git-Tag: v0.3.0~19 X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=db0e72763ac5660b01c8c2db75901f4efa2c44cc;p=paraslash.git Simplify para_basename(). No need to allocate additional memory. This saves a malloc/free in the hot path of com_ls(), the only caller. Also take care for the case basename == NULL. --- diff --git a/aft.c b/aft.c index 51703aa4..6481f3e4 100644 --- a/aft.c +++ b/aft.c @@ -804,8 +804,8 @@ static char *make_image_lines(struct afs_info *afsi) static char *make_filename_lines(const char *path, unsigned flags) { - char *basename, *dirname; - char *ret; + char *dirname, *ret; + const char *basename; if (!(flags & LS_FLAG_FULL_PATH)) return make_message("%s: %s\n", @@ -814,9 +814,8 @@ static char *make_filename_lines(const char *path, unsigned flags) dirname = para_dirname(path); ret = make_message("%s: %s\n%s: %s\n%s: %s\n", status_item_list[SI_PATH], path, - status_item_list[SI_DIRECTORY], dirname, - status_item_list[SI_BASENAME], basename); - free(basename); + status_item_list[SI_DIRECTORY], dirname? dirname : "?", + status_item_list[SI_BASENAME], basename? basename : "?"); free(dirname); return ret; } diff --git a/string.c b/string.c index 8c97ccf7..2b361aee 100644 --- a/string.c +++ b/string.c @@ -189,29 +189,27 @@ __must_check __malloc char *para_dirname(const char *name) } /** - * paraslash's version of basename() + * Paraslash's version of basename(). * - * \param name Pointer to the full path + * \param name Pointer to the full path. * - * Compute the filename component of \p name + * Compute the filename component of \a name. * - * \return If \p name is \p NULL or the empty string, return \p NULL, - * Otherwise, make a copy of \p name and return its filename component. Caller - * is responsible to free the result. + * \return \p NULL if (a) \a name is the empty string of \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 __malloc char *para_basename(const char *name) +__must_check const char *para_basename(const char *name) { - char *p; + const char *ret; if (!name || !*name) return NULL; - p = strrchr(name, '/'); - if (!p) - return para_strdup(name); - p++; - if (!*p) - return NULL; - return para_strdup(p); + ret = strrchr(name, '/'); + if (!ret) + return name; + ret++; + return ret; } /** diff --git a/string.h b/string.h index eb29b919..febe1a02 100644 --- a/string.h +++ b/string.h @@ -23,7 +23,7 @@ __must_check __malloc char *para_strdup(const char *s); __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 __malloc char *para_basename(const char *name); +__must_check const char *para_basename(const char *name); void chop(char* buf); __must_check __malloc char *para_tmpname(void); __must_check int para_mkstemp(char *template, mode_t mode);