linux-sysprog · beginner · ~15 min
Robust whitespace-delimited /proc parsing.
Pull the address range and permission string out of one line of /proc/self/maps — the memory map of a running process.
Implement int parse_maps_line(const char *line, unsigned long *start, unsigned long *end, char perms[5]) that parses a single maps line.
line: one map entry in the format start-end perms offset dev inode [path], e.g. 55b3e1e5e000-55b3e1e60000 r-xp 00000000 fd:00 12345 /usr/bin/cat. Addresses are hexadecimal; perms is 4 characters (r/-, w/-, x/-, p/s).start, end: where to store the parsed start and end addresses.perms: a 5-byte buffer for the 4 permission chars plus a NUL.On success, stores the start and end addresses in *start/*end, copies the 4 permission characters into perms (NUL-terminated), and returns 1. Returns 0 if the line does not parse. The offset, dev, inode, and path fields are ignored.
parse_maps_line("55b3e1e5e000-55b3e1e60000 r-xp 00000000 fd:00 12345 /usr/bin/cat", &s, &e, perms)
-> 1, s = 0x55b3e1e5e000, e = 0x55b3e1e60000, perms = "r-xp"
parse_maps_line("7ffff7ff5000-7ffff7ff7000 r--p 00000000 00:00 0", &s, &e, perms)
-> 1, perms = "r--p" (anonymous mapping, no path)
parse_maps_line("garbage", &s, &e, perms)
-> 0
0.perms must be NUL-terminated (5 bytes total).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 'start-end perms offset dev inode [path]', plus output pointers start, end and a 5-byte perms buffer.
Fills *start, *end, and the 4-char NUL-terminated perms; returns 1 on success, 0 on parse failure.
Addresses are hex; perms is 4 chars + NUL. Use sscanf and check it matched 3 fields.
#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.