]> granicus.if.org Git - postgresql/blob - src/port/dirent.c
Remove cvs keywords from all files.
[postgresql] / src / port / dirent.c
1 /*-------------------------------------------------------------------------
2  *
3  * dirent.c
4  *        opendir/readdir/closedir for win32/msvc
5  *
6  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/port/dirent.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17 #include <dirent.h>
18
19
20 struct DIR
21 {
22         char       *dirname;
23         struct dirent ret;                      /* Used to return to caller */
24         HANDLE          handle;
25 };
26
27 DIR *
28 opendir(const char *dirname)
29 {
30         DWORD           attr;
31         DIR                *d;
32
33         /* Make sure it is a directory */
34         attr = GetFileAttributes(dirname);
35         if (attr == INVALID_FILE_ATTRIBUTES)
36         {
37                 errno = ENOENT;
38                 return NULL;
39         }
40         if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
41         {
42                 errno = ENOTDIR;
43                 return NULL;
44         }
45
46         d = malloc(sizeof(DIR));
47         if (!d)
48         {
49                 errno = ENOMEM;
50                 return NULL;
51         }
52         d->dirname = malloc(strlen(dirname) + 4);
53         if (!d->dirname)
54         {
55                 errno = ENOMEM;
56                 free(d);
57                 return NULL;
58         }
59         strcpy(d->dirname, dirname);
60         if (d->dirname[strlen(d->dirname) - 1] != '/' &&
61                 d->dirname[strlen(d->dirname) - 1] != '\\')
62                 strcat(d->dirname, "\\");               /* Append backslash if not already
63                                                                                  * there */
64         strcat(d->dirname, "*");        /* Search for entries named anything */
65         d->handle = INVALID_HANDLE_VALUE;
66         d->ret.d_ino = 0;                       /* no inodes on win32 */
67         d->ret.d_reclen = 0;            /* not used on win32 */
68
69         return d;
70 }
71
72 struct dirent *
73 readdir(DIR *d)
74 {
75         WIN32_FIND_DATA fd;
76
77         if (d->handle == INVALID_HANDLE_VALUE)
78         {
79                 d->handle = FindFirstFile(d->dirname, &fd);
80                 if (d->handle == INVALID_HANDLE_VALUE)
81                 {
82                         errno = ENOENT;
83                         return NULL;
84                 }
85         }
86         else
87         {
88                 if (!FindNextFile(d->handle, &fd))
89                 {
90                         if (GetLastError() == ERROR_NO_MORE_FILES)
91                         {
92                                 /* No more files, force errno=0 (unlike mingw) */
93                                 errno = 0;
94                                 return NULL;
95                         }
96                         _dosmaperr(GetLastError());
97                         return NULL;
98                 }
99         }
100         strcpy(d->ret.d_name, fd.cFileName);            /* Both strings are MAX_PATH
101                                                                                                  * long */
102         d->ret.d_namlen = strlen(d->ret.d_name);
103         return &d->ret;
104 }
105
106 int
107 closedir(DIR *d)
108 {
109         if (d->handle != INVALID_HANDLE_VALUE)
110                 FindClose(d->handle);
111         free(d->dirname);
112         free(d);
113         return 0;
114 }