492bb9296235dadb7f3189dc9e3d1a3dee806a95
[idzebra-moved-to-github.git] / util / dirent.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21
22 #include <ctype.h>
23 #include <assert.h>
24 #ifdef WIN32
25 #include <io.h>
26 #endif
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <direntz.h>
31
32 #ifdef WIN32
33
34 struct DIR {
35     HANDLE handle;
36     WIN32_FIND_DATA find_data;
37     struct dirent entry;
38 };
39
40 DIR *opendir (const char *name)
41 {
42     char fullName[MAX_PATH+1];
43     DIR *dd = malloc (sizeof(*dd));
44
45     if (!dd)
46         return NULL;
47     strcpy (fullName, name);
48     strcat (fullName, "\\*.*");
49     dd->handle = FindFirstFile(fullName, &dd->find_data);
50     return dd;
51 }
52                                                           
53 struct dirent *readdir (DIR *dd)
54 {
55     if (dd->handle == INVALID_HANDLE_VALUE)
56         return NULL;
57     strcpy (dd->entry.d_name, dd->find_data.cFileName);
58     if (!FindNextFile(dd->handle, &dd->find_data))
59     {
60         FindClose (dd->handle);
61         dd->handle = INVALID_HANDLE_VALUE;
62     }
63     return &dd->entry;
64 }
65
66 void closedir(DIR *dd)
67 {
68     if (dd->handle != INVALID_HANDLE_VALUE)
69         FindClose (dd->handle);
70     if (dd)
71         free (dd);
72 }
73
74 #endif
75
76 /*
77  * Local variables:
78  * c-basic-offset: 4
79  * indent-tabs-mode: nil
80  * End:
81  * vim: shiftwidth=4 tabstop=8 expandtab
82  */
83