1LINK(3P)                   POSIX Programmer's Manual                  LINK(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       link - link to a file
13

SYNOPSIS

15       #include <unistd.h>
16
17       int link(const char *path1, const char *path2);
18
19

DESCRIPTION

21       The link() function shall create a new link (directory entry)  for  the
22       existing file, path1.
23
24       The  path1  argument points to a pathname naming an existing file.  The
25       path2 argument points to a pathname naming the new directory  entry  to
26       be  created. The link() function shall atomically create a new link for
27       the existing file and the link count of the file shall  be  incremented
28       by one.
29
30       If  path1  names  a directory, link() shall fail unless the process has
31       appropriate privileges and the implementation supports using link()  on
32       directories.
33
34       Upon  successful  completion, link() shall mark for update the st_ctime
35       field of the file. Also, the st_ctime and st_mtime fields of the direc‐
36       tory that contains the new entry shall be marked for update.
37
38       If  link()  fails,  no  link shall be created and the link count of the
39       file shall remain unchanged.
40
41       The implementation may require that the calling process has  permission
42       to access the existing file.
43

RETURN VALUE

45       Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
46       returned and errno set to indicate the error.
47

ERRORS

49       The link() function shall fail if:
50
51       EACCES A component of either path prefix denies search  permission,  or
52              the  requested  link requires writing in a directory that denies
53              write permission, or the calling process does not  have  permis‐
54              sion  to  access  the  existing file and this is required by the
55              implementation.
56
57       EEXIST The path2 argument resolves to an existing file or refers  to  a
58              symbolic link.
59
60       ELOOP  A loop exists in symbolic links encountered during resolution of
61              the path1 or path2 argument.
62
63       EMLINK The number of links to the file  named  by  path1  would  exceed
64              {LINK_MAX}.
65
66       ENAMETOOLONG
67              The  length of the path1 or path2 argument exceeds {PATH_MAX} or
68              a pathname component is longer than {NAME_MAX}.
69
70       ENOENT A component of either path prefix does not exist; the file named
71              by  path1  does  not exist; or path1 or path2 points to an empty
72              string.
73
74       ENOSPC The directory to contain the link cannot be extended.
75
76       ENOTDIR
77              A component of either path prefix is not a directory.
78
79       EPERM  The file named by path1 is a directory and  either  the  calling
80              process  does not have appropriate privileges or the implementa‐
81              tion prohibits using link() on directories.
82
83       EROFS  The requested link requires writing in a directory  on  a  read-
84              only file system.
85
86       EXDEV  The  link named by path2 and the file named by path1 are on dif‐
87              ferent file systems and  the  implementation  does  not  support
88              links between file systems.
89
90       EXDEV  path1 refers to a named STREAM.
91
92
93       The link() function may fail if:
94
95       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
96              resolution of the path1 or path2 argument.
97
98       ENAMETOOLONG
99              As a result of encountering a symbolic link in resolution of the
100              path1  or path2 argument, the length of the substituted pathname
101              string exceeded {PATH_MAX}.
102
103
104       The following sections are informative.
105

EXAMPLES

107   Creating a Link to a File
108       The following example shows how to  create  a  link  to  a  file  named
109       /home/cnd/mod1 by creating a new directory entry named /modules/pass1.
110
111
112              #include <unistd.h>
113
114
115              char *path1 = "/home/cnd/mod1";
116              char *path2 = "/modules/pass1";
117              int   status;
118              ...
119              status = link (path1, path2);
120
121   Creating a Link to a File Within a Program
122       In  the  following  program  example,  the  link()  function  links the
123       /etc/passwd file (defined as PASSWDFILE) to a file  named  /etc/opasswd
124       (defined as SAVEFILE), which is used to save the current password file.
125       Then, after removing the current password file (defined as PASSWDFILE),
126       the  new  password file is saved as the current password file using the
127       link() function again.
128
129
130              #include <unistd.h>
131
132
133              #define LOCKFILE "/etc/ptmp"
134              #define PASSWDFILE "/etc/passwd"
135              #define SAVEFILE "/etc/opasswd"
136              ...
137              /* Save current password file */
138              link (PASSWDFILE, SAVEFILE);
139
140
141              /* Remove current password file. */
142              unlink (PASSWDFILE);
143
144
145              /* Save new password file as current password file. */
146              link (LOCKFILE,PASSWDFILE);
147

APPLICATION USAGE

149       Some implementations do allow links between file systems.
150

RATIONALE

152       Linking to a directory is restricted to the superuser in most  histori‐
153       cal  implementations  because  this capability may produce loops in the
154       file hierarchy or otherwise corrupt the file system.   This  volume  of
155       IEEE Std 1003.1-2001  continues  that  philosophy by prohibiting link()
156       and unlink() from doing this. Other functions could do it if the imple‐
157       mentor designed such an extension.
158
159       Some  historical  implementations  allow  linking of files on different
160       file systems. Wording was  added  to  explicitly  allow  this  optional
161       behavior.
162
163       The  exception for cross-file system links is intended to apply only to
164       links that are programmatically indistinguishable from "hard" links.
165

FUTURE DIRECTIONS

167       None.
168

SEE ALSO

170       symlink(),    unlink(),    the    Base    Definitions     volume     of
171       IEEE Std 1003.1-2001, <unistd.h>
172
174       Portions  of  this text are reprinted and reproduced in electronic form
175       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
176       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
177       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
178       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
179       event of any discrepancy between this version and the original IEEE and
180       The  Open Group Standard, the original IEEE and The Open Group Standard
181       is the referee document. The original Standard can be  obtained  online
182       at http://www.opengroup.org/unix/online.html .
183
184
185
186IEEE/The Open Group                  2003                             LINK(3P)
Impressum