2
|
1 |
/*
|
|
2 |
* Copyright 1995-2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* Posix-compatible directory access routines
|
|
28 |
*/
|
|
29 |
|
|
30 |
#include <windows.h>
|
|
31 |
#include <direct.h> /* For _getdrive() */
|
|
32 |
#include <errno.h>
|
|
33 |
#include <assert.h>
|
|
34 |
|
|
35 |
#include "dirent_md.h"
|
|
36 |
|
|
37 |
|
|
38 |
/* Caller must have already run dirname through JVM_NativePath, which removes
|
|
39 |
duplicate slashes and converts all instances of '/' into '\\'. */
|
|
40 |
|
|
41 |
DIR *
|
|
42 |
opendir(const char *dirname)
|
|
43 |
{
|
|
44 |
DIR *dirp = (DIR *)malloc(sizeof(DIR));
|
|
45 |
DWORD fattr;
|
|
46 |
char alt_dirname[4] = { 0, 0, 0, 0 };
|
|
47 |
|
|
48 |
if (dirp == 0) {
|
|
49 |
errno = ENOMEM;
|
|
50 |
return 0;
|
|
51 |
}
|
|
52 |
|
|
53 |
/*
|
|
54 |
* Win32 accepts "\" in its POSIX stat(), but refuses to treat it
|
|
55 |
* as a directory in FindFirstFile(). We detect this case here and
|
|
56 |
* prepend the current drive name.
|
|
57 |
*/
|
|
58 |
if (dirname[1] == '\0' && dirname[0] == '\\') {
|
|
59 |
alt_dirname[0] = _getdrive() + 'A' - 1;
|
|
60 |
alt_dirname[1] = ':';
|
|
61 |
alt_dirname[2] = '\\';
|
|
62 |
alt_dirname[3] = '\0';
|
|
63 |
dirname = alt_dirname;
|
|
64 |
}
|
|
65 |
|
|
66 |
dirp->path = (char *)malloc(strlen(dirname) + 5);
|
|
67 |
if (dirp->path == 0) {
|
|
68 |
free(dirp);
|
|
69 |
errno = ENOMEM;
|
|
70 |
return 0;
|
|
71 |
}
|
|
72 |
strcpy(dirp->path, dirname);
|
|
73 |
|
|
74 |
fattr = GetFileAttributes(dirp->path);
|
|
75 |
if (fattr == ((DWORD)-1)) {
|
|
76 |
free(dirp->path);
|
|
77 |
free(dirp);
|
|
78 |
errno = ENOENT;
|
|
79 |
return 0;
|
|
80 |
} else if ((fattr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
|
|
81 |
free(dirp->path);
|
|
82 |
free(dirp);
|
|
83 |
errno = ENOTDIR;
|
|
84 |
return 0;
|
|
85 |
}
|
|
86 |
|
|
87 |
/* Append "*.*", or possibly "\\*.*", to path */
|
|
88 |
if (dirp->path[1] == ':'
|
|
89 |
&& (dirp->path[2] == '\0'
|
|
90 |
|| (dirp->path[2] == '\\' && dirp->path[3] == '\0'))) {
|
|
91 |
/* No '\\' needed for cases like "Z:" or "Z:\" */
|
|
92 |
strcat(dirp->path, "*.*");
|
|
93 |
} else {
|
|
94 |
strcat(dirp->path, "\\*.*");
|
|
95 |
}
|
|
96 |
|
|
97 |
dirp->handle = FindFirstFile(dirp->path, &dirp->find_data);
|
|
98 |
if (dirp->handle == INVALID_HANDLE_VALUE) {
|
|
99 |
if (GetLastError() != ERROR_FILE_NOT_FOUND) {
|
|
100 |
free(dirp->path);
|
|
101 |
free(dirp);
|
|
102 |
errno = EACCES;
|
|
103 |
return 0;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
return dirp;
|
|
107 |
}
|
|
108 |
|
|
109 |
struct dirent *
|
|
110 |
readdir(DIR *dirp)
|
|
111 |
{
|
|
112 |
if (dirp->handle == INVALID_HANDLE_VALUE) {
|
|
113 |
return 0;
|
|
114 |
}
|
|
115 |
|
|
116 |
strcpy(dirp->dirent.d_name, dirp->find_data.cFileName);
|
|
117 |
|
|
118 |
if (!FindNextFile(dirp->handle, &dirp->find_data)) {
|
|
119 |
if (GetLastError() == ERROR_INVALID_HANDLE) {
|
|
120 |
errno = EBADF;
|
|
121 |
return 0;
|
|
122 |
}
|
|
123 |
FindClose(dirp->handle);
|
|
124 |
dirp->handle = INVALID_HANDLE_VALUE;
|
|
125 |
}
|
|
126 |
|
|
127 |
return &dirp->dirent;
|
|
128 |
}
|
|
129 |
|
|
130 |
int
|
|
131 |
closedir(DIR *dirp)
|
|
132 |
{
|
|
133 |
if (dirp->handle != INVALID_HANDLE_VALUE) {
|
|
134 |
if (!FindClose(dirp->handle)) {
|
|
135 |
errno = EBADF;
|
|
136 |
return -1;
|
|
137 |
}
|
|
138 |
dirp->handle = INVALID_HANDLE_VALUE;
|
|
139 |
}
|
|
140 |
free(dirp->path);
|
|
141 |
free(dirp);
|
|
142 |
return 0;
|
|
143 |
}
|
|
144 |
|
|
145 |
void
|
|
146 |
rewinddir(DIR *dirp)
|
|
147 |
{
|
|
148 |
if (dirp->handle != INVALID_HANDLE_VALUE) {
|
|
149 |
FindClose(dirp->handle);
|
|
150 |
}
|
|
151 |
dirp->handle = FindFirstFile(dirp->path, &dirp->find_data);
|
|
152 |
}
|