cc [ flag ... ] file ... -lgen [ library ... ]
#include <libgen.h>
char *dirname(char *path);
MT-Safe
If path or *path is zero, a pointer to a static constant ‘‘.’’ is returned.
dirname() and basename() together yield a complete path name. dirname (path) is the directory where basename (path) is found.
Input string Output pointer /usr/lib /usr /usr/ / usr . / / . . .. .
The following code reads a path name, changes directory to the parent directory of the named file (see chdir(2) ), and opens the file.
char path[100], *pathcopy; int fd; gets (path); pathcopy = strdup (path); chdir (dirname (pathcopy) ); free (pathcopy); fd = open (basename (path), O_RDONLY);
When compiling multi-thread applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in multi-thread applications.