linux-sysprog · beginner · ~10 min
Parse and validate a small bracketed integer prefix.
int syslog_severity(const char *line);
A syslog line begins with <PRI> where PRI = facility×8 + severity (0–191).
Return the severity (PRI % 8, 0–7), or -1 if the line doesn't start with
<, has no digits, has no closing >, has more than 3 digits, or PRI > 191.
<, read up to 3 digits, then require >.Every syslog line starts with <PRI>; severity = PRI mod 8. Triage pipelines read it constantly.
int syslog_severity(const char *line) {
/* TODO */
(void)line;
return -1;
}
Forgetting the closing '>'. Allowing PRI > 191. Not capping digit count.
PRI 0 and 191 (the extremes). Missing bracket. Overlong number.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.