linux-sysprog · beginner · ~15 min
Robust whitespace-delimited /proc parsing.
Each /proc/self/maps line looks like:
55b3e1e5e000-55b3e1e60000 r-xp 00000000 fd:00 12345 /usr/bin/cat
The fields, separated by whitespace: start-end perms offset dev inode [path].
Implement
int parse_maps_line(const char *line, unsigned long *start, unsigned long *end, char perms[5]).
perms is 4 characters of r/-, w/-, x/-, p/s — copy them as a NUL-terminated 5-char string.
Return 1 on success, 0 on parse failure. Ignore offset/dev/inode/path.
Reading /proc/self/maps is how every memory-analysis tool starts. The line format is the foundation; you can build a 'where is libc?' lookup in 30 lines.
One /proc/self/maps line.
0/1 + filled outputs.
Use sscanf.
#include <stddef.h>
int parse_maps_line(const char *line, unsigned long *start, unsigned long *end, char perms[5]) { /* TODO */ (void)line; (void)start; (void)end; (void)perms; return 0; }
Reading perms as 4 chars via %4s and forgetting NUL.
Line with no trailing path (anonymous mapping).
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.