]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/link.2
xnu-3248.60.10.tar.gz
[apple/xnu.git] / bsd / man / man2 / link.2
CommitLineData
9bccf70c
A
1.\" $NetBSD: link.2,v 1.7 1995/02/27 12:34:01 cgd Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)link.2 8.3 (Berkeley) 1/12/94
35.\"
b0d623f7 36.Dd October 29, 2008
9bccf70c
A
37.Dt LINK 2
38.Os BSD 4
39.Sh NAME
fe8ab488
A
40.Nm link ,
41.Nm linkat
9bccf70c
A
42.Nd make a hard file link
43.Sh SYNOPSIS
44.Fd #include <unistd.h>
45.Ft int
2d21ac55
A
46.Fo link
47.Fa "const char *path1"
48.Fa "const char *path2"
49.Fc
fe8ab488
A
50.Ft int
51.Fo linkat
52.Fa "int fd1" "const char *name1" "int fd2" "const char *name2" "int flag"
53.Fc
9bccf70c
A
54.Sh DESCRIPTION
55The
56.Fn link
57function call
58atomically creates the specified directory entry (hard link)
2d21ac55 59.Fa path2
9bccf70c 60with the attributes of the underlying object pointed at by
2d21ac55
A
61.Fa path1 .
62If the link is successful,
63the link count of the underlying object is incremented;
64.Fa path1
9bccf70c 65and
2d21ac55 66.Fa path2
9bccf70c
A
67share equal access and rights
68to the
69underlying object.
70.Pp
71If
2d21ac55 72.Fa path1
9bccf70c 73is removed, the file
2d21ac55 74.Fa path2
9bccf70c
A
75is not deleted and the link count of the
76underlying object is
77decremented.
78.Pp
2d21ac55
A
79In order for the system call to succeed,
80.Fa path1
81must exist and both
82.Fa path1
9bccf70c 83and
2d21ac55 84.Fa path2
9bccf70c 85must be in the same file system.
2d21ac55
A
86As mandated by POSIX.1,
87.Fa path1
9bccf70c 88may not be a directory.
b0d623f7
A
89.Pp
90.Fn link
91will resolve and follow symbolic links contained within both
92.Fa path1
93and
94.Fa path2 .
95If the last component of
96.Fa path1
97is a symbolic link,
98.Fn link
99will point the hard link,
100.Fa path2 ,
101to the underlying object pointed to by
102.Fa path1 ,
103not to the symbolic link itself.
fe8ab488
A
104.Pp
105The
106.Fn linkat
107system call is equivalent to
108.Fa link
109except in the case where either
110.Fa name1
111or
112.Fa name2
113or both are relative paths.
114In this case a relative path
115.Fa name1
116is interpreted relative to
117the directory associated with the file descriptor
118.Fa fd1
119instead of the current working directory and similarly for
120.Fa name2
121and the file descriptor
122.Fa fd2 .
123.Pp
124Values for
125.Fa flag
126are constructed by a bitwise-inclusive OR of flags from the following
127list, defined in
128.In fcntl.h :
129.Bl -tag -width indent
130.It Dv AT_SYMLINK_FOLLOW
131If
132.Fa name1
133names a symbolic link, a new link for the target of the symbolic link is
134created.
135.El
136.Pp
137If
138.Fn linkat
139is passed the special value
140.Dv AT_FDCWD
141in the
142.Fa fd1
143or
144.Fa fd2
145parameter, the current working directory is used for the respective
146.Fa name
147argument.
148If both
149.Fa fd1
150and
151.Fa fd2
152have value
153.Dv AT_FDCWD ,
154the behavior is identical to a call to
155.Fn link .
156Unless
157.Fa flag
158contains the
159.Dv AT_SYMLINK_FOLLOW
160flag, if
161.Fa name1
162names a symbolic link, a new link is created for the symbolic link
163.Fa name1
164and not its target. On OS X, not assigning AT_SYMLINK_FOLLOW to
165.Fa flag
166may result in some filesystems returning an error.
9bccf70c
A
167.Sh RETURN VALUES
168Upon successful completion, a value of 0 is returned. Otherwise,
169a value of -1 is returned and
170.Va errno
171is set to indicate the error.
172.Sh ERRORS
173.Fn Link
174will fail and no link will be created if:
175.Bl -tag -width Er
2d21ac55
A
176.\" ==========
177.It Bq Er EACCES
178A component of either path prefix denies search permission.
179.\" ==========
180.It Bq Er EACCES
181The requested link requires writing in a directory with a mode
182that denies write permission.
183.\" ==========
184.It Bq Er EACCES
185The current process cannot access the existing file.
186.\" ==========
187.It Bq Er EDQUOT
188The directory in which the entry for the new link
189is being placed cannot be extended because the
190user's quota of disk blocks on the file system
191containing the directory has been exhausted.
192.\" ==========
193.It Bq Er EEXIST
194The link named by
195.Fa path2
196already exists.
197.\" ==========
198.It Bq Er EFAULT
199One of the pathnames specified
200is outside the process's allocated address space.
201.\" ==========
202.It Bq Er EIO
203An I/O error occurs while reading from or writing to
204the file system to make the directory entry.
205.\" ==========
206.It Bq Er ELOOP
207Too many symbolic links are encountered in translating one of the pathnames.
208This is taken to be indicative of a looping symbolic link.
209.\" ==========
210.It Bq Er EMLINK
211The file already has {LINK_MAX} links.
212.\" ==========
9bccf70c 213.It Bq Er ENAMETOOLONG
2d21ac55 214A component of a pathname exceeds
9bccf70c
A
215.Dv {NAME_MAX}
216characters, or an entire path name exceeded
217.Dv {PATH_MAX}
218characters.
2d21ac55 219.\" ==========
9bccf70c 220.It Bq Er ENOENT
b0d623f7 221A component of either path prefix does not exist, or is a dangling symbolic link.
2d21ac55 222.\" ==========
9bccf70c
A
223.It Bq Er ENOENT
224The file named by
2d21ac55 225.Fa path1
b0d623f7 226does not exist, or is a dangling symbolic link.
2d21ac55
A
227.\" ==========
228.It Bq Er ENOSPC
229The directory in which the entry for the new link is being placed
230cannot be extended because there is no space left on the file
231system containing the directory.
232.\" ==========
233.It Bq Er ENOTDIR
234A component of either path prefix is not a directory.
235.\" ==========
9bccf70c
A
236.It Bq Er EPERM
237The file named by
2d21ac55 238.Fa path1
9bccf70c 239is a directory.
2d21ac55
A
240.\" ==========
241.It Bq Er EROFS
242The requested link requires writing in a directory
243on a read-only file system.
244.\" ==========
9bccf70c
A
245.It Bq Er EXDEV
246The link named by
2d21ac55 247.Fa path2
9bccf70c 248and the file named by
2d21ac55 249.Fa path1
9bccf70c 250are on different file systems.
9bccf70c 251.El
fe8ab488
A
252.Pp
253In addition to the errors returned by the
254.Fn link ,
255the
256.Fn linkat
257system call may fail if:
258.Bl -tag -width Er
259.It Bq Er EBADF
260The
261.Fa name1
262or
263.Fa name2
264argument does not specify an absolute path and the
265.Fa fd1
266or
267.Fa fd2
268argument, respectively, is neither
269.Dv AT_FDCWD
270nor a valid file descriptor open for searching.
271.It Bq Er EINVAL
272The value of the
273.Fa flag
274argument is not valid.
275.It Bq Er ENOTSUP
276.Fa flag
277was not set to
278.Dv AT_SYMLINK_FOLLOW (some filesystems only)
279.It Bq Er ENOTDIR
280The
281.Fa name1
282or
283.Fa name2
284argument is not an absolute path and
285.Fa fd1
286or
287.Fa fd2 ,
288respectively, is neither
289.Dv AT_FDCWD
290nor a file descriptor associated with a directory.
291.El
9bccf70c
A
292.Sh SEE ALSO
293.Xr symlink 2 ,
294.Xr unlink 2
295.Sh STANDARDS
296The
297.Fn link
298function is expected to conform to
299.St -p1003.1-88 .
fe8ab488
A
300The
301.Fn linkat
302system call is expected to conform to POSIX.1-2008 .