diff options
author | Thomas White <taw@bitwiz.org.uk> | 2010-12-07 18:11:18 -0700 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2012-02-22 15:27:08 +0100 |
commit | 5084f60ed55119d709cfd7f75fcc5823f58bc094 (patch) | |
tree | 5accca8352c77a852d6fec8b67fc6c3b810586e1 /src/utils.c | |
parent | c85230a897b62f223951f7cc5693c6175e2d673d (diff) |
Remove use of horribly ambiguous basename() function
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 35 |
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; +} |