908a4505947222d5072547cb8f4ee6fc33dd3a96
[idzebra-moved-to-github.git] / util / dirent.c
1 /*
2  * Copyright (C) 1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dirent.c,v $
7  * Revision 1.2  1997-09-17 12:19:24  adam
8  * Zebra version corresponds to YAZ version 1.4.
9  * Changed Zebra server so that it doesn't depend on global common_resource.
10  *
11  * 
12  *
13  * This utility implements a opendir/readdir/close on Windows.
14  */
15
16 #ifdef WINDOWS
17 #include <assert.h>
18 #include <io.h>
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include <direntz.h>
23
24 struct DIR {
25     HANDLE handle;
26     WIN32_FIND_DATA find_data;
27     struct dirent entry;
28 };
29
30 DIR *opendir (const char *name)
31 {
32     char fullName[MAX_PATH+1];
33     DIR *dd = malloc (sizeof(*dd));
34
35     if (!dd)
36         return NULL;
37     strcpy (fullName, name);
38     strcat (fullName, "\\*.*");
39     dd->handle = FindFirstFile(fullName, &dd->find_data);
40     return dd;
41 }
42                                                           
43 struct dirent *readdir (DIR *dd)
44 {
45     if (dd->handle == INVALID_HANDLE_VALUE)
46         return NULL;
47     strcpy (dd->entry.d_name, dd->find_data.cFileName);
48     if (!FindNextFile(dd->handle, &dd->find_data))
49     {
50         FindClose (dd->handle);
51         dd->handle = INVALID_HANDLE_VALUE;
52     }
53     return &dd->entry;
54 }
55
56 void closedir(DIR *dd)
57 {
58     if (dd->handle != INVALID_HANDLE_VALUE)
59         FindClose (dd->handle);
60     if (dd)
61         free (dd);
62 }
63
64 #endif