cybersecurity · intermediate · ~15 min
`stat` and POSIX permission bits.
Check whether a file is world-writable by inspecting its permission bits with stat.
Implement int is_world_writable(const char *path) that returns 1 if the file at path has the world-writable bit (S_IWOTH) set, 0 if not, and -1 if stat fails. No main — the grader calls it.
path: the file to inspect.
1 (world-writable), 0 (not), or -1 (stat failed, e.g. the file does not exist).
chmod 0666 file -> is_world_writable("file") = 1
chmod 0644 file -> is_world_writable("file") = 0
is_world_writable("does-not-exist") -> -1
stat error) returns -1.stat and test st.st_mode & S_IWOTH.A file path.
1 if world-writable, 0 if not, -1 if stat fails.
Use stat and test st_mode against S_IWOTH.
#include <sys/stat.h>
int is_world_writable(const char *path) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.