1REALPATH(3P) POSIX Programmer's Manual REALPATH(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
11
13 realpath — resolve a pathname
14
16 #include <stdlib.h>
17
18 char *realpath(const char *restrict file_name,
19 char *restrict resolved_name);
20
22 The realpath() function shall derive, from the pathname pointed to by
23 file_name, an absolute pathname that resolves to the same directory
24 entry, whose resolution does not involve '.', '..', or symbolic links.
25 If resolved_name is a null pointer, the generated pathname shall be
26 stored as a null-terminated string in a buffer allocated as if by a
27 call to malloc(). Otherwise, if {PATH_MAX} is defined as a constant in
28 the <limits.h> header, then the generated pathname shall be stored as a
29 null-terminated string, up to a maximum of {PATH_MAX} bytes, in the
30 buffer pointed to by resolved_name.
31
32 If resolved_name is not a null pointer and {PATH_MAX} is not defined as
33 a constant in the <limits.h> header, the behavior is undefined.
34
36 Upon successful completion, realpath() shall return a pointer to the
37 buffer containing the resolved name. Otherwise, realpath() shall
38 return a null pointer and set errno to indicate the error.
39
40 If the resolved_name argument is a null pointer, the pointer returned
41 by realpath() can be passed to free().
42
43 If the resolved_name argument is not a null pointer and the realpath()
44 function fails, the contents of the buffer pointed to by resolved_name
45 are undefined.
46
48 The realpath() function shall fail if:
49
50 EACCES Search permission was denied for a component of the path prefix
51 of file_name.
52
53 EINVAL The file_name argument is a null pointer.
54
55 EIO An error occurred while reading from the file system.
56
57 ELOOP A loop exists in symbolic links encountered during resolution of
58 the file_name argument.
59
60 ENAMETOOLONG
61 The length of a component of a pathname is longer than
62 {NAME_MAX}.
63
64 ENOENT A component of file_name does not name an existing file or
65 file_name points to an empty string.
66
67 ENOTDIR
68 A component of the path prefix names an existing file that is
69 neither a directory nor a symbolic link to a directory, or the
70 file_name argument contains at least one non-<slash> character
71 and ends with one or more trailing <slash> characters and the
72 last pathname component names an existing file that is neither a
73 directory nor a symbolic link to a directory.
74
75 The realpath() function may fail if:
76
77 EACCES The file_name argument does not begin with a <slash> and none of
78 the symbolic links (if any) processed during pathname resolution
79 of file_name had contents that began with a <slash>, and either
80 search permission was denied for the current directory or read
81 or search permission was denied for a directory above the cur‐
82 rent directory in the file hierarchy.
83
84 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
85 resolution of the file_name argument.
86
87 ENAMETOOLONG
88 The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
89 tion of a symbolic link produced an intermediate result with a
90 length that exceeds {PATH_MAX}.
91
92 ENOMEM Insufficient storage space is available.
93
94 The following sections are informative.
95
97 Generating an Absolute Pathname
98 The following example generates an absolute pathname for the file iden‐
99 tified by the symlinkpath argument. The generated pathname is stored in
100 the buffer pointed to by actualpath.
101
102 #include <stdlib.h>
103 ...
104 char *symlinkpath = "/tmp/symlink/file";
105 char *actualpath;
106
107 actualpath = realpath(symlinkpath, NULL);
108 if (actualpath != NULL)
109 {
110 ... use actualpath ...
111
112 free(actualpath);
113 }
114 else
115 {
116 ... handle error ...
117 }
118
120 For functions that allocate memory as if by malloc(), the application
121 should release such memory when it is no longer required by a call to
122 free(). For realpath(), this is the return value.
123
125 Since realpath() has no length argument, if {PATH_MAX} is not defined
126 as a constant in <limits.h>, applications have no way of determining
127 how large a buffer they need to allocate for it to be safe to pass to
128 realpath(). A {PATH_MAX} value obtained from a prior pathconf() call
129 is out-of-date by the time realpath() is called. Hence the only reli‐
130 able way to use realpath() when {PATH_MAX} is not defined in <limits.h>
131 is to pass a null pointer for resolved_name so that realpath() will
132 allocate a buffer of the necessary size.
133
135 None.
136
138 fpathconf(), free(), getcwd(), sysconf()
139
140 The Base Definitions volume of POSIX.1‐2008, <limits.h>, <stdlib.h>
141
143 Portions of this text are reprinted and reproduced in electronic form
144 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
145 -- Portable Operating System Interface (POSIX), The Open Group Base
146 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
147 cal and Electronics Engineers, Inc and The Open Group. (This is
148 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
149 event of any discrepancy between this version and the original IEEE and
150 The Open Group Standard, the original IEEE and The Open Group Standard
151 is the referee document. The original Standard can be obtained online
152 at http://www.unix.org/online.html .
153
154 Any typographical or formatting errors that appear in this page are
155 most likely to have been introduced during the conversion of the source
156 files to man page format. To report such errors, see https://www.ker‐
157 nel.org/doc/man-pages/reporting_bugs.html .
158
159
160
161IEEE/The Open Group 2013 REALPATH(3P)