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
11

NAME

13       dirname — report the parent directory name of a file pathname
14

SYNOPSIS

16       #include <libgen.h>
17
18       char *dirname(char *path);
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 thread-safe.
31

RETURN VALUE

33       The  dirname()  function shall return a pointer to a string that is the
34       parent directory of path.  If path is a null pointer or  points  to  an
35       empty string, a pointer to a string "." is returned.
36
37       The  dirname()  function  may modify the string pointed to by path, and
38       may return a pointer to internal storage. The returned pointer might be
39       invalidated or the storage might be overwritten by a subsequent call to
40       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           char *path = NULL, *pathcopy;
52           size_t buflen = 0;
53           ssize_t linelen = 0;
54           int fd;
55
56           linelen = getline(&path, &buflen, stdin);
57
58           path[linelen-1] = 0;
59           pathcopy = strdup(path);
60           if (chdir(dirname(pathcopy)) < 0) {
61               ...
62           }
63           if ((fd = open(basename(path), O_RDONLY)) >= 0) {
64               ...
65               close (fd);
66           }
67           ...
68           free (pathcopy);
69           free (path);
70
71   Sample Input and Output Strings for dirname()
72       In  the  following  table,  the input string is the value pointed to by
73       path, and the output string is the return value of the dirname()  func‐
74       tion.
75
76                           ┌─────────────┬───────────────┐
77Input String Output String 
78                           ├─────────────┼───────────────┤
79                           │"/usr/lib"   │ "/usr"        │
80                           │"/usr/"      │ "/"           │
81                           │"usr"        │ "."           │
82                           │"/"          │ "/"           │
83                           │"."          │ "."           │
84                           │".."         │ "."           │
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()
102
103       The Base Definitions volume of POSIX.1‐2008, <libgen.h>
104
106       Portions of this text are reprinted and reproduced in  electronic  form
107       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
108       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
109       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
110       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
111       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
112       event of any discrepancy between this version and the original IEEE and
113       The  Open Group Standard, the original IEEE and The Open Group Standard
114       is the referee document. The original Standard can be  obtained  online
115       at http://www.unix.org/online.html .
116
117       Any  typographical  or  formatting  errors that appear in this page are
118       most likely to have been introduced during the conversion of the source
119       files  to  man page format. To report such errors, see https://www.ker
120       nel.org/doc/man-pages/reporting_bugs.html .
121
122
123
124IEEE/The Open Group                  2013                          DIRNAME(3P)
Impressum