networking · beginner · ~15 min
Respect the unix-socket path length limit.
A Unix-domain socket address (struct sockaddr_un) stores its path in sun_path, a fixed 108-byte field (including the terminating NUL). Check whether a path fits before binding, since overlong paths get silently truncated.
Implement int path_fits_sun(const char *path) that returns 1 if strlen(path) < 108 (leaving room for the NUL), otherwise 0.
A NUL-terminated path string.
Returns 1 if the path fits in sun_path, else 0.
path_fits_sun("/tmp/app.sock") -> 1
path_fits_sun(<150-char path>) -> 0
< 108, not <= 108, because one byte is reserved for the NUL.A NUL-terminated path string.
1 if strlen(path) < 108, else 0.
sun_path holds 108 bytes including the NUL, so the path length must be < 108.
#include <string.h>
int path_fits_sun(const char *path) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.