networking · beginner · ~15 min

Does the path fit in sun_path?

Respect the unix-socket path length limit.

Challenge

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.

Task

Implement int path_fits_sun(const char *path) that returns 1 if strlen(path) < 108 (leaving room for the NUL), otherwise 0.

Input

A NUL-terminated path string.

Output

Returns 1 if the path fits in sun_path, else 0.

Example

path_fits_sun("/tmp/app.sock")   ->   1
path_fits_sun(<150-char path>)   ->   0

Edge cases

  • The limit is < 108, not <= 108, because one byte is reserved for the NUL.

Input format

A NUL-terminated path string.

Output format

1 if strlen(path) < 108, else 0.

Constraints

sun_path holds 108 bytes including the NUL, so the path length must be < 108.

Starter code

#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.