The FSF has a new address.
[bpt/guile.git] / libguile / win32-dirent.c
CommitLineData
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 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
26DIR *
27opendir (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;
2e945bcc 36 file = 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
2e945bcc 49 dir = malloc (sizeof (DIR));
8dd6dfdc
MV
50 dir->mask = file;
51 dir->fd = (int) hnd;
2e945bcc 52 dir->data = 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
60struct dirent *
61readdir (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
83int
84closedir (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
93void
94rewinddir (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
105void
106seekdir (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
118off_t
119telldir (DIR * dir)
120{
121 return dir->filepos;
122}
123
124int
125dirfd (DIR * dir)
126{
127 return dir->fd;
128}