dirent.h文件
Posted hsy1941
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dirent.h文件相关的知识,希望对你有一定的参考价值。
dirent.h头文件
1 /* 2 * Dirent interface for Microsoft Visual Studio 3 * 4 * Copyright (C) 1998-2019 Toni Ronkko 5 * This file is part of dirent. Dirent may be freely distributed 6 * under the MIT license. For all details and documentation, see 7 * https://github.com/tronkko/dirent 8 */ 9 #ifndef DIRENT_H 10 #define DIRENT_H 11 12 /* Hide warnings about unreferenced local functions */ 13 #if defined(__clang__) 14 # pragma clang diagnostic ignored "-Wunused-function" 15 #elif defined(_MSC_VER) 16 # pragma warning(disable:4505) 17 #elif defined(__GNUC__) 18 # pragma GCC diagnostic ignored "-Wunused-function" 19 #endif 20 21 /* 22 * Include windows.h without Windows Sockets 1.1 to prevent conflicts with 23 * Windows Sockets 2.0. 24 */ 25 #ifndef WIN32_LEAN_AND_MEAN 26 # define WIN32_LEAN_AND_MEAN 27 #endif 28 #include <windows.h> 29 30 #include <stdio.h> 31 #include <stdarg.h> 32 #include <wchar.h> 33 #include <string.h> 34 #include <stdlib.h> 35 #include <malloc.h> 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 #include <errno.h> 39 40 /* Indicates that d_type field is available in dirent structure */ 41 #define _DIRENT_HAVE_D_TYPE 42 43 /* Indicates that d_namlen field is available in dirent structure */ 44 #define _DIRENT_HAVE_D_NAMLEN 45 46 /* Entries missing from MSVC 6.0 */ 47 #if !defined(FILE_ATTRIBUTE_DEVICE) 48 # define FILE_ATTRIBUTE_DEVICE 0x40 49 #endif 50 51 /* File type and permission flags for stat(), general mask */ 52 #if !defined(S_IFMT) 53 # define S_IFMT _S_IFMT 54 #endif 55 56 /* Directory bit */ 57 #if !defined(S_IFDIR) 58 # define S_IFDIR _S_IFDIR 59 #endif 60 61 /* Character device bit */ 62 #if !defined(S_IFCHR) 63 # define S_IFCHR _S_IFCHR 64 #endif 65 66 /* Pipe bit */ 67 #if !defined(S_IFFIFO) 68 # define S_IFFIFO _S_IFFIFO 69 #endif 70 71 /* Regular file bit */ 72 #if !defined(S_IFREG) 73 # define S_IFREG _S_IFREG 74 #endif 75 76 /* Read permission */ 77 #if !defined(S_IREAD) 78 # define S_IREAD _S_IREAD 79 #endif 80 81 /* Write permission */ 82 #if !defined(S_IWRITE) 83 # define S_IWRITE _S_IWRITE 84 #endif 85 86 /* Execute permission */ 87 #if !defined(S_IEXEC) 88 # define S_IEXEC _S_IEXEC 89 #endif 90 91 /* Pipe */ 92 #if !defined(S_IFIFO) 93 # define S_IFIFO _S_IFIFO 94 #endif 95 96 /* Block device */ 97 #if !defined(S_IFBLK) 98 # define S_IFBLK 0 99 #endif 100 101 /* Link */ 102 #if !defined(S_IFLNK) 103 # define S_IFLNK 0 104 #endif 105 106 /* Socket */ 107 #if !defined(S_IFSOCK) 108 # define S_IFSOCK 0 109 #endif 110 111 /* Read user permission */ 112 #if !defined(S_IRUSR) 113 # define S_IRUSR S_IREAD 114 #endif 115 116 /* Write user permission */ 117 #if !defined(S_IWUSR) 118 # define S_IWUSR S_IWRITE 119 #endif 120 121 /* Execute user permission */ 122 #if !defined(S_IXUSR) 123 # define S_IXUSR 0 124 #endif 125 126 /* Read group permission */ 127 #if !defined(S_IRGRP) 128 # define S_IRGRP 0 129 #endif 130 131 /* Write group permission */ 132 #if !defined(S_IWGRP) 133 # define S_IWGRP 0 134 #endif 135 136 /* Execute group permission */ 137 #if !defined(S_IXGRP) 138 # define S_IXGRP 0 139 #endif 140 141 /* Read others permission */ 142 #if !defined(S_IROTH) 143 # define S_IROTH 0 144 #endif 145 146 /* Write others permission */ 147 #if !defined(S_IWOTH) 148 # define S_IWOTH 0 149 #endif 150 151 /* Execute others permission */ 152 #if !defined(S_IXOTH) 153 # define S_IXOTH 0 154 #endif 155 156 /* Maximum length of file name */ 157 #if !defined(PATH_MAX) 158 # define PATH_MAX MAX_PATH 159 #endif 160 #if !defined(FILENAME_MAX) 161 # define FILENAME_MAX MAX_PATH 162 #endif 163 #if !defined(NAME_MAX) 164 # define NAME_MAX FILENAME_MAX 165 #endif 166 167 /* File type flags for d_type */ 168 #define DT_UNKNOWN 0 169 #define DT_REG S_IFREG 170 #define DT_DIR S_IFDIR 171 #define DT_FIFO S_IFIFO 172 #define DT_SOCK S_IFSOCK 173 #define DT_CHR S_IFCHR 174 #define DT_BLK S_IFBLK 175 #define DT_LNK S_IFLNK 176 177 /* Macros for converting between st_mode and d_type */ 178 #define IFTODT(mode) ((mode) & S_IFMT) 179 #define DTTOIF(type) (type) 180 181 /* 182 * File type macros. Note that block devices, sockets and links cannot be 183 * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are 184 * only defined for compatibility. These macros should always return false 185 * on Windows. 186 */ 187 #if !defined(S_ISFIFO) 188 # define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) 189 #endif 190 #if !defined(S_ISDIR) 191 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) 192 #endif 193 #if !defined(S_ISREG) 194 # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) 195 #endif 196 #if !defined(S_ISLNK) 197 # define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) 198 #endif 199 #if !defined(S_ISSOCK) 200 # define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) 201 #endif 202 #if !defined(S_ISCHR) 203 # define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) 204 #endif 205 #if !defined(S_ISBLK) 206 # define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) 207 #endif 208 209 /* Return the exact length of the file name without zero terminator */ 210 #define _D_EXACT_NAMLEN(p) ((p)->d_namlen) 211 212 /* Return the maximum size of a file name */ 213 #define _D_ALLOC_NAMLEN(p) ((PATH_MAX)+1) 214 215 216 #ifdef __cplusplus 217 extern "C" { 218 #endif 219 220 221 /* Wide-character version */ 222 struct _wdirent { 223 /* Always zero */ 224 long d_ino; 225 226 /* File position within stream */ 227 long d_off; 228 229 /* Structure size */ 230 unsigned short d_reclen; 231 232 /* Length of name without */ 233 size_t d_namlen; 234 235 /* File type */ 236 int d_type; 237 238 /* File name */ 239 wchar_t d_name[PATH_MAX+1]; 240 }; 241 typedef struct _wdirent _wdirent; 242 243 struct _WDIR { 244 /* Current directory entry */ 245 struct _wdirent ent; 246 247 /* Private file data */ 248 WIN32_FIND_DATAW data; 249 250 /* True if data is valid */ 251 int cached; 252 253 /* Win32 search handle */ 254 HANDLE handle; 255 256 /* Initial directory name */ 257 wchar_t *patt; 258 }; 259 typedef struct _WDIR _WDIR; 260 261 /* Multi-byte character version */ 262 struct dirent { 263 /* Always zero */ 264 long d_ino; 265 266 /* File position within stream */ 267 long d_off; 268 269 /* Structure size */ 270 unsigned short d_reclen; 271 272 /* Length of name without */ 273 size_t d_namlen; 274 275 /* File type */ 276 int d_type; 277 278 /* File name */ 279 char d_name[PATH_MAX+1]; 280 }; 281 typedef struct dirent dirent; 282 283 struct DIR { 284 struct dirent ent; 285 struct _WDIR *wdirp; 286 }; 287 typedef struct DIR DIR; 288 289 290 /* Dirent functions */ 291 static DIR *opendir (const char *dirname); 292 static _WDIR *_wopendir (const wchar_t *dirname); 293 294 static struct dirent *readdir (DIR *dirp); 295 static struct _wdirent *_wreaddir (_WDIR *dirp); 296 297 static int readdir_r( 298 DIR *dirp, struct dirent *entry, struct dirent **result); 299 static int _wreaddir_r( 300 _WDIR *dirp, struct _wdirent *entry, struct _wdirent **result); 301 302 static int closedir (DIR *dirp); 303 static int _wclosedir (_WDIR *dirp); 304 305 static void rewinddir (DIR* dirp); 306 static void _wrewinddir (_WDIR* dirp); 307 308 static int scandir (const char *dirname, struct dirent ***namelist, 309 int (*filter)(const struct dirent*), 310 int (*compare)(const struct dirent**, const struct dirent**)); 311 312 static int alphasort (const struct dirent **a, const struct dirent **b); 313 314 static int versionsort (const struct dirent **a, const struct dirent **b); 315 316 317 /* For compatibility with Symbian */ 318 #define wdirent _wdirent 319 #define WDIR _WDIR 320 #define wopendir _wopendir 321 #define wreaddir _wreaddir 322 #define wclosedir _wclosedir 323 #define wrewinddir _wrewinddir 324 325 326 /* Internal utility functions */ 327 static WIN32_FIND_DATAW *dirent_first (_WDIR *dirp); 328 static WIN32_FIND_DATAW *dirent_next (_WDIR *dirp); 329 330 static int dirent_mbstowcs_s( 331 size_t *pReturnValue, 332 wchar_t *wcstr, 333 size_t sizeInWords, 334 const char *mbstr, 335 size_t count); 336 337 static int dirent_wcstombs_s( 338 size_t *pReturnValue, 339 char *mbstr, 340 size_t sizeInBytes, 341 const wchar_t *wcstr, 342 size_t count); 343 344 static void dirent_set_errno (int error); 345 346 347 /* 348 * Open directory stream DIRNAME for read and return a pointer to the 349 * internal working area that is used to retrieve individual directory 350 * entries. 351 */ 352 static _WDIR* 353 _wopendir( 354 const wchar_t *dirname) 355 { 356 _WDIR *dirp; 357 DWORD n; 358 wchar_t *p; 359 360 /* Must have directory name */ 361 if (dirname == NULL || dirname[0] == ‘