DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

dirname(S)


dirname -- report the parent directory name of a file path name

Synopsis

   cc [flag . . . ] file . . . -lgen [library] . . .
   

#include <libgen.h>

char *dirname(char *path);

Description

Given a pointer to a null-terminated character string that contains a file system path name, dirname returns a pointer to a static constant string that is the parent directory of that file. In doing this, it sometimes places a null byte in the path name after the next to last element, so the content of path must be disposable. Trailing ``/'' characters in the path are not counted as part of the path.

If path or *path does not contain ``/'', a pointer to a static constant . is returned.

dirname and basename(S) together yield a complete path name. dirname (path) is the directory where basename (path) is found.

References

basename(C), basename(S), chdir(S)

Examples

A simple file name and the strings ``.'' and ``..'' all have ``.'' as their return value.

Input string Output pointer
/usr/lib /usr
/usr/ /
usr .
/ /
. .
.. .

 Input string   Output pointer
 /usr/lib       /usr
 /usr/          /
 usr            .
 /              /
 .              .
 ..             .

The following code reads a path name, changes directory to the appropriate directory [see chdir(S)], and opens the file.

   char path[100], *pathcopy;
   int fd;
   gets (path);
   pathcopy = strdup (path);
   chdir (dirname (pathcopy) );
   fd = open (basename (path), O_RDONLY);

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 - 01 June 2005