1.\" $NetBSD: link.2,v 1.30 2013/01/13 08:15:02 dholland Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993, 2011 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)link.2 8.3 (Berkeley) 1/12/94 31.\" 32.Dd November 18, 2012 33.Dt LINK 2 34.Os 35.Sh NAME 36.Nm link 37.Nd make a hard file link 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In unistd.h 42.Ft int 43.Fn link "const char *name1" "const char *name2" 44.In fcntl.h 45.Ft int 46.Fn linkat "int fd1" "const char *name1" "int fd2" "const char *name2" "int flags" 47.Sh DESCRIPTION 48The 49.Fn link 50function call 51atomically creates the specified directory entry (hard link) 52.Fa name2 53with the attributes of the underlying object pointed at by 54.Fa name1 . 55If the link is successful: the link count of the underlying object 56is incremented; 57.Fa name1 58and 59.Fa name2 60share equal access and rights 61to the 62underlying object. 63.Pp 64If 65.Fa name1 66is removed, the file 67.Fa name2 68is not deleted and the link count of the 69underlying object is 70decremented. 71.Pp 72.Fa name1 73must exist for the hard link to 74succeed and 75both 76.Fa name1 77and 78.Fa name2 79must be in the same file system. 80.Fa name1 81may not be a directory unless the caller is the super-user 82and the file system containing it supports linking to directories. 83.Pp 84.Fn linkat 85works the same way as 86.Fn link 87except if 88.Fa name1 89(resp. 90.Fa name2 ) 91is relative. 92In that case, it is looked up from a directory whose file 93descriptor was passed as 94.Fa fd1 95(resp. 96.Fa fd2 ) . 97Search permission is required on the directories named by 98.Fa fd1 99and 100.Fa fd2. 101.\" (These alternatives await a decision about the semantics of O_SEARCH) 102.\" Search permission is required on the directories named by 103.\" .Fa fd1 104.\" and 105.\" .Fa fd2 106.\" unless they were opened with the 107.\" .Dv O_SEARCH 108.\" flag. 109.\" - or - 110.\" The directories named by 111.\" .Fa fd1 112.\" and 113.\" .Fa fd2 114.\" must have been opened with the 115.\" .Dv O_SEARCH 116.\" flag. 117.Fa fd1 118or 119.Fa fd2 120can be set to 121.Dv AT_FDCWD 122in order to specify the current directory. 123.Pp 124When operating on a symlink, 125.Fn link 126resolves the symlink and creates a hard link on the target. 127.Fn linkat 128will do the same if 129.Dv AT_SYMLINK_FOLLOW 130is set in 131.Fa flags , 132but it will link on the symlink itself if the flag is clear. 133.Sh RETURN VALUES 134Upon successful completion, a value of 0 is returned. 135Otherwise, a value of \-1 is returned and 136.Va errno 137is set to indicate the error. 138.Sh ERRORS 139.Fn link 140will fail and no link will be created if: 141.Bl -tag -width Er 142.It Bq Er EACCES 143A component of either path prefix denies search permission, or 144the requested link requires writing in a directory with a mode 145that denies write permission. 146.It Bq Er EDQUOT 147The directory in which the entry for the new link 148is being placed cannot be extended because the 149user's quota of disk blocks on the file system 150containing the directory has been exhausted. 151.It Bq Er EEXIST 152The link named by 153.Fa name2 154does exist. 155.It Bq Er EFAULT 156One of the pathnames specified 157is outside the process's allocated address space. 158.It Bq Er EIO 159An I/O error occurred while reading from or writing to 160the file system to make the directory entry. 161.It Bq Er ELOOP 162Too many symbolic links were encountered in translating one of the pathnames. 163.It Bq Er EMLINK 164The link count of the file named by 165.Fa name1 166would exceed 167.Dv {LINK_MAX} . 168.It Bq Er ENAMETOOLONG 169A component of a pathname exceeded 170.Brq Dv NAME_MAX 171characters, or an entire path name exceeded 172.Brq Dv PATH_MAX 173characters. 174.It Bq Er ENOENT 175A component of either path prefix does not exist, or the file named 176by 177.Fa name1 178does not exist. 179.It Bq Er ENOSPC 180The directory in which the entry for the new link is being placed 181cannot be extended because there is no space left on the file 182system containing the directory. 183.It Bq Er ENOTDIR 184A component of either path prefix is not a directory. 185.It Bq Er EOPNOTSUPP 186The file system containing the file named by 187.Fa name1 188does not support links. 189.It Bq Er EPERM 190The file named by 191.Fa name1 192is a directory and the effective 193user ID is not super-user, 194or the file system containing the file does not permit the use of 195.Fn link 196on a directory. 197.It Bq Er EROFS 198The requested link requires writing in a directory on a read-only file 199system. 200.It Bq Er EXDEV 201The link named by 202.Fa name2 203and the file named by 204.Fa name1 205are on different file systems. 206.El 207.Sh SEE ALSO 208.Xr symlink 2 , 209.Xr unlink 2 210.Sh STANDARDS 211The 212.Fn link 213function conforms to 214.St -p1003.1-90 . 215.Fn linkat 216conforms to 217.St -p1003.1-2008 . 218