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