Hi all,
While porting gdbserver I stumbled on yet again having to provide write
and read functions
as wrappers around ReadFile and WriteFile.
This is about the fourth time I do in a short period, so, I've put those
functions
in libmingwex.a (*), to make this the last time. While I was at it, I
added also fdopen,
implemented in terms of the wide version found in coredll.dll, and also
added open and lseek.
Keep in mind that file descriptors in WinCE are really disguised
HANDLEs, so doing a:
write (1, "msg", 3);
Won't work.
You need to do:
write ((int)fileno(stdout), "msg", 3);
(*) -lmingwex is added by default by the compiler driver, you don't need
to do anything.
Cheers,
Pedro Alves
P.S.: I missed some things in the first patch, which I fixed on the second.
(patch 1)
src/mingw/ChangeLog.mingw32ce:
2006-11-11 Pedro Alves <[EMAIL PROTECTED]>
* mingwex/Makefile.in (WINCE_DISTFILES): Add fdopen.c, read.c, write.c,
open.c and lseek.c.
(WINCE_OBJS): Add fdopen.o, read.o, write.o, open.o and lseek.o.
* mingwex/wince/open.c: New file.
* mingwex/wince/lseek.c: New file.
* mingwex/wince/fdopen.c: New file.
* mingwex/wince/read.c: New file.
* mingwex/wince/write.c: New file.
(patch 2)
src/mingw/ChangeLog.mingw32ce:
2006-11-11 Pedro Alves <[EMAIL PROTECTED]>
* mingwex/wince/open.c (open): Rename to _open.
* mingwex/wince/lseek.c (lseek): Rename to _lseek.
* mingwex/wince/fdopen.c (fdopen): Rename to _fdopen.
* mingwex/wince/read.c (read): Rename to _read.
* mingwex/wince/write.c (write): Rename to _write.
* include/fcntl.h: Enable file.
* include/io.h (_open, open, lseek, _lseek,
read, _read, write, _write): Unhide.
* include/stdio.h (fdopen, _fdopen): Unhide.
* include/unistd.h: Enable file.
* moldname.def.in (open, lseek, read, write): Unhide.
Index: Makefile.in
===================================================================
--- Makefile.in (revision 750)
+++ Makefile.in (working copy)
@@ -85,7 +85,7 @@ COMPLEX_DISTFILES = \
WINCE_DISTFILES = \
asctime.c freopen.c gmtime.c localtime.c mktime.c strftime.c time.c \
- tempnam.c unlink.c wcsftime.c
+ tempnam.c unlink.c wcsftime.c fdopen.c read.c write.c open.c lseek.c
CC = @CC@
# FIXME: Which is it, CC or CC_FOR_TARGET?
@@ -185,7 +185,7 @@ COMPLEX_OBJS = \
ctan.o ctanf.o ctanl.o ctanh.o ctanhf.o ctanhl.o
WINCE_OBJS = \
asctime.o freopen.o gmtime.o localtime.o mktime.o strftime.o time.o \
- tempnam.o unlink.o wcsftime.o
+ tempnam.o unlink.o wcsftime.o fdopen.o read.o write.o open.o lseek.o
ifneq (,$(findstring wince,$(target_alias)))
LIB_OBJS = $(WINCE_OBJS)
Index: wince/open.c
===================================================================
--- wince/open.c (revision 0)
+++ wince/open.c (revision 0)
@@ -0,0 +1,74 @@
+#include <windows.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int
+open (const char *path, int oflag, ...)
+{
+ wchar_t wpath[MAX_PATH];
+ DWORD fileaccess;
+ DWORD fileshare;
+ DWORD filecreate;
+ DWORD fileattrib;
+ HANDLE hnd;
+
+ size_t path_len = strlen (path);
+ if (path_len >= MAX_PATH)
+ return -1;
+
+ switch (oflag & (O_RDONLY | O_WRONLY | O_RDWR))
+ {
+ case O_RDONLY:
+ fileaccess = GENERIC_READ;
+ break;
+ case O_WRONLY:
+ fileaccess = GENERIC_WRITE;
+ break;
+ case O_RDWR:
+ fileaccess = GENERIC_READ | GENERIC_WRITE;
+ break;
+ default:
+ return -1;
+ }
+
+ switch (oflag & (O_CREAT | O_EXCL | O_TRUNC))
+ {
+ case 0:
+ case O_EXCL: /* ignore EXCL w/o CREAT */
+ filecreate = OPEN_EXISTING;
+ break;
+ case O_CREAT:
+ filecreate = OPEN_ALWAYS;
+ break;
+ case O_CREAT | O_EXCL:
+ case O_CREAT | O_TRUNC | O_EXCL:
+ filecreate = CREATE_NEW;
+ break;
+
+ case O_TRUNC:
+ case O_TRUNC | O_EXCL: /* ignore EXCL w/o CREAT */
+ filecreate = TRUNCATE_EXISTING;
+ break;
+ case O_CREAT | O_TRUNC:
+ filecreate = CREATE_ALWAYS;
+ break;
+ default:
+ /* this can't happen ... all cases are covered */
+ return -1;
+ }
+
+ mbstowcs (wpath, path, path_len + 1);
+
+ fileshare = FILE_SHARE_READ | FILE_SHARE_WRITE;
+ fileattrib = FILE_ATTRIBUTE_NORMAL;
+
+ hnd = CreateFileW (wpath, fileaccess, fileshare, NULL, filecreate,
+ fileattrib, NULL);
+ if (hnd == INVALID_HANDLE_VALUE)
+ return -1;
+
+ if (oflag & O_APPEND)
+ SetFilePointer (hnd, 0, NULL, FILE_END);
+
+ return (int) hnd;
+}
Index: wince/lseek.c
===================================================================
--- wince/lseek.c (revision 0)
+++ wince/lseek.c (revision 0)
@@ -0,0 +1,21 @@
+#include <windows.h>
+#include <unistd.h>
+
+long
+lseek (int fildes, long offset, int whence)
+{
+ DWORD mode = 0;
+ switch (whence)
+ {
+ case SEEK_SET:
+ mode = FILE_BEGIN;
+ break;
+ case SEEK_CUR:
+ mode = FILE_CURRENT;
+ break;
+ case SEEK_END:
+ mode = FILE_END;
+ break;
+ }
+ return (long) SetFilePointer ((HANDLE) fildes, offset, NULL, mode);
+}
Property changes on: wince/lseek.c
___________________________________________________________________
Name: svn:eol-style
+ native
Index: wince/fdopen.c
===================================================================
--- wince/fdopen.c (revision 0)
+++ wince/fdopen.c (revision 0)
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include <string.h>
+
+#define MAX_MODE 64
+
+FILE *
+fdopen (int fildes, const char *mode)
+{
+ FILE *f;
+ wchar_t wmode[MAX_MODE];
+ size_t sizem = strlen (mode) + 1;
+ if (sizem > MAX_MODE)
+ return NULL;
+ mbstowcs (wmode, mode, sizem);
+ f = _wfdopen (fildes, wmode);
+ return f;
+}
Property changes on: wince/fdopen.c
___________________________________________________________________
Name: svn:eol-style
+ native
Index: wince/read.c
===================================================================
--- wince/read.c (revision 0)
+++ wince/read.c (revision 0)
@@ -0,0 +1,13 @@
+#include <windows.h>
+#include <unistd.h>
+
+int
+read (int fildes, void *buf, unsigned int bufsize)
+{
+ DWORD NumberOfBytesRead;
+ if (bufsize > 0x7fffffff)
+ bufsize = 0x7fffffff;
+ if (!ReadFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesRead, NULL))
+ return -1;
+ return (int) NumberOfBytesRead;
+}
Property changes on: wince/read.c
___________________________________________________________________
Name: svn:eol-style
+ native
Index: wince/write.c
===================================================================
--- wince/write.c (revision 0)
+++ wince/write.c (revision 0)
@@ -0,0 +1,13 @@
+#include <windows.h>
+#include <unistd.h>
+
+int
+write (int fildes, const void *buf, unsigned int bufsize)
+{
+ DWORD NumberOfBytesWritten;
+ if (bufsize > 0x7fffffff)
+ bufsize = 0x7fffffff;
+ if (!WriteFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesWritten, NULL))
+ return -1;
+ return (int) NumberOfBytesWritten;
+}
Property changes on: wince/write.c
___________________________________________________________________
Name: svn:eol-style
+ native
Index: mingwex/wince/open.c
===================================================================
--- mingwex/wince/open.c (revision 784)
+++ mingwex/wince/open.c (working copy)
@@ -3,7 +3,7 @@
#include <fcntl.h>
int
-open (const char *path, int oflag, ...)
+_open (const char *path, int oflag, ...)
{
wchar_t wpath[MAX_PATH];
DWORD fileaccess;
Index: mingwex/wince/lseek.c
===================================================================
--- mingwex/wince/lseek.c (revision 784)
+++ mingwex/wince/lseek.c (working copy)
@@ -2,7 +2,7 @@
#include <unistd.h>
long
-lseek (int fildes, long offset, int whence)
+_lseek (int fildes, long offset, int whence)
{
DWORD mode = 0;
switch (whence)
Index: mingwex/wince/fdopen.c
===================================================================
--- mingwex/wince/fdopen.c (revision 784)
+++ mingwex/wince/fdopen.c (working copy)
@@ -4,7 +4,7 @@
#define MAX_MODE 64
FILE *
-fdopen (int fildes, const char *mode)
+_fdopen (int fildes, const char *mode)
{
FILE *f;
wchar_t wmode[MAX_MODE];
Index: mingwex/wince/read.c
===================================================================
--- mingwex/wince/read.c (revision 784)
+++ mingwex/wince/read.c (working copy)
@@ -2,7 +2,7 @@
#include <unistd.h>
int
-read (int fildes, void *buf, unsigned int bufsize)
+_read (int fildes, void *buf, unsigned int bufsize)
{
DWORD NumberOfBytesRead;
if (bufsize > 0x7fffffff)
Index: mingwex/wince/write.c
===================================================================
--- mingwex/wince/write.c (revision 784)
+++ mingwex/wince/write.c (working copy)
@@ -2,7 +2,7 @@
#include <unistd.h>
int
-write (int fildes, const void *buf, unsigned int bufsize)
+_write (int fildes, const void *buf, unsigned int bufsize)
{
DWORD NumberOfBytesWritten;
if (bufsize > 0x7fffffff)
Index: include/fcntl.h
===================================================================
--- include/fcntl.h (revision 750)
+++ include/fcntl.h (working copy)
@@ -8,10 +8,6 @@
* in sys/stat.h (ick).
*
*/
-#ifdef __COREDLL__
-# include_next <fcntl.h>
-#else /* __COREDLL__ */
-
#ifndef _FCNTL_H_
#define _FCNTL_H_
@@ -71,5 +67,3 @@
#endif /* Not _NO_OLDNAMES */
#endif /* Not _FCNTL_H_ */
-
-#endif /* Not __COREDLL__ */
Index: include/io.h
===================================================================
--- include/io.h (revision 750)
+++ include/io.h (working copy)
@@ -200,10 +200,11 @@
#endif
#ifndef __COREDLL__
-
_CRTIMP int __cdecl _access (const char*, int);
_CRTIMP int __cdecl _chsize (int, long);
+#endif /* __COREDLL__ */
_CRTIMP int __cdecl _close (int);
+#ifndef __COREDLL__
_CRTIMP int __cdecl _commit(int);
/* NOTE: The only significant bit in unPermissions appears to be bit 7 (0x80),
@@ -227,15 +228,19 @@
/* LK_... locking commands defined in sys/locking.h. */
_CRTIMP int __cdecl _locking (int, int, long);
+#endif /* __COREDLL__ */
_CRTIMP long __cdecl _lseek (int, long, int);
/* Optional third argument is unsigned unPermissions. */
_CRTIMP int __cdecl _open (const char*, int, ...);
+#ifndef __COREDLL__
_CRTIMP int __cdecl _open_osfhandle (long, int);
_CRTIMP int __cdecl _pipe (int *, unsigned int, int);
+#endif /* __COREDLL__ */
_CRTIMP int __cdecl _read (int, void*, unsigned int);
+#ifndef __COREDLL__
_CRTIMP int __cdecl _setmode (int, int);
/* SH_... flags for nShFlags defined in share.h
@@ -247,9 +252,7 @@
_CRTIMP int __cdecl _umask (int);
#endif /* __COREDLL__ */
_CRTIMP int __cdecl _unlink (const char*);
-#ifndef __COREDLL__
_CRTIMP int __cdecl _write (int, const void*, unsigned int);
-#endif /* __COREDLL__ */
/* Wide character versions. Also declared in wchar.h. */
/* Not in crtdll.dll */
@@ -284,25 +287,27 @@
#ifndef __COREDLL__
_CRTIMP int __cdecl access (const char*, int);
_CRTIMP int __cdecl chsize (int, long );
+#endif /* __COREDLL__ */
_CRTIMP int __cdecl close (int);
+#ifndef __COREDLL__
_CRTIMP int __cdecl creat (const char*, int);
_CRTIMP int __cdecl dup (int);
_CRTIMP int __cdecl dup2 (int, int);
_CRTIMP int __cdecl eof (int);
_CRTIMP long __cdecl filelength (int);
_CRTIMP int __cdecl isatty (int);
+#endif /* __COREDLL__ */
_CRTIMP long __cdecl lseek (int, long, int);
_CRTIMP int __cdecl open (const char*, int, ...);
_CRTIMP int __cdecl read (int, void*, unsigned int);
+#ifndef __COREDLL__
_CRTIMP int __cdecl setmode (int, int);
_CRTIMP int __cdecl sopen (const char*, int, int, ...);
_CRTIMP long __cdecl tell (int);
_CRTIMP int __cdecl umask (int);
#endif /* __COREDLL__ */
_CRTIMP int __cdecl unlink (const char*);
-#ifndef __COREDLL__
_CRTIMP int __cdecl write (int, const void*, unsigned int);
-#endif /* __COREDLL__ */
#endif /* _UWIN */
/* Wide character versions. Also declared in wchar.h. */
Index: include/stdio.h
===================================================================
--- include/stdio.h (revision 750)
+++ include/stdio.h (working copy)
@@ -419,7 +419,9 @@
#ifndef __COREDLL__
_CRTIMP int __cdecl _fgetchar (void);
_CRTIMP int __cdecl _fputchar (int);
+#endif
_CRTIMP FILE* __cdecl _fdopen (int, const char*);
+#ifndef __COREDLL__
_CRTIMP FILE* __cdecl _fsopen(const char*, const char*, int);
_CRTIMP int __cdecl _fileno (FILE*);
#endif
@@ -431,10 +433,10 @@
#endif
#ifndef _NO_OLDNAMES
+_CRTIMP FILE* __cdecl fdopen (int, const char*);
#ifndef __COREDLL__
_CRTIMP int __cdecl fgetchar (void);
_CRTIMP int __cdecl fputchar (int);
-_CRTIMP FILE* __cdecl fdopen (int, const char*);
_CRTIMP int __cdecl fileno (FILE*);
#else
_CRTIMP void* __cdecl fileno (FILE*);
Index: include/unistd.h
===================================================================
--- include/unistd.h (revision 750)
+++ include/unistd.h (working copy)
@@ -4,11 +4,6 @@
* unistd.h maps (roughly) to io.h
*/
-#ifdef __COREDLL__
-/* No such file on Windows CE. */
-# include_next <unistd.h>
-#else /* __COREDLL__ */
-
#ifndef _UNISTD_H
#define _UNISTD_H
@@ -36,5 +31,3 @@
#endif
#endif /* _UNISTD_H */
-
-#endif /* __COREDLL__ */
Index: moldname.def.in
===================================================================
--- moldname.def.in (revision 750)
+++ moldname.def.in (working copy)
@@ -29,7 +29,9 @@
chdir
chmod
chsize
+#endif /* __COREDLL__ */
close
+#ifndef __COREDLL__
creat
cwait
#endif /* __COREDLL__ */
@@ -83,21 +85,25 @@
kbhit
lfind
lsearch
+#endif /* __COREDLL__ */
lseek
-#endif /* __COREDLL__ */
ltoa
memccpy
memicmp
#ifndef __COREDLL__
mkdir
mktemp
+#endif /* __COREDLL__ */
open
+#ifndef __COREDLL__
pclose
popen
putch
putenv
putw
+#endif /* __COREDLL__ */
read
+#ifndef __COREDLL__
rmdir
rmtmp
searchenv
@@ -160,9 +166,7 @@
#if (__MSVCRT__)
wpopen
#endif
-#ifndef __COREDLL__
write
-#endif /* __COREDLL__ */
; non-ANSI functions declared in math.h
j0
j1
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Cegcc-devel mailing list
Cegcc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cegcc-devel