]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/win32-dirent.c
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / libguile / win32-dirent.c
1 /* Copyright (C) 2001, 2006, 2008 Free Software Foundation, Inc.
2  * 
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.
7  *
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.
12  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include "libguile/__scm.h"
23
24 #include <windows.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "win32-dirent.h"
29
30 DIR *
31 opendir (const char * name)
32 {
33   DIR *dir;
34   HANDLE hnd;
35   char *file;
36   WIN32_FIND_DATA find;
37
38   if (!name || !*name) 
39     return NULL;
40   file = malloc (strlen (name) + 3);
41   strcpy (file, name);
42   if (file[strlen (name) - 1] != '/' && file[strlen (name) - 1] != '\\')
43     strcat (file, "/*");
44   else
45     strcat (file, "*");
46   
47   if ((hnd = FindFirstFile (file, &find)) == INVALID_HANDLE_VALUE)
48     {
49       free (file);
50       return NULL;
51     }
52
53   dir = malloc (sizeof (DIR));
54   dir->mask = file;
55   dir->fd = (int) hnd;
56   dir->data = malloc (sizeof (WIN32_FIND_DATA));
57   dir->allocation = sizeof (WIN32_FIND_DATA);
58   dir->size = dir->allocation;
59   dir->filepos = 0;
60   memcpy (dir->data, &find, sizeof (WIN32_FIND_DATA));
61   return dir;
62 }
63
64 struct dirent *
65 readdir (DIR * dir)
66 {
67   static struct dirent entry;
68   WIN32_FIND_DATA *find;
69
70   entry.d_ino = 0;
71   entry.d_type = 0;
72   find = (WIN32_FIND_DATA *) dir->data;
73
74   if (dir->filepos)
75     {
76       if (!FindNextFile ((HANDLE) dir->fd, find))
77         return NULL;
78     }
79
80   entry.d_off = dir->filepos;
81   strncpy (entry.d_name, find->cFileName, sizeof (entry.d_name));
82   entry.d_reclen = strlen (find->cFileName);
83   dir->filepos++;
84   return &entry;
85 }
86
87 int 
88 closedir (DIR * dir)
89 {
90   HANDLE hnd = (HANDLE) dir->fd;
91   free (dir->data);
92   free (dir->mask);
93   free (dir);
94   return FindClose (hnd) ? 0 : -1;
95 }
96
97 void 
98 rewinddir (DIR * dir)
99 {
100   HANDLE hnd = (HANDLE) dir->fd;
101   WIN32_FIND_DATA *find = (WIN32_FIND_DATA *) dir->data;
102
103   FindClose (hnd);
104   hnd = FindFirstFile (dir->mask, find);
105   dir->fd = (int) hnd;
106   dir->filepos = 0;
107 }
108
109 void 
110 seekdir (DIR * dir, off_t offset)
111 {
112   off_t n;
113
114   rewinddir (dir);
115   for (n = 0; n < offset; n++)
116     {
117       if (FindNextFile ((HANDLE) dir->fd, (WIN32_FIND_DATA *) dir->data))
118         dir->filepos++;
119     }
120 }
121
122 off_t 
123 telldir (DIR * dir)
124 {
125   return dir->filepos;
126 }
127
128 int 
129 dirfd (DIR * dir)
130 {
131   return dir->fd;
132 }