Website now in git not CVS
[xapian.git] / xapian-core / common / msvc_dirent.cc
blob49eb44eda6a293f90aaf2dfcdda444457a23fb18
1 /** @file msvc_dirent.cc
2 * @brief Implementation of dirent functions using WIN32 API.
3 */
4 /*
5 Implementation of POSIX directory browsing functions and types for Win32.
7 Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
8 History: Created March 1997. Updated June 2003.
9 Fixes since importing into Xapian:
10 2008-03-04 Fixed readdir() not to set errno to ENOENT when the
11 end of the directory is reached.
13 Copyright Kevlin Henney, 1997, 2003. All rights reserved.
15 Permission to use, copy, modify, and distribute this software and its
16 documentation for any purpose is hereby granted without fee, provided
17 that this copyright and permissions notice appear in all copies and
18 derivatives.
20 This software is supplied "as is" without express or implied warranty.
22 But that said, if there are any problems please get in touch.
25 #include <config.h>
26 #ifdef __WIN32__
28 #include "msvc_dirent.h"
29 #include <cerrno>
30 #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
31 #include <cstdlib>
32 #include <cstring>
34 #ifdef __cplusplus
35 extern "C"
37 #endif
39 struct DIR
41 long handle; /* -1 for failed rewind */
42 struct _finddata_t info;
43 struct dirent result; /* d_name null iff first time */
44 char *name; /* null-terminated char string */
47 DIR *opendir(const char *name)
49 DIR *dir = 0;
51 if(name && name[0])
53 size_t base_length = strlen(name);
54 const char *all = /* search pattern must end with suitable wildcard */
55 strchr("/\\", name[base_length - 1]) ? "*" : "/*";
57 if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
58 (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
60 strcat(strcpy(dir->name, name), all);
62 if((dir->handle = (long) _findfirst(dir->name, &dir->info)) != -1)
64 dir->result.d_name = 0;
66 else /* rollback */
68 free(dir->name);
69 free(dir);
70 dir = 0;
73 else /* rollback */
75 free(dir);
76 dir = 0;
77 errno = ENOMEM;
80 else
82 errno = EINVAL;
85 return dir;
88 int closedir(DIR *dir)
90 int result = -1;
92 if(dir)
94 if(dir->handle != -1)
96 result = _findclose(dir->handle);
99 free(dir->name);
100 free(dir);
103 if(result == -1) /* map all errors to EBADF */
105 errno = EBADF;
108 return result;
111 struct dirent *readdir(DIR *dir)
113 struct dirent *result = 0;
115 if(dir && dir->handle != -1)
117 if(!dir->result.d_name) {
118 result = &dir->result;
119 result->d_name = dir->info.name;
120 } else {
121 int orig_errno = errno;
122 if (_findnext(dir->handle, &dir->info) != -1) {
123 result = &dir->result;
124 result->d_name = dir->info.name;
125 } else if (errno == ENOENT) {
126 // _findnext sets errno to ENOENT when the end of the directory
127 // is reached. However, according to POSIX, the value of errno
128 // should not be changed by this condition. Therefore, we have
129 // to set it back to the original value.
130 errno = orig_errno;
134 else
136 errno = EBADF;
139 return result;
142 void rewinddir(DIR *dir)
144 if(dir && dir->handle != -1)
146 _findclose(dir->handle);
147 dir->handle = (long) _findfirst(dir->name, &dir->info);
148 dir->result.d_name = 0;
150 else
152 errno = EBADF;
156 #ifdef __cplusplus
158 #endif
160 #endif