]> sourceware.org Git - glibc.git/blame_incremental - manual/filesys.texi
Avoid uninitialized result in sem_open when file does not exist
[glibc.git] / manual / filesys.texi
... / ...
CommitLineData
1@node File System Interface, Pipes and FIFOs, Low-Level I/O, Top
2@c %MENU% Functions for manipulating files
3@chapter File System Interface
4
5This chapter describes @theglibc{}'s functions for manipulating
6files. Unlike the input and output functions (@pxref{I/O on Streams};
7@pxref{Low-Level I/O}), these functions are concerned with operating
8on the files themselves rather than on their contents.
9
10Among the facilities described in this chapter are functions for
11examining or modifying directories, functions for renaming and deleting
12files, and functions for examining and setting file attributes such as
13access permissions and modification times.
14
15@menu
16* Working Directory:: This is used to resolve relative
17 file names.
18* Descriptor-Relative Access:: Ways to control file name lookup.
19* Accessing Directories:: Finding out what files a directory
20 contains.
21* Working with Directory Trees:: Apply actions to all files or a selectable
22 subset of a directory hierarchy.
23* Hard Links:: Adding alternate names to a file.
24* Symbolic Links:: A file that ``points to'' a file name.
25* Deleting Files:: How to delete a file, and what that means.
26* Renaming Files:: Changing a file's name.
27* Creating Directories:: A system call just for creating a directory.
28* File Attributes:: Attributes of individual files.
29* Making Special Files:: How to create special files.
30* Temporary Files:: Naming and creating temporary files.
31@end menu
32
33@node Working Directory
34@section Working Directory
35
36@cindex current working directory
37@cindex working directory
38@cindex change working directory
39Each process has associated with it a directory, called its @dfn{current
40working directory} or simply @dfn{working directory}, that is used in
41the resolution of relative file names (@pxref{File Name Resolution}).
42
43When you log in and begin a new session, your working directory is
44initially set to the home directory associated with your login account
45in the system user database. You can find any user's home directory
46using the @code{getpwuid} or @code{getpwnam} functions; see @ref{User
47Database}.
48
49Users can change the working directory using shell commands like
50@code{cd}. The functions described in this section are the primitives
51used by those commands and by other programs for examining and changing
52the working directory.
53@pindex cd
54
55Prototypes for these functions are declared in the header file
56@file{unistd.h}.
57@pindex unistd.h
58
59@deftypefun {char *} getcwd (char *@var{buffer}, size_t @var{size})
60@standards{POSIX.1, unistd.h}
61@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
62@c If buffer is NULL, this function calls malloc and realloc, and, in
63@c case of error, free. Linux offers a getcwd syscall that we use on
64@c GNU/Linux systems, but it may fail if the pathname is too long. As a
65@c fallback, and on other systems, the generic implementation opens each
66@c parent directory with opendir, which allocates memory for the
67@c directory stream with malloc. If a fstatat64 syscall is not
68@c available, very deep directory trees may also have to malloc to build
69@c longer sequences of ../../../... than those supported by a global
70@c const read-only string.
71
72@c linux/__getcwd
73@c posix/__getcwd
74@c malloc/realloc/free if buffer is NULL, or if dir is too deep
75@c lstat64 -> see its own entry
76@c fstatat64
77@c direct syscall if possible, alloca+snprintf+*stat64 otherwise
78@c openat64_not_cancel_3, close_not_cancel_no_status
79@c __fdopendir, __opendir, __readdir, rewinddir
80The @code{getcwd} function returns an absolute file name representing
81the current working directory, storing it in the character array
82@var{buffer} that you provide. The @var{size} argument is how you tell
83the system the allocation size of @var{buffer}.
84
85The @glibcadj{} version of this function also permits you to specify a
86null pointer for the @var{buffer} argument. Then @code{getcwd}
87allocates a buffer automatically, as with @code{malloc}
88(@pxref{Unconstrained Allocation}). If the @var{size} is greater than
89zero, then the buffer is that large; otherwise, the buffer is as large
90as necessary to hold the result.
91
92The return value is @var{buffer} on success and a null pointer on failure.
93The following @code{errno} error conditions are defined for this function:
94
95@table @code
96@item EINVAL
97The @var{size} argument is zero and @var{buffer} is not a null pointer.
98
99@item ERANGE
100The @var{size} argument is less than the length of the working directory
101name. You need to allocate a bigger array and try again.
102
103@item EACCES
104Permission to read or search a component of the file name was denied.
105@end table
106@end deftypefun
107
108You could implement the behavior of GNU's @w{@code{getcwd (NULL, 0)}}
109using only the standard behavior of @code{getcwd}:
110
111@smallexample
112char *
113gnu_getcwd ()
114@{
115 size_t size = 100;
116
117 while (1)
118 @{
119 char *buffer = (char *) xmalloc (size);
120 if (getcwd (buffer, size) == buffer)
121 return buffer;
122 free (buffer);
123 if (errno != ERANGE)
124 return 0;
125 size *= 2;
126 @}
127@}
128@end smallexample
129
130@noindent
131@xref{Malloc Examples}, for information about @code{xmalloc}, which is
132not a library function but is a customary name used in most GNU
133software.
134
135@deftypefn {Deprecated Function} {char *} getwd (char *@var{buffer})
136@standards{BSD, unistd.h}
137@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @ascuintl{}}@acunsafe{@acsmem{} @acsfd{}}}
138@c Besides the getcwd safety issues, it calls strerror_r on error, which
139@c brings in all of the i18n issues.
140This is similar to @code{getcwd}, but has no way to specify the size of
141the buffer. @Theglibc{} provides @code{getwd} only
142for backwards compatibility with BSD.
143
144The @var{buffer} argument should be a pointer to an array at least
145@code{PATH_MAX} bytes long (@pxref{Limits for Files}). On @gnuhurdsystems{}
146there is no limit to the size of a file name, so this is not
147necessarily enough space to contain the directory name. That is why
148this function is deprecated.
149@end deftypefn
150
151@vindex PWD
152@deftypefun {char *} get_current_dir_name (void)
153@standards{GNU, unistd.h}
154@safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
155@c Besides getcwd, which this function calls as a fallback, it calls
156@c getenv, with the potential thread-safety issues that brings about.
157The @code{get_current_dir_name} function is basically equivalent to
158@w{@code{getcwd (NULL, 0)}}, except the value of the @env{PWD}
159environment variable is first examined, and if it does in fact
160correspond to the current directory, that value is returned. This is
161a subtle difference which is visible if the path described by the
162value in @env{PWD} is using one or more symbolic links, in which case
163the value returned by @code{getcwd} would resolve the symbolic links
164and therefore yield a different result.
165
166This function is a GNU extension.
167@end deftypefun
168
169@deftypefun int chdir (const char *@var{filename})
170@standards{POSIX.1, unistd.h}
171@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
172This function is used to set the process's working directory to
173@var{filename}.
174
175The normal, successful return value from @code{chdir} is @code{0}. A
176value of @code{-1} is returned to indicate an error. The @code{errno}
177error conditions defined for this function are the usual file name
178syntax errors (@pxref{File Name Errors}), plus @code{ENOTDIR} if the
179file @var{filename} is not a directory.
180@end deftypefun
181
182@deftypefun int fchdir (int @var{filedes})
183@standards{XPG, unistd.h}
184@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
185This function is used to set the process's working directory to
186directory associated with the file descriptor @var{filedes}.
187
188The normal, successful return value from @code{fchdir} is @code{0}. A
189value of @code{-1} is returned to indicate an error. The following
190@code{errno} error conditions are defined for this function:
191
192@table @code
193@item EACCES
194Read permission is denied for the directory named by @code{dirname}.
195
196@item EBADF
197The @var{filedes} argument is not a valid file descriptor.
198
199@item ENOTDIR
200The file descriptor @var{filedes} is not associated with a directory.
201
202@item EINTR
203The function call was interrupt by a signal.
204
205@item EIO
206An I/O error occurred.
207@end table
208@end deftypefun
209
210@node Descriptor-Relative Access
211@section Descriptor-Relative Access
212@cindex file name resolution based on descriptors
213@cindex descriptor-based file name resolution
214@cindex @code{@dots{}at} functions
215
216Many functions that accept file names have @code{@dots{}at} variants
217which accept a file descriptor and a file name argument instead of just
218a file name argument. For example, @code{fstatat} is the
219descriptor-based variant of the @code{fstat} function. Most such
220functions also accept an additional flags argument which changes the
221behavior of the file name lookup based on the passed @code{AT_@dots{}}
222flags.
223
224There are several reasons to use descriptor-relative access:
225
226@itemize @bullet
227@item
228The working directory is a process-wide resource, so individual threads
229cannot change it without affecting other threads in the process.
230Explicitly specifying the directory against which relative paths are
231resolved can be a thread-safe alternative to changing the working
232directory.
233
234@item
235If a program wishes to access a directory tree which is being modified
236concurrently, perhaps even by a different user on the system, the
237program must avoid looking up file names with multiple components, in
238order to detect symbolic links, using the @code{O_NOFOLLOW} flag
239(@pxref{Open-time Flags}) or the @code{AT_SYMLINK_FOLLOW} flag
240(described below). Without directory-relative access, it is necessary
241to use the @code{fchdir} function to change the working directory
242(@pxref{Working Directory}), which is not thread-safe.
243
244@item
245Listing directory contents using the @code{readdir} or @code{readdir64}
246functions (@pxref{Reading/Closing Directory}) does not provide full file
247name paths. Using @code{@dots{}at} functions, it is possible to use the
248file names directly, without having to construct such full paths.
249
250@item
251Additional flags available with some of the @code{@dots{}at} functions
252provide access to functionality which is not available otherwise.
253@end itemize
254
255The file descriptor used by these @code{@dots{}at} functions has the
256following uses:
257
258@itemize @bullet
259@item
260It can be a file descriptor referring to a directory. Such a descriptor
261can be created explicitly using the @code{open} function and the
262@code{O_RDONLY} file access mode, with or without the @code{O_DIRECTORY}
263flag. @xref{Opening and Closing Files}. Or it can be created
264implicitly by @code{opendir} and retrieved using the @code{dirfd}
265function. @xref{Opening a Directory}.
266
267If a directory descriptor is used with one of the @code{@dots{}at}
268functions, a relative file name argument is resolved relative to
269directory referred to by the file descriptor, just as if that directory
270were the current working directory. Absolute file name arguments
271(starting with @samp{/}) are resolved against the file system root, and
272the descriptor argument is effectively ignored.
273
274This means that file name lookup is not constrained to the directory of
275the descriptor. For example, it is possible to access a file
276@file{example} in the descriptor's parent directory using a file name
277argument @code{"../example"}, or in the root directory using
278@code{"/example"}.
279
280If the file descriptor refers to a directory, the empty string @code{""}
281is not a valid file name argument. It is possible to use @code{"."} to
282refer to the directory itself. Also see @code{AT_EMPTY_PATH} below.
283
284@item
285@vindex @code{AT_FDCWD}
286The special value @code{AT_FDCWD}. This means that the current working
287directory is used for the lookup if the file name is a relative. For
288@code{@dots{}at} functions with an @code{AT_@dots{}} flags argument,
289this provides a shortcut to use those flags with regular (not
290descriptor-based) file name lookups.
291
292If @code{AT_FDCWD} is used, the empty string @code{""} is not a valid
293file name argument.
294
295@item
296An arbitrary file descriptor, along with an empty string @code{""} as
297the file name argument, and the @code{AT_EMPTY_PATH} flag. In this
298case, the operation uses the file descriptor directly, without further
299file name resolution. On Linux, this allows operations on descriptors
300opened with the @code{O_PATH} flag. For regular descriptors (opened
301without @code{O_PATH}), the same functionality is also available through
302the plain descriptor-based functions (for example, @code{fstat} instead
303of @code{fstatat}).
304
305This is a GNU extension.
306@end itemize
307
308@cindex file name resolution flags
309@cindex @code{AT_*} file name resolution flags
310The flags argument in @code{@dots{}at} functions can be a combination of
311the following flags, defined in @file{fcntl.h}. Not all such functions
312support all flags, and some (such as @code{openat}) do not accept a
313flags argument at all.
314
315In the flag descriptions below, the @dfn{effective final path component}
316refers to the final component (basename) of the full path constructed
317from the descriptor and file name arguments, using file name lookup, as
318described above.
319
320@vtable @code
321@item AT_EMPTY_PATH
322This flag is used with an empty file name @code{""} and a descriptor
323which does not necessarily refer to a directory. It is most useful with
324@code{O_PATH} descriptors, as described above. This flag is a GNU
325extension.
326
327@item AT_NO_AUTOMOUNT
328If the effective final path component refers to a potential file system
329mount point controlled by an auto-mounting service, the operation does
330not trigger auto-mounting and refers to the unmounted mount point
331instead. @xref{Mount-Unmount-Remount}. If a file system has already
332been mounted at the effective final path component, the operation
333applies to the file or directory in the mounted file system, not the
334underlying file system that was mounted over. This flag is a GNU
335extension.
336
337@item AT_SYMLINK_FOLLOW
338If the effective final path component is a symbolic link, the
339operation follows the symbolic link and operates on its target. (For
340most functions, this is the default behavior.)
341
342@item AT_SYMLINK_NOFOLLOW
343If the effective final path component is a symbolic link, the
344operation operates on the symbolic link, without following it. The
345difference in behavior enabled by this flag is similar to the difference
346between the @code{lstat} and @code{stat} functions, or the behavior
347activated by the @code{O_NOFOLLOW} argument to the @code{open} function.
348Even with the @code{AT_SYMLINK_NOFOLLOW} flag present, symbolic links in
349a non-final component of the file name are still followed.
350@end vtable
351
352@strong{Note:} There is no relationship between these flags and the type
353argument to the @code{getauxval} function (with @code{AT_@dots{}}
354constants defined in @file{elf.h}). @xref{Auxiliary Vector}.
355
356@node Accessing Directories
357@section Accessing Directories
358@cindex accessing directories
359@cindex reading from a directory
360@cindex directories, accessing
361
362The facilities described in this section let you read the contents of a
363directory file. This is useful if you want your program to list all the
364files in a directory, perhaps as part of a menu.
365
366@cindex directory stream
367The @code{opendir} function opens a @dfn{directory stream} whose
368elements are directory entries. Alternatively @code{fdopendir} can be
369used which can have advantages if the program needs to have more
370control over the way the directory is opened for reading. This
371allows, for instance, to pass the @code{O_NOATIME} flag to
372@code{open}.
373
374You use the @code{readdir} function on the directory stream to
375retrieve these entries, represented as @w{@code{struct dirent}}
376objects. The name of the file for each entry is stored in the
377@code{d_name} member of this structure. There are obvious parallels
378here to the stream facilities for ordinary files, described in
379@ref{I/O on Streams}.
380
381@menu
382* Directory Entries:: Format of one directory entry.
383* Opening a Directory:: How to open a directory stream.
384* Reading/Closing Directory:: How to read directory entries from the stream.
385* Simple Directory Lister:: A very simple directory listing program.
386* Random Access Directory:: Rereading part of the directory
387 already read with the same stream.
388* Scanning Directory Content:: Get entries for user selected subset of
389 contents in given directory.
390* Simple Directory Lister Mark II:: Revised version of the program.
391* Low-level Directory Access:: AS-Safe functions for directory access.
392@end menu
393
394@node Directory Entries
395@subsection Format of a Directory Entry
396
397@pindex dirent.h
398This section describes what you find in a single directory entry, as you
399might obtain it from a directory stream. All the symbols are declared
400in the header file @file{dirent.h}.
401
402@deftp {Data Type} {struct dirent}
403@standards{POSIX.1, dirent.h}
404This is a structure type used to return information about directory
405entries. It contains the following fields:
406
407@table @code
408@item char d_name[]
409This is the null-terminated file name component. This is the only
410field you can count on in all POSIX systems.
411
412@item ino_t d_fileno
413This is the file serial number. For BSD compatibility, you can also
414refer to this member as @code{d_ino}. On @gnulinuxhurdsystems{} and most POSIX
415systems, for most files this the same as the @code{st_ino} member that
416@code{stat} will return for the file. @xref{File Attributes}.
417
418@item unsigned char d_namlen
419This is the length of the file name, not including the terminating
420null character. Its type is @code{unsigned char} because that is the
421integer type of the appropriate size. This member is a BSD extension.
422The symbol @code{_DIRENT_HAVE_D_NAMLEN} is defined if this member is
423available.
424
425@item unsigned char d_type
426This is the type of the file, possibly unknown. The following constants
427are defined for its value:
428
429@vtable @code
430@item DT_UNKNOWN
431The type is unknown. Only some filesystems have full support to
432return the type of the file, others might always return this value.
433
434@item DT_REG
435A regular file.
436
437@item DT_DIR
438A directory.
439
440@item DT_FIFO
441A named pipe, or FIFO. @xref{FIFO Special Files}.
442
443@item DT_SOCK
444A local-domain socket. @c !!! @xref{Local Domain}.
445
446@item DT_CHR
447A character device.
448
449@item DT_BLK
450A block device.
451
452@item DT_LNK
453A symbolic link.
454@end vtable
455
456This member is a BSD extension. The symbol @code{_DIRENT_HAVE_D_TYPE}
457is defined if this member is available. On systems where it is used, it
458corresponds to the file type bits in the @code{st_mode} member of
459@code{struct stat}. If the value cannot be determined the member
460value is DT_UNKNOWN. These two macros convert between @code{d_type}
461values and @code{st_mode} values:
462
463@deftypefun int IFTODT (mode_t @var{mode})
464@standards{BSD, dirent.h}
465@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
466This returns the @code{d_type} value corresponding to @var{mode}.
467@end deftypefun
468
469@deftypefun mode_t DTTOIF (int @var{dtype})
470@standards{BSD, dirent.h}
471@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
472This returns the @code{st_mode} value corresponding to @var{dtype}.
473@end deftypefun
474@end table
475
476This structure may contain additional members in the future. Their
477availability is always announced in the compilation environment by a
478macro named @code{_DIRENT_HAVE_D_@var{xxx}} where @var{xxx} is replaced
479by the name of the new member. For instance, the member @code{d_reclen}
480available on some systems is announced through the macro
481@code{_DIRENT_HAVE_D_RECLEN}.
482
483When a file has multiple names, each name has its own directory entry.
484The only way you can tell that the directory entries belong to a
485single file is that they have the same value for the @code{d_fileno}
486field.
487
488File attributes such as size, modification times etc., are part of the
489file itself, not of any particular directory entry. @xref{File
490Attributes}.
491@end deftp
492
493@node Opening a Directory
494@subsection Opening a Directory Stream
495
496@pindex dirent.h
497This section describes how to open a directory stream. All the symbols
498are declared in the header file @file{dirent.h}.
499
500@deftp {Data Type} DIR
501@standards{POSIX.1, dirent.h}
502The @code{DIR} data type represents a directory stream.
503@end deftp
504
505You shouldn't ever allocate objects of the @code{struct dirent} or
506@code{DIR} data types, since the directory access functions do that for
507you. Instead, you refer to these objects using the pointers returned by
508the following functions.
509
510Directory streams are a high-level interface. On Linux, alternative
511interfaces for accessing directories using file descriptors are
512available. @xref{Low-level Directory Access}.
513
514@deftypefun {DIR *} opendir (const char *@var{dirname})
515@standards{POSIX.1, dirent.h}
516@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
517@c Besides the safe syscall, we have to allocate the DIR object with
518@c __alloc_dir, that calls malloc.
519The @code{opendir} function opens and returns a directory stream for
520reading the directory whose file name is @var{dirname}. The stream has
521type @code{DIR *}.
522
523If unsuccessful, @code{opendir} returns a null pointer. In addition to
524the usual file name errors (@pxref{File Name Errors}), the
525following @code{errno} error conditions are defined for this function:
526
527@table @code
528@item EACCES
529Read permission is denied for the directory named by @code{dirname}.
530
531@item EMFILE
532The process has too many files open.
533
534@item ENFILE
535The entire system, or perhaps the file system which contains the
536directory, cannot support any additional open files at the moment.
537(This problem cannot happen on @gnuhurdsystems{}.)
538
539@item ENOMEM
540Not enough memory available.
541@end table
542
543The @code{DIR} type is typically implemented using a file descriptor,
544and the @code{opendir} function in terms of the @code{open} function.
545@xref{Low-Level I/O}. Directory streams and the underlying
546file descriptors are closed on @code{exec} (@pxref{Executing a File}).
547@end deftypefun
548
549The directory which is opened for reading by @code{opendir} is
550identified by the name. In some situations this is not sufficient.
551Or the way @code{opendir} implicitly creates a file descriptor for the
552directory is not the way a program might want it. In these cases an
553alternative interface can be used.
554
555@deftypefun {DIR *} fdopendir (int @var{fd})
556@standards{GNU, dirent.h}
557@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
558@c The DIR object is allocated with __alloc_dir, that calls malloc.
559The @code{fdopendir} function works just like @code{opendir} but
560instead of taking a file name and opening a file descriptor for the
561directory the caller is required to provide a file descriptor. This
562file descriptor is then used in subsequent uses of the returned
563directory stream object.
564
565The caller must make sure the file descriptor is associated with a
566directory and it allows reading.
567
568If the @code{fdopendir} call returns successfully the file descriptor
569is now under the control of the system. It can be used in the same
570way the descriptor implicitly created by @code{opendir} can be used
571but the program must not close the descriptor.
572
573In case the function is unsuccessful it returns a null pointer and the
574file descriptor remains to be usable by the program. The following
575@code{errno} error conditions are defined for this function:
576
577@table @code
578@item EBADF
579The file descriptor is not valid.
580
581@item ENOTDIR
582The file descriptor is not associated with a directory.
583
584@item EINVAL
585The descriptor does not allow reading the directory content.
586
587@item ENOMEM
588Not enough memory available.
589@end table
590@end deftypefun
591
592In some situations it can be desirable to get hold of the file
593descriptor which is created by the @code{opendir} call. For instance,
594to switch the current working directory to the directory just read the
595@code{fchdir} function could be used. Historically the @code{DIR} type
596was exposed and programs could access the fields. This does not happen
597in @theglibc{}. Instead a separate function is provided to allow
598access.
599
600@deftypefun int dirfd (DIR *@var{dirstream})
601@standards{GNU, dirent.h}
602@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
603The function @code{dirfd} returns the file descriptor associated with
604the directory stream @var{dirstream}. This descriptor can be used until
605the directory is closed with @code{closedir}. If the directory stream
606implementation is not using file descriptors the return value is
607@code{-1}.
608@end deftypefun
609
610@node Reading/Closing Directory
611@subsection Reading and Closing a Directory Stream
612
613@pindex dirent.h
614This section describes how to read directory entries from a directory
615stream, and how to close the stream when you are done with it. All the
616symbols are declared in the header file @file{dirent.h}.
617
618@deftypefun {struct dirent *} readdir (DIR *@var{dirstream})
619@standards{POSIX.1, dirent.h}
620@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
621@c This function holds dirstream's non-recursive lock, which brings
622@c about the usual issues with locks and async signals and cancellation,
623@c but the lock taking is not enough to make the returned value safe to
624@c use, since it points to a stream's internal buffer that can be
625@c overwritten by subsequent calls or even released by closedir.
626This function reads the next entry from the directory. It normally
627returns a pointer to a structure containing information about the
628file. This structure is associated with the @var{dirstream} handle
629and can be rewritten by a subsequent call.
630
631@strong{Portability Note:} On some systems @code{readdir} may not
632return entries for @file{.} and @file{..}, even though these are always
633valid file names in any directory. @xref{File Name Resolution}.
634
635If there are no more entries in the directory or an error is detected,
636@code{readdir} returns a null pointer. The following @code{errno} error
637conditions are defined for this function:
638
639@table @code
640@item EBADF
641The @var{dirstream} argument is not valid.
642@end table
643
644To distinguish between an end-of-directory condition or an error, you
645must set @code{errno} to zero before calling @code{readdir}. To avoid
646entering an infinite loop, you should stop reading from the directory
647after the first error.
648
649@strong{Caution:} The pointer returned by @code{readdir} points to
650a buffer within the @code{DIR} object. The data in that buffer will
651be overwritten by the next call to @code{readdir}. You must take care,
652for instance, to copy the @code{d_name} string if you need it later.
653
654Because of this, it is not safe to share a @code{DIR} object among
655multiple threads, unless you use your own locking to ensure that
656no thread calls @code{readdir} while another thread is still using the
657data from the previous call. In @theglibc{}, it is safe to call
658@code{readdir} from multiple threads as long as each thread uses
659its own @code{DIR} object. POSIX.1-2008 does not require this to
660be safe, but we are not aware of any operating systems where it
661does not work.
662
663@code{readdir_r} allows you to provide your own buffer for the
664@code{struct dirent}, but it is less portable than @code{readdir}, and
665has problems with very long filenames (see below). We recommend
666you use @code{readdir}, but do not share @code{DIR} objects.
667@end deftypefun
668
669@deftypefun int readdir_r (DIR *@var{dirstream}, struct dirent *@var{entry}, struct dirent **@var{result})
670@standards{GNU, dirent.h}
671@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
672This function is a version of @code{readdir} which performs internal
673locking. Like @code{readdir} it returns the next entry from the
674directory. To prevent conflicts between simultaneously running
675threads the result is stored inside the @var{entry} object.
676
677@strong{Portability Note:} @code{readdir_r} is deprecated. It is
678recommended to use @code{readdir} instead of @code{readdir_r} for the
679following reasons:
680
681@itemize @bullet
682@item
683On systems which do not define @code{NAME_MAX}, it may not be possible
684to use @code{readdir_r} safely because the caller does not specify the
685length of the buffer for the directory entry.
686
687@item
688On some systems, @code{readdir_r} cannot read directory entries with
689very long names. If such a name is encountered, @theglibc{}
690implementation of @code{readdir_r} returns with an error code of
691@code{ENAMETOOLONG} after the final directory entry has been read. On
692other systems, @code{readdir_r} may return successfully, but the
693@code{d_name} member may not be NUL-terminated or may be truncated.
694
695@item
696POSIX-1.2008 does not guarantee that @code{readdir} is thread-safe,
697even when access to the same @var{dirstream} is serialized. But in
698current implementations (including @theglibc{}), it is safe to call
699@code{readdir} concurrently on different @var{dirstream}s, so there is
700no need to use @code{readdir_r} in most multi-threaded programs. In
701the rare case that multiple threads need to read from the same
702@var{dirstream}, it is still better to use @code{readdir} and external
703synchronization.
704
705@item
706It is expected that future versions of POSIX will obsolete
707@code{readdir_r} and mandate the level of thread safety for
708@code{readdir} which is provided by @theglibc{} and other
709implementations today.
710@end itemize
711
712Normally @code{readdir_r} returns zero and sets @code{*@var{result}}
713to @var{entry}. If there are no more entries in the directory or an
714error is detected, @code{readdir_r} sets @code{*@var{result}} to a
715null pointer and returns a nonzero error code, also stored in
716@code{errno}, as described for @code{readdir}.
717
718It is also important to look at the definition of the @code{struct
719dirent} type. Simply passing a pointer to an object of this type for
720the second parameter of @code{readdir_r} might not be enough. Some
721systems don't define the @code{d_name} element sufficiently long. In
722this case the user has to provide additional space. There must be room
723for at least @code{NAME_MAX + 1} characters in the @code{d_name} array.
724Code to call @code{readdir_r} could look like this:
725
726@smallexample
727 union
728 @{
729 struct dirent d;
730 char b[offsetof (struct dirent, d_name) + NAME_MAX + 1];
731 @} u;
732
733 if (readdir_r (dir, &u.d, &res) == 0)
734 @dots{}
735@end smallexample
736@end deftypefun
737
738To support large filesystems on 32-bit machines there are LFS variants
739of the last two functions.
740
741@deftypefun {struct dirent64 *} readdir64 (DIR *@var{dirstream})
742@standards{LFS, dirent.h}
743@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
744The @code{readdir64} function is just like the @code{readdir} function
745except that it returns a pointer to a record of type @code{struct
746dirent64}. Some of the members of this data type (notably @code{d_ino})
747might have a different size to allow large filesystems.
748
749In all other aspects this function is equivalent to @code{readdir}.
750@end deftypefun
751
752@deftypefun int readdir64_r (DIR *@var{dirstream}, struct dirent64 *@var{entry}, struct dirent64 **@var{result})
753@standards{LFS, dirent.h}
754@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
755The deprecated @code{readdir64_r} function is equivalent to the
756@code{readdir_r} function except that it takes parameters of base type
757@code{struct dirent64} instead of @code{struct dirent} in the second and
758third position. The same precautions mentioned in the documentation of
759@code{readdir_r} also apply here.
760@end deftypefun
761
762@deftypefun int closedir (DIR *@var{dirstream})
763@standards{POSIX.1, dirent.h}
764@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{/hurd}}@acunsafe{@acsmem{} @acsfd{} @aculock{/hurd}}}
765@c No synchronization in the posix implementation, only in the hurd
766@c one. This is regarded as safe because it is undefined behavior if
767@c other threads could still be using the dir stream while it's closed.
768This function closes the directory stream @var{dirstream}. It returns
769@code{0} on success and @code{-1} on failure.
770
771The following @code{errno} error conditions are defined for this
772function:
773
774@table @code
775@item EBADF
776The @var{dirstream} argument is not valid.
777@end table
778@end deftypefun
779
780@node Simple Directory Lister
781@subsection Simple Program to List a Directory
782
783Here's a simple program that prints the names of the files in
784the current working directory:
785
786@smallexample
787@include dir.c.texi
788@end smallexample
789
790The order in which files appear in a directory tends to be fairly
791random. A more useful program would sort the entries (perhaps by
792alphabetizing them) before printing them; see
793@ref{Scanning Directory Content}, and @ref{Array Sort Function}.
794
795
796@node Random Access Directory
797@subsection Random Access in a Directory Stream
798
799@pindex dirent.h
800This section describes how to reread parts of a directory that you have
801already read from an open directory stream. All the symbols are
802declared in the header file @file{dirent.h}.
803
804@deftypefun void rewinddir (DIR *@var{dirstream})
805@standards{POSIX.1, dirent.h}
806@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
807The @code{rewinddir} function is used to reinitialize the directory
808stream @var{dirstream}, so that if you call @code{readdir} it
809returns information about the first entry in the directory again. This
810function also notices if files have been added or removed to the
811directory since it was opened with @code{opendir}. (Entries for these
812files might or might not be returned by @code{readdir} if they were
813added or removed since you last called @code{opendir} or
814@code{rewinddir}.)
815@end deftypefun
816
817@deftypefun {long int} telldir (DIR *@var{dirstream})
818@standards{BSD, dirent.h}
819@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{/bsd} @asulock{/bsd}}@acunsafe{@acsmem{/bsd} @aculock{/bsd}}}
820@c The implementation is safe on most platforms, but on BSD it uses
821@c cookies, buckets and records, and the global array of pointers to
822@c dynamically allocated records is guarded by a non-recursive lock.
823The @code{telldir} function returns the file position of the directory
824stream @var{dirstream}. You can use this value with @code{seekdir} to
825restore the directory stream to that position.
826@end deftypefun
827
828@deftypefun void seekdir (DIR *@var{dirstream}, long int @var{pos})
829@standards{BSD, dirent.h}
830@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{/bsd} @asulock{/bsd}}@acunsafe{@acsmem{/bsd} @aculock{/bsd}}}
831@c The implementation is safe on most platforms, but on BSD it uses
832@c cookies, buckets and records, and the global array of pointers to
833@c dynamically allocated records is guarded by a non-recursive lock.
834The @code{seekdir} function sets the file position of the directory
835stream @var{dirstream} to @var{pos}. The value @var{pos} must be the
836result of a previous call to @code{telldir} on this particular stream;
837closing and reopening the directory can invalidate values returned by
838@code{telldir}.
839@end deftypefun
840
841
842@node Scanning Directory Content
843@subsection Scanning the Content of a Directory
844
845A higher-level interface to the directory handling functions is the
846@code{scandir} function. With its help one can select a subset of the
847entries in a directory, possibly sort them and get a list of names as
848the result.
849
850@deftypefun int scandir (const char *@var{dir}, struct dirent ***@var{namelist}, int (*@var{selector}) (const struct dirent *), int (*@var{cmp}) (const struct dirent **, const struct dirent **))
851@standards{BSD, dirent.h}
852@standards{SVID, dirent.h}
853@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
854@c The scandir function calls __opendirat, __readdir, and __closedir to
855@c go over the named dir; malloc and realloc to allocate the namelist
856@c and copies of each selected dirent, besides the selector, if given,
857@c and qsort and the cmp functions if the latter is given. In spite of
858@c the cleanup handler that releases memory and the file descriptor in
859@c case of synchronous cancellation, an asynchronous cancellation may
860@c still leak memory and a file descriptor. Although readdir is unsafe
861@c in general, the use of an internal dir stream for sequential scanning
862@c of the directory with copying of dirents before subsequent calls
863@c makes the use safe, and the fact that the dir stream is private to
864@c each scandir call does away with the lock issues in readdir and
865@c closedir.
866
867The @code{scandir} function scans the contents of the directory selected
868by @var{dir}. The result in *@var{namelist} is an array of pointers to
869structures of type @code{struct dirent} which describe all selected
870directory entries and which is allocated using @code{malloc}. Instead
871of always getting all directory entries returned, the user supplied
872function @var{selector} can be used to decide which entries are in the
873result. Only the entries for which @var{selector} returns a non-zero
874value are selected.
875
876Finally the entries in *@var{namelist} are sorted using the
877user-supplied function @var{cmp}. The arguments passed to the @var{cmp}
878function are of type @code{struct dirent **}, therefore one cannot
879directly use the @code{strcmp} or @code{strcoll} functions; instead see
880the functions @code{alphasort} and @code{versionsort} below.
881
882The return value of the function is the number of entries placed in
883*@var{namelist}. If it is @code{-1} an error occurred (either the
884directory could not be opened for reading or memory allocation failed) and
885the global variable @code{errno} contains more information on the error.
886@end deftypefun
887
888As described above, the fourth argument to the @code{scandir} function
889must be a pointer to a sorting function. For the convenience of the
890programmer @theglibc{} contains implementations of functions which
891are very helpful for this purpose.
892
893@deftypefun int alphasort (const struct dirent **@var{a}, const struct dirent **@var{b})
894@standards{BSD, dirent.h}
895@standards{SVID, dirent.h}
896@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
897@c Calls strcoll.
898The @code{alphasort} function behaves like the @code{strcoll} function
899(@pxref{String/Array Comparison}). The difference is that the arguments
900are not string pointers but instead they are of type
901@code{struct dirent **}.
902
903The return value of @code{alphasort} is less than, equal to, or greater
904than zero depending on the order of the two entries @var{a} and @var{b}.
905@end deftypefun
906
907@deftypefun int versionsort (const struct dirent **@var{a}, const struct dirent **@var{b})
908@standards{GNU, dirent.h}
909@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
910@c Calls strverscmp, which will accesses the locale object multiple
911@c times.
912The @code{versionsort} function is like @code{alphasort} except that it
913uses the @code{strverscmp} function internally.
914@end deftypefun
915
916If the filesystem supports large files we cannot use the @code{scandir}
917anymore since the @code{dirent} structure might not able to contain all
918the information. The LFS provides the new type @w{@code{struct
919dirent64}}. To use this we need a new function.
920
921@deftypefun int scandir64 (const char *@var{dir}, struct dirent64 ***@var{namelist}, int (*@var{selector}) (const struct dirent64 *), int (*@var{cmp}) (const struct dirent64 **, const struct dirent64 **))
922@standards{GNU, dirent.h}
923@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
924@c See scandir.
925The @code{scandir64} function works like the @code{scandir} function
926except that the directory entries it returns are described by elements
927of type @w{@code{struct dirent64}}. The function pointed to by
928@var{selector} is again used to select the desired entries, except that
929@var{selector} now must point to a function which takes a
930@w{@code{struct dirent64 *}} parameter.
931
932Similarly the @var{cmp} function should expect its two arguments to be
933of type @code{struct dirent64 **}.
934@end deftypefun
935
936As @var{cmp} is now a function of a different type, the functions
937@code{alphasort} and @code{versionsort} cannot be supplied for that
938argument. Instead we provide the two replacement functions below.
939
940@deftypefun int alphasort64 (const struct dirent64 **@var{a}, const struct dirent **@var{b})
941@standards{GNU, dirent.h}
942@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
943@c See alphasort.
944The @code{alphasort64} function behaves like the @code{strcoll} function
945(@pxref{String/Array Comparison}). The difference is that the arguments
946are not string pointers but instead they are of type
947@code{struct dirent64 **}.
948
949Return value of @code{alphasort64} is less than, equal to, or greater
950than zero depending on the order of the two entries @var{a} and @var{b}.
951@end deftypefun
952
953@deftypefun int versionsort64 (const struct dirent64 **@var{a}, const struct dirent64 **@var{b})
954@standards{GNU, dirent.h}
955@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
956@c See versionsort.
957The @code{versionsort64} function is like @code{alphasort64}, excepted that it
958uses the @code{strverscmp} function internally.
959@end deftypefun
960
961It is important not to mix the use of @code{scandir} and the 64-bit
962comparison functions or vice versa. There are systems on which this
963works but on others it will fail miserably.
964
965@node Simple Directory Lister Mark II
966@subsection Simple Program to List a Directory, Mark II
967
968Here is a revised version of the directory lister found above
969(@pxref{Simple Directory Lister}). Using the @code{scandir} function we
970can avoid the functions which work directly with the directory contents.
971After the call the returned entries are available for direct use.
972
973@smallexample
974@include dir2.c.texi
975@end smallexample
976
977Note the simple selector function in this example. Since we want to see
978all directory entries we always return @code{1}.
979
980@node Low-level Directory Access
981@subsection Low-level Directory Access
982
983The stream-based directory functions are not AS-Safe and cannot be
984used after @code{vfork}. @xref{POSIX Safety Concepts}. The functions
985below provide an alternative that can be used in these contexts.
986
987Directory data is obtained from a file descriptor, as created by the
988@code{open} function, with or without the @code{O_DIRECTORY} flag.
989@xref{Opening and Closing Files}.
990
991@deftypefun ssize_t getdents64 (int @var{fd}, void *@var{buffer}, size_t @var{length})
992@standards{Linux, dirent.h}
993@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
994The @code{getdents64} function reads at most @var{length} bytes of
995directory entry data from the file descriptor @var{fd} and stores it
996into the byte array starting at @var{buffer}.
997
998On success, the function returns the number of bytes written to the
999buffer. This number is zero if @var{fd} is already at the end of the
1000directory stream. On error, the function returns @code{-1} and sets
1001@code{errno} to the appropriate error code.
1002
1003The data is stored as a sequence of @code{struct dirent64} records,
1004which can be traversed using the @code{d_reclen} member. The buffer
1005should be large enough to hold the largest possible directory entry.
1006Note that some file systems support file names longer than
1007@code{NAME_MAX} bytes (e.g., because they support up to 255 Unicode
1008characters), so a buffer size of at least 1024 is recommended.
1009
1010This function is specific to Linux.
1011@end deftypefun
1012
1013
1014@node Working with Directory Trees
1015@section Working with Directory Trees
1016@cindex directory hierarchy
1017@cindex hierarchy, directory
1018@cindex tree, directory
1019
1020The functions described so far for handling the files in a directory
1021have allowed you to either retrieve the information bit by bit, or to
1022process all the files as a group (see @code{scandir}). Sometimes it is
1023useful to process whole hierarchies of directories and their contained
1024files. The X/Open specification defines two functions to do this. The
1025simpler form is derived from an early definition in @w{System V} systems
1026and therefore this function is available on SVID-derived systems. The
1027prototypes and required definitions can be found in the @file{ftw.h}
1028header.
1029
1030There are four functions in this family: @code{ftw}, @code{nftw} and
1031their 64-bit counterparts @code{ftw64} and @code{nftw64}. These
1032functions take as one of their arguments a pointer to a callback
1033function of the appropriate type.
1034
1035@deftp {Data Type} __ftw_func_t
1036@standards{GNU, ftw.h}
1037
1038@smallexample
1039int (*) (const char *, const struct stat *, int)
1040@end smallexample
1041
1042The type of callback functions given to the @code{ftw} function. The
1043first parameter points to the file name, the second parameter to an
1044object of type @code{struct stat} which is filled in for the file named
1045in the first parameter.
1046
1047@noindent
1048The last parameter is a flag giving more information about the current
1049file. It can have the following values:
1050
1051@vtable @code
1052@item FTW_F
1053The item is either a normal file or a file which does not fit into one
1054of the following categories. This could be special files, sockets etc.
1055@item FTW_D
1056The item is a directory.
1057@item FTW_NS
1058The @code{stat} call failed and so the information pointed to by the
1059second parameter is invalid.
1060@item FTW_DNR
1061The item is a directory which cannot be read.
1062@item FTW_SL
1063The item is a symbolic link. Since symbolic links are normally followed
1064seeing this value in a @code{ftw} callback function means the referenced
1065file does not exist. The situation for @code{nftw} is different.
1066
1067This value is only available if the program is compiled with
1068@code{_XOPEN_EXTENDED} defined before including
1069the first header. The original SVID systems do not have symbolic links.
1070@end vtable
1071
1072If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
1073type is in fact @code{__ftw64_func_t} since this mode changes
1074@code{struct stat} to be @code{struct stat64}.
1075@end deftp
1076
1077For the LFS interface and for use in the function @code{ftw64}, the
1078header @file{ftw.h} defines another function type.
1079
1080@deftp {Data Type} __ftw64_func_t
1081@standards{GNU, ftw.h}
1082
1083@smallexample
1084int (*) (const char *, const struct stat64 *, int)
1085@end smallexample
1086
1087This type is used just like @code{__ftw_func_t} for the callback
1088function, but this time is called from @code{ftw64}. The second
1089parameter to the function is a pointer to a variable of type
1090@code{struct stat64} which is able to represent the larger values.
1091@end deftp
1092
1093@deftp {Data Type} __nftw_func_t
1094@standards{GNU, ftw.h}
1095
1096@smallexample
1097int (*) (const char *, const struct stat *, int, struct FTW *)
1098@end smallexample
1099
1100The first three arguments are the same as for the @code{__ftw_func_t}
1101type. However for the third argument some additional values are defined
1102to allow finer differentiation:
1103@vtable @code
1104@item FTW_DP
1105The current item is a directory and all subdirectories have already been
1106visited and reported. This flag is returned instead of @code{FTW_D} if
1107the @code{FTW_DEPTH} flag is passed to @code{nftw} (see below).
1108@item FTW_SLN
1109The current item is a stale symbolic link. The file it points to does
1110not exist.
1111@end vtable
1112
1113The last parameter of the callback function is a pointer to a structure
1114with some extra information as described below.
1115
1116If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
1117type is in fact @code{__nftw64_func_t} since this mode changes
1118@code{struct stat} to be @code{struct stat64}.
1119@end deftp
1120
1121For the LFS interface there is also a variant of this data type
1122available which has to be used with the @code{nftw64} function.
1123
1124@deftp {Data Type} __nftw64_func_t
1125@standards{GNU, ftw.h}
1126
1127@smallexample
1128int (*) (const char *, const struct stat64 *, int, struct FTW *)
1129@end smallexample
1130
1131This type is used just like @code{__nftw_func_t} for the callback
1132function, but this time is called from @code{nftw64}. The second
1133parameter to the function is this time a pointer to a variable of type
1134@code{struct stat64} which is able to represent the larger values.
1135@end deftp
1136
1137@deftp {Data Type} {struct FTW}
1138@standards{XPG4.2, ftw.h}
1139The information contained in this structure helps in interpreting the
1140name parameter and gives some information about the current state of the
1141traversal of the directory hierarchy.
1142
1143@table @code
1144@item int base
1145The value is the offset into the string passed in the first parameter to
1146the callback function of the beginning of the file name. The rest of
1147the string is the path of the file. This information is especially
1148important if the @code{FTW_CHDIR} flag was set in calling @code{nftw}
1149since then the current directory is the one the current item is found
1150in.
1151@item int level
1152Whilst processing, the code tracks how many directories down it has gone
1153to find the current file. This nesting level starts at @math{0} for
1154files in the initial directory (or is zero for the initial file if a
1155file was passed).
1156@end table
1157@end deftp
1158
1159
1160@deftypefun int ftw (const char *@var{filename}, __ftw_func_t @var{func}, int @var{descriptors})
1161@standards{SVID, ftw.h}
1162@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
1163@c see nftw for safety details
1164The @code{ftw} function calls the callback function given in the
1165parameter @var{func} for every item which is found in the directory
1166specified by @var{filename} and all directories below. The function
1167follows symbolic links if necessary but does not process an item twice.
1168If @var{filename} is not a directory then it itself is the only object
1169returned to the callback function.
1170
1171The file name passed to the callback function is constructed by taking
1172the @var{filename} parameter and appending the names of all passed
1173directories and then the local file name. So the callback function can
1174use this parameter to access the file. @code{ftw} also calls
1175@code{stat} for the file and passes that information on to the callback
1176function. If this @code{stat} call is not successful the failure is
1177indicated by setting the third argument of the callback function to
1178@code{FTW_NS}. Otherwise it is set according to the description given
1179in the account of @code{__ftw_func_t} above.
1180
1181The callback function is expected to return @math{0} to indicate that no
1182error occurred and that processing should continue. If an error
1183occurred in the callback function or it wants @code{ftw} to return
1184immediately, the callback function can return a value other than
1185@math{0}. This is the only correct way to stop the function. The
1186program must not use @code{setjmp} or similar techniques to continue
1187from another place. This would leave resources allocated by the
1188@code{ftw} function unfreed.
1189
1190The @var{descriptors} parameter to @code{ftw} specifies how many file
1191descriptors it is allowed to consume. The function runs faster the more
1192descriptors it can use. For each level in the directory hierarchy at
1193most one descriptor is used, but for very deep ones any limit on open
1194file descriptors for the process or the system may be exceeded.
1195Moreover, file descriptor limits in a multi-threaded program apply to
1196all the threads as a group, and therefore it is a good idea to supply a
1197reasonable limit to the number of open descriptors.
1198
1199The return value of the @code{ftw} function is @math{0} if all callback
1200function calls returned @math{0} and all actions performed by the
1201@code{ftw} succeeded. If a function call failed (other than calling
1202@code{stat} on an item) the function returns @math{-1}. If a callback
1203function returns a value other than @math{0} this value is returned as
1204the return value of @code{ftw}.
1205
1206When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
120732-bit system this function is in fact @code{ftw64}, i.e., the LFS
1208interface transparently replaces the old interface.
1209@end deftypefun
1210
1211@deftypefun int ftw64 (const char *@var{filename}, __ftw64_func_t @var{func}, int @var{descriptors})
1212@standards{Unix98, ftw.h}
1213@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
1214This function is similar to @code{ftw} but it can work on filesystems
1215with large files. File information is reported using a variable of type
1216@code{struct stat64} which is passed by reference to the callback
1217function.
1218
1219When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
122032-bit system this function is available under the name @code{ftw} and
1221transparently replaces the old implementation.
1222@end deftypefun
1223
1224@deftypefun int nftw (const char *@var{filename}, __nftw_func_t @var{func}, int @var{descriptors}, int @var{flag})
1225@standards{XPG4.2, ftw.h}
1226@safety{@prelim{}@mtsafe{@mtasscwd{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{} @acscwd{}}}
1227@c ftw_startup calls alloca, malloc, free, xstat/lxstat, tdestroy, and ftw_dir
1228@c if FTW_CHDIR, call open, and fchdir, or chdir and getcwd
1229@c ftw_dir calls open_dir_stream, readdir64, process_entry, closedir
1230@c if FTW_CHDIR, also calls fchdir
1231@c open_dir_stream calls malloc, realloc, readdir64, free, closedir,
1232@c then openat64_not_cancel_3 and fdopendir or opendir, then dirfd.
1233@c process_entry may cal realloc, fxstatat/lxstat/xstat, ftw_dir, and
1234@c find_object (tsearch) and add_object (tfind).
1235@c Since each invocation of *ftw uses its own private search tree, none
1236@c of the search tree concurrency issues apply.
1237The @code{nftw} function works like the @code{ftw} functions. They call
1238the callback function @var{func} for all items found in the directory
1239@var{filename} and below. At most @var{descriptors} file descriptors
1240are consumed during the @code{nftw} call.
1241
1242One difference is that the callback function is of a different type. It
1243is of type @w{@code{struct FTW *}} and provides the callback function
1244with the extra information described above.
1245
1246A second difference is that @code{nftw} takes a fourth argument, which
1247is @math{0} or a bitwise-OR combination of any of the following values.
1248
1249@vtable @code
1250@item FTW_PHYS
1251While traversing the directory symbolic links are not followed. Instead
1252symbolic links are reported using the @code{FTW_SL} value for the type
1253parameter to the callback function. If the file referenced by a
1254symbolic link does not exist @code{FTW_SLN} is returned instead.
1255@item FTW_MOUNT
1256The callback function is only called for items which are on the same
1257mounted filesystem as the directory given by the @var{filename}
1258parameter to @code{nftw}.
1259@item FTW_CHDIR
1260If this flag is given the current working directory is changed to the
1261directory of the reported object before the callback function is called.
1262When @code{ntfw} finally returns the current directory is restored to
1263its original value.
1264@item FTW_DEPTH
1265If this option is specified then all subdirectories and files within
1266them are processed before processing the top directory itself
1267(depth-first processing). This also means the type flag given to the
1268callback function is @code{FTW_DP} and not @code{FTW_D}.
1269@item FTW_ACTIONRETVAL
1270If this option is specified then return values from callbacks
1271are handled differently. If the callback returns @code{FTW_CONTINUE},
1272walking continues normally. @code{FTW_STOP} means walking stops
1273and @code{FTW_STOP} is returned to the caller. If @code{FTW_SKIP_SUBTREE}
1274is returned by the callback with @code{FTW_D} argument, the subtree
1275is skipped and walking continues with next sibling of the directory.
1276If @code{FTW_SKIP_SIBLINGS} is returned by the callback, all siblings
1277of the current entry are skipped and walking continues in its parent.
1278No other return values should be returned from the callbacks if
1279this option is set. This option is a GNU extension.
1280@end vtable
1281
1282The return value is computed in the same way as for @code{ftw}.
1283@code{nftw} returns @math{0} if no failures occurred and all callback
1284functions returned @math{0}. In case of internal errors, such as memory
1285problems, the return value is @math{-1} and @code{errno} is set
1286accordingly. If the return value of a callback invocation was non-zero
1287then that value is returned.
1288
1289When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
129032-bit system this function is in fact @code{nftw64}, i.e., the LFS
1291interface transparently replaces the old interface.
1292@end deftypefun
1293
1294@deftypefun int nftw64 (const char *@var{filename}, __nftw64_func_t @var{func}, int @var{descriptors}, int @var{flag})
1295@standards{Unix98, ftw.h}
1296@safety{@prelim{}@mtsafe{@mtasscwd{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{} @acscwd{}}}
1297This function is similar to @code{nftw} but it can work on filesystems
1298with large files. File information is reported using a variable of type
1299@code{struct stat64} which is passed by reference to the callback
1300function.
1301
1302When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
130332-bit system this function is available under the name @code{nftw} and
1304transparently replaces the old implementation.
1305@end deftypefun
1306
1307
1308@node Hard Links
1309@section Hard Links
1310@cindex hard link
1311@cindex link, hard
1312@cindex multiple names for one file
1313@cindex file names, multiple
1314
1315In POSIX systems, one file can have many names at the same time. All of
1316the names are equally real, and no one of them is preferred to the
1317others.
1318
1319To add a name to a file, use the @code{link} function. (The new name is
1320also called a @dfn{hard link} to the file.) Creating a new link to a
1321file does not copy the contents of the file; it simply makes a new name
1322by which the file can be known, in addition to the file's existing name
1323or names.
1324
1325One file can have names in several directories, so the organization
1326of the file system is not a strict hierarchy or tree.
1327
1328In most implementations, it is not possible to have hard links to the
1329same file in multiple file systems. @code{link} reports an error if you
1330try to make a hard link to the file from another file system when this
1331cannot be done.
1332
1333The prototype for the @code{link} function is declared in the header
1334file @file{unistd.h}.
1335@pindex unistd.h
1336
1337@deftypefun int link (const char *@var{oldname}, const char *@var{newname})
1338@standards{POSIX.1, unistd.h}
1339@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1340The @code{link} function makes a new link to the existing file named by
1341@var{oldname}, under the new name @var{newname}.
1342
1343This function returns a value of @code{0} if it is successful and
1344@code{-1} on failure. In addition to the usual file name errors
1345(@pxref{File Name Errors}) for both @var{oldname} and @var{newname}, the
1346following @code{errno} error conditions are defined for this function:
1347
1348@table @code
1349@item EACCES
1350You are not allowed to write to the directory in which the new link is
1351to be written.
1352@ignore
1353Some implementations also require that the existing file be accessible
1354by the caller, and use this error to report failure for that reason.
1355@end ignore
1356
1357@item EEXIST
1358There is already a file named @var{newname}. If you want to replace
1359this link with a new link, you must remove the old link explicitly first.
1360
1361@item EMLINK
1362There are already too many links to the file named by @var{oldname}.
1363(The maximum number of links to a file is @w{@code{LINK_MAX}}; see
1364@ref{Limits for Files}.)
1365
1366@item ENOENT
1367The file named by @var{oldname} doesn't exist. You can't make a link to
1368a file that doesn't exist.
1369
1370@item ENOSPC
1371The directory or file system that would contain the new link is full
1372and cannot be extended.
1373
1374@item EPERM
1375On @gnulinuxhurdsystems{} and some others, you cannot make links to
1376directories.
1377Many systems allow only privileged users to do so. This error
1378is used to report the problem.
1379
1380@item EROFS
1381The directory containing the new link can't be modified because it's on
1382a read-only file system.
1383
1384@item EXDEV
1385The directory specified in @var{newname} is on a different file system
1386than the existing file.
1387
1388@item EIO
1389A hardware error occurred while trying to read or write the to filesystem.
1390@end table
1391@end deftypefun
1392
1393@deftypefun int linkat (int oldfd, const char *@var{oldname}, int newfd, const char *@var{newname}, int flags)
1394@standards{POSIX.1, unistd.h}
1395@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1396
1397The @code{linkat} function is analogous to the @code{link} function,
1398except that it identifies its source and target using a combination of a
1399file descriptor (referring to a directory) and a file name.
1400@xref{Descriptor-Relative Access}. For @code{linkat}, if a file name is
1401not absolute, it is resolved relative to the corresponding file
1402descriptor. As usual, the special value @code{AT_FDCWD} denotes the
1403current directory.
1404
1405The @var{flags} argument is a combination of the following flags:
1406
1407@table @code
1408@item AT_SYMLINK_FOLLOW
1409If the source path identified by @var{oldfd} and @var{oldname} is a
1410symbolic link, @code{linkat} follows the symbolic link and creates a
1411link to its target. If the flag is not set, a link for the symbolic
1412link itself is created; this is not supported by all file systems and
1413@code{linkat} can fail in this case.
1414
1415@item AT_EMPTY_PATH
1416If this flag is specified, @var{oldname} can be an empty string. In
1417this case, a new link to the file denoted by the descriptor @var{oldfd}
1418is created, which may have been opened with @code{O_PATH} or
1419@code{O_TMPFILE}. This flag is a GNU extension.
1420@end table
1421@end deftypefun
1422
1423@node Symbolic Links
1424@section Symbolic Links
1425@cindex soft link
1426@cindex link, soft
1427@cindex symbolic link
1428@cindex link, symbolic
1429
1430@gnusystems{} support @dfn{soft links} or @dfn{symbolic links}. This
1431is a kind of ``file'' that is essentially a pointer to another file
1432name. Unlike hard links, symbolic links can be made to directories or
1433across file systems with no restrictions. You can also make a symbolic
1434link to a name which is not the name of any file. (Opening this link
1435will fail until a file by that name is created.) Likewise, if the
1436symbolic link points to an existing file which is later deleted, the
1437symbolic link continues to point to the same file name even though the
1438name no longer names any file.
1439
1440The reason symbolic links work the way they do is that special things
1441happen when you try to open the link. The @code{open} function realizes
1442you have specified the name of a link, reads the file name contained in
1443the link, and opens that file name instead. The @code{stat} function
1444likewise operates on the file that the symbolic link points to, instead
1445of on the link itself.
1446
1447By contrast, other operations such as deleting or renaming the file
1448operate on the link itself. The functions @code{readlink} and
1449@code{lstat} also refrain from following symbolic links, because their
1450purpose is to obtain information about the link. @code{link}, the
1451function that makes a hard link, does too. It makes a hard link to the
1452symbolic link, which one rarely wants.
1453
1454Some systems have, for some functions operating on files, a limit on
1455how many symbolic links are followed when resolving a path name. The
1456limit if it exists is published in the @file{sys/param.h} header file.
1457
1458@deftypevr Macro int MAXSYMLINKS
1459@standards{BSD, sys/param.h}
1460
1461The macro @code{MAXSYMLINKS} specifies how many symlinks some function
1462will follow before returning @code{ELOOP}. Not all functions behave the
1463same and this value is not the same as that returned for
1464@code{_SC_SYMLOOP} by @code{sysconf}. In fact, the @code{sysconf}
1465result can indicate that there is no fixed limit although
1466@code{MAXSYMLINKS} exists and has a finite value.
1467@end deftypevr
1468
1469Prototypes for most of the functions listed in this section are in
1470@file{unistd.h}.
1471@pindex unistd.h
1472
1473@deftypefun int symlink (const char *@var{oldname}, const char *@var{newname})
1474@standards{BSD, unistd.h}
1475@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1476The @code{symlink} function makes a symbolic link to @var{oldname} named
1477@var{newname}.
1478
1479The normal return value from @code{symlink} is @code{0}. A return value
1480of @code{-1} indicates an error. In addition to the usual file name
1481syntax errors (@pxref{File Name Errors}), the following @code{errno}
1482error conditions are defined for this function:
1483
1484@table @code
1485@item EEXIST
1486There is already an existing file named @var{newname}.
1487
1488@item EROFS
1489The file @var{newname} would exist on a read-only file system.
1490
1491@item ENOSPC
1492The directory or file system cannot be extended to make the new link.
1493
1494@item EIO
1495A hardware error occurred while reading or writing data on the disk.
1496
1497@comment not sure about these
1498@ignore
1499@item ELOOP
1500There are too many levels of indirection. This can be the result of
1501circular symbolic links to directories.
1502
1503@item EDQUOT
1504The new link can't be created because the user's disk quota has been
1505exceeded.
1506@end ignore
1507@end table
1508@end deftypefun
1509
1510@deftypefun ssize_t readlink (const char *@var{filename}, char *@var{buffer}, size_t @var{size})
1511@standards{BSD, unistd.h}
1512@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1513The @code{readlink} function gets the value of the symbolic link
1514@var{filename}. The file name that the link points to is copied into
1515@var{buffer}. This file name string is @emph{not} null-terminated;
1516@code{readlink} normally returns the number of characters copied. The
1517@var{size} argument specifies the maximum number of characters to copy,
1518usually the allocation size of @var{buffer}.
1519
1520If the return value equals @var{size}, you cannot tell whether or not
1521there was room to return the entire name. So make a bigger buffer and
1522call @code{readlink} again. Here is an example:
1523
1524@smallexample
1525char *
1526readlink_malloc (const char *filename)
1527@{
1528 size_t size = 50;
1529 char *buffer = NULL;
1530
1531 while (1)
1532 @{
1533 buffer = xreallocarray (buffer, size, 2);
1534 size *= 2;
1535 ssize_t nchars = readlink (filename, buffer, size);
1536 if (nchars < 0)
1537 @{
1538 free (buffer);
1539 return NULL;
1540 @}
1541 if (nchars < size)
1542 return buffer;
1543 @}
1544@}
1545@end smallexample
1546
1547@c @group Invalid outside example.
1548A value of @code{-1} is returned in case of error. In addition to the
1549usual file name errors (@pxref{File Name Errors}), the following
1550@code{errno} error conditions are defined for this function:
1551
1552@table @code
1553@item EINVAL
1554The named file is not a symbolic link.
1555
1556@item EIO
1557A hardware error occurred while reading or writing data on the disk.
1558@end table
1559@c @end group
1560@end deftypefun
1561
1562In some situations it is desirable to resolve all the
1563symbolic links to get the real
1564name of a file where no prefix names a symbolic link which is followed
1565and no filename in the path is @code{.} or @code{..}. This is for
1566instance desirable if files have to be compared in which case different
1567names can refer to the same inode.
1568
1569@deftypefun {char *} canonicalize_file_name (const char *@var{name})
1570@standards{GNU, stdlib.h}
1571@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
1572@c Calls realpath.
1573
1574The @code{canonicalize_file_name} function returns the absolute name of
1575the file named by @var{name} which contains no @code{.}, @code{..}
1576components nor any repeated path separators (@code{/}) or symlinks. The
1577result is passed back as the return value of the function in a block of
1578memory allocated with @code{malloc}. If the result is not used anymore
1579the memory should be freed with a call to @code{free}.
1580
1581If any of the path components are missing the function returns a NULL
1582pointer. This is also what is returned if the length of the path
1583reaches or exceeds @code{PATH_MAX} characters. In any case
1584@code{errno} is set accordingly.
1585
1586@table @code
1587@item ENAMETOOLONG
1588The resulting path is too long. This error only occurs on systems which
1589have a limit on the file name length.
1590
1591@item EACCES
1592At least one of the path components is not readable.
1593
1594@item ENOENT
1595The input file name is empty.
1596
1597@item ENOENT
1598At least one of the path components does not exist.
1599
1600@item ELOOP
1601More than @code{MAXSYMLINKS} many symlinks have been followed.
1602@end table
1603
1604This function is a GNU extension and is declared in @file{stdlib.h}.
1605@end deftypefun
1606
1607The Unix standard includes a similar function which differs from
1608@code{canonicalize_file_name} in that the user has to provide the buffer
1609where the result is placed in.
1610
1611@deftypefun {char *} realpath (const char *restrict @var{name}, char *restrict @var{resolved})
1612@standards{XPG, stdlib.h}
1613@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
1614@c Calls malloc, realloc, getcwd, lxstat64, readlink, alloca.
1615
1616A call to @code{realpath} where the @var{resolved} parameter is
1617@code{NULL} behaves exactly like @code{canonicalize_file_name}. The
1618function allocates a buffer for the file name and returns a pointer to
1619it. If @var{resolved} is not @code{NULL} it points to a buffer into
1620which the result is copied. It is the callers responsibility to
1621allocate a buffer which is large enough. On systems which define
1622@code{PATH_MAX} this means the buffer must be large enough for a
1623pathname of this size. For systems without limitations on the pathname
1624length the requirement cannot be met and programs should not call
1625@code{realpath} with anything but @code{NULL} for the second parameter.
1626
1627One other difference is that the buffer @var{resolved} (if nonzero) will
1628contain the part of the path component which does not exist or is not
1629readable if the function returns @code{NULL} and @code{errno} is set to
1630@code{EACCES} or @code{ENOENT}.
1631
1632This function is declared in @file{stdlib.h}.
1633@end deftypefun
1634
1635The advantage of using this function is that it is more widely
1636available. The drawback is that it reports failures for long paths on
1637systems which have no limits on the file name length.
1638
1639@node Deleting Files
1640@section Deleting Files
1641@cindex deleting a file
1642@cindex removing a file
1643@cindex unlinking a file
1644
1645You can delete a file with @code{unlink} or @code{remove}.
1646
1647Deletion actually deletes a file name. If this is the file's only name,
1648then the file is deleted as well. If the file has other remaining names
1649(@pxref{Hard Links}), it remains accessible under those names.
1650
1651@deftypefun int unlink (const char *@var{filename})
1652@standards{POSIX.1, unistd.h}
1653@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1654The @code{unlink} function deletes the file name @var{filename}. If
1655this is a file's sole name, the file itself is also deleted. (Actually,
1656if any process has the file open when this happens, deletion is
1657postponed until all processes have closed the file.)
1658
1659@pindex unistd.h
1660The function @code{unlink} is declared in the header file @file{unistd.h}.
1661
1662This function returns @code{0} on successful completion, and @code{-1}
1663on error. In addition to the usual file name errors
1664(@pxref{File Name Errors}), the following @code{errno} error conditions are
1665defined for this function:
1666
1667@table @code
1668@item EACCES
1669Write permission is denied for the directory from which the file is to be
1670removed, or the directory has the sticky bit set and you do not own the file.
1671
1672@item EBUSY
1673This error indicates that the file is being used by the system in such a
1674way that it can't be unlinked. For example, you might see this error if
1675the file name specifies the root directory or a mount point for a file
1676system.
1677
1678@item ENOENT
1679The file name to be deleted doesn't exist.
1680
1681@item EPERM
1682On some systems @code{unlink} cannot be used to delete the name of a
1683directory, or at least can only be used this way by a privileged user.
1684To avoid such problems, use @code{rmdir} to delete directories. (On
1685@gnulinuxhurdsystems{} @code{unlink} can never delete the name of a directory.)
1686
1687@item EROFS
1688The directory containing the file name to be deleted is on a read-only
1689file system and can't be modified.
1690@end table
1691@end deftypefun
1692
1693@deftypefun int rmdir (const char *@var{filename})
1694@standards{POSIX.1, unistd.h}
1695@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1696@cindex directories, deleting
1697@cindex deleting a directory
1698The @code{rmdir} function deletes a directory. The directory must be
1699empty before it can be removed; in other words, it can only contain
1700entries for @file{.} and @file{..}.
1701
1702In most other respects, @code{rmdir} behaves like @code{unlink}. There
1703are two additional @code{errno} error conditions defined for
1704@code{rmdir}:
1705
1706@table @code
1707@item ENOTEMPTY
1708@itemx EEXIST
1709The directory to be deleted is not empty.
1710@end table
1711
1712These two error codes are synonymous; some systems use one, and some use
1713the other. @gnulinuxhurdsystems{} always use @code{ENOTEMPTY}.
1714
1715The prototype for this function is declared in the header file
1716@file{unistd.h}.
1717@pindex unistd.h
1718@end deftypefun
1719
1720@deftypefun int remove (const char *@var{filename})
1721@standards{ISO, stdio.h}
1722@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1723@c Calls unlink and rmdir.
1724This is the @w{ISO C} function to remove a file. It works like
1725@code{unlink} for files and like @code{rmdir} for directories.
1726@code{remove} is declared in @file{stdio.h}.
1727@pindex stdio.h
1728@end deftypefun
1729
1730@node Renaming Files
1731@section Renaming Files
1732
1733The @code{rename} function is used to change a file's name.
1734
1735@cindex renaming a file
1736@deftypefun int rename (const char *@var{oldname}, const char *@var{newname})
1737@standards{ISO, stdio.h}
1738@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1739@c In the absence of a rename syscall, there's an emulation with link
1740@c and unlink, but it's racy, even more so if newname exists and is
1741@c unlinked first.
1742The @code{rename} function renames the file @var{oldname} to
1743@var{newname}. The file formerly accessible under the name
1744@var{oldname} is afterwards accessible as @var{newname} instead. (If
1745the file had any other names aside from @var{oldname}, it continues to
1746have those names.)
1747
1748The directory containing the name @var{newname} must be on the same file
1749system as the directory containing the name @var{oldname}.
1750
1751One special case for @code{rename} is when @var{oldname} and
1752@var{newname} are two names for the same file. The consistent way to
1753handle this case is to delete @var{oldname}. However, in this case
1754POSIX requires that @code{rename} do nothing and report success---which
1755is inconsistent. We don't know what your operating system will do.
1756
1757If @var{oldname} is not a directory, then any existing file named
1758@var{newname} is removed during the renaming operation. However, if
1759@var{newname} is the name of a directory, @code{rename} fails in this
1760case.
1761
1762If @var{oldname} is a directory, then either @var{newname} must not
1763exist or it must name a directory that is empty. In the latter case,
1764the existing directory named @var{newname} is deleted first. The name
1765@var{newname} must not specify a subdirectory of the directory
1766@code{oldname} which is being renamed.
1767
1768One useful feature of @code{rename} is that the meaning of @var{newname}
1769changes ``atomically'' from any previously existing file by that name to
1770its new meaning (i.e., the file that was called @var{oldname}). There is
1771no instant at which @var{newname} is non-existent ``in between'' the old
1772meaning and the new meaning. If there is a system crash during the
1773operation, it is possible for both names to still exist; but
1774@var{newname} will always be intact if it exists at all.
1775
1776If @code{rename} fails, it returns @code{-1}. In addition to the usual
1777file name errors (@pxref{File Name Errors}), the following
1778@code{errno} error conditions are defined for this function:
1779
1780@table @code
1781@item EACCES
1782One of the directories containing @var{newname} or @var{oldname}
1783refuses write permission; or @var{newname} and @var{oldname} are
1784directories and write permission is refused for one of them.
1785
1786@item EBUSY
1787A directory named by @var{oldname} or @var{newname} is being used by
1788the system in a way that prevents the renaming from working. This includes
1789directories that are mount points for filesystems, and directories
1790that are the current working directories of processes.
1791
1792@item ENOTEMPTY
1793@itemx EEXIST
1794The directory @var{newname} isn't empty. @gnulinuxhurdsystems{} always return
1795@code{ENOTEMPTY} for this, but some other systems return @code{EEXIST}.
1796
1797@item EINVAL
1798@var{oldname} is a directory that contains @var{newname}.
1799
1800@item EISDIR
1801@var{newname} is a directory but the @var{oldname} isn't.
1802
1803@item EMLINK
1804The parent directory of @var{newname} would have too many links
1805(entries).
1806
1807@item ENOENT
1808The file @var{oldname} doesn't exist.
1809
1810@item ENOSPC
1811The directory that would contain @var{newname} has no room for another
1812entry, and there is no space left in the file system to expand it.
1813
1814@item EROFS
1815The operation would involve writing to a directory on a read-only file
1816system.
1817
1818@item EXDEV
1819The two file names @var{newname} and @var{oldname} are on different
1820file systems.
1821@end table
1822@end deftypefun
1823
1824@node Creating Directories
1825@section Creating Directories
1826@cindex creating a directory
1827@cindex directories, creating
1828
1829@pindex mkdir
1830Directories are created with the @code{mkdir} function. (There is also
1831a shell command @code{mkdir} which does the same thing.)
1832@c !!! umask
1833
1834@deftypefun int mkdir (const char *@var{filename}, mode_t @var{mode})
1835@standards{POSIX.1, sys/stat.h}
1836@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1837The @code{mkdir} function creates a new, empty directory with name
1838@var{filename}.
1839
1840The argument @var{mode} specifies the file permissions for the new
1841directory file. @xref{Permission Bits}, for more information about
1842this.
1843
1844A return value of @code{0} indicates successful completion, and
1845@code{-1} indicates failure. In addition to the usual file name syntax
1846errors (@pxref{File Name Errors}), the following @code{errno} error
1847conditions are defined for this function:
1848
1849@table @code
1850@item EACCES
1851Write permission is denied for the parent directory in which the new
1852directory is to be added.
1853
1854@item EEXIST
1855A file named @var{filename} already exists.
1856
1857@item EMLINK
1858The parent directory has too many links (entries).
1859
1860Well-designed file systems never report this error, because they permit
1861more links than your disk could possibly hold. However, you must still
1862take account of the possibility of this error, as it could result from
1863network access to a file system on another machine.
1864
1865@item ENOSPC
1866The file system doesn't have enough room to create the new directory.
1867
1868@item EROFS
1869The parent directory of the directory being created is on a read-only
1870file system and cannot be modified.
1871@end table
1872
1873To use this function, your program should include the header file
1874@file{sys/stat.h}.
1875@pindex sys/stat.h
1876@end deftypefun
1877
1878@node File Attributes
1879@section File Attributes
1880
1881@pindex ls
1882When you issue an @samp{ls -l} shell command on a file, it gives you
1883information about the size of the file, who owns it, when it was last
1884modified, etc. These are called the @dfn{file attributes}, and are
1885associated with the file itself and not a particular one of its names.
1886
1887This section contains information about how you can inquire about and
1888modify the attributes of a file.
1889
1890@menu
1891* Attribute Meanings:: The names of the file attributes,
1892 and what their values mean.
1893* Reading Attributes:: How to read the attributes of a file.
1894* Testing File Type:: Distinguishing ordinary files,
1895 directories, links@dots{}
1896* File Owner:: How ownership for new files is determined,
1897 and how to change it.
1898* Permission Bits:: How information about a file's access
1899 mode is stored.
1900* Access Permission:: How the system decides who can access a file.
1901* Setting Permissions:: How permissions for new files are assigned,
1902 and how to change them.
1903* Testing File Access:: How to find out if your process can
1904 access a file.
1905* File Times:: About the time attributes of a file.
1906* File Size:: Manually changing the size of a file.
1907* Storage Allocation:: Allocate backing storage for files.
1908@end menu
1909
1910@node Attribute Meanings
1911@subsection The meaning of the File Attributes
1912@cindex status of a file
1913@cindex attributes of a file
1914@cindex file attributes
1915
1916When you read the attributes of a file, they come back in a structure
1917called @code{struct stat}. This section describes the names of the
1918attributes, their data types, and what they mean. For the functions
1919to read the attributes of a file, see @ref{Reading Attributes}.
1920
1921The header file @file{sys/stat.h} declares all the symbols defined
1922in this section.
1923@pindex sys/stat.h
1924
1925@deftp {Data Type} {struct stat}
1926@standards{POSIX.1, sys/stat.h}
1927The @code{stat} structure type is used to return information about the
1928attributes of a file. It contains at least the following members:
1929
1930@table @code
1931@item mode_t st_mode
1932Specifies the mode of the file. This includes file type information
1933(@pxref{Testing File Type}) and the file permission bits
1934(@pxref{Permission Bits}).
1935
1936@item ino_t st_ino
1937The file serial number, which distinguishes this file from all other
1938files on the same device.
1939
1940@item dev_t st_dev
1941Identifies the device containing the file. The @code{st_ino} and
1942@code{st_dev}, taken together, uniquely identify the file. The
1943@code{st_dev} value is not necessarily consistent across reboots or
1944system crashes, however.
1945
1946@item nlink_t st_nlink
1947The number of hard links to the file. This count keeps track of how
1948many directories have entries for this file. If the count is ever
1949decremented to zero, then the file itself is discarded as soon as no
1950process still holds it open. Symbolic links are not counted in the
1951total.
1952
1953@item uid_t st_uid
1954The user ID of the file's owner. @xref{File Owner}.
1955
1956@item gid_t st_gid
1957The group ID of the file. @xref{File Owner}.
1958
1959@item off_t st_size
1960This specifies the size of a regular file in bytes. For files that are
1961really devices this field isn't usually meaningful. For symbolic links
1962this specifies the length of the file name the link refers to.
1963
1964@item time_t st_atime
1965This is the last access time for the file. @xref{File Times}.
1966
1967@item unsigned long int st_atime_usec
1968This is the fractional part of the last access time for the file.
1969@xref{File Times}.
1970
1971@item time_t st_mtime
1972This is the time of the last modification to the contents of the file.
1973@xref{File Times}.
1974
1975@item unsigned long int st_mtime_usec
1976This is the fractional part of the time of the last modification to the
1977contents of the file. @xref{File Times}.
1978
1979@item time_t st_ctime
1980This is the time of the last modification to the attributes of the file.
1981@xref{File Times}.
1982
1983@item unsigned long int st_ctime_usec
1984This is the fractional part of the time of the last modification to the
1985attributes of the file. @xref{File Times}.
1986
1987@c !!! st_rdev
1988@item blkcnt_t st_blocks
1989This is the amount of disk space that the file occupies, measured in
1990units of 512-byte blocks.
1991
1992The number of disk blocks is not strictly proportional to the size of
1993the file, for two reasons: the file system may use some blocks for
1994internal record keeping; and the file may be sparse---it may have
1995``holes'' which contain zeros but do not actually take up space on the
1996disk.
1997
1998You can tell (approximately) whether a file is sparse by comparing this
1999value with @code{st_size}, like this:
2000
2001@smallexample
2002(st.st_blocks * 512 < st.st_size)
2003@end smallexample
2004
2005This test is not perfect because a file that is just slightly sparse
2006might not be detected as sparse at all. For practical applications,
2007this is not a problem.
2008
2009@item unsigned int st_blksize
2010The optimal block size for reading or writing this file, in bytes. You
2011might use this size for allocating the buffer space for reading or
2012writing the file. (This is unrelated to @code{st_blocks}.)
2013@end table
2014@end deftp
2015
2016The extensions for the Large File Support (LFS) require, even on 32-bit
2017machines, types which can handle file sizes up to @twoexp{63}.
2018Therefore a new definition of @code{struct stat} is necessary.
2019
2020@deftp {Data Type} {struct stat64}
2021@standards{LFS, sys/stat.h}
2022The members of this type are the same and have the same names as those
2023in @code{struct stat}. The only difference is that the members
2024@code{st_ino}, @code{st_size}, and @code{st_blocks} have a different
2025type to support larger values.
2026
2027@table @code
2028@item mode_t st_mode
2029Specifies the mode of the file. This includes file type information
2030(@pxref{Testing File Type}) and the file permission bits
2031(@pxref{Permission Bits}).
2032
2033@item ino64_t st_ino
2034The file serial number, which distinguishes this file from all other
2035files on the same device.
2036
2037@item dev_t st_dev
2038Identifies the device containing the file. The @code{st_ino} and
2039@code{st_dev}, taken together, uniquely identify the file. The
2040@code{st_dev} value is not necessarily consistent across reboots or
2041system crashes, however.
2042
2043@item nlink_t st_nlink
2044The number of hard links to the file. This count keeps track of how
2045many directories have entries for this file. If the count is ever
2046decremented to zero, then the file itself is discarded as soon as no
2047process still holds it open. Symbolic links are not counted in the
2048total.
2049
2050@item uid_t st_uid
2051The user ID of the file's owner. @xref{File Owner}.
2052
2053@item gid_t st_gid
2054The group ID of the file. @xref{File Owner}.
2055
2056@item off64_t st_size
2057This specifies the size of a regular file in bytes. For files that are
2058really devices this field isn't usually meaningful. For symbolic links
2059this specifies the length of the file name the link refers to.
2060
2061@item time_t st_atime
2062This is the last access time for the file. @xref{File Times}.
2063
2064@item unsigned long int st_atime_usec
2065This is the fractional part of the last access time for the file.
2066@xref{File Times}.
2067
2068@item time_t st_mtime
2069This is the time of the last modification to the contents of the file.
2070@xref{File Times}.
2071
2072@item unsigned long int st_mtime_usec
2073This is the fractional part of the time of the last modification to the
2074contents of the file. @xref{File Times}.
2075
2076@item time_t st_ctime
2077This is the time of the last modification to the attributes of the file.
2078@xref{File Times}.
2079
2080@item unsigned long int st_ctime_usec
2081This is the fractional part of the time of the last modification to the
2082attributes of the file. @xref{File Times}.
2083
2084@c !!! st_rdev
2085@item blkcnt64_t st_blocks
2086This is the amount of disk space that the file occupies, measured in
2087units of 512-byte blocks.
2088
2089@item unsigned int st_blksize
2090The optimal block size for reading of writing this file, in bytes. You
2091might use this size for allocating the buffer space for reading of
2092writing the file. (This is unrelated to @code{st_blocks}.)
2093@end table
2094@end deftp
2095
2096Some of the file attributes have special data type names which exist
2097specifically for those attributes. (They are all aliases for well-known
2098integer types that you know and love.) These typedef names are defined
2099in the header file @file{sys/types.h} as well as in @file{sys/stat.h}.
2100Here is a list of them.
2101
2102@deftp {Data Type} mode_t
2103@standards{POSIX.1, sys/types.h}
2104This is an integer data type used to represent file modes. In
2105@theglibc{}, this is an unsigned type no narrower than @code{unsigned
2106int}.
2107@end deftp
2108
2109@cindex inode number
2110@deftp {Data Type} ino_t
2111@standards{POSIX.1, sys/types.h}
2112This is an unsigned integer type used to represent file serial numbers.
2113(In Unix jargon, these are sometimes called @dfn{inode numbers}.)
2114In @theglibc{}, this type is no narrower than @code{unsigned int}.
2115
2116If the source is compiled with @code{_FILE_OFFSET_BITS == 64} this type
2117is transparently replaced by @code{ino64_t}.
2118@end deftp
2119
2120@deftp {Data Type} ino64_t
2121@standards{Unix98, sys/types.h}
2122This is an unsigned integer type used to represent file serial numbers
2123for the use in LFS. In @theglibc{}, this type is no narrower than
2124@code{unsigned int}.
2125
2126When compiling with @code{_FILE_OFFSET_BITS == 64} this type is
2127available under the name @code{ino_t}.
2128@end deftp
2129
2130@deftp {Data Type} dev_t
2131@standards{POSIX.1, sys/types.h}
2132This is an arithmetic data type used to represent file device numbers.
2133In @theglibc{}, this is an integer type no narrower than @code{int}.
2134@end deftp
2135
2136@deftp {Data Type} nlink_t
2137@standards{POSIX.1, sys/types.h}
2138This is an integer type used to represent file link counts.
2139@end deftp
2140
2141@deftp {Data Type} blkcnt_t
2142@standards{Unix98, sys/types.h}
2143This is a signed integer type used to represent block counts.
2144In @theglibc{}, this type is no narrower than @code{int}.
2145
2146If the source is compiled with @code{_FILE_OFFSET_BITS == 64} this type
2147is transparently replaced by @code{blkcnt64_t}.
2148@end deftp
2149
2150@deftp {Data Type} blkcnt64_t
2151@standards{Unix98, sys/types.h}
2152This is a signed integer type used to represent block counts for the
2153use in LFS. In @theglibc{}, this type is no narrower than @code{int}.
2154
2155When compiling with @code{_FILE_OFFSET_BITS == 64} this type is
2156available under the name @code{blkcnt_t}.
2157@end deftp
2158
2159@node Reading Attributes
2160@subsection Reading the Attributes of a File
2161
2162To examine the attributes of files, use the functions @code{stat},
2163@code{fstat} and @code{lstat}. They return the attribute information in
2164a @code{struct stat} object. All three functions are declared in the
2165header file @file{sys/stat.h}.
2166
2167@deftypefun int stat (const char *@var{filename}, struct stat *@var{buf})
2168@standards{POSIX.1, sys/stat.h}
2169@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2170The @code{stat} function returns information about the attributes of the
2171file named by @w{@var{filename}} in the structure pointed to by @var{buf}.
2172
2173If @var{filename} is the name of a symbolic link, the attributes you get
2174describe the file that the link points to. If the link points to a
2175nonexistent file name, then @code{stat} fails reporting a nonexistent
2176file.
2177
2178The return value is @code{0} if the operation is successful, or
2179@code{-1} on failure. In addition to the usual file name errors
2180(@pxref{File Name Errors}, the following @code{errno} error conditions
2181are defined for this function:
2182
2183@table @code
2184@item ENOENT
2185The file named by @var{filename} doesn't exist.
2186@end table
2187
2188When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2189function is in fact @code{stat64} since the LFS interface transparently
2190replaces the normal implementation.
2191@end deftypefun
2192
2193@deftypefun int stat64 (const char *@var{filename}, struct stat64 *@var{buf})
2194@standards{Unix98, sys/stat.h}
2195@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2196This function is similar to @code{stat} but it is also able to work on
2197files larger than @twoexp{31} bytes on 32-bit systems. To be able to do
2198this the result is stored in a variable of type @code{struct stat64} to
2199which @var{buf} must point.
2200
2201When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2202function is available under the name @code{stat} and so transparently
2203replaces the interface for small files on 32-bit machines.
2204@end deftypefun
2205
2206@deftypefun int fstat (int @var{filedes}, struct stat *@var{buf})
2207@standards{POSIX.1, sys/stat.h}
2208@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2209The @code{fstat} function is like @code{stat}, except that it takes an
2210open file descriptor as an argument instead of a file name.
2211@xref{Low-Level I/O}.
2212
2213Like @code{stat}, @code{fstat} returns @code{0} on success and @code{-1}
2214on failure. The following @code{errno} error conditions are defined for
2215@code{fstat}:
2216
2217@table @code
2218@item EBADF
2219The @var{filedes} argument is not a valid file descriptor.
2220@end table
2221
2222When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2223function is in fact @code{fstat64} since the LFS interface transparently
2224replaces the normal implementation.
2225@end deftypefun
2226
2227@deftypefun int fstat64 (int @var{filedes}, struct stat64 *@var{buf})
2228@standards{Unix98, sys/stat.h}
2229@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2230This function is similar to @code{fstat} but is able to work on large
2231files on 32-bit platforms. For large files the file descriptor
2232@var{filedes} should be obtained by @code{open64} or @code{creat64}.
2233The @var{buf} pointer points to a variable of type @code{struct stat64}
2234which is able to represent the larger values.
2235
2236When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2237function is available under the name @code{fstat} and so transparently
2238replaces the interface for small files on 32-bit machines.
2239@end deftypefun
2240
2241@deftypefun int fstatat (int @var{filedes}, const char *@var{filename}, struct stat *@var{buf}, int @var{flags})
2242@standards{POSIX.1, sys/stat.h}
2243@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2244This function is a descriptor-relative version of the @code{fstat}
2245function above. @xref{Descriptor-Relative Access}. The @var{flags}
2246argument can contain a combination of the flags @code{AT_EMPTY_PATH},
2247@code{AT_NO_AUTOMOUNT}, @code{AT_SYMLINK_NOFOLLOW}.
2248
2249Compared to @code{fstat}, the following additional error conditions can
2250occur:
2251
2252@table @code
2253@item EBADF
2254The @var{filedes} argument is not a valid file descriptor.
2255
2256@item EINVAL
2257The @var{flags} argument is not valid for this function.
2258
2259@item ENOTDIR
2260The descriptor @var{filedes} is not associated with a directory, and
2261@var{filename} is a relative file name.
2262@end table
2263
2264When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2265function is in fact @code{fstatat64} since the LFS interface transparently
2266replaces the normal implementation.
2267@end deftypefun
2268
2269@deftypefun int fstatat64 (int @var{filedes}, const char *@var{filename}, struct stat64 *@var{buf}, int @var{flags})
2270@standards{GNU, sys/stat.h}
2271@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2272This function is the large-file variant of @code{fstatat}, similar to
2273how @code{fstat64} is the variant of @code{fstat}.
2274
2275When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2276function is available under the name @code{fstatat} and so transparently
2277replaces the interface for small files on 32-bit machines.
2278@end deftypefun
2279
2280@deftypefun int lstat (const char *@var{filename}, struct stat *@var{buf})
2281@standards{BSD, sys/stat.h}
2282@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2283@c Direct system call through lxstat, sometimes with an xstat conv call
2284@c afterwards.
2285The @code{lstat} function is like @code{stat}, except that it does not
2286follow symbolic links. If @var{filename} is the name of a symbolic
2287link, @code{lstat} returns information about the link itself; otherwise
2288@code{lstat} works like @code{stat}. @xref{Symbolic Links}.
2289
2290When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2291function is in fact @code{lstat64} since the LFS interface transparently
2292replaces the normal implementation.
2293@end deftypefun
2294
2295@deftypefun int lstat64 (const char *@var{filename}, struct stat64 *@var{buf})
2296@standards{Unix98, sys/stat.h}
2297@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2298@c Direct system call through lxstat64, sometimes with an xstat conv
2299@c call afterwards.
2300This function is similar to @code{lstat} but it is also able to work on
2301files larger than @twoexp{31} bytes on 32-bit systems. To be able to do
2302this the result is stored in a variable of type @code{struct stat64} to
2303which @var{buf} must point.
2304
2305When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} this
2306function is available under the name @code{lstat} and so transparently
2307replaces the interface for small files on 32-bit machines.
2308@end deftypefun
2309
2310@node Testing File Type
2311@subsection Testing the Type of a File
2312
2313The @dfn{file mode}, stored in the @code{st_mode} field of the file
2314attributes, contains two kinds of information: the file type code, and
2315the access permission bits. This section discusses only the type code,
2316which you can use to tell whether the file is a directory, socket,
2317symbolic link, and so on. For details about access permissions see
2318@ref{Permission Bits}.
2319
2320There are two ways you can access the file type information in a file
2321mode. Firstly, for each file type there is a @dfn{predicate macro}
2322which examines a given file mode and returns whether it is of that type
2323or not. Secondly, you can mask out the rest of the file mode to leave
2324just the file type code, and compare this against constants for each of
2325the supported file types.
2326
2327All of the symbols listed in this section are defined in the header file
2328@file{sys/stat.h}.
2329@pindex sys/stat.h
2330
2331The following predicate macros test the type of a file, given the value
2332@var{m} which is the @code{st_mode} field returned by @code{stat} on
2333that file:
2334
2335@deftypefn Macro int S_ISDIR (mode_t @var{m})
2336@standards{POSIX, sys/stat.h}
2337@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2338This macro returns non-zero if the file is a directory.
2339@end deftypefn
2340
2341@deftypefn Macro int S_ISCHR (mode_t @var{m})
2342@standards{POSIX, sys/stat.h}
2343@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2344This macro returns non-zero if the file is a character special file (a
2345device like a terminal).
2346@end deftypefn
2347
2348@deftypefn Macro int S_ISBLK (mode_t @var{m})
2349@standards{POSIX, sys/stat.h}
2350@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2351This macro returns non-zero if the file is a block special file (a device
2352like a disk).
2353@end deftypefn
2354
2355@deftypefn Macro int S_ISREG (mode_t @var{m})
2356@standards{POSIX, sys/stat.h}
2357@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2358This macro returns non-zero if the file is a regular file.
2359@end deftypefn
2360
2361@deftypefn Macro int S_ISFIFO (mode_t @var{m})
2362@standards{POSIX, sys/stat.h}
2363@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2364This macro returns non-zero if the file is a FIFO special file, or a
2365pipe. @xref{Pipes and FIFOs}.
2366@end deftypefn
2367
2368@deftypefn Macro int S_ISLNK (mode_t @var{m})
2369@standards{GNU, sys/stat.h}
2370@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2371This macro returns non-zero if the file is a symbolic link.
2372@xref{Symbolic Links}.
2373@end deftypefn
2374
2375@deftypefn Macro int S_ISSOCK (mode_t @var{m})
2376@standards{GNU, sys/stat.h}
2377@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2378This macro returns non-zero if the file is a socket. @xref{Sockets}.
2379@end deftypefn
2380
2381An alternate non-POSIX method of testing the file type is supported for
2382compatibility with BSD. The mode can be bitwise AND-ed with
2383@code{S_IFMT} to extract the file type code, and compared to the
2384appropriate constant. For example,
2385
2386@smallexample
2387S_ISCHR (@var{mode})
2388@end smallexample
2389
2390@noindent
2391is equivalent to:
2392
2393@smallexample
2394((@var{mode} & S_IFMT) == S_IFCHR)
2395@end smallexample
2396
2397@deftypevr Macro int S_IFMT
2398@standards{BSD, sys/stat.h}
2399This is a bit mask used to extract the file type code from a mode value.
2400@end deftypevr
2401
2402These are the symbolic names for the different file type codes:
2403
2404@vtable @code
2405@item S_IFDIR
2406@standards{BSD, sys/stat.h}
2407This is the file type constant of a directory file.
2408
2409@item S_IFCHR
2410@standards{BSD, sys/stat.h}
2411This is the file type constant of a character-oriented device file.
2412
2413@item S_IFBLK
2414@standards{BSD, sys/stat.h}
2415This is the file type constant of a block-oriented device file.
2416
2417@item S_IFREG
2418@standards{BSD, sys/stat.h}
2419This is the file type constant of a regular file.
2420
2421@item S_IFLNK
2422@standards{BSD, sys/stat.h}
2423This is the file type constant of a symbolic link.
2424
2425@item S_IFSOCK
2426@standards{BSD, sys/stat.h}
2427This is the file type constant of a socket.
2428
2429@item S_IFIFO
2430@standards{BSD, sys/stat.h}
2431This is the file type constant of a FIFO or pipe.
2432@end vtable
2433
2434The POSIX.1b standard introduced a few more objects which possibly can
2435be implemented as objects in the filesystem. These are message queues,
2436semaphores, and shared memory objects. To allow differentiating these
2437objects from other files the POSIX standard introduced three new test
2438macros. But unlike the other macros they do not take the value of the
2439@code{st_mode} field as the parameter. Instead they expect a pointer to
2440the whole @code{struct stat} structure.
2441
2442@deftypefn Macro int S_TYPEISMQ (struct stat *@var{s})
2443@standards{POSIX, sys/stat.h}
2444@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2445If the system implements POSIX message queues as distinct objects and the
2446file is a message queue object, this macro returns a non-zero value.
2447In all other cases the result is zero.
2448@end deftypefn
2449
2450@deftypefn Macro int S_TYPEISSEM (struct stat *@var{s})
2451@standards{POSIX, sys/stat.h}
2452@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2453If the system implements POSIX semaphores as distinct objects and the
2454file is a semaphore object, this macro returns a non-zero value.
2455In all other cases the result is zero.
2456@end deftypefn
2457
2458@deftypefn Macro int S_TYPEISSHM (struct stat *@var{s})
2459@standards{POSIX, sys/stat.h}
2460@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2461If the system implements POSIX shared memory objects as distinct objects
2462and the file is a shared memory object, this macro returns a non-zero
2463value. In all other cases the result is zero.
2464@end deftypefn
2465
2466@node File Owner
2467@subsection File Owner
2468@cindex file owner
2469@cindex owner of a file
2470@cindex group owner of a file
2471
2472Every file has an @dfn{owner} which is one of the registered user names
2473defined on the system. Each file also has a @dfn{group} which is one of
2474the defined groups. The file owner can often be useful for showing you
2475who edited the file (especially when you edit with GNU Emacs), but its
2476main purpose is for access control.
2477
2478The file owner and group play a role in determining access because the
2479file has one set of access permission bits for the owner, another set
2480that applies to users who belong to the file's group, and a third set of
2481bits that applies to everyone else. @xref{Access Permission}, for the
2482details of how access is decided based on this data.
2483
2484When a file is created, its owner is set to the effective user ID of the
2485process that creates it (@pxref{Process Persona}). The file's group ID
2486may be set to either the effective group ID of the process, or the group
2487ID of the directory that contains the file, depending on the system
2488where the file is stored. When you access a remote file system, it
2489behaves according to its own rules, not according to the system your
2490program is running on. Thus, your program must be prepared to encounter
2491either kind of behavior no matter what kind of system you run it on.
2492
2493@pindex chown
2494@pindex chgrp
2495You can change the owner and/or group owner of an existing file using
2496the @code{chown} function. This is the primitive for the @code{chown}
2497and @code{chgrp} shell commands.
2498
2499@pindex unistd.h
2500The prototype for this function is declared in @file{unistd.h}.
2501
2502@deftypefun int chown (const char *@var{filename}, uid_t @var{owner}, gid_t @var{group})
2503@standards{POSIX.1, unistd.h}
2504@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2505The @code{chown} function changes the owner of the file @var{filename} to
2506@var{owner}, and its group owner to @var{group}.
2507
2508Changing the owner of the file on certain systems clears the set-user-ID
2509and set-group-ID permission bits. (This is because those bits may not
2510be appropriate for the new owner.) Other file permission bits are not
2511changed.
2512
2513The return value is @code{0} on success and @code{-1} on failure.
2514In addition to the usual file name errors (@pxref{File Name Errors}),
2515the following @code{errno} error conditions are defined for this function:
2516
2517@table @code
2518@item EPERM
2519This process lacks permission to make the requested change.
2520
2521Only privileged users or the file's owner can change the file's group.
2522On most file systems, only privileged users can change the file owner;
2523some file systems allow you to change the owner if you are currently the
2524owner. When you access a remote file system, the behavior you encounter
2525is determined by the system that actually holds the file, not by the
2526system your program is running on.
2527
2528@xref{Options for Files}, for information about the
2529@code{_POSIX_CHOWN_RESTRICTED} macro.
2530
2531@item EROFS
2532The file is on a read-only file system.
2533@end table
2534@end deftypefun
2535
2536@deftypefun int fchown (int @var{filedes}, uid_t @var{owner}, gid_t @var{group})
2537@standards{BSD, unistd.h}
2538@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2539This is like @code{chown}, except that it changes the owner of the open
2540file with descriptor @var{filedes}.
2541
2542The return value from @code{fchown} is @code{0} on success and @code{-1}
2543on failure. The following @code{errno} error codes are defined for this
2544function:
2545
2546@table @code
2547@item EBADF
2548The @var{filedes} argument is not a valid file descriptor.
2549
2550@item EINVAL
2551The @var{filedes} argument corresponds to a pipe or socket, not an ordinary
2552file.
2553
2554@item EPERM
2555This process lacks permission to make the requested change. For details
2556see @code{chmod} above.
2557
2558@item EROFS
2559The file resides on a read-only file system.
2560@end table
2561@end deftypefun
2562
2563@node Permission Bits
2564@subsection The Mode Bits for Access Permission
2565
2566The @dfn{file mode}, stored in the @code{st_mode} field of the file
2567attributes, contains two kinds of information: the file type code, and
2568the access permission bits. This section discusses only the access
2569permission bits, which control who can read or write the file.
2570@xref{Testing File Type}, for information about the file type code.
2571
2572All of the symbols listed in this section are defined in the header file
2573@file{sys/stat.h}.
2574@pindex sys/stat.h
2575
2576@cindex file permission bits
2577These symbolic constants are defined for the file mode bits that control
2578access permission for the file:
2579
2580@vtable @code
2581@item S_IRUSR
2582@itemx S_IREAD
2583@standards{POSIX.1, sys/stat.h}
2584@standardsx{S_IREAD, BSD, sys/stat.h}
2585Read permission bit for the owner of the file. On many systems this bit
2586is 0400. @code{S_IREAD} is an obsolete synonym provided for BSD
2587compatibility.
2588
2589@item S_IWUSR
2590@itemx S_IWRITE
2591@standards{POSIX.1, sys/stat.h}
2592@standardsx{S_IWRITE, BSD, sys/stat.h}
2593Write permission bit for the owner of the file. Usually 0200.
2594@w{@code{S_IWRITE}} is an obsolete synonym provided for BSD compatibility.
2595
2596@item S_IXUSR
2597@itemx S_IEXEC
2598@standards{POSIX.1, sys/stat.h}
2599@standardsx{S_IEXEC, BSD, sys/stat.h}
2600Execute (for ordinary files) or search (for directories) permission bit
2601for the owner of the file. Usually 0100. @code{S_IEXEC} is an obsolete
2602synonym provided for BSD compatibility.
2603
2604@item S_IRWXU
2605@standards{POSIX.1, sys/stat.h}
2606This is equivalent to @samp{(S_IRUSR | S_IWUSR | S_IXUSR)}.
2607
2608@item S_IRGRP
2609@standards{POSIX.1, sys/stat.h}
2610Read permission bit for the group owner of the file. Usually 040.
2611
2612@item S_IWGRP
2613@standards{POSIX.1, sys/stat.h}
2614Write permission bit for the group owner of the file. Usually 020.
2615
2616@item S_IXGRP
2617@standards{POSIX.1, sys/stat.h}
2618Execute or search permission bit for the group owner of the file.
2619Usually 010.
2620
2621@item S_IRWXG
2622@standards{POSIX.1, sys/stat.h}
2623This is equivalent to @samp{(S_IRGRP | S_IWGRP | S_IXGRP)}.
2624
2625@item S_IROTH
2626@standards{POSIX.1, sys/stat.h}
2627Read permission bit for other users. Usually 04.
2628
2629@item S_IWOTH
2630@standards{POSIX.1, sys/stat.h}
2631Write permission bit for other users. Usually 02.
2632
2633@item S_IXOTH
2634@standards{POSIX.1, sys/stat.h}
2635Execute or search permission bit for other users. Usually 01.
2636
2637@item S_IRWXO
2638@standards{POSIX.1, sys/stat.h}
2639This is equivalent to @samp{(S_IROTH | S_IWOTH | S_IXOTH)}.
2640
2641@item S_ISUID
2642@standards{POSIX, sys/stat.h}
2643This is the set-user-ID on execute bit, usually 04000.
2644@xref{How Change Persona}.
2645
2646@item S_ISGID
2647@standards{POSIX, sys/stat.h}
2648This is the set-group-ID on execute bit, usually 02000.
2649@xref{How Change Persona}.
2650
2651@cindex sticky bit
2652@item S_ISVTX
2653@standards{BSD, sys/stat.h}
2654This is the @dfn{sticky} bit, usually 01000.
2655
2656For a directory it gives permission to delete a file in that directory
2657only if you own that file. Ordinarily, a user can either delete all the
2658files in a directory or cannot delete any of them (based on whether the
2659user has write permission for the directory). The same restriction
2660applies---you must have both write permission for the directory and own
2661the file you want to delete. The one exception is that the owner of the
2662directory can delete any file in the directory, no matter who owns it
2663(provided the owner has given himself write permission for the
2664directory). This is commonly used for the @file{/tmp} directory, where
2665anyone may create files but not delete files created by other users.
2666
2667Originally the sticky bit on an executable file modified the swapping
2668policies of the system. Normally, when a program terminated, its pages
2669in core were immediately freed and reused. If the sticky bit was set on
2670the executable file, the system kept the pages in core for a while as if
2671the program were still running. This was advantageous for a program
2672likely to be run many times in succession. This usage is obsolete in
2673modern systems. When a program terminates, its pages always remain in
2674core as long as there is no shortage of memory in the system. When the
2675program is next run, its pages will still be in core if no shortage
2676arose since the last run.
2677
2678On some modern systems where the sticky bit has no useful meaning for an
2679executable file, you cannot set the bit at all for a non-directory.
2680If you try, @code{chmod} fails with @code{EFTYPE};
2681@pxref{Setting Permissions}.
2682
2683Some systems (particularly SunOS) have yet another use for the sticky
2684bit. If the sticky bit is set on a file that is @emph{not} executable,
2685it means the opposite: never cache the pages of this file at all. The
2686main use of this is for the files on an NFS server machine which are
2687used as the swap area of diskless client machines. The idea is that the
2688pages of the file will be cached in the client's memory, so it is a
2689waste of the server's memory to cache them a second time. With this
2690usage the sticky bit also implies that the filesystem may fail to record
2691the file's modification time onto disk reliably (the idea being that
2692no-one cares for a swap file).
2693
2694This bit is only available on BSD systems (and those derived from
2695them). Therefore one has to use the @code{_GNU_SOURCE} feature select
2696macro, or not define any feature test macros, to get the definition
2697(@pxref{Feature Test Macros}).
2698@end vtable
2699
2700The actual bit values of the symbols are listed in the table above
2701so you can decode file mode values when debugging your programs.
2702These bit values are correct for most systems, but they are not
2703guaranteed.
2704
2705@strong{Warning:} Writing explicit numbers for file permissions is bad
2706practice. Not only is it not portable, it also requires everyone who
2707reads your program to remember what the bits mean. To make your program
2708clean use the symbolic names.
2709
2710@node Access Permission
2711@subsection How Your Access to a File is Decided
2712@cindex permission to access a file
2713@cindex access permission for a file
2714@cindex file access permission
2715
2716Recall that the operating system normally decides access permission for
2717a file based on the effective user and group IDs of the process and its
2718supplementary group IDs, together with the file's owner, group and
2719permission bits. These concepts are discussed in detail in @ref{Process
2720Persona}.
2721
2722If the effective user ID of the process matches the owner user ID of the
2723file, then permissions for read, write, and execute/search are
2724controlled by the corresponding ``user'' (or ``owner'') bits. Likewise,
2725if any of the effective group ID or supplementary group IDs of the
2726process matches the group owner ID of the file, then permissions are
2727controlled by the ``group'' bits. Otherwise, permissions are controlled
2728by the ``other'' bits.
2729
2730Privileged users, like @samp{root}, can access any file regardless of
2731its permission bits. As a special case, for a file to be executable
2732even by a privileged user, at least one of its execute bits must be set.
2733
2734@node Setting Permissions
2735@subsection Assigning File Permissions
2736
2737@cindex file creation mask
2738@cindex umask
2739The primitive functions for creating files (for example, @code{open} or
2740@code{mkdir}) take a @var{mode} argument, which specifies the file
2741permissions to give the newly created file. This mode is modified by
2742the process's @dfn{file creation mask}, or @dfn{umask}, before it is
2743used.
2744
2745The bits that are set in the file creation mask identify permissions
2746that are always to be disabled for newly created files. For example, if
2747you set all the ``other'' access bits in the mask, then newly created
2748files are not accessible at all to processes in the ``other'' category,
2749even if the @var{mode} argument passed to the create function would
2750permit such access. In other words, the file creation mask is the
2751complement of the ordinary access permissions you want to grant.
2752
2753Programs that create files typically specify a @var{mode} argument that
2754includes all the permissions that make sense for the particular file.
2755For an ordinary file, this is typically read and write permission for
2756all classes of users. These permissions are then restricted as
2757specified by the individual user's own file creation mask.
2758
2759@findex chmod
2760To change the permission of an existing file given its name, call
2761@code{chmod}. This function uses the specified permission bits and
2762ignores the file creation mask.
2763
2764@pindex umask
2765In normal use, the file creation mask is initialized by the user's login
2766shell (using the @code{umask} shell command), and inherited by all
2767subprocesses. Application programs normally don't need to worry about
2768the file creation mask. It will automatically do what it is supposed to
2769do.
2770
2771When your program needs to create a file and bypass the umask for its
2772access permissions, the easiest way to do this is to use @code{fchmod}
2773after opening the file, rather than changing the umask. In fact,
2774changing the umask is usually done only by shells. They use the
2775@code{umask} function.
2776
2777The functions in this section are declared in @file{sys/stat.h}.
2778@pindex sys/stat.h
2779
2780@deftypefun mode_t umask (mode_t @var{mask})
2781@standards{POSIX.1, sys/stat.h}
2782@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2783The @code{umask} function sets the file creation mask of the current
2784process to @var{mask}, and returns the previous value of the file
2785creation mask.
2786
2787Here is an example showing how to read the mask with @code{umask}
2788without changing it permanently:
2789
2790@smallexample
2791mode_t
2792read_umask (void)
2793@{
2794 mode_t mask = umask (0);
2795 umask (mask);
2796 return mask;
2797@}
2798@end smallexample
2799
2800@noindent
2801However, on @gnuhurdsystems{} it is better to use @code{getumask} if
2802you just want to read the mask value, because it is reentrant.
2803@end deftypefun
2804
2805@deftypefun mode_t getumask (void)
2806@standards{GNU, sys/stat.h}
2807@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2808Return the current value of the file creation mask for the current
2809process. This function is a GNU extension and is only available on
2810@gnuhurdsystems{}.
2811@end deftypefun
2812
2813@deftypefun int chmod (const char *@var{filename}, mode_t @var{mode})
2814@standards{POSIX.1, sys/stat.h}
2815@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2816The @code{chmod} function sets the access permission bits for the file
2817named by @var{filename} to @var{mode}.
2818
2819If @var{filename} is a symbolic link, @code{chmod} changes the
2820permissions of the file pointed to by the link, not those of the link
2821itself.
2822
2823This function returns @code{0} if successful and @code{-1} if not. In
2824addition to the usual file name errors (@pxref{File Name
2825Errors}), the following @code{errno} error conditions are defined for
2826this function:
2827
2828@table @code
2829@item ENOENT
2830The named file doesn't exist.
2831
2832@item EPERM
2833This process does not have permission to change the access permissions
2834of this file. Only the file's owner (as judged by the effective user ID
2835of the process) or a privileged user can change them.
2836
2837@item EROFS
2838The file resides on a read-only file system.
2839
2840@item EFTYPE
2841@var{mode} has the @code{S_ISVTX} bit (the ``sticky bit'') set,
2842and the named file is not a directory. Some systems do not allow setting the
2843sticky bit on non-directory files, and some do (and only some of those
2844assign a useful meaning to the bit for non-directory files).
2845
2846You only get @code{EFTYPE} on systems where the sticky bit has no useful
2847meaning for non-directory files, so it is always safe to just clear the
2848bit in @var{mode} and call @code{chmod} again. @xref{Permission Bits},
2849for full details on the sticky bit.
2850@end table
2851@end deftypefun
2852
2853@deftypefun int fchmod (int @var{filedes}, mode_t @var{mode})
2854@standards{BSD, sys/stat.h}
2855@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2856This is like @code{chmod}, except that it changes the permissions of the
2857currently open file given by @var{filedes}.
2858
2859The return value from @code{fchmod} is @code{0} on success and @code{-1}
2860on failure. The following @code{errno} error codes are defined for this
2861function:
2862
2863@table @code
2864@item EBADF
2865The @var{filedes} argument is not a valid file descriptor.
2866
2867@item EINVAL
2868The @var{filedes} argument corresponds to a pipe or socket, or something
2869else that doesn't really have access permissions.
2870
2871@item EPERM
2872This process does not have permission to change the access permissions
2873of this file. Only the file's owner (as judged by the effective user ID
2874of the process) or a privileged user can change them.
2875
2876@item EROFS
2877The file resides on a read-only file system.
2878@end table
2879@end deftypefun
2880
2881@node Testing File Access
2882@subsection Testing Permission to Access a File
2883@cindex testing access permission
2884@cindex access, testing for
2885@cindex setuid programs and file access
2886
2887In some situations it is desirable to allow programs to access files or
2888devices even if this is not possible with the permissions granted to the
2889user. One possible solution is to set the setuid-bit of the program
2890file. If such a program is started the @emph{effective} user ID of the
2891process is changed to that of the owner of the program file. So to
2892allow write access to files like @file{/etc/passwd}, which normally can
2893be written only by the super-user, the modifying program will have to be
2894owned by @code{root} and the setuid-bit must be set.
2895
2896But besides the files the program is intended to change the user should
2897not be allowed to access any file to which s/he would not have access
2898anyway. The program therefore must explicitly check whether @emph{the
2899user} would have the necessary access to a file, before it reads or
2900writes the file.
2901
2902To do this, use the function @code{access}, which checks for access
2903permission based on the process's @emph{real} user ID rather than the
2904effective user ID. (The setuid feature does not alter the real user ID,
2905so it reflects the user who actually ran the program.)
2906
2907There is another way you could check this access, which is easy to
2908describe, but very hard to use. This is to examine the file mode bits
2909and mimic the system's own access computation. This method is
2910undesirable because many systems have additional access control
2911features; your program cannot portably mimic them, and you would not
2912want to try to keep track of the diverse features that different systems
2913have. Using @code{access} is simple and automatically does whatever is
2914appropriate for the system you are using.
2915
2916@code{access} is @emph{only} appropriate to use in setuid programs.
2917A non-setuid program will always use the effective ID rather than the
2918real ID.
2919
2920@pindex unistd.h
2921The symbols in this section are declared in @file{unistd.h}.
2922
2923@deftypefun int access (const char *@var{filename}, int @var{how})
2924@standards{POSIX.1, unistd.h}
2925@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2926The @code{access} function checks to see whether the file named by
2927@var{filename} can be accessed in the way specified by the @var{how}
2928argument. The @var{how} argument either can be the bitwise OR of the
2929flags @code{R_OK}, @code{W_OK}, @code{X_OK}, or the existence test
2930@code{F_OK}.
2931
2932This function uses the @emph{real} user and group IDs of the calling
2933process, rather than the @emph{effective} IDs, to check for access
2934permission. As a result, if you use the function from a @code{setuid}
2935or @code{setgid} program (@pxref{How Change Persona}), it gives
2936information relative to the user who actually ran the program.
2937
2938The return value is @code{0} if the access is permitted, and @code{-1}
2939otherwise. (In other words, treated as a predicate function,
2940@code{access} returns true if the requested access is @emph{denied}.)
2941
2942In addition to the usual file name errors (@pxref{File Name
2943Errors}), the following @code{errno} error conditions are defined for
2944this function:
2945
2946@table @code
2947@item EACCES
2948The access specified by @var{how} is denied.
2949
2950@item ENOENT
2951The file doesn't exist.
2952
2953@item EROFS
2954Write permission was requested for a file on a read-only file system.
2955@end table
2956@end deftypefun
2957
2958These macros are defined in the header file @file{unistd.h} for use
2959as the @var{how} argument to the @code{access} function. The values
2960are integer constants.
2961@pindex unistd.h
2962
2963@deftypevr Macro int R_OK
2964@standards{POSIX.1, unistd.h}
2965Flag meaning test for read permission.
2966@end deftypevr
2967
2968@deftypevr Macro int W_OK
2969@standards{POSIX.1, unistd.h}
2970Flag meaning test for write permission.
2971@end deftypevr
2972
2973@deftypevr Macro int X_OK
2974@standards{POSIX.1, unistd.h}
2975Flag meaning test for execute/search permission.
2976@end deftypevr
2977
2978@deftypevr Macro int F_OK
2979@standards{POSIX.1, unistd.h}
2980Flag meaning test for existence of the file.
2981@end deftypevr
2982
2983@node File Times
2984@subsection File Times
2985
2986@cindex file access time
2987@cindex file modification time
2988@cindex file attribute modification time
2989Each file has three time stamps associated with it: its access time,
2990its modification time, and its attribute modification time. These
2991correspond to the @code{st_atime}, @code{st_mtime}, and @code{st_ctime}
2992members of the @code{stat} structure; see @ref{File Attributes}.
2993
2994All of these times are represented in calendar time format, as
2995@code{time_t} objects. This data type is defined in @file{time.h}.
2996For more information about representation and manipulation of time
2997values, see @ref{Calendar Time}.
2998@pindex time.h
2999
3000Reading from a file updates its access time attribute, and writing
3001updates its modification time. When a file is created, all three
3002time stamps for that file are set to the current time. In addition, the
3003attribute change time and modification time fields of the directory that
3004contains the new entry are updated.
3005
3006Adding a new name for a file with the @code{link} function updates the
3007attribute change time field of the file being linked, and both the
3008attribute change time and modification time fields of the directory
3009containing the new name. These same fields are affected if a file name
3010is deleted with @code{unlink}, @code{remove} or @code{rmdir}. Renaming
3011a file with @code{rename} affects only the attribute change time and
3012modification time fields of the two parent directories involved, and not
3013the times for the file being renamed.
3014
3015Changing the attributes of a file (for example, with @code{chmod})
3016updates its attribute change time field.
3017
3018You can also change some of the time stamps of a file explicitly using
3019the @code{utime} function---all except the attribute change time. You
3020need to include the header file @file{utime.h} to use this facility.
3021@pindex utime.h
3022
3023@deftp {Data Type} {struct utimbuf}
3024@standards{POSIX.1, utime.h}
3025The @code{utimbuf} structure is used with the @code{utime} function to
3026specify new access and modification times for a file. It contains the
3027following members:
3028
3029@table @code
3030@item time_t actime
3031This is the access time for the file.
3032
3033@item time_t modtime
3034This is the modification time for the file.
3035@end table
3036@end deftp
3037
3038@deftypefun int utime (const char *@var{filename}, const struct utimbuf *@var{times})
3039@standards{POSIX.1, utime.h}
3040@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3041@c In the absence of a utime syscall, it non-atomically converts times
3042@c to a struct timeval and calls utimes.
3043This function is used to modify the file times associated with the file
3044named @var{filename}.
3045
3046If @var{times} is a null pointer, then the access and modification times
3047of the file are set to the current time. Otherwise, they are set to the
3048values from the @code{actime} and @code{modtime} members (respectively)
3049of the @code{utimbuf} structure pointed to by @var{times}.
3050
3051The attribute modification time for the file is set to the current time
3052in either case (since changing the time stamps is itself a modification
3053of the file attributes).
3054
3055The @code{utime} function returns @code{0} if successful and @code{-1}
3056on failure. In addition to the usual file name errors
3057(@pxref{File Name Errors}), the following @code{errno} error conditions
3058are defined for this function:
3059
3060@table @code
3061@item EACCES
3062There is a permission problem in the case where a null pointer was
3063passed as the @var{times} argument. In order to update the time stamp on
3064the file, you must either be the owner of the file, have write
3065permission for the file, or be a privileged user.
3066
3067@item ENOENT
3068The file doesn't exist.
3069
3070@item EPERM
3071If the @var{times} argument is not a null pointer, you must either be
3072the owner of the file or be a privileged user.
3073
3074@item EROFS
3075The file lives on a read-only file system.
3076@end table
3077@end deftypefun
3078
3079Each of the three time stamps has a corresponding microsecond part,
3080which extends its resolution. These fields are called
3081@code{st_atime_usec}, @code{st_mtime_usec}, and @code{st_ctime_usec};
3082each has a value between 0 and 999,999, which indicates the time in
3083microseconds. They correspond to the @code{tv_usec} field of a
3084@code{timeval} structure; see @ref{Time Types}.
3085
3086The @code{utimes} function is like @code{utime}, but also lets you specify
3087the fractional part of the file times. The prototype for this function is
3088in the header file @file{sys/time.h}.
3089@pindex sys/time.h
3090
3091@deftypefun int utimes (const char *@var{filename}, const struct timeval @var{tvp}@t{[2]})
3092@standards{BSD, sys/time.h}
3093@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3094@c In the absence of a utimes syscall, it non-atomically converts tvp
3095@c to struct timespec array and issues a utimensat syscall, or to
3096@c struct utimbuf and calls utime.
3097This function sets the file access and modification times of the file
3098@var{filename}. The new file access time is specified by
3099@code{@var{tvp}[0]}, and the new modification time by
3100@code{@var{tvp}[1]}. Similar to @code{utime}, if @var{tvp} is a null
3101pointer then the access and modification times of the file are set to
3102the current time. This function comes from BSD.
3103
3104The return values and error conditions are the same as for the @code{utime}
3105function.
3106@end deftypefun
3107
3108@deftypefun int lutimes (const char *@var{filename}, const struct timeval @var{tvp}@t{[2]})
3109@standards{BSD, sys/time.h}
3110@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3111@c Since there's no lutimes syscall, it non-atomically converts tvp
3112@c to struct timespec array and issues a utimensat syscall.
3113This function is like @code{utimes}, except that it does not follow
3114symbolic links. If @var{filename} is the name of a symbolic link,
3115@code{lutimes} sets the file access and modification times of the
3116symbolic link special file itself (as seen by @code{lstat};
3117@pxref{Symbolic Links}) while @code{utimes} sets the file access and
3118modification times of the file the symbolic link refers to. This
3119function comes from FreeBSD, and is not available on all platforms (if
3120not available, it will fail with @code{ENOSYS}).
3121
3122The return values and error conditions are the same as for the @code{utime}
3123function.
3124@end deftypefun
3125
3126@deftypefun int futimes (int @var{fd}, const struct timeval @var{tvp}@t{[2]})
3127@standards{BSD, sys/time.h}
3128@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3129@c Since there's no futimes syscall, it non-atomically converts tvp
3130@c to struct timespec array and issues a utimensat syscall, falling back
3131@c to utimes on a /proc/self/fd symlink.
3132This function is like @code{utimes}, except that it takes an open file
3133descriptor as an argument instead of a file name. @xref{Low-Level
3134I/O}. This function comes from FreeBSD, and is not available on all
3135platforms (if not available, it will fail with @code{ENOSYS}).
3136
3137Like @code{utimes}, @code{futimes} returns @code{0} on success and @code{-1}
3138on failure. The following @code{errno} error conditions are defined for
3139@code{futimes}:
3140
3141@table @code
3142@item EACCES
3143There is a permission problem in the case where a null pointer was
3144passed as the @var{times} argument. In order to update the time stamp on
3145the file, you must either be the owner of the file, have write
3146permission for the file, or be a privileged user.
3147
3148@item EBADF
3149The @var{filedes} argument is not a valid file descriptor.
3150
3151@item EPERM
3152If the @var{times} argument is not a null pointer, you must either be
3153the owner of the file or be a privileged user.
3154
3155@item EROFS
3156The file lives on a read-only file system.
3157@end table
3158@end deftypefun
3159
3160@node File Size
3161@subsection File Size
3162
3163Normally file sizes are maintained automatically. A file begins with a
3164size of @math{0} and is automatically extended when data is written past
3165its end. It is also possible to empty a file completely by an
3166@code{open} or @code{fopen} call.
3167
3168However, sometimes it is necessary to @emph{reduce} the size of a file.
3169This can be done with the @code{truncate} and @code{ftruncate} functions.
3170They were introduced in BSD Unix. @code{ftruncate} was later added to
3171POSIX.1.
3172
3173Some systems allow you to extend a file (creating holes) with these
3174functions. This is useful when using memory-mapped I/O
3175(@pxref{Memory-mapped I/O}), where files are not automatically extended.
3176However, it is not portable but must be implemented if @code{mmap}
3177allows mapping of files (i.e., @code{_POSIX_MAPPED_FILES} is defined).
3178
3179Using these functions on anything other than a regular file gives
3180@emph{undefined} results. On many systems, such a call will appear to
3181succeed, without actually accomplishing anything.
3182
3183@deftypefun int truncate (const char *@var{filename}, off_t @var{length})
3184@standards{X/Open, unistd.h}
3185@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3186@c In the absence of a truncate syscall, we use open and ftruncate.
3187
3188The @code{truncate} function changes the size of @var{filename} to
3189@var{length}. If @var{length} is shorter than the previous length, data
3190at the end will be lost. The file must be writable by the user to
3191perform this operation.
3192
3193If @var{length} is longer, holes will be added to the end. However, some
3194systems do not support this feature and will leave the file unchanged.
3195
3196When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
3197@code{truncate} function is in fact @code{truncate64} and the type
3198@code{off_t} has 64 bits which makes it possible to handle files up to
3199@twoexp{63} bytes in length.
3200
3201The return value is @math{0} for success, or @math{-1} for an error. In
3202addition to the usual file name errors, the following errors may occur:
3203
3204@table @code
3205
3206@item EACCES
3207The file is a directory or not writable.
3208
3209@item EINVAL
3210@var{length} is negative.
3211
3212@item EFBIG
3213The operation would extend the file beyond the limits of the operating system.
3214
3215@item EIO
3216A hardware I/O error occurred.
3217
3218@item EPERM
3219The file is "append-only" or "immutable".
3220
3221@item EINTR
3222The operation was interrupted by a signal.
3223
3224@end table
3225
3226@end deftypefun
3227
3228@deftypefun int truncate64 (const char *@var{name}, off64_t @var{length})
3229@standards{Unix98, unistd.h}
3230@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3231@c In the absence of a syscall, try truncate if length fits.
3232This function is similar to the @code{truncate} function. The
3233difference is that the @var{length} argument is 64 bits wide even on 32
3234bits machines, which allows the handling of files with sizes up to
3235@twoexp{63} bytes.
3236
3237When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} on a
323832 bits machine this function is actually available under the name
3239@code{truncate} and so transparently replaces the 32 bits interface.
3240@end deftypefun
3241
3242@deftypefun int ftruncate (int @var{fd}, off_t @var{length})
3243@standards{POSIX, unistd.h}
3244@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3245
3246This is like @code{truncate}, but it works on a file descriptor @var{fd}
3247for an opened file instead of a file name to identify the object. The
3248file must be opened for writing to successfully carry out the operation.
3249
3250The POSIX standard leaves it implementation defined what happens if the
3251specified new @var{length} of the file is bigger than the original size.
3252The @code{ftruncate} function might simply leave the file alone and do
3253nothing or it can increase the size to the desired size. In this later
3254case the extended area should be zero-filled. So using @code{ftruncate}
3255is no reliable way to increase the file size but if it is possible it is
3256probably the fastest way. The function also operates on POSIX shared
3257memory segments if these are implemented by the system.
3258
3259@code{ftruncate} is especially useful in combination with @code{mmap}.
3260Since the mapped region must have a fixed size one cannot enlarge the
3261file by writing something beyond the last mapped page. Instead one has
3262to enlarge the file itself and then remap the file with the new size.
3263The example below shows how this works.
3264
3265When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
3266@code{ftruncate} function is in fact @code{ftruncate64} and the type
3267@code{off_t} has 64 bits which makes it possible to handle files up to
3268@twoexp{63} bytes in length.
3269
3270The return value is @math{0} for success, or @math{-1} for an error. The
3271following errors may occur:
3272
3273@table @code
3274
3275@item EBADF
3276@var{fd} does not correspond to an open file.
3277
3278@item EACCES
3279@var{fd} is a directory or not open for writing.
3280
3281@item EINVAL
3282@var{length} is negative.
3283
3284@item EFBIG
3285The operation would extend the file beyond the limits of the operating system.
3286@c or the open() call -- with the not-yet-discussed feature of opening
3287@c files with extra-large offsets.
3288
3289@item EIO
3290A hardware I/O error occurred.
3291
3292@item EPERM
3293The file is "append-only" or "immutable".
3294
3295@item EINTR
3296The operation was interrupted by a signal.
3297
3298@c ENOENT is also possible on Linux --- however it only occurs if the file
3299@c descriptor has a `file' structure but no `inode' structure. I'm not
3300@c sure how such an fd could be created. Perhaps it's a bug.
3301
3302@end table
3303
3304@end deftypefun
3305
3306@deftypefun int ftruncate64 (int @var{id}, off64_t @var{length})
3307@standards{Unix98, unistd.h}
3308@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3309@c In the absence of a syscall, try ftruncate if length fits.
3310This function is similar to the @code{ftruncate} function. The
3311difference is that the @var{length} argument is 64 bits wide even on 32
3312bits machines which allows the handling of files with sizes up to
3313@twoexp{63} bytes.
3314
3315When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} on a
331632 bits machine this function is actually available under the name
3317@code{ftruncate} and so transparently replaces the 32 bits interface.
3318@end deftypefun
3319
3320As announced here is a little example of how to use @code{ftruncate} in
3321combination with @code{mmap}:
3322
3323@smallexample
3324int fd;
3325void *start;
3326size_t len;
3327
3328int
3329add (off_t at, void *block, size_t size)
3330@{
3331 if (at + size > len)
3332 @{
3333 /* Resize the file and remap. */
3334 size_t ps = sysconf (_SC_PAGESIZE);
3335 size_t ns = (at + size + ps - 1) & ~(ps - 1);
3336 void *np;
3337 if (ftruncate (fd, ns) < 0)
3338 return -1;
3339 np = mmap (NULL, ns, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
3340 if (np == MAP_FAILED)
3341 return -1;
3342 start = np;
3343 len = ns;
3344 @}
3345 memcpy ((char *) start + at, block, size);
3346 return 0;
3347@}
3348@end smallexample
3349
3350The function @code{add} writes a block of memory at an arbitrary
3351position in the file. If the current size of the file is too small it
3352is extended. Note that it is extended by a whole number of pages. This
3353is a requirement of @code{mmap}. The program has to keep track of the
3354real size, and when it has finished a final @code{ftruncate} call should
3355set the real size of the file.
3356
3357@node Storage Allocation
3358@subsection Storage Allocation
3359@cindex allocating file storage
3360@cindex file allocation
3361@cindex storage allocating
3362
3363@cindex file fragmentation
3364@cindex fragmentation of files
3365@cindex sparse files
3366@cindex files, sparse
3367Most file systems support allocating large files in a non-contiguous
3368fashion: the file is split into @emph{fragments} which are allocated
3369sequentially, but the fragments themselves can be scattered across the
3370disk. File systems generally try to avoid such fragmentation because it
3371decreases performance, but if a file gradually increases in size, there
3372might be no other option than to fragment it. In addition, many file
3373systems support @emph{sparse files} with @emph{holes}: regions of null
3374bytes for which no backing storage has been allocated by the file
3375system. When the holes are finally overwritten with data, fragmentation
3376can occur as well.
3377
3378Explicit allocation of storage for yet-unwritten parts of the file can
3379help the system to avoid fragmentation. Additionally, if storage
3380pre-allocation fails, it is possible to report the out-of-disk error
3381early, often without filling up the entire disk. However, due to
3382deduplication, copy-on-write semantics, and file compression, such
3383pre-allocation may not reliably prevent the out-of-disk-space error from
3384occurring later. Checking for write errors is still required, and
3385writes to memory-mapped regions created with @code{mmap} can still
3386result in @code{SIGBUS}.
3387
3388@deftypefun int posix_fallocate (int @var{fd}, off_t @var{offset}, off_t @var{length})
3389@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3390@c If the file system does not support allocation,
3391@c @code{posix_fallocate} has a race with file extension (if
3392@c @var{length} is zero) or with concurrent writes of non-NUL bytes (if
3393@c @var{length} is positive).
3394
3395Allocate backing store for the region of @var{length} bytes starting at
3396byte @var{offset} in the file for the descriptor @var{fd}. The file
3397length is increased to @samp{@var{length} + @var{offset}} if necessary.
3398
3399@var{fd} must be a regular file opened for writing, or @code{EBADF} is
3400returned. If there is insufficient disk space to fulfill the allocation
3401request, @code{ENOSPC} is returned.
3402
3403@strong{Note:} If @code{fallocate} is not available (because the file
3404system does not support it), @code{posix_fallocate} is emulated, which
3405has the following drawbacks:
3406
3407@itemize @bullet
3408@item
3409It is very inefficient because all file system blocks in the requested
3410range need to be examined (even if they have been allocated before) and
3411potentially rewritten. In contrast, with proper @code{fallocate}
3412support (see below), the file system can examine the internal file
3413allocation data structures and eliminate holes directly, maybe even
3414using unwritten extents (which are pre-allocated but uninitialized on
3415disk).
3416
3417@item
3418There is a race condition if another thread or process modifies the
3419underlying file in the to-be-allocated area. Non-null bytes could be
3420overwritten with null bytes.
3421
3422@item
3423If @var{fd} has been opened with the @code{O_WRONLY} flag, the function
3424will fail with an @code{errno} value of @code{EBADF}.
3425
3426@item
3427If @var{fd} has been opened with the @code{O_APPEND} flag, the function
3428will fail with an @code{errno} value of @code{EBADF}.
3429
3430@item
3431If @var{length} is zero, @code{ftruncate} is used to increase the file
3432size as requested, without allocating file system blocks. There is a
3433race condition which means that @code{ftruncate} can accidentally
3434truncate the file if it has been extended concurrently.
3435@end itemize
3436
3437On Linux, if an application does not benefit from emulation or if the
3438emulation is harmful due to its inherent race conditions, the
3439application can use the Linux-specific @code{fallocate} function, with a
3440zero flag argument. For the @code{fallocate} function, @theglibc{} does
3441not perform allocation emulation if the file system does not support
3442allocation. Instead, an @code{EOPNOTSUPP} is returned to the caller.
3443
3444@end deftypefun
3445
3446@deftypefun int posix_fallocate64 (int @var{fd}, off64_t @var{offset}, off64_t @var{length})
3447@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3448
3449This function is a variant of @code{posix_fallocate64} which accepts
345064-bit file offsets on all platforms.
3451
3452@end deftypefun
3453
3454@node Making Special Files
3455@section Making Special Files
3456@cindex creating special files
3457@cindex special files
3458
3459The @code{mknod} function is the primitive for making special files,
3460such as files that correspond to devices. @Theglibc{} includes
3461this function for compatibility with BSD.
3462
3463The prototype for @code{mknod} is declared in @file{sys/stat.h}.
3464@pindex sys/stat.h
3465
3466@deftypefun int mknod (const char *@var{filename}, mode_t @var{mode}, dev_t @var{dev})
3467@standards{BSD, sys/stat.h}
3468@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3469@c Instead of issuing the syscall directly, we go through xmknod.
3470@c Although the internal xmknod takes a dev_t*, that could lead to
3471@c @mtsrace races, it's passed a pointer to mknod's dev.
3472The @code{mknod} function makes a special file with name @var{filename}.
3473The @var{mode} specifies the mode of the file, and may include the various
3474special file bits, such as @code{S_IFCHR} (for a character special file)
3475or @code{S_IFBLK} (for a block special file). @xref{Testing File Type}.
3476
3477The @var{dev} argument specifies which device the special file refers to.
3478Its exact interpretation depends on the kind of special file being created.
3479
3480The return value is @code{0} on success and @code{-1} on error. In addition
3481to the usual file name errors (@pxref{File Name Errors}), the
3482following @code{errno} error conditions are defined for this function:
3483
3484@table @code
3485@item EPERM
3486The calling process is not privileged. Only the superuser can create
3487special files.
3488
3489@item ENOSPC
3490The directory or file system that would contain the new file is full
3491and cannot be extended.
3492
3493@item EROFS
3494The directory containing the new file can't be modified because it's on
3495a read-only file system.
3496
3497@item EEXIST
3498There is already a file named @var{filename}. If you want to replace
3499this file, you must remove the old file explicitly first.
3500@end table
3501@end deftypefun
3502
3503@node Temporary Files
3504@section Temporary Files
3505
3506If you need to use a temporary file in your program, you can use the
3507@code{tmpfile} function to open it. Or you can use the @code{tmpnam}
3508(better: @code{tmpnam_r}) function to provide a name for a temporary
3509file and then you can open it in the usual way with @code{fopen}.
3510
3511The @code{tempnam} function is like @code{tmpnam} but lets you choose
3512what directory temporary files will go in, and something about what
3513their file names will look like. Important for multi-threaded programs
3514is that @code{tempnam} is reentrant, while @code{tmpnam} is not since it
3515returns a pointer to a static buffer.
3516
3517These facilities are declared in the header file @file{stdio.h}.
3518@pindex stdio.h
3519
3520@deftypefun {FILE *} tmpfile (void)
3521@standards{ISO, stdio.h}
3522@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
3523@c The unsafety issues are those of fdopen, plus @acsfd because of the
3524@c open.
3525@c __path_search (internal buf, !dir, const pfx, !try_tmpdir) ok
3526@c libc_secure_genenv only if try_tmpdir
3527@c xstat64, strlen, strcmp, sprintf
3528@c __gen_tempname (internal tmpl, __GT_FILE) ok
3529@c strlen, memcmp, getpid, open/mkdir/lxstat64 ok
3530@c HP_TIMING_NOW if available ok
3531@c gettimeofday (!tz) first time, or every time if no HP_TIMING_NOW ok
3532@c static value is used and modified without synchronization ok
3533@c but the use is as a source of non-cryptographic randomness
3534@c with retries in case of collision, so it should be safe
3535@c unlink, fdopen
3536This function creates a temporary binary file for update mode, as if by
3537calling @code{fopen} with mode @code{"wb+"}. The file is deleted
3538automatically when it is closed or when the program terminates. (On
3539some other @w{ISO C} systems the file may fail to be deleted if the program
3540terminates abnormally).
3541
3542This function is reentrant.
3543
3544When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
354532-bit system this function is in fact @code{tmpfile64}, i.e., the LFS
3546interface transparently replaces the old interface.
3547@end deftypefun
3548
3549@deftypefun {FILE *} tmpfile64 (void)
3550@standards{Unix98, stdio.h}
3551@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
3552This function is similar to @code{tmpfile}, but the stream it returns a
3553pointer to was opened using @code{tmpfile64}. Therefore this stream can
3554be used for files larger than @twoexp{31} bytes on 32-bit machines.
3555
3556Please note that the return type is still @code{FILE *}. There is no
3557special @code{FILE} type for the LFS interface.
3558
3559If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
3560bits machine this function is available under the name @code{tmpfile}
3561and so transparently replaces the old interface.
3562@end deftypefun
3563
3564@deftypefun {char *} tmpnam (char *@var{result})
3565@standards{ISO, stdio.h}
3566@safety{@prelim{}@mtunsafe{@mtasurace{:tmpnam/!result}}@asunsafe{}@acsafe{}}
3567@c The passed-in buffer should not be modified concurrently with the
3568@c call.
3569@c __path_search (static or passed-in buf, !dir, !pfx, !try_tmpdir) ok
3570@c __gen_tempname (internal tmpl, __GT_NOCREATE) ok
3571This function constructs and returns a valid file name that does not
3572refer to any existing file. If the @var{result} argument is a null
3573pointer, the return value is a pointer to an internal static string,
3574which might be modified by subsequent calls and therefore makes this
3575function non-reentrant. Otherwise, the @var{result} argument should be
3576a pointer to an array of at least @code{L_tmpnam} characters, and the
3577result is written into that array.
3578
3579It is possible for @code{tmpnam} to fail if you call it too many times
3580without removing previously-created files. This is because the limited
3581length of the temporary file names gives room for only a finite number
3582of different names. If @code{tmpnam} fails it returns a null pointer.
3583
3584@strong{Warning:} Between the time the pathname is constructed and the
3585file is created another process might have created a file with the same
3586name using @code{tmpnam}, leading to a possible security hole. The
3587implementation generates names which can hardly be predicted, but when
3588opening the file you should use the @code{O_EXCL} flag. Using
3589@code{tmpfile} or @code{mkstemp} is a safe way to avoid this problem.
3590@end deftypefun
3591
3592@deftypefun {char *} tmpnam_r (char *@var{result})
3593@standards{GNU, stdio.h}
3594@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3595This function is nearly identical to the @code{tmpnam} function, except
3596that if @var{result} is a null pointer it returns a null pointer.
3597
3598This guarantees reentrancy because the non-reentrant situation of
3599@code{tmpnam} cannot happen here.
3600
3601@strong{Warning}: This function has the same security problems as
3602@code{tmpnam}.
3603@end deftypefun
3604
3605@deftypevr Macro int L_tmpnam
3606@standards{ISO, stdio.h}
3607The value of this macro is an integer constant expression that
3608represents the minimum size of a string large enough to hold a file name
3609generated by the @code{tmpnam} function.
3610@end deftypevr
3611
3612@deftypevr Macro int TMP_MAX
3613@standards{ISO, stdio.h}
3614The macro @code{TMP_MAX} is a lower bound for how many temporary names
3615you can create with @code{tmpnam}. You can rely on being able to call
3616@code{tmpnam} at least this many times before it might fail saying you
3617have made too many temporary file names.
3618
3619With @theglibc{}, you can create a very large number of temporary
3620file names. If you actually created the files, you would probably run
3621out of disk space before you ran out of names. Some other systems have
3622a fixed, small limit on the number of temporary files. The limit is
3623never less than @code{25}.
3624@end deftypevr
3625
3626@deftypefun {char *} tempnam (const char *@var{dir}, const char *@var{prefix})
3627@standards{SVID, stdio.h}
3628@safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
3629@c There's no way (short of being setuid) to avoid getenv("TMPDIR"),
3630@c even with a non-NULL dir.
3631@c
3632@c __path_search (internal buf, dir, pfx, try_tmpdir) unsafe getenv
3633@c __gen_tempname (internal tmpl, __GT_NOCREATE) ok
3634@c strdup
3635This function generates a unique temporary file name. If @var{prefix}
3636is not a null pointer, up to five characters of this string are used as
3637a prefix for the file name. The return value is a string newly
3638allocated with @code{malloc}, so you should release its storage with
3639@code{free} when it is no longer needed.
3640
3641Because the string is dynamically allocated this function is reentrant.
3642
3643The directory prefix for the temporary file name is determined by
3644testing each of the following in sequence. The directory must exist and
3645be writable.
3646
3647@itemize @bullet
3648@item
3649The environment variable @code{TMPDIR}, if it is defined. For security
3650reasons this only happens if the program is not SUID or SGID enabled.
3651
3652@item
3653The @var{dir} argument, if it is not a null pointer.
3654
3655@item
3656The value of the @code{P_tmpdir} macro.
3657
3658@item
3659The directory @file{/tmp}.
3660@end itemize
3661
3662This function is defined for SVID compatibility.
3663
3664@strong{Warning:} Between the time the pathname is constructed and the
3665file is created another process might have created a file with the same
3666name using @code{tempnam}, leading to a possible security hole. The
3667implementation generates names which can hardly be predicted, but when
3668opening the file you should use the @code{O_EXCL} flag. Using
3669@code{tmpfile} or @code{mkstemp} is a safe way to avoid this problem.
3670@end deftypefun
3671@cindex TMPDIR environment variable
3672
3673@c !!! are we putting SVID/GNU/POSIX.1/BSD in here or not??
3674@deftypevr {SVID Macro} {char *} P_tmpdir
3675@standards{SVID, stdio.h}
3676This macro is the name of the default directory for temporary files.
3677@end deftypevr
3678
3679Older Unix systems did not have the functions just described. Instead
3680they used @code{mktemp} and @code{mkstemp}. Both of these functions
3681work by modifying a file name template string you pass. The last six
3682characters of this string must be @samp{XXXXXX}. These six @samp{X}s
3683are replaced with six characters which make the whole string a unique
3684file name. Usually the template string is something like
3685@samp{/tmp/@var{prefix}XXXXXX}, and each program uses a unique @var{prefix}.
3686
3687@strong{NB:} Because @code{mktemp} and @code{mkstemp} modify the
3688template string, you @emph{must not} pass string constants to them.
3689String constants are normally in read-only storage, so your program
3690would crash when @code{mktemp} or @code{mkstemp} tried to modify the
3691string. These functions are declared in the header file @file{stdlib.h}.
3692@pindex stdlib.h
3693
3694@deftypefun {char *} mktemp (char *@var{template})
3695@standards{Unix, stdlib.h}
3696@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3697@c __gen_tempname (caller tmpl, __GT_NOCREATE) ok
3698The @code{mktemp} function generates a unique file name by modifying
3699@var{template} as described above. If successful, it returns
3700@var{template} as modified. If @code{mktemp} cannot find a unique file
3701name, it makes @var{template} an empty string and returns that. If
3702@var{template} does not end with @samp{XXXXXX}, @code{mktemp} returns a
3703null pointer.
3704
3705@strong{Warning:} Between the time the pathname is constructed and the
3706file is created another process might have created a file with the same
3707name using @code{mktemp}, leading to a possible security hole. The
3708implementation generates names which can hardly be predicted, but when
3709opening the file you should use the @code{O_EXCL} flag. Using
3710@code{mkstemp} is a safe way to avoid this problem.
3711@end deftypefun
3712
3713@deftypefun int mkstemp (char *@var{template})
3714@standards{BSD, stdlib.h}
3715@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
3716@c __gen_tempname (caller tmpl, __GT_FILE) ok
3717The @code{mkstemp} function generates a unique file name just as
3718@code{mktemp} does, but it also opens the file for you with @code{open}
3719(@pxref{Opening and Closing Files}). If successful, it modifies
3720@var{template} in place and returns a file descriptor for that file open
3721for reading and writing. If @code{mkstemp} cannot create a
3722uniquely-named file, it returns @code{-1}. If @var{template} does not
3723end with @samp{XXXXXX}, @code{mkstemp} returns @code{-1} and does not
3724modify @var{template}.
3725
3726The file is opened using mode @code{0600}. If the file is meant to be
3727used by other users this mode must be changed explicitly.
3728@end deftypefun
3729
3730Unlike @code{mktemp}, @code{mkstemp} is actually guaranteed to create a
3731unique file that cannot possibly clash with any other program trying to
3732create a temporary file. This is because it works by calling
3733@code{open} with the @code{O_EXCL} flag, which says you want to create a
3734new file and get an error if the file already exists.
3735
3736@deftypefun {char *} mkdtemp (char *@var{template})
3737@standards{BSD, stdlib.h}
3738@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3739@c __gen_tempname (caller tmpl, __GT_DIR) ok
3740The @code{mkdtemp} function creates a directory with a unique name. If
3741it succeeds, it overwrites @var{template} with the name of the
3742directory, and returns @var{template}. As with @code{mktemp} and
3743@code{mkstemp}, @var{template} should be a string ending with
3744@samp{XXXXXX}.
3745
3746If @code{mkdtemp} cannot create an uniquely named directory, it returns
3747@code{NULL} and sets @code{errno} appropriately. If @var{template} does
3748not end with @samp{XXXXXX}, @code{mkdtemp} returns @code{NULL} and does
3749not modify @var{template}. @code{errno} will be set to @code{EINVAL} in
3750this case.
3751
3752The directory is created using mode @code{0700}.
3753@end deftypefun
3754
3755The directory created by @code{mkdtemp} cannot clash with temporary
3756files or directories created by other users. This is because directory
3757creation always works like @code{open} with @code{O_EXCL}.
3758@xref{Creating Directories}.
3759
3760The @code{mkdtemp} function comes from OpenBSD.
3761
3762@c FIXME these are undocumented:
3763@c faccessat
3764@c fchmodat
3765@c fchownat
3766@c futimesat
3767@c fstatat (there's a commented-out safety assessment for this one)
3768@c statx
3769@c mkdirat
3770@c mkfifoat
3771@c name_to_handle_at
3772@c openat
3773@c open_by_handle_at
3774@c readlinkat
3775@c renameat
3776@c renameat2
3777@c scandirat
3778@c symlinkat
3779@c unlinkat
3780@c utimensat
3781@c mknodat
This page took 0.054338 seconds and 6 git commands to generate.