1 #define WIN32_LEAN_AND_MEAN
3 extern int my_fclose(FILE *pf);
5 #define WIN32IO_IS_STDIO
10 #include <sys/socket.h>
20 * The following is just a basic wrapping of the stdio
22 * redirected io subsystem for all XS modules
37 /* the rest are the remapped stdio routines */
57 dummy_globalmode(int mode)
65 #if defined(_DLL) || !defined(_MSC_VER)
66 /* It may or may not be fixed (ok on NT), but DLL runtime
67 does not export the functions used in the workround
69 #define WIN95_OSFHANDLE_FIXED
72 #if defined(_WIN32) && !defined(WIN95_OSFHANDLE_FIXED) && defined(_M_IX86)
74 EXTERN_C int __cdecl _alloc_osfhnd(void);
75 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
76 EXTERN_C void __cdecl _lock_fhandle(int);
77 EXTERN_C void __cdecl _unlock_fhandle(int);
78 EXTERN_C void __cdecl _unlock(int);
80 #if (_MSC_VER >= 1000)
82 long osfhnd; /* underlying OS file HANDLE */
83 char osfile; /* attributes of file (e.g., open in text mode?) */
84 char pipech; /* one char buffer for handles opened on pipes */
85 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
87 CRITICAL_SECTION lock;
88 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
91 EXTERN_C ioinfo * __pioinfo[];
94 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
95 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
96 #define _osfile(i) (_pioinfo(i)->osfile)
98 #else /* (_MSC_VER >= 1000) */
99 extern char _osfile[];
100 #endif /* (_MSC_VER >= 1000) */
102 #define FOPEN 0x01 /* file handle open */
103 #define FAPPEND 0x20 /* file handle opened O_APPEND */
104 #define FDEV 0x40 /* file handle refers to device */
105 #define FTEXT 0x80 /* file handle is in text mode */
107 #define _STREAM_LOCKS 26 /* Table of stream locks */
108 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
109 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
112 *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
115 * This function allocates a free C Runtime file handle and associates
116 * it with the Win32 HANDLE specified by the first parameter. This is a
117 * temperary fix for WIN95's brain damage GetFileType() error on socket
118 * we just bypass that call for socket
121 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
122 * int flags - flags to associate with C Runtime file handle.
125 * returns index of entry in fh, if successful
126 * return -1, if no free entry is found
130 *******************************************************************************/
133 my_open_osfhandle(long osfhandle, int flags)
136 char fileflags; /* _osfile flags */
138 /* copy relevant flags from second parameter */
142 fileflags |= FAPPEND;
147 /* attempt to allocate a C Runtime file handle */
148 if((fh = _alloc_osfhnd()) == -1) {
149 errno = EMFILE; /* too many open files */
150 _doserrno = 0L; /* not an OS error */
151 return -1; /* return error to caller */
154 /* the file is open. now, set the info in _osfhnd array */
155 _set_osfhnd(fh, osfhandle);
157 fileflags |= FOPEN; /* mark as open */
159 #if (_MSC_VER >= 1000)
160 _osfile(fh) = fileflags; /* set osfile entry */
163 _osfile[fh] = fileflags; /* set osfile entry */
164 _unlock(fh+_FH_LOCKS); /* unlock handle */
167 return fh; /* return handle */
170 #define _open_osfhandle my_open_osfhandle
173 /* simulate flock by locking a range on the file */
175 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
176 #define LK_LEN 0xffff0000
179 my_flock(int fd, int oper)
185 fh = (HANDLE)_get_osfhandle(fd);
186 memset(&o, 0, sizeof(o));
189 case LOCK_SH: /* shared lock */
190 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
192 case LOCK_EX: /* exclusive lock */
193 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
195 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
196 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
198 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
199 LK_ERR(LockFileEx(fh,
200 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
201 0, LK_LEN, 0, &o),i);
203 case LOCK_UN: /* unlock lock */
204 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
206 default: /* unknown */
218 __declspec(dllexport)
220 WIN32_IOSUBSYSTEM win32stdio = {
221 12345678L, /* begin of structure; */
222 dummy_errno, /* (*pfunc_errno)(void); */
223 dummy_environ, /* (*pfunc_environ)(void); */
224 dummy_stdin, /* (*pfunc_stdin)(void); */
225 dummy_stdout, /* (*pfunc_stdout)(void); */
226 dummy_stderr, /* (*pfunc_stderr)(void); */
227 ferror, /* (*pfunc_ferror)(FILE *fp); */
228 feof, /* (*pfunc_feof)(FILE *fp); */
229 strerror, /* (*strerror)(int e); */
230 vfprintf, /* (*pfunc_vfprintf)(FILE *pf, const char *format, va_list arg); */
231 vprintf, /* (*pfunc_vprintf)(const char *format, va_list arg); */
232 fread, /* (*pfunc_fread)(void *buf, size_t size, size_t count, FILE *pf); */
233 fwrite, /* (*pfunc_fwrite)(void *buf, size_t size, size_t count, FILE *pf); */
234 fopen, /* (*pfunc_fopen)(const char *path, const char *mode); */
235 fdopen, /* (*pfunc_fdopen)(int fh, const char *mode); */
236 freopen, /* (*pfunc_freopen)(const char *path, const char *mode, FILE *pf); */
237 my_fclose, /* (*pfunc_fclose)(FILE *pf); */
238 fputs, /* (*pfunc_fputs)(const char *s,FILE *pf); */
239 fputc, /* (*pfunc_fputc)(int c,FILE *pf); */
240 ungetc, /* (*pfunc_ungetc)(int c,FILE *pf); */
241 getc, /* (*pfunc_getc)(FILE *pf); */
242 fileno, /* (*pfunc_fileno)(FILE *pf); */
243 clearerr, /* (*pfunc_clearerr)(FILE *pf); */
244 fflush, /* (*pfunc_fflush)(FILE *pf); */
245 ftell, /* (*pfunc_ftell)(FILE *pf); */
246 fseek, /* (*pfunc_fseek)(FILE *pf,long offset,int origin); */
247 fgetpos, /* (*pfunc_fgetpos)(FILE *pf,fpos_t *p); */
248 fsetpos, /* (*pfunc_fsetpos)(FILE *pf,fpos_t *p); */
249 rewind, /* (*pfunc_rewind)(FILE *pf); */
250 tmpfile, /* (*pfunc_tmpfile)(void); */
251 abort, /* (*pfunc_abort)(void); */
252 fstat, /* (*pfunc_fstat)(int fd,struct stat *bufptr); */
253 stat, /* (*pfunc_stat)(const char *name,struct stat *bufptr); */
254 _pipe, /* (*pfunc_pipe)( int *phandles, unsigned int psize, int textmode ); */
255 _popen, /* (*pfunc_popen)( const char *command, const char *mode ); */
256 _pclose, /* (*pfunc_pclose)( FILE *pf); */
257 setmode, /* (*pfunc_setmode)( int fd, int mode); */
258 lseek, /* (*pfunc_lseek)( int fd, long offset, int origin); */
259 tell, /* (*pfunc_tell)( int fd); */
260 dup, /* (*pfunc_dup)( int fd); */
261 dup2, /* (*pfunc_dup2)(int h1, int h2); */
262 open, /* (*pfunc_open)(const char *path, int oflag,...); */
263 close, /* (*pfunc_close)(int fd); */
264 eof, /* (*pfunc_eof)(int fd); */
265 read, /* (*pfunc_read)(int fd, void *buf, unsigned int cnt); */
266 write, /* (*pfunc_write)(int fd, const void *buf, unsigned int cnt); */
267 dummy_globalmode, /* (*pfunc_globalmode)(int mode) */
274 my_flock, /* (*pfunc_flock)(int fd, int oper) */
294 87654321L, /* end of structure */