1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>dirent.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> </center>
<hr><h1>dirent.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/*</font>
00002 <font class="comment"></font>
00003 <font class="comment"> Implementation of POSIX directory browsing functions and types for Win32.</font>
00004 <font class="comment"></font>
00005 <font class="comment"> Kevlin Henney (mailto:kevlin@acm.org), March 1997.</font>
00006 <font class="comment"></font>
00007 <font class="comment"> Copyright Kevlin Henney, 1997. All rights reserved.</font>
00008 <font class="comment"></font>
00009 <font class="comment"> Permission to use, copy, modify, and distribute this software and its</font>
00010 <font class="comment"> documentation for any purpose is hereby granted without fee, provided</font>
00011 <font class="comment"> that this copyright and permissions notice appear in all copies and</font>
00012 <font class="comment"> derivatives, and that no charge may be made for the software and its</font>
00013 <font class="comment"> documentation except to cover cost of distribution.</font>
00014 <font class="comment"> </font>
00015 <font class="comment"> This software is supplied "as is" without express or implied warranty.</font>
00016 <font class="comment"></font>
00017 <font class="comment"> But that said, if there are any problems please get in touch.</font>
00018 <font class="comment"></font>
00019 <font class="comment">*/</font>
00020
00021 <font class="preprocessor">#include <dirent.h></font>
00022 <font class="preprocessor">#include <errno.h></font>
00023 <font class="preprocessor">#include <io.h></font>
00024 <font class="preprocessor">#include <stdlib.h></font>
00025 <font class="preprocessor">#include <string.h></font>
00026
00027 <font class="keyword">struct </font>DIR
00028 {
00029 <font class="keywordtype">long</font> handle; <font class="comment">/* -1 for failed rewind */</font>
00030 <font class="keyword">struct </font>_finddata_t info;
00031 <font class="keyword">struct </font>dirent result; <font class="comment">/* d_name null iff first time */</font>
00032 <font class="keywordtype">char</font> *name; <font class="comment">/* NTBS */</font>
00033 };
00034
00035 DIR *opendir(<font class="keyword">const</font> <font class="keywordtype">char</font> *name)
00036 {
00037 DIR *dir = 0;
00038
00039 <font class="keywordflow">if</font>(name && name[0])
00040 {
00041 size_t base_length = strlen(name);
00042 <font class="keyword">const</font> <font class="keywordtype">char</font> *all = <font class="comment">/* the root directory is a special case... */</font>
00043 strchr(<font class="stringliteral">"/\\"</font>, name[base_length - 1]) ? <font class="stringliteral">"*"</font> : <font class="stringliteral">"/*"</font>;
00044
00045 <font class="keywordflow">if</font>((dir = (DIR *) malloc(<font class="keyword">sizeof</font> *dir)) != 0 &&
00046 (dir->name = (<font class="keywordtype">char</font> *) malloc(base_length + strlen(all) + 1)) != 0)
00047 {
00048 strcat(strcpy(dir->name, name), all);
00049
00050 <font class="keywordflow">if</font>((dir->handle = _findfirst(dir->name, &dir->info)) != -1)
00051 {
00052 dir->result.d_name = 0;
00053 }
00054 <font class="keywordflow">else</font> <font class="comment">/* rollback */</font>
00055 {
00056 free(dir->name);
00057 free(dir);
00058 dir = 0;
00059 }
00060 }
00061 <font class="keywordflow">else</font> <font class="comment">/* rollback */</font>
00062 {
00063 free(dir);
00064 dir = 0;
00065 errno = ENOMEM;
00066 }
00067 }
00068 <font class="keywordflow">else</font>
00069 {
00070 errno = EINVAL;
00071 }
00072
00073 <font class="keywordflow">return</font> dir;
00074 }
00075
00076 <font class="keywordtype">int</font> closedir(DIR *dir)
00077 {
00078 <font class="keywordtype">int</font> result = -1;
00079
00080 <font class="keywordflow">if</font>(dir)
00081 {
00082 <font class="keywordflow">if</font>(dir->handle != -1)
00083 {
00084 result = _findclose(dir->handle);
00085 }
00086
00087 free(dir->name);
00088 free(dir);
00089 }
00090
00091 <font class="keywordflow">if</font>(result == -1) <font class="comment">/* map all errors to EBADF */</font>
00092 {
00093 errno = EBADF;
00094 }
00095
00096 <font class="keywordflow">return</font> result;
00097 }
00098
00099 <font class="keyword">struct </font>dirent *readdir(DIR *dir)
00100 {
00101 <font class="keyword">struct </font>dirent *result = 0;
00102
00103 <font class="keywordflow">if</font>(dir && dir->handle != -1)
00104 {
00105 <font class="keywordflow">if</font>(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
00106 {
00107 result = &dir->result;
00108 result->d_name = dir->info.name;
00109 }
00110 }
00111 <font class="keywordflow">else</font>
00112 {
00113 errno = EBADF;
00114 }
00115
00116 <font class="keywordflow">return</font> result;
00117 }
00118
00119 <font class="keywordtype">void</font> rewinddir(DIR *dir)
00120 {
00121 <font class="keywordflow">if</font>(dir && dir->handle != -1)
00122 {
00123 _findclose(dir->handle);
00124 dir->handle = _findfirst(dir->name, &dir->info);
00125 dir->result.d_name = 0;
00126 }
00127 <font class="keywordflow">else</font>
00128 {
00129 errno = EBADF;
00130 }
00131 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:12:58 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>
|