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