1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
  29  */
  30 
  31 /*      Copyright (c) 1988 AT&T     */
  32 /*        All Rights Reserved   */
  33 
  34 #ifndef _DIRENT_H
  35 #define _DIRENT_H
  36 
  37 #include <sys/feature_tests.h>
  38 
  39 #include <sys/types.h>
  40 #include <sys/dirent.h>
  41 
  42 #ifdef  __cplusplus
  43 extern "C" {
  44 #endif
  45 
  46 #if defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX)
  47 
  48 #define MAXNAMLEN       512             /* maximum filename length */
  49 #define DIRBUF          8192            /* buffer size for fs-indep. dirs */
  50 
  51 #endif /* defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX) */
  52 
  53 #if !defined(__XOPEN_OR_POSIX)
  54 
  55 typedef struct {
  56         int     dd_fd;          /* file descriptor */
  57         int     dd_loc;         /* offset in block */
  58         int     dd_size;        /* amount of valid data */
  59         char    *dd_buf;        /* directory block */
  60 } DIR;                          /* stream data from opendir() */
  61 
  62 
  63 #else
  64 
  65 typedef struct {
  66         int     d_fd;           /* file descriptor */
  67         int     d_loc;          /* offset in block */
  68         int     d_size;         /* amount of valid data */
  69         char    *d_buf;         /* directory block */
  70 } DIR;                          /* stream data from opendir() */
  71 
  72 #endif /* !defined(__XOPEN_OR_POSIX) */
  73 
  74 /* large file compilation environment setup */
  75 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
  76 #ifdef __PRAGMA_REDEFINE_EXTNAME
  77 #pragma redefine_extname        readdir readdir64
  78 #pragma redefine_extname        scandir scandir64
  79 #pragma redefine_extname        alphasort alphasort64
  80 #else
  81 #define readdir                 readdir64
  82 #define scandir                 scandir64
  83 #define alphasort               alphasort64
  84 #endif
  85 #endif  /* _FILE_OFFSET_BITS == 64 */
  86 
  87 /* In the LP64 compilation environment, all APIs are already large file */
  88 #if defined(_LP64) && defined(_LARGEFILE64_SOURCE)
  89 #ifdef __PRAGMA_REDEFINE_EXTNAME
  90 #pragma redefine_extname        readdir64       readdir
  91 #pragma redefine_extname        scandir64       scandir
  92 #pragma redefine_extname        alphasort64     alphasort
  93 #else
  94 #define readdir64               readdir
  95 #define scandir64               scandir
  96 #define alphsort64              alphasort
  97 #endif
  98 #endif  /* _LP64 && _LARGEFILE64_SOURCE */
  99 
 100 extern DIR              *opendir(const char *);
 101 #if defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX) || defined(_XPG7) || \
 102         defined(_ATFILE_SOURCE)
 103 extern DIR              *fdopendir(int);
 104 extern int              dirfd(DIR *);
 105 #endif /* defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX) ... */
 106 
 107 #if defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX) || defined(_XPG7)
 108 extern int              scandir(const char *, struct dirent ***,
 109                                 int (*)(const struct dirent *),
 110                                 int (*)(const struct dirent **,
 111                                         const struct dirent **));
 112 extern int              alphasort(const struct dirent **,
 113                                         const struct dirent **);
 114 #endif /* defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX) ... */
 115 
 116 extern struct dirent    *readdir(DIR *);
 117 #if defined(__EXTENSIONS__) || !defined(_POSIX_C_SOURCE) || \
 118         defined(_XOPEN_SOURCE)
 119 extern long             telldir(DIR *);
 120 extern void             seekdir(DIR *, long);
 121 #endif /* defined(__EXTENSIONS__) || !defined(_POSIX_C_SOURCE) ... */
 122 extern void             rewinddir(DIR *);
 123 extern int              closedir(DIR *);
 124 
 125 /* transitional large file interface */
 126 #if     defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
 127             !defined(__PRAGMA_REDEFINE_EXTNAME))
 128 extern struct dirent64  *readdir64(DIR *);
 129 #if defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX)
 130 extern int      scandir64(const char *, struct dirent64 ***,
 131                         int (*)(const struct dirent64 *),
 132                         int (*)(const struct dirent64 **,
 133                                 const struct dirent64 **));
 134 extern int      alphasort64(const struct dirent64 **, const struct dirent64 **);
 135 #endif /* defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX) */
 136 #endif
 137 
 138 #if defined(__EXTENSIONS__) || !defined(_POSIX_C_SOURCE) || \
 139         defined(_XOPEN_SOURCE)
 140 #define rewinddir(dirp) seekdir(dirp, 0L)
 141 #endif
 142 
 143 /*
 144  * readdir_r() prototype is defined here.
 145  *
 146  * There are several variations, depending on whether compatibility with old
 147  * POSIX draft specifications or the final specification is desired and on
 148  * whether the large file compilation environment is active.  To combat a
 149  * combinatorial explosion, enabling large files implies using the final
 150  * specification (since the definition of the large file environment
 151  * considerably postdates that of the final readdir_r specification).
 152  *
 153  * In the LP64 compilation environment, all APIs are already large file,
 154  * and since there are no 64-bit applications that can have seen the
 155  * draft implementation, again, we use the final POSIX specification.
 156  */
 157 
 158 #if     defined(__EXTENSIONS__) || defined(_REENTRANT) || \
 159         !defined(__XOPEN_OR_POSIX) || (_POSIX_C_SOURCE - 0 >= 199506L) || \
 160         defined(_POSIX_PTHREAD_SEMANTICS)
 161 
 162 #if     !defined(_LP64) && _FILE_OFFSET_BITS == 32
 163 
 164 #if     (_POSIX_C_SOURCE - 0 >= 199506L) || defined(_POSIX_PTHREAD_SEMANTICS)
 165 
 166 #ifdef  __PRAGMA_REDEFINE_EXTNAME
 167 #pragma redefine_extname readdir_r      __posix_readdir_r
 168 extern int readdir_r(DIR *_RESTRICT_KYWD, struct dirent *_RESTRICT_KYWD,
 169                 struct dirent **_RESTRICT_KYWD);
 170 #else   /* __PRAGMA_REDEFINE_EXTNAME */
 171 
 172 extern int __posix_readdir_r(DIR *_RESTRICT_KYWD,
 173     struct dirent *_RESTRICT_KYWD, struct dirent **_RESTRICT_KYWD);
 174 
 175 #ifdef  __lint
 176 #define readdir_r       __posix_readdir_r
 177 #else   /* !__lint */
 178 
 179 static int
 180 readdir_r(DIR *_RESTRICT_KYWD __dp, struct dirent *_RESTRICT_KYWD __ent,
 181                 struct dirent **_RESTRICT_KYWD __res) {
 182         return (__posix_readdir_r(__dp, __ent, __res));
 183 }
 184 
 185 #endif /* !__lint */
 186 #endif /* __PRAGMA_REDEFINE_EXTNAME */
 187 
 188 #else  /* (_POSIX_C_SOURCE - 0 >= 199506L) || ... */
 189 
 190 extern struct dirent *readdir_r(DIR *__dp, struct dirent *__ent);
 191 
 192 #endif  /* (_POSIX_C_SOURCE - 0 >= 199506L) || ... */
 193 
 194 #else   /* !_LP64 && _FILE_OFFSET_BITS == 32 */
 195 
 196 #if defined(_LP64)
 197 #ifdef  __PRAGMA_REDEFINE_EXTNAME
 198 #pragma redefine_extname readdir64_r    readdir_r
 199 #else
 200 #define readdir64_r             readdir_r
 201 #endif
 202 #else   /* _LP64 */
 203 #ifdef  __PRAGMA_REDEFINE_EXTNAME
 204 #pragma redefine_extname readdir_r      readdir64_r
 205 #else
 206 #define readdir_r               readdir64_r
 207 #endif
 208 #endif  /* _LP64 */
 209 extern int readdir_r(DIR *_RESTRICT_KYWD, struct dirent *_RESTRICT_KYWD,
 210         struct dirent **_RESTRICT_KYWD);
 211 
 212 #endif  /* !_LP64 && _FILE_OFFSET_BITS == 32 */
 213 
 214 #if     defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
 215             !defined(__PRAGMA_REDEFINE_EXTNAME))
 216 /* transitional large file interface */
 217 extern int readdir64_r(DIR *_RESTRICT_KYWD, struct dirent64 *_RESTRICT_KYWD,
 218         struct dirent64 **_RESTRICT_KYWD);
 219 #endif
 220 
 221 #endif /* defined(__EXTENSIONS__) || defined(_REENTRANT)... */
 222 
 223 #ifdef  __cplusplus
 224 }
 225 #endif
 226 
 227 #endif  /* _DIRENT_H */