linux-sysprog · beginner · ~15 min

Reentrant function variant

Recognise reentrant (_r) library variants.

Challenge

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.

Task

Implement int is_reentrant(const char *fn) that returns 1 if fn is a reentrant _r variant, else 0.

Input

  • fn: a library function name string.

Output

1 if the name ends in _r (e.g. "strtok_r", "localtime_r"); 0 otherwise (e.g. "strtok", "localtime").

Example

is_reentrant("strtok_r")      ->   1
is_reentrant("localtime_r")   ->   1
is_reentrant("strtok")        ->   0

Input format

fn: a library function name string.

Output format

1 if the name ends in _r, else 0.

Constraints

The reentrant variants end in the suffix _r.

Starter code

#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.