Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / port / dirent.c
blob4f4e8d4a5326be95c388d0e5ddd5f71cbefab989
1 /*-------------------------------------------------------------------------
3 * dirent.c
4 * opendir/readdir/closedir for win32/msvc
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
16 #include "postgres.h"
17 #include <dirent.h>
20 struct DIR
22 char *dirname;
23 struct dirent ret; /* Used to return to caller */
24 HANDLE handle;
27 DIR *
28 opendir(const char *dirname)
30 DWORD attr;
31 DIR *d;
33 /* Make sure it is a directory */
34 attr = GetFileAttributes(dirname);
35 if (attr == INVALID_FILE_ATTRIBUTES)
37 errno = ENOENT;
38 return NULL;
40 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
42 errno = ENOTDIR;
43 return NULL;
46 d = malloc(sizeof(DIR));
47 if (!d)
49 errno = ENOMEM;
50 return NULL;
52 d->dirname = malloc(strlen(dirname) + 4);
53 if (!d->dirname)
55 errno = ENOMEM;
56 free(d);
57 return NULL;
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 */
69 return d;
72 struct dirent *
73 readdir(DIR *d)
75 WIN32_FIND_DATA fd;
77 if (d->handle == INVALID_HANDLE_VALUE)
79 d->handle = FindFirstFile(d->dirname, &fd);
80 if (d->handle == INVALID_HANDLE_VALUE)
82 errno = ENOENT;
83 return NULL;
86 else
88 if (!FindNextFile(d->handle, &fd))
90 if (GetLastError() == ERROR_NO_MORE_FILES)
92 /* No more files, force errno=0 (unlike mingw) */
93 errno = 0;
94 return NULL;
96 _dosmaperr(GetLastError());
97 return NULL;
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;
107 closedir(DIR *d)
109 if (d->handle != INVALID_HANDLE_VALUE)
110 FindClose(d->handle);
111 free(d->dirname);
112 free(d);
113 return 0;