Back to index

glibc  2.9
stat.h
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1995-2004, 2005, 2006, 2007
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /*
00021  *     POSIX Standard: 5.6 File Characteristics  <sys/stat.h>
00022  */
00023 
00024 #ifndef       _SYS_STAT_H
00025 #define       _SYS_STAT_H   1
00026 
00027 #include <features.h>
00028 
00029 #include <bits/types.h>            /* For __mode_t and __dev_t.  */
00030 
00031 #if defined __USE_XOPEN || defined __USE_XOPEN2K || defined __USE_MISC \
00032          || defined __USE_ATFILE
00033 # if defined __USE_XOPEN || defined __USE_XOPEN2K
00034 #  define __need_time_t
00035 # endif
00036 # if defined __USE_MISC || defined __USE_ATFILE
00037 #  define __need_timespec
00038 # endif
00039 # include <time.h>          /* For time_t resp. timespec.  */
00040 #endif
00041 
00042 #if defined __USE_XOPEN || defined __USE_XOPEN2K
00043 /* The Single Unix specification says that some more types are
00044    available here.  */
00045 # ifndef __dev_t_defined
00046 typedef __dev_t dev_t;
00047 #  define __dev_t_defined
00048 # endif
00049 
00050 # ifndef __gid_t_defined
00051 typedef __gid_t gid_t;
00052 #  define __gid_t_defined
00053 # endif
00054 
00055 # ifndef __ino_t_defined
00056 #  ifndef __USE_FILE_OFFSET64
00057 typedef __ino_t ino_t;
00058 #  else
00059 typedef __ino64_t ino_t;
00060 #  endif
00061 #  define __ino_t_defined
00062 # endif
00063 
00064 # ifndef __mode_t_defined
00065 typedef __mode_t mode_t;
00066 #  define __mode_t_defined
00067 # endif
00068 
00069 # ifndef __nlink_t_defined
00070 typedef __nlink_t nlink_t;
00071 #  define __nlink_t_defined
00072 # endif
00073 
00074 # ifndef __off_t_defined
00075 #  ifndef __USE_FILE_OFFSET64
00076 typedef __off_t off_t;
00077 #  else
00078 typedef __off64_t off_t;
00079 #  endif
00080 #  define __off_t_defined
00081 # endif
00082 
00083 # ifndef __uid_t_defined
00084 typedef __uid_t uid_t;
00085 #  define __uid_t_defined
00086 # endif
00087 #endif /* X/Open */
00088 
00089 #ifdef __USE_UNIX98
00090 # ifndef __blkcnt_t_defined
00091 #  ifndef __USE_FILE_OFFSET64
00092 typedef __blkcnt_t blkcnt_t;
00093 #  else
00094 typedef __blkcnt64_t blkcnt_t;
00095 #  endif
00096 #  define __blkcnt_t_defined
00097 # endif
00098 
00099 # ifndef __blksize_t_defined
00100 typedef __blksize_t blksize_t;
00101 #  define __blksize_t_defined
00102 # endif
00103 #endif /* Unix98 */
00104 
00105 __BEGIN_DECLS
00106 
00107 #include <bits/stat.h>
00108 
00109 #if defined __USE_BSD || defined __USE_MISC || defined __USE_XOPEN
00110 # define S_IFMT             __S_IFMT
00111 # define S_IFDIR     __S_IFDIR
00112 # define S_IFCHR     __S_IFCHR
00113 # define S_IFBLK     __S_IFBLK
00114 # define S_IFREG     __S_IFREG
00115 # ifdef __S_IFIFO
00116 #  define S_IFIFO    __S_IFIFO
00117 # endif
00118 # ifdef __S_IFLNK
00119 #  define S_IFLNK    __S_IFLNK
00120 # endif
00121 # if (defined __USE_BSD || defined __USE_MISC || defined __USE_UNIX98) \
00122      && defined __S_IFSOCK
00123 #  define S_IFSOCK   __S_IFSOCK
00124 # endif
00125 #endif
00126 
00127 /* Test macros for file types.     */
00128 
00129 #define       __S_ISTYPE(mode, mask)      (((mode) & __S_IFMT) == (mask))
00130 
00131 #define       S_ISDIR(mode)  __S_ISTYPE((mode), __S_IFDIR)
00132 #define       S_ISCHR(mode)  __S_ISTYPE((mode), __S_IFCHR)
00133 #define       S_ISBLK(mode)  __S_ISTYPE((mode), __S_IFBLK)
00134 #define       S_ISREG(mode)  __S_ISTYPE((mode), __S_IFREG)
00135 #ifdef __S_IFIFO
00136 # define S_ISFIFO(mode)      __S_ISTYPE((mode), __S_IFIFO)
00137 #endif
00138 #ifdef __S_IFLNK
00139 # define S_ISLNK(mode)       __S_ISTYPE((mode), __S_IFLNK)
00140 #endif
00141 
00142 #if defined __USE_BSD && !defined __S_IFLNK
00143 # define S_ISLNK(mode)  0
00144 #endif
00145 
00146 #if (defined __USE_BSD || defined __USE_UNIX98) \
00147     && defined __S_IFSOCK
00148 # define S_ISSOCK(mode) __S_ISTYPE((mode), __S_IFSOCK)
00149 #endif
00150 
00151 /* These are from POSIX.1b.  If the objects are not implemented using separate
00152    distinct file types, the macros always will evaluate to zero.  Unlike the
00153    other S_* macros the following three take a pointer to a `struct stat'
00154    object as the argument.  */
00155 #ifdef __USE_POSIX199309
00156 # define S_TYPEISMQ(buf) __S_TYPEISMQ(buf)
00157 # define S_TYPEISSEM(buf) __S_TYPEISSEM(buf)
00158 # define S_TYPEISSHM(buf) __S_TYPEISSHM(buf)
00159 #endif
00160 
00161 
00162 /* Protection bits.  */
00163 
00164 #define       S_ISUID __S_ISUID    /* Set user ID on execution.  */
00165 #define       S_ISGID       __S_ISGID     /* Set group ID on execution.  */
00166 
00167 #if defined __USE_BSD || defined __USE_MISC || defined __USE_XOPEN
00168 /* Save swapped text after use (sticky bit).  This is pretty well obsolete.  */
00169 # define S_ISVTX     __S_ISVTX
00170 #endif
00171 
00172 #define       S_IRUSR       __S_IREAD     /* Read by owner.  */
00173 #define       S_IWUSR       __S_IWRITE    /* Write by owner.  */
00174 #define       S_IXUSR       __S_IEXEC     /* Execute by owner.  */
00175 /* Read, write, and execute by owner.  */
00176 #define       S_IRWXU       (__S_IREAD|__S_IWRITE|__S_IEXEC)
00177 
00178 #if defined __USE_MISC && defined __USE_BSD
00179 # define S_IREAD     S_IRUSR
00180 # define S_IWRITE    S_IWUSR
00181 # define S_IEXEC     S_IXUSR
00182 #endif
00183 
00184 #define       S_IRGRP       (S_IRUSR >> 3)       /* Read by group.  */
00185 #define       S_IWGRP       (S_IWUSR >> 3)       /* Write by group.  */
00186 #define       S_IXGRP       (S_IXUSR >> 3)       /* Execute by group.  */
00187 /* Read, write, and execute by group.  */
00188 #define       S_IRWXG       (S_IRWXU >> 3)
00189 
00190 #define       S_IROTH       (S_IRGRP >> 3)       /* Read by others.  */
00191 #define       S_IWOTH       (S_IWGRP >> 3)       /* Write by others.  */
00192 #define       S_IXOTH       (S_IXGRP >> 3)       /* Execute by others.  */
00193 /* Read, write, and execute by others.  */
00194 #define       S_IRWXO       (S_IRWXG >> 3)
00195 
00196 
00197 #ifdef __USE_BSD
00198 /* Macros for common mode bit masks.  */
00199 # define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
00200 # define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)/* 07777 */
00201 # define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666*/
00202 
00203 # define S_BLKSIZE   512    /* Block size for `st_blocks'.  */
00204 #endif
00205 
00206 
00207 #ifndef __USE_FILE_OFFSET64
00208 /* Get file attributes for FILE and put them in BUF.  */
00209 extern int stat (__const char *__restrict __file,
00210                struct stat *__restrict __buf) __THROW __nonnull ((1, 2));
00211 
00212 /* Get file attributes for the file, device, pipe, or socket
00213    that file descriptor FD is open on and put them in BUF.  */
00214 extern int fstat (int __fd, struct stat *__buf) __THROW __nonnull ((2));
00215 #else
00216 # ifdef __REDIRECT_NTH
00217 extern int __REDIRECT_NTH (stat, (__const char *__restrict __file,
00218                               struct stat *__restrict __buf), stat64)
00219      __nonnull ((1, 2));
00220 extern int __REDIRECT_NTH (fstat, (int __fd, struct stat *__buf), fstat64)
00221      __nonnull ((2));
00222 # else
00223 #  define stat stat64
00224 #  define fstat fstat64
00225 # endif
00226 #endif
00227 #ifdef __USE_LARGEFILE64
00228 extern int stat64 (__const char *__restrict __file,
00229                  struct stat64 *__restrict __buf) __THROW __nonnull ((1, 2));
00230 extern int fstat64 (int __fd, struct stat64 *__buf) __THROW __nonnull ((2));
00231 #endif
00232 
00233 #ifdef __USE_ATFILE
00234 /* Similar to stat, get the attributes for FILE and put them in BUF.
00235    Relative path names are interpreted relative to FD unless FD is
00236    AT_FDCWD.  */
00237 # ifndef __USE_FILE_OFFSET64
00238 extern int fstatat (int __fd, __const char *__restrict __file,
00239                   struct stat *__restrict __buf, int __flag)
00240      __THROW __nonnull ((2, 3));
00241 # else
00242 #  ifdef __REDIRECT_NTH
00243 extern int __REDIRECT_NTH (fstatat, (int __fd, __const char *__restrict __file,
00244                                  struct stat *__restrict __buf,
00245                                  int __flag),
00246                         fstatat64) __nonnull ((2, 3));
00247 #  else
00248 #   define fstatat fstatat64
00249 #  endif
00250 # endif
00251 
00252 extern int fstatat64 (int __fd, __const char *__restrict __file,
00253                     struct stat64 *__restrict __buf, int __flag)
00254      __THROW __nonnull ((2, 3));
00255 #endif
00256 
00257 #if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
00258 # ifndef __USE_FILE_OFFSET64
00259 /* Get file attributes about FILE and put them in BUF.
00260    If FILE is a symbolic link, do not follow it.  */
00261 extern int lstat (__const char *__restrict __file,
00262                 struct stat *__restrict __buf) __THROW __nonnull ((1, 2));
00263 # else
00264 #  ifdef __REDIRECT_NTH
00265 extern int __REDIRECT_NTH (lstat,
00266                         (__const char *__restrict __file,
00267                          struct stat *__restrict __buf), lstat64)
00268      __nonnull ((1, 2));
00269 #  else
00270 #   define lstat lstat64
00271 #  endif
00272 # endif
00273 # ifdef __USE_LARGEFILE64
00274 extern int lstat64 (__const char *__restrict __file,
00275                   struct stat64 *__restrict __buf)
00276      __THROW __nonnull ((1, 2));
00277 # endif
00278 #endif
00279 
00280 /* Set file access permissions for FILE to MODE.
00281    If FILE is a symbolic link, this affects its target instead.  */
00282 extern int chmod (__const char *__file, __mode_t __mode)
00283      __THROW __nonnull ((1));
00284 
00285 #ifdef __USE_BSD
00286 /* Set file access permissions for FILE to MODE.
00287    If FILE is a symbolic link, this affects the link itself
00288    rather than its target.  */
00289 extern int lchmod (__const char *__file, __mode_t __mode)
00290      __THROW __nonnull ((1));
00291 #endif
00292 
00293 /* Set file access permissions of the file FD is open on to MODE.  */
00294 #if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
00295 extern int fchmod (int __fd, __mode_t __mode) __THROW;
00296 #endif
00297 
00298 #ifdef __USE_ATFILE
00299 /* Set file access permissions of FILE relative to
00300    the directory FD is open on.  */
00301 extern int fchmodat (int __fd, __const char *__file, __mode_t mode, int __flag)
00302      __THROW __nonnull ((2)) __wur;
00303 #endif /* Use ATFILE.  */
00304 
00305 
00306 
00307 /* Set the file creation mask of the current process to MASK,
00308    and return the old creation mask.  */
00309 extern __mode_t umask (__mode_t __mask) __THROW;
00310 
00311 #ifdef __USE_GNU
00312 /* Get the current `umask' value without changing it.
00313    This function is only available under the GNU Hurd.  */
00314 extern __mode_t getumask (void) __THROW;
00315 #endif
00316 
00317 /* Create a new directory named PATH, with permission bits MODE.  */
00318 extern int mkdir (__const char *__path, __mode_t __mode)
00319      __THROW __nonnull ((1));
00320 
00321 #ifdef __USE_ATFILE
00322 /* Like mkdir, create a new directory with permission bits MODE.  But
00323    interpret relative PATH names relative to the directory associated
00324    with FD.  */
00325 extern int mkdirat (int __fd, __const char *__path, __mode_t __mode)
00326      __THROW __nonnull ((2));
00327 #endif
00328 
00329 /* Create a device file named PATH, with permission and special bits MODE
00330    and device number DEV (which can be constructed from major and minor
00331    device numbers with the `makedev' macro above).  */
00332 #if defined __USE_MISC || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
00333 extern int mknod (__const char *__path, __mode_t __mode, __dev_t __dev)
00334      __THROW __nonnull ((1));
00335 #endif
00336 
00337 #ifdef __USE_ATFILE
00338 /* Like mknod, create a new device file with permission bits MODE and
00339    device number DEV.  But interpret relative PATH names relative to
00340    the directory associated with FD.  */
00341 extern int mknodat (int __fd, __const char *__path, __mode_t __mode,
00342                   __dev_t __dev) __THROW __nonnull ((2));
00343 #endif
00344 
00345 
00346 /* Create a new FIFO named PATH, with permission bits MODE.  */
00347 extern int mkfifo (__const char *__path, __mode_t __mode)
00348      __THROW __nonnull ((1));
00349 
00350 #ifdef __USE_ATFILE
00351 /* Like mkfifo, create a new FIFO with permission bits MODE.  But
00352    interpret relative PATH names relative to the directory associated
00353    with FD.  */
00354 extern int mkfifoat (int __fd, __const char *__path, __mode_t __mode)
00355      __THROW __nonnull ((2));
00356 #endif
00357 
00358 #ifdef __USE_ATFILE
00359 /* Set file access and modification times relative to directory file
00360    descriptor.  */
00361 extern int utimensat (int __fd, __const char *__path,
00362                     __const struct timespec __times[2],
00363                     int __flags)
00364      __THROW __nonnull ((2));
00365 #endif
00366 
00367 #ifdef __USE_GNU
00368 /* XXX This will change to the macro for the next 2008 POSIX revision.  */
00369 /* Set file access and modification times of the file associated with FD.  */
00370 extern int futimens (int __fd, __const struct timespec __times[2]) __THROW;
00371 #endif
00372 
00373 /* To allow the `struct stat' structure and the file type `mode_t'
00374    bits to vary without changing shared library major version number,
00375    the `stat' family of functions and `mknod' are in fact inline
00376    wrappers around calls to `xstat', `fxstat', `lxstat', and `xmknod',
00377    which all take a leading version-number argument designating the
00378    data structure and bits used.  <bits/stat.h> defines _STAT_VER with
00379    the version number corresponding to `struct stat' as defined in
00380    that file; and _MKNOD_VER with the version number corresponding to
00381    the S_IF* macros defined therein.  It is arranged that when not
00382    inlined these function are always statically linked; that way a
00383    dynamically-linked executable always encodes the version number
00384    corresponding to the data structures it uses, so the `x' functions
00385    in the shared library can adapt without needing to recompile all
00386    callers.  */
00387 
00388 #ifndef _STAT_VER
00389 # define _STAT_VER   0
00390 #endif
00391 #ifndef _MKNOD_VER
00392 # define _MKNOD_VER  0
00393 #endif
00394 
00395 /* Wrappers for stat and mknod system calls.  */
00396 #ifndef __USE_FILE_OFFSET64
00397 extern int __fxstat (int __ver, int __fildes, struct stat *__stat_buf)
00398      __THROW __nonnull ((3));
00399 extern int __xstat (int __ver, __const char *__filename,
00400                   struct stat *__stat_buf) __THROW __nonnull ((2, 3));
00401 extern int __lxstat (int __ver, __const char *__filename,
00402                    struct stat *__stat_buf) __THROW __nonnull ((2, 3));
00403 extern int __fxstatat (int __ver, int __fildes, __const char *__filename,
00404                      struct stat *__stat_buf, int __flag)
00405      __THROW __nonnull ((3, 4));
00406 #else
00407 # ifdef __REDIRECT_NTH
00408 extern int __REDIRECT_NTH (__fxstat, (int __ver, int __fildes,
00409                                   struct stat *__stat_buf), __fxstat64)
00410      __nonnull ((3));
00411 extern int __REDIRECT_NTH (__xstat, (int __ver, __const char *__filename,
00412                                  struct stat *__stat_buf), __xstat64)
00413      __nonnull ((2, 3));
00414 extern int __REDIRECT_NTH (__lxstat, (int __ver, __const char *__filename,
00415                                   struct stat *__stat_buf), __lxstat64)
00416      __nonnull ((2, 3));
00417 extern int __REDIRECT_NTH (__fxstatat, (int __ver, int __fildes,
00418                                    __const char *__filename,
00419                                    struct stat *__stat_buf, int __flag),
00420                         __fxstatat64) __nonnull ((3, 4));
00421 
00422 # else
00423 #  define __fxstat __fxstat64
00424 #  define __xstat __xstat64
00425 #  define __lxstat __lxstat64
00426 # endif
00427 #endif
00428 
00429 #ifdef __USE_LARGEFILE64
00430 extern int __fxstat64 (int __ver, int __fildes, struct stat64 *__stat_buf)
00431      __THROW __nonnull ((3));
00432 extern int __xstat64 (int __ver, __const char *__filename,
00433                     struct stat64 *__stat_buf) __THROW __nonnull ((2, 3));
00434 extern int __lxstat64 (int __ver, __const char *__filename,
00435                      struct stat64 *__stat_buf) __THROW __nonnull ((2, 3));
00436 extern int __fxstatat64 (int __ver, int __fildes, __const char *__filename,
00437                       struct stat64 *__stat_buf, int __flag)
00438      __THROW __nonnull ((3, 4));
00439 #endif
00440 extern int __xmknod (int __ver, __const char *__path, __mode_t __mode,
00441                    __dev_t *__dev) __THROW __nonnull ((2, 4));
00442 
00443 extern int __xmknodat (int __ver, int __fd, __const char *__path,
00444                      __mode_t __mode, __dev_t *__dev)
00445      __THROW __nonnull ((3, 5));
00446 
00447 #if defined __GNUC__ && __GNUC__ >= 2 && defined __USE_EXTERN_INLINES
00448 /* Inlined versions of the real stat and mknod functions.  */
00449 
00450 __extern_inline int
00451 __NTH (stat (__const char *__path, struct stat *__statbuf))
00452 {
00453   return __xstat (_STAT_VER, __path, __statbuf);
00454 }
00455 
00456 # if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
00457 __extern_inline int
00458 __NTH (lstat (__const char *__path, struct stat *__statbuf))
00459 {
00460   return __lxstat (_STAT_VER, __path, __statbuf);
00461 }
00462 # endif
00463 
00464 __extern_inline int
00465 __NTH (fstat (int __fd, struct stat *__statbuf))
00466 {
00467   return __fxstat (_STAT_VER, __fd, __statbuf);
00468 }
00469 
00470 # ifdef __USE_ATFILE
00471 __extern_inline int
00472 __NTH (fstatat (int __fd, __const char *__filename, struct stat *__statbuf,
00473               int __flag))
00474 {
00475   return __fxstatat (_STAT_VER, __fd, __filename, __statbuf, __flag);
00476 }
00477 # endif
00478 
00479 # if defined __USE_MISC || defined __USE_BSD
00480 __extern_inline int
00481 __NTH (mknod (__const char *__path, __mode_t __mode, __dev_t __dev))
00482 {
00483   return __xmknod (_MKNOD_VER, __path, __mode, &__dev);
00484 }
00485 # endif
00486 
00487 # ifdef __USE_ATFILE
00488 __extern_inline int
00489 __NTH (mknodat (int __fd, __const char *__path, __mode_t __mode,
00490               __dev_t __dev))
00491 {
00492   return __xmknodat (_MKNOD_VER, __fd, __path, __mode, &__dev);
00493 }
00494 # endif
00495 
00496 # if defined __USE_LARGEFILE64 \
00497   && (! defined __USE_FILE_OFFSET64 \
00498       || (defined __REDIRECT_NTH && defined __OPTIMIZE__))
00499 __extern_inline int
00500 __NTH (stat64 (__const char *__path, struct stat64 *__statbuf))
00501 {
00502   return __xstat64 (_STAT_VER, __path, __statbuf);
00503 }
00504 
00505 #  if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
00506 __extern_inline int
00507 __NTH (lstat64 (__const char *__path, struct stat64 *__statbuf))
00508 {
00509   return __lxstat64 (_STAT_VER, __path, __statbuf);
00510 }
00511 #  endif
00512 
00513 __extern_inline int
00514 __NTH (fstat64 (int __fd, struct stat64 *__statbuf))
00515 {
00516   return __fxstat64 (_STAT_VER, __fd, __statbuf);
00517 }
00518 
00519 #  ifdef __USE_GNU
00520 __extern_inline int
00521 __NTH (fstatat64 (int __fd, __const char *__filename, struct stat64 *__statbuf,
00522                 int __flag))
00523 {
00524   return __fxstatat64 (_STAT_VER, __fd, __filename, __statbuf, __flag);
00525 }
00526 #  endif
00527 
00528 # endif
00529 
00530 #endif
00531 
00532 __END_DECLS
00533 
00534 
00535 #endif /* sys/stat.h  */