1UNLINK(3P)                 POSIX Programmer's Manual                UNLINK(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       unlink - remove a directory entry
13

SYNOPSIS

15       #include <unistd.h>
16
17       int unlink(const char *path);
18
19

DESCRIPTION

21       The unlink() function shall remove a link to a file. If  path  names  a
22       symbolic  link,  unlink()  shall remove the symbolic link named by path
23       and shall not affect any file or directory named by the contents of the
24       symbolic  link.  Otherwise, unlink() shall remove the link named by the
25       pathname pointed to by path and shall decrement the link count  of  the
26       file referenced by the link.
27
28       When  the file's link count becomes 0 and no process has the file open,
29       the space occupied by the file shall be freed and  the  file  shall  no
30       longer  be accessible. If one or more processes have the file open when
31       the last link is removed, the link shall  be  removed  before  unlink()
32       returns,  but the removal of the file contents shall be postponed until
33       all references to the file are closed.
34
35       The path argument shall not name a directory  unless  the  process  has
36       appropriate  privileges  and the implementation supports using unlink()
37       on directories.
38
39       Upon successful completion, unlink() shall mark for update the st_ctime
40       and  st_mtime  fields of the parent directory. Also, if the file's link
41       count is not 0, the st_ctime field of the  file  shall  be  marked  for
42       update.
43

RETURN VALUE

45       Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
46       returned and errno set to indicate the error. If -1  is  returned,  the
47       named file shall not be changed.
48

ERRORS

50       The unlink() function shall fail and shall not unlink the file if:
51
52       EACCES Search  permission is denied for a component of the path prefix,
53              or write permission is denied on the  directory  containing  the
54              directory entry to be removed.
55
56       EBUSY  The  file  named by the path argument cannot be unlinked because
57              it is being used by the system or another process and the imple‐
58              mentation considers this an error.
59
60       ELOOP  A loop exists in symbolic links encountered during resolution of
61              the path argument.
62
63       ENAMETOOLONG
64              The length of the path argument exceeds {PATH_MAX} or a pathname
65              component is longer than {NAME_MAX}.
66
67       ENOENT A component of path does not name an existing file or path is an
68              empty string.
69
70       ENOTDIR
71              A component of the path prefix is not a directory.
72
73       EPERM  The file named by path is a directory, and  either  the  calling
74              process does not have appropriate privileges, or the implementa‐
75              tion prohibits using unlink() on directories.
76
77       EPERM or EACCES
78
79              The S_ISVTX flag is set on the  directory  containing  the  file
80              referred  to by the path argument and the caller is not the file
81              owner, nor is the caller the directory owner, nor does the call‐
82              er have appropriate privileges.
83
84       EROFS  The  directory  entry to be unlinked is part of a read-only file
85              system.
86
87
88       The unlink() function may fail and not unlink the file if:
89
90       EBUSY  The file named by path is a named STREAM.
91
92       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
93              resolution of the path argument.
94
95       ENAMETOOLONG
96              As a result of encountering a symbolic link in resolution of the
97              path argument, the length of  the  substituted  pathname  string
98              exceeded {PATH_MAX}.
99
100       ETXTBSY
101              The  entry  to be unlinked is the last directory entry to a pure
102              procedure (shared text) file that is being executed.
103
104
105       The following sections are informative.
106

EXAMPLES

108   Removing a Link to a File
109       The following example shows how to  remove  a  link  to  a  file  named
110       /home/cnd/mod1 by removing the entry named /modules/pass1.
111
112
113              #include <unistd.h>
114
115
116              char *path = "/modules/pass1";
117              int   status;
118              ...
119              status = unlink(path);
120
121   Checking for an Error
122       The  following  example fragment creates a temporary password lock file
123       named LOCKFILE, which is defined as /etc/ptmp, and gets a file descrip‐
124       tor  for it. If the file cannot be opened for writing, unlink() is used
125       to remove the link between the file descriptor and LOCKFILE.
126
127
128              #include <sys/types.h>
129              #include <stdio.h>
130              #include <fcntl.h>
131              #include <errno.h>
132              #include <unistd.h>
133              #include <sys/stat.h>
134
135
136              #define LOCKFILE "/etc/ptmp"
137
138
139              int pfd;  /* Integer for file descriptor returned by open call. */
140              FILE *fpfd;  /* File pointer for use in putpwent(). */
141              ...
142              /* Open password Lock file. If it exists, this is an error. */
143              if ((pfd = open(LOCKFILE, O_WRONLY| O_CREAT | O_EXCL, S_IRUSR
144                  | S_IWUSR | S_IRGRP | S_IROTH)) == -1)  {
145                  fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n");
146                  exit(1);
147              }
148
149
150              /* Lock file created; proceed with fdopen of lock file so that
151                 putpwent() can be used.
152               */
153              if ((fpfd = fdopen(pfd, "w")) == NULL) {
154                  close(pfd);
155                  unlink(LOCKFILE);
156                  exit(1);
157              }
158
159   Replacing Files
160       The following example fragment uses unlink() to discard links to files,
161       so  that they can be replaced with new versions of the files. The first
162       call removes the link to LOCKFILE if an error occurs. Successive  calls
163       remove  the  links  to SAVEFILE and PASSWDFILE so that new links can be
164       created, then removes the link to LOCKFILE when it is no longer needed.
165
166
167              #include <sys/types.h>
168              #include <stdio.h>
169              #include <fcntl.h>
170              #include <errno.h>
171              #include <unistd.h>
172              #include <sys/stat.h>
173
174
175              #define LOCKFILE "/etc/ptmp"
176              #define PASSWDFILE "/etc/passwd"
177              #define SAVEFILE "/etc/opasswd"
178              ...
179              /* If no change was made, assume error and leave passwd unchanged. */
180              if (!valid_change) {
181                  fprintf(stderr, "Could not change password for user %s\n", user);
182                  unlink(LOCKFILE);
183                  exit(1);
184              }
185
186
187              /* Change permissions on new password file. */
188              chmod(LOCKFILE, S_IRUSR | S_IRGRP | S_IROTH);
189
190
191              /* Remove saved password file. */
192              unlink(SAVEFILE);
193
194
195              /* Save current password file. */
196              link(PASSWDFILE, SAVEFILE);
197
198
199              /* Remove current password file. */
200              unlink(PASSWDFILE);
201
202
203              /* Save new password file as current password file. */
204              link(LOCKFILE,PASSWDFILE);
205
206
207              /* Remove lock file. */
208              unlink(LOCKFILE);
209
210
211              exit(0);
212

APPLICATION USAGE

214       Applications should use rmdir() to remove a directory.
215

RATIONALE

217       Unlinking a directory is restricted to the superuser in many historical
218       implementations for reasons given in link() (see also rename()).
219
220       The  meaning  of  [EBUSY] in historical implementations is "mount point
221       busy". Since this volume of IEEE Std 1003.1-2001  does  not  cover  the
222       system administration concepts of mounting and unmounting, the descrip‐
223       tion of the error was changed to "resource busy". (This meaning is used
224       by some device drivers when a second process tries to open an exclusive
225       use device.) The wording is also intended to allow  implementations  to
226       refuse  to  remove  a  directory  if  it is the root or current working
227       directory of any process.
228

FUTURE DIRECTIONS

230       None.
231

SEE ALSO

233       close(), link(), remove(), rmdir(),  the  Base  Definitions  volume  of
234       IEEE Std 1003.1-2001, <unistd.h>
235
237       Portions  of  this text are reprinted and reproduced in electronic form
238       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
239       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
240       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
241       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
242       event of any discrepancy between this version and the original IEEE and
243       The  Open Group Standard, the original IEEE and The Open Group Standard
244       is the referee document. The original Standard can be  obtained  online
245       at http://www.opengroup.org/unix/online.html .
246
247
248
249IEEE/The Open Group                  2003                           UNLINK(3P)
Impressum