diff -r c2f4c0285180 -r f600b0d1fc5d bl/utils.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bl/utils.c Fri Jan 27 10:30:16 2012 +0000 @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include + +#define is_valid_drive(d) ((d)>='a' && (d)<='z' || (d)>='A' && (d)<='Z') + +/* + * Gets the last component of the filename. If filename ends with a directory + * separator it gets the component before the last slash. If filename consists + * only of directory separators (and on Windows, possibly a drive letter), a + * single separator is returned. If filename is empty, it gets ".". + */ +char *path_get_basename(const char *filename) +{ + ssize_t base,last_nonslash; + size_t len; + char *retval; + if (*filename=='\0') + return str_dup("."); + last_nonslash=strlen(filename)-1; + while (last_nonslash>=0 && BL_IS_DIR_SEPARATOR(filename[last_nonslash])) + last_nonslash--; + if (last_nonslash<0) + /* string only containing slashes */ + return str_dup(BL_DIR_SEPARATOR_S); +#ifdef WIN32 + if (last_nonslash==1 && is_valid_drive(filename[0]) && filename[1]==':') + /* string only containing slashes and a drive */ + return str_dup(BL_DIR_SEPARATOR_S); +#endif + base=last_nonslash; + while (base>=0 && !BL_IS_DIR_SEPARATOR(filename[base])) + base--; +#ifdef WIN32 + if (base==-1 && is_valid_drive(filename[0]) && filename[1] == ':') + base=1; +#endif + len=last_nonslash-base; + retval=mem_alloc(len+1,1); + memcpy(retval,filename+base+1,len); + retval[len]='\0'; + return retval; +}