Go to the previous, next section.

Hard Links

In POSIX systems, one file can have many names at the same time. All of the names are equally real, and no one of them is preferred to the others.

To add a name to a file, use the link function. (The new name is also called a hard link to the file.) Creating a new link to a file does not copy the contents of the file; it simply makes a new name by which the file can be known, in addition to the file's existing name or names.

One file can have names in several directories, so the the organization of the file system is not a strict hierarchy or tree.

Since a particular file exists within a single file system, all its names must be in directories in that file system. link reports an error if you try to make a hard link to the file from another file system.

The prototype for the link function is declared in the header file `unistd.h'.

Function: int link (const char *oldname, const char *newname)

The link function makes a new link to the existing file named by oldname, under the new name newname.

This function returns a value of 0 if it is successful and -1 on failure. In addition to the usual file name syntax errors (see section File Name Errors) for both oldname and newname, the following errno error conditions are defined for this function:

The directory in which the new link is to be written is not writable.

There is already a file named newname. If you want to replace this link with a new link, you must remove the old link explicitly first.

There are already too many links to the file named by oldname. (The maximum number of links to a file is LINK_MAX; see section Limits on File System Capacity.)

Well-designed file systems never report this error, because they permit more links than your disk could possibly hold. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.

The file named by oldname doesn't exist. You can't make a link to a file that doesn't exist.

The directory or file system that would contain the new link is "full" and cannot be extended.

Some implementations only allow privileged users to make links to directories, and others prohibit this operation entirely. This error is used to report the problem.

The directory containing the new link can't be modified because it's on a read-only file system.

The directory specified in newname is on a different file system than the existing file.

Go to the previous, next section.