cybersecurity · beginner · ~20 min

Pattern-match log file rotation candidates

Strict prefix matching.

Challenge

Given a base log name name (e.g. app.log) and a candidate filename candidate, implement int is_rotated_log(const char *name, const char *candidate) that returns 1 iff candidate matches name exactly OR starts with name + "." followed by at least one character. (E.g. app.log, app.log.1, app.log.2024-01-15.gz all match; app.log. does not.)

Why this matters

A common pattern in incident response: scan a directory listing for rotated log files (app.log.1, app.log.2024-01-15, etc.) so you can pull the right archive. Building a simple matcher teaches glob-like patterns the disciplined way.

Input format

Two null-terminated strings.

Output format

0 or 1.

Constraints

No regex.

Starter code

int is_rotated_log(const char *name, const char *candidate) { /* TODO */ return 0; }

Common mistakes

Treating app.log2 as a rotation (must require literal . between name and suffix); accepting app.log. with no trailing chars (degenerate case).

Edge cases to handle

Exact match. Empty candidate. Candidate shorter than name.

Complexity

O(strlen).

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