6 #define WIN32_LEAN_AND_MEAN
7 #define WIN32IO_IS_STDIO
14 #include <sys/socket.h>
23 // The following is just a basic wrapping of the stdio
26 // redirected io subsystem for all XS modules
30 static int * dummy_errno()
35 static FILE *dummy_stderr()
40 static FILE *dummy_stdin()
45 static FILE *dummy_stdout()
50 static int dummy_globalmode(int mode)
59 #if defined(_WIN32) && !defined(WIN95_OSFHANDLE_FIXED) && defined(_M_IX86)
62 #define EXT_C_FUNC extern "C"
64 #define EXT_C_FUNC extern
67 EXT_C_FUNC int __cdecl _alloc_osfhnd(void);
68 EXT_C_FUNC int __cdecl _set_osfhnd(int fh, long value);
69 EXT_C_FUNC void __cdecl _lock_fhandle(int);
70 EXT_C_FUNC void __cdecl _unlock_fhandle(int);
71 EXT_C_FUNC void __cdecl _unlock(int);
73 #if (_MSC_VER >= 1000)
76 long osfhnd; /* underlying OS file HANDLE */
77 char osfile; /* attributes of file (e.g., open in text mode?) */
78 char pipech; /* one char buffer for handles opened on pipes */
79 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
81 CRITICAL_SECTION lock;
82 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
85 EXT_C_FUNC ioinfo * __pioinfo[];
88 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
89 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
90 #define _osfile(i) (_pioinfo(i)->osfile)
92 extern "C" extern char _osfile[];
93 #endif // (_MSC_VER >= 1000)
95 #define FOPEN 0x01 // file handle open
96 #define FAPPEND 0x20 // file handle opened O_APPEND
97 #define FDEV 0x40 // file handle refers to device
98 #define FTEXT 0x80 // file handle is in text mode
100 #define _STREAM_LOCKS 26 // Table of stream locks
101 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) // Last stream lock
102 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) // Table of fh locks
105 *int _patch_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
108 * This function allocates a free C Runtime file handle and associates
109 * it with the Win32 HANDLE specified by the first parameter. This is a
110 * temperary fix for WIN95's brain damage GetFileType() error on socket
111 * we just bypass that call for socket
114 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
115 * int flags - flags to associate with C Runtime file handle.
118 * returns index of entry in fh, if successful
119 * return -1, if no free entry is found
123 *******************************************************************************/
125 int my_open_osfhandle(long osfhandle, int flags)
128 char fileflags; // _osfile flags
130 // copy relevant flags from second parameter
133 if(flags & _O_APPEND)
134 fileflags |= FAPPEND;
139 // attempt to allocate a C Runtime file handle
140 if((fh = _alloc_osfhnd()) == -1)
142 errno = EMFILE; // too many open files
143 _doserrno = 0L; // not an OS error
144 return -1; // return error to caller
147 // the file is open. now, set the info in _osfhnd array
148 _set_osfhnd(fh, osfhandle);
150 fileflags |= FOPEN; // mark as open
152 #if (_MSC_VER >= 1000)
153 _osfile(fh) = fileflags; // set osfile entry
156 _osfile[fh] = fileflags; // set osfile entry
157 _unlock(fh+_FH_LOCKS); // unlock handle
161 return fh; // return handle
164 int __cdecl stolen_open_osfhandle(long osfhandle, int flags)
166 return _open_osfhandle(osfhandle, flags);
170 long my_get_osfhandle( int filehandle )
172 return _get_osfhandle(filehandle);
175 WIN32_IOSUBSYSTEM win32stdio = {
176 12345678L, // begin of structure;
177 dummy_errno, // (*pfunc_errno)(void);
178 dummy_stdin, // (*pfunc_stdin)(void);
179 dummy_stdout, // (*pfunc_stdout)(void);
180 dummy_stderr, // (*pfunc_stderr)(void);
181 ferror, // (*pfunc_ferror)(FILE *fp);
182 feof, // (*pfunc_feof)(FILE *fp);
183 strerror, // (*strerror)(int e);
184 vfprintf, // (*pfunc_vfprintf)(FILE *pf, const char *format, va_list arg);
185 vprintf, // (*pfunc_vprintf)(const char *format, va_list arg);
186 fread, // (*pfunc_fread)(void *buf, size_t size, size_t count, FILE *pf);
187 fwrite, // (*pfunc_fwrite)(void *buf, size_t size, size_t count, FILE *pf);
188 fopen, // (*pfunc_fopen)(const char *path, const char *mode);
189 fdopen, // (*pfunc_fdopen)(int fh, const char *mode);
190 freopen, // (*pfunc_freopen)(const char *path, const char *mode, FILE *pf);
191 fclose, // (*pfunc_fclose)(FILE *pf);
192 fputs, // (*pfunc_fputs)(const char *s,FILE *pf);
193 fputc, // (*pfunc_fputc)(int c,FILE *pf);
194 ungetc, // (*pfunc_ungetc)(int c,FILE *pf);
195 getc, // (*pfunc_getc)(FILE *pf);
196 fileno, // (*pfunc_fileno)(FILE *pf);
197 clearerr, // (*pfunc_clearerr)(FILE *pf);
198 fflush, // (*pfunc_fflush)(FILE *pf);
199 ftell, // (*pfunc_ftell)(FILE *pf);
200 fseek, // (*pfunc_fseek)(FILE *pf,long offset,int origin);
201 fgetpos, // (*pfunc_fgetpos)(FILE *pf,fpos_t *p);
202 fsetpos, // (*pfunc_fsetpos)(FILE *pf,fpos_t *p);
203 rewind, // (*pfunc_rewind)(FILE *pf);
204 tmpfile, // (*pfunc_tmpfile)(void);
205 abort, // (*pfunc_abort)(void);
206 fstat, // (*pfunc_fstat)(int fd,struct stat *bufptr);
207 stat, // (*pfunc_stat)(const char *name,struct stat *bufptr);
208 _pipe, // (*pfunc_pipe)( int *phandles, unsigned int psize, int textmode );
209 _popen, // (*pfunc_popen)( const char *command, const char *mode );
210 _pclose, // (*pfunc_pclose)( FILE *pf);
211 setmode, // (*pfunc_setmode)( int fd, int mode);
212 lseek, // (*pfunc_lseek)( int fd, long offset, int origin);
213 tell, // (*pfunc_tell)( int fd);
214 dup, // (*pfunc_dup)( int fd);
215 dup2, // (*pfunc_dup2)(int h1, int h2);
216 open, // (*pfunc_open)(const char *path, int oflag,...);
217 close, // (*pfunc_close)(int fd);
218 eof, // (*pfunc_eof)(int fd);
219 read, // (*pfunc_read)(int fd, void *buf, unsigned int cnt);
220 write, // (*pfunc_write)(int fd, const void *buf, unsigned int cnt);
221 dummy_globalmode,// (*pfunc_globalmode)(int mode)
225 87654321L, // end of structure