]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | @node File System Interface, Pipes and FIFOs, Low-Level I/O, Top |
7a68c94a | 2 | @c %MENU% Functions for manipulating files |
28f540f4 RM |
3 | @chapter File System Interface |
4 | ||
5 | This chapter describes the GNU C library's functions for manipulating | |
8b7fb588 UD |
6 | files. Unlike the input and output functions (@pxref{I/O on Streams}; |
7 | @pxref{Low-Level I/O}), these functions are concerned with operating | |
04b9968b | 8 | on the files themselves rather than on their contents. |
28f540f4 RM |
9 | |
10 | Among the facilities described in this chapter are functions for | |
11 | examining or modifying directories, functions for renaming and deleting | |
12 | files, and functions for examining and setting file attributes such as | |
13 | access permissions and modification times. | |
14 | ||
15 | @menu | |
16 | * Working Directory:: This is used to resolve relative | |
17 | file names. | |
18 | * Accessing Directories:: Finding out what files a directory | |
19 | contains. | |
d01d6319 | 20 | * Working with Directory Trees:: Apply actions to all files or a selectable |
f2ea0f5b | 21 | subset of a directory hierarchy. |
28f540f4 RM |
22 | * Hard Links:: Adding alternate names to a file. |
23 | * Symbolic Links:: A file that ``points to'' a file name. | |
24 | * Deleting Files:: How to delete a file, and what that means. | |
25 | * Renaming Files:: Changing a file's name. | |
26 | * Creating Directories:: A system call just for creating a directory. | |
27 | * File Attributes:: Attributes of individual files. | |
28 | * Making Special Files:: How to create special files. | |
29 | * Temporary Files:: Naming and creating temporary files. | |
30 | @end menu | |
31 | ||
32 | @node Working Directory | |
33 | @section Working Directory | |
34 | ||
35 | @cindex current working directory | |
36 | @cindex working directory | |
37 | @cindex change working directory | |
38 | Each process has associated with it a directory, called its @dfn{current | |
39 | working directory} or simply @dfn{working directory}, that is used in | |
40 | the resolution of relative file names (@pxref{File Name Resolution}). | |
41 | ||
42 | When you log in and begin a new session, your working directory is | |
43 | initially set to the home directory associated with your login account | |
44 | in the system user database. You can find any user's home directory | |
45 | using the @code{getpwuid} or @code{getpwnam} functions; see @ref{User | |
46 | Database}. | |
47 | ||
48 | Users can change the working directory using shell commands like | |
49 | @code{cd}. The functions described in this section are the primitives | |
50 | used by those commands and by other programs for examining and changing | |
51 | the working directory. | |
52 | @pindex cd | |
53 | ||
54 | Prototypes for these functions are declared in the header file | |
55 | @file{unistd.h}. | |
56 | @pindex unistd.h | |
57 | ||
58 | @comment unistd.h | |
59 | @comment POSIX.1 | |
60 | @deftypefun {char *} getcwd (char *@var{buffer}, size_t @var{size}) | |
61 | The @code{getcwd} function returns an absolute file name representing | |
62 | the current working directory, storing it in the character array | |
63 | @var{buffer} that you provide. The @var{size} argument is how you tell | |
64 | the system the allocation size of @var{buffer}. | |
65 | ||
66 | The GNU library version of this function also permits you to specify a | |
67 | null pointer for the @var{buffer} argument. Then @code{getcwd} | |
68 | allocates a buffer automatically, as with @code{malloc} | |
69 | (@pxref{Unconstrained Allocation}). If the @var{size} is greater than | |
70 | zero, then the buffer is that large; otherwise, the buffer is as large | |
71 | as necessary to hold the result. | |
72 | ||
73 | The return value is @var{buffer} on success and a null pointer on failure. | |
74 | The following @code{errno} error conditions are defined for this function: | |
75 | ||
76 | @table @code | |
77 | @item EINVAL | |
78 | The @var{size} argument is zero and @var{buffer} is not a null pointer. | |
79 | ||
80 | @item ERANGE | |
81 | The @var{size} argument is less than the length of the working directory | |
82 | name. You need to allocate a bigger array and try again. | |
83 | ||
84 | @item EACCES | |
85 | Permission to read or search a component of the file name was denied. | |
86 | @end table | |
87 | @end deftypefun | |
88 | ||
9afc8a59 UD |
89 | You could implement the behavior of GNU's @w{@code{getcwd (NULL, 0)}} |
90 | using only the standard behavior of @code{getcwd}: | |
28f540f4 RM |
91 | |
92 | @smallexample | |
93 | char * | |
d1d62b53 | 94 | gnu_getcwd () |
28f540f4 | 95 | @{ |
d1d62b53 AJ |
96 | size_t size = 100; |
97 | ||
28f540f4 RM |
98 | while (1) |
99 | @{ | |
e8b1163e | 100 | char *buffer = (char *) xmalloc (size); |
d1d62b53 AJ |
101 | if (getcwd (buffer, size) == buffer) |
102 | return buffer; | |
28f540f4 | 103 | free (buffer); |
e8b1163e | 104 | if (errno != ERANGE) |
d1d62b53 | 105 | return 0; |
e8b1163e | 106 | size *= 2; |
28f540f4 RM |
107 | @} |
108 | @} | |
109 | @end smallexample | |
110 | ||
111 | @noindent | |
112 | @xref{Malloc Examples}, for information about @code{xmalloc}, which is | |
113 | not a library function but is a customary name used in most GNU | |
114 | software. | |
115 | ||
116 | @comment unistd.h | |
117 | @comment BSD | |
118 | @deftypefun {char *} getwd (char *@var{buffer}) | |
119 | This is similar to @code{getcwd}, but has no way to specify the size of | |
120 | the buffer. The GNU library provides @code{getwd} only | |
121 | for backwards compatibility with BSD. | |
122 | ||
123 | The @var{buffer} argument should be a pointer to an array at least | |
124 | @code{PATH_MAX} bytes long (@pxref{Limits for Files}). In the GNU | |
125 | system there is no limit to the size of a file name, so this is not | |
126 | necessarily enough space to contain the directory name. That is why | |
127 | this function is deprecated. | |
128 | @end deftypefun | |
129 | ||
130 | @comment unistd.h | |
131 | @comment POSIX.1 | |
132 | @deftypefun int chdir (const char *@var{filename}) | |
133 | This function is used to set the process's working directory to | |
134 | @var{filename}. | |
135 | ||
136 | The normal, successful return value from @code{chdir} is @code{0}. A | |
137 | value of @code{-1} is returned to indicate an error. The @code{errno} | |
138 | error conditions defined for this function are the usual file name | |
139 | syntax errors (@pxref{File Name Errors}), plus @code{ENOTDIR} if the | |
140 | file @var{filename} is not a directory. | |
141 | @end deftypefun | |
142 | ||
143 | ||
144 | @node Accessing Directories | |
145 | @section Accessing Directories | |
146 | @cindex accessing directories | |
147 | @cindex reading from a directory | |
148 | @cindex directories, accessing | |
149 | ||
150 | The facilities described in this section let you read the contents of a | |
151 | directory file. This is useful if you want your program to list all the | |
152 | files in a directory, perhaps as part of a menu. | |
153 | ||
154 | @cindex directory stream | |
155 | The @code{opendir} function opens a @dfn{directory stream} whose | |
156 | elements are directory entries. You use the @code{readdir} function on | |
157 | the directory stream to retrieve these entries, represented as | |
158 | @w{@code{struct dirent}} objects. The name of the file for each entry is | |
159 | stored in the @code{d_name} member of this structure. There are obvious | |
160 | parallels here to the stream facilities for ordinary files, described in | |
161 | @ref{I/O on Streams}. | |
162 | ||
163 | @menu | |
164 | * Directory Entries:: Format of one directory entry. | |
165 | * Opening a Directory:: How to open a directory stream. | |
166 | * Reading/Closing Directory:: How to read directory entries from the stream. | |
167 | * Simple Directory Lister:: A very simple directory listing program. | |
168 | * Random Access Directory:: Rereading part of the directory | |
169 | already read with the same stream. | |
0d8733c4 UD |
170 | * Scanning Directory Content:: Get entries for user selected subset of |
171 | contents in given directory. | |
172 | * Simple Directory Lister Mark II:: Revised version of the program. | |
28f540f4 RM |
173 | @end menu |
174 | ||
175 | @node Directory Entries | |
176 | @subsection Format of a Directory Entry | |
177 | ||
178 | @pindex dirent.h | |
179 | This section describes what you find in a single directory entry, as you | |
180 | might obtain it from a directory stream. All the symbols are declared | |
181 | in the header file @file{dirent.h}. | |
182 | ||
183 | @comment dirent.h | |
184 | @comment POSIX.1 | |
185 | @deftp {Data Type} {struct dirent} | |
186 | This is a structure type used to return information about directory | |
187 | entries. It contains the following fields: | |
188 | ||
189 | @table @code | |
190 | @item char d_name[] | |
191 | This is the null-terminated file name component. This is the only | |
192 | field you can count on in all POSIX systems. | |
193 | ||
194 | @item ino_t d_fileno | |
195 | This is the file serial number. For BSD compatibility, you can also | |
196 | refer to this member as @code{d_ino}. In the GNU system and most POSIX | |
197 | systems, for most files this the same as the @code{st_ino} member that | |
198 | @code{stat} will return for the file. @xref{File Attributes}. | |
199 | ||
200 | @item unsigned char d_namlen | |
201 | This is the length of the file name, not including the terminating null | |
202 | character. Its type is @code{unsigned char} because that is the integer | |
203 | type of the appropriate size | |
204 | ||
205 | @item unsigned char d_type | |
206 | This is the type of the file, possibly unknown. The following constants | |
207 | are defined for its value: | |
208 | ||
209 | @table @code | |
210 | @item DT_UNKNOWN | |
211 | The type is unknown. On some systems this is the only value returned. | |
212 | ||
213 | @item DT_REG | |
214 | A regular file. | |
215 | ||
216 | @item DT_DIR | |
217 | A directory. | |
218 | ||
219 | @item DT_FIFO | |
220 | A named pipe, or FIFO. @xref{FIFO Special Files}. | |
221 | ||
222 | @item DT_SOCK | |
223 | A local-domain socket. @c !!! @xref{Local Domain}. | |
224 | ||
225 | @item DT_CHR | |
226 | A character device. | |
227 | ||
228 | @item DT_BLK | |
229 | A block device. | |
230 | @end table | |
231 | ||
838e5ffe | 232 | This member is a BSD extension. On systems where it is used, it |
28f540f4 | 233 | corresponds to the file type bits in the @code{st_mode} member of |
838e5ffe UD |
234 | @code{struct statbuf}. On other systems it will always be DT_UNKNOWN. |
235 | These two macros convert between @code{d_type} values and @code{st_mode} | |
236 | values: | |
28f540f4 RM |
237 | |
238 | @deftypefun int IFTODT (mode_t @var{mode}) | |
239 | This returns the @code{d_type} value corresponding to @var{mode}. | |
240 | @end deftypefun | |
241 | ||
838e5ffe UD |
242 | @deftypefun mode_t DTTOIF (int @var{dtype}) |
243 | This returns the @code{st_mode} value corresponding to @var{dtype}. | |
28f540f4 RM |
244 | @end deftypefun |
245 | @end table | |
246 | ||
247 | This structure may contain additional members in the future. | |
248 | ||
249 | When a file has multiple names, each name has its own directory entry. | |
250 | The only way you can tell that the directory entries belong to a | |
251 | single file is that they have the same value for the @code{d_fileno} | |
252 | field. | |
253 | ||
04b9968b UD |
254 | File attributes such as size, modification times etc., are part of the |
255 | file itself, not of any particular directory entry. @xref{File | |
28f540f4 RM |
256 | Attributes}. |
257 | @end deftp | |
258 | ||
259 | @node Opening a Directory | |
260 | @subsection Opening a Directory Stream | |
261 | ||
262 | @pindex dirent.h | |
263 | This section describes how to open a directory stream. All the symbols | |
264 | are declared in the header file @file{dirent.h}. | |
265 | ||
266 | @comment dirent.h | |
267 | @comment POSIX.1 | |
268 | @deftp {Data Type} DIR | |
d68171ed | 269 | The @code{DIR} data type represents a directory stream. |
28f540f4 RM |
270 | @end deftp |
271 | ||
272 | You shouldn't ever allocate objects of the @code{struct dirent} or | |
273 | @code{DIR} data types, since the directory access functions do that for | |
274 | you. Instead, you refer to these objects using the pointers returned by | |
275 | the following functions. | |
276 | ||
277 | @comment dirent.h | |
278 | @comment POSIX.1 | |
279 | @deftypefun {DIR *} opendir (const char *@var{dirname}) | |
280 | The @code{opendir} function opens and returns a directory stream for | |
281 | reading the directory whose file name is @var{dirname}. The stream has | |
282 | type @code{DIR *}. | |
283 | ||
284 | If unsuccessful, @code{opendir} returns a null pointer. In addition to | |
285 | the usual file name errors (@pxref{File Name Errors}), the | |
286 | following @code{errno} error conditions are defined for this function: | |
287 | ||
288 | @table @code | |
289 | @item EACCES | |
290 | Read permission is denied for the directory named by @code{dirname}. | |
291 | ||
292 | @item EMFILE | |
293 | The process has too many files open. | |
294 | ||
295 | @item ENFILE | |
296 | The entire system, or perhaps the file system which contains the | |
297 | directory, cannot support any additional open files at the moment. | |
298 | (This problem cannot happen on the GNU system.) | |
299 | @end table | |
300 | ||
301 | The @code{DIR} type is typically implemented using a file descriptor, | |
302 | and the @code{opendir} function in terms of the @code{open} function. | |
303 | @xref{Low-Level I/O}. Directory streams and the underlying | |
304 | file descriptors are closed on @code{exec} (@pxref{Executing a File}). | |
305 | @end deftypefun | |
306 | ||
307 | @node Reading/Closing Directory | |
308 | @subsection Reading and Closing a Directory Stream | |
309 | ||
310 | @pindex dirent.h | |
311 | This section describes how to read directory entries from a directory | |
312 | stream, and how to close the stream when you are done with it. All the | |
313 | symbols are declared in the header file @file{dirent.h}. | |
314 | ||
315 | @comment dirent.h | |
316 | @comment POSIX.1 | |
317 | @deftypefun {struct dirent *} readdir (DIR *@var{dirstream}) | |
318 | This function reads the next entry from the directory. It normally | |
319 | returns a pointer to a structure containing information about the file. | |
320 | This structure is statically allocated and can be rewritten by a | |
321 | subsequent call. | |
322 | ||
04b9968b | 323 | @strong{Portability Note:} On some systems @code{readdir} may not |
28f540f4 RM |
324 | return entries for @file{.} and @file{..}, even though these are always |
325 | valid file names in any directory. @xref{File Name Resolution}. | |
326 | ||
327 | If there are no more entries in the directory or an error is detected, | |
328 | @code{readdir} returns a null pointer. The following @code{errno} error | |
329 | conditions are defined for this function: | |
330 | ||
331 | @table @code | |
332 | @item EBADF | |
333 | The @var{dirstream} argument is not valid. | |
334 | @end table | |
a68b0d31 UD |
335 | |
336 | @code{readdir} is not thread safe. Multiple threads using | |
337 | @code{readdir} on the same @var{dirstream} may overwrite the return | |
338 | value. Use @code{readdir_r} when this is critical. | |
339 | @end deftypefun | |
340 | ||
341 | @comment dirent.h | |
342 | @comment GNU | |
dd7d45e8 | 343 | @deftypefun int readdir_r (DIR *@var{dirstream}, struct dirent *@var{entry}, struct dirent **@var{result}) |
fd26970f | 344 | This function is the reentrant version of @code{readdir}. Like |
a68b0d31 | 345 | @code{readdir} it returns the next entry from the directory. But to |
04b9968b UD |
346 | prevent conflicts between simultaneously running threads the result is |
347 | not stored in statically allocated memory. Instead the argument | |
348 | @var{entry} points to a place to store the result. | |
a68b0d31 UD |
349 | |
350 | The return value is @code{0} in case the next entry was read | |
351 | successfully. In this case a pointer to the result is returned in | |
352 | *@var{result}. It is not required that *@var{result} is the same as | |
6d52618b | 353 | @var{entry}. If something goes wrong while executing @code{readdir_r} |
1618c590 UD |
354 | the function returns a value indicating the error (as described for |
355 | @code{readdir}). | |
a68b0d31 | 356 | |
c063ba61 UD |
357 | If there are no more directory entries, @code{readdir_r}'s return value is |
358 | @code{0}, and *@var{result} is set to @code{NULL}. | |
359 | ||
04b9968b UD |
360 | @strong{Portability Note:} On some systems @code{readdir_r} may not |
361 | return a NUL terminated string for the file name, even when there is no | |
362 | @code{d_reclen} field in @code{struct dirent} and the file | |
363 | name is the maximum allowed size. Modern systems all have the | |
364 | @code{d_reclen} field, and on old systems multi-threading is not | |
365 | critical. In any case there is no such problem with the @code{readdir} | |
366 | function, so that even on systems without the @code{d_reclen} member one | |
367 | could use multiple threads by using external locking. | |
c329332e UD |
368 | |
369 | It is also important to look at the definition of the @code{struct | |
370 | dirent} type. Simply passing a pointer to an object of this type for | |
371 | the second parameter of @code{readdir_r} might not be enough. Some | |
372 | systems don't define the @code{d_name} element sufficiently long. In | |
373 | this case the user has to provide additional space. There must be room | |
374 | for at least @code{NAME_MAX + 1} characters in the @code{d_name} array. | |
375 | Code to call @code{readdir_r} could look like this: | |
376 | ||
377 | @smallexample | |
378 | union | |
379 | @{ | |
380 | struct dirent d; | |
381 | char b[offsetof (struct dirent, d_name) + NAME_MAX + 1]; | |
382 | @} u; | |
383 | ||
384 | if (readdir_r (dir, &u.d, &res) == 0) | |
385 | ... | |
386 | @end smallexample | |
387 | @end deftypefun | |
388 | ||
389 | To support large filesystems on 32-bit machines there are LFS variants | |
390 | of the last two functions. | |
391 | ||
392 | @comment dirent.h | |
393 | @comment LFS | |
394 | @deftypefun {struct dirent64 *} readdir64 (DIR *@var{dirstream}) | |
395 | The @code{readdir64} function is just like the @code{readdir} function | |
396 | except that it returns a pointer to a record of type @code{struct | |
397 | dirent64}. Some of the members of this data type (notably @code{d_ino}) | |
398 | might have a different size to allow large filesystems. | |
399 | ||
400 | In all other aspects this function is equivalent to @code{readdir}. | |
401 | @end deftypefun | |
402 | ||
403 | @comment dirent.h | |
404 | @comment LFS | |
405 | @deftypefun int readdir64_r (DIR *@var{dirstream}, struct dirent64 *@var{entry}, struct dirent64 **@var{result}) | |
406 | The @code{readdir64_r} function is equivalent to the @code{readdir_r} | |
407 | function except that it takes parameters of base type @code{struct | |
408 | dirent64} instead of @code{struct dirent} in the second and third | |
409 | position. The same precautions mentioned in the documentation of | |
410 | @code{readdir_r} also apply here. | |
28f540f4 RM |
411 | @end deftypefun |
412 | ||
413 | @comment dirent.h | |
414 | @comment POSIX.1 | |
415 | @deftypefun int closedir (DIR *@var{dirstream}) | |
416 | This function closes the directory stream @var{dirstream}. It returns | |
d68171ed | 417 | @code{0} on success and @code{-1} on failure. |
28f540f4 RM |
418 | |
419 | The following @code{errno} error conditions are defined for this | |
420 | function: | |
421 | ||
422 | @table @code | |
423 | @item EBADF | |
424 | The @var{dirstream} argument is not valid. | |
425 | @end table | |
426 | @end deftypefun | |
427 | ||
428 | @node Simple Directory Lister | |
429 | @subsection Simple Program to List a Directory | |
430 | ||
431 | Here's a simple program that prints the names of the files in | |
432 | the current working directory: | |
433 | ||
434 | @smallexample | |
435 | @include dir.c.texi | |
436 | @end smallexample | |
437 | ||
438 | The order in which files appear in a directory tends to be fairly | |
439 | random. A more useful program would sort the entries (perhaps by | |
0d8733c4 | 440 | alphabetizing them) before printing them; see |
8b7fb588 | 441 | @ref{Scanning Directory Content}, and @ref{Array Sort Function}. |
28f540f4 | 442 | |
28f540f4 RM |
443 | |
444 | @node Random Access Directory | |
445 | @subsection Random Access in a Directory Stream | |
446 | ||
447 | @pindex dirent.h | |
448 | This section describes how to reread parts of a directory that you have | |
449 | already read from an open directory stream. All the symbols are | |
450 | declared in the header file @file{dirent.h}. | |
451 | ||
452 | @comment dirent.h | |
453 | @comment POSIX.1 | |
454 | @deftypefun void rewinddir (DIR *@var{dirstream}) | |
455 | The @code{rewinddir} function is used to reinitialize the directory | |
456 | stream @var{dirstream}, so that if you call @code{readdir} it | |
457 | returns information about the first entry in the directory again. This | |
458 | function also notices if files have been added or removed to the | |
459 | directory since it was opened with @code{opendir}. (Entries for these | |
460 | files might or might not be returned by @code{readdir} if they were | |
461 | added or removed since you last called @code{opendir} or | |
462 | @code{rewinddir}.) | |
463 | @end deftypefun | |
464 | ||
465 | @comment dirent.h | |
466 | @comment BSD | |
467 | @deftypefun off_t telldir (DIR *@var{dirstream}) | |
468 | The @code{telldir} function returns the file position of the directory | |
469 | stream @var{dirstream}. You can use this value with @code{seekdir} to | |
470 | restore the directory stream to that position. | |
471 | @end deftypefun | |
472 | ||
473 | @comment dirent.h | |
474 | @comment BSD | |
475 | @deftypefun void seekdir (DIR *@var{dirstream}, off_t @var{pos}) | |
476 | The @code{seekdir} function sets the file position of the directory | |
477 | stream @var{dirstream} to @var{pos}. The value @var{pos} must be the | |
478 | result of a previous call to @code{telldir} on this particular stream; | |
479 | closing and reopening the directory can invalidate values returned by | |
480 | @code{telldir}. | |
481 | @end deftypefun | |
482 | ||
0d8733c4 UD |
483 | |
484 | @node Scanning Directory Content | |
485 | @subsection Scanning the Content of a Directory | |
486 | ||
487 | A higher-level interface to the directory handling functions is the | |
488 | @code{scandir} function. With its help one can select a subset of the | |
04b9968b UD |
489 | entries in a directory, possibly sort them and get a list of names as |
490 | the result. | |
0d8733c4 | 491 | |
5679cdb6 UD |
492 | @comment dirent.h |
493 | @comment BSD/SVID | |
494 | @deftypefun int scandir (const char *@var{dir}, struct dirent ***@var{namelist}, int (*@var{selector}) (const struct dirent *), int (*@var{cmp}) (const void *, const void *)) | |
0d8733c4 UD |
495 | |
496 | The @code{scandir} function scans the contents of the directory selected | |
04b9968b | 497 | by @var{dir}. The result in *@var{namelist} is an array of pointers to |
0d8733c4 UD |
498 | structure of type @code{struct dirent} which describe all selected |
499 | directory entries and which is allocated using @code{malloc}. Instead | |
500 | of always getting all directory entries returned, the user supplied | |
501 | function @var{selector} can be used to decide which entries are in the | |
04b9968b | 502 | result. Only the entries for which @var{selector} returns a non-zero |
0d8733c4 UD |
503 | value are selected. |
504 | ||
04b9968b UD |
505 | Finally the entries in *@var{namelist} are sorted using the |
506 | user-supplied function @var{cmp}. The arguments passed to the @var{cmp} | |
507 | function are of type @code{struct dirent **}, therefore one cannot | |
508 | directly use the @code{strcmp} or @code{strcoll} functions; instead see | |
509 | the functions @code{alphasort} and @code{versionsort} below. | |
0d8733c4 | 510 | |
04b9968b UD |
511 | The return value of the function is the number of entries placed in |
512 | *@var{namelist}. If it is @code{-1} an error occurred (either the | |
af6f3906 UD |
513 | directory could not be opened for reading or the malloc call failed) and |
514 | the global variable @code{errno} contains more information on the error. | |
0d8733c4 UD |
515 | @end deftypefun |
516 | ||
04b9968b UD |
517 | As described above the fourth argument to the @code{scandir} function |
518 | must be a pointer to a sorting function. For the convenience of the | |
519 | programmer the GNU C library contains implementations of functions which | |
520 | are very helpful for this purpose. | |
0d8733c4 | 521 | |
5679cdb6 UD |
522 | @comment dirent.h |
523 | @comment BSD/SVID | |
0d8733c4 | 524 | @deftypefun int alphasort (const void *@var{a}, const void *@var{b}) |
5679cdb6 | 525 | The @code{alphasort} function behaves like the @code{strcoll} function |
0d8733c4 UD |
526 | (@pxref{String/Array Comparison}). The difference is that the arguments |
527 | are not string pointers but instead they are of type | |
528 | @code{struct dirent **}. | |
529 | ||
04b9968b UD |
530 | The return value of @code{alphasort} is less than, equal to, or greater |
531 | than zero depending on the order of the two entries @var{a} and @var{b}. | |
0d8733c4 UD |
532 | @end deftypefun |
533 | ||
5679cdb6 UD |
534 | @comment dirent.h |
535 | @comment GNU | |
1f205a47 | 536 | @deftypefun int versionsort (const void *@var{a}, const void *@var{b}) |
04b9968b | 537 | The @code{versionsort} function is like @code{alphasort} except that it |
1f205a47 UD |
538 | uses the @code{strverscmp} function internally. |
539 | @end deftypefun | |
540 | ||
5679cdb6 UD |
541 | If the filesystem supports large files we cannot use the @code{scandir} |
542 | anymore since the @code{dirent} structure might not able to contain all | |
543 | the information. The LFS provides the new type @w{@code{struct | |
544 | dirent64}}. To use this we need a new function. | |
545 | ||
546 | @comment dirent.h | |
547 | @comment GNU | |
548 | @deftypefun int scandir64 (const char *@var{dir}, struct dirent64 ***@var{namelist}, int (*@var{selector}) (const struct dirent64 *), int (*@var{cmp}) (const void *, const void *)) | |
549 | The @code{scandir64} function works like the @code{scandir} function | |
04b9968b UD |
550 | except that the directory entries it returns are described by elements |
551 | of type @w{@code{struct dirent64}}. The function pointed to by | |
552 | @var{selector} is again used to select the desired entries, except that | |
5679cdb6 | 553 | @var{selector} now must point to a function which takes a |
789b13c4 | 554 | @w{@code{struct dirent64 *}} parameter. |
5679cdb6 | 555 | |
04b9968b UD |
556 | Similarly the @var{cmp} function should expect its two arguments to be |
557 | of type @code{struct dirent64 **}. | |
5679cdb6 UD |
558 | @end deftypefun |
559 | ||
04b9968b UD |
560 | As @var{cmp} is now a function of a different type, the functions |
561 | @code{alphasort} and @code{versionsort} cannot be supplied for that | |
562 | argument. Instead we provide the two replacement functions below. | |
5679cdb6 UD |
563 | |
564 | @comment dirent.h | |
565 | @comment GNU | |
566 | @deftypefun int alphasort64 (const void *@var{a}, const void *@var{b}) | |
567 | The @code{alphasort64} function behaves like the @code{strcoll} function | |
568 | (@pxref{String/Array Comparison}). The difference is that the arguments | |
569 | are not string pointers but instead they are of type | |
570 | @code{struct dirent64 **}. | |
571 | ||
789b13c4 UD |
572 | Return value of @code{alphasort64} is less than, equal to, or greater |
573 | than zero depending on the order of the two entries @var{a} and @var{b}. | |
5679cdb6 UD |
574 | @end deftypefun |
575 | ||
576 | @comment dirent.h | |
577 | @comment GNU | |
578 | @deftypefun int versionsort64 (const void *@var{a}, const void *@var{b}) | |
579 | The @code{versionsort64} function is like @code{alphasort64}, excepted that it | |
580 | uses the @code{strverscmp} function internally. | |
581 | @end deftypefun | |
582 | ||
04b9968b | 583 | It is important not to mix the use of @code{scandir} and the 64-bit |
5679cdb6 | 584 | comparison functions or vice versa. There are systems on which this |
68b50604 | 585 | works but on others it will fail miserably. |
5679cdb6 | 586 | |
0d8733c4 UD |
587 | @node Simple Directory Lister Mark II |
588 | @subsection Simple Program to List a Directory, Mark II | |
589 | ||
590 | Here is a revised version of the directory lister found above | |
591 | (@pxref{Simple Directory Lister}). Using the @code{scandir} function we | |
04b9968b UD |
592 | can avoid the functions which work directly with the directory contents. |
593 | After the call the returned entries are available for direct use. | |
0d8733c4 UD |
594 | |
595 | @smallexample | |
596 | @include dir2.c.texi | |
597 | @end smallexample | |
598 | ||
04b9968b UD |
599 | Note the simple selector function in this example. Since we want to see |
600 | all directory entries we always return @code{1}. | |
0d8733c4 UD |
601 | |
602 | ||
04b9968b UD |
603 | @node Working with Directory Trees |
604 | @section Working with Directory Trees | |
f2ea0f5b UD |
605 | @cindex directory hierarchy |
606 | @cindex hierarchy, directory | |
2604afb1 UD |
607 | @cindex tree, directory |
608 | ||
04b9968b UD |
609 | The functions described so far for handling the files in a directory |
610 | have allowed you to either retrieve the information bit by bit, or to | |
611 | process all the files as a group (see @code{scandir}). Sometimes it is | |
612 | useful to process whole hierarchies of directories and their contained | |
613 | files. The X/Open specification defines two functions to do this. The | |
614 | simpler form is derived from an early definition in @w{System V} systems | |
615 | and therefore this function is available on SVID-derived systems. The | |
616 | prototypes and required definitions can be found in the @file{ftw.h} | |
617 | header. | |
618 | ||
619 | There are four functions in this family: @code{ftw}, @code{nftw} and | |
620 | their 64-bit counterparts @code{ftw64} and @code{nftw64}. These | |
621 | functions take as one of their arguments a pointer to a callback | |
622 | function of the appropriate type. | |
2604afb1 | 623 | |
a3a4a74e UD |
624 | @comment ftw.h |
625 | @comment GNU | |
2604afb1 UD |
626 | @deftp {Data Type} __ftw_func_t |
627 | ||
628 | @smallexample | |
629 | int (*) (const char *, const struct stat *, int) | |
630 | @end smallexample | |
631 | ||
04b9968b UD |
632 | The type of callback functions given to the @code{ftw} function. The |
633 | first parameter points to the file name, the second parameter to an | |
634 | object of type @code{struct stat} which is filled in for the file named | |
635 | in the first parameter. | |
2604afb1 UD |
636 | |
637 | @noindent | |
04b9968b | 638 | The last parameter is a flag giving more information about the current |
2604afb1 UD |
639 | file. It can have the following values: |
640 | ||
a3a4a74e | 641 | @vtable @code |
2604afb1 | 642 | @item FTW_F |
04b9968b UD |
643 | The item is either a normal file or a file which does not fit into one |
644 | of the following categories. This could be special files, sockets etc. | |
2604afb1 | 645 | @item FTW_D |
04b9968b | 646 | The item is a directory. |
2604afb1 | 647 | @item FTW_NS |
04b9968b UD |
648 | The @code{stat} call failed and so the information pointed to by the |
649 | second paramater is invalid. | |
2604afb1 UD |
650 | @item FTW_DNR |
651 | The item is a directory which cannot be read. | |
652 | @item FTW_SL | |
653 | The item is a symbolic link. Since symbolic links are normally followed | |
654 | seeing this value in a @code{ftw} callback function means the referenced | |
655 | file does not exist. The situation for @code{nftw} is different. | |
656 | ||
657 | This value is only available if the program is compiled with | |
658 | @code{_BSD_SOURCE} or @code{_XOPEN_EXTENDED} defined before including | |
659 | the first header. The original SVID systems do not have symbolic links. | |
a3a4a74e UD |
660 | @end vtable |
661 | ||
662 | If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
04b9968b | 663 | type is in fact @code{__ftw64_func_t} since this mode changes |
a3a4a74e | 664 | @code{struct stat} to be @code{struct stat64}. |
2604afb1 UD |
665 | @end deftp |
666 | ||
04b9968b | 667 | For the LFS interface and for use in the function @code{ftw64}, the |
a3a4a74e UD |
668 | header @file{ftw.h} defines another function type. |
669 | ||
670 | @comment ftw.h | |
671 | @comment GNU | |
672 | @deftp {Data Type} __ftw64_func_t | |
673 | ||
674 | @smallexample | |
675 | int (*) (const char *, const struct stat64 *, int) | |
676 | @end smallexample | |
677 | ||
678 | This type is used just like @code{__ftw_func_t} for the callback | |
04b9968b UD |
679 | function, but this time is called from @code{ftw64}. The second |
680 | parameter to the function is a pointer to a variable of type | |
a3a4a74e UD |
681 | @code{struct stat64} which is able to represent the larger values. |
682 | @end deftp | |
683 | ||
684 | @comment ftw.h | |
685 | @comment GNU | |
2604afb1 UD |
686 | @deftp {Data Type} __nftw_func_t |
687 | ||
688 | @smallexample | |
689 | int (*) (const char *, const struct stat *, int, struct FTW *) | |
690 | @end smallexample | |
691 | ||
692 | @vindex FTW_DP | |
693 | @vindex FTW_SLN | |
04b9968b UD |
694 | The first three arguments are the same as for the @code{__ftw_func_t} |
695 | type. However for the third argument some additional values are defined | |
696 | to allow finer differentiation: | |
2604afb1 UD |
697 | @table @code |
698 | @item FTW_DP | |
699 | The current item is a directory and all subdirectories have already been | |
700 | visited and reported. This flag is returned instead of @code{FTW_D} if | |
04b9968b | 701 | the @code{FTW_DEPTH} flag is passed to @code{nftw} (see below). |
2604afb1 UD |
702 | @item FTW_SLN |
703 | The current item is a stale symbolic link. The file it points to does | |
704 | not exist. | |
705 | @end table | |
706 | ||
707 | The last parameter of the callback function is a pointer to a structure | |
708 | with some extra information as described below. | |
a3a4a74e UD |
709 | |
710 | If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
04b9968b | 711 | type is in fact @code{__nftw64_func_t} since this mode changes |
a3a4a74e | 712 | @code{struct stat} to be @code{struct stat64}. |
2604afb1 UD |
713 | @end deftp |
714 | ||
a3a4a74e UD |
715 | For the LFS interface there is also a variant of this data type |
716 | available which has to be used with the @code{nftw64} function. | |
717 | ||
718 | @comment ftw.h | |
719 | @comment GNU | |
720 | @deftp {Data Type} __nftw64_func_t | |
721 | ||
722 | @smallexample | |
723 | int (*) (const char *, const struct stat64 *, int, struct FTW *) | |
724 | @end smallexample | |
725 | ||
726 | This type is used just like @code{__nftw_func_t} for the callback | |
04b9968b UD |
727 | function, but this time is called from @code{nftw64}. The second |
728 | parameter to the function is this time a pointer to a variable of type | |
a3a4a74e UD |
729 | @code{struct stat64} which is able to represent the larger values. |
730 | @end deftp | |
731 | ||
732 | @comment ftw.h | |
733 | @comment XPG4.2 | |
2604afb1 | 734 | @deftp {Data Type} {struct FTW} |
04b9968b UD |
735 | The information contained in this structure helps in interpreting the |
736 | name parameter and gives some information about the current state of the | |
737 | traversal of the directory hierarchy. | |
2604afb1 UD |
738 | |
739 | @table @code | |
740 | @item int base | |
04b9968b UD |
741 | The value is the offset into the string passed in the first parameter to |
742 | the callback function of the beginning of the file name. The rest of | |
743 | the string is the path of the file. This information is especially | |
744 | important if the @code{FTW_CHDIR} flag was set in calling @code{nftw} | |
745 | since then the current directory is the one the current item is found | |
746 | in. | |
2604afb1 | 747 | @item int level |
04b9968b UD |
748 | Whilst processing, the code tracks how many directories down it has gone |
749 | to find the current file. This nesting level starts at @math{0} for | |
750 | files in the initial directory (or is zero for the initial file if a | |
751 | file was passed). | |
2604afb1 UD |
752 | @end table |
753 | @end deftp | |
754 | ||
755 | ||
756 | @comment ftw.h | |
757 | @comment SVID | |
758 | @deftypefun int ftw (const char *@var{filename}, __ftw_func_t @var{func}, int @var{descriptors}) | |
759 | The @code{ftw} function calls the callback function given in the | |
760 | parameter @var{func} for every item which is found in the directory | |
761 | specified by @var{filename} and all directories below. The function | |
762 | follows symbolic links if necessary but does not process an item twice. | |
04b9968b UD |
763 | If @var{filename} is not a directory then it itself is the only object |
764 | returned to the callback function. | |
2604afb1 | 765 | |
04b9968b UD |
766 | The file name passed to the callback function is constructed by taking |
767 | the @var{filename} parameter and appending the names of all passed | |
2604afb1 | 768 | directories and then the local file name. So the callback function can |
04b9968b UD |
769 | use this parameter to access the file. @code{ftw} also calls |
770 | @code{stat} for the file and passes that information on to the callback | |
771 | function. If this @code{stat} call was not successful the failure is | |
772 | indicated by setting the third argument of the callback function to | |
773 | @code{FTW_NS}. Otherwise it is set according to the description given | |
774 | in the account of @code{__ftw_func_t} above. | |
2604afb1 UD |
775 | |
776 | The callback function is expected to return @math{0} to indicate that no | |
04b9968b UD |
777 | error occurred and that processing should continue. If an error |
778 | occurred in the callback function or it wants @code{ftw} to return | |
779 | immediately, the callback function can return a value other than | |
2604afb1 | 780 | @math{0}. This is the only correct way to stop the function. The |
04b9968b UD |
781 | program must not use @code{setjmp} or similar techniques to continue |
782 | from another place. This would leave resources allocated by the | |
783 | @code{ftw} function unfreed. | |
784 | ||
785 | The @var{descriptors} parameter to @code{ftw} specifies how many file | |
786 | descriptors it is allowed to consume. The function runs faster the more | |
787 | descriptors it can use. For each level in the directory hierarchy at | |
788 | most one descriptor is used, but for very deep ones any limit on open | |
789 | file descriptors for the process or the system may be exceeded. | |
790 | Moreover, file descriptor limits in a multi-threaded program apply to | |
791 | all the threads as a group, and therefore it is a good idea to supply a | |
792 | reasonable limit to the number of open descriptors. | |
2604afb1 UD |
793 | |
794 | The return value of the @code{ftw} function is @math{0} if all callback | |
795 | function calls returned @math{0} and all actions performed by the | |
04b9968b UD |
796 | @code{ftw} succeeded. If a function call failed (other than calling |
797 | @code{stat} on an item) the function returns @math{-1}. If a callback | |
2604afb1 UD |
798 | function returns a value other than @math{0} this value is returned as |
799 | the return value of @code{ftw}. | |
a3a4a74e UD |
800 | |
801 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
04b9968b | 802 | 32-bit system this function is in fact @code{ftw64}, i.e. the LFS |
a3a4a74e UD |
803 | interface transparently replaces the old interface. |
804 | @end deftypefun | |
805 | ||
806 | @comment ftw.h | |
807 | @comment Unix98 | |
808 | @deftypefun int ftw64 (const char *@var{filename}, __ftw64_func_t @var{func}, int @var{descriptors}) | |
809 | This function is similar to @code{ftw} but it can work on filesystems | |
04b9968b UD |
810 | with large files. File information is reported using a variable of type |
811 | @code{struct stat64} which is passed by reference to the callback | |
812 | function. | |
a3a4a74e UD |
813 | |
814 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
04b9968b | 815 | 32-bit system this function is available under the name @code{ftw} and |
a3a4a74e | 816 | transparently replaces the old implementation. |
2604afb1 UD |
817 | @end deftypefun |
818 | ||
819 | @comment ftw.h | |
820 | @comment XPG4.2 | |
821 | @deftypefun int nftw (const char *@var{filename}, __nftw_func_t @var{func}, int @var{descriptors}, int @var{flag}) | |
04b9968b UD |
822 | The @code{nftw} function works like the @code{ftw} functions. They call |
823 | the callback function @var{func} for all items found in the directory | |
2604afb1 UD |
824 | @var{filename} and below. At most @var{descriptors} file descriptors |
825 | are consumed during the @code{nftw} call. | |
826 | ||
04b9968b UD |
827 | One difference is that the callback function is of a different type. It |
828 | is of type @w{@code{struct FTW *}} and provides the callback function | |
829 | with the extra information described above. | |
2604afb1 | 830 | |
04b9968b UD |
831 | A second difference is that @code{nftw} takes a fourth argument, which |
832 | is @math{0} or a bitwise-OR combination of any of the following values. | |
2604afb1 | 833 | |
a3a4a74e | 834 | @vtable @code |
2604afb1 | 835 | @item FTW_PHYS |
04b9968b UD |
836 | While traversing the directory symbolic links are not followed. Instead |
837 | symbolic links are reported using the @code{FTW_SL} value for the type | |
838 | parameter to the callback function. If the file referenced by a | |
d01d6319 | 839 | symbolic link does not exist @code{FTW_SLN} is returned instead. |
2604afb1 UD |
840 | @item FTW_MOUNT |
841 | The callback function is only called for items which are on the same | |
04b9968b | 842 | mounted filesystem as the directory given by the @var{filename} |
2604afb1 UD |
843 | parameter to @code{nftw}. |
844 | @item FTW_CHDIR | |
845 | If this flag is given the current working directory is changed to the | |
04b9968b UD |
846 | directory of the reported object before the callback function is called. |
847 | When @code{ntfw} finally returns the current directory is restored to | |
848 | its original value. | |
2604afb1 | 849 | @item FTW_DEPTH |
04b9968b UD |
850 | If this option is specified then all subdirectories and files within |
851 | them are processed before processing the top directory itself | |
852 | (depth-first processing). This also means the type flag given to the | |
853 | callback function is @code{FTW_DP} and not @code{FTW_D}. | |
a3a4a74e | 854 | @end vtable |
2604afb1 UD |
855 | |
856 | The return value is computed in the same way as for @code{ftw}. | |
04b9968b UD |
857 | @code{nftw} returns @math{0} if no failures occurred and all callback |
858 | functions returned @math{0}. In case of internal errors, such as memory | |
859 | problems, the return value is @math{-1} and @var{errno} is set | |
860 | accordingly. If the return value of a callback invocation was non-zero | |
861 | then that value is returned. | |
a3a4a74e UD |
862 | |
863 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
04b9968b | 864 | 32-bit system this function is in fact @code{nftw64}, i.e. the LFS |
a3a4a74e UD |
865 | interface transparently replaces the old interface. |
866 | @end deftypefun | |
867 | ||
868 | @comment ftw.h | |
869 | @comment Unix98 | |
870 | @deftypefun int nftw64 (const char *@var{filename}, __nftw64_func_t @var{func}, int @var{descriptors}, int @var{flag}) | |
871 | This function is similar to @code{nftw} but it can work on filesystems | |
04b9968b UD |
872 | with large files. File information is reported using a variable of type |
873 | @code{struct stat64} which is passed by reference to the callback | |
874 | function. | |
a3a4a74e UD |
875 | |
876 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
04b9968b | 877 | 32-bit system this function is available under the name @code{nftw} and |
a3a4a74e | 878 | transparently replaces the old implementation. |
2604afb1 UD |
879 | @end deftypefun |
880 | ||
881 | ||
28f540f4 RM |
882 | @node Hard Links |
883 | @section Hard Links | |
884 | @cindex hard link | |
885 | @cindex link, hard | |
886 | @cindex multiple names for one file | |
887 | @cindex file names, multiple | |
888 | ||
889 | In POSIX systems, one file can have many names at the same time. All of | |
890 | the names are equally real, and no one of them is preferred to the | |
891 | others. | |
892 | ||
893 | To add a name to a file, use the @code{link} function. (The new name is | |
894 | also called a @dfn{hard link} to the file.) Creating a new link to a | |
895 | file does not copy the contents of the file; it simply makes a new name | |
896 | by which the file can be known, in addition to the file's existing name | |
897 | or names. | |
898 | ||
3081378b | 899 | One file can have names in several directories, so the organization |
28f540f4 RM |
900 | of the file system is not a strict hierarchy or tree. |
901 | ||
902 | In most implementations, it is not possible to have hard links to the | |
903 | same file in multiple file systems. @code{link} reports an error if you | |
904 | try to make a hard link to the file from another file system when this | |
905 | cannot be done. | |
906 | ||
907 | The prototype for the @code{link} function is declared in the header | |
908 | file @file{unistd.h}. | |
909 | @pindex unistd.h | |
910 | ||
911 | @comment unistd.h | |
912 | @comment POSIX.1 | |
913 | @deftypefun int link (const char *@var{oldname}, const char *@var{newname}) | |
914 | The @code{link} function makes a new link to the existing file named by | |
915 | @var{oldname}, under the new name @var{newname}. | |
916 | ||
917 | This function returns a value of @code{0} if it is successful and | |
918 | @code{-1} on failure. In addition to the usual file name errors | |
919 | (@pxref{File Name Errors}) for both @var{oldname} and @var{newname}, the | |
920 | following @code{errno} error conditions are defined for this function: | |
921 | ||
922 | @table @code | |
923 | @item EACCES | |
04b9968b UD |
924 | You are not allowed to write to the directory in which the new link is |
925 | to be written. | |
d68171ed | 926 | @ignore |
28f540f4 RM |
927 | Some implementations also require that the existing file be accessible |
928 | by the caller, and use this error to report failure for that reason. | |
929 | @end ignore | |
930 | ||
931 | @item EEXIST | |
932 | There is already a file named @var{newname}. If you want to replace | |
933 | this link with a new link, you must remove the old link explicitly first. | |
934 | ||
935 | @item EMLINK | |
936 | There are already too many links to the file named by @var{oldname}. | |
937 | (The maximum number of links to a file is @w{@code{LINK_MAX}}; see | |
938 | @ref{Limits for Files}.) | |
939 | ||
940 | @item ENOENT | |
941 | The file named by @var{oldname} doesn't exist. You can't make a link to | |
942 | a file that doesn't exist. | |
943 | ||
944 | @item ENOSPC | |
945 | The directory or file system that would contain the new link is full | |
946 | and cannot be extended. | |
947 | ||
948 | @item EPERM | |
949 | In the GNU system and some others, you cannot make links to directories. | |
950 | Many systems allow only privileged users to do so. This error | |
951 | is used to report the problem. | |
952 | ||
953 | @item EROFS | |
954 | The directory containing the new link can't be modified because it's on | |
955 | a read-only file system. | |
956 | ||
957 | @item EXDEV | |
958 | The directory specified in @var{newname} is on a different file system | |
959 | than the existing file. | |
960 | ||
961 | @item EIO | |
962 | A hardware error occurred while trying to read or write the to filesystem. | |
963 | @end table | |
964 | @end deftypefun | |
965 | ||
966 | @node Symbolic Links | |
967 | @section Symbolic Links | |
968 | @cindex soft link | |
969 | @cindex link, soft | |
970 | @cindex symbolic link | |
971 | @cindex link, symbolic | |
972 | ||
973 | The GNU system supports @dfn{soft links} or @dfn{symbolic links}. This | |
974 | is a kind of ``file'' that is essentially a pointer to another file | |
975 | name. Unlike hard links, symbolic links can be made to directories or | |
976 | across file systems with no restrictions. You can also make a symbolic | |
977 | link to a name which is not the name of any file. (Opening this link | |
978 | will fail until a file by that name is created.) Likewise, if the | |
979 | symbolic link points to an existing file which is later deleted, the | |
980 | symbolic link continues to point to the same file name even though the | |
981 | name no longer names any file. | |
982 | ||
983 | The reason symbolic links work the way they do is that special things | |
984 | happen when you try to open the link. The @code{open} function realizes | |
985 | you have specified the name of a link, reads the file name contained in | |
986 | the link, and opens that file name instead. The @code{stat} function | |
987 | likewise operates on the file that the symbolic link points to, instead | |
988 | of on the link itself. | |
989 | ||
990 | By contrast, other operations such as deleting or renaming the file | |
991 | operate on the link itself. The functions @code{readlink} and | |
992 | @code{lstat} also refrain from following symbolic links, because their | |
04b9968b UD |
993 | purpose is to obtain information about the link. @code{link}, the |
994 | function that makes a hard link, does too. It makes a hard link to the | |
28f540f4 RM |
995 | symbolic link, which one rarely wants. |
996 | ||
997 | Prototypes for the functions listed in this section are in | |
998 | @file{unistd.h}. | |
999 | @pindex unistd.h | |
1000 | ||
1001 | @comment unistd.h | |
1002 | @comment BSD | |
1003 | @deftypefun int symlink (const char *@var{oldname}, const char *@var{newname}) | |
1004 | The @code{symlink} function makes a symbolic link to @var{oldname} named | |
1005 | @var{newname}. | |
1006 | ||
1007 | The normal return value from @code{symlink} is @code{0}. A return value | |
1008 | of @code{-1} indicates an error. In addition to the usual file name | |
1009 | syntax errors (@pxref{File Name Errors}), the following @code{errno} | |
1010 | error conditions are defined for this function: | |
1011 | ||
1012 | @table @code | |
1013 | @item EEXIST | |
1014 | There is already an existing file named @var{newname}. | |
1015 | ||
1016 | @item EROFS | |
1017 | The file @var{newname} would exist on a read-only file system. | |
1018 | ||
1019 | @item ENOSPC | |
1020 | The directory or file system cannot be extended to make the new link. | |
1021 | ||
1022 | @item EIO | |
1023 | A hardware error occurred while reading or writing data on the disk. | |
1024 | ||
1025 | @ignore | |
1026 | @comment not sure about these | |
1027 | @item ELOOP | |
1028 | There are too many levels of indirection. This can be the result of | |
1029 | circular symbolic links to directories. | |
1030 | ||
1031 | @item EDQUOT | |
1032 | The new link can't be created because the user's disk quota has been | |
1033 | exceeded. | |
1034 | @end ignore | |
1035 | @end table | |
1036 | @end deftypefun | |
1037 | ||
1038 | @comment unistd.h | |
1039 | @comment BSD | |
1040 | @deftypefun int readlink (const char *@var{filename}, char *@var{buffer}, size_t @var{size}) | |
1041 | The @code{readlink} function gets the value of the symbolic link | |
1042 | @var{filename}. The file name that the link points to is copied into | |
1043 | @var{buffer}. This file name string is @emph{not} null-terminated; | |
1044 | @code{readlink} normally returns the number of characters copied. The | |
1045 | @var{size} argument specifies the maximum number of characters to copy, | |
1046 | usually the allocation size of @var{buffer}. | |
1047 | ||
1048 | If the return value equals @var{size}, you cannot tell whether or not | |
1049 | there was room to return the entire name. So make a bigger buffer and | |
1050 | call @code{readlink} again. Here is an example: | |
1051 | ||
1052 | @smallexample | |
1053 | char * | |
1054 | readlink_malloc (char *filename) | |
1055 | @{ | |
1056 | int size = 100; | |
1057 | ||
1058 | while (1) | |
1059 | @{ | |
1060 | char *buffer = (char *) xmalloc (size); | |
1061 | int nchars = readlink (filename, buffer, size); | |
1062 | if (nchars < size) | |
1063 | return buffer; | |
1064 | free (buffer); | |
1065 | size *= 2; | |
1066 | @} | |
1067 | @} | |
1068 | @end smallexample | |
1069 | ||
1070 | @c @group Invalid outside example. | |
1071 | A value of @code{-1} is returned in case of error. In addition to the | |
1072 | usual file name errors (@pxref{File Name Errors}), the following | |
1073 | @code{errno} error conditions are defined for this function: | |
1074 | ||
1075 | @table @code | |
1076 | @item EINVAL | |
1077 | The named file is not a symbolic link. | |
1078 | ||
1079 | @item EIO | |
1080 | A hardware error occurred while reading or writing data on the disk. | |
1081 | @end table | |
1082 | @c @end group | |
1083 | @end deftypefun | |
1084 | ||
1085 | @node Deleting Files | |
1086 | @section Deleting Files | |
1087 | @cindex deleting a file | |
1088 | @cindex removing a file | |
1089 | @cindex unlinking a file | |
1090 | ||
04b9968b | 1091 | You can delete a file with @code{unlink} or @code{remove}. |
28f540f4 RM |
1092 | |
1093 | Deletion actually deletes a file name. If this is the file's only name, | |
04b9968b UD |
1094 | then the file is deleted as well. If the file has other remaining names |
1095 | (@pxref{Hard Links}), it remains accessible under those names. | |
28f540f4 RM |
1096 | |
1097 | @comment unistd.h | |
1098 | @comment POSIX.1 | |
1099 | @deftypefun int unlink (const char *@var{filename}) | |
1100 | The @code{unlink} function deletes the file name @var{filename}. If | |
1101 | this is a file's sole name, the file itself is also deleted. (Actually, | |
1102 | if any process has the file open when this happens, deletion is | |
1103 | postponed until all processes have closed the file.) | |
1104 | ||
1105 | @pindex unistd.h | |
1106 | The function @code{unlink} is declared in the header file @file{unistd.h}. | |
1107 | ||
1108 | This function returns @code{0} on successful completion, and @code{-1} | |
1109 | on error. In addition to the usual file name errors | |
d68171ed | 1110 | (@pxref{File Name Errors}), the following @code{errno} error conditions are |
28f540f4 RM |
1111 | defined for this function: |
1112 | ||
1113 | @table @code | |
1114 | @item EACCES | |
1115 | Write permission is denied for the directory from which the file is to be | |
1116 | removed, or the directory has the sticky bit set and you do not own the file. | |
1117 | ||
1118 | @item EBUSY | |
1119 | This error indicates that the file is being used by the system in such a | |
1120 | way that it can't be unlinked. For example, you might see this error if | |
1121 | the file name specifies the root directory or a mount point for a file | |
1122 | system. | |
1123 | ||
1124 | @item ENOENT | |
1125 | The file name to be deleted doesn't exist. | |
1126 | ||
1127 | @item EPERM | |
04b9968b UD |
1128 | On some systems @code{unlink} cannot be used to delete the name of a |
1129 | directory, or at least can only be used this way by a privileged user. | |
1130 | To avoid such problems, use @code{rmdir} to delete directories. (In the | |
1131 | GNU system @code{unlink} can never delete the name of a directory.) | |
28f540f4 RM |
1132 | |
1133 | @item EROFS | |
04b9968b UD |
1134 | The directory containing the file name to be deleted is on a read-only |
1135 | file system and can't be modified. | |
28f540f4 RM |
1136 | @end table |
1137 | @end deftypefun | |
1138 | ||
1139 | @comment unistd.h | |
1140 | @comment POSIX.1 | |
1141 | @deftypefun int rmdir (const char *@var{filename}) | |
1142 | @cindex directories, deleting | |
1143 | @cindex deleting a directory | |
1144 | The @code{rmdir} function deletes a directory. The directory must be | |
1145 | empty before it can be removed; in other words, it can only contain | |
1146 | entries for @file{.} and @file{..}. | |
1147 | ||
1148 | In most other respects, @code{rmdir} behaves like @code{unlink}. There | |
1149 | are two additional @code{errno} error conditions defined for | |
1150 | @code{rmdir}: | |
1151 | ||
1152 | @table @code | |
1153 | @item ENOTEMPTY | |
1154 | @itemx EEXIST | |
d68171ed | 1155 | The directory to be deleted is not empty. |
28f540f4 RM |
1156 | @end table |
1157 | ||
1158 | These two error codes are synonymous; some systems use one, and some use | |
1159 | the other. The GNU system always uses @code{ENOTEMPTY}. | |
1160 | ||
1161 | The prototype for this function is declared in the header file | |
1162 | @file{unistd.h}. | |
1163 | @pindex unistd.h | |
1164 | @end deftypefun | |
1165 | ||
1166 | @comment stdio.h | |
f65fd747 | 1167 | @comment ISO |
28f540f4 | 1168 | @deftypefun int remove (const char *@var{filename}) |
f65fd747 | 1169 | This is the @w{ISO C} function to remove a file. It works like |
28f540f4 RM |
1170 | @code{unlink} for files and like @code{rmdir} for directories. |
1171 | @code{remove} is declared in @file{stdio.h}. | |
1172 | @pindex stdio.h | |
1173 | @end deftypefun | |
1174 | ||
1175 | @node Renaming Files | |
1176 | @section Renaming Files | |
1177 | ||
1178 | The @code{rename} function is used to change a file's name. | |
1179 | ||
1180 | @cindex renaming a file | |
1181 | @comment stdio.h | |
f65fd747 | 1182 | @comment ISO |
28f540f4 | 1183 | @deftypefun int rename (const char *@var{oldname}, const char *@var{newname}) |
04b9968b | 1184 | The @code{rename} function renames the file @var{oldname} to |
28f540f4 | 1185 | @var{newname}. The file formerly accessible under the name |
04b9968b UD |
1186 | @var{oldname} is afterwards accessible as @var{newname} instead. (If |
1187 | the file had any other names aside from @var{oldname}, it continues to | |
1188 | have those names.) | |
28f540f4 | 1189 | |
04b9968b UD |
1190 | The directory containing the name @var{newname} must be on the same file |
1191 | system as the directory containing the name @var{oldname}. | |
28f540f4 RM |
1192 | |
1193 | One special case for @code{rename} is when @var{oldname} and | |
1194 | @var{newname} are two names for the same file. The consistent way to | |
04b9968b UD |
1195 | handle this case is to delete @var{oldname}. However, in this case |
1196 | POSIX requires that @code{rename} do nothing and report success---which | |
1197 | is inconsistent. We don't know what your operating system will do. | |
28f540f4 | 1198 | |
04b9968b | 1199 | If @var{oldname} is not a directory, then any existing file named |
28f540f4 RM |
1200 | @var{newname} is removed during the renaming operation. However, if |
1201 | @var{newname} is the name of a directory, @code{rename} fails in this | |
1202 | case. | |
1203 | ||
04b9968b | 1204 | If @var{oldname} is a directory, then either @var{newname} must not |
28f540f4 RM |
1205 | exist or it must name a directory that is empty. In the latter case, |
1206 | the existing directory named @var{newname} is deleted first. The name | |
1207 | @var{newname} must not specify a subdirectory of the directory | |
1208 | @code{oldname} which is being renamed. | |
1209 | ||
04b9968b UD |
1210 | One useful feature of @code{rename} is that the meaning of @var{newname} |
1211 | changes ``atomically'' from any previously existing file by that name to | |
1212 | its new meaning (i.e. the file that was called @var{oldname}). There is | |
1213 | no instant at which @var{newname} is non-existent ``in between'' the old | |
1214 | meaning and the new meaning. If there is a system crash during the | |
1215 | operation, it is possible for both names to still exist; but | |
1216 | @var{newname} will always be intact if it exists at all. | |
28f540f4 RM |
1217 | |
1218 | If @code{rename} fails, it returns @code{-1}. In addition to the usual | |
1219 | file name errors (@pxref{File Name Errors}), the following | |
1220 | @code{errno} error conditions are defined for this function: | |
1221 | ||
1222 | @table @code | |
1223 | @item EACCES | |
1224 | One of the directories containing @var{newname} or @var{oldname} | |
1225 | refuses write permission; or @var{newname} and @var{oldname} are | |
1226 | directories and write permission is refused for one of them. | |
1227 | ||
1228 | @item EBUSY | |
1229 | A directory named by @var{oldname} or @var{newname} is being used by | |
1230 | the system in a way that prevents the renaming from working. This includes | |
1231 | directories that are mount points for filesystems, and directories | |
1232 | that are the current working directories of processes. | |
1233 | ||
1234 | @item ENOTEMPTY | |
1235 | @itemx EEXIST | |
1236 | The directory @var{newname} isn't empty. The GNU system always returns | |
1237 | @code{ENOTEMPTY} for this, but some other systems return @code{EEXIST}. | |
1238 | ||
1239 | @item EINVAL | |
04b9968b | 1240 | @var{oldname} is a directory that contains @var{newname}. |
28f540f4 RM |
1241 | |
1242 | @item EISDIR | |
04b9968b | 1243 | @var{newname} is a directory but the @var{oldname} isn't. |
28f540f4 RM |
1244 | |
1245 | @item EMLINK | |
04b9968b UD |
1246 | The parent directory of @var{newname} would have too many links |
1247 | (entries). | |
28f540f4 RM |
1248 | |
1249 | @item ENOENT | |
04b9968b | 1250 | The file @var{oldname} doesn't exist. |
28f540f4 RM |
1251 | |
1252 | @item ENOSPC | |
1253 | The directory that would contain @var{newname} has no room for another | |
1254 | entry, and there is no space left in the file system to expand it. | |
1255 | ||
1256 | @item EROFS | |
1257 | The operation would involve writing to a directory on a read-only file | |
1258 | system. | |
1259 | ||
1260 | @item EXDEV | |
04b9968b | 1261 | The two file names @var{newname} and @var{oldname} are on different |
28f540f4 RM |
1262 | file systems. |
1263 | @end table | |
1264 | @end deftypefun | |
1265 | ||
1266 | @node Creating Directories | |
1267 | @section Creating Directories | |
1268 | @cindex creating a directory | |
1269 | @cindex directories, creating | |
1270 | ||
1271 | @pindex mkdir | |
1272 | Directories are created with the @code{mkdir} function. (There is also | |
1273 | a shell command @code{mkdir} which does the same thing.) | |
1274 | @c !!! umask | |
1275 | ||
1276 | @comment sys/stat.h | |
1277 | @comment POSIX.1 | |
1278 | @deftypefun int mkdir (const char *@var{filename}, mode_t @var{mode}) | |
04b9968b | 1279 | The @code{mkdir} function creates a new, empty directory with name |
28f540f4 RM |
1280 | @var{filename}. |
1281 | ||
1282 | The argument @var{mode} specifies the file permissions for the new | |
1283 | directory file. @xref{Permission Bits}, for more information about | |
1284 | this. | |
1285 | ||
1286 | A return value of @code{0} indicates successful completion, and | |
1287 | @code{-1} indicates failure. In addition to the usual file name syntax | |
1288 | errors (@pxref{File Name Errors}), the following @code{errno} error | |
1289 | conditions are defined for this function: | |
1290 | ||
1291 | @table @code | |
1292 | @item EACCES | |
1293 | Write permission is denied for the parent directory in which the new | |
1294 | directory is to be added. | |
1295 | ||
1296 | @item EEXIST | |
1297 | A file named @var{filename} already exists. | |
1298 | ||
1299 | @item EMLINK | |
04b9968b | 1300 | The parent directory has too many links (entries). |
28f540f4 RM |
1301 | |
1302 | Well-designed file systems never report this error, because they permit | |
1303 | more links than your disk could possibly hold. However, you must still | |
1304 | take account of the possibility of this error, as it could result from | |
1305 | network access to a file system on another machine. | |
1306 | ||
1307 | @item ENOSPC | |
1308 | The file system doesn't have enough room to create the new directory. | |
1309 | ||
1310 | @item EROFS | |
1311 | The parent directory of the directory being created is on a read-only | |
04b9968b | 1312 | file system and cannot be modified. |
28f540f4 RM |
1313 | @end table |
1314 | ||
1315 | To use this function, your program should include the header file | |
1316 | @file{sys/stat.h}. | |
1317 | @pindex sys/stat.h | |
1318 | @end deftypefun | |
1319 | ||
1320 | @node File Attributes | |
1321 | @section File Attributes | |
1322 | ||
1323 | @pindex ls | |
1324 | When you issue an @samp{ls -l} shell command on a file, it gives you | |
1325 | information about the size of the file, who owns it, when it was last | |
04b9968b UD |
1326 | modified, etc. These are called the @dfn{file attributes}, and are |
1327 | associated with the file itself and not a particular one of its names. | |
28f540f4 RM |
1328 | |
1329 | This section contains information about how you can inquire about and | |
04b9968b | 1330 | modify the attributes of a file. |
28f540f4 RM |
1331 | |
1332 | @menu | |
d68171ed | 1333 | * Attribute Meanings:: The names of the file attributes, |
28f540f4 RM |
1334 | and what their values mean. |
1335 | * Reading Attributes:: How to read the attributes of a file. | |
1336 | * Testing File Type:: Distinguishing ordinary files, | |
d68171ed | 1337 | directories, links... |
28f540f4 RM |
1338 | * File Owner:: How ownership for new files is determined, |
1339 | and how to change it. | |
1340 | * Permission Bits:: How information about a file's access | |
d68171ed | 1341 | mode is stored. |
28f540f4 RM |
1342 | * Access Permission:: How the system decides who can access a file. |
1343 | * Setting Permissions:: How permissions for new files are assigned, | |
1344 | and how to change them. | |
1345 | * Testing File Access:: How to find out if your process can | |
d68171ed | 1346 | access a file. |
28f540f4 | 1347 | * File Times:: About the time attributes of a file. |
7ce241a0 | 1348 | * File Size:: Manually changing the size of a file. |
28f540f4 RM |
1349 | @end menu |
1350 | ||
1351 | @node Attribute Meanings | |
04b9968b | 1352 | @subsection The meaning of the File Attributes |
28f540f4 RM |
1353 | @cindex status of a file |
1354 | @cindex attributes of a file | |
1355 | @cindex file attributes | |
1356 | ||
1357 | When you read the attributes of a file, they come back in a structure | |
1358 | called @code{struct stat}. This section describes the names of the | |
1359 | attributes, their data types, and what they mean. For the functions | |
1360 | to read the attributes of a file, see @ref{Reading Attributes}. | |
1361 | ||
1362 | The header file @file{sys/stat.h} declares all the symbols defined | |
1363 | in this section. | |
1364 | @pindex sys/stat.h | |
1365 | ||
1366 | @comment sys/stat.h | |
1367 | @comment POSIX.1 | |
1368 | @deftp {Data Type} {struct stat} | |
1369 | The @code{stat} structure type is used to return information about the | |
1370 | attributes of a file. It contains at least the following members: | |
1371 | ||
1372 | @table @code | |
1373 | @item mode_t st_mode | |
1374 | Specifies the mode of the file. This includes file type information | |
1375 | (@pxref{Testing File Type}) and the file permission bits | |
1376 | (@pxref{Permission Bits}). | |
1377 | ||
1378 | @item ino_t st_ino | |
1379 | The file serial number, which distinguishes this file from all other | |
1380 | files on the same device. | |
1381 | ||
1382 | @item dev_t st_dev | |
1383 | Identifies the device containing the file. The @code{st_ino} and | |
1384 | @code{st_dev}, taken together, uniquely identify the file. The | |
1385 | @code{st_dev} value is not necessarily consistent across reboots or | |
1386 | system crashes, however. | |
1387 | ||
1388 | @item nlink_t st_nlink | |
1389 | The number of hard links to the file. This count keeps track of how | |
1390 | many directories have entries for this file. If the count is ever | |
1391 | decremented to zero, then the file itself is discarded as soon as no | |
1392 | process still holds it open. Symbolic links are not counted in the | |
1393 | total. | |
1394 | ||
1395 | @item uid_t st_uid | |
1396 | The user ID of the file's owner. @xref{File Owner}. | |
1397 | ||
1398 | @item gid_t st_gid | |
1399 | The group ID of the file. @xref{File Owner}. | |
1400 | ||
1401 | @item off_t st_size | |
04b9968b UD |
1402 | This specifies the size of a regular file in bytes. For files that are |
1403 | really devices this field isn't usually meaningful. For symbolic links | |
1404 | this specifies the length of the file name the link refers to. | |
28f540f4 RM |
1405 | |
1406 | @item time_t st_atime | |
1407 | This is the last access time for the file. @xref{File Times}. | |
1408 | ||
1409 | @item unsigned long int st_atime_usec | |
1410 | This is the fractional part of the last access time for the file. | |
1411 | @xref{File Times}. | |
1412 | ||
1413 | @item time_t st_mtime | |
1414 | This is the time of the last modification to the contents of the file. | |
1415 | @xref{File Times}. | |
1416 | ||
1417 | @item unsigned long int st_mtime_usec | |
04b9968b | 1418 | This is the fractional part of the time of the last modification to the |
28f540f4 RM |
1419 | contents of the file. @xref{File Times}. |
1420 | ||
1421 | @item time_t st_ctime | |
1422 | This is the time of the last modification to the attributes of the file. | |
1423 | @xref{File Times}. | |
1424 | ||
1425 | @item unsigned long int st_ctime_usec | |
04b9968b | 1426 | This is the fractional part of the time of the last modification to the |
28f540f4 RM |
1427 | attributes of the file. @xref{File Times}. |
1428 | ||
1429 | @c !!! st_rdev | |
a3a4a74e | 1430 | @item blkcnt_t st_blocks |
28f540f4 RM |
1431 | This is the amount of disk space that the file occupies, measured in |
1432 | units of 512-byte blocks. | |
1433 | ||
1434 | The number of disk blocks is not strictly proportional to the size of | |
1435 | the file, for two reasons: the file system may use some blocks for | |
1436 | internal record keeping; and the file may be sparse---it may have | |
1437 | ``holes'' which contain zeros but do not actually take up space on the | |
1438 | disk. | |
1439 | ||
1440 | You can tell (approximately) whether a file is sparse by comparing this | |
1441 | value with @code{st_size}, like this: | |
1442 | ||
1443 | @smallexample | |
1444 | (st.st_blocks * 512 < st.st_size) | |
1445 | @end smallexample | |
1446 | ||
1447 | This test is not perfect because a file that is just slightly sparse | |
1448 | might not be detected as sparse at all. For practical applications, | |
1449 | this is not a problem. | |
1450 | ||
1451 | @item unsigned int st_blksize | |
1452 | The optimal block size for reading of writing this file, in bytes. You | |
1453 | might use this size for allocating the buffer space for reading of | |
1454 | writing the file. (This is unrelated to @code{st_blocks}.) | |
1455 | @end table | |
1456 | @end deftp | |
1457 | ||
04b9968b UD |
1458 | The extensions for the Large File Support (LFS) require, even on 32-bit |
1459 | machines, types which can handle file sizes up to @math{2^63}. | |
1460 | Therefore a new definition of @code{struct stat} is necessary. | |
a3a4a74e UD |
1461 | |
1462 | @comment sys/stat.h | |
1463 | @comment LFS | |
1464 | @deftp {Data Type} {struct stat64} | |
1465 | The members of this type are the same and have the same names as those | |
1466 | in @code{struct stat}. The only difference is that the members | |
1467 | @code{st_ino}, @code{st_size}, and @code{st_blocks} have a different | |
1468 | type to support larger values. | |
1469 | ||
1470 | @table @code | |
1471 | @item mode_t st_mode | |
1472 | Specifies the mode of the file. This includes file type information | |
1473 | (@pxref{Testing File Type}) and the file permission bits | |
1474 | (@pxref{Permission Bits}). | |
1475 | ||
1476 | @item ino64_t st_ino | |
1477 | The file serial number, which distinguishes this file from all other | |
1478 | files on the same device. | |
1479 | ||
1480 | @item dev_t st_dev | |
1481 | Identifies the device containing the file. The @code{st_ino} and | |
1482 | @code{st_dev}, taken together, uniquely identify the file. The | |
1483 | @code{st_dev} value is not necessarily consistent across reboots or | |
1484 | system crashes, however. | |
1485 | ||
1486 | @item nlink_t st_nlink | |
1487 | The number of hard links to the file. This count keeps track of how | |
1488 | many directories have entries for this file. If the count is ever | |
1489 | decremented to zero, then the file itself is discarded as soon as no | |
1490 | process still holds it open. Symbolic links are not counted in the | |
1491 | total. | |
1492 | ||
1493 | @item uid_t st_uid | |
1494 | The user ID of the file's owner. @xref{File Owner}. | |
1495 | ||
1496 | @item gid_t st_gid | |
1497 | The group ID of the file. @xref{File Owner}. | |
1498 | ||
1499 | @item off64_t st_size | |
04b9968b UD |
1500 | This specifies the size of a regular file in bytes. For files that are |
1501 | really devices this field isn't usually meaningful. For symbolic links | |
1502 | this specifies the length of the file name the link refers to. | |
a3a4a74e UD |
1503 | |
1504 | @item time_t st_atime | |
1505 | This is the last access time for the file. @xref{File Times}. | |
1506 | ||
1507 | @item unsigned long int st_atime_usec | |
1508 | This is the fractional part of the last access time for the file. | |
1509 | @xref{File Times}. | |
1510 | ||
1511 | @item time_t st_mtime | |
1512 | This is the time of the last modification to the contents of the file. | |
1513 | @xref{File Times}. | |
1514 | ||
1515 | @item unsigned long int st_mtime_usec | |
04b9968b | 1516 | This is the fractional part of the time of the last modification to the |
a3a4a74e UD |
1517 | contents of the file. @xref{File Times}. |
1518 | ||
1519 | @item time_t st_ctime | |
1520 | This is the time of the last modification to the attributes of the file. | |
1521 | @xref{File Times}. | |
1522 | ||
1523 | @item unsigned long int st_ctime_usec | |
04b9968b | 1524 | This is the fractional part of the time of the last modification to the |
a3a4a74e UD |
1525 | attributes of the file. @xref{File Times}. |
1526 | ||
1527 | @c !!! st_rdev | |
1528 | @item blkcnt64_t st_blocks | |
1529 | This is the amount of disk space that the file occupies, measured in | |
1530 | units of 512-byte blocks. | |
1531 | ||
1532 | @item unsigned int st_blksize | |
1533 | The optimal block size for reading of writing this file, in bytes. You | |
1534 | might use this size for allocating the buffer space for reading of | |
1535 | writing the file. (This is unrelated to @code{st_blocks}.) | |
1536 | @end table | |
1537 | @end deftp | |
1538 | ||
fc549258 UD |
1539 | Some of the file attributes have special data type names which exist |
1540 | specifically for those attributes. (They are all aliases for well-known | |
1541 | integer types that you know and love.) These typedef names are defined | |
1542 | in the header file @file{sys/types.h} as well as in @file{sys/stat.h}. | |
1543 | Here is a list of them. | |
1544 | ||
28f540f4 RM |
1545 | @comment sys/types.h |
1546 | @comment POSIX.1 | |
1547 | @deftp {Data Type} mode_t | |
1548 | This is an integer data type used to represent file modes. In the | |
1549 | GNU system, this is equivalent to @code{unsigned int}. | |
1550 | @end deftp | |
1551 | ||
1552 | @cindex inode number | |
1553 | @comment sys/types.h | |
1554 | @comment POSIX.1 | |
1555 | @deftp {Data Type} ino_t | |
1556 | This is an arithmetic data type used to represent file serial numbers. | |
1557 | (In Unix jargon, these are sometimes called @dfn{inode numbers}.) | |
1558 | In the GNU system, this type is equivalent to @code{unsigned long int}. | |
a3a4a74e UD |
1559 | |
1560 | If the source is compiled with @code{_FILE_OFFSET_BITS == 64} this type | |
1561 | is transparently replaced by @code{ino64_t}. | |
1562 | @end deftp | |
1563 | ||
1564 | @comment sys/types.h | |
1565 | @comment Unix98 | |
1566 | @deftp {Data Type} ino64_t | |
1567 | This is an arithmetic data type used to represent file serial numbers | |
1568 | for the use in LFS. In the GNU system, this type is equivalent to | |
1569 | @code{unsigned long longint}. | |
1570 | ||
1571 | When compiling with @code{_FILE_OFFSET_BITS == 64} this type is | |
1572 | available under the name @code{ino_t}. | |
28f540f4 RM |
1573 | @end deftp |
1574 | ||
1575 | @comment sys/types.h | |
1576 | @comment POSIX.1 | |
1577 | @deftp {Data Type} dev_t | |
1578 | This is an arithmetic data type used to represent file device numbers. | |
1579 | In the GNU system, this is equivalent to @code{int}. | |
1580 | @end deftp | |
1581 | ||
1582 | @comment sys/types.h | |
1583 | @comment POSIX.1 | |
1584 | @deftp {Data Type} nlink_t | |
1585 | This is an arithmetic data type used to represent file link counts. | |
1586 | In the GNU system, this is equivalent to @code{unsigned short int}. | |
1587 | @end deftp | |
1588 | ||
a3a4a74e UD |
1589 | @comment sys/types.h |
1590 | @comment Unix98 | |
1591 | @deftp {Data Type} blkcnt_t | |
1592 | This is an arithmetic data type used to represent block counts. | |
1593 | In the GNU system, this is equivalent to @code{unsigned long int}. | |
1594 | ||
1595 | If the source is compiled with @code{_FILE_OFFSET_BITS == 64} this type | |
1596 | is transparently replaced by @code{blkcnt64_t}. | |
1597 | @end deftp | |
1598 | ||
1599 | @comment sys/types.h | |
1600 | @comment Unix98 | |
1601 | @deftp {Data Type} blkcnt64_t | |
1602 | This is an arithmetic data type used to represent block counts for the | |
1603 | use in LFS. In the GNU system, this is equivalent to @code{unsigned | |
1604 | long long int}. | |
1605 | ||
1606 | When compiling with @code{_FILE_OFFSET_BITS == 64} this type is | |
1607 | available under the name @code{blkcnt_t}. | |
1608 | @end deftp | |
1609 | ||
28f540f4 RM |
1610 | @node Reading Attributes |
1611 | @subsection Reading the Attributes of a File | |
1612 | ||
1613 | To examine the attributes of files, use the functions @code{stat}, | |
1614 | @code{fstat} and @code{lstat}. They return the attribute information in | |
1615 | a @code{struct stat} object. All three functions are declared in the | |
1616 | header file @file{sys/stat.h}. | |
1617 | ||
1618 | @comment sys/stat.h | |
1619 | @comment POSIX.1 | |
1620 | @deftypefun int stat (const char *@var{filename}, struct stat *@var{buf}) | |
1621 | The @code{stat} function returns information about the attributes of the | |
04b9968b | 1622 | file named by @w{@var{filename}} in the structure pointed to by @var{buf}. |
28f540f4 RM |
1623 | |
1624 | If @var{filename} is the name of a symbolic link, the attributes you get | |
1625 | describe the file that the link points to. If the link points to a | |
04b9968b | 1626 | nonexistent file name, then @code{stat} fails reporting a nonexistent |
28f540f4 RM |
1627 | file. |
1628 | ||
04b9968b UD |
1629 | The return value is @code{0} if the operation is successful, or |
1630 | @code{-1} on failure. In addition to the usual file name errors | |
28f540f4 RM |
1631 | (@pxref{File Name Errors}, the following @code{errno} error conditions |
1632 | are defined for this function: | |
1633 | ||
1634 | @table @code | |
1635 | @item ENOENT | |
1636 | The file named by @var{filename} doesn't exist. | |
1637 | @end table | |
a3a4a74e UD |
1638 | |
1639 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
1640 | function is in fact @code{stat64} since the LFS interface transparently | |
1641 | replaces the normal implementation. | |
1642 | @end deftypefun | |
1643 | ||
1644 | @comment sys/stat.h | |
1645 | @comment Unix98 | |
1646 | @deftypefun int stat64 (const char *@var{filename}, struct stat64 *@var{buf}) | |
1647 | This function is similar to @code{stat} but it is also able to work on | |
04b9968b | 1648 | files larger then @math{2^31} bytes on 32-bit systems. To be able to do |
a3a4a74e UD |
1649 | this the result is stored in a variable of type @code{struct stat64} to |
1650 | which @var{buf} must point. | |
1651 | ||
1652 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
1653 | function is available under the name @code{stat} and so transparently | |
04b9968b | 1654 | replaces the interface for small files on 32-bit machines. |
28f540f4 RM |
1655 | @end deftypefun |
1656 | ||
1657 | @comment sys/stat.h | |
1658 | @comment POSIX.1 | |
1659 | @deftypefun int fstat (int @var{filedes}, struct stat *@var{buf}) | |
1660 | The @code{fstat} function is like @code{stat}, except that it takes an | |
1661 | open file descriptor as an argument instead of a file name. | |
1662 | @xref{Low-Level I/O}. | |
1663 | ||
1664 | Like @code{stat}, @code{fstat} returns @code{0} on success and @code{-1} | |
1665 | on failure. The following @code{errno} error conditions are defined for | |
1666 | @code{fstat}: | |
1667 | ||
1668 | @table @code | |
1669 | @item EBADF | |
1670 | The @var{filedes} argument is not a valid file descriptor. | |
1671 | @end table | |
a3a4a74e UD |
1672 | |
1673 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
1674 | function is in fact @code{fstat64} since the LFS interface transparently | |
1675 | replaces the normal implementation. | |
1676 | @end deftypefun | |
1677 | ||
1678 | @comment sys/stat.h | |
1679 | @comment Unix98 | |
1680 | @deftypefun int fstat64 (int @var{filedes}, struct stat64 *@var{buf}) | |
04b9968b UD |
1681 | This function is similar to @code{fstat} but is able to work on large |
1682 | files on 32-bit platforms. For large files the file descriptor | |
1683 | @var{filedes} should be obtained by @code{open64} or @code{creat64}. | |
a3a4a74e UD |
1684 | The @var{buf} pointer points to a variable of type @code{struct stat64} |
1685 | which is able to represent the larger values. | |
1686 | ||
1687 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
1688 | function is available under the name @code{fstat} and so transparently | |
04b9968b | 1689 | replaces the interface for small files on 32-bit machines. |
28f540f4 RM |
1690 | @end deftypefun |
1691 | ||
1692 | @comment sys/stat.h | |
1693 | @comment BSD | |
1694 | @deftypefun int lstat (const char *@var{filename}, struct stat *@var{buf}) | |
1695 | The @code{lstat} function is like @code{stat}, except that it does not | |
1696 | follow symbolic links. If @var{filename} is the name of a symbolic | |
04b9968b | 1697 | link, @code{lstat} returns information about the link itself; otherwise |
28f540f4 | 1698 | @code{lstat} works like @code{stat}. @xref{Symbolic Links}. |
a3a4a74e UD |
1699 | |
1700 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
1701 | function is in fact @code{lstat64} since the LFS interface transparently | |
1702 | replaces the normal implementation. | |
1703 | @end deftypefun | |
1704 | ||
1705 | @comment sys/stat.h | |
1706 | @comment Unix98 | |
1707 | @deftypefun int lstat64 (const char *@var{filename}, struct stat64 *@var{buf}) | |
1708 | This function is similar to @code{lstat} but it is also able to work on | |
04b9968b | 1709 | files larger then @math{2^31} bytes on 32-bit systems. To be able to do |
a3a4a74e UD |
1710 | this the result is stored in a variable of type @code{struct stat64} to |
1711 | which @var{buf} must point. | |
1712 | ||
1713 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this | |
1714 | function is available under the name @code{lstat} and so transparently | |
04b9968b | 1715 | replaces the interface for small files on 32-bit machines. |
28f540f4 RM |
1716 | @end deftypefun |
1717 | ||
1718 | @node Testing File Type | |
1719 | @subsection Testing the Type of a File | |
1720 | ||
1721 | The @dfn{file mode}, stored in the @code{st_mode} field of the file | |
1722 | attributes, contains two kinds of information: the file type code, and | |
1723 | the access permission bits. This section discusses only the type code, | |
04b9968b UD |
1724 | which you can use to tell whether the file is a directory, socket, |
1725 | symbolic link, and so on. For details about access permissions see | |
28f540f4 RM |
1726 | @ref{Permission Bits}. |
1727 | ||
04b9968b UD |
1728 | There are two ways you can access the file type information in a file |
1729 | mode. Firstly, for each file type there is a @dfn{predicate macro} | |
1730 | which examines a given file mode and returns whether it is of that type | |
1731 | or not. Secondly, you can mask out the rest of the file mode to leave | |
1732 | just the file type code, and compare this against constants for each of | |
1733 | the supported file types. | |
28f540f4 RM |
1734 | |
1735 | All of the symbols listed in this section are defined in the header file | |
1736 | @file{sys/stat.h}. | |
1737 | @pindex sys/stat.h | |
1738 | ||
1739 | The following predicate macros test the type of a file, given the value | |
1740 | @var{m} which is the @code{st_mode} field returned by @code{stat} on | |
1741 | that file: | |
1742 | ||
1743 | @comment sys/stat.h | |
1744 | @comment POSIX | |
1745 | @deftypefn Macro int S_ISDIR (mode_t @var{m}) | |
04b9968b | 1746 | This macro returns non-zero if the file is a directory. |
28f540f4 RM |
1747 | @end deftypefn |
1748 | ||
1749 | @comment sys/stat.h | |
1750 | @comment POSIX | |
1751 | @deftypefn Macro int S_ISCHR (mode_t @var{m}) | |
04b9968b | 1752 | This macro returns non-zero if the file is a character special file (a |
28f540f4 RM |
1753 | device like a terminal). |
1754 | @end deftypefn | |
1755 | ||
1756 | @comment sys/stat.h | |
1757 | @comment POSIX | |
1758 | @deftypefn Macro int S_ISBLK (mode_t @var{m}) | |
04b9968b | 1759 | This macro returns non-zero if the file is a block special file (a device |
28f540f4 RM |
1760 | like a disk). |
1761 | @end deftypefn | |
1762 | ||
1763 | @comment sys/stat.h | |
1764 | @comment POSIX | |
1765 | @deftypefn Macro int S_ISREG (mode_t @var{m}) | |
04b9968b | 1766 | This macro returns non-zero if the file is a regular file. |
28f540f4 RM |
1767 | @end deftypefn |
1768 | ||
1769 | @comment sys/stat.h | |
1770 | @comment POSIX | |
1771 | @deftypefn Macro int S_ISFIFO (mode_t @var{m}) | |
04b9968b | 1772 | This macro returns non-zero if the file is a FIFO special file, or a |
28f540f4 RM |
1773 | pipe. @xref{Pipes and FIFOs}. |
1774 | @end deftypefn | |
1775 | ||
1776 | @comment sys/stat.h | |
1777 | @comment GNU | |
1778 | @deftypefn Macro int S_ISLNK (mode_t @var{m}) | |
04b9968b | 1779 | This macro returns non-zero if the file is a symbolic link. |
28f540f4 RM |
1780 | @xref{Symbolic Links}. |
1781 | @end deftypefn | |
1782 | ||
1783 | @comment sys/stat.h | |
1784 | @comment GNU | |
1785 | @deftypefn Macro int S_ISSOCK (mode_t @var{m}) | |
04b9968b | 1786 | This macro returns non-zero if the file is a socket. @xref{Sockets}. |
28f540f4 RM |
1787 | @end deftypefn |
1788 | ||
f2ea0f5b | 1789 | An alternate non-POSIX method of testing the file type is supported for |
04b9968b | 1790 | compatibility with BSD. The mode can be bitwise AND-ed with |
28f540f4 | 1791 | @code{S_IFMT} to extract the file type code, and compared to the |
04b9968b | 1792 | appropriate constant. For example, |
28f540f4 RM |
1793 | |
1794 | @smallexample | |
1795 | S_ISCHR (@var{mode}) | |
1796 | @end smallexample | |
1797 | ||
1798 | @noindent | |
1799 | is equivalent to: | |
1800 | ||
1801 | @smallexample | |
1802 | ((@var{mode} & S_IFMT) == S_IFCHR) | |
1803 | @end smallexample | |
1804 | ||
1805 | @comment sys/stat.h | |
1806 | @comment BSD | |
1807 | @deftypevr Macro int S_IFMT | |
04b9968b | 1808 | This is a bit mask used to extract the file type code from a mode value. |
28f540f4 RM |
1809 | @end deftypevr |
1810 | ||
1811 | These are the symbolic names for the different file type codes: | |
1812 | ||
1813 | @table @code | |
1814 | @comment sys/stat.h | |
1815 | @comment BSD | |
1816 | @item S_IFDIR | |
1817 | @vindex S_IFDIR | |
04b9968b | 1818 | This is the file type constant of a directory file. |
28f540f4 RM |
1819 | |
1820 | @comment sys/stat.h | |
1821 | @comment BSD | |
1822 | @item S_IFCHR | |
1823 | @vindex S_IFCHR | |
04b9968b | 1824 | This is the file type constant of a character-oriented device file. |
28f540f4 RM |
1825 | |
1826 | @comment sys/stat.h | |
1827 | @comment BSD | |
1828 | @item S_IFBLK | |
1829 | @vindex S_IFBLK | |
04b9968b | 1830 | This is the file type constant of a block-oriented device file. |
28f540f4 RM |
1831 | |
1832 | @comment sys/stat.h | |
1833 | @comment BSD | |
1834 | @item S_IFREG | |
1835 | @vindex S_IFREG | |
04b9968b | 1836 | This is the file type constant of a regular file. |
28f540f4 RM |
1837 | |
1838 | @comment sys/stat.h | |
1839 | @comment BSD | |
1840 | @item S_IFLNK | |
1841 | @vindex S_IFLNK | |
04b9968b | 1842 | This is the file type constant of a symbolic link. |
28f540f4 RM |
1843 | |
1844 | @comment sys/stat.h | |
1845 | @comment BSD | |
1846 | @item S_IFSOCK | |
1847 | @vindex S_IFSOCK | |
04b9968b | 1848 | This is the file type constant of a socket. |
28f540f4 RM |
1849 | |
1850 | @comment sys/stat.h | |
1851 | @comment BSD | |
1852 | @item S_IFIFO | |
1853 | @vindex S_IFIFO | |
04b9968b | 1854 | This is the file type constant of a FIFO or pipe. |
28f540f4 RM |
1855 | @end table |
1856 | ||
d0db5a44 UD |
1857 | The POSIX.1b standard introduced a few more objects which possibly can |
1858 | be implemented as object in the filesystem. These are message queues, | |
1859 | semaphores, and shared memory objects. To allow differentiating these | |
1860 | objects from other files the POSIX standard introduces three new test | |
1861 | macros. But unlike the other macros it does not take the value of the | |
1862 | @code{st_mode} field as the parameter. Instead they expect a pointer to | |
1863 | the whole @code{struct stat} structure. | |
1864 | ||
1865 | @comment sys/stat.h | |
1866 | @comment POSIX | |
1867 | @deftypefn Macro int S_TYPEISMQ (struct stat @var{s}) | |
1868 | If the system implement POSIX message queues as distinct objects and the | |
1869 | file is a message queue object, this macro returns a non-zero value. | |
1870 | In all other cases the result is zero. | |
1871 | @end deftypefn | |
1872 | ||
1873 | @comment sys/stat.h | |
1874 | @comment POSIX | |
1875 | @deftypefn Macro int S_TYPEISSEM (struct stat @var{s}) | |
1876 | If the system implement POSIX semaphores as distinct objects and the | |
1877 | file is a semaphore object, this macro returns a non-zero value. | |
1878 | In all other cases the result is zero. | |
1879 | @end deftypefn | |
1880 | ||
1881 | @comment sys/stat.h | |
1882 | @comment POSIX | |
1883 | @deftypefn Macro int S_TYPEISSHM (struct stat @var{s}) | |
1884 | If the system implement POSIX shared memory objects as distinct objects | |
1885 | and the file is an shared memory object, this macro returns a non-zero | |
1886 | value. In all other cases the result is zero. | |
1887 | @end deftypefn | |
1888 | ||
28f540f4 RM |
1889 | @node File Owner |
1890 | @subsection File Owner | |
1891 | @cindex file owner | |
1892 | @cindex owner of a file | |
1893 | @cindex group owner of a file | |
1894 | ||
1895 | Every file has an @dfn{owner} which is one of the registered user names | |
04b9968b UD |
1896 | defined on the system. Each file also has a @dfn{group} which is one of |
1897 | the defined groups. The file owner can often be useful for showing you | |
1898 | who edited the file (especially when you edit with GNU Emacs), but its | |
1899 | main purpose is for access control. | |
28f540f4 RM |
1900 | |
1901 | The file owner and group play a role in determining access because the | |
04b9968b UD |
1902 | file has one set of access permission bits for the owner, another set |
1903 | that applies to users who belong to the file's group, and a third set of | |
d01d6319 | 1904 | bits that applies to everyone else. @xref{Access Permission}, for the |
04b9968b UD |
1905 | details of how access is decided based on this data. |
1906 | ||
1907 | When a file is created, its owner is set to the effective user ID of the | |
1908 | process that creates it (@pxref{Process Persona}). The file's group ID | |
1909 | may be set to either the effective group ID of the process, or the group | |
1910 | ID of the directory that contains the file, depending on the system | |
1911 | where the file is stored. When you access a remote file system, it | |
1912 | behaves according to its own rules, not according to the system your | |
28f540f4 | 1913 | program is running on. Thus, your program must be prepared to encounter |
04b9968b | 1914 | either kind of behavior no matter what kind of system you run it on. |
28f540f4 RM |
1915 | |
1916 | @pindex chown | |
1917 | @pindex chgrp | |
1918 | You can change the owner and/or group owner of an existing file using | |
1919 | the @code{chown} function. This is the primitive for the @code{chown} | |
1920 | and @code{chgrp} shell commands. | |
1921 | ||
1922 | @pindex unistd.h | |
1923 | The prototype for this function is declared in @file{unistd.h}. | |
1924 | ||
1925 | @comment unistd.h | |
1926 | @comment POSIX.1 | |
1927 | @deftypefun int chown (const char *@var{filename}, uid_t @var{owner}, gid_t @var{group}) | |
1928 | The @code{chown} function changes the owner of the file @var{filename} to | |
1929 | @var{owner}, and its group owner to @var{group}. | |
1930 | ||
1931 | Changing the owner of the file on certain systems clears the set-user-ID | |
04b9968b UD |
1932 | and set-group-ID permission bits. (This is because those bits may not |
1933 | be appropriate for the new owner.) Other file permission bits are not | |
1934 | changed. | |
28f540f4 RM |
1935 | |
1936 | The return value is @code{0} on success and @code{-1} on failure. | |
d68171ed | 1937 | In addition to the usual file name errors (@pxref{File Name Errors}), |
28f540f4 RM |
1938 | the following @code{errno} error conditions are defined for this function: |
1939 | ||
1940 | @table @code | |
1941 | @item EPERM | |
1942 | This process lacks permission to make the requested change. | |
1943 | ||
1944 | Only privileged users or the file's owner can change the file's group. | |
1945 | On most file systems, only privileged users can change the file owner; | |
1946 | some file systems allow you to change the owner if you are currently the | |
1947 | owner. When you access a remote file system, the behavior you encounter | |
1948 | is determined by the system that actually holds the file, not by the | |
1949 | system your program is running on. | |
1950 | ||
1951 | @xref{Options for Files}, for information about the | |
1952 | @code{_POSIX_CHOWN_RESTRICTED} macro. | |
1953 | ||
1954 | @item EROFS | |
1955 | The file is on a read-only file system. | |
1956 | @end table | |
1957 | @end deftypefun | |
1958 | ||
1959 | @comment unistd.h | |
1960 | @comment BSD | |
1961 | @deftypefun int fchown (int @var{filedes}, int @var{owner}, int @var{group}) | |
04b9968b UD |
1962 | This is like @code{chown}, except that it changes the owner of the open |
1963 | file with descriptor @var{filedes}. | |
28f540f4 RM |
1964 | |
1965 | The return value from @code{fchown} is @code{0} on success and @code{-1} | |
1966 | on failure. The following @code{errno} error codes are defined for this | |
1967 | function: | |
1968 | ||
1969 | @table @code | |
1970 | @item EBADF | |
1971 | The @var{filedes} argument is not a valid file descriptor. | |
1972 | ||
1973 | @item EINVAL | |
1974 | The @var{filedes} argument corresponds to a pipe or socket, not an ordinary | |
1975 | file. | |
1976 | ||
1977 | @item EPERM | |
04b9968b UD |
1978 | This process lacks permission to make the requested change. For details |
1979 | see @code{chmod} above. | |
28f540f4 RM |
1980 | |
1981 | @item EROFS | |
1982 | The file resides on a read-only file system. | |
1983 | @end table | |
1984 | @end deftypefun | |
1985 | ||
1986 | @node Permission Bits | |
1987 | @subsection The Mode Bits for Access Permission | |
1988 | ||
1989 | The @dfn{file mode}, stored in the @code{st_mode} field of the file | |
1990 | attributes, contains two kinds of information: the file type code, and | |
1991 | the access permission bits. This section discusses only the access | |
1992 | permission bits, which control who can read or write the file. | |
d01d6319 | 1993 | @xref{Testing File Type}, for information about the file type code. |
28f540f4 RM |
1994 | |
1995 | All of the symbols listed in this section are defined in the header file | |
1996 | @file{sys/stat.h}. | |
1997 | @pindex sys/stat.h | |
1998 | ||
1999 | @cindex file permission bits | |
2000 | These symbolic constants are defined for the file mode bits that control | |
2001 | access permission for the file: | |
2002 | ||
2003 | @table @code | |
2004 | @comment sys/stat.h | |
2005 | @comment POSIX.1 | |
2006 | @item S_IRUSR | |
2007 | @vindex S_IRUSR | |
2008 | @comment sys/stat.h | |
2009 | @comment BSD | |
2010 | @itemx S_IREAD | |
2011 | @vindex S_IREAD | |
04b9968b UD |
2012 | Read permission bit for the owner of the file. On many systems this bit |
2013 | is 0400. @code{S_IREAD} is an obsolete synonym provided for BSD | |
28f540f4 RM |
2014 | compatibility. |
2015 | ||
2016 | @comment sys/stat.h | |
2017 | @comment POSIX.1 | |
2018 | @item S_IWUSR | |
2019 | @vindex S_IWUSR | |
2020 | @comment sys/stat.h | |
2021 | @comment BSD | |
2022 | @itemx S_IWRITE | |
2023 | @vindex S_IWRITE | |
2024 | Write permission bit for the owner of the file. Usually 0200. | |
2025 | @w{@code{S_IWRITE}} is an obsolete synonym provided for BSD compatibility. | |
2026 | ||
2027 | @comment sys/stat.h | |
2028 | @comment POSIX.1 | |
2029 | @item S_IXUSR | |
2030 | @vindex S_IXUSR | |
2031 | @comment sys/stat.h | |
2032 | @comment BSD | |
2033 | @itemx S_IEXEC | |
2034 | @vindex S_IEXEC | |
2035 | Execute (for ordinary files) or search (for directories) permission bit | |
2036 | for the owner of the file. Usually 0100. @code{S_IEXEC} is an obsolete | |
2037 | synonym provided for BSD compatibility. | |
2038 | ||
2039 | @comment sys/stat.h | |
2040 | @comment POSIX.1 | |
2041 | @item S_IRWXU | |
2042 | @vindex S_IRWXU | |
2043 | This is equivalent to @samp{(S_IRUSR | S_IWUSR | S_IXUSR)}. | |
2044 | ||
2045 | @comment sys/stat.h | |
2046 | @comment POSIX.1 | |
2047 | @item S_IRGRP | |
2048 | @vindex S_IRGRP | |
2049 | Read permission bit for the group owner of the file. Usually 040. | |
2050 | ||
2051 | @comment sys/stat.h | |
2052 | @comment POSIX.1 | |
2053 | @item S_IWGRP | |
2054 | @vindex S_IWGRP | |
2055 | Write permission bit for the group owner of the file. Usually 020. | |
2056 | ||
2057 | @comment sys/stat.h | |
2058 | @comment POSIX.1 | |
2059 | @item S_IXGRP | |
2060 | @vindex S_IXGRP | |
2061 | Execute or search permission bit for the group owner of the file. | |
2062 | Usually 010. | |
2063 | ||
2064 | @comment sys/stat.h | |
2065 | @comment POSIX.1 | |
2066 | @item S_IRWXG | |
2067 | @vindex S_IRWXG | |
2068 | This is equivalent to @samp{(S_IRGRP | S_IWGRP | S_IXGRP)}. | |
2069 | ||
2070 | @comment sys/stat.h | |
2071 | @comment POSIX.1 | |
2072 | @item S_IROTH | |
2073 | @vindex S_IROTH | |
2074 | Read permission bit for other users. Usually 04. | |
2075 | ||
2076 | @comment sys/stat.h | |
2077 | @comment POSIX.1 | |
2078 | @item S_IWOTH | |
2079 | @vindex S_IWOTH | |
2080 | Write permission bit for other users. Usually 02. | |
2081 | ||
2082 | @comment sys/stat.h | |
2083 | @comment POSIX.1 | |
2084 | @item S_IXOTH | |
2085 | @vindex S_IXOTH | |
2086 | Execute or search permission bit for other users. Usually 01. | |
2087 | ||
2088 | @comment sys/stat.h | |
2089 | @comment POSIX.1 | |
2090 | @item S_IRWXO | |
2091 | @vindex S_IRWXO | |
2092 | This is equivalent to @samp{(S_IROTH | S_IWOTH | S_IXOTH)}. | |
2093 | ||
2094 | @comment sys/stat.h | |
2095 | @comment POSIX | |
2096 | @item S_ISUID | |
2097 | @vindex S_ISUID | |
d68171ed | 2098 | This is the set-user-ID on execute bit, usually 04000. |
28f540f4 RM |
2099 | @xref{How Change Persona}. |
2100 | ||
2101 | @comment sys/stat.h | |
2102 | @comment POSIX | |
2103 | @item S_ISGID | |
2104 | @vindex S_ISGID | |
2105 | This is the set-group-ID on execute bit, usually 02000. | |
2106 | @xref{How Change Persona}. | |
2107 | ||
2108 | @cindex sticky bit | |
2109 | @comment sys/stat.h | |
2110 | @comment BSD | |
2111 | @item S_ISVTX | |
2112 | @vindex S_ISVTX | |
2113 | This is the @dfn{sticky} bit, usually 01000. | |
2114 | ||
04b9968b UD |
2115 | For a directory it gives permission to delete a file in that directory |
2116 | only if you own that file. Ordinarily, a user can either delete all the | |
2117 | files in a directory or cannot delete any of them (based on whether the | |
2118 | user has write permission for the directory). The same restriction | |
2119 | applies---you must have both write permission for the directory and own | |
28f540f4 RM |
2120 | the file you want to delete. The one exception is that the owner of the |
2121 | directory can delete any file in the directory, no matter who owns it | |
2122 | (provided the owner has given himself write permission for the | |
2123 | directory). This is commonly used for the @file{/tmp} directory, where | |
04b9968b | 2124 | anyone may create files but not delete files created by other users. |
28f540f4 RM |
2125 | |
2126 | Originally the sticky bit on an executable file modified the swapping | |
2127 | policies of the system. Normally, when a program terminated, its pages | |
2128 | in core were immediately freed and reused. If the sticky bit was set on | |
2129 | the executable file, the system kept the pages in core for a while as if | |
2130 | the program were still running. This was advantageous for a program | |
2131 | likely to be run many times in succession. This usage is obsolete in | |
2132 | modern systems. When a program terminates, its pages always remain in | |
2133 | core as long as there is no shortage of memory in the system. When the | |
2134 | program is next run, its pages will still be in core if no shortage | |
2135 | arose since the last run. | |
2136 | ||
2137 | On some modern systems where the sticky bit has no useful meaning for an | |
2138 | executable file, you cannot set the bit at all for a non-directory. | |
d68171ed | 2139 | If you try, @code{chmod} fails with @code{EFTYPE}; |
28f540f4 RM |
2140 | @pxref{Setting Permissions}. |
2141 | ||
2142 | Some systems (particularly SunOS) have yet another use for the sticky | |
2143 | bit. If the sticky bit is set on a file that is @emph{not} executable, | |
2144 | it means the opposite: never cache the pages of this file at all. The | |
2145 | main use of this is for the files on an NFS server machine which are | |
2146 | used as the swap area of diskless client machines. The idea is that the | |
2147 | pages of the file will be cached in the client's memory, so it is a | |
04b9968b UD |
2148 | waste of the server's memory to cache them a second time. With this |
2149 | usage the sticky bit also implies that the filesystem may fail to record | |
2150 | the file's modification time onto disk reliably (the idea being that | |
2151 | no-one cares for a swap file). | |
dd7d45e8 UD |
2152 | |
2153 | This bit is only available on BSD systems (and those derived from | |
2154 | them). Therefore one has to use the @code{_BSD_SOURCE} feature select | |
2155 | macro to get the definition (@pxref{Feature Test Macros}). | |
28f540f4 RM |
2156 | @end table |
2157 | ||
2158 | The actual bit values of the symbols are listed in the table above | |
2159 | so you can decode file mode values when debugging your programs. | |
2160 | These bit values are correct for most systems, but they are not | |
2161 | guaranteed. | |
2162 | ||
2163 | @strong{Warning:} Writing explicit numbers for file permissions is bad | |
04b9968b UD |
2164 | practice. Not only is it not portable, it also requires everyone who |
2165 | reads your program to remember what the bits mean. To make your program | |
2166 | clean use the symbolic names. | |
28f540f4 RM |
2167 | |
2168 | @node Access Permission | |
2169 | @subsection How Your Access to a File is Decided | |
2170 | @cindex permission to access a file | |
2171 | @cindex access permission for a file | |
2172 | @cindex file access permission | |
2173 | ||
2174 | Recall that the operating system normally decides access permission for | |
04b9968b | 2175 | a file based on the effective user and group IDs of the process and its |
28f540f4 | 2176 | supplementary group IDs, together with the file's owner, group and |
04b9968b UD |
2177 | permission bits. These concepts are discussed in detail in @ref{Process |
2178 | Persona}. | |
28f540f4 RM |
2179 | |
2180 | If the effective user ID of the process matches the owner user ID of the | |
2181 | file, then permissions for read, write, and execute/search are | |
2182 | controlled by the corresponding ``user'' (or ``owner'') bits. Likewise, | |
2183 | if any of the effective group ID or supplementary group IDs of the | |
2184 | process matches the group owner ID of the file, then permissions are | |
2185 | controlled by the ``group'' bits. Otherwise, permissions are controlled | |
2186 | by the ``other'' bits. | |
2187 | ||
04b9968b UD |
2188 | Privileged users, like @samp{root}, can access any file regardless of |
2189 | its permission bits. As a special case, for a file to be executable | |
2190 | even by a privileged user, at least one of its execute bits must be set. | |
28f540f4 RM |
2191 | |
2192 | @node Setting Permissions | |
2193 | @subsection Assigning File Permissions | |
2194 | ||
2195 | @cindex file creation mask | |
2196 | @cindex umask | |
2197 | The primitive functions for creating files (for example, @code{open} or | |
2198 | @code{mkdir}) take a @var{mode} argument, which specifies the file | |
04b9968b UD |
2199 | permissions to give the newly created file. This mode is modified by |
2200 | the process's @dfn{file creation mask}, or @dfn{umask}, before it is | |
2201 | used. | |
28f540f4 RM |
2202 | |
2203 | The bits that are set in the file creation mask identify permissions | |
2204 | that are always to be disabled for newly created files. For example, if | |
2205 | you set all the ``other'' access bits in the mask, then newly created | |
04b9968b UD |
2206 | files are not accessible at all to processes in the ``other'' category, |
2207 | even if the @var{mode} argument passed to the create function would | |
2208 | permit such access. In other words, the file creation mask is the | |
2209 | complement of the ordinary access permissions you want to grant. | |
28f540f4 RM |
2210 | |
2211 | Programs that create files typically specify a @var{mode} argument that | |
2212 | includes all the permissions that make sense for the particular file. | |
2213 | For an ordinary file, this is typically read and write permission for | |
2214 | all classes of users. These permissions are then restricted as | |
2215 | specified by the individual user's own file creation mask. | |
2216 | ||
2217 | @findex chmod | |
2218 | To change the permission of an existing file given its name, call | |
04b9968b UD |
2219 | @code{chmod}. This function uses the specified permission bits and |
2220 | ignores the file creation mask. | |
28f540f4 RM |
2221 | |
2222 | @pindex umask | |
04b9968b | 2223 | In normal use, the file creation mask is initialized by the user's login |
28f540f4 RM |
2224 | shell (using the @code{umask} shell command), and inherited by all |
2225 | subprocesses. Application programs normally don't need to worry about | |
04b9968b | 2226 | the file creation mask. It will automatically do what it is supposed to |
28f540f4 RM |
2227 | do. |
2228 | ||
04b9968b | 2229 | When your program needs to create a file and bypass the umask for its |
28f540f4 | 2230 | access permissions, the easiest way to do this is to use @code{fchmod} |
04b9968b UD |
2231 | after opening the file, rather than changing the umask. In fact, |
2232 | changing the umask is usually done only by shells. They use the | |
2233 | @code{umask} function. | |
28f540f4 RM |
2234 | |
2235 | The functions in this section are declared in @file{sys/stat.h}. | |
2236 | @pindex sys/stat.h | |
2237 | ||
2238 | @comment sys/stat.h | |
2239 | @comment POSIX.1 | |
2240 | @deftypefun mode_t umask (mode_t @var{mask}) | |
2241 | The @code{umask} function sets the file creation mask of the current | |
2242 | process to @var{mask}, and returns the previous value of the file | |
2243 | creation mask. | |
2244 | ||
2245 | Here is an example showing how to read the mask with @code{umask} | |
2246 | without changing it permanently: | |
2247 | ||
2248 | @smallexample | |
2249 | mode_t | |
2250 | read_umask (void) | |
2251 | @{ | |
0163d97b | 2252 | mode_t mask = umask (0); |
28f540f4 | 2253 | umask (mask); |
0163d97b | 2254 | return mask; |
28f540f4 RM |
2255 | @} |
2256 | @end smallexample | |
2257 | ||
2258 | @noindent | |
2259 | However, it is better to use @code{getumask} if you just want to read | |
04b9968b | 2260 | the mask value, because it is reentrant (at least if you use the GNU |
28f540f4 RM |
2261 | operating system). |
2262 | @end deftypefun | |
2263 | ||
2264 | @comment sys/stat.h | |
2265 | @comment GNU | |
2266 | @deftypefun mode_t getumask (void) | |
2267 | Return the current value of the file creation mask for the current | |
2268 | process. This function is a GNU extension. | |
2269 | @end deftypefun | |
2270 | ||
2271 | @comment sys/stat.h | |
2272 | @comment POSIX.1 | |
2273 | @deftypefun int chmod (const char *@var{filename}, mode_t @var{mode}) | |
2274 | The @code{chmod} function sets the access permission bits for the file | |
2275 | named by @var{filename} to @var{mode}. | |
2276 | ||
04b9968b UD |
2277 | If @var{filename} is a symbolic link, @code{chmod} changes the |
2278 | permissions of the file pointed to by the link, not those of the link | |
28f540f4 RM |
2279 | itself. |
2280 | ||
2281 | This function returns @code{0} if successful and @code{-1} if not. In | |
2282 | addition to the usual file name errors (@pxref{File Name | |
2283 | Errors}), the following @code{errno} error conditions are defined for | |
2284 | this function: | |
2285 | ||
2286 | @table @code | |
2287 | @item ENOENT | |
2288 | The named file doesn't exist. | |
2289 | ||
2290 | @item EPERM | |
04b9968b UD |
2291 | This process does not have permission to change the access permissions |
2292 | of this file. Only the file's owner (as judged by the effective user ID | |
2293 | of the process) or a privileged user can change them. | |
28f540f4 RM |
2294 | |
2295 | @item EROFS | |
2296 | The file resides on a read-only file system. | |
2297 | ||
2298 | @item EFTYPE | |
2299 | @var{mode} has the @code{S_ISVTX} bit (the ``sticky bit'') set, | |
2300 | and the named file is not a directory. Some systems do not allow setting the | |
2301 | sticky bit on non-directory files, and some do (and only some of those | |
2302 | assign a useful meaning to the bit for non-directory files). | |
2303 | ||
2304 | You only get @code{EFTYPE} on systems where the sticky bit has no useful | |
2305 | meaning for non-directory files, so it is always safe to just clear the | |
2306 | bit in @var{mode} and call @code{chmod} again. @xref{Permission Bits}, | |
2307 | for full details on the sticky bit. | |
2308 | @end table | |
2309 | @end deftypefun | |
2310 | ||
2311 | @comment sys/stat.h | |
2312 | @comment BSD | |
2313 | @deftypefun int fchmod (int @var{filedes}, int @var{mode}) | |
04b9968b UD |
2314 | This is like @code{chmod}, except that it changes the permissions of the |
2315 | currently open file given by @var{filedes}. | |
28f540f4 RM |
2316 | |
2317 | The return value from @code{fchmod} is @code{0} on success and @code{-1} | |
2318 | on failure. The following @code{errno} error codes are defined for this | |
2319 | function: | |
2320 | ||
2321 | @table @code | |
2322 | @item EBADF | |
2323 | The @var{filedes} argument is not a valid file descriptor. | |
2324 | ||
2325 | @item EINVAL | |
2326 | The @var{filedes} argument corresponds to a pipe or socket, or something | |
2327 | else that doesn't really have access permissions. | |
2328 | ||
2329 | @item EPERM | |
04b9968b UD |
2330 | This process does not have permission to change the access permissions |
2331 | of this file. Only the file's owner (as judged by the effective user ID | |
2332 | of the process) or a privileged user can change them. | |
28f540f4 RM |
2333 | |
2334 | @item EROFS | |
2335 | The file resides on a read-only file system. | |
2336 | @end table | |
2337 | @end deftypefun | |
2338 | ||
2339 | @node Testing File Access | |
2340 | @subsection Testing Permission to Access a File | |
2341 | @cindex testing access permission | |
2342 | @cindex access, testing for | |
2343 | @cindex setuid programs and file access | |
2344 | ||
3a4cbb41 UD |
2345 | In some situations it is desirable to allow programs to access files or |
2346 | devices even if this is not possible with the permissions granted to the | |
2347 | user. One possible solution is to set the setuid-bit of the program | |
2348 | file. If such a program is started the @emph{effective} user ID of the | |
2349 | process is changed to that of the owner of the program file. So to | |
2350 | allow write access to files like @file{/etc/passwd}, which normally can | |
2351 | be written only by the super-user, the modifying program will have to be | |
2352 | owned by @code{root} and the setuid-bit must be set. | |
2353 | ||
2354 | But beside the files the program is intended to change the user should | |
2355 | not be allowed to access any file to which s/he would not have access | |
2356 | anyway. The program therefore must explicitly check whether @emph{the | |
2357 | user} would have the necessary access to a file, before it reads or | |
2358 | writes the file. | |
28f540f4 RM |
2359 | |
2360 | To do this, use the function @code{access}, which checks for access | |
2361 | permission based on the process's @emph{real} user ID rather than the | |
2362 | effective user ID. (The setuid feature does not alter the real user ID, | |
2363 | so it reflects the user who actually ran the program.) | |
2364 | ||
2365 | There is another way you could check this access, which is easy to | |
2366 | describe, but very hard to use. This is to examine the file mode bits | |
2367 | and mimic the system's own access computation. This method is | |
2368 | undesirable because many systems have additional access control | |
2369 | features; your program cannot portably mimic them, and you would not | |
2370 | want to try to keep track of the diverse features that different systems | |
2371 | have. Using @code{access} is simple and automatically does whatever is | |
2372 | appropriate for the system you are using. | |
2373 | ||
2374 | @code{access} is @emph{only} only appropriate to use in setuid programs. | |
2375 | A non-setuid program will always use the effective ID rather than the | |
2376 | real ID. | |
2377 | ||
2378 | @pindex unistd.h | |
2379 | The symbols in this section are declared in @file{unistd.h}. | |
2380 | ||
2381 | @comment unistd.h | |
2382 | @comment POSIX.1 | |
2383 | @deftypefun int access (const char *@var{filename}, int @var{how}) | |
2384 | The @code{access} function checks to see whether the file named by | |
2385 | @var{filename} can be accessed in the way specified by the @var{how} | |
2386 | argument. The @var{how} argument either can be the bitwise OR of the | |
2387 | flags @code{R_OK}, @code{W_OK}, @code{X_OK}, or the existence test | |
2388 | @code{F_OK}. | |
2389 | ||
04b9968b UD |
2390 | This function uses the @emph{real} user and group IDs of the calling |
2391 | process, rather than the @emph{effective} IDs, to check for access | |
28f540f4 RM |
2392 | permission. As a result, if you use the function from a @code{setuid} |
2393 | or @code{setgid} program (@pxref{How Change Persona}), it gives | |
2394 | information relative to the user who actually ran the program. | |
2395 | ||
2396 | The return value is @code{0} if the access is permitted, and @code{-1} | |
2397 | otherwise. (In other words, treated as a predicate function, | |
2398 | @code{access} returns true if the requested access is @emph{denied}.) | |
2399 | ||
2400 | In addition to the usual file name errors (@pxref{File Name | |
2401 | Errors}), the following @code{errno} error conditions are defined for | |
2402 | this function: | |
2403 | ||
2404 | @table @code | |
2405 | @item EACCES | |
2406 | The access specified by @var{how} is denied. | |
2407 | ||
2408 | @item ENOENT | |
2409 | The file doesn't exist. | |
2410 | ||
2411 | @item EROFS | |
2412 | Write permission was requested for a file on a read-only file system. | |
2413 | @end table | |
2414 | @end deftypefun | |
2415 | ||
2416 | These macros are defined in the header file @file{unistd.h} for use | |
2417 | as the @var{how} argument to the @code{access} function. The values | |
2418 | are integer constants. | |
2419 | @pindex unistd.h | |
2420 | ||
2421 | @comment unistd.h | |
2422 | @comment POSIX.1 | |
2423 | @deftypevr Macro int R_OK | |
04b9968b | 2424 | Flag meaning test for read permission. |
28f540f4 RM |
2425 | @end deftypevr |
2426 | ||
2427 | @comment unistd.h | |
2428 | @comment POSIX.1 | |
2429 | @deftypevr Macro int W_OK | |
04b9968b | 2430 | Flag meaning test for write permission. |
28f540f4 RM |
2431 | @end deftypevr |
2432 | ||
2433 | @comment unistd.h | |
2434 | @comment POSIX.1 | |
2435 | @deftypevr Macro int X_OK | |
04b9968b | 2436 | Flag meaning test for execute/search permission. |
28f540f4 RM |
2437 | @end deftypevr |
2438 | ||
2439 | @comment unistd.h | |
2440 | @comment POSIX.1 | |
2441 | @deftypevr Macro int F_OK | |
04b9968b | 2442 | Flag meaning test for existence of the file. |
28f540f4 RM |
2443 | @end deftypevr |
2444 | ||
2445 | @node File Times | |
2446 | @subsection File Times | |
2447 | ||
2448 | @cindex file access time | |
2449 | @cindex file modification time | |
2450 | @cindex file attribute modification time | |
f2ea0f5b | 2451 | Each file has three time stamps associated with it: its access time, |
28f540f4 RM |
2452 | its modification time, and its attribute modification time. These |
2453 | correspond to the @code{st_atime}, @code{st_mtime}, and @code{st_ctime} | |
d68171ed | 2454 | members of the @code{stat} structure; see @ref{File Attributes}. |
28f540f4 RM |
2455 | |
2456 | All of these times are represented in calendar time format, as | |
2457 | @code{time_t} objects. This data type is defined in @file{time.h}. | |
2458 | For more information about representation and manipulation of time | |
2459 | values, see @ref{Calendar Time}. | |
2460 | @pindex time.h | |
2461 | ||
2462 | Reading from a file updates its access time attribute, and writing | |
2463 | updates its modification time. When a file is created, all three | |
f2ea0f5b | 2464 | time stamps for that file are set to the current time. In addition, the |
28f540f4 RM |
2465 | attribute change time and modification time fields of the directory that |
2466 | contains the new entry are updated. | |
2467 | ||
2468 | Adding a new name for a file with the @code{link} function updates the | |
2469 | attribute change time field of the file being linked, and both the | |
2470 | attribute change time and modification time fields of the directory | |
2471 | containing the new name. These same fields are affected if a file name | |
04b9968b | 2472 | is deleted with @code{unlink}, @code{remove} or @code{rmdir}. Renaming |
28f540f4 RM |
2473 | a file with @code{rename} affects only the attribute change time and |
2474 | modification time fields of the two parent directories involved, and not | |
2475 | the times for the file being renamed. | |
2476 | ||
04b9968b UD |
2477 | Changing the attributes of a file (for example, with @code{chmod}) |
2478 | updates its attribute change time field. | |
28f540f4 | 2479 | |
f2ea0f5b | 2480 | You can also change some of the time stamps of a file explicitly using |
28f540f4 RM |
2481 | the @code{utime} function---all except the attribute change time. You |
2482 | need to include the header file @file{utime.h} to use this facility. | |
2483 | @pindex utime.h | |
2484 | ||
2485 | @comment time.h | |
2486 | @comment POSIX.1 | |
2487 | @deftp {Data Type} {struct utimbuf} | |
2488 | The @code{utimbuf} structure is used with the @code{utime} function to | |
2489 | specify new access and modification times for a file. It contains the | |
2490 | following members: | |
2491 | ||
2492 | @table @code | |
2493 | @item time_t actime | |
2494 | This is the access time for the file. | |
2495 | ||
2496 | @item time_t modtime | |
2497 | This is the modification time for the file. | |
2498 | @end table | |
2499 | @end deftp | |
2500 | ||
2501 | @comment time.h | |
2502 | @comment POSIX.1 | |
2503 | @deftypefun int utime (const char *@var{filename}, const struct utimbuf *@var{times}) | |
2504 | This function is used to modify the file times associated with the file | |
2505 | named @var{filename}. | |
2506 | ||
2507 | If @var{times} is a null pointer, then the access and modification times | |
2508 | of the file are set to the current time. Otherwise, they are set to the | |
2509 | values from the @code{actime} and @code{modtime} members (respectively) | |
04b9968b | 2510 | of the @code{utimbuf} structure pointed to by @var{times}. |
28f540f4 RM |
2511 | |
2512 | The attribute modification time for the file is set to the current time | |
f2ea0f5b | 2513 | in either case (since changing the time stamps is itself a modification |
28f540f4 RM |
2514 | of the file attributes). |
2515 | ||
2516 | The @code{utime} function returns @code{0} if successful and @code{-1} | |
2517 | on failure. In addition to the usual file name errors | |
2518 | (@pxref{File Name Errors}), the following @code{errno} error conditions | |
2519 | are defined for this function: | |
2520 | ||
2521 | @table @code | |
2522 | @item EACCES | |
2523 | There is a permission problem in the case where a null pointer was | |
f2ea0f5b | 2524 | passed as the @var{times} argument. In order to update the time stamp on |
28f540f4 | 2525 | the file, you must either be the owner of the file, have write |
04b9968b | 2526 | permission for the file, or be a privileged user. |
28f540f4 RM |
2527 | |
2528 | @item ENOENT | |
2529 | The file doesn't exist. | |
2530 | ||
2531 | @item EPERM | |
2532 | If the @var{times} argument is not a null pointer, you must either be | |
04b9968b | 2533 | the owner of the file or be a privileged user. |
28f540f4 RM |
2534 | |
2535 | @item EROFS | |
2536 | The file lives on a read-only file system. | |
2537 | @end table | |
2538 | @end deftypefun | |
2539 | ||
2540 | Each of the three time stamps has a corresponding microsecond part, | |
2541 | which extends its resolution. These fields are called | |
2542 | @code{st_atime_usec}, @code{st_mtime_usec}, and @code{st_ctime_usec}; | |
2543 | each has a value between 0 and 999,999, which indicates the time in | |
2544 | microseconds. They correspond to the @code{tv_usec} field of a | |
2545 | @code{timeval} structure; see @ref{High-Resolution Calendar}. | |
2546 | ||
2547 | The @code{utimes} function is like @code{utime}, but also lets you specify | |
2548 | the fractional part of the file times. The prototype for this function is | |
2549 | in the header file @file{sys/time.h}. | |
2550 | @pindex sys/time.h | |
2551 | ||
2552 | @comment sys/time.h | |
2553 | @comment BSD | |
2554 | @deftypefun int utimes (const char *@var{filename}, struct timeval @var{tvp}@t{[2]}) | |
04b9968b UD |
2555 | This function sets the file access and modification times of the file |
2556 | @var{filename}. The new file access time is specified by | |
28f540f4 RM |
2557 | @code{@var{tvp}[0]}, and the new modification time by |
2558 | @code{@var{tvp}[1]}. This function comes from BSD. | |
2559 | ||
2560 | The return values and error conditions are the same as for the @code{utime} | |
2561 | function. | |
2562 | @end deftypefun | |
2563 | ||
7ce241a0 UD |
2564 | @node File Size |
2565 | @subsection File Size | |
2566 | ||
2567 | Normally file sizes are maintained automatically. A file begins with a | |
04b9968b UD |
2568 | size of @math{0} and is automatically extended when data is written past |
2569 | its end. It is also possible to empty a file completely by an | |
7ce241a0 UD |
2570 | @code{open} or @code{fopen} call. |
2571 | ||
04b9968b | 2572 | However, sometimes it is necessary to @emph{reduce} the size of a file. |
7ce241a0 UD |
2573 | This can be done with the @code{truncate} and @code{ftruncate} functions. |
2574 | They were introduced in BSD Unix. @code{ftruncate} was later added to | |
2575 | POSIX.1. | |
2576 | ||
2577 | Some systems allow you to extend a file (creating holes) with these | |
2578 | functions. This is useful when using memory-mapped I/O | |
2579 | (@pxref{Memory-mapped I/O}), where files are not automatically extended. | |
04b9968b UD |
2580 | However, it is not portable but must be implemented if @code{mmap} |
2581 | allows mapping of files (i.e., @code{_POSIX_MAPPED_FILES} is defined). | |
7ce241a0 UD |
2582 | |
2583 | Using these functions on anything other than a regular file gives | |
2584 | @emph{undefined} results. On many systems, such a call will appear to | |
2585 | succeed, without actually accomplishing anything. | |
2586 | ||
fb971363 UD |
2587 | @comment unistd.h |
2588 | @comment X/Open | |
7ce241a0 UD |
2589 | @deftypefun int truncate (const char *@var{filename}, off_t @var{length}) |
2590 | ||
2591 | The @code{truncate} function changes the size of @var{filename} to | |
fb971363 UD |
2592 | @var{length}. If @var{length} is shorter than the previous length, data |
2593 | at the end will be lost. The file must be writable by the user to | |
2594 | perform this operation. | |
7ce241a0 UD |
2595 | |
2596 | If @var{length} is longer, holes will be added to the end. However, some | |
2597 | systems do not support this feature and will leave the file unchanged. | |
2598 | ||
fb971363 UD |
2599 | When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the |
2600 | @code{truncate} function is in fact @code{truncate64} and the type | |
2601 | @code{off_t} has 64 bits which makes it possible to handle files up to | |
2602 | @math{2^63} bytes in length. | |
2603 | ||
7ce241a0 UD |
2604 | The return value is @math{0} for success, or @math{-1} for an error. In |
2605 | addition to the usual file name errors, the following errors may occur: | |
2606 | ||
2607 | @table @code | |
2608 | ||
2609 | @item EACCES | |
2610 | The file is a directory or not writable. | |
2611 | ||
2612 | @item EINVAL | |
2613 | @var{length} is negative. | |
2614 | ||
2615 | @item EFBIG | |
2616 | The operation would extend the file beyond the limits of the operating system. | |
2617 | ||
2618 | @item EIO | |
04b9968b | 2619 | A hardware I/O error occurred. |
7ce241a0 UD |
2620 | |
2621 | @item EPERM | |
2622 | The file is "append-only" or "immutable". | |
2623 | ||
2624 | @item EINTR | |
2625 | The operation was interrupted by a signal. | |
2626 | ||
2627 | @end table | |
2628 | ||
2629 | @end deftypefun | |
2630 | ||
fb971363 UD |
2631 | @comment unistd.h |
2632 | @comment Unix98 | |
2633 | @deftypefun int truncate64 (const char *@var{name}, off64_t @var{length}) | |
2634 | This function is similar to the @code{truncate} function. The | |
2635 | difference is that the @var{length} argument is 64 bits wide even on 32 | |
d0db5a44 | 2636 | bits machines, which allows the handling of files with sizes up to |
e8b1163e | 2637 | @math{2^63} bytes. |
fb971363 UD |
2638 | |
2639 | When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
2640 | 32 bits machine this function is actually available under the name | |
2641 | @code{truncate} and so transparently replaces the 32 bits interface. | |
2642 | @end deftypefun | |
2643 | ||
2644 | @comment unistd.h | |
2645 | @comment POSIX | |
7ce241a0 UD |
2646 | @deftypefun int ftruncate (int @var{fd}, off_t @var{length}) |
2647 | ||
fb971363 UD |
2648 | This is like @code{truncate}, but it works on a file descriptor @var{fd} |
2649 | for an opened file instead of a file name to identify the object. The | |
2650 | file must be opened for writing to successfully carry out the operation. | |
2651 | ||
2652 | The POSIX standard leaves it implementation defined what happens if the | |
2653 | specified new @var{length} of the file is bigger than the original size. | |
2654 | The @code{ftruncate} function might simply leave the file alone and do | |
2655 | nothing or it can increase the size to the desired size. In this later | |
2656 | case the extended area should be zero-filled. So using @code{ftruncate} | |
2657 | is no reliable way to increase the file size but if it is possible it is | |
2658 | probably the fastest way. The function also operates on POSIX shared | |
2659 | memory segments if these are implemented by the system. | |
7ce241a0 UD |
2660 | |
2661 | @code{ftruncate} is especially useful in combination with @code{mmap}. | |
2662 | Since the mapped region must have a fixed size one cannot enlarge the | |
2663 | file by writing something beyond the last mapped page. Instead one has | |
2664 | to enlarge the file itself and then remap the file with the new size. | |
2665 | The example below shows how this works. | |
2666 | ||
fb971363 UD |
2667 | When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the |
2668 | @code{ftruncate} function is in fact @code{ftruncate64} and the type | |
2669 | @code{off_t} has 64 bits which makes it possible to handle files up to | |
2670 | @math{2^63} bytes in length. | |
2671 | ||
7ce241a0 UD |
2672 | The return value is @math{0} for success, or @math{-1} for an error. The |
2673 | following errors may occur: | |
2674 | ||
2675 | @table @code | |
2676 | ||
2677 | @item EBADF | |
2678 | @var{fd} does not correspond to an open file. | |
2679 | ||
2680 | @item EACCES | |
04b9968b | 2681 | @var{fd} is a directory or not open for writing. |
7ce241a0 UD |
2682 | |
2683 | @item EINVAL | |
2684 | @var{length} is negative. | |
2685 | ||
2686 | @item EFBIG | |
2687 | The operation would extend the file beyond the limits of the operating system. | |
2688 | @c or the open() call -- with the not-yet-discussed feature of opening | |
2689 | @c files with extra-large offsets. | |
2690 | ||
2691 | @item EIO | |
04b9968b | 2692 | A hardware I/O error occurred. |
7ce241a0 UD |
2693 | |
2694 | @item EPERM | |
2695 | The file is "append-only" or "immutable". | |
2696 | ||
2697 | @item EINTR | |
2698 | The operation was interrupted by a signal. | |
2699 | ||
2700 | @c ENOENT is also possible on Linux --- however it only occurs if the file | |
2701 | @c descriptor has a `file' structure but no `inode' structure. I'm not | |
2702 | @c sure how such an fd could be created. Perhaps it's a bug. | |
2703 | ||
2704 | @end table | |
2705 | ||
2706 | @end deftypefun | |
2707 | ||
fb971363 UD |
2708 | @comment unistd.h |
2709 | @comment Unix98 | |
2710 | @deftypefun int ftruncate64 (int @var{id}, off64_t @var{length}) | |
2711 | This function is similar to the @code{ftruncate} function. The | |
2712 | difference is that the @var{length} argument is 64 bits wide even on 32 | |
e8b1163e AJ |
2713 | bits machines which allows the handling of files with sizes up to |
2714 | @math{2^63} bytes. | |
fb971363 UD |
2715 | |
2716 | When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
2717 | 32 bits machine this function is actually available under the name | |
2718 | @code{ftruncate} and so transparently replaces the 32 bits interface. | |
2719 | @end deftypefun | |
2720 | ||
04b9968b | 2721 | As announced here is a little example of how to use @code{ftruncate} in |
7ce241a0 UD |
2722 | combination with @code{mmap}: |
2723 | ||
2724 | @smallexample | |
2725 | int fd; | |
2726 | void *start; | |
2727 | size_t len; | |
2728 | ||
2729 | int | |
2730 | add (off_t at, void *block, size_t size) | |
2731 | @{ | |
2732 | if (at + size > len) | |
2733 | @{ | |
2734 | /* Resize the file and remap. */ | |
2735 | size_t ps = sysconf (_SC_PAGESIZE); | |
2736 | size_t ns = (at + size + ps - 1) & ~(ps - 1); | |
2737 | void *np; | |
2738 | if (ftruncate (fd, ns) < 0) | |
2739 | return -1; | |
2740 | np = mmap (NULL, ns, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); | |
2741 | if (np == MAP_FAILED) | |
2742 | return -1; | |
2743 | start = np; | |
2744 | len = ns; | |
2745 | @} | |
2746 | memcpy ((char *) start + at, block, size); | |
2747 | return 0; | |
2748 | @} | |
2749 | @end smallexample | |
2750 | ||
04b9968b UD |
2751 | The function @code{add} writes a block of memory at an arbitrary |
2752 | position in the file. If the current size of the file is too small it | |
2753 | is extended. Note the it is extended by a round number of pages. This | |
2754 | is a requirement of @code{mmap}. The program has to keep track of the | |
2755 | real size, and when it has finished a final @code{ftruncate} call should | |
2756 | set the real size of the file. | |
7ce241a0 | 2757 | |
28f540f4 RM |
2758 | @node Making Special Files |
2759 | @section Making Special Files | |
2760 | @cindex creating special files | |
2761 | @cindex special files | |
2762 | ||
2763 | The @code{mknod} function is the primitive for making special files, | |
2764 | such as files that correspond to devices. The GNU library includes | |
2765 | this function for compatibility with BSD. | |
2766 | ||
2767 | The prototype for @code{mknod} is declared in @file{sys/stat.h}. | |
2768 | @pindex sys/stat.h | |
2769 | ||
2770 | @comment sys/stat.h | |
2771 | @comment BSD | |
2772 | @deftypefun int mknod (const char *@var{filename}, int @var{mode}, int @var{dev}) | |
2773 | The @code{mknod} function makes a special file with name @var{filename}. | |
2774 | The @var{mode} specifies the mode of the file, and may include the various | |
2775 | special file bits, such as @code{S_IFCHR} (for a character special file) | |
2776 | or @code{S_IFBLK} (for a block special file). @xref{Testing File Type}. | |
2777 | ||
2778 | The @var{dev} argument specifies which device the special file refers to. | |
2779 | Its exact interpretation depends on the kind of special file being created. | |
2780 | ||
2781 | The return value is @code{0} on success and @code{-1} on error. In addition | |
2782 | to the usual file name errors (@pxref{File Name Errors}), the | |
2783 | following @code{errno} error conditions are defined for this function: | |
2784 | ||
2785 | @table @code | |
2786 | @item EPERM | |
2787 | The calling process is not privileged. Only the superuser can create | |
2788 | special files. | |
2789 | ||
2790 | @item ENOSPC | |
2791 | The directory or file system that would contain the new file is full | |
2792 | and cannot be extended. | |
2793 | ||
2794 | @item EROFS | |
2795 | The directory containing the new file can't be modified because it's on | |
2796 | a read-only file system. | |
2797 | ||
2798 | @item EEXIST | |
2799 | There is already a file named @var{filename}. If you want to replace | |
2800 | this file, you must remove the old file explicitly first. | |
2801 | @end table | |
2802 | @end deftypefun | |
2803 | ||
2804 | @node Temporary Files | |
2805 | @section Temporary Files | |
2806 | ||
2807 | If you need to use a temporary file in your program, you can use the | |
2808 | @code{tmpfile} function to open it. Or you can use the @code{tmpnam} | |
04b9968b UD |
2809 | (better: @code{tmpnam_r}) function to provide a name for a temporary |
2810 | file and then you can open it in the usual way with @code{fopen}. | |
28f540f4 RM |
2811 | |
2812 | The @code{tempnam} function is like @code{tmpnam} but lets you choose | |
2813 | what directory temporary files will go in, and something about what | |
04b9968b UD |
2814 | their file names will look like. Important for multi-threaded programs |
2815 | is that @code{tempnam} is reentrant, while @code{tmpnam} is not since it | |
d68171ed | 2816 | returns a pointer to a static buffer. |
28f540f4 RM |
2817 | |
2818 | These facilities are declared in the header file @file{stdio.h}. | |
2819 | @pindex stdio.h | |
2820 | ||
2821 | @comment stdio.h | |
f65fd747 | 2822 | @comment ISO |
28f540f4 RM |
2823 | @deftypefun {FILE *} tmpfile (void) |
2824 | This function creates a temporary binary file for update mode, as if by | |
2825 | calling @code{fopen} with mode @code{"wb+"}. The file is deleted | |
2826 | automatically when it is closed or when the program terminates. (On | |
f65fd747 | 2827 | some other @w{ISO C} systems the file may fail to be deleted if the program |
28f540f4 | 2828 | terminates abnormally). |
d68171ed UD |
2829 | |
2830 | This function is reentrant. | |
a3a4a74e UD |
2831 | |
2832 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
04b9968b UD |
2833 | 32-bit system this function is in fact @code{tmpfile64}, i.e. the LFS |
2834 | interface transparently replaces the old interface. | |
a3a4a74e UD |
2835 | @end deftypefun |
2836 | ||
2837 | @comment stdio.h | |
2838 | @comment Unix98 | |
2839 | @deftypefun {FILE *} tmpfile64 (void) | |
04b9968b UD |
2840 | This function is similar to @code{tmpfile}, but the stream it returns a |
2841 | pointer to was opened using @code{tmpfile64}. Therefore this stream can | |
2842 | be used for files larger then @math{2^31} bytes on 32-bit machines. | |
a3a4a74e UD |
2843 | |
2844 | Please note that the return type is still @code{FILE *}. There is no | |
2845 | special @code{FILE} type for the LFS interface. | |
2846 | ||
2847 | If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32 | |
2848 | bits machine this function is available under the name @code{tmpfile} | |
2849 | and so transparently replaces the old interface. | |
28f540f4 RM |
2850 | @end deftypefun |
2851 | ||
2852 | @comment stdio.h | |
f65fd747 | 2853 | @comment ISO |
28f540f4 | 2854 | @deftypefun {char *} tmpnam (char *@var{result}) |
04b9968b UD |
2855 | This function constructs and returns a valid file name that does not |
2856 | refer to any existing file. If the @var{result} argument is a null | |
2857 | pointer, the return value is a pointer to an internal static string, | |
2858 | which might be modified by subsequent calls and therefore makes this | |
2859 | function non-reentrant. Otherwise, the @var{result} argument should be | |
2860 | a pointer to an array of at least @code{L_tmpnam} characters, and the | |
2861 | result is written into that array. | |
d68171ed UD |
2862 | |
2863 | It is possible for @code{tmpnam} to fail if you call it too many times | |
04b9968b UD |
2864 | without removing previously-created files. This is because the limited |
2865 | length of the temporary file names gives room for only a finite number | |
2866 | of different names. If @code{tmpnam} fails it returns a null pointer. | |
2867 | ||
2868 | @strong{Warning:} Between the time the pathname is constructed and the | |
2869 | file is created another process might have created a file with the same | |
2870 | name using @code{tmpnam}, leading to a possible security hole. The | |
2871 | implementation generates names which can hardly be predicted, but when | |
2872 | opening the file you should use the @code{O_EXCL} flag. Using | |
e5448d7a | 2873 | @code{tmpfile} or @code{mkstemp} is a safe way to avoid this problem. |
d68171ed UD |
2874 | @end deftypefun |
2875 | ||
2876 | @comment stdio.h | |
2877 | @comment GNU | |
2878 | @deftypefun {char *} tmpnam_r (char *@var{result}) | |
04b9968b UD |
2879 | This function is nearly identical to the @code{tmpnam} function, except |
2880 | that if @var{result} is a null pointer it returns a null pointer. | |
d68171ed | 2881 | |
04b9968b | 2882 | This guarantees reentrancy because the non-reentrant situation of |
d68171ed | 2883 | @code{tmpnam} cannot happen here. |
e5448d7a UD |
2884 | |
2885 | @strong{Warning}: This function has the same security problems as | |
2886 | @code{tmpnam}. | |
28f540f4 RM |
2887 | @end deftypefun |
2888 | ||
2889 | @comment stdio.h | |
f65fd747 | 2890 | @comment ISO |
28f540f4 | 2891 | @deftypevr Macro int L_tmpnam |
04b9968b UD |
2892 | The value of this macro is an integer constant expression that |
2893 | represents the minimum size of a string large enough to hold a file name | |
2894 | generated by the @code{tmpnam} function. | |
28f540f4 RM |
2895 | @end deftypevr |
2896 | ||
2897 | @comment stdio.h | |
f65fd747 | 2898 | @comment ISO |
28f540f4 RM |
2899 | @deftypevr Macro int TMP_MAX |
2900 | The macro @code{TMP_MAX} is a lower bound for how many temporary names | |
2901 | you can create with @code{tmpnam}. You can rely on being able to call | |
2902 | @code{tmpnam} at least this many times before it might fail saying you | |
2903 | have made too many temporary file names. | |
2904 | ||
2905 | With the GNU library, you can create a very large number of temporary | |
04b9968b UD |
2906 | file names. If you actually created the files, you would probably run |
2907 | out of disk space before you ran out of names. Some other systems have | |
2908 | a fixed, small limit on the number of temporary files. The limit is | |
2909 | never less than @code{25}. | |
28f540f4 RM |
2910 | @end deftypevr |
2911 | ||
2912 | @comment stdio.h | |
2913 | @comment SVID | |
2914 | @deftypefun {char *} tempnam (const char *@var{dir}, const char *@var{prefix}) | |
04b9968b UD |
2915 | This function generates a unique temporary file name. If @var{prefix} |
2916 | is not a null pointer, up to five characters of this string are used as | |
2917 | a prefix for the file name. The return value is a string newly | |
2918 | allocated with @code{malloc}, so you should release its storage with | |
2919 | @code{free} when it is no longer needed. | |
28f540f4 | 2920 | |
d68171ed UD |
2921 | Because the string is dynamically allocated this function is reentrant. |
2922 | ||
04b9968b UD |
2923 | The directory prefix for the temporary file name is determined by |
2924 | testing each of the following in sequence. The directory must exist and | |
2925 | be writable. | |
28f540f4 RM |
2926 | |
2927 | @itemize @bullet | |
2928 | @item | |
d68171ed UD |
2929 | The environment variable @code{TMPDIR}, if it is defined. For security |
2930 | reasons this only happens if the program is not SUID or SGID enabled. | |
28f540f4 RM |
2931 | |
2932 | @item | |
2933 | The @var{dir} argument, if it is not a null pointer. | |
2934 | ||
2935 | @item | |
2936 | The value of the @code{P_tmpdir} macro. | |
2937 | ||
2938 | @item | |
2939 | The directory @file{/tmp}. | |
2940 | @end itemize | |
2941 | ||
2942 | This function is defined for SVID compatibility. | |
e5448d7a UD |
2943 | |
2944 | @strong{Warning:} Between the time the pathname is constructed and the | |
2945 | file is created another process might have created a file with the same | |
2946 | name using @code{tempnam}, leading to a possible security hole. The | |
2947 | implementation generates names which can hardly be predicted, but when | |
2948 | opening the file you should use the @code{O_EXCL} flag. Using | |
2949 | @code{tmpfile} or @code{mkstemp} is a safe way to avoid this problem. | |
28f540f4 RM |
2950 | @end deftypefun |
2951 | @cindex TMPDIR environment variable | |
2952 | ||
2953 | @comment stdio.h | |
2954 | @comment SVID | |
2955 | @c !!! are we putting SVID/GNU/POSIX.1/BSD in here or not?? | |
2956 | @deftypevr {SVID Macro} {char *} P_tmpdir | |
2957 | This macro is the name of the default directory for temporary files. | |
2958 | @end deftypevr | |
2959 | ||
2960 | Older Unix systems did not have the functions just described. Instead | |
2961 | they used @code{mktemp} and @code{mkstemp}. Both of these functions | |
2962 | work by modifying a file name template string you pass. The last six | |
2963 | characters of this string must be @samp{XXXXXX}. These six @samp{X}s | |
2964 | are replaced with six characters which make the whole string a unique | |
2965 | file name. Usually the template string is something like | |
2966 | @samp{/tmp/@var{prefix}XXXXXX}, and each program uses a unique @var{prefix}. | |
2967 | ||
2968 | @strong{Note:} Because @code{mktemp} and @code{mkstemp} modify the | |
2969 | template string, you @emph{must not} pass string constants to them. | |
2970 | String constants are normally in read-only storage, so your program | |
2971 | would crash when @code{mktemp} or @code{mkstemp} tried to modify the | |
2972 | string. | |
2973 | ||
b4751608 | 2974 | @comment stdlib.h |
28f540f4 RM |
2975 | @comment Unix |
2976 | @deftypefun {char *} mktemp (char *@var{template}) | |
2977 | The @code{mktemp} function generates a unique file name by modifying | |
2978 | @var{template} as described above. If successful, it returns | |
2979 | @var{template} as modified. If @code{mktemp} cannot find a unique file | |
2980 | name, it makes @var{template} an empty string and returns that. If | |
2981 | @var{template} does not end with @samp{XXXXXX}, @code{mktemp} returns a | |
2982 | null pointer. | |
4bca4c17 | 2983 | |
04b9968b UD |
2984 | @strong{Warning:} Between the time the pathname is constructed and the |
2985 | file is created another process might have created a file with the same | |
2986 | name using @code{mktemp}, leading to a possible security hole. The | |
2987 | implementation generates names which can hardly be predicted, but when | |
2988 | opening the file you should use the @code{O_EXCL} flag. Using | |
4bca4c17 | 2989 | @code{mkstemp} is a safe way to avoid this problem. |
28f540f4 RM |
2990 | @end deftypefun |
2991 | ||
b4751608 | 2992 | @comment stdlib.h |
28f540f4 RM |
2993 | @comment BSD |
2994 | @deftypefun int mkstemp (char *@var{template}) | |
2995 | The @code{mkstemp} function generates a unique file name just as | |
2996 | @code{mktemp} does, but it also opens the file for you with @code{open} | |
2997 | (@pxref{Opening and Closing Files}). If successful, it modifies | |
04b9968b | 2998 | @var{template} in place and returns a file descriptor for that file open |
28f540f4 | 2999 | for reading and writing. If @code{mkstemp} cannot create a |
b4751608 AS |
3000 | uniquely-named file, it returns @code{-1}. If @var{template} does not |
3001 | end with @samp{XXXXXX}, @code{mkstemp} returns @code{-1} and does not | |
3002 | modify @var{template}. | |
54fce91d UD |
3003 | |
3004 | The file is opened using mode @code{0600}. If the file is meant to be | |
04b9968b | 3005 | used by other users this mode must be changed explicitly. |
28f540f4 RM |
3006 | @end deftypefun |
3007 | ||
3008 | Unlike @code{mktemp}, @code{mkstemp} is actually guaranteed to create a | |
3009 | unique file that cannot possibly clash with any other program trying to | |
3010 | create a temporary file. This is because it works by calling | |
04b9968b UD |
3011 | @code{open} with the @code{O_EXCL} flag, which says you want to create a |
3012 | new file and get an error if the file already exists. | |
2e65ca2b UD |
3013 | |
3014 | @comment stdlib.h | |
3015 | @comment BSD | |
3016 | @deftypefun {char *} mkdtemp (char *@var{template}) | |
3017 | The @code{mkdtemp} function creates a directory with a unique name. If | |
3018 | it succeeds, it overwrites @var{template} with the name of the | |
3019 | directory, and returns @var{template}. As with @code{mktemp} and | |
3020 | @code{mkstemp}, @var{template} should be a string ending with | |
3021 | @samp{XXXXXX}. | |
3022 | ||
3023 | If @code{mkdtemp} cannot create an uniquely named directory, it returns | |
3024 | @code{NULL} and sets @var{errno} appropriately. If @var{template} does | |
3025 | not end with @samp{XXXXXX}, @code{mkdtemp} returns @code{NULL} and does | |
3026 | not modify @var{template}. @var{errno} will be set to @code{EINVAL} in | |
3027 | this case. | |
3028 | ||
3029 | The directory is created using mode @code{0700}. | |
3030 | @end deftypefun | |
3031 | ||
3032 | The directory created by @code{mkdtemp} cannot clash with temporary | |
3033 | files or directories created by other users. This is because directory | |
3034 | creation always works like @code{open} with @code{O_EXCL}. | |
3035 | @xref{Creating Directories}. | |
3036 | ||
3037 | The @code{mkdtemp} function comes from OpenBSD. |