Index: trunk/src/emx/Makefile
===================================================================
--- trunk/src/emx/Makefile	(revision 729)
+++ trunk/src/emx/Makefile	(revision 732)
@@ -77,5 +77,5 @@
 # The C compiler flags
 CFLAGS.INC  += -Iinclude -Isrc/include
-CFLAGS      = -Wall -mstack-arg-probe $(CFLAGS.INC) $(CFLAGS.$(MODE)) $(CFLAGS.KIND)
+CFLAGS      = -Wall -Wmissing-prototypes -mstack-arg-probe $(CFLAGS.INC) $(CFLAGS.$(MODE)) $(CFLAGS.KIND)
 # The additional C compiler flags for different build modes
 CFLAGS.opt  = -g -O3
Index: trunk/src/emx/bsd/db/include/sys/mman.h
===================================================================
--- trunk/src/emx/bsd/db/include/sys/mman.h	(revision 729)
+++ trunk/src/emx/bsd/db/include/sys/mman.h	(revision 732)
@@ -83,5 +83,8 @@
 __BEGIN_DECLS
 /* Some of these int's should probably be size_t's */
+#ifndef _MMAP_DECLARED
+#define	_MMAP_DECLARED
 caddr_t	mmap __P((caddr_t, size_t, int, int, int, off_t));
+#endif
 int	mprotect __P((caddr_t, int, int));
 int	munmap __P((caddr_t, int));
Index: trunk/src/emx/include/386/_limits.h
===================================================================
--- trunk/src/emx/include/386/_limits.h	(revision 729)
+++ trunk/src/emx/include/386/_limits.h	(revision 732)
@@ -90,5 +90,5 @@
 
 /* bird: our off_t is 32 bit signed at the moment. */
-#if 0
+#if 1
 #define	__OFF_MAX	__LLONG_MAX	/* max value for an off_t */
 #define	__OFF_MIN	__LLONG_MIN	/* min value for an off_t */
Index: trunk/src/emx/include/386/ansi.h
===================================================================
--- trunk/src/emx/include/386/ansi.h	(revision 729)
+++ trunk/src/emx/include/386/ansi.h	(revision 732)
@@ -75,5 +75,5 @@
  */
 #define	_BSD_CT_RUNE_T_	int			/* arg type for ctype funcs */
-#if 0
+#if 1
 #define	_BSD_OFF_T_	__int64_t		/* file offset */
 #else
Index: trunk/src/emx/include/dirent.h
===================================================================
--- trunk/src/emx/include/dirent.h	(revision 729)
+++ trunk/src/emx/include/dirent.h	(revision 732)
@@ -1,32 +1,148 @@
-/* dirent.h (emx+gcc) */
+/*-
+ * Copyright (c) 1989, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)dirent.h	8.2 (Berkeley) 7/28/94
+ * $FreeBSD: src/include/dirent.h,v 1.13 2002/09/10 18:12:16 mike Exp $
+ */
 
-#ifndef _DIRENT_H
-#define _DIRENT_H
+/** @file
+ * FreeBSD 5.1
+ * @changes bird: Merged in EMX stuff and internal LIBC stuff.
+ */
 
-#if !defined (_SYS_TYPES_H)
-#warning <dirent.h> requires <sys/types.h>
-#include <sys/types.h>
+#ifndef _DIRENT_H_
+#define _DIRENT_H_
+
+/*
+ * The kernel defines the format of directory entries returned by
+ * the getdirentries(2) system call.
+ */
+#include <sys/cdefs.h>
+#include <sys/dirent.h>
+
+#if __BSD_VISIBLE || __XSI_VISIBLE
+/*
+ * XXX this is probably illegal in the __XSI_VISIBLE case, but brings us closer
+ * to the specification.
+ */
+#define	d_ino		d_fileno	/* backward and XSI compatibility */
 #endif
 
-#include <sys/dirent.h>
+#if __BSD_VISIBLE
 
-#if defined (__cplusplus)
-extern "C" {
+/* definitions for library routines operating on directories. */
+#define	DIRBLKSIZ	1024
+
+struct _telldir;		/* see telldir.h */
+
+/* bird: EMX internal structure. */
+struct _dircontents
+{
+  struct _dircontents * _d_next;
+  char *                _d_entry;
+  __off_t               _d_size;
+  unsigned short        _d_time;
+  unsigned short        _d_date;
+  unsigned short        _d_attr;
+  unsigned char         _d_type;
+};
+
+/* structure describing an open directory. */
+typedef struct _dirdesc {
+#if 0
+	int	dd_fd;		/* file descriptor associated with directory */
+	long	dd_loc;		/* offset in current buffer */
+	long	dd_size;	/* amount of data returned by getdirentries */
+	char	*dd_buf;	/* data buffer */
+	int	dd_len;		/* size of data buffer */
+	long	dd_seek;	/* magic cookie returned by getdirentries */
+	long	dd_rewind;	/* magic cookie for rewinding */
+	int	dd_flags;	/* flags for readdir */
+	void	*dd_lock;	/* hack to avoid including <pthread.h> */
+	struct _telldir *dd_td;	/* telldir position recording */
+#else
+        int                   dd_id;
+        long                  dd_loc;
+        struct _dircontents * dd_contents;
+        struct _dircontents * dd_cp;
+        struct dirent         dd_dirent;
+#endif
+} DIR;
+
+#define	dirfd(dirp)	((dirp)->dd_fd)
+
+/* flags for opendir2 */
+#define DTF_HIDEW	0x0001	/* hide whiteout entries */
+#define DTF_NODUP	0x0002	/* don't return duplicate names */
+#define DTF_REWIND	0x0004	/* rewind after reading union stack */
+#define __DTF_READALL	0x0008	/* everything has been read */
+
+#ifndef NULL
+#define	NULL	0
 #endif
 
-struct _dirdesc;
+#else /* !__BSD_VISIBLE */
 
-typedef struct _dirdesc DIR;
+typedef	void *	DIR;
 
-DIR *opendir (__const__ char *);
-struct dirent *readdir (DIR *);
-int closedir (DIR *);
-void rewinddir (DIR *);
+#endif /* __BSD_VISIBLE */
 
-#if !defined (_POSIX_SOURCE)
-void seekdir (DIR *, long);
-long telldir (DIR *);
+#ifndef _KERNEL
+
+__BEGIN_DECLS
+#if __BSD_VISIBLE
+/** @todo DIR	*__opendir2(const char *, int); */
+/** @todo int	 alphasort(const void *, const void *); */
+/** @todo int	 getdents(int, char *, int); */
+/** @todo int	 getdirentries(int, char *, int, long *); */
 #endif
+DIR	*opendir(const char *);
+struct dirent *
+	 readdir(DIR *);
+#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE >= 500
+int	 readdir_r(DIR *, struct dirent *, struct dirent **);
+#endif
+void	 rewinddir(DIR *);
+#if __BSD_VISIBLE
+int	 scandir(const char *, struct dirent ***,
+	    int (*)(struct dirent *), int (*)(const void *, const void *));
+#endif
+#if __XSI_VISIBLE
+void	 seekdir(DIR *, long);
+long	 telldir(DIR *);
+#endif
+int	 closedir(DIR *);
 
+
+/* bird: EMX extra - start */
 DIR *_opendir (__const__ char *);
 struct dirent *_readdir (DIR *);
@@ -35,8 +151,9 @@
 int _closedir (DIR *);
 void _rewinddir (DIR *);
+/* bird: EMX extra - end */
 
-#if defined (__cplusplus)
-}
-#endif
+__END_DECLS
 
-#endif /* not _DIRENT_H */
+#endif /* !_KERNEL */
+
+#endif /* !_DIRENT_H_ */
Index: trunk/src/emx/include/emx/io.h
===================================================================
--- trunk/src/emx/include/emx/io.h	(revision 729)
+++ trunk/src/emx/include/emx/io.h	(revision 732)
@@ -7,4 +7,6 @@
 extern "C" {
 #endif
+
+#include <sys/types.h>
 
 #if !defined (NULL)
@@ -14,9 +16,4 @@
 #define NULL ((void *)0)
 #endif
-#endif
-
-#if !defined (_SIZE_T)
-#define _SIZE_T
-typedef unsigned long size_t;
 #endif
 
@@ -217,6 +214,6 @@
 void _closestream (struct _FILE *);
 int _fflush_nolock (struct _FILE *);
-int _fseek_nolock (struct _FILE *, long, int);
-long _ftell_nolock (struct _FILE *);
+int _fseek_nolock (struct _FILE *, off_t, int);
+off_t _ftell_nolock (struct _FILE *);
 size_t _fwrite_nolock (const void *, size_t, size_t, struct _FILE *);
 int _input (struct _FILE *, __const__ char *, char *);
Index: trunk/src/emx/include/emx/syscalls.h
===================================================================
--- trunk/src/emx/include/emx/syscalls.h	(revision 729)
+++ trunk/src/emx/include/emx/syscalls.h	(revision 732)
@@ -3,4 +3,6 @@
 #ifndef _EMX_SYSCALLS_H
 #define _EMX_SYSCALLS_H
+
+#include <sys/types.h>
 
 #if defined (__cplusplus)
@@ -37,10 +39,4 @@
 typedef unsigned long sigset_t;
 #endif
-
-#if !defined (_SIZE_T)
-#define _SIZE_T
-typedef unsigned long size_t;
-#endif
-
 
 struct hostent;
@@ -54,14 +50,19 @@
 struct timeval;
 
+#pragma pack(1)
 struct _find
 {
-  char           reserved[21];
-  unsigned char  attr;
-  unsigned short time;
-  unsigned short date;
-  unsigned short size_lo;         /* split due to alignment problems */
-  unsigned short size_hi;
-  char name[257];                 /* Big buffer for OS/2             */
-};
+    /** Size of file, in number of bytes. */
+    off_t           cbFile;
+    /** Last written (OS/2 & DOS time). */
+    unsigned short  time;
+    /** Last written (OS/2 & DOS date). */
+    unsigned short  date;
+    /** File attributes. */
+    unsigned char   attr;
+    /** File name */
+    char            szName[257];
+};
+#pragma pack()
 
 struct _new_proc
@@ -128,5 +129,5 @@
 int __chmod (__const__ char *name, int flag, int attr);
 int __chdrive (char drive);
-int __chsize (int handle, long length);
+int __chsize (int handle, off_t length);
 long long __clock (void);
 int __close (int handle);
@@ -146,5 +147,5 @@
 int __fsync (int handle);
 void __ftime (struct timeb *ptr);
-int __ftruncate (int handle, long length);
+int __ftruncate (int handle, off_t length);
 int __getcwd (char *buffer, char drive);
 char __getdrive (void);
@@ -175,9 +176,9 @@
 int __kill (int pid, int sig);
 int __listen (int handle, int backlog);
-int __lseek (int handle, long offset, int origin);
+off_t __lseek (int handle, off_t offset, int origin);
 int __memavail (void);
 int __mkdir (__const__ char *name);
 int __newthread (int tid);
-int __open (__const__ char *name, int flags, unsigned long size);
+int __open (__const__ char *name, int flags, off_t size);
 int __pause (void);
 int __pipe (int *two_handles, int pipe_size);
Index: trunk/src/emx/include/io.h
===================================================================
--- trunk/src/emx/include/io.h	(revision 729)
+++ trunk/src/emx/include/io.h	(revision 732)
@@ -49,5 +49,5 @@
 int access (__const__ char *, int);
 int chmod (__const__ char *, int);
-int chsize (int, long);
+int chsize (int, off_t);
 int close (int);
 int creat (const char *, mode_t);
@@ -55,5 +55,5 @@
 int dup2 (int, int);
 int eof (int);
-long filelength (int);
+off_t filelength (int);
 int fstat (int, struct stat *);
 int fsync (int);
@@ -77,5 +77,5 @@
 int sopen (__const__ char *, int, int, ...);
 int stat (__const__ char *, struct stat *);
-long tell (int);
+off_t tell (int);
 #ifndef _TRUNCATE_DECLARED
 #define	_TRUNCATE_DECLARED
@@ -91,5 +91,5 @@
 int _access (__const__ char *, int);
 int _chmod (__const__ char *, int);
-int _chsize (int, long);
+int _chsize (int, off_t);
 int _close (int);
 int _creat (__const__ char *, int);
@@ -98,5 +98,5 @@
 int _dup2 (int, int);
 int _eof (int);
-long _filelength (int);
+off_t _filelength (int);
 int _fstat (int, struct stat *);
 int _fsync (int);
@@ -123,6 +123,6 @@
 int _sopen (__const__ char *, int, int, ...);
 int _stat (__const__ char *, struct stat *);
-long _tell (int);
-int _truncate (char *, long);
+off_t _tell (int);
+int _truncate (char *, off_t);
 int _umask (int);
 int _unlink (__const__ char *);
Index: trunk/src/emx/include/os2emx.h
===================================================================
--- trunk/src/emx/include/os2emx.h	(revision 729)
+++ trunk/src/emx/include/os2emx.h	(revision 732)
@@ -1268,5 +1268,5 @@
 #define FIL_QUERYEASIZEL		12
 #define FIL_QUERYEASFROMLISTL		13
-#define FIL_QUERYFULLNAME		5 /* DosQueryPathInfo */
+#define FIL_QUERYFULLNAME		5
 
 #define FILE_BEGIN			0
@@ -1589,4 +1589,39 @@
 typedef FILEFINDBUF4 *PFILEFINDBUF4;
 
+typedef struct _FILEFINDBUF3L
+{
+    ULONG       oNextEntryOffset;
+    FDATE       fdateCreation;
+    FTIME       ftimeCreation;
+    FDATE       fdateLastAccess;
+    FTIME       ftimeLastAccess;
+    FDATE       fdateLastWrite;
+    FTIME       ftimeLastWrite;
+    LONGLONG    cbFile;
+    LONGLONG    cbFileAlloc;
+    ULONG       attrFile;
+    UCHAR       cchName;
+    CHAR        achName[CCHMAXPATHCOMP];
+} FILEFINDBUF3L;
+typedef FILEFINDBUF3L *PFILEFINDBUF3L;
+
+typedef struct _FILEFINDBUF4L
+{
+    ULONG       oNextEntryOffset;
+    FDATE       fdateCreation;
+    FTIME       ftimeCreation;
+    FDATE       fdateLastAccess;
+    FTIME       ftimeLastAccess;
+    FDATE       fdateLastWrite;
+    FTIME       ftimeLastWrite;
+    LONGLONG    cbFile;
+    LONGLONG    cbFileAlloc;
+    ULONG       attrFile;
+    ULONG       cbList;
+    UCHAR       cchName;
+    CHAR        achName[CCHMAXPATHCOMP];
+} FILEFINDBUF4L;
+typedef FILEFINDBUF4L  *PFILEFINDBUF4L;
+
 typedef struct _FILELOCK
 {
@@ -1596,5 +1631,5 @@
 typedef FILELOCK *PFILELOCK;
 
-typedef struct _FILELOCKL     /* flock */
+typedef struct _FILELOCKL
 {
   LONGLONG lOffset;
Index: trunk/src/emx/include/stdio.h
===================================================================
--- trunk/src/emx/include/stdio.h	(revision 729)
+++ trunk/src/emx/include/stdio.h	(revision 732)
@@ -1,42 +1,181 @@
-/* stdio.h (emx+gcc) */
-
-#ifndef _STDIO_H
-#define _STDIO_H
-
-#if defined (__cplusplus)
-extern "C" {
-#endif
+/*-
+ * Copyright (c) 1990, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Chris Torek.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
+ * $FreeBSD: src/include/stdio.h,v 1.51 2003/01/13 08:41:47 tjr Exp $
+ */
+
+/** @file
+ * FreeBSD 5.1
+ * @changed bird: EMX isms + LIBC implementation specifics.
+ * @chagned bird: Made quite a few @todos on function which aren't implemented.
+ */
+
+#ifndef	_STDIO_H_
+#define	_STDIO_H_
 
 #include <sys/cdefs.h>
 #include <sys/_types.h>
 
-#if !defined(_VA_LIST_DECLARED) && !defined(_VA_LIST) /* bird: emx */
+typedef	__off_t		fpos_t;
+
+#ifndef _SIZE_T_DECLARED
+typedef	__size_t	size_t;
+#define	_SIZE_T_DECLARED
+#define _SIZE_T                         /* bird: emx */
+#endif
+
+#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
+#ifndef _VA_LIST_DECLARED
+typedef	__va_list	va_list;
 #define	_VA_LIST_DECLARED
 #define _VA_LIST                        /* bird: emx */
-typedef	__va_list	va_list;
-#endif
-
-#if !defined (_SIZE_T)
-#define _SIZE_T
-typedef unsigned long size_t;
-#endif
-
-#if !defined (NULL)
-#if defined (__cplusplus)
-#define NULL 0
-#else
-#define NULL ((void *)0)
-#endif
-#endif
-
-#if !defined (BUFSIZ)
-#define BUFSIZ 5120
-#endif
-
-#if !defined (_FILE_T)
+#endif
+#endif
+
+#ifndef NULL
+#define	NULL	0
+#endif
+
+#define	_FSTDIO			/* Define for new stdio with functions. */
+
+#if 0                                   /* bird: emx */
+
+/*
+ * NB: to fit things in six character monocase externals, the stdio
+ * code uses the prefix `__s' for stdio objects, typically followed
+ * by a three-character attempt at a mnemonic.
+ */
+
+/* stdio buffers */
+struct __sbuf {
+	unsigned char *_base;
+	int	_size;
+};
+
+/* hold a buncha junk that would grow the ABI */
+struct __sFILEX;
+
+/*
+ * stdio state variables.
+ *
+ * The following always hold:
+ *
+ *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
+ *		_lbfsize is -_bf._size, else _lbfsize is 0
+ *	if _flags&__SRD, _w is 0
+ *	if _flags&__SWR, _r is 0
+ *
+ * This ensures that the getc and putc macros (or inline functions) never
+ * try to write or read from a file that is in `read' or `write' mode.
+ * (Moreover, they can, and do, automatically switch from read mode to
+ * write mode, and back, on "r+" and "w+" files.)
+ *
+ * _lbfsize is used only to make the inline line-buffered output stream
+ * code as compact as possible.
+ *
+ * _ub, _up, and _ur are used when ungetc() pushes back more characters
+ * than fit in the current _bf, or when ungetc() pushes back a character
+ * that does not match the previous one in _bf.  When this happens,
+ * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
+ * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
+ *
+ * NB: see WARNING above before changing the layout of this structure!
+ */
+typedef	struct __sFILE {
+	unsigned char *_p;	/* current position in (some) buffer */
+	int	_r;		/* read space left for getc() */
+	int	_w;		/* write space left for putc() */
+	short	_flags;		/* flags, below; this FILE is free if 0 */
+	short	_file;		/* fileno, if Unix descriptor, else -1 */
+	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
+	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
+
+	/* operations */
+	void	*_cookie;	/* cookie passed to io functions */
+	int	(*_close)(void *);
+	int	(*_read)(void *, char *, int);
+	fpos_t	(*_seek)(void *, fpos_t, int);
+	int	(*_write)(void *, const char *, int);
+
+	/* separate buffer for long sequences of ungetc() */
+	struct	__sbuf _ub;	/* ungetc buffer */
+	struct __sFILEX *_extra; /* additions to FILE to not break ABI */
+	int	_ur;		/* saved _r when _r is counting ungetc data */
+
+	/* tricks to meet minimum requirements even when malloc() fails */
+	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
+	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
+
+	/* separate buffer for fgetln() when line crosses buffer boundary */
+	struct	__sbuf _lb;	/* buffer for fgetln() */
+
+	/* Unix stdio files get aligned to block boundaries on fseek() */
+	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
+	fpos_t	_offset;	/* current lseek offset (see WARNING) */
+} FILE;
+
+__BEGIN_DECLS
+extern FILE *__stdinp;
+extern FILE *__stdoutp;
+extern FILE *__stderrp;
+__END_DECLS
+
+#define	__SLBF	0x0001		/* line buffered */
+#define	__SNBF	0x0002		/* unbuffered */
+#define	__SRD	0x0004		/* OK to read */
+#define	__SWR	0x0008		/* OK to write */
+	/* RD and WR are never simultaneously asserted */
+#define	__SRW	0x0010		/* open for reading & writing */
+#define	__SEOF	0x0020		/* found EOF */
+#define	__SERR	0x0040		/* found error */
+#define	__SMBF	0x0080		/* _buf is from malloc */
+#define	__SAPP	0x0100		/* fdopen()ed in append mode */
+#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
+#define	__SOPT	0x0400		/* do fseek() optimization */
+#define	__SNPT	0x0800		/* do not do fseek() optimization */
+#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
+#define	__SMOD	0x2000		/* true => fgetln modified _p text */
+#define	__SALC	0x4000		/* allocate string space dynamically */
+#define	__SIGN	0x8000		/* ignore this file in _fwalk */
+
+#else  /* bird: EMX specific FILE stuff starts. */
+
 #define _FILE_T
 #define _FILE_MEMBERS_HAVE_UNDERSCORE
 struct _file2;
-struct _FILE
+typedef struct _FILE
 {
   char * _ptr;
@@ -54,27 +193,312 @@
   int    (*_flush)(struct _FILE *, int);
   struct _file2 *_more;
-};
-
-typedef struct _FILE FILE;
-
+} FILE;
+
+__BEGIN_DECLS
 extern FILE _streamv[];
-
-#define stdin  (&_streamv[0])
-#define stdout (&_streamv[1])
-#define stderr (&_streamv[2])
-
-#endif
-
-#if !defined (SEEK_SET)
-#define SEEK_SET 0
-#define SEEK_CUR 1
-#define SEEK_END 2
-#endif
-
-#if !defined (EOF)
-#define EOF (-1)
-#endif
+__END_DECLS
+
+#define __stdinp    (&_streamv[0])
+#define __stdoutp   (&_streamv[1])
+#define __stderrp   (&_streamv[2])
+
+#endif /* bird: EMX specific FILE stuff ends. */
+
+
+/*
+ * The following three definitions are for ANSI C, which took them
+ * from System V, which brilliantly took internal interface macros and
+ * made them official arguments to setvbuf(), without renaming them.
+ * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
+ *
+ * Although numbered as their counterparts above, the implementation
+ * does not rely on this.
+ */
+#define	_IOFBF	0		/* setvbuf should set fully buffered */
+#define	_IOLBF	0x20 /* bird: emx, was 1 */ /* setvbuf should set line buffered */
+#define	_IONBF	0x40 /* bird: emx, was 2 */ /* setvbuf should set unbuffered */
+
+#define	BUFSIZ	5120 /* bird: emx, was 1024 */ /* size of buffer used by setbuf */
+#define	EOF	(-1)
+
+/*
+ * FOPEN_MAX is a minimum maximum, and is the number of streams that
+ * stdio can provide without attempting to allocate further resources
+ * (which could fail).  Do not use this for anything.
+ */
+				/* must be == _POSIX_STREAM_MAX <limits.h> */
+#define	FOPEN_MAX	14   /* bird: emx, was 20 */ /* must be <= OPEN_MAX <sys/syslimits.h> */
+#define	FILENAME_MAX	260  /* bird: emx, was 1024 */ /* must be <= PATH_MAX <sys/syslimits.h> */
+
+/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
+#if __XSI_VISIBLE
+#define	P_tmpdir	"." /* bird: emx, was "/var/tmp/" */
+#endif
+#define	L_tmpnam	260  /* bird: emx, was 1024 */ /* XXX must be == PATH_MAX */
+#define	TMP_MAX		1000 /* bird: emx, was 308915776 */
+
+#ifndef SEEK_SET
+#define	SEEK_SET	0	/* set file offset to offset */
+#endif
+#ifndef SEEK_CUR
+#define	SEEK_CUR	1	/* set file offset to current plus offset */
+#endif
+#ifndef SEEK_END
+#define	SEEK_END	2	/* set file offset to EOF plus offset */
+#endif
+
+#define	stdin	__stdinp
+#define	stdout	__stdoutp
+#define	stderr	__stderrp
+
+__BEGIN_DECLS
+/*
+ * Functions defined in ANSI C standard.
+ */
+void	 clearerr(FILE *);
+int	 fclose(FILE *);
+int	 feof(FILE *);
+int	 ferror(FILE *);
+int	 fflush(FILE *);
+int	 fgetc(FILE *);
+int	 fgetpos(FILE * __restrict, fpos_t * __restrict);
+char	*fgets(char * __restrict, int, FILE * __restrict);
+FILE	*fopen(const char * __restrict, const char * __restrict);
+int	 fprintf(FILE * __restrict, const char * __restrict, ...);
+int	 fputc(int, FILE *);
+int	 fputs(const char * __restrict, FILE * __restrict);
+size_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
+FILE	*freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
+int	 fscanf(FILE * __restrict, const char * __restrict, ...);
+int	 fseek(FILE *, long, int);
+int	 fsetpos(FILE *, const fpos_t *);
+long	 ftell(FILE *);
+size_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
+#if 0                                   /* bird: emx */
+int	 getc(FILE *);
+#else                                   /* bird: emx */
+/** @todo: Check the standard, if this is correct or not. declaration might be required. */
+#define getc(s)   fgetc(s)              /* bird: emx */
+#endif                                  /* bird: emx */
+int	 getchar(void);
+char	*gets(char *);
+void	 perror(const char *);
+int	 printf(const char * __restrict, ...);
+#if 0                                   /* bird: emx */
+int	 putc(int, FILE *);
+#else                                   /* bird: emx */
+/** @todo: Check the standard, if this is correct or not. declaration might be required. */
+#define putc(c,s) fputc(c,s)            /* bird: emx */
+#endif                                  /* bird: emx */
+int	 putchar(int);
+int	 puts(const char *);
+int	 remove(const char *);
+int	 rename(const char *, const char *);
+void	 rewind(FILE *);
+int	 scanf(const char * __restrict, ...);
+void	 setbuf(FILE * __restrict, char * __restrict);
+int	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
+int	 sprintf(char * __restrict, const char * __restrict, ...);
+int	 sscanf(const char * __restrict, const char * __restrict, ...);
+FILE	*tmpfile(void);
+char	*tmpnam(char *);
+int	 ungetc(int, FILE *);
+int	 vfprintf(FILE * __restrict, const char * __restrict,
+	    __va_list);
+int	 vprintf(const char * __restrict, __va_list);
+int	 vsprintf(char * __restrict, const char * __restrict,
+	    __va_list);
+
+#if __ISO_C_VISIBLE >= 1999
+int	 snprintf(char * __restrict, size_t, const char * __restrict,
+	    ...) __printflike(3, 4);
+int	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
+	    __scanflike(2, 0);
+int	 vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
+int	 vsnprintf(char * __restrict, size_t, const char * __restrict,
+	    __va_list) __printflike(3, 0);
+int	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
+	    __scanflike(2, 0);
+#endif
+
+/*
+ * Functions defined in all versions of POSIX 1003.1.
+ */
+#if __BSD_VISIBLE || __POSIX_VISIBLE <= 199506
+/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
+#define	L_cuserid	9 /* bird: emx, was 17 */ /* legacy */
+#endif
+
+#if __POSIX_VISIBLE
+#define	L_ctermid	260 /* bird: emx, was 1024 */ /* size for ctermid(3); PATH_MAX */
+
+/** @todo char	*ctermid(char *); */
+FILE	*fdopen(int, const char *);
+int	 fileno(FILE *);
+#endif /* __POSIX_VISIBLE */
+
+#if __POSIX_VISIBLE >= 199209
+int	 pclose(FILE *);
+FILE	*popen(const char *, const char *);
+#endif
+
+#if __POSIX_VISIBLE >= 199506
+/** @todo int	 ftrylockfile(FILE *); */
+/** @todo void	 flockfile(FILE *); */
+/** @todo void	 funlockfile(FILE *); */
+
+/*
+ * These are normally used through macros as defined below, but POSIX
+ * requires functions as well.
+ */
+/** @todo int	 getc_unlocked(FILE *); */
+/** @todo int	 getchar_unlocked(void); */
+/** @todo int	 putc_unlocked(int, FILE *); */
+/** @todo int	 putchar_unlocked(int); */
+#endif
+#if __BSD_VISIBLE
+/** @todo void	clearerr_unlocked(FILE *); */
+/** @todo int	feof_unlocked(FILE *); */
+/** @todo int	ferror_unlocked(FILE *); */
+/** @todo int	fileno_unlocked(FILE *); */
+#endif
+
+#if __POSIX_VISIBLE >= 200112
+int	 fseeko(FILE *, __off_t, int);
+__off_t	 ftello(FILE *);
+#endif
+
+#if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
+int	 getw(FILE *);
+int	 putw(int, FILE *);
+#endif /* BSD or X/Open before issue 6 */
+
+#if __XSI_VISIBLE
+char	*tempnam(const char *, const char *);
+#endif
+
+/*
+ * Routines that are purely local.
+ */
+#if __BSD_VISIBLE
+/** @todo int	 asprintf(char **, const char *, ...) __printflike(2, 3); */
+/** @todo char	*ctermid_r(char *); */
+/** @todo char	*fgetln(FILE *, size_t *); */
+#if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3
+#define	__ATTR_FORMAT_ARG	__attribute__((__format_arg__(2)))
+#else
+#define	__ATTR_FORMAT_ARG
+#endif
+/** @todo __const char *fmtcheck(const char *, const char *) __ATTR_FORMAT_ARG; */
+/** @todo int	 fpurge(FILE *); */
+void	 setbuffer(FILE *, char *, int);
+/** @todo int	 setlinebuf(FILE *); */
+/** @todo int	 vasprintf(char **, const char *, __va_list)
+	    __printflike(2, 0); */
+
+/*
+ * The system error table contains messages for the first sys_nerr
+ * positive errno values.  Use strerror() or strerror_r() from <string.h>
+ * instead.
+ */
+extern __const int sys_nerr;
+extern __const char *__const sys_errlist[];
+
+/*
+ * Stdio function-access interface.
+ */
+/** @todo FILE	*funopen(const void *,
+	    int (*)(void *, char *, int),
+	    int (*)(void *, const char *, int),
+	    fpos_t (*)(void *, fpos_t, int),
+	    int (*)(void *)); */
+/** @todo #define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) */
+/** @todo #define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) */
+
+/*
+ * Portability hacks.  See <sys/types.h>.
+ */
+#ifndef _FTRUNCATE_DECLARED
+#define	_FTRUNCATE_DECLARED
+int	 ftruncate(int, __off_t);
+#endif
+#ifndef _LSEEK_DECLARED
+#define	_LSEEK_DECLARED
+__off_t	 lseek(int, __off_t, int);
+#endif
+#ifndef _MMAP_DECLARED
+#define	_MMAP_DECLARED
+void	*mmap(void *, size_t, int, int, int, __off_t);
+#endif
+#ifndef _TRUNCATE_DECLARED
+#define	_TRUNCATE_DECLARED
+int	 truncate(const char *, __off_t);
+#endif
+#endif /* __BSD_VISIBLE */
+
+#if 0 /* bird: Skip FreeBSD sepcific LIBC stuff. */
+/*
+ * Functions internal to the implementation.
+ */
+int	__srget(FILE *);
+int	__swbuf(int, FILE *);
+
+/*
+ * The __sfoo macros are here so that we can
+ * define function versions in the C library.
+ */
+#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
+#if defined(__GNUC__) && defined(__STDC__)
+static __inline int __sputc(int _c, FILE *_p) {
+	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
+		return (*_p->_p++ = _c);
+	else
+		return (__swbuf(_c, _p));
+}
+#else
+/*
+ * This has been tuned to generate reasonable code on the vax using pcc.
+ */
+#define	__sputc(c, p) \
+	(--(p)->_w < 0 ? \
+		(p)->_w >= (p)->_lbfsize ? \
+			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
+				(int)*(p)->_p++ : \
+				__swbuf('\n', p) : \
+			__swbuf((int)(c), p) : \
+		(*(p)->_p = (c), (int)*(p)->_p++))
+#endif
+
+#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
+#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
+#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
+#define	__sfileno(p)	((p)->_file)
+
+#if __BSD_VISIBLE
+/*
+ * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
+ * B.8.2.7 for the rationale behind the *_unlocked() macros.
+ */
+#define	feof_unlocked(p)	__sfeof(p)
+#define	ferror_unlocked(p)	__sferror(p)
+#define	clearerr_unlocked(p)	__sclearerr(p)
+#define	fileno_unlocked(p)	__sfileno(p)
+#endif
+#if __POSIX_VISIBLE >= 199506
+#define	getc_unlocked(fp)	__sgetc(fp)
+#define	putc_unlocked(x, fp)	__sputc(x, fp)
+
+#define	getchar_unlocked()	getc_unlocked(stdin)
+#define	putchar_unlocked(x)	putc_unlocked(x, stdout)
+#endif
+
+#endif /* bird: Skip FreeBSD sepcific LIBC stuff. */
+
+
+/* bird: start of EMX isms. */
 
 #if !defined (_IOREAD)
+/** @todo change to double underscore prefix to prevent confusion with
+ * setvbuf() constants. See the short rant about it above. */
 #define _IOREAD 0x01
 #define _IOWRT  0x02
@@ -82,81 +506,5 @@
 #define _IOEOF  0x08
 #define _IOERR  0x10
-#define _IOFBF  0x00
-#define _IOLBF  0x20
-#define _IONBF  0x40
-#endif
-
-#if !defined (FOPEN_MAX)
-#define	FOPEN_MAX       14
-#endif
-
-#if !defined (FILENAME_MAX)
-#define	FILENAME_MAX    260
-#endif
-
-#if !defined (TMP_MAX)
-#define TMP_MAX         1000
-#endif
-
-#if !defined (P_tmpdir)
-#define P_tmpdir "."
-#define L_tmpnam (sizeof (P_tmpdir) + 13)
-#endif
-
-#if !defined (L_cuserid)
-#define L_cuserid       9
-#endif
-
-#if !defined (_FPOS_T)
-#define _FPOS_T
-typedef struct
-{
-  long _pos;
-  long _reserved1;
-  short _mbstate;
-  short _reserved2;
-} fpos_t;
-#endif
-
-
-void clearerr (FILE *);
-int fclose (FILE *);
-int feof (FILE *);
-int ferror (FILE *);
-int fflush (FILE *);
-int fgetc (FILE *);
-int fgetpos (FILE *, fpos_t *);
-char *fgets (char *, int, FILE *);
-FILE *fopen (__const__ char *, __const__ char *);
-int fprintf (FILE *, __const__ char *, ...);
-int fputc (int, FILE *);
-int fputs (__const__ char *, FILE *);
-size_t fread (void *, size_t, size_t, FILE *);
-FILE *freopen (__const__ char *, __const__ char *, FILE *);
-int fscanf (FILE *, __const__ char *, ...);
-int fseek (FILE *, long, int);
-int fsetpos (FILE *, __const__ fpos_t *);
-long ftell (FILE *);
-size_t fwrite (__const__ void *, size_t, size_t, FILE *);
-int getchar (void);
-char *gets (char *);
-void perror (__const__ char *);
-int printf (__const__ char *, ...);
-int putchar (int);
-int puts (__const__ char *);
-int remove (__const__ char *);
-int rename (__const__ char *, __const__ char *);
-void rewind (FILE *);
-int scanf (__const__ char *, ...);
-int setbuf (FILE *, char *);
-int setvbuf (FILE *, char *, int, size_t);
-int sprintf (char *, __const__ char *, ...);
-int sscanf (__const__ char *, __const__ char *, ...);
-FILE *tmpfile (void);
-char *tmpnam (char *);
-int ungetc (int, FILE *);
-int vfprintf (FILE *, __const__ char *, va_list);
-int vprintf (__const__ char *, va_list);
-int vsprintf (char *, __const__ char *, va_list);
+#endif
 
 int _fill (FILE *);
@@ -174,42 +522,14 @@
 }
 
-#define getc(s)   fgetc(s)
-#define putc(c,s) fputc(c,s)
-
 extern __inline__ int getchar (void) { return getc (stdin); }
 extern __inline__ int putchar (int _c) { return putc (_c, stdout); }
 
-
-#if !defined (__STRICT_ANSI__)
-
-/* POSIX.1 */
-
-/* ctermid() */
-FILE *fdopen (int, __const__ char *);
-int fileno (FILE *);
-
-extern __inline__ int fileno (FILE *_s) { return _s->_handle; }
-
-#endif
-
-
 #if !defined (__STRICT_ANSI__) && !defined (_POSIX_SOURCE)
 
 char *cuserid (char *);
-int getw (FILE *);
 int fcloseall (void);
 int fgetchar (void);
 int flushall (void);
 int fputchar (int);
-int pclose (FILE *);
-FILE *popen (__const__ char *, __const__ char *);
-int putw (int, FILE *);
-int setbuffer (FILE *, char *, size_t);
-int snprintf (char *, size_t, __const__ char *, ...);
-char *tempnam (__const__ char *, __const__ char *);
-int vfscanf (FILE *, __const__ char *, va_list);
-int vscanf (__const__ char *, va_list);
-int vsnprintf (char *, size_t, __const__ char *, va_list);
-int vsscanf (__const__ char *, __const__ char *, va_list);
 
 #endif
@@ -234,5 +554,5 @@
 FILE *_popen (__const__ char *, __const__ char *);
 int _putw (int, FILE *);
-int _setbuffer (FILE *, char *, size_t);
+void _setbuffer (FILE *, char *, int);
 int _snprintf (char *, size_t, __const__ char *, ...);
 char *_tempnam (__const__ char *, __const__ char *);
@@ -240,9 +560,7 @@
 
 #endif
-
-
-#if defined (__cplusplus)
-}
-#endif
-
-#endif /* not _STDIO_H */
+/* bird: end of EMX isms. */
+
+__END_DECLS
+#endif /* !_STDIO_H_ */
+
Index: trunk/src/emx/include/sys/_types.h
===================================================================
--- trunk/src/emx/include/sys/_types.h	(revision 729)
+++ trunk/src/emx/include/sys/_types.h	(revision 732)
@@ -57,5 +57,5 @@
 typedef	int		__nl_item;
 typedef	__uint16_t	__nlink_t;	/* link count */
-#if 1 /* bird: emx */
+#if 0 /* bird: emx */
 typedef	__int32_t	__off_t;	/* file offset */
 #else
Index: trunk/src/emx/include/sys/dir.h
===================================================================
--- trunk/src/emx/include/sys/dir.h	(revision 729)
+++ trunk/src/emx/include/sys/dir.h	(revision 732)
@@ -1,77 +1,57 @@
-/* sys/dir.h (emx+gcc) */
+/*
+ * Copyright (c) 1982, 1986, 1989, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)dir.h	8.2 (Berkeley) 1/4/94
+ * $FreeBSD: src/sys/sys/dir.h,v 1.6 1999/08/28 00:51:39 peter Exp $
+ */
 
-#ifndef _SYS_DIR_H
-#define _SYS_DIR_H
+/** @file
+ * FreeBSD 5.1
+ */
 
-#if !defined (_SYS_TYPES_H)
-#warning <sys/dir.h> requires <sys/types.h>
-#include <sys/types.h>
+#ifndef _SYS_DIR_H_
+#define	_SYS_DIR_H_
+
+#if __GNUC__
+#warning "The information in this file should be obtained from <dirent.h>"
+#warning "and is provided solely (and temporarily) for backward compatibility."
 #endif
 
-#if defined (__cplusplus)
-extern "C" {
-#endif
+#include <dirent.h>
 
-#if !defined (MAXNAMLEN)
-#define MAXNAMLEN  260
-#endif
+/*
+ * Backwards compatibility.
+ */
+#define	direct		dirent
+#define	DIRSIZ(dp)	_GENERIC_DIRSIZ(dp)
 
-#if !defined (MAXPATHLEN)
-#define MAXPATHLEN 260
-#endif
-
-#if !defined (A_RONLY)
-#define A_RONLY   0x01
-#define A_HIDDEN  0x02
-#define A_SYSTEM  0x04
-#define A_LABEL   0x08
-#define A_DIR     0x10
-#define A_ARCHIVE 0x20
-#endif
-
-#define _DIRECT_D_MODE_RENAMED_D_ATTR
-
-struct direct
-{
-  ino_t          d_ino;                 /* Almost not used           */
-  int            d_reclen;              /* Almost not used           */
-  int            d_namlen;              /* Length of d_name          */
-  char           d_name[MAXNAMLEN + 1]; /* File name, 0 terminated   */
-  long           d_size;                /* File size (bytes)         */
-  unsigned short d_attr;                /* OS file attributes        */
-  unsigned short d_time;                /* OS file modification time */
-  unsigned short d_date;                /* OS file modification date */
-};
-
-struct _dircontents
-{
-  char *                _d_entry;
-  long                  _d_size;
-  unsigned short        _d_attr;
-  unsigned short        _d_time;
-  unsigned short        _d_date;
-  struct _dircontents * _d_next;
-};
-
-struct _dirdesc
-{
-  int                   dd_id;
-  long                  dd_loc;
-  struct _dircontents * dd_contents;
-  struct _dircontents * dd_cp;
-};
-
-typedef struct _dirdesc DIR;
-
-DIR *opendir (__const__ char *name);
-struct direct *readdir (DIR *dirp);
-void seekdir (DIR *dirp, long off);
-long telldir (DIR *dirp);
-int closedir (DIR *dirp);
-void rewinddir (DIR *dirp);
-
-#if defined (__cplusplus)
-}
-#endif
-
-#endif /* not SYS_DIR_H */
+#endif /* !_SYS_DIR_H_ */
Index: trunk/src/emx/include/sys/dirent.h
===================================================================
--- trunk/src/emx/include/sys/dirent.h	(revision 729)
+++ trunk/src/emx/include/sys/dirent.h	(revision 732)
@@ -1,15 +1,121 @@
-/* sys/dirent.h (emx+gcc) */
+/*-
+ * Copyright (c) 1989, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)dirent.h	8.3 (Berkeley) 8/10/94
+ * $FreeBSD: src/sys/sys/dirent.h,v 1.13 2002/09/10 18:12:16 mike Exp $
+ */
 
-#ifndef _SYS_DIRENT_H
-#define _SYS_DIRENT_H
+/** @file
+ * FreeBSD 5.1
+ * @changed bird: Merged in all the EMX stuff.
+ */
 
-#if !defined (_SYS_TYPES_H)
-#warning <sys/dirent.h> requires <sys/types.h>
-#include <sys/types.h>
+#ifndef	_SYS_DIRENT_H_
+#define	_SYS_DIRENT_H_
+
+#include <sys/cdefs.h>
+#include <sys/_types.h>
+
+/*
+ * The dirent structure defines the format of directory entries returned by
+ * the getdirentries(2) system call.
+ *
+ * A directory entry has a struct dirent at the front of it, containing its
+ * inode number, the length of the entry, and the length of the name
+ * contained in the entry.  These are followed by the name padded to a 4
+ * byte boundary with null bytes.  All names are guaranteed null terminated.
+ * The maximum length of a name in a directory is MAXNAMLEN.
+ */
+
+struct dirent {
+	__uint32_t d_fileno;		/* file number of entry */
+	__uint16_t d_reclen;		/* length of this record */
+	__uint8_t  d_type; 		/* file type, see below */
+	__uint8_t  d_namlen;		/* length of string in d_name */
+#if __BSD_VISIBLE
+#ifndef MAXNAMLEN  /* bird: emx */
+#define	MAXNAMLEN	260 /* bird: changed from 255. */
+#endif             /* bird: emx */
+	char	d_name[MAXNAMLEN + 1];	/* name must be no longer than this */
+#else
+	char	d_name[260 + 1];	/* bird: changed from 255. */ /* name must be no longer than this */
+#endif
+/* bird: Extra EMX fields - start */
+        __uint8_t  d_attr;              /* OS file attributes        */
+        __uint16_t d_time;              /* OS file modification time */
+        __uint16_t d_date;              /* OS file modification date */
+        __off_t    d_size;              /* File size (bytes)         */
+/* bird: Extra EMX fields - end */
+};
+
+#if __BSD_VISIBLE
+/*
+ * File types
+ */
+#define	DT_UNKNOWN	 0
+#define	DT_FIFO		 1
+#define	DT_CHR		 2
+#define	DT_DIR		 4
+#define	DT_BLK		 6
+#define	DT_REG		 8
+#define	DT_LNK		10
+#define	DT_SOCK		12
+#define	DT_WHT		14
+
+/*
+ * Convert between stat structure types and directory types.
+ */
+#define	IFTODT(mode)	(((mode) & 0170000) >> 12)
+#define	DTTOIF(dirtype)	((dirtype) << 12)
+
+/*
+ * The _GENERIC_DIRSIZ macro gives the minimum record length which will hold
+ * the directory entry.  This requires the amount of space in struct direct
+ * without the d_name field, plus enough space for the name with a terminating
+ * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
+ *
+ * XXX although this macro is in the implementation namespace, it requires
+ * a manifest constant that is not.
+ */
+#define	_GENERIC_DIRSIZ(dp) \
+    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
+#endif /* __BSD_VISIBLE */
+
+#ifdef _KERNEL
+#define	GENERIC_DIRSIZ(dp)	_GENERIC_DIRSIZ(dp)
 #endif
 
-#if !defined (MAXNAMLEN)
-#define MAXNAMLEN  260
-#endif
+
+/* bird: EMX stuff */
+#define _DIRENT_D_MODE_RENAMED_D_ATTR
 
 #if !defined (MAXPATHLEN)
@@ -26,17 +132,3 @@
 #endif
 
-#define _DIRENT_D_MODE_RENAMED_D_ATTR
-
-struct dirent
-{
-  ino_t          d_ino;                 /* Almost not used           */
-  int            d_reclen;              /* Almost not used           */
-  int            d_namlen;              /* Length of d_name          */
-  char           d_name[MAXNAMLEN + 1]; /* File name, 0 terminated   */
-  long           d_size;                /* File size (bytes)         */
-  unsigned short d_attr;                /* OS file attributes        */
-  unsigned short d_time;                /* OS file modification time */
-  unsigned short d_date;                /* OS file modification date */
-};
-
 #endif /* not SYS_DIRENT_H */
Index: trunk/src/emx/include/sys/dirtree.h
===================================================================
--- trunk/src/emx/include/sys/dirtree.h	(revision 729)
+++ trunk/src/emx/include/sys/dirtree.h	(revision 732)
@@ -4,11 +4,17 @@
 #define _SYS_DIRTREE_H
 
-#if defined (__cplusplus)
-extern "C" {
+#include <sys/cdefs.h>
+#include <sys/_types.h>
+
+#if !defined(_TIME_T_DECLARED) && !defined(_TIME_T)
+typedef	__time_t	time_t;
+#define	_TIME_T_DECLARED
+#define _TIME_T
 #endif
 
-#if !defined (_TIME_T)
-#define _TIME_T
-typedef unsigned long time_t;
+#if !defined(_OFF_T_DECLARED) && !defined(_OFF_T)
+typedef	__off_t		off_t;		/* file offset */
+#define	_OFF_T_DECLARED
+#define _OFF_T
 #endif
 
@@ -18,10 +24,9 @@
   struct _dt_node *sub;    /* Pointer to next level (child) */
   char *name;              /* Name */
-  long size;               /* File size */
+  off_t size;              /* File size */
   long user;               /* Available for user */
   time_t mtime;            /* Timestamp for last update */
   unsigned char attr;      /* Attributes */
 };
-
 
 struct _dt_tree
@@ -34,13 +39,11 @@
 #define _DT_NOCPDIR  0x8000
 
-void _dt_free (struct _dt_tree *dt);
+__BEGIN_DECLS
+void    _dt_free (struct _dt_tree *dt);
 struct _dt_tree *_dt_read (__const__ char *dir, __const__ char *mask,
     unsigned flags);
-void _dt_sort (struct _dt_tree *dt, __const__ char *spec);
-int _dt_split (__const__ char *src, char *dir, char *mask);
+void    _dt_sort (struct _dt_tree *dt, __const__ char *spec);
+int     _dt_split (__const__ char *src, char *dir, char *mask);
+__END_DECLS
 
-#if defined (__cplusplus)
-}
-#endif
-
-#endif /* not _SYS_DIRTREE_H */
+#endif /* !_SYS_DIRTREE_H */
Index: trunk/src/emx/src/lib/io/_flushst.c
===================================================================
--- trunk/src/emx/src/lib/io/_flushst.c	(revision 729)
+++ trunk/src/emx/src/lib/io/_flushst.c	(revision 732)
@@ -52,5 +52,5 @@
               w = 0;
               if (pflags != NULL && (*pflags & O_APPEND))
-                lseek (fh, 0L, SEEK_END);
+                lseek (fh, 0, SEEK_END);
             }
           stream->_ptr = stream->_buffer;
Index: trunk/src/emx/src/lib/io/_seekhdr.c
===================================================================
--- trunk/src/emx/src/lib/io/_seekhdr.c	(revision 729)
+++ trunk/src/emx/src/lib/io/_seekhdr.c	(revision 732)
@@ -22,5 +22,5 @@
       unsigned short hdr_loc_hi;
     } patch;
-  long original_pos;
+  off_t original_pos;
   int saved_errno;
 
@@ -37,5 +37,5 @@
     goto failure;
   if (lseek (handle, original_pos + patch.hdr_loc_lo
-              + 65536L * patch.hdr_loc_hi, SEEK_SET) == -1)
+              + 65536 * patch.hdr_loc_hi, SEEK_SET) == -1)
     goto failure;
   return 0;
Index: trunk/src/emx/src/lib/io/_vsopen.c
===================================================================
--- trunk/src/emx/src/lib/io/_vsopen.c	(revision 729)
+++ trunk/src/emx/src/lib/io/_vsopen.c	(revision 732)
@@ -14,148 +14,178 @@
 /*       O_TEXT|O_WRONLY  does/can not overwrite Ctrl-Z */
 
-#define FALSE   0
-#define TRUE    1
-
 int _fmode_bin;                 /* Set non-zero to make binary mode default */
 
 #define SH_MASK           0x70
 
-/* This function may be called as follows (assuming that oflag does
-   not include O_CREAT nor O_SIZE):
+/**
+ * This function may be called as follows (assuming that fOpen does
+ *  not include O_CREAT nor O_SIZE):
+ *
+ *  _vsopen(pszName, fOpen, fShare)
+ *  _vsopen(pszName, fOpen | O_CREAT, fShare, pmode)
+ *  _vsopen(pszName, fOpen | O_SIZE, fShare, off_t size)
+ *  _vsopen(pszName, fOpen | O_CREAT | O_SIZE, fShare, pmode, off_t size)
+ *
+ * @remark O_SIZE means a off_t sized argument not unsigned long as emxlib say.
+ */
+int _vsopen(const char *pszName, int fOpen, int fShare, va_list va)
+{
+    int     hFile;
+    /** Flags is for __open.
+     *  0 -  1  Access mode (O_RDONLY, O_WRONLY, O_RDWR).
+     *  4 -  6  Shared (SH_*)
+     *  8 - 15  File attributes (DOS style).
+     * 16 - 23  __open flags, _SO_*
+     */
+    int     fFlags;
+    int    *pfFlags;
+    int     saved_errno;
+    int     bits;
+    char    chDummy;
+    int     fCtrlZKludge = 0;
+    off_t   cbInitial;
 
-   _vsopen (name, oflag, shflag)
-   _vsopen (name, oflag | O_CREAT, shflag, pmode)
-   _vsopen (name, oflag | O_SIZE, shflag, size)
-   _vsopen (name, oflag | O_CREAT | O_SIZE, shflag, pmode, size) */
-
-int _vsopen (const char *name, int oflag, int shflag, va_list va)
-{
-  int handle, flags, *pflags, saved_errno;
-  int bits;
-  char dummy, ctrlz_kludge = FALSE;
-  unsigned long initial_size;
-
-  if ((oflag & O_ACCMODE) == O_RDONLY && (oflag & (O_TRUNC|O_CREAT)))
+    /*
+     * Validate input
+     */
+    if ((fOpen & O_ACCMODE) == O_RDONLY && (fOpen & (O_TRUNC|O_CREAT)))
     {
-      errno = EINVAL;
-      return -1;
-    }
-
-  bits = oflag & (O_ACCMODE|O_NDELAY|O_APPEND);
-  if (oflag & O_BINARY)
-    /* do nothing */;
-  else if (oflag & O_TEXT)
-    bits |= O_TEXT;
-  else if (_fmode_bin == 0)          /* neither O_TEXT nor O_BINARY given */
-    bits |= O_TEXT;
-
-  if ((bits & O_TEXT) && (oflag & O_APPEND) && (oflag & O_ACCMODE) == O_WRONLY)
-    {
-      /* The caller requests to open a text file for appending in
-         write-only.  To remove the Ctrl-Z (if there is any), we have
-         to temporarily open the file in read/write mode. */
-
-      flags = O_RDWR | (shflag & SH_MASK);
-      ctrlz_kludge = TRUE;
-    }
-  else
-    flags = (oflag & O_ACCMODE) | (shflag & SH_MASK);
-
-  if (oflag & O_CREAT)
-    {
-      int attr, pmode;
-
-      attr = 0;
-      pmode = va_arg (va, int);
-      if (!(pmode & S_IWRITE))
-        attr |= _A_RDONLY;
-      flags |= (attr << 8) | _SO_CREAT;
-      if (oflag & O_EXCL)
-        flags |= _SO_EXCL;
-    }
-
-  if (oflag & O_TRUNC)
-    flags |= _SO_TRUNC;
-
-  if (oflag & O_NOINHERIT)
-    flags |= _SO_NOINHERIT;
-
-  if (oflag & O_SYNC)
-    flags |= _SO_SYNC;
-
-  initial_size = 0;
-  if (oflag & O_SIZE)
-    {
-      initial_size = va_arg (va, unsigned long);
-      flags |= _SO_SIZE;
-    }
-    
-  saved_errno = errno;
-  handle = __open (name, flags, initial_size);
-  if (handle < 0 && ctrlz_kludge && errno == EACCES)
-    {
-      /* Perhaps read access is denied.  Try again. */
-      errno = saved_errno;
-      ctrlz_kludge = FALSE;
-      flags = (flags & ~O_ACCMODE) | (oflag & O_ACCMODE);
-      handle = __open (name, flags, initial_size);
-    }
-  if (handle < 0)
-    return -1;
-
-  if (__ioctl1 (handle, 0) & 0x80)
-    {
-      bits |= F_DEV;
-      oflag &= ~O_APPEND;
-    }
-
-  if (!(bits & F_DEV) && (bits & O_TEXT))
-    {
-      long size;
-
-      /* Remove the last character of the file if it is a Ctrl-Z.  Do
-         this even if O_SIZE is given as we don't know whether the
-         file existed before; removing a spurious Ctrl-Z due to O_SIZE
-         should be harmless. */
-
-      /* DEC Pathworks seems to treat the offset as unsigned number.
-         Therefore we do not use __lseek (handle, -1L, SEEK_END). */
-
-      size = __lseek (handle, 0L, SEEK_END);
-      if (size > 0 && __lseek (handle, size - 1, SEEK_SET) != -1L
-          && __read (handle, &dummy, 1) == 1 && dummy == 0x1a)
-        __ftruncate (handle, size - 1); /* Remove Ctrl-Z */
-      __lseek (handle, 0L, SEEK_SET);
-    }
-
-  if (ctrlz_kludge)
-    {
-      /* Reopen the handle in write-only mode. */
-
-      __close (handle);
-      flags = (flags & ~O_ACCMODE) | (oflag & O_ACCMODE);
-      flags &= ~_SO_EXCL;       /* Ignore O_EXCL */
-      handle = __open (name, flags, initial_size);
-      if (handle < 0)
+        errno = EINVAL;
         return -1;
     }
 
-  pflags = _fd_init (handle);
-  if (pflags == NULL)
+    /*
+     * Build fFlags.
+     */
+    bits = fOpen & (O_ACCMODE|O_NDELAY|O_APPEND);
+    if (fOpen & O_BINARY)
+        /* do nothing */;
+    else if (fOpen & O_TEXT)
+        bits |= O_TEXT;
+    else if (_fmode_bin == 0)          /* neither O_TEXT nor O_BINARY given */
+        bits |= O_TEXT;
+
+    if ((bits & O_TEXT) && (fOpen & O_APPEND) && (fOpen & O_ACCMODE) == O_WRONLY)
     {
-      __close (handle);
-      errno = EMFILE;
-      return -1;
+        /* The caller requests to open a text file for appending in
+           write-only.  To remove the Ctrl-Z (if there is any), we have
+           to temporarily open the file in read/write mode. */
+
+        fFlags = O_RDWR | (fShare & SH_MASK);
+        fCtrlZKludge = 1;
+    }
+    else
+        fFlags = (fOpen & O_ACCMODE) | (fShare & SH_MASK);
+
+    /* pmode(=attr) - attr is 2nd byte of flags. */
+    if (fOpen & O_CREAT)
+    {
+        int attr, pmode;
+
+        attr = 0;
+        pmode = va_arg(va, int);
+        if (!(pmode & S_IWRITE))
+            attr |= _A_RDONLY;
+        fFlags |= (attr << 8) | _SO_CREAT;
+        if (fOpen & O_EXCL)
+            fFlags |= _SO_EXCL;
     }
 
-  *pflags = bits;
+    if (fOpen & O_TRUNC)
+        fFlags |= _SO_TRUNC;
 
-  /* When opening a file for appending, move to the end of the file.
-     This is required for passing the handle to a child process. */
+    if (fOpen & O_NOINHERIT)
+        fFlags |= _SO_NOINHERIT;
 
-  if (!(bits & F_DEV) && (bits & O_APPEND))
-    __lseek (handle, 0L, SEEK_END);
-  errno = saved_errno;
+    if (fOpen & O_SYNC)
+        fFlags |= _SO_SYNC;
 
-  return handle;
+    /* Initial file size */
+    cbInitial = 0;
+    if (fOpen & O_SIZE)
+    {
+        cbInitial = va_arg(va, off_t);
+        fFlags |= _SO_SIZE;
+    }
+
+    /*
+     * Open - note Ctrl-Z hack.
+     */
+    saved_errno = errno;
+    hFile = __open(pszName, fFlags, cbInitial);
+    if (hFile < 0 && fCtrlZKludge && errno == EACCES)
+    {
+        /* Perhaps read access is denied. Try again. */
+        errno = saved_errno;
+        fCtrlZKludge = 0;
+        fFlags = (fFlags & ~O_ACCMODE) | (fOpen & O_ACCMODE);
+        hFile = __open(pszName, fFlags, cbInitial);
+    }
+    if (hFile < 0)
+        return -1;
+
+    /*
+     * Check what we got a handle to.
+     */
+    if (__ioctl1(hFile, 0) & 0x80)
+    {
+        bits |= F_DEV;
+        fOpen &= ~O_APPEND;
+    }
+
+    /*
+     * For text files we shall remove eventual ending Ctrl-Z.
+     */
+    if (!(bits & F_DEV) && (bits & O_TEXT))
+    {
+        off_t cbSize;
+
+        /* Remove the last character of the file if it is a Ctrl-Z.  Do
+           this even if O_SIZE is given as we don't know whether the
+           file existed before; removing a spurious Ctrl-Z due to O_SIZE
+           should be harmless. */
+
+        cbSize = __lseek(hFile, -1, SEEK_END);
+        if (cbSize >= 0)
+        {
+            if (__read(hFile, &chDummy, 1) == 1 && chDummy == 0x1a)
+                __ftruncate(hFile, cbSize); /* Remove Ctrl-Z */
+            __lseek(hFile, 0L, SEEK_SET);
+        }
+    }
+
+    /*
+     * Reopen the file in write-only mode if Ctrl-Z hack applied.
+     */
+    if (fCtrlZKludge)
+    {
+        __close(hFile);
+        fFlags = (fFlags & ~O_ACCMODE) | (fOpen & O_ACCMODE);
+        fFlags &= ~_SO_EXCL;       /* Ignore O_EXCL */
+        hFile = __open(pszName, fFlags, cbInitial);
+        if (hFile < 0)
+            return -1;
+    }
+
+    /*
+     * Set file bits.
+     */
+    pfFlags = _fd_init(hFile);
+    if (!pfFlags)
+    {
+        __close(hFile);
+        errno = EMFILE;
+        return -1;
+    }
+    *pfFlags = bits;
+
+    /*
+     * When opening a file for appending, move to the end of the file.
+     * This is required for passing the file to a child process.
+     */
+    if (!(bits & F_DEV) && (bits & O_APPEND))
+        __lseek(hFile, 0L, SEEK_END);
+    errno = saved_errno;
+
+    return hFile;
 }
Index: trunk/src/emx/src/lib/io/chsize.c
===================================================================
--- trunk/src/emx/src/lib/io/chsize.c	(revision 729)
+++ trunk/src/emx/src/lib/io/chsize.c	(revision 732)
@@ -8,8 +8,9 @@
 #include <emx/syscalls.h>
 
-int _STD(chsize) (int handle, long length)
+int _STD(chsize) (int handle, off_t length)
 {
-  long n;
-  int i, j, *pflags;
+  off_t n;
+  off_t i;
+  int j, *pflags;
   char zeros[4096];
 
@@ -19,6 +20,6 @@
       return -1;
     }
-  n = (long)__lseek (handle, 0L, SEEK_END);
-  if (n == -1L)
+  n = __lseek (handle, 0, SEEK_END);
+  if (n == -1)
     return -1;
   if (__chsize (handle, length) != 0)
Index: trunk/src/emx/src/lib/io/eof.c
===================================================================
--- trunk/src/emx/src/lib/io/eof.c	(revision 729)
+++ trunk/src/emx/src/lib/io/eof.c	(revision 732)
@@ -9,5 +9,5 @@
 {
   int *pflags;
-  long cur, len;
+  off_t cur, len;
 
   if ((pflags = _fd_flags (handle)) == NULL)
Index: trunk/src/emx/src/lib/io/fflush.c
===================================================================
--- trunk/src/emx/src/lib/io/fflush.c	(revision 729)
+++ trunk/src/emx/src/lib/io/fflush.c	(revision 732)
@@ -15,5 +15,5 @@
 {
   int result, n, ft, saved_errno;
-  long pos;
+  off_t pos;
 
   result = 0;
@@ -52,5 +52,5 @@
          write error occurs, otherwise zero." */
       pos = _ftell_nolock (stream);
-      if (pos != -1L)
+      if (pos != -1)
         lseek (stream->_handle, pos, SEEK_SET);
     }
Index: trunk/src/emx/src/lib/io/fgetpos.c
===================================================================
--- trunk/src/emx/src/lib/io/fgetpos.c	(revision 729)
+++ trunk/src/emx/src/lib/io/fgetpos.c	(revision 732)
@@ -6,12 +6,10 @@
 int _STD(fgetpos) (FILE *stream, fpos_t *pos)
 {
-  long n;
-
-  if ((n = ftell (stream)) == (long)EOF)
+  __off_t off;
+  *pos = EOF;                           /* crash test */
+  off = ftello (stream);
+  if (off == EOF)
     return EOF;
-  pos->_pos = n;
-  pos->_reserved1 = 0;
-  pos->_mbstate = stream->_mbstate;
-  pos->_reserved2 = 0;
+  *pos = (fpos_t)off;
   return 0;
 }
Index: trunk/src/emx/src/lib/io/fileleng.c
===================================================================
--- trunk/src/emx/src/lib/io/fileleng.c	(revision 729)
+++ trunk/src/emx/src/lib/io/fileleng.c	(revision 732)
@@ -5,13 +5,13 @@
 #include <emx/syscalls.h>
 
-long _STD(filelength) (int handle)
+off_t _STD(filelength) (int handle)
 {
-  long cur, n;
+  off_t offcur, offend;
 
-  cur = (long)__lseek (handle, 0L, SEEK_CUR);
-  if (cur == -1L)
+  offcur = __lseek (handle, 0L, SEEK_CUR);
+  if (offcur == -1)
     return -1;
-  n = (long)__lseek (handle, 0L, SEEK_END);
-  __lseek (handle, cur, SEEK_SET);
-  return n;
+  offend = __lseek (handle, 0L, SEEK_END);
+  __lseek (handle, offcur, SEEK_SET);
+  return offend;
 }
Index: trunk/src/emx/src/lib/io/fseek.c
===================================================================
--- trunk/src/emx/src/lib/io/fseek.c	(revision 729)
+++ trunk/src/emx/src/lib/io/fseek.c	(revision 732)
@@ -11,7 +11,7 @@
 #include <emx/io.h>
 
-int _fseek_nolock (FILE *stream, long offset, int origin)
+int _fseek_nolock (FILE *stream, off_t offset, int origin)
 {
-  long cur_pos;
+  off_t cur_pos;
   int fflush_result, *pflags;
 
@@ -35,5 +35,5 @@
       && bbuf (stream) && stream->_ungetc_count == 0)
     {
-      long file_pos, end_pos, buf_pos;
+      off_t file_pos, end_pos, buf_pos;
       int text_mode, n;
 
@@ -48,5 +48,5 @@
       else if (origin == SEEK_END)
         {
-          end_pos = lseek (stream->_handle, 0L, SEEK_END);
+          end_pos = lseek (stream->_handle, 0, SEEK_END);
           lseek (stream->_handle, file_pos, SEEK_SET);
           if (end_pos == -1)
@@ -96,5 +96,5 @@
               if (offset >= buf_pos)
                 {
-                  long tmp_pos;
+                  off_t tmp_pos;
 
                   /* The new position is within the buffer.  Adjust
@@ -179,11 +179,16 @@
 
 
+int _STD(fseeko) (FILE *stream, off_t offset, int origin)
+{
+    int result;
+
+    STREAM_LOCK (stream);
+    result = _fseek_nolock (stream, offset, origin);
+    STREAM_UNLOCK (stream);
+    return result;
+}
+
 int _STD(fseek) (FILE *stream, long offset, int origin)
 {
-  int result;
-
-  STREAM_LOCK (stream);
-  result = _fseek_nolock (stream, offset, origin);
-  STREAM_UNLOCK (stream);
-  return result;
+  return fseeko (stream, (off_t)offset, origin);
 }
Index: trunk/src/emx/src/lib/io/fsetpos.c
===================================================================
--- trunk/src/emx/src/lib/io/fsetpos.c	(revision 729)
+++ trunk/src/emx/src/lib/io/fsetpos.c	(revision 732)
@@ -6,7 +6,6 @@
 int _STD(fsetpos) (FILE *stream, const fpos_t *pos)
 {
-  if (fseek (stream, pos->_pos, SEEK_SET) != 0)
+  if (fseeko (stream, (__off_t)*pos, SEEK_SET) != 0)
     return -1;
-  stream->_mbstate = pos->_mbstate;
   return 0;
 }
Index: trunk/src/emx/src/lib/io/ftell.c
===================================================================
--- trunk/src/emx/src/lib/io/ftell.c	(revision 729)
+++ trunk/src/emx/src/lib/io/ftell.c	(revision 732)
@@ -11,8 +11,8 @@
 #include <emx/io.h>
 
-long _ftell_nolock (FILE *stream)
+off_t _ftell_nolock (FILE *stream)
 {
-  long pos;
-  int *pflags;
+  off_t pos;
+  int * pflags;
 
   if (stream->_flags & _IOSPECIAL)
@@ -98,12 +98,26 @@
 }
 
+__off_t _STD(ftello) (FILE *stream)
+{
+  off_t off;
+
+  STREAM_LOCK (stream);
+  off = _ftell_nolock (stream);
+  STREAM_UNLOCK (stream);
+  return off;
+}
 
 long _STD(ftell) (FILE *stream)
 {
-  long r;
-
-  STREAM_LOCK (stream);
-  r = _ftell_nolock (stream);
-  STREAM_UNLOCK (stream);
-  return r;
+  off_t off = ftello (stream);
+#if OFF_MAX > LONG_MAX
+  if (off > LONG_MAX)
+    {
+      errno = EOVERFLOW;
+      return -1;
+    }
+  return (long)off;
+#else
+  return off;
+#endif
 }
Index: trunk/src/emx/src/lib/io/lseek.c
===================================================================
--- trunk/src/emx/src/lib/io/lseek.c	(revision 729)
+++ trunk/src/emx/src/lib/io/lseek.c	(revision 732)
@@ -10,5 +10,5 @@
 {
   int *pflags, *pla;
-  long n, cur;
+  off_t n, cur;
 
   if ((pflags = _fd_flags (handle)) == NULL
@@ -28,8 +28,8 @@
           return -1;
         }
-      cur = (long)__lseek (handle, 0L, SEEK_CUR);
+      cur = __lseek (handle, 0, SEEK_CUR);
       if (cur == -1L)
         return -1;
-      n = (long)__lseek (handle, 0L, origin);
+      n = __lseek (handle, 0, origin);
       if (n + offset < 0)
         {
@@ -40,6 +40,6 @@
       offset += n; origin = SEEK_SET;
     }
-  n = (long)__lseek (handle, offset, origin);
-  if (n == -1L)
+  n = __lseek (handle, offset, origin);
+  if (n == -1)
     return -1;
   else
Index: trunk/src/emx/src/lib/io/open.c
===================================================================
--- trunk/src/emx/src/lib/io/open.c	(revision 729)
+++ trunk/src/emx/src/lib/io/open.c	(revision 732)
@@ -7,4 +7,7 @@
 #include <emx/io.h>
 
+/**
+ * @remark O_SIZE means a off_t sized argument not unsigned long as emxlib say.
+ */
 int _STD(open) (const char *name, int oflag, ...)
 {
Index: trunk/src/emx/src/lib/io/rewind.c
===================================================================
--- trunk/src/emx/src/lib/io/rewind.c	(revision 729)
+++ trunk/src/emx/src/lib/io/rewind.c	(revision 732)
@@ -13,5 +13,5 @@
   STREAM_LOCK (stream);
   _fflush_nolock (stream);
-  _fseek_nolock (stream, 0L, SEEK_SET);
+  _fseek_nolock (stream, 0, SEEK_SET);
   stream->_flags &= ~_IOERR;
   STREAM_UNLOCK (stream);
Index: trunk/src/emx/src/lib/io/setbuf.c
===================================================================
--- trunk/src/emx/src/lib/io/setbuf.c	(revision 729)
+++ trunk/src/emx/src/lib/io/setbuf.c	(revision 732)
@@ -4,6 +4,6 @@
 #include <stdio.h>
 
-int _STD(setbuf) (FILE *stream, char *buffer)
+void _STD(setbuf) (FILE *stream, char *buffer)
 {
-  return setvbuf (stream, buffer, (buffer != NULL ? _IOFBF : _IONBF), BUFSIZ);
+  setvbuf (stream, buffer, (buffer != NULL ? _IOFBF : _IONBF), BUFSIZ);
 }
Index: trunk/src/emx/src/lib/io/setbuffe.c
===================================================================
--- trunk/src/emx/src/lib/io/setbuffe.c	(revision 729)
+++ trunk/src/emx/src/lib/io/setbuffe.c	(revision 732)
@@ -4,6 +4,6 @@
 #include <stdio.h>
 
-int _STD(setbuffer) (FILE *stream, char *buffer, size_t size)
+void _STD(setbuffer) (FILE *stream, char *buffer, int size)
 {
-  return setvbuf (stream, buffer, (buffer != NULL ? _IOFBF : _IONBF), size);
+  setvbuf (stream, buffer, (buffer != NULL ? _IOFBF : _IONBF), (size_t)size);
 }
Index: trunk/src/emx/src/lib/io/sopen.c
===================================================================
--- trunk/src/emx/src/lib/io/sopen.c	(revision 729)
+++ trunk/src/emx/src/lib/io/sopen.c	(revision 732)
@@ -6,4 +6,7 @@
 #include <emx/io.h>
 
+/**
+ * @remark O_SIZE means a off_t sized argument not unsigned long as emxlib say.
+ */
 int _STD(sopen) (const char *name, int oflag, int shflag, ...)
 {
Index: trunk/src/emx/src/lib/io/tell.c
===================================================================
--- trunk/src/emx/src/lib/io/tell.c	(revision 729)
+++ trunk/src/emx/src/lib/io/tell.c	(revision 732)
@@ -7,8 +7,8 @@
 #include <emx/syscalls.h>
 
-long _STD(tell) (int handle)
+off_t _STD(tell) (int handle)
 {
   int *pflags, *pla;
-  long n;
+  off_t n;
 
   if ((pflags = _fd_flags (handle)) == NULL
@@ -18,5 +18,5 @@
       return -1;
     }
-  n = (long)__lseek (handle, 0L, SEEK_CUR);
+  n = __lseek (handle, 0L, SEEK_CUR);
   if (n == -1)
     return n;
Index: trunk/src/emx/src/lib/io/write.c
===================================================================
--- trunk/src/emx/src/lib/io/write.c	(revision 729)
+++ trunk/src/emx/src/lib/io/write.c	(revision 732)
@@ -122,9 +122,9 @@
       else
         {
-          long pos;
+          off_t pos;
           int saved_errno;
 
           saved_errno = errno;
-          pos = __lseek (handle, -1L, SEEK_CUR);
+          pos = __lseek (handle, -1, SEEK_CUR);
           if (pos != -1)
             __ftruncate (handle, pos);
@@ -148,5 +148,5 @@
     }
   if ((*pflags & (F_DEV|F_SOCKET|F_PIPE|O_APPEND)) == O_APPEND)
-    __lseek (handle, 0L, SEEK_END);
+    __lseek (handle, 0, SEEK_END);
   if (nbyte == 0)                 /* Avoid truncation of file */
     return 0;
Index: trunk/src/emx/src/lib/libc.def
===================================================================
--- trunk/src/emx/src/lib/libc.def	(revision 729)
+++ trunk/src/emx/src/lib/libc.def	(revision 732)
@@ -73,4 +73,8 @@
     "___locale_time" @53
     "__sys_virtual_address_limit" @54
+    "___pfnDosOpenL" @55
+    "___pfnDosSetFileLocksL" @56
+    "___pfnDosSetFilePtrL" @57
+    "___pfnDosSetFileSizeL" @58
 
 ; code
@@ -808,2 +812,6 @@
     "__std_unsetenv" @831
     "__std_lockf" @832
+    "___init_largefileio" @833
+    "__std_fseeko" @834
+    "__std_ftello" @835
+    "__std_readdir_r" @836
Index: trunk/src/emx/src/lib/misc/dirent.c
===================================================================
--- trunk/src/emx/src/lib/misc/dirent.c	(revision 729)
+++ trunk/src/emx/src/lib/misc/dirent.c	(revision 732)
@@ -12,195 +12,227 @@
 #include <emx/syscalls.h>
 
-struct _dircontents
-{
-  char *                _d_entry;
-  long                  _d_size;
-  unsigned short        _d_attr;
-  unsigned short        _d_time;
-  unsigned short        _d_date;
-  struct _dircontents * _d_next;
-};
-
-struct _dirdesc
-{
-  int                   dd_id;
-  long                  dd_loc;
-  struct _dircontents * dd_contents;
-  struct _dircontents * dd_cp;
-  struct dirent         dd_dirent;
-};
-
 
 static int _readdir_ino = 31415926;
 
-static void free_dircontents (struct _dircontents *dp);
-static char *getdirent (const char *dir, struct _find *pfind,
-    const char *base);
-
-int _STD(closedir) (DIR *dirp)
-{
-  free_dircontents (dirp->dd_contents);
-  free (dirp);
-  return 0;
-}
-
-
-struct dirent *_STD(readdir) (DIR *dirp)
-{
-  if (dirp->dd_cp == NULL)
+/*******************************************************************************
+*   Internal Functions                                                         *
+*******************************************************************************/
+static void free_dircontents(struct _dircontents *dp);
+static char *getdirent(const char *dir, struct _find *pfind, const char *base);
+
+
+static void free_dircontents(struct _dircontents *dp)
+{
+    while (dp != NULL)
+    {
+        void *pv = dp;
+        dp = dp->_d_next;
+        free(pv);
+    }
+}
+
+static char *getdirent(const char *dir, struct _find *pfind, const char *base)
+{
+    int rc;
+
+    if (dir != NULL)
+        rc = __findfirst(dir, A_DIR | A_HIDDEN | A_SYSTEM, pfind);
+    else
+        rc = __findnext(pfind);
+    if (rc)
+        return NULL;
+
+    _fnlwr2(pfind->szName, base);
+    return pfind->szName;
+}
+
+DIR *_STD(opendir)(const char *name)
+{
+    struct stat             statb;
+    struct _find            find;
+    DIR *                   pdir;
+    char *                  psz;
+    char                    nbuf[MAXPATHLEN+1];
+    int                     cchname;
+    int                     saved_errno = errno;
+
+    /*
+     * Put directory name in nbuf.
+     */
+    cchname = strlen(name);
+    memcpy(nbuf, name, cchname + 1);
+    psz = nbuf + cchname;
+    if (_trslash (nbuf, cchname, 0))
+    {
+        nbuf[cchname++] = '.';        /* psz now points to '.' */
+        nbuf[cchname] = 0;
+    }
+
+    /*
+     * Check that the directory exists and is a directory.
+     */
+    if (stat(nbuf, &statb) < 0)
+        return NULL;
+    if ((statb.st_mode & S_IFMT) != S_IFDIR)
+    {
+        errno = ENOTDIR;
+        return NULL;
+    }
+
+    /*
+     * Start enumeration.
+     * We're reading _everything_ now, which isn't such an good idea for
+     * large directories actually. (I wouldn't try this on the FreeDB tree.)
+     */
+    pdir = malloc(sizeof(DIR));
+    if (pdir == NULL)
+    {
+        errno = ENOMEM;
+        return NULL;
+    }
+    if (!*psz) /* If dot was added. */
+        *psz++ = '\\';
+    strcpy(psz, "*.*");
+    pdir->dd_loc = 0;
+    pdir->dd_contents = NULL;
+    pdir->dd_cp = NULL;
+    psz = getdirent(nbuf, &find, name);
+    if (psz == NULL)
+    {
+        errno = ENOMEM;
+        return pdir; /* why no free and return? */
+    }
+
+    do
+    {
+        struct _dircontents *   dp;
+        int                     cch;
+
+        /*
+         * Allocate memory for the directory entry.
+         * To limit number of heap blocks and malloc+free calls, we allocate
+         * on chunk of memory, placing the name after the struct.
+         */
+        cch = strlen(psz) + 1;
+        dp = malloc(sizeof(struct _dircontents) + cch);
+        if (dp == NULL)
+        {
+            free_dircontents(pdir->dd_contents);
+            errno = ENOMEM;
+            return NULL;
+        }
+        dp->_d_entry = (char*)(dp + 1);
+
+        /*
+         * Enter the data.
+         */
+        memcpy(dp->_d_entry, psz, cch);
+        if (pdir->dd_contents != NULL)
+            pdir->dd_cp->_d_next = dp;
+        else
+            pdir->dd_contents = dp;
+        pdir->dd_cp = dp;
+        dp->_d_next = NULL;
+        dp->_d_size = find.cbFile;
+        dp->_d_attr = find.attr;
+        dp->_d_time = find.time;
+        dp->_d_date = find.date;
+
+        /*
+         * Next entry.
+         */
+        psz = getdirent(NULL, &find, name);
+    } while (psz != NULL);
+
+    /*
+     * Position at start
+     */
+    pdir->dd_cp = pdir->dd_contents;
+    errno = saved_errno; /* getdirent/findnext sets errno when done */
+    return pdir;
+}
+
+
+int _STD(readdir_r)(DIR *dirp, struct dirent *pdent, struct dirent **ppdent)
+{
+    int cch;
+    /* done? */
+    if (dirp->dd_cp == NULL)
+    {
+        errno = ENOENT;
+        *ppdent = NULL;
+        return -1;
+    }
+
+    /*
+     * Copy the data over to the entry.
+     */
+    cch = strlen(dirp->dd_cp->_d_entry);
+    memcpy(pdent->d_name, dirp->dd_cp->_d_entry, cch + 1);
+    pdent->d_namlen = cch;
+    pdent->d_reclen = pdent->d_namlen;
+    pdent->d_ino = _readdir_ino++;
+    if (_readdir_ino == 0)
+        _readdir_ino = 1;
+    pdent->d_type = (pdent->d_attr & A_DIR) ? DT_DIR : DT_REG;
+    pdent->d_size = dirp->dd_cp->_d_size;
+    pdent->d_attr = dirp->dd_cp->_d_attr;
+    pdent->d_time = dirp->dd_cp->_d_time;
+    pdent->d_date = dirp->dd_cp->_d_date;
+
+    /*
+     * Advance the stream and return successfully.
+     */
+    dirp->dd_cp = dirp->dd_cp->_d_next;
+    dirp->dd_loc++;
+    *ppdent = pdent;
+    return 0;
+}
+
+
+struct dirent *_STD(readdir)(DIR *dirp)
+{
+    struct dirent *p = &dirp->dd_dirent;
+    if (!readdir_r(dirp, p, &p))
+        return p;
     return NULL;
-  strcpy (dirp->dd_dirent.d_name, dirp->dd_cp->_d_entry);
-  dirp->dd_dirent.d_namlen = strlen (dirp->dd_dirent.d_name);
-  dirp->dd_dirent.d_reclen = dirp->dd_dirent.d_namlen;
-  dirp->dd_dirent.d_ino = _readdir_ino++;
-  if (_readdir_ino == 0)
-    _readdir_ino = 1;
-  dirp->dd_dirent.d_size = dirp->dd_cp->_d_size;
-  dirp->dd_dirent.d_attr = dirp->dd_cp->_d_attr;
-  dirp->dd_dirent.d_time = dirp->dd_cp->_d_time;
-  dirp->dd_dirent.d_date = dirp->dd_cp->_d_date;
-  dirp->dd_cp = dirp->dd_cp->_d_next;
-  ++dirp->dd_loc;
-  return &dirp->dd_dirent;
-}
-
-
-void _STD(seekdir) (DIR *dirp, long off)
-{
-  long i;
-  struct _dircontents *dp;
-
-  if (off >= 0)
-    {
-      i = 0;
-      for (dp = dirp->dd_contents; i < off && dp != NULL; dp = dp->_d_next)
-        ++i;
-      dirp->dd_loc = i;
-      dirp->dd_cp = dp;
-    }
-  else
-    errno = EINVAL;
-}
-
-
-void _STD(rewinddir) (DIR *dirp)
-{
-  _seekdir (dirp, 0L);
-}
-
-
-long _STD(telldir) (DIR *dirp)
-{
-  return dirp->dd_loc;
-}
-
-
-DIR *_STD(opendir) (const char *name)
-{
-  struct stat statb;
-  struct _find find;
-  DIR *dirp;
-  char *s;
-  struct _dircontents *dp;
-  char nbuf[MAXPATHLEN+1];
-  int len;
-
-  len = strlen (name);
-  memcpy (nbuf, name, len + 1);
-  s = nbuf + len;
-  if (_trslash (nbuf, len, 0))
-    {
-      nbuf[len++] = '.';        /* s now points to '.' */
-      nbuf[len] = 0;
-    }
-  if (stat (nbuf, &statb) < 0)
-    return NULL;
-  if ((statb.st_mode & S_IFMT) != S_IFDIR)
-    {
-      errno = ENOTDIR;
-      return NULL;
-    }
-  dirp = malloc (sizeof (DIR));
-  if (dirp == NULL)
-    {
-      errno = ENOMEM;
-      return NULL;
-    }
-  if (*s == 0)
-    *s++ = '\\';
-  strcpy (s, "*.*");
-  dirp->dd_loc = 0;
-  dirp->dd_contents = NULL;
-  dirp->dd_cp = NULL;
-  s = getdirent (nbuf, &find, name);
-  if (s == NULL)
-    {
-      errno = ENOMEM;
-      return dirp;
-    }
-  do
-    {
-      dp = malloc (sizeof (struct _dircontents));
-      if (dp == NULL)
-        {
-          free_dircontents (dirp->dd_contents);
-          errno = ENOMEM;
-          return NULL;
-        }
-      dp->_d_entry = malloc (strlen (s) + 1);
-      if (dp->_d_entry == NULL)
-        {
-          free (dp);
-          free_dircontents (dirp->dd_contents);
-          errno = ENOMEM;
-          return NULL;
-        }
-      if (dirp->dd_contents != NULL)
-        dirp->dd_cp->_d_next = dp;
-      else
-        dirp->dd_contents = dp;
-      dirp->dd_cp = dp;
-      strcpy (dp->_d_entry, s);
-      dp->_d_next = NULL;
-      dp->_d_size = ((unsigned long)find.size_hi << 16) + find.size_lo;
-      dp->_d_attr = find.attr;
-      dp->_d_time = find.time;
-      dp->_d_date = find.date;
-      s = getdirent (NULL, &find, name);
-    } while (s != NULL);
-  dirp->dd_cp = dirp->dd_contents;
-  return dirp;
-}
-
-
-static void free_dircontents (struct _dircontents *dp)
-{
-  struct _dircontents *odp;
-
-  while (dp != NULL)
-    {
-      if (dp->_d_entry != NULL)
-        free(dp->_d_entry);
-      odp = dp;
-      dp = dp->_d_next;
-      free (odp);
-    }
-}
-
-
-static char *getdirent (const char *dir, struct _find *pfind, const char *base)
-{
-  int rc;
-
-  if (dir != NULL)
-    rc = __findfirst (dir, A_DIR|A_HIDDEN|A_SYSTEM, pfind);
-  else
-    rc = __findnext (pfind);
-  if (rc == 0)
-    {
-      _fnlwr2 (pfind->name, base);
-      return pfind->name;
-    }
-  else
-    return NULL;
-}
+}
+
+
+void _STD(seekdir)(DIR *dirp, long off)
+{
+    if (off >= 0)
+    {
+        long                i;
+        struct _dircontents *dp;
+
+        i = 0;
+        for (dp = dirp->dd_contents; i < off && dp != NULL; dp = dp->_d_next)
+            ++i;
+        dirp->dd_loc = i;
+        dirp->dd_cp = dp;
+    }
+    else
+        errno = EINVAL;
+}
+
+
+void _STD(rewinddir)(DIR *dirp)
+{
+    _seekdir(dirp, 0);
+}
+
+
+long _STD(telldir)(DIR *dirp)
+{
+    return dirp->dd_loc;
+}
+
+
+int _STD(closedir)(DIR *dirp)
+{
+    free_dircontents(dirp->dd_contents);
+    free(dirp);
+    return 0;
+}
+
Index: trunk/src/emx/src/lib/misc/dtread.c
===================================================================
--- trunk/src/emx/src/lib/misc/dtread.c	(revision 729)
+++ trunk/src/emx/src/lib/misc/dtread.c	(revision 732)
@@ -57,6 +57,6 @@
 static int _dt_match (struct _dt_rdata *dp)
 {
-  if ((dp->flags & _DT_NOCPDIR) && (strcmp (dp->find.name, ".") == 0 ||
-                                    strcmp (dp->find.name, "..") == 0))
+  if ((dp->flags & _DT_NOCPDIR) && (strcmp (dp->find.szName, ".") == 0 ||
+                                    strcmp (dp->find.szName, "..") == 0))
     return 0;
   return 1;
@@ -66,25 +66,25 @@
 static struct _dt_node *_dt_add (struct _dt_rdata *dp)
 {
-  struct _dt_node *new;
+  struct _dt_node *pnew;
   struct tm tm;
 
-  new = malloc (sizeof (*new));
-  if (new == NULL)
+  pnew = malloc (sizeof (*pnew));
+  if (pnew == NULL)
     {
       errno = ENOMEM;
       return NULL;
     }
-  new->next = NULL;
-  new->sub = NULL;
-  new->name = strdup (dp->find.name);
-  if (new->name == NULL)
+  pnew->next = NULL;
+  pnew->sub = NULL;
+  pnew->name = strdup (dp->find.szName);
+  if (pnew->name == NULL)
     {
       errno = ENOMEM;
       return NULL;
     }
-  _fnlwr2 (new->name, dp->dir);
-  new->size = dp->find.size_lo + ((long)dp->find.size_hi << 16);
-  new->user = 0;
-  new->attr = dp->find.attr;
+  _fnlwr2 (pnew->name, dp->dir);
+  pnew->size = dp->find.cbFile;
+  pnew->user = 0;
+  pnew->attr = dp->find.attr;
   tm.tm_sec = (dp->find.time & 0x1f) * 2;
   tm.tm_min = (dp->find.time >> 5) & 0x3f;
@@ -94,7 +94,7 @@
   tm.tm_year = ((dp->find.date >> 9) & 0x7f) + 1980 - 1900;
   tm.tm_isdst = -1;             /* unknown */
-  new->mtime = _mktime (&tm);
-  _loc2gmt (&new->mtime, -1);
-  return new;
+  pnew->mtime = _mktime (&tm);
+  _loc2gmt (&pnew->mtime, -1);
+  return pnew;
 }
 
@@ -103,5 +103,5 @@
                              int path_len)
 {
-  struct _dt_node *new, **add;
+  struct _dt_node *pnew, **add;
   int r, len2;
 
@@ -114,9 +114,9 @@
       if (!(dp->dir_pass && (dp->find.attr & A_DIR)) && _dt_match (dp))
         {
-          new = _dt_add (dp);
-          if (new == NULL)
+          pnew = _dt_add (dp);
+          if (pnew == NULL)
             return -1;
-          *add = new;
-          add = &new->next;
+          *add = pnew;
+          add = &pnew->next;
         }
       r = __findnext (&dp->find);
@@ -132,9 +132,9 @@
           if ((dp->find.attr & A_DIR) && _dt_match (dp))
             {
-              new = _dt_add (dp);
-              if (new == NULL)
+              pnew = _dt_add (dp);
+              if (pnew == NULL)
                 return -1;
-              *add = new;
-              add = &new->next;
+              *add = pnew;
+              add = &pnew->next;
             }
           r = __findnext (&dp->find);
@@ -144,12 +144,12 @@
     }
   if (dp->flags & _DT_TREE)
-    for (new = *dst; new != NULL; new = new->next)
-      if ((new->attr & A_DIR) && strcmp (new->name, ".") != 0
-          && strcmp (new->name, "..") != 0)
+    for (pnew = *dst; pnew != NULL; pnew = pnew->next)
+      if ((pnew->attr & A_DIR) && strcmp (pnew->name, ".") != 0
+          && strcmp (pnew->name, "..") != 0)
         {
-          len2 = strlen (new->name);
-          strcpy (dp->path + path_len, new->name);
+          len2 = strlen (pnew->name);
+          strcpy (dp->path + path_len, pnew->name);
           dp->path[path_len+len2] = '/';
-          if (_dt_read_recurse (dp, &new->sub, path_len+len2+1) < 0)
+          if (_dt_read_recurse (dp, &pnew->sub, path_len+len2+1) < 0)
             return -1;
         }
Index: trunk/src/emx/src/lib/misc/fnexplod.c
===================================================================
--- trunk/src/emx/src/lib/misc/fnexplod.c	(revision 729)
+++ trunk/src/emx/src/lib/misc/fnexplod.c	(revision 732)
@@ -32,6 +32,7 @@
   do
     {
-      if (strcmp (find.name, ".") != 0 &&
-          strcmp (find.name, "..") != 0)
+      if (   find.szName[0] != '.'
+          || (   find.szName[1] != '\0'
+              && (find.szName[1] != '.' || find.szName[2] != '\0')))
         {
           if (n + 1 >= a)
@@ -46,5 +47,5 @@
               list = tmp;
             }
-          strcpy (p, find.name);
+          strcpy (p, find.szName);
           _fnlwr2 (name, name);
           q = strdup (name);
@@ -61,5 +62,5 @@
   return list;
 
-failure:            
+failure:
   if (list != NULL)
     {
Index: trunk/src/emx/src/lib/misc/wildcard.c
===================================================================
--- trunk/src/emx/src/lib/misc/wildcard.c	(revision 729)
+++ trunk/src/emx/src/lib/misc/wildcard.c	(revision 732)
@@ -52,8 +52,9 @@
           do
             {
-              if (strcmp (find.name, ".") != 0 &&
-                  strcmp (find.name, "..") != 0)
+              if (   find.szName[0] != '.'
+                  || (   find.szName[1] != '\0'
+                      && (find.szName[1] != '.' || find.szName[2] != '\0')))
                 {
-                  strcpy (p, find.name);
+                  strcpy (p, find.szName);
                   _fnlwr2 (p, line+1);
                   q = strdup (line);
Index: trunk/src/emx/src/lib/sys/__chsize.c
===================================================================
--- trunk/src/emx/src/lib/sys/__chsize.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__chsize.c	(revision 732)
@@ -1,19 +1,40 @@
-/* sys/chsize.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/chsize.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   Copyright (c) 2003 Dimitry Froloff
+   Copyright (c) 2003 knut st. osmundsen
+ */
 
 #include "libc-alias.h"
 #include <os2emx.h>
 #include <emx/syscalls.h>
+#include <limits.h>
+#include <errno.h>
 #include "syscalls.h"
 
-int __chsize (int handle, long length)
+
+int __chsize (int hFile, __off_t cbFile)
 {
-  ULONG rc;
+    ULONG rc;
 
-  rc = DosSetFileSize (handle, length);
-  if (rc != 0)
+#if OFF_MAX > LONG_MAX
+    if (__pfnDosSetFileSizeL)
+        rc = __pfnDosSetFileSizeL(hFile, cbFile);
+    else
     {
-      _sys_set_errno (rc);
-      return -1;
+        if (cbFile > LONG_MAX)
+        {
+            errno = EOVERFLOW;
+            return -1;
+        }
+        rc = DosSetFileSize(hFile, cbFile);
     }
-  return 0;
+#else
+    rc = DosSetFileSize(hFile, cbFile);
+#endif
+
+    if (rc)
+    {
+        _sys_set_errno(rc);
+        return -1;
+    }
+    return 0;
 }
Index: trunk/src/emx/src/lib/sys/__endthread.c
===================================================================
--- trunk/src/emx/src/lib/sys/__endthread.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__endthread.c	(revision 732)
@@ -14,6 +14,9 @@
     {
       tp = _sys_thread_table[tid];
-      if (tp->fd.find_handle != HDIR_CREATE)
-        DosFindClose (tp->fd.find_handle);
+      if (tp->fd.hdir != HDIR_CREATE)
+        {
+          DosFindClose (tp->fd.hdir);
+          tp->fd.hdir = HDIR_CREATE;
+        }
       DosSubFreeMem (_sys_private_heap, tp, sizeof (thread_data));
       _sys_thread_table[tid] = NULL;
Index: trunk/src/emx/src/lib/sys/__fcntl.c
===================================================================
--- trunk/src/emx/src/lib/sys/__fcntl.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__fcntl.c	(revision 732)
@@ -69,8 +69,15 @@
    it's take from elsewhere. */
 
-static int __fcntl_locking (int handle, int request, struct flock *pflock)
+static int __fcntl_locking (int hFile, int request, struct flock *pflock)
 {
     APIRET        rc;
-    FILESTATUS3   fsts3;
+    union
+    {
+        FILESTATUS3     fsts3;
+        FILESTATUS3L    fsts3L;
+    } info;
+#if OFF_MAX > LONG_MAX
+    int     fLarge = 0;
+#endif
 
     /* check input */
@@ -82,33 +89,48 @@
     }
 
-    /* check handle & get filesize. */
-    rc = DosQueryFileInfo (handle, FIL_STANDARD, &fsts3, sizeof(fsts3));
+    /* check hFile & get filesize. */
+#if OFF_MAX > LONG_MAX
+    if (__pfnDosOpenL)
+    {
+        rc = DosQueryFileInfo(hFile, FIL_STANDARDL, &info, sizeof(info.fsts3L));
+        fLarge = 1;
+    }
+    else
+#endif
+        rc = DosQueryFileInfo(hFile, FIL_STANDARD, &info, sizeof(info.fsts3));
     if (!rc)
     {
-        FILELOCK      FileLockDummy = {0, 0};
-        FILELOCK      FileLock;
-        PFILELOCK     pFLLock;
-        PFILELOCK     pFLUnlock;
-        ULONG         fAccess;
-        ULONG         ulTimeout;
+        ULONG       fAccess;
+        int         fLock;
+        ULONG       ulTimeout;
+        off_t       cbFile;
+        off_t       offStart;
+        off_t       cbRange;
+#if OFF_MAX > LONG_MAX
+        if (fLarge)
+            cbFile = info.fsts3L.cbFile;
+        else
+#endif
+            cbFile = info.fsts3.cbFile;
+
+        /* range */
+        cbRange = pflock->l_len ? pflock->l_len : OFF_MAX;
 
         /* offset */
         switch (pflock->l_whence)
         {
-            case SEEK_SET:  FileLock.lOffset = pflock->l_start; break;
-            case SEEK_CUR:  FileLock.lOffset = tell (handle) + pflock->l_start; break;
-            case SEEK_END:  FileLock.lOffset = fsts3.cbFile - pflock->l_start; break;
+            case SEEK_SET:  offStart = pflock->l_start; break;
+            case SEEK_CUR:  offStart = tell(hFile) + pflock->l_start; break;
+            case SEEK_END:  offStart = cbFile - pflock->l_start; break;
             default:
                 errno = EINVAL;
                 return -1;
         }
-        if (FileLock.lOffset < 0)
+        if (    offStart < 0
+            ||  cbRange + offStart < 0 )
         {
             errno = EINVAL;
             return -1;
         }
-
-        /* range */
-        FileLock.lRange = pflock->l_len ? pflock->l_len : LONG_MAX;
 
         /* flags and order */
@@ -117,6 +139,5 @@
         {
             case F_UNLCK:
-                pFLLock   = &FileLockDummy;
-                pFLUnlock = &FileLock;
+                fLock = 0;
                 break;
 
@@ -124,6 +145,5 @@
                 fAccess = 1; /* shared */
             case F_WRLCK:
-                pFLLock   = &FileLock;
-                pFLUnlock = &FileLockDummy;
+                fLock = 1;
                 break;
 
@@ -140,5 +160,33 @@
 
         /* Do work. */
-        rc = DosSetFileLocks (handle, pFLUnlock, pFLLock, ulTimeout, fAccess);
+#if OFF_MAX > LONG_MAX
+        if (__pfnDosSetFileLocksL)
+        {
+            FILELOCKL   aflock[2];
+            memset(&aflock[(fLock + 1) & 1], 0, sizeof(aflock[0]));
+            aflock[fLock].lOffset = offStart;
+            aflock[fLock].lRange  = cbRange;
+            rc = __pfnDosSetFileLocksL(hFile, &aflock[0], &aflock[1], ulTimeout, fAccess);
+        }
+        else
+#endif
+        {
+            FILELOCK    aflock[2];
+#if OFF_MAX > LONG_MAX
+            if (    offStart > LONG_MAX
+                ||  cbRange > LONG_MAX
+                ||  offStart + cbRange > LONG_MAX
+                )
+              {
+                errno = EOVERFLOW;
+                return -1;
+              }
+#endif
+            memset(&aflock[(fLock + 1) & 1], 0, sizeof(aflock[0]));
+            aflock[fLock].lOffset = offStart;
+            aflock[fLock].lRange  = cbRange;
+
+            rc = DosSetFileLocks(hFile, &aflock[0], &aflock[1], ulTimeout, fAccess);
+        }
     }
 
Index: trunk/src/emx/src/lib/sys/__fstat.c
===================================================================
--- trunk/src/emx/src/lib/sys/__fstat.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__fstat.c	(revision 732)
@@ -1,3 +1,5 @@
-/* sys/fstat.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/fstat.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   sys/__fstat.c (libc)  -- Copyright (c) 2003 by knut st. osmundsen
+ */
 
 #include "libc-alias.h"
@@ -9,78 +11,124 @@
 #include "syscalls.h"
 
-int __fstat (int handle, struct stat *buf)
+int __fstat(int hFile, struct stat *pStat)
 {
-  ULONG rc;
-  ULONG htype, hflags;
-  FILESTATUS3 info;
+    ULONG rc;
+    ULONG ulType;
+    ULONG flFlags;
 
-  memset (buf, 0, sizeof (*buf));
-  rc = DosQueryHType (handle, &htype, &hflags);
-  if (rc != 0)
+    /*
+     * Use query handle type to figure out the file type.
+     */
+    memset(pStat, 0, sizeof(*pStat));
+    rc = DosQueryHType(hFile, &ulType, &flFlags);
+    if (rc)
     {
-      _sys_set_errno (rc);
-      return -1;
+        _sys_set_errno(rc);
+        return -1;
     }
-  switch (htype & 0xff)
+    switch (ulType & 0xff)
     {
-    case 1:
-      buf->st_mode = S_IFCHR;
-      break;
-    case 2:
-      buf->st_mode = S_IFSOCK;
-      break;
-    default:
-      buf->st_mode = S_IFREG;
-      break;
+        case FHT_CHRDEV:
+            pStat->st_mode = S_IFCHR;
+            break;
+        case FHT_PIPE:
+            pStat->st_mode = S_IFSOCK;
+            break;
+        default:
+            pStat->st_mode = S_IFREG;
+            break;
     }
-  if (buf->st_mode == S_IFREG)
+
+    if (pStat->st_mode == S_IFREG)
     {
-      rc = DosQueryFileInfo (handle, FIL_STANDARD, &info, sizeof (info));
-      if (rc != 0)
+        union
         {
-          _sys_set_errno (rc);
-          return -1;
+            FILESTATUS3     fsts3;
+            FILESTATUS3L    fsts3L;
+        } info;
+    #if OFF_MAX > LONG_MAX
+        int     fLarge = 0;
+    #endif
+
+        /*
+         * Get file info.
+         */
+#if OFF_MAX > LONG_MAX
+        if (__pfnDosOpenL)
+        {
+            rc = DosQueryFileInfo(hFile, FIL_STANDARDL, &info, sizeof(info.fsts3L));
+            fLarge = 1;
         }
-      buf->st_attr = info.attrFile;
-      buf->st_size = info.cbFile;
-      buf->st_reserved = 0;
-      buf->st_mtime = _sys_p2t (info.ftimeLastWrite, info.fdateLastWrite);
-      if (FTIMEZEROP (info.ftimeCreation) &&
-          FDATEZEROP (info.fdateCreation))
-        buf->st_ctime = buf->st_mtime;
-      else
-        buf->st_ctime = _sys_p2t (info.ftimeCreation, info.fdateCreation);
-      if (FTIMEZEROP (info.ftimeLastAccess) &&
-          FDATEZEROP (info.fdateLastAccess))
-        buf->st_atime = buf->st_mtime;
-      else
-        buf->st_atime = _sys_p2t (info.ftimeLastAccess, info.fdateLastAccess);
-      if (info.attrFile & 1)
-        buf->st_mode |= (S_IREAD >> 6) * 0111;
-      else
-        buf->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
+        else
+#endif
+            rc = DosQueryFileInfo(hFile, FIL_STANDARD, &info, sizeof(info.fsts3));
+        if (rc)
+        {
+            _sys_set_errno (rc);
+            return -1;
+        }
+
+        /*
+         * Format stats struct.
+         *      We know the info struct layouts!
+         *      Only cbFile, cbFileAlloc and attrFile need be accessed
+         *      using the specific structure.
+         */
+        /* Times: FAT might not return create and access time. */
+        pStat->st_mtime = _sys_p2t(info.fsts3.ftimeLastWrite, info.fsts3.fdateLastWrite);
+        if (    FTIMEZEROP(info.fsts3.ftimeCreation)
+            &&  FDATEZEROP(info.fsts3.fdateCreation))
+            pStat->st_ctime = pStat->st_mtime;
+        else
+            pStat->st_ctime = _sys_p2t(info.fsts3.ftimeCreation, info.fsts3.fdateCreation);
+        if (    FTIMEZEROP(info.fsts3.ftimeLastAccess)
+            &&  FDATEZEROP(info.fsts3.fdateLastAccess))
+            pStat->st_atime = pStat->st_mtime;
+        else
+            pStat->st_atime = _sys_p2t(info.fsts3.ftimeLastAccess, info.fsts3.fdateLastAccess);
+
+#if OFF_MAX > LONG_MAX
+        if (fLarge)
+        {
+            pStat->st_size = info.fsts3L.cbFile;
+            rc = info.fsts3L.attrFile;
+        }
+        else
+#endif
+        {
+            pStat->st_size = info.fsts3.cbFile;
+            rc = info.fsts3.attrFile;
+        }
+        pStat->st_attr = rc;
+        if (rc & FILE_READONLY)
+            pStat->st_mode |= (S_IREAD >> 6) * 0111;
+        else
+            pStat->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
     }
-  else
+    else
     {
-      buf->st_size = 0;
-      rc = DosQueryFHState (handle, &hflags);
-      if (rc != 0)
+        pStat->st_size = 0;
+        rc = DosQueryFHState(hFile, &flFlags);
+        if (rc)
         {
-          _sys_set_errno (rc);
-          return -1;
+            _sys_set_errno (rc);
+            return -1;
         }
-      if ((hflags & 7) == 0)
-        buf->st_mode |= (S_IREAD >> 6) * 0111;
-      else
-        buf->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
+        if ((flFlags & 7) == OPEN_ACCESS_READONLY)
+            pStat->st_mode |= (S_IREAD >> 6) * 0111;
+        else
+            pStat->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
     }
-  buf->st_uid = 0;
-  buf->st_gid = 0;
-  buf->st_ino = _sys_ino++;
-  if (_sys_ino == 0)
-    _sys_ino = 1;
-  buf->st_dev = 0;
-  buf->st_rdev = 0;
-  buf->st_nlink = 1;
-  return 0;
+
+    /* default fake stuff */
+    pStat->st_uid = 0;
+    pStat->st_gid = 0;
+    pStat->st_ino = _sys_ino++;
+    if (_sys_ino == 0)
+        _sys_ino = 1;
+    pStat->st_dev = 0;
+    pStat->st_rdev = 0;
+    pStat->st_nlink = 1;
+    pStat->st_reserved = 0;             /* what is this good for?!? */
+    return 0;
 }
Index: trunk/src/emx/src/lib/sys/__ftruncate.c
===================================================================
--- trunk/src/emx/src/lib/sys/__ftruncate.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__ftruncate.c	(revision 732)
@@ -1,29 +1,73 @@
-/* sys/ftruncat.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/ftruncat.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   Copyright (c) 2003 Dimitry Froloff
+   Copyright (c) 2003 knut st. osmundsen
+ */
 
 #include "libc-alias.h"
 #include <os2emx.h>
 #include <emx/syscalls.h>
+#include <limits.h>
+#include <errno.h>
 #include "syscalls.h"
 
-int __ftruncate (int handle, long length)
+int __ftruncate(int hFile, off_t cbFile)
 {
-  ULONG rc;
-  FILESTATUS3 info;
+    ULONG       rc;
+    off_t       cbCur;
+    union
+    {
+        FILESTATUS3     fsts3;
+        FILESTATUS3L    fsts3L;
+    } info;
 
-  rc = DosQueryFileInfo (handle, FIL_STANDARD, &info, sizeof (info));
-  if (rc != 0)
+    /*
+     * First step, figure out the current size.
+     */
+#if OFF_MAX > LONG_MAX
+    if (__pfnDosOpenL)
     {
-      _sys_set_errno (rc);
-      return -1;
+        rc = DosQueryFileInfo(hFile, FIL_STANDARDL, &info, sizeof(info.fsts3L));
+        cbCur = info.fsts3L.cbFile;
     }
-  if (info.cbFile > length)
+    else
+#endif
     {
-      rc = DosSetFileSize (handle, length);
-      if (rc != 0)
+        rc = DosQueryFileInfo(hFile, FIL_STANDARD, &info, sizeof(info.fsts3));
+        cbCur = info.fsts3.cbFile;
+    }
+    if (rc)
+    {
+        _sys_set_errno(rc);
+        return -1;
+    }
+
+
+    /*
+     * Is the file larger than the truncation size?
+     * Then set the desired file size.
+     */
+    if (cbCur > cbFile)
+    {
+#if OFF_MAX > LONG_MAX
+        if (__pfnDosSetFileSizeL)
+            rc = __pfnDosSetFileSizeL(hFile, cbFile);
+        else
         {
-          _sys_set_errno (rc);
-          return -1;
+            if (cbFile > __LONG_MAX)
+            {
+                errno = EOVERFLOW;
+                return -1;
+            }
+            rc = DosSetFileSize(hFile, cbFile);
+        }
+#else
+        rc = DosSetFileSize(hFile, cbFile);
+#endif
+        if (rc != 0)
+        {
+            _sys_set_errno(rc);
+            return -1;
         }
     }
-  return 0;
+    return 0;
 }
Index: trunk/src/emx/src/lib/sys/__init.c
===================================================================
--- trunk/src/emx/src/lib/sys/__init.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__init.c	(revision 732)
@@ -488,6 +488,5 @@
       __sigemptyset (&tp->signals[n].sa_mask);
     }
-  tp->fd.find_handle = HDIR_CREATE;
-  tp->fd.find_next = NULL;
-  tp->fd.find_count = 0;
-}
+  tp->fd.hdir = HDIR_CREATE;
+  tp->fd.cFiles = 0;
+}
Index: trunk/src/emx/src/lib/sys/__initdll.c
===================================================================
--- trunk/src/emx/src/lib/sys/__initdll.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__initdll.c	(revision 732)
@@ -1,3 +1,5 @@
-/*
+/* $Id$ */
+/** @file
+
     Dynamic library low-level initialization routine.
 
@@ -9,4 +11,13 @@
 */
 
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+#define SYS_PRIVATE_HEAP_SIZE 0x100000
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
 #include "libc-alias.h"
 #define INCL_DOS
@@ -21,9 +32,17 @@
 #include "syscalls.h"
 
-#define SYS_PRIVATE_HEAP_SIZE 0x100000
-
-extern int __init_environ(const char *pszEnv);
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
 extern unsigned char _osminor;
 extern unsigned char _osmajor;
+
+
+/*******************************************************************************
+*   External Functions                                                         *
+*******************************************************************************/
+extern int __init_environ(const char *pszEnv);
+extern void __init_largefileio(void) __attribute__((weak));
+
 
 /**
@@ -85,4 +104,5 @@
      * Let's try put it in high memory...
      */
+    /** @todo Redo this private heap stuff. */
     rc = DosAllocMem(&_sys_private_heap, SYS_PRIVATE_HEAP_SIZE,
                      _sys_virtual_address_limit
@@ -113,4 +133,12 @@
 
     /*
+     * Init long file I/O functions.
+     * This is weak, so that it's only linked in when we're building with
+     * large file suppoort enabled.
+     */
+    if (__init_largefileio)
+        __init_largefileio();
+
+    /*
      * Get current time for clock() for use as process startup time.
      */
@@ -118,2 +146,3 @@
     return 0;
 }
+
Index: trunk/src/emx/src/lib/sys/__lseek.c
===================================================================
--- trunk/src/emx/src/lib/sys/__lseek.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__lseek.c	(revision 732)
@@ -1,21 +1,58 @@
-/* sys/lseek.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/lseek.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   Copyright (c) 2003 Dimitry Froloff
+   Copyright (c) 2003 knut st. osmundsen
+ */
 
 #include "libc-alias.h"
 #include <os2emx.h>
 #include <emx/syscalls.h>
+#include <limits.h>
+#include <errno.h>
 #include "syscalls.h"
 
-int __lseek (int handle, long offset, int origin)
+off_t __lseek(int hFile, off_t off, int origin)
 {
-  ULONG rc;
-  ULONG new;
+    ULONG   rc;
+    off_t   cbNew;
 
-  rc = DosSetFilePtr (handle, offset, origin, &new);
-  if (rc == 0)
-    return (int)new;
-  else
+    if (origin == FILE_SECTOR)
     {
-      _sys_set_errno (rc);
-      return -1;
+        errno = EINVAL;
+        return -1;
     }
+
+#if OFF_MAX > LONG_MAX
+    if (__pfnDosSetFilePtrL)
+    {
+        LONGLONG cbNewTmp;
+        rc = __pfnDosSetFilePtrL(hFile, off, origin, &cbNewTmp);
+        cbNew = cbNewTmp;
+    }
+    else
+    {
+        ULONG cbNewTmp;
+        if (off > LONG_MAX || off < LONG_MIN)
+        {
+            errno = EOVERFLOW;
+            return -1;
+        }
+        rc = DosSetFilePtr(hFile, off, origin, &cbNewTmp);
+        cbNew = cbNewTmp;
+    }
+#else
+    {
+        ULONG cbNewTmp;
+        rc = DosSetFilePtr(hFile, off, origin, &cbNewTmp);
+        cbNew = cbNewTmp;
+    }
+#endif
+
+    if (rc)
+    {
+        _sys_set_errno (rc);
+        return -1;
+    }
+
+    return cbNew;
 }
+
Index: trunk/src/emx/src/lib/sys/__open.c
===================================================================
--- trunk/src/emx/src/lib/sys/__open.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__open.c	(revision 732)
@@ -1,3 +1,6 @@
-/* sys/open.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/open.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   Copyright (c) 2003 Dimitry Froloff
+   Copyright (c) 2003 knut st. osmundsen
+ */
 
 #include "libc-alias.h"
@@ -6,81 +9,113 @@
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
 #include <emx/syscalls.h>
 #include "syscalls.h"
 
-int __open (const char *name, int flags, unsigned long size)
+int __open(const char *pszFile, int flags, off_t cbInitial)
 {
-  ULONG rc;
-  ULONG open_mode, attr, action, open_flag;
-  HFILE handle;
-  int fail_errno;
+    ULONG   rc;
+    ULONG   flOpenMode;
+    ULONG   flOpenFlags;
+    ULONG   flAttr;
+    ULONG   ulAction;
+    HFILE   hFile;
+    int     failed_open_errno;
 
-  /* Interpret "/dev/null" as the name of the null device "NUL".
-     Interpret "/dev/tty" as the name of the console device "CON". */
+    /*
+     * Interpret "/dev/null" as the pszFile of the null device "NUL".
+     * Interpret "/dev/tty" as the pszFile of the console device "CON".
+     */
+    if (!strcmp(pszFile, "/dev/null"))
+        pszFile = "nul";
+    else if (!strcmp(pszFile, "/dev/tty"))
+        pszFile = "con";
 
-  if (strcmp (name, "/dev/null") == 0)
-    name = "nul";
-  else if (strcmp (name, "/dev/tty") == 0)
-    name = "con";
+    /*
+     * Extract the access mode and sharing mode bits.
+     */
+    flOpenMode = flags & 0x77;
 
-  /* Extract the access mode and sharing mode bits. */
+    /*
+     * File O_NOINHERIT and O_SYNC.
+     */
+    if (flags & _SO_NOINHERIT)
+        flOpenMode |= OPEN_FLAGS_NOINHERIT;
+    if (flags & _SO_SYNC)
+        flOpenMode |= OPEN_FLAGS_WRITE_THROUGH;
 
-  open_mode = flags & 0x77;
+    /*
+     * Extract the file flAttribute bits.
+     */
+    flAttr = (flags >> 8) & 0xff;
+    if (_sys_umask & 0200)
+        flAttr |= FILE_READONLY;
 
-  /* Handle O_NOINHERIT and O_SYNC. */
+    /*
+     * Translate ERROR_OPEN_FAILED to ENOENT unless O_EXCL is set (see below).
+     */
+    failed_open_errno = ENOENT;
 
-  if (flags & _SO_NOINHERIT)
-    open_mode |= OPEN_FLAGS_NOINHERIT;
-  if (flags & _SO_SYNC)
-    open_mode |= OPEN_FLAGS_WRITE_THROUGH;
+    /*
+     * Compute `flOpenFlags' depending on `flags'.  Note that _SO_CREAT is
+     * set for O_CREAT.
+     */
+    if (flags & _SO_CREAT)
+    {
+        if (flags & _SO_EXCL)
+        {
+            flOpenFlags = OPEN_ACTION_FAIL_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
+            failed_open_errno = EEXIST;
+        }
+        else if (flags & _SO_TRUNC)
+            flOpenFlags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
+        else
+            flOpenFlags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
+    }
+    else if (flags & _SO_TRUNC)
+        flOpenFlags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
+    else
+        flOpenFlags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
 
-  /* Extract the file attribute bits. */
+    if (!(flags & _SO_SIZE))
+        cbInitial = 0;
 
-  attr = (flags >> 8) & 0xff;
-  if (_sys_umask & 0200)
-    attr |= 1;
+    /*
+     * Try to open the file.
+     */
+#if OFF_MAX > LONG_MAX
+    if (__pfnDosOpenL)
+    {
+        LONGLONG cbInitialTmp = cbInitial;
+        rc = __pfnDosOpenL(pszFile, &hFile, &ulAction, cbInitialTmp, flAttr, flOpenFlags, flOpenMode, NULL);
+    }
+    else
+    {
+        ULONG cbInitialTmp = (ULONG)cbInitial;
+        if (cbInitial > LONG_MAX)
+        {
+            errno = EOVERFLOW;
+            return -1;
+        }
+        rc = DosOpen(pszFile, &hFile, &ulAction, cbInitialTmp, flAttr, flOpenFlags, flOpenMode, NULL);
+    }
+#else
+    {
+        ULONG cbInitialTmp = cbInitial;
+        rc = DosOpen(pszFile, &hFile, &ulAction, cbInitialTmp, flAttr, flOpenFlags, flOpenMode, NULL);
+    }
+#endif
 
-  /* Translate ERROR_OPEN_FAILED to ENOENT unless O_EXCL is set (see
-     below). */
-
-  fail_errno = ENOENT;
-
-  /* Compute `open_flag' depending on `flags'.  Note that _SO_CREAT is
-     set for O_CREAT. */
-
-  if (flags & _SO_CREAT)
+    /*
+     * Handle any errors.
+     */
+    if (rc)
     {
-      if (flags & _SO_EXCL)
-        {
-          open_flag = OPEN_ACTION_FAIL_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
-          fail_errno = EEXIST;
-        }
-      else if (flags & _SO_TRUNC)
-        open_flag = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
-      else
-        open_flag = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
+        if (rc == ERROR_OPEN_FAILED)
+            errno = failed_open_errno;
+        else
+            _sys_set_errno(rc);
+        return -1;
     }
-  else if (flags & _SO_TRUNC)
-    open_flag = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
-  else
-    open_flag = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
-
-  if (!(flags & _SO_SIZE))
-    size = 0;
-
-  /* Try to open the file and handle errors. */
-
-  rc = DosOpen (name, &handle, &action, size, attr, open_flag, open_mode,
-                NULL);
-  if (rc == ERROR_OPEN_FAILED)
-    {
-      errno = fail_errno;
-      return -1;
-    }
-  if (rc != 0)
-    {
-      _sys_set_errno (rc);
-      return -1;
-    }
-  return handle;
+    return hFile;
 }
Index: trunk/src/emx/src/lib/sys/__stat.c
===================================================================
--- trunk/src/emx/src/lib/sys/__stat.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/__stat.c	(revision 732)
@@ -1,3 +1,5 @@
-/* sys/stat.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/stat.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   sys/__stat.c (libc)  -- Copyright (c) 2003 by knut st. osmundsen
+ */
 
 #include "libc-alias.h"
@@ -10,58 +12,102 @@
 #include "syscalls.h"
 
-int __stat (const char *name, struct stat *buf)
+int __stat(const char *pszPath, struct stat *pStat)
 {
-  ULONG rc;
-  FILESTATUS3 info;
+    ULONG   rc;
+    union
+    {
+        FILESTATUS3     fsts3;
+        FILESTATUS3L    fsts3L;
+    } info;
+#if OFF_MAX > LONG_MAX
+    int     fLarge = 0;
+#endif
 
-  memset (buf, 0, sizeof (*buf));
-  if ((name[0] == '/' || name[0] == '\\') && strlen (name) >= 6 &&
-      memicmp (name+1, "pipe", 4) == 0 && (name[5] == '/' || name[5] == '\\'))
+    /*
+     * Validate input, refusing named pipes.
+     */
+    memset(pStat, 0, sizeof(*pStat));
+    if (    (pszPath[0] == '/' || pszPath[0] == '\\')
+        &&  !strnicmp(pszPath+1, "pipe", 4)
+        &&  (pszPath[5] == '/' || pszPath[5] == '\\'))
     {
-      errno = ENOENT;
-      return -1;
+        errno = ENOENT;
+        return -1;
     }
-  rc = DosQueryPathInfo (name, FIL_STANDARD, &info, sizeof (info));
-  if (rc != 0)
+
+    /*
+     * Get path info.
+     */
+#if OFF_MAX > LONG_MAX
+    if (__pfnDosOpenL)
     {
-      _sys_set_errno (rc);
-      return -1;
+        rc = DosQueryPathInfo(pszPath, FIL_STANDARDL, &info, sizeof(info.fsts3L));
+        fLarge = 1;
     }
-  buf->st_attr = info.attrFile;
-  buf->st_reserved = 0;
-  buf->st_mtime = _sys_p2t (info.ftimeLastWrite, info.fdateLastWrite);
-  if (FTIMEZEROP (info.ftimeCreation) &&
-      FDATEZEROP (info.fdateCreation))
-    buf->st_ctime = buf->st_mtime;
-  else
-    buf->st_ctime = _sys_p2t (info.ftimeCreation, info.fdateCreation);
-  if (FTIMEZEROP (info.ftimeLastAccess) &&
-      FDATEZEROP (info.fdateLastAccess))
-    buf->st_atime = buf->st_mtime;
-  else
-    buf->st_atime = _sys_p2t (info.ftimeLastAccess, info.fdateLastAccess);
-  if (info.attrFile & 0x10)     /* directory */
+    else
+#endif
+        rc = DosQueryPathInfo(pszPath, FIL_STANDARD, &info, sizeof(info.fsts3));
+    if (rc)
     {
-      buf->st_mode = S_IFDIR;
-      buf->st_mode |= ((S_IREAD|S_IWRITE|S_IEXEC) >> 6) * 0111;
-      buf->st_size = 0;
+        _sys_set_errno(rc);
+        return -1;
     }
-  else
+
+    /*
+     * Format stats struct.
+     *      We know the info struct layouts!
+     *      Only cbFile, cbFileAlloc and attrFile need be accessed
+     *      using the specific structure.
+     */
+    /* Times: FAT might not return create and access time. */
+    pStat->st_mtime = _sys_p2t(info.fsts3.ftimeLastWrite, info.fsts3.fdateLastWrite);
+    if (   FTIMEZEROP(info.fsts3.ftimeCreation)
+        && FDATEZEROP(info.fsts3.fdateCreation))
+        pStat->st_ctime = pStat->st_mtime;
+    else
+        pStat->st_ctime = _sys_p2t(info.fsts3.ftimeCreation, info.fsts3.fdateCreation);
+    if (   FTIMEZEROP(info.fsts3.ftimeLastAccess)
+        && FDATEZEROP(info.fsts3.fdateLastAccess))
+        pStat->st_atime = pStat->st_mtime;
+    else
+        pStat->st_atime = _sys_p2t(info.fsts3.ftimeLastAccess, info.fsts3.fdateLastAccess);
+
+#if OFF_MAX > LONG_MAX
+    rc = fLarge ? info.fsts3L.attrFile : info.fsts3.attrFile;
+#else
+    rc = info.fsts3.attrFile;
+#endif
+    pStat->st_attr = rc;
+    if (rc & FILE_DIRECTORY)
     {
-      buf->st_size = info.cbFile;
-      buf->st_mode = S_IFREG;
-      if (info.attrFile & 1)
-        buf->st_mode |= (S_IREAD >> 6) * 0111;
-      else
-        buf->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
+        /* directory */
+        pStat->st_mode = S_IFDIR;
+        pStat->st_mode |= ((S_IREAD|S_IWRITE|S_IEXEC) >> 6) * 0111;
+        pStat->st_size = 0;
     }
-  buf->st_uid = 0;
-  buf->st_gid = 0;
-  buf->st_ino = _sys_ino++;
-  if (_sys_ino == 0)
-    _sys_ino = 1;
-  buf->st_dev = 0;
-  buf->st_rdev = 0;
-  buf->st_nlink = 1;
-  return 0;
+    else
+    {
+#if OFF_MAX > LONG_MAX
+        if (fLarge)
+            pStat->st_size = info.fsf3L.cbFile;
+        else
+#endif
+            pStat->st_size = info.fsts3.cbFile;
+        pStat->st_mode = S_IFREG;
+        if (rc & FILE_READONLY)
+            pStat->st_mode |= (S_IREAD >> 6) * 0111;
+        else
+            pStat->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
+    }
+    /* default fake stuff */
+    pStat->st_uid = 0;
+    pStat->st_gid = 0;
+    pStat->st_ino = _sys_ino++;
+    if (_sys_ino == 0)
+        _sys_ino = 1;
+    pStat->st_dev = 0;
+    pStat->st_rdev = 0;
+    pStat->st_nlink = 1;
+    pStat->st_reserved = 0;             /* what is this good for?!? */
+    return 0;
 }
Index: trunk/src/emx/src/lib/sys/filefind.c
===================================================================
--- trunk/src/emx/src/lib/sys/filefind.c	(revision 729)
+++ trunk/src/emx/src/lib/sys/filefind.c	(revision 732)
@@ -1,22 +1,28 @@
-/* sys/filefind.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/filefind.c (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   sys/filefind.c (libc)    -- Copyright (c) 2003 by knut st. osmundsen
+*/
 
 #include "libc-alias.h"
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
+#define INCL_BASE
 #include <os2emx.h>
 #include <emx/syscalls.h>
 #include "syscalls.h"
 
-/* Close the directory handle. */
-
-static void find_close (struct find_data *fd)
-{
-  if (fd->find_handle != HDIR_CREATE)
-    {
-      DosFindClose (fd->find_handle);
-      fd->find_handle = HDIR_CREATE;
-    }
-  fd->find_count = 0;
-  fd->find_next = NULL;
+/**
+ * Close a directory find session.
+ * @param   pFD     The directory find data.
+ */
+static void find_close(struct find_data *pFD)
+{
+    if (pFD->hdir != HDIR_CREATE)
+    {
+        DosFindClose(pFD->hdir);
+        pFD->hdir = HDIR_CREATE;
+    }
+    pFD->cFiles = 0;
+    pFD->pchNext = NULL;
 }
 
@@ -24,100 +30,171 @@
    move to the next one. */
 
-static int find_conv (struct find_data *fd, struct _find *fp)
-{
-  const FILEFINDBUF3 *src;
-
-  /* Check if there are any entries.  Close the handle and return
-     ENOENT if there are no entries.  (Checking SRC is redundant.) */
-
-  src = fd->find_next;
-  if (fd->find_count < 1 || src == NULL)
-    {
-      find_close (fd);
-      errno = ENOENT;
-      return -1;
-    }
-
-  /* Fill-in target object. */
-
-  fp->attr = (unsigned char)src->attrFile;
-  fp->time = XUSHORT (src->ftimeLastWrite);
-  fp->date = XUSHORT (src->fdateLastWrite);
-  fp->size_lo = (unsigned short)(src->cbFile & 0xffff);
-  fp->size_hi = (unsigned short)(src->cbFile >> 16);
-  strcpy (fp->name, src->achName);
-
-  /* Move to the next entry. */
-
-  if (src->oNextEntryOffset == 0)
-    {
-      fd->find_next = NULL;
-      fd->find_count = 0;
-    }
-  else
-    {
-      fd->find_next = (FILEFINDBUF3 *)((char *)src + src->oNextEntryOffset);
-      fd->find_count -= 1;
-    }
-
-  return 0;
-}
-
-
-int __findfirst (const char *name, int attr, struct _find *fp)
-{
-  ULONG rc;
-  struct find_data *fd;
-
-  fd = &SYS_THREAD->fd;
-
-  if (fd->find_handle != HDIR_CREATE)
-    {
-      /* Closing the handle is not strictly required as DosFindFirst
-         can reuse an open handle.  However, this simplifies error
-         handling below (will DosFindFirst close the handle on error
-         if it is open?). */
-
-      DosFindClose (fd->find_handle);
-      fd->find_handle = HDIR_CREATE;
-    }
-
-  fd->find_count = FIND_COUNT;
-  rc = DosFindFirst (name, &fd->find_handle, attr & 0x37,
-                     &fd->find_buf[0], sizeof (fd->find_buf),
-                     &fd->find_count, FIL_STANDARD);
-  if (rc != 0)
-    {
-      fd->find_handle = HDIR_CREATE; /* Perhaps modified by DosFindFirst */
-      fd->find_count = 0;
-      fd->find_next = NULL;
-      _sys_set_errno (rc);
-      return -1;
-    }
-  fd->find_next = &fd->find_buf[0];
-  return find_conv (fd, fp);
-}
-
-
-int __findnext (struct _find *fp)
-{
-  ULONG rc;
-  struct find_data *fd;
-
-  fd = &SYS_THREAD->fd;
-  if (fd->find_count < 1)
-    {
-      fd->find_count = FIND_COUNT;
-      rc = DosFindNext (fd->find_handle, &fd->find_buf[0],
-                        sizeof (fd->find_buf), &fd->find_count);
-      if (rc != 0)
+/**
+ * Build a 'struct _find' from the next file in the directory find data.
+ * @returns 0 on success.
+ * @returns -1 on failure, ENOENT as errno.
+ * @param   pFD     The directory find data.
+ */
+static int find_conv(struct find_data *pFD, struct _find *fp)
+{
+    union
+    {
+        const char             *pch;
+        const FILEFINDBUF3     *pFindbuf3;
+        const FILEFINDBUF3L    *pFindbuf3L;
+    } u;
+    u.pch = pFD->pchNext;
+
+    /*
+     * If there ain't any entries, flag ENOENT error, clean up and
+     * return failure.
+     */
+    if (!pFD->cFiles)
+    {
+        find_close(pFD);
+        errno = ENOENT;
+        return -1;
+    }
+
+    /*
+     * Fill-in target object.
+     *  About the find structure we know that these fields are not the same
+     *  in the two versions:
+     *      cbFile, cbFileAlloc, attrFile, cchName, achName.
+     */
+    fp->time = XUSHORT(u.pFindbuf3->ftimeLastWrite);
+    fp->date = XUSHORT(u.pFindbuf3->fdateLastWrite);
+#if OFF_MAX > LONG_MAX
+    if (pFD->fType == FIL_STANDARDL)
+    {
+        fp->cbFile = u.pFindbuf3L->cbFile;
+        fp->attr   = (unsigned char)u.pFindbuf3L->attrFile;
+        strcpy(fp->szName, &u.pFindbuf3L->achName[0]);
+    }
+    else
+#endif
+    {
+        fp->cbFile = u.pFindbuf3->cbFile;
+        fp->attr   = (unsigned char)u.pFindbuf3->attrFile;
+        strcpy(fp->szName, &u.pFindbuf3->achName[0]);
+    }
+
+    /*
+     * Next entry.
+     */
+    if (pFD->cFiles && u.pFindbuf3->oNextEntryOffset)
+    {
+        pFD->pchNext = u.pch + u.pFindbuf3->oNextEntryOffset;
+        pFD->cFiles--;
+    }
+    else
+        pFD->cFiles = 0;
+
+    return 0;
+}
+
+
+/**
+ * Start a directory find session.
+ * It's only possible to perform one such session per thread. A call to this
+ * function will terminate the previous one.
+ *
+ * @returns 0 on success
+ * @returns -1 on failure and errno set appropriately.
+ * @param   pszName Search pattern. Usually "<somepath>\\*"
+ * @param   attr    File attributes to include in the search.
+ *                  If 0 files with any attributes may be included.
+ * @param   fp      Where to put the data on the first file found.
+ */
+int __findfirst(const char *pszName, int attr, struct _find *fp)
+{
+    ULONG               rc;
+    struct find_data   *pFD = &SYS_THREAD->fd;
+
+    /*
+     * Cleanup any open find sessions first.
+     */
+    if (pFD->hdir != HDIR_CREATE)
+    {
+        /* Closing the handle is not strictly required as DosFindFirst
+           can reuse an open handle.  However, this simplifies error
+           handling below (will DosFindFirst close the handle on error
+           if it is open?). */
+        DosFindClose(pFD->hdir);
+        pFD->hdir = HDIR_CREATE;
+    }
+
+    /*
+     * Start file enumeration.
+     */
+    pFD->cFiles = 30; /* !! Careful with this number! Wrong values can trigger stupid bugs. */
+#if OFF_MAX > LONG_MAX
+    pFD->fType = __pfnDosOpenL ? FIL_STANDARDL : FIL_STANDARD;
+#else
+    pFD->fType = FIL_STANDARD;
+#endif
+    rc = DosFindFirst(pszName,
+                      &pFD->hdir,
+                      attr & (FILE_NORMAL | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED),
+                      &pFD->achBuffer[0],
+                      sizeof(pFD->achBuffer),
+                      &pFD->cFiles,
+                      pFD->fType);
+    if (rc)
+    {
+        pFD->hdir = HDIR_CREATE; /* Perhaps modified by DosFindFirst */
+        pFD->cFiles = 0;
+        pFD->pchNext = NULL;
+        _sys_set_errno(rc);
+        return -1;
+    }
+    pFD->pchNext = &pFD->achBuffer[0];
+    return find_conv(pFD, fp);
+}
+
+
+/**
+ * Get the next file in the current directory find session.
+ * It's only possible to perform one such session per thread. A call to this
+ * function will terminate the previous one.
+ *
+ * @returns 0 on success
+ * @returns -1 on failure and errno set appropriately.
+ * @param   pszName Search pattern. Usually "<somepath>\\*"
+ * @param   attr    File attributes to include in the search.
+ *                  If 0 files with any attributes may be included.
+ * @param   fp      Where to put the data on the first file found.
+ */
+int __findnext(struct _find *fp)
+{
+    ULONG               rc;
+    struct find_data   *pFD = &SYS_THREAD->fd;
+
+    /*
+     * Check incoming.
+     */
+    if (pFD->hdir == HDIR_CREATE)
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    /*
+     * Do we need to fetch more files?
+     */
+    if (!pFD->cFiles)
+    {
+        pFD->cFiles = 30; /* !! Careful with this number! Wrong values can trigger stupid bugs. */
+        rc = DosFindNext(pFD->hdir, &pFD->achBuffer[0], sizeof(pFD->achBuffer), &pFD->cFiles);
+        if (rc)
         {
-          find_close (fd);
-          _sys_set_errno (rc);
-          return -1;
+            find_close(pFD);
+            _sys_set_errno(rc);
+            return -1;
         }
-      fd->find_next = &fd->find_buf[0];
-    }
-
-  return find_conv (fd, fp);
-}
+        pFD->pchNext = &pFD->achBuffer[0];
+    }
+
+    return find_conv(pFD, fp);
+}
+
Index: trunk/src/emx/src/lib/sys/largefileio.c
===================================================================
--- trunk/src/emx/src/lib/sys/largefileio.c	(revision 732)
+++ trunk/src/emx/src/lib/sys/largefileio.c	(revision 732)
@@ -0,0 +1,97 @@
+/* $Id$ */
+/** @file
+ *
+ * Large File Support.
+ *
+ * Copyright (c) 2003 Dimitry Froloff
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with This program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+#define ORD_DOS32OPENL              981
+#define ORD_DOS32SETFILEPTRL        988
+#define ORD_DOS32SETFILESIZEL       989
+#define ORD_DOS32SETFILELOCKSL      986
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#define INCL_BASE
+#include <os2.h>
+
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+ULONG (* _System __pfnDosOpenL)(PCSZ pszFileName, PHFILE phFile,
+    PULONG pulAction, LONGLONG llFileSize, ULONG ulAttribute, ULONG ulOpenFlags,
+    ULONG ulOpenMode, PEAOP2 pEABuf) = NULL;
+ULONG (* _System __pfnDosSetFilePtrL)(HFILE hFile, LONGLONG llOffset,
+    ULONG ulOrigin, PLONGLONG pllPos) = NULL;
+ULONG (* _System __pfnDosSetFileSizeL)(HFILE hFile, LONGLONG cbSize) = NULL;
+ULONG (* _System __pfnDosSetFileLocksL)(HFILE hFile,
+    __const__ FILELOCKL *pflUnlock, __const__ FILELOCKL *pflLock,
+    ULONG ulTimeout, ULONG flFlags) = NULL;
+
+
+/**
+ * Initiates the function pointers to the large file APIs.
+ *
+ * I/O functions should test if the function pointer is NULL to determin
+ * support for large files.
+ *
+ * @author  Dimitry Froloff
+ * @author  knut st. osmundsen <bird-srcspam@anduin.net>
+ */
+void __init_largefileio(void)
+{
+    HMODULE     hmod = NULLHANDLE;
+
+    /* only once! */
+    if (__pfnDosOpenL)
+        return;
+
+    /*
+     * Get a handle to DOSCALLS/DOSCALL1 which is suitable for DosQueryProcAddr.
+     */
+    if (DosLoadModule(NULL, 0, "DOSCALLS", &hmod))
+    {
+        /* this is impossible! */
+#ifdef DEBUG
+        asm ("int $3");
+#endif
+        return;
+    }
+
+    if (    DosQueryProcAddr(hmod, ORD_DOS32OPENL, NULL, (PFN*)&__pfnDosOpenL)
+        ||  DosQueryProcAddr(hmod, ORD_DOS32SETFILEPTRL, NULL, (PFN*)&__pfnDosSetFilePtrL)
+        ||  DosQueryProcAddr(hmod, ORD_DOS32SETFILESIZEL, NULL, (PFN*)&__pfnDosSetFileSizeL)
+        ||  DosQueryProcAddr(hmod, ORD_DOS32SETFILELOCKSL, NULL, (PFN*)&__pfnDosSetFileLocksL)
+          )
+    {
+        __pfnDosOpenL = NULL;
+        __pfnDosSetFilePtrL = NULL;
+        __pfnDosSetFileSizeL = NULL;
+        __pfnDosSetFileLocksL = NULL;
+    }
+    DosFreeModule(hmod);
+}
+
Index: trunk/src/emx/src/lib/sys/syscalls.h
===================================================================
--- trunk/src/emx/src/lib/sys/syscalls.h	(revision 729)
+++ trunk/src/emx/src/lib/sys/syscalls.h	(revision 732)
@@ -1,3 +1,6 @@
-/* sys/syscalls.h (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes */
+/* sys/syscalls.h (emx+gcc) -- Copyright (c) 1992-1996 by Eberhard Mattes
+   sys/filefind.c (libc)    -- Copyright (c) 2003 by Andrew Zabolotny
+   sys/filefind.c (libc)    -- Copyright (c) 2003 by Knut St. Osmundsen
+ */
 
 #include "libc-alias.h"
@@ -24,9 +27,4 @@
 
 #define XUSHORT(x) (*(USHORT *)&(x))
-
-/* Fetch this many FILEFINDBUF3 entries at once with DosFindFirst and
-   DosFindNext. */
-
-#define FIND_COUNT      4
 
 /* Maximum number of heap objects (16 = 512 / 32). */
@@ -81,13 +79,40 @@
 #if defined (_OS2EMX_H)
 
-#define FTIMEZEROP(x) (*(USHORT *)&(x) == 0)
-#define FDATEZEROP(x) (*(USHORT *)&(x) == 0)
+/** @group Large File Support - API pointers.
+ * Test if the function pointer is set before calling it.
+ * For APIs which take a structure level parameter, check __pfnDosOpenL.
+ * @{ */
+extern ULONG (* _System __pfnDosOpenL)(PCSZ pszFileName, PHFILE phFile,
+    PULONG pulAction, LONGLONG llFileSize, ULONG ulAttribute, ULONG ulOpenFlags,
+    ULONG ulOpenMode, PEAOP2 pEABuf);
+extern ULONG (* _System __pfnDosSetFilePtrL)(HFILE hFile, LONGLONG llOffset,
+    ULONG ulOrigin, PLONGLONG pllPos);
+extern ULONG (* _System __pfnDosSetFileSizeL)(HFILE hFile, LONGLONG cbSize);
+extern ULONG (* _System __pfnDosSetFileLocksL)(HFILE hFile,
+    __const__ FILELOCKL *pflUnlock, __const__ FILELOCKL *pflLock,
+    ULONG ulTimeout, ULONG flFlags);
+/** @} */
 
+
+/** Test if a DOS/OS2 file time is zero. */
+#define FTIMEZEROP(x) (*(PUSHORT)&(x) == 0)
+/** Test if a DOS/OS2 file date is zero. */
+#define FDATEZEROP(x) (*(PUSHORT)&(x) == 0)
+
+/** Directory find data entry.
+ * Used by __findfirst() and __findnext(). */
 struct find_data
 {
-  HDIR find_handle;
-  ULONG find_count;
-  FILEFINDBUF3 find_buf[FIND_COUNT];
-  const FILEFINDBUF3 *find_next;
+    /** Directory handle. HDIR_CREATE if no session opened. */
+    HDIR        hdir;
+    /** Type of buffer content. FIL_STANDARDL or FIL_STANDARD,
+     * i.e. FILEFINDBUF3 or FILEFINDBUF3L. */
+    ULONG       fType;
+    /** Number of files left in the buffer. */
+    ULONG       cFiles;
+    /** Pointer to the next entry. Don't test on this, test on cFiles! */
+    const char *pchNext;
+    /** Buffer. */
+    char        achBuffer[2048];
 };
 
@@ -152,2 +177,3 @@
 ULONG _sys_shrink_heap_by (ULONG decr, ULONG sbrk_model);
 ULONG _sys_shrink_heap_obj_by (ULONG decr);
+
Index: trunk/src/emx/version.smak
===================================================================
--- trunk/src/emx/version.smak	(revision 729)
+++ trunk/src/emx/version.smak	(revision 732)
@@ -4,5 +4,5 @@
 VH = 0
 # Middle part of version number
-VM = 3
+VM = 4
 # Low part of version number
 VL = 0
