Commit | Line | Data |
---|---|---|
8dd6dfdc MV |
1 | /* Copyright (C) 2001 Free Software Foundation, Inc. |
2 | * | |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
8dd6dfdc | 7 | * |
73be1d9e MV |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
8dd6dfdc | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
8dd6dfdc MV |
17 | |
18 | #include "libguile/__scm.h" | |
19 | ||
20 | #include <windows.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | ||
21e11a3b | 24 | #include "win32-dirent.h" |
8dd6dfdc MV |
25 | |
26 | DIR * | |
27 | opendir (const char * name) | |
28 | { | |
29 | DIR *dir; | |
30 | HANDLE hnd; | |
31 | char *file; | |
32 | WIN32_FIND_DATA find; | |
33 | ||
34 | if (!name || !*name) | |
35 | return NULL; | |
67329a9e | 36 | file = scm_malloc (strlen (name) + 3); |
8dd6dfdc MV |
37 | strcpy (file, name); |
38 | if (file[strlen (name) - 1] != '/' && file[strlen (name) - 1] != '\\') | |
39 | strcat (file, "/*"); | |
40 | else | |
41 | strcat (file, "*"); | |
42 | ||
43 | if ((hnd = FindFirstFile (file, &find)) == INVALID_HANDLE_VALUE) | |
44 | { | |
45 | free (file); | |
46 | return NULL; | |
47 | } | |
48 | ||
67329a9e | 49 | dir = scm_malloc (sizeof (DIR)); |
8dd6dfdc MV |
50 | dir->mask = file; |
51 | dir->fd = (int) hnd; | |
67329a9e | 52 | dir->data = scm_malloc (sizeof (WIN32_FIND_DATA)); |
8dd6dfdc MV |
53 | dir->allocation = sizeof (WIN32_FIND_DATA); |
54 | dir->size = dir->allocation; | |
55 | dir->filepos = 0; | |
56 | memcpy (dir->data, &find, sizeof (WIN32_FIND_DATA)); | |
57 | return dir; | |
58 | } | |
59 | ||
60 | struct dirent * | |
61 | readdir (DIR * dir) | |
62 | { | |
63 | static struct dirent entry; | |
64 | WIN32_FIND_DATA *find; | |
65 | ||
66 | entry.d_ino = 0; | |
67 | entry.d_type = 0; | |
68 | find = (WIN32_FIND_DATA *) dir->data; | |
69 | ||
70 | if (dir->filepos) | |
71 | { | |
72 | if (!FindNextFile ((HANDLE) dir->fd, find)) | |
73 | return NULL; | |
74 | } | |
75 | ||
76 | entry.d_off = dir->filepos; | |
77 | strncpy (entry.d_name, find->cFileName, sizeof (entry.d_name)); | |
78 | entry.d_reclen = strlen (find->cFileName); | |
79 | dir->filepos++; | |
80 | return &entry; | |
81 | } | |
82 | ||
83 | int | |
84 | closedir (DIR * dir) | |
85 | { | |
86 | HANDLE hnd = (HANDLE) dir->fd; | |
87 | free (dir->data); | |
88 | free (dir->mask); | |
89 | free (dir); | |
90 | return FindClose (hnd) ? 0 : -1; | |
91 | } | |
92 | ||
93 | void | |
94 | rewinddir (DIR * dir) | |
95 | { | |
96 | HANDLE hnd = (HANDLE) dir->fd; | |
97 | WIN32_FIND_DATA *find = (WIN32_FIND_DATA *) dir->data; | |
98 | ||
99 | FindClose (hnd); | |
100 | hnd = FindFirstFile (dir->mask, find); | |
101 | dir->fd = (int) hnd; | |
102 | dir->filepos = 0; | |
103 | } | |
104 | ||
105 | void | |
106 | seekdir (DIR * dir, off_t offset) | |
107 | { | |
108 | off_t n; | |
109 | ||
110 | rewinddir (dir); | |
111 | for (n = 0; n < offset; n++) | |
112 | { | |
113 | if (FindNextFile ((HANDLE) dir->fd, (WIN32_FIND_DATA *) dir->data)) | |
114 | dir->filepos++; | |
115 | } | |
116 | } | |
117 | ||
118 | off_t | |
119 | telldir (DIR * dir) | |
120 | { | |
121 | return dir->filepos; | |
122 | } | |
123 | ||
124 | int | |
125 | dirfd (DIR * dir) | |
126 | { | |
127 | return dir->fd; | |
128 | } |