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