#include "dirname.h" #include #include /* Return the address of the last file name component of |name|. If |name| has no relative file name components because it is a file system root, return the empty string. */ char * last_component(char const *name) { char const *base = name + 0; char const *p; bool saw_slash = false; while (((*base) == '/')) base++; for (p = base; *p; p++) { if (((*p) == '/')) saw_slash = true; else if (saw_slash) { base = p; saw_slash = false; } } return (char *) base; } /* Return the length of the basename |name|. Typically |name| is the value returned by |base_name| or |last_component|. Act like |strlen(name)|, except omit all trailing slashes. */ size_t base_len(char const *name) { size_t len; for (len = strlen(name); 1 < len && ((name[len - 1]) == '/'); len--) continue; return len; }