5 #include <gclib/strfuncs.h>
6 #include <gclib/utils.h>
8 #define is_valid_drive(d) ((d)>='a' && (d)<='z' || (d)>='A' && (d)<='Z')
11 * Gets the last component of the filename. If filename ends with a directory
12 * separator it gets the component before the last slash. If filename consists
13 * only of directory separators (and on Windows, possibly a drive letter), a
14 * single separator is returned. If filename is empty, it gets ".".
16 char *path_get_basename(const char *filename)
18 ssize_t base,last_nonslash;
23 last_nonslash=strlen(filename)-1;
24 while (last_nonslash>=0 && GC_IS_DIR_SEPARATOR(filename[last_nonslash]))
27 /* string only containing slashes */
28 return str_dup(GC_DIR_SEPARATOR_S);
30 if (last_nonslash==1 && is_valid_drive(filename[0]) && filename[1]==':')
31 /* string only containing slashes and a drive */
32 return str_dup(GC_DIR_SEPARATOR_S);
35 while (base>=0 && !GC_IS_DIR_SEPARATOR(filename[base]))
38 if (base==-1 && is_valid_drive(filename[0]) && filename[1] == ':')
41 len=last_nonslash-base;
42 retval=mem_alloc(len+1,1);
43 memcpy(retval,filename+base+1,len);