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

NAME

6       unlink - remove a directory entry
7

SYNOPSIS

9       #include <unistd.h>
10
11       int unlink(const char *path);
12
13

DESCRIPTION

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

RETURN VALUE

39       Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
40       returned  and  errno  set to indicate the error. If -1 is returned, the
41       named file shall not be changed.
42

ERRORS

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

EXAMPLES

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

APPLICATION USAGE

208       Applications should use rmdir() to remove a directory.
209

RATIONALE

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

FUTURE DIRECTIONS

224       None.
225

SEE ALSO

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