1DIRNAME(3P) POSIX Programmer's Manual DIRNAME(3P)
2
3
4
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
12 dirname — report the parent directory name of a file pathname
13
15 #include <libgen.h>
16
17 char *dirname(char *path);
18
20 The dirname() function shall take a pointer to a character string that
21 contains a pathname, and return a pointer to a string that is a path‐
22 name of the parent directory of that file. The dirname() function shall
23 not perform pathname resolution; the result shall not be affected by
24 whether or not path exists or by its file type. Trailing '/' characters
25 in the path that are not also leading '/' characters shall not be
26 counted as part of the path.
27
28 If path does not contain a '/', then dirname() shall return a pointer
29 to the string ".". If path is a null pointer or points to an empty
30 string, dirname() shall return a pointer to the string ".".
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 a subsequent call to dirname().
35
36 The dirname() function need not be thread-safe.
37
39 The dirname() function shall return a pointer to a string as described
40 above.
41
42 The dirname() function may modify the string pointed to by path, and
43 may return a pointer to internal storage. The returned pointer might be
44 invalidated or the storage might be overwritten by a subsequent call to
45 dirname(). The returned pointer might also be invalidated if the call‐
46 ing thread is terminated.
47
49 No errors are defined.
50
51 The following sections are informative.
52
54 The following code fragment reads a pathname, changes the current work‐
55 ing directory to the parent directory, and opens the file.
56
57
58 char *path = NULL, *pathcopy;
59 size_t buflen = 0;
60 ssize_t linelen = 0;
61 int fd;
62
63 linelen = getline(&path, &buflen, stdin);
64
65 path[linelen-1] = 0;
66 pathcopy = strdup(path);
67 if (chdir(dirname(pathcopy)) < 0) {
68 ...
69 }
70 if ((fd = open(basename(path), O_RDONLY)) >= 0) {
71 ...
72 close (fd);
73 }
74 ...
75 free (pathcopy);
76 free (path);
77
78 The EXAMPLES section of the basename() function (see basename())
79 includes a table showing examples of the results of processing several
80 sample pathnames by the basename() and dirname() functions and by the
81 basename and dirname utilities.
82
84 The dirname() and basename() functions together yield a complete path‐
85 name. The expression dirname(path) obtains the pathname of the direc‐
86 tory where basename(path) is found.
87
88 Since the meaning of the leading "//" is implementation-defined,
89 dirname("//foo) may return either "//" or '/' (but nothing else).
90
92 None.
93
95 None.
96
98 basename()
99
100 The Base Definitions volume of POSIX.1‐2017, <libgen.h>
101
102 The Shell and Utilities volume of POSIX.1‐2017, basename, dirname
103
105 Portions of this text are reprinted and reproduced in electronic form
106 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
107 table Operating System Interface (POSIX), The Open Group Base Specifi‐
108 cations Issue 7, 2018 Edition, Copyright (C) 2018 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 Any typographical or formatting errors that appear in this page are
116 most likely to have been introduced during the conversion of the source
117 files to man page format. To report such errors, see https://www.ker‐
118 nel.org/doc/man-pages/reporting_bugs.html .
119
120
121
122IEEE/The Open Group 2017 DIRNAME(3P)