The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dirent.cpp
Go to the documentation of this file.
1 /*
2 
3  Implementation of POSIX directory browsing functions and types for Win32.
4 
5  Kevlin Henney (mailto:kevlin@acm.org), March 1997.
6 
7  Copyright Kevlin Henney, 1997. All rights reserved.
8 
9  Permission to use, copy, modify, and distribute this software and its
10  documentation for any purpose is hereby granted without fee, provided
11  that this copyright and permissions notice appear in all copies and
12  derivatives, and that no charge may be made for the software and its
13  documentation except to cover cost of distribution.
14 
15  This software is supplied "as is" without express or implied warranty.
16 
17  But that said, if there are any problems please get in touch.
18 
19 */
20 
21 #include <dirent.h>
22 #include <errno.h>
23 #include <io.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
28 {
29  long handle; /* -1 for failed rewind */
30  struct _finddata_t info;
31  struct dirent result; /* d_name null iff first time */
32  char *name; /* NTBS */
33 };
34 
35 SWDLLEXPORT DIR *opendir(const char *name)
36 {
37  DIR *dir = 0;
38 
39  if(name && name[0])
40  {
41  size_t base_length = strlen(name);
42  const char *all = /* the root directory is a special case... */
43  strchr("/\\", name[base_length - 1]) ? "*" : "/*";
44 
45  if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
46  (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
47  {
48  strcat(strcpy(dir->name, name), all);
49 
50  if((dir->handle = _findfirst(dir->name, &dir->info)) != -1)
51  {
52  dir->result.d_name = 0;
53  }
54  else /* rollback */
55  {
56  free(dir->name);
57  free(dir);
58  dir = 0;
59  }
60  }
61  else /* rollback */
62  {
63  free(dir);
64  dir = 0;
65  errno = ENOMEM;
66  }
67  }
68  else
69  {
70  errno = EINVAL;
71  }
72 
73  return dir;
74 }
75 
77 {
78  int result = -1;
79 
80  if(dir)
81  {
82  if(dir->handle != -1)
83  {
84  result = _findclose(dir->handle);
85  }
86 
87  free(dir->name);
88  free(dir);
89  }
90 
91  if(result == -1) /* map all errors to EBADF */
92  {
93  errno = EBADF;
94  }
95 
96  return result;
97 }
98 
100 {
101  struct dirent *result = 0;
102 
103  if(dir && dir->handle != -1)
104  {
105  if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
106  {
107  result = &dir->result;
108  result->d_name = dir->info.name;
109  }
110  }
111  else
112  {
113  errno = EBADF;
114  }
115 
116  return result;
117 }
118 
120 {
121  if(dir && dir->handle != -1)
122  {
123  _findclose(dir->handle);
124  dir->handle = _findfirst(dir->name, &dir->info);
125  dir->result.d_name = 0;
126  }
127  else
128  {
129  errno = EBADF;
130  }
131 }
#define SWDLLEXPORT
Definition: defs.h:171
long handle
Definition: dirent.cpp:29
struct dirent result
Definition: dirent.cpp:31
Definition: dirent.h:24
struct _finddata_t info
Definition: dirent.cpp:30
Definition: dirent.cpp:27
char * malloc()
free(preg->fastmap)
SWDLLEXPORT void rewinddir(DIR *dir)
Definition: dirent.cpp:119
char * d_name
Definition: dirent.h:26
SWDLLEXPORT DIR * opendir(const char *name)
Definition: dirent.cpp:35
SWDLLEXPORT struct dirent * readdir(DIR *dir)
Definition: dirent.cpp:99
SWDLLEXPORT int closedir(DIR *dir)
Definition: dirent.cpp:76
char * name
Definition: dirent.cpp:32
result
Definition: regex.c:5545