linux-sysprog · beginner · ~15 min
Recognise reentrant (_r) library variants.
Library functions with internal static state (like strtok, localtime) have reentrant _r variants that take a caller-supplied buffer and are thread-safe. Detect the _r form by its name.
Implement int is_reentrant(const char *fn) that returns 1 if fn is a reentrant _r variant, else 0.
fn: a library function name string.1 if the name ends in _r (e.g. "strtok_r", "localtime_r"); 0 otherwise (e.g. "strtok", "localtime").
is_reentrant("strtok_r") -> 1
is_reentrant("localtime_r") -> 1
is_reentrant("strtok") -> 0
fn: a library function name string.
1 if the name ends in _r, else 0.
The reentrant variants end in the suffix _r.
#include <string.h>
int is_reentrant(const char *fn) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.