file-handling · beginner · ~10 min
Find the last separator and copy the tail safely.
Pull the filename out of a path — everything after the last / — without the surprising in-place behaviour of libc's basename().
Implement int basename_of(const char *path, char *out, size_t cap) that copies the part of path following the last / into out (NUL-terminated, bounded by cap).
path: a file path (NUL-terminated).out / cap: destination buffer and its size.Return the number of bytes written to out. A path with no / is its own basename. Return -1 if path/out is NULL or the result doesn't fit in cap.
basename_of("/a/b/file.txt", out, 64) -> 8, out = "file.txt"
basename_of("plain.c", out, 64) -> out = "plain.c"
basename_of("/etc/", out, 64) -> out = "" (trailing slash)
/ (e.g. strrchr) and copy what follows; bound the copy by cap.Pulling the filename out of a path is a constant chore — and doing it by hand avoids the surprising in-place behaviour of libc basename().
path: a file path. out/cap: result buffer and size.
int bytes written to out (final path component), or -1 on NULL / overflow.
Basename is the text after the last /; trailing slash gives an empty result.
#include <stddef.h>
int basename_of(const char *path, char *out, size_t cap) {
/* TODO */
(void)path; (void)out; (void)cap;
return -1;
}
Including the slash. Mishandling a trailing slash. No overflow guard.
No slash. Trailing slash → empty string. Tiny buffer.
O(path length).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.