aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index 99a2dd75..e8239994 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -9,6 +9,7 @@
*
*/
+#include <libgen.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
@@ -485,3 +486,37 @@ char *check_prefix(char *prefix)
free(prefix);
return new;
}
+
+
+char *safe_basename(const char *in)
+{
+ int i;
+ char *cpy;
+ char *res;
+
+ cpy = strdup(in);
+
+ /* Get rid of any trailing slashes */
+ for ( i=strlen(cpy)-1; i>0; i-- ) {
+ if ( cpy[i] == '/' ) {
+ cpy[i] = '\0';
+ } else {
+ break;
+ }
+ }
+
+ /* Find the base name */
+ for ( i=strlen(cpy)-1; i>=0; i-- ) {
+ if ( cpy[i] == '/' ) {
+ i++;
+ break;
+ }
+ }
+
+ res = strdup(cpy+i);
+ /* If we didn't find a previous slash, i==0 so res==cpy */
+
+ free(cpy);
+
+ return res;
+}