cybersecurity · intermediate · ~15 min · safe pentest lab
Parse an /etc/passwd line and test whether its UID field is 0 (root-equivalent).
An account with UID 0 has full root power. Parse one /etc/passwd line:
int is_uid0_line(const char *line);
The format is name:passwd:UID:GID:gecos:home:shell. Return 1 if the UID (third field) is exactly 0, else 0. Malformed lines return 0.
A single passwd line (no trailing newline required).
1 if UID==0, else 0.
Fewer than two colons before the UID, or a non-numeric/empty UID, returns 0.
#include <stddef.h>
/* /etc/passwd line "name:pw:UID:GID:gecos:home:shell". Return 1 if the UID field == 0. */
int is_uid0_line(const char *line){ (void)line; return 0; }
Using atoi on the whole line; not validating that the UID field is purely numeric.
00 equals 0; a line with only a name returns 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.