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