1DIRNAME(P)                 POSIX Programmer's Manual                DIRNAME(P)
2
3
4

NAME

6       dirname - report the parent directory name of a file pathname
7

SYNOPSIS

9       #include <libgen.h>
10
11       char *dirname(char *path);
12
13

DESCRIPTION

15       The  dirname() function shall take a pointer to a character string that
16       contains a pathname, and return a pointer to a string that is  a  path‐
17       name  of  the parent directory of that file. Trailing '/' characters in
18       the path are not counted as part of the path.
19
20       If path does not contain a '/' , then dirname() shall return a  pointer
21       to  the  string  "."  . If path is a null pointer or points to an empty
22       string, dirname() shall return a pointer to the string "."  .
23
24       The dirname() function need not be reentrant. A function  that  is  not
25       required to be reentrant is not required to be thread-safe.
26

RETURN VALUE

28       The  dirname()  function shall return a pointer to a string that is the
29       parent directory of path. If path is a null pointer  or  points  to  an
30       empty string, a pointer to a string "." is returned.
31
32       The  dirname()  function  may modify the string pointed to by path, and
33       may return a pointer to static storage that may then be overwritten  by
34       subsequent calls to dirname().
35

ERRORS

37       No errors are defined.
38
39       The following sections are informative.
40

EXAMPLES

42       The following code fragment reads a pathname, changes the current work‐
43       ing directory to the parent directory, and opens the file.
44
45
46              char path[PATH_MAX], *pathcopy;
47              int fd;
48              fgets(path, PATH_MAX, stdin);
49              pathcopy = strdup(path);
50              chdir(dirname(pathcopy));
51              fd = open(basename(path), O_RDONLY);
52
53   Sample Input and Output Strings for dirname()
54       In the following table, the input string is the  value  pointed  to  by
55       path,  and the output string is the return value of the dirname() func‐
56       tion.
57
58                            Input String   Output String
59                            "/usr/lib"     "/usr"
60                            "/usr/"        "/"
61                            "usr"          "."
62                            "/"            "/"
63                            "."            "."
64                            ".."           "."
65
66   Changing the Current Directory to the Parent Directory
67       The following program fragment reads a pathname,  changes  the  current
68       working directory to the parent directory, and opens the file.
69
70
71              #include <unistd.h>
72              #include <limits.h>
73              #include <stdio.h>
74              #include <fcntl.h>
75              #include <string.h>
76              #include <libgen.h>
77              ...
78              char path[PATH_MAX], *pathcopy;
79              int fd;
80              ...
81              fgets(path, PATH_MAX, stdin);
82              pathcopy = strdup(path);
83              chdir(dirname(pathcopy));
84              fd = open(basename(path), O_RDONLY);
85

APPLICATION USAGE

87       The  dirname() and basename() functions together yield a complete path‐
88       name. The expression dirname(path) obtains the pathname of  the  direc‐
89       tory where basename(path) is found.
90
91       Since  the  meaning  of  the  leading  "//"  is implementation-defined,
92       dirname(" //foo) may return either "//" or '/' (but nothing else).
93

RATIONALE

95       None.
96

FUTURE DIRECTIONS

98       None.
99

SEE ALSO

101       basename() , the Base Definitions volume of IEEE Std 1003.1-2001, <lib‐
102       gen.h>
103
105       Portions  of  this text are reprinted and reproduced in electronic form
106       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
107       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
108       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
109       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
110       event of any discrepancy between this version and the original IEEE and
111       The  Open Group Standard, the original IEEE and The Open Group Standard
112       is the referee document. The original Standard can be  obtained  online
113       at http://www.opengroup.org/unix/online.html .
114
115
116
117IEEE/The Open Group                  2003                           DIRNAME(P)
Impressum