From aa37d3d42f37ace8dc196320f1677e4e14fe77c8 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 14 Jan 2012 15:17:08 +0000 Subject: [PATCH] LRN: add function GNUNET_STRINGS_get_short_name to get basename --- src/include/gnunet_strings_lib.h | 14 ++++++++++++++ src/util/strings.c | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h index edeccaf3b..8101a8113 100644 --- a/src/include/gnunet_strings_lib.h +++ b/src/include/gnunet_strings_lib.h @@ -199,6 +199,20 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t); char * GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta); +/** + * "man basename" + * Returns a pointer to a part of filename (allocates nothing)! + * + * @param filename filename to extract basename from + * @return short (base) name of the file (that is, everything following the + * last directory separator in filename. If filename ends with a + * directory separator, the result will be a zero-length string. + * If filename has no directory separators, the result is filename + * itself. + */ +const char * +GNUNET_STRINGS_get_short_name (const char *filename); + #if 0 /* keep Emacsens' auto-indent happy */ { #endif diff --git a/src/util/strings.c b/src/util/strings.c index bd0a8eb52..1a884bc75 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -607,6 +607,26 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) return ret; } - +/** + * "man basename" + * Returns a pointer to a part of filename (allocates nothing)! + * + * @param filename filename to extract basename from + * @return short (base) name of the file (that is, everything following the + * last directory separator in filename. If filename ends with a + * directory separator, the result will be a zero-length string. + * If filename has no directory separators, the result is filename + * itself. + */ +const char * +GNUNET_STRINGS_get_short_name (const char *filename) +{ + const char *short_fn = filename; + const char *ss; + while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) + && (ss[1] != '\0')) + short_fn = 1 + ss; + return short_fn; +} /* end of strings.c */ -- 2.25.1