1LINK(P) POSIX Programmer's Manual LINK(P)
2
3
4
6 link - link to a file
7
9 #include <unistd.h>
10
11 int link(const char *path1, const char *path2);
12
13
15 The link() function shall create a new link (directory entry) for the
16 existing file, path1.
17
18 The path1 argument points to a pathname naming an existing file. The
19 path2 argument points to a pathname naming the new directory entry to
20 be created. The link() function shall atomically create a new link for
21 the existing file and the link count of the file shall be incremented
22 by one.
23
24 If path1 names a directory, link() shall fail unless the process has
25 appropriate privileges and the implementation supports using link() on
26 directories.
27
28 Upon successful completion, link() shall mark for update the st_ctime
29 field of the file. Also, the st_ctime and st_mtime fields of the direc‐
30 tory that contains the new entry shall be marked for update.
31
32 If link() fails, no link shall be created and the link count of the
33 file shall remain unchanged.
34
35 The implementation may require that the calling process has permission
36 to access the existing file.
37
39 Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
40 returned and errno set to indicate the error.
41
43 The link() function shall fail if:
44
45 EACCES A component of either path prefix denies search permission, or
46 the requested link requires writing in a directory that denies
47 write permission, or the calling process does not have permis‐
48 sion to access the existing file and this is required by the
49 implementation.
50
51 EEXIST The path2 argument resolves to an existing file or refers to a
52 symbolic link.
53
54 ELOOP A loop exists in symbolic links encountered during resolution of
55 the path1 or path2 argument.
56
57 EMLINK The number of links to the file named by path1 would exceed
58 {LINK_MAX}.
59
60 ENAMETOOLONG
61 The length of the path1 or path2 argument exceeds {PATH_MAX} or
62 a pathname component is longer than {NAME_MAX}.
63
64 ENOENT A component of either path prefix does not exist; the file named
65 by path1 does not exist; or path1 or path2 points to an empty
66 string.
67
68 ENOSPC The directory to contain the link cannot be extended.
69
70 ENOTDIR
71 A component of either path prefix is not a directory.
72
73 EPERM The file named by path1 is a directory and either the calling
74 process does not have appropriate privileges or the implementa‐
75 tion prohibits using link() on directories.
76
77 EROFS The requested link requires writing in a directory on a read-
78 only file system.
79
80 EXDEV The link named by path2 and the file named by path1 are on dif‐
81 ferent file systems and the implementation does not support
82 links between file systems.
83
84 EXDEV path1 refers to a named STREAM.
85
86
87 The link() function may fail if:
88
89 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
90 resolution of the path1 or path2 argument.
91
92 ENAMETOOLONG
93 As a result of encountering a symbolic link in resolution of the
94 path1 or path2 argument, the length of the substituted pathname
95 string exceeded {PATH_MAX}.
96
97
98 The following sections are informative.
99
101 Creating a Link to a File
102 The following example shows how to create a link to a file named
103 /home/cnd/mod1 by creating a new directory entry named /modules/pass1.
104
105
106 #include <unistd.h>
107
108
109 char *path1 = "/home/cnd/mod1";
110 char *path2 = "/modules/pass1";
111 int status;
112 ...
113 status = link (path1, path2);
114
115 Creating a Link to a File Within a Program
116 In the following program example, the link() function links the
117 /etc/passwd file (defined as PASSWDFILE) to a file named /etc/opasswd
118 (defined as SAVEFILE), which is used to save the current password file.
119 Then, after removing the current password file (defined as PASSWDFILE),
120 the new password file is saved as the current password file using the
121 link() function again.
122
123
124 #include <unistd.h>
125
126
127 #define LOCKFILE "/etc/ptmp"
128 #define PASSWDFILE "/etc/passwd"
129 #define SAVEFILE "/etc/opasswd"
130 ...
131 /* Save current password file */
132 link (PASSWDFILE, SAVEFILE);
133
134
135 /* Remove current password file. */
136 unlink (PASSWDFILE);
137
138
139 /* Save new password file as current password file. */
140 link (LOCKFILE,PASSWDFILE);
141
143 Some implementations do allow links between file systems.
144
146 Linking to a directory is restricted to the superuser in most histori‐
147 cal implementations because this capability may produce loops in the
148 file hierarchy or otherwise corrupt the file system. This volume of
149 IEEE Std 1003.1-2001 continues that philosophy by prohibiting link()
150 and unlink() from doing this. Other functions could do it if the imple‐
151 mentor designed such an extension.
152
153 Some historical implementations allow linking of files on different
154 file systems. Wording was added to explicitly allow this optional
155 behavior.
156
157 The exception for cross-file system links is intended to apply only to
158 links that are programmatically indistinguishable from "hard" links.
159
161 None.
162
164 symlink() , unlink() , the Base Definitions volume of
165 IEEE Std 1003.1-2001, <unistd.h>
166
168 Portions of this text are reprinted and reproduced in electronic form
169 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
170 -- Portable Operating System Interface (POSIX), The Open Group Base
171 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
172 Electrical and Electronics Engineers, Inc and The Open Group. In the
173 event of any discrepancy between this version and the original IEEE and
174 The Open Group Standard, the original IEEE and The Open Group Standard
175 is the referee document. The original Standard can be obtained online
176 at http://www.opengroup.org/unix/online.html .
177
178
179
180IEEE/The Open Group 2003 LINK(P)