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

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       dirname - report the parent directory name of a file pathname
13

SYNOPSIS

15       #include <libgen.h>
16
17       char *dirname(char *path);
18
19

DESCRIPTION

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

RETURN VALUE

34       The dirname() function shall return a pointer to a string that  is  the
35       parent  directory  of  path.  If path is a null pointer or points to an
36       empty string, a pointer to a string "." is returned.
37
38       The dirname() function may modify the string pointed to  by  path,  and
39       may  return a pointer to static storage that may then be overwritten by
40       subsequent calls to dirname().
41

ERRORS

43       No errors are defined.
44
45       The following sections are informative.
46

EXAMPLES

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

APPLICATION USAGE

94       The dirname() and basename() functions together yield a complete  path‐
95       name.  The  expression dirname(path) obtains the pathname of the direc‐
96       tory where basename(path) is found.
97
98       Since the  meaning  of  the  leading  "//"  is  implementation-defined,
99       dirname(" //foo) may return either "//" or '/' (but nothing else).
100

RATIONALE

102       None.
103

FUTURE DIRECTIONS

105       None.
106

SEE ALSO

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