linux-sysprog · beginner · ~15 min

Parse a /proc/self/maps line

Robust whitespace-delimited /proc parsing.

Challenge

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.

Why this matters

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.

Input format

One /proc/self/maps line.

Output format

0/1 + filled outputs.

Constraints

Use sscanf.

Starter code

#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; }

Common mistakes

Reading perms as 4 chars via %4s and forgetting NUL.

Edge cases to handle

Line with no trailing path (anonymous mapping).

Complexity

O(strlen).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.