linux-sysprog · beginner · ~15 min

Is it an absolute path?

Distinguish absolute from relative paths.

Challenge

execv does not search $PATH, so it needs an absolute path — one that begins at the filesystem root with '/'.

Task

Implement int is_absolute_path(const char *p) that returns 1 if p is non-empty and its first character is '/', else 0.

Input

  • p: a path string (possibly empty).

Output

1 if p[0] == '/' (and p is non-empty), else 0.

Example

is_absolute_path("/bin/ls")   ->   1
is_absolute_path("ls")        ->   0
is_absolute_path("./a")       ->   0
is_absolute_path("")          ->   0

Edge cases

  • Empty string: return 0.
  • Relative (ls, ./a): return 0.

Input format

p: a path string, possibly empty.

Output format

1 if non-empty and starts with '/', else 0.

Starter code

int is_absolute_path(const char *p) {
    /* TODO */
    return 0;
}

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