Skip to content

Commit 4207d6b

Browse files
committed
Add opendir/readdir/closedir() for Win32.
Keep SRA copyright on file because it contains BSD license clause.
1 parent 45d0409 commit 4207d6b

File tree

4 files changed

+153
-4
lines changed

4 files changed

+153
-4
lines changed

configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11257,7 +11257,8 @@ esac
1125711257
1125811258
# Win32 can't to rename or unlink on an open file
1125911259
case $host_os in win32*)
11260-
LIBOBJS="$LIBOBJS dirmod.$ac_objext" ;;
11260+
LIBOBJS="$LIBOBJS dirmod.$ac_objext"
11261+
LIBOBJS="$LIBOBJS opendir.$ac_objext" ;;
1126111262
esac
1126211263
1126311264
# On HPUX 9, rint() is not in regular libm.a but in /lib/pa1.1/libm.a;

configure.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dnl Process this file with autoconf to produce a configure script.
2-
dnl $Header: /cvsroot/pgsql/configure.in,v 1.245 2003/05/07 03:47:08 momjian Exp $
2+
dnl $Header: /cvsroot/pgsql/configure.in,v 1.246 2003/05/09 01:16:29 momjian Exp $
33
dnl
44
dnl Developers, please strive to achieve this order:
55
dnl
@@ -863,7 +863,8 @@ esac
863863

864864
# Win32 can't to rename or unlink on an open file
865865
case $host_os in win32*)
866-
AC_LIBOBJ(dirmod) ;;
866+
AC_LIBOBJ(dirmod)
867+
AC_LIBOBJ(opendir) ;;
867868
esac
868869

869870
# On HPUX 9, rint() is not in regular libm.a but in /lib/pa1.1/libm.a;

src/include/c.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $Id: c.h,v 1.141 2003/04/25 16:18:40 momjian Exp $
15+
* $Id: c.h,v 1.142 2003/05/09 01:16:29 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -721,6 +721,31 @@ int pgunlink(const char *path);
721721
#define unlink(from, to) pgunlink(from, to)
722722
#endif
723723

724+
/*
725+
* Win32 doesn't have opendir/readdir/closedir()
726+
*/
727+
#ifdef WIN32
728+
struct dirent {
729+
ino_t d_ino; /* inode (always 1 on WIN32) */
730+
char d_name[MAX_PATH + 1]; /* filename (null terminated) */
731+
};
732+
733+
typedef struct {
734+
HANDLE handle; /* handle for FindFirstFile or
735+
* FindNextFile */
736+
long offset; /* offset into directory */
737+
int finished; /* 1 if there are not more files */
738+
WIN32_FIND_DATA finddata; /* file data FindFirstFile or FindNextFile
739+
* returns */
740+
char *dir; /* the directory path we are reading */
741+
struct dirent ent; /* the dirent to return */
742+
} DIR;
743+
744+
extern DIR *opendir(const char *);
745+
extern struct dirent *readdir(DIR *);
746+
extern int closedir(DIR *);
747+
#endif
748+
724749
/*
725750
* Win32 requires a special close for sockets and pipes, while on Unix
726751
* close() does them all.

src/port/opendir.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* $Header: /cvsroot/pgsql/src/port/Attic/opendir.c,v 1.1 2003/05/09 01:16:29 momjian Exp $
3+
*
4+
* Copyright (c) 2003 SRA, Inc.
5+
* Copyright (c) 2003 SKC, Inc.
6+
*
7+
* Permission to use, copy, modify, and distribute this software and
8+
* its documentation for any purpose, without fee, and without a
9+
* written agreement is hereby granted, provided that the above
10+
* copyright notice and this paragraph and the following two
11+
* paragraphs appear in all copies.
12+
*
13+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
14+
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
15+
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
16+
* DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
17+
* OF THE POSSIBILITY OF SUCH DAMAGE.
18+
*
19+
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
22+
* IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
23+
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24+
*/
25+
26+
#include "postgres.h"
27+
28+
#include <stddef.h>
29+
#include <sys/types.h>
30+
#include <windows.h>
31+
32+
#include "dirent.h"
33+
34+
DIR *
35+
opendir(const char *dir)
36+
{
37+
DIR *dp;
38+
char *findspec;
39+
HANDLE handle;
40+
size_t dirlen;
41+
42+
dirlen = strlen(dir);
43+
findspec = palloc(dirlen + 2 + 1);
44+
if (findspec == NULL)
45+
return NULL;
46+
47+
if (dirlen == 0)
48+
strcpy(findspec, "*");
49+
else if (isalpha(dir[0]) && dir[1] == ':' && dir[2] == '\0')
50+
sprintf(findspec, "%s*", dir);
51+
else if (dir[dirlen - 1] == '/' || dir[dirlen - 1] == '\\')
52+
sprintf(findspec, "%s*", dir);
53+
else
54+
sprintf(findspec, "%s\\*", dir);
55+
56+
dp = (DIR *)palloc(sizeof(DIR));
57+
if (dp == NULL)
58+
{
59+
pfree(findspec);
60+
errno = ENOMEM;
61+
return NULL;
62+
}
63+
64+
dp->offset = 0;
65+
dp->finished = 0;
66+
dp->dir = pstrdup(dir);
67+
if (dp->dir == NULL)
68+
{
69+
pfree(dp);
70+
pfree(findspec);
71+
errno = ENOMEM;
72+
return NULL;
73+
}
74+
75+
handle = FindFirstFile(findspec, &(dp->finddata));
76+
if (handle == INVALID_HANDLE_VALUE)
77+
{
78+
pfree(dp->dir);
79+
pfree(dp);
80+
pfree(findspec);
81+
errno = ENOENT;
82+
return NULL;
83+
}
84+
dp->handle = handle;
85+
86+
pfree(findspec);
87+
return dp;
88+
}
89+
90+
91+
struct dirent *
92+
readdir(DIR *dp)
93+
{
94+
if (dp == NULL || dp->finished)
95+
return NULL;
96+
97+
if (dp->offset != 0)
98+
{
99+
if (FindNextFile(dp->handle, &(dp->finddata)) == 0)
100+
{
101+
dp->finished = 1;
102+
return NULL;
103+
}
104+
}
105+
dp->offset++;
106+
107+
strncpy(dp->ent.d_name, dp->finddata.cFileName, MAX_PATH);
108+
dp->ent.d_ino = 1;
109+
110+
return &(dp->ent);
111+
}
112+
113+
114+
int
115+
closedir(DIR *dp)
116+
{
117+
FindClose(dp->handle);
118+
pfree(dp->dir);
119+
pfree(dp);
120+
121+
return 0;
122+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy