basename(3) — Subroutines
NAME
basename, dirname, dirname_r − Returns the base filename or directory portion of a pathname
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <string.h>
char ∗ basename (
char ∗ path);
char ∗ dirname (
char ∗ path);
int dirname_r (
char ∗ path,
char ∗ buf,
int size);
PARAMETERS
pathThe string from which the base pathname or the directory portion is to be extracted.
bufSpecifies a working buffer to hold the directory name component.
sizeSpecifies the length of buf.
DESCRIPTION
The basename() and dirname() functions complement each other. The basename() function reads path and returns a pointer to the portion of the string following the last / (slash).
The dirname() function reads path and returns a pointer to a new string that consists of everything before the last / (slash) character. If there is no / (slash), the dirname() function returns NULL.
The dirname_r() function is the reentrant version of the dirname() function. The dirname_r() function places the directory name component in the buffer pointed at by buf. If there is no / (slash), the dirname() function returns -1.
RETURN VALUES
Upon success, the basename() and dirname() functions return the component string. Upon failure, these functions return NULL.
Upon success, the dirname_r() function returns 0 (zero); upon failure (for example, if there is no / (slash), the dirname() function returns NULL and the dirname_r() function returns -1 and sets errno.
ERRORS
If the dirname_r() function fails, errno may be set to the following:
[EINVAL]If buf is invalid or too small.
RELATED INFORMATION
Commands: basename(1)