| LINK(2) | System Calls Manual | LINK(2) |
link — make a hard
file link
Standard C Library (libc, -lc)
#include
<unistd.h>
int
link(const
char *name1, const char
*name2);
#include
<fcntl.h>
int
linkat(int
fd1, const char
*name1, int fd2,
const char *name2,
int flags);
The
link()
function call atomically creates the specified directory entry (hard link)
name2 with the attributes of the underlying object
pointed at by name1. If the link is successful: the
link count of the underlying object is incremented;
name1 and name2 share equal
access and rights to the underlying object.
If name1 is removed, the file name2 is not deleted and the link count of the underlying object is decremented.
name1 must exist for the hard link to succeed and both name1 and name2 must be in the same file system. name1 may not be a directory.
linkat()
works the same way as link() except if
name1 (resp. name2) is relative.
In that case, it is looked up from a directory whose file descriptor was
passed as fd1 (resp. fd2).
Search permission is required on the directories named by
fd1 and fd2.
fd1 or fd2 can be set to
AT_FDCWD in order to specify the current
directory.
When operating on a symlink,
link()
resolves the symlink and creates a hard link on the target.
linkat() will do the same if
AT_SYMLINK_FOLLOW is set in
flags, but it will link on the symlink itself if the
flag is clear.
The link() and
linkat() functions return the value 0 if
successful; otherwise the value -1 is returned and the global
variable errno is set to indicate the error.
link() and
linkat() will fail and no link will be created
if:
EACCES]EDQUOT]EEXIST]EFAULT]EIO]ELOOP]EMLINK]LINK_MAX}.ENAMETOOLONG]NAME_MAX}
characters, or an entire path name exceeded
{PATH_MAX} characters.ENOENT]ENOSPC]ENOTDIR]EOPNOTSUPP]EPERM]EROFS]EXDEV]In addition, linkat() will fail if:
EBADF]AT_FDCWD nor a valid file descriptor open for
reading or searching.ENOTDIR]The link() function conforms to
IEEE Std 1003.1-1990 (“POSIX.1”).
linkat() conforms to IEEE Std
1003.1-2008 (“POSIX.1”).
The link() function appeared in
Version 1 AT&T UNIX.
| July 20, 2024 | NetBSD 11.0 |