3 #define WIN32_LEAN_AND_MEAN
5 extern int my_fclose(FILE *pf);
7 #define WIN32IO_IS_STDIO
12 #include <sys/socket.h>
21 #define START_EXTERN_C extern "C" {
22 #define END_EXTERN_C }
24 #define START_EXTERN_C
31 * The following is just a basic wrapping of the stdio
33 * redirected io subsystem for all XS modules
48 /* the rest are the remapped stdio routines */
68 dummy_globalmode(int mode)
76 #if defined(_DLL) || defined(__BORLANDC__)
77 /* It may or may not be fixed (ok on NT), but DLL runtime
78 does not export the functions used in the workround
80 #define WIN95_OSFHANDLE_FIXED
83 #if defined(_WIN32) && !defined(WIN95_OSFHANDLE_FIXED) && defined(_M_IX86)
86 #define EXT_C_FUNC extern "C"
88 #define EXT_C_FUNC extern
91 EXT_C_FUNC int __cdecl _alloc_osfhnd(void);
92 EXT_C_FUNC int __cdecl _set_osfhnd(int fh, long value);
93 EXT_C_FUNC void __cdecl _lock_fhandle(int);
94 EXT_C_FUNC void __cdecl _unlock_fhandle(int);
95 EXT_C_FUNC void __cdecl _unlock(int);
97 #if (_MSC_VER >= 1000)
99 long osfhnd; /* underlying OS file HANDLE */
100 char osfile; /* attributes of file (e.g., open in text mode?) */
101 char pipech; /* one char buffer for handles opened on pipes */
102 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
104 CRITICAL_SECTION lock;
105 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
108 EXT_C_FUNC ioinfo * __pioinfo[];
111 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
112 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
113 #define _osfile(i) (_pioinfo(i)->osfile)
115 #else /* (_MSC_VER >= 1000) */
116 extern char _osfile[];
117 #endif /* (_MSC_VER >= 1000) */
119 #define FOPEN 0x01 /* file handle open */
120 #define FAPPEND 0x20 /* file handle opened O_APPEND */
121 #define FDEV 0x40 /* file handle refers to device */
122 #define FTEXT 0x80 /* file handle is in text mode */
124 #define _STREAM_LOCKS 26 /* Table of stream locks */
125 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
126 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
129 *int _patch_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
132 * This function allocates a free C Runtime file handle and associates
133 * it with the Win32 HANDLE specified by the first parameter. This is a
134 * temperary fix for WIN95's brain damage GetFileType() error on socket
135 * we just bypass that call for socket
138 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
139 * int flags - flags to associate with C Runtime file handle.
142 * returns index of entry in fh, if successful
143 * return -1, if no free entry is found
147 *******************************************************************************/
150 my_open_osfhandle(long osfhandle, int flags)
153 char fileflags; /* _osfile flags */
155 /* copy relevant flags from second parameter */
159 fileflags |= FAPPEND;
164 /* attempt to allocate a C Runtime file handle */
165 if((fh = _alloc_osfhnd()) == -1) {
166 errno = EMFILE; /* too many open files */
167 _doserrno = 0L; /* not an OS error */
168 return -1; /* return error to caller */
171 /* the file is open. now, set the info in _osfhnd array */
172 _set_osfhnd(fh, osfhandle);
174 fileflags |= FOPEN; /* mark as open */
176 #if (_MSC_VER >= 1000)
177 _osfile(fh) = fileflags; /* set osfile entry */
180 _osfile[fh] = fileflags; /* set osfile entry */
181 _unlock(fh+_FH_LOCKS); /* unlock handle */
184 return fh; /* return handle */
189 my_open_osfhandle(long osfhandle, int flags)
191 return _open_osfhandle(osfhandle, flags);
196 my_get_osfhandle( int filehandle )
198 return _get_osfhandle(filehandle);
205 /* simulate flock by locking a range on the file */
208 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
209 #define LK_LEN 0xffff0000
212 my_flock(int fd, int oper)
218 fh = (HANDLE)my_get_osfhandle(fd);
219 memset(&o, 0, sizeof(o));
222 case LOCK_SH: /* shared lock */
223 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
225 case LOCK_EX: /* exclusive lock */
226 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
228 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
229 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
231 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
232 LK_ERR(LockFileEx(fh,
233 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
234 0, LK_LEN, 0, &o),i);
236 case LOCK_UN: /* unlock lock */
237 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
239 default: /* unknown */
251 __declspec(dllexport)
253 WIN32_IOSUBSYSTEM win32stdio = {
254 12345678L, /* begin of structure; */
255 dummy_errno, /* (*pfunc_errno)(void); */
256 dummy_environ, /* (*pfunc_environ)(void); */
257 dummy_stdin, /* (*pfunc_stdin)(void); */
258 dummy_stdout, /* (*pfunc_stdout)(void); */
259 dummy_stderr, /* (*pfunc_stderr)(void); */
260 ferror, /* (*pfunc_ferror)(FILE *fp); */
261 feof, /* (*pfunc_feof)(FILE *fp); */
262 strerror, /* (*strerror)(int e); */
263 vfprintf, /* (*pfunc_vfprintf)(FILE *pf, const char *format, va_list arg); */
264 vprintf, /* (*pfunc_vprintf)(const char *format, va_list arg); */
265 fread, /* (*pfunc_fread)(void *buf, size_t size, size_t count, FILE *pf); */
266 fwrite, /* (*pfunc_fwrite)(void *buf, size_t size, size_t count, FILE *pf); */
267 fopen, /* (*pfunc_fopen)(const char *path, const char *mode); */
268 fdopen, /* (*pfunc_fdopen)(int fh, const char *mode); */
269 freopen, /* (*pfunc_freopen)(const char *path, const char *mode, FILE *pf); */
270 my_fclose, /* (*pfunc_fclose)(FILE *pf); */
271 fputs, /* (*pfunc_fputs)(const char *s,FILE *pf); */
272 fputc, /* (*pfunc_fputc)(int c,FILE *pf); */
273 ungetc, /* (*pfunc_ungetc)(int c,FILE *pf); */
274 getc, /* (*pfunc_getc)(FILE *pf); */
275 fileno, /* (*pfunc_fileno)(FILE *pf); */
276 clearerr, /* (*pfunc_clearerr)(FILE *pf); */
277 fflush, /* (*pfunc_fflush)(FILE *pf); */
278 ftell, /* (*pfunc_ftell)(FILE *pf); */
279 fseek, /* (*pfunc_fseek)(FILE *pf,long offset,int origin); */
280 fgetpos, /* (*pfunc_fgetpos)(FILE *pf,fpos_t *p); */
281 fsetpos, /* (*pfunc_fsetpos)(FILE *pf,fpos_t *p); */
282 rewind, /* (*pfunc_rewind)(FILE *pf); */
283 tmpfile, /* (*pfunc_tmpfile)(void); */
284 abort, /* (*pfunc_abort)(void); */
285 fstat, /* (*pfunc_fstat)(int fd,struct stat *bufptr); */
286 stat, /* (*pfunc_stat)(const char *name,struct stat *bufptr); */
287 _pipe, /* (*pfunc_pipe)( int *phandles, unsigned int psize, int textmode ); */
288 _popen, /* (*pfunc_popen)( const char *command, const char *mode ); */
289 _pclose, /* (*pfunc_pclose)( FILE *pf); */
290 setmode, /* (*pfunc_setmode)( int fd, int mode); */
291 lseek, /* (*pfunc_lseek)( int fd, long offset, int origin); */
292 tell, /* (*pfunc_tell)( int fd); */
293 dup, /* (*pfunc_dup)( int fd); */
294 dup2, /* (*pfunc_dup2)(int h1, int h2); */
295 open, /* (*pfunc_open)(const char *path, int oflag,...); */
296 close, /* (*pfunc_close)(int fd); */
297 eof, /* (*pfunc_eof)(int fd); */
298 read, /* (*pfunc_read)(int fd, void *buf, unsigned int cnt); */
299 write, /* (*pfunc_write)(int fd, const void *buf, unsigned int cnt); */
300 dummy_globalmode, /* (*pfunc_globalmode)(int mode) */
307 my_flock, /* (*pfunc_flock)(int fd, int oper) */
327 87654321L, /* end of structure */