Use ## to create sections (Learn more)
* build a path from an entry upto the root using recursion
* it appends '/' after strlcat, therefore buf needs to be prepared with '/'
* and it will leave a trailing '/'
* returns the position of that trailing '/' so it can be deleted afterwards
* (or, in case of truncation, the position of the nul byte */
static size_t copy_path_helper(const struct dircache_entry *entry, char *buf, size_t size)
int offset = 1;
/* has parent? */
offset += copy_path_helper(entry->up, buf, size);
size_t len = strlcpy(buf+offset, entry->d_name, size - offset) + offset;
if (len < size)
buf[len++] = '/';
buf[len] = '\0';
* Function to copy the full absolute path from dircache to the given buffer
* using the given dircache_entry pointer.
* Returns the size of the resulting string, or 0 if an error occured
size_t dircache_copy_path(int index, char *buf, size_t size)
if (!size || !buf || index < 0)
buf = '/';
size_t res = copy_path_helper(&dircache_root[index], buf, size - 1);
/* fixup trailing '/' */
buf[res] = '\0';
Share the private access URL responsibly.
Anyone will be able to find and view this paste. Google will index it.
With great power comes great responsibility.Pastie wisely.