6 #define WIN32_LEAN_AND_MEAN
7 #define WIN32IO_IS_STDIO
14 #include <sys/socket.h>
21 struct servent* win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
24 d->s_name = s->s_name;
25 d->s_aliases = s->s_aliases;
26 d->s_port = s->s_port;
27 if (strlen(s->s_proto)) d->s_proto = s->s_proto;
28 else if (proto && strlen(proto)) d->s_proto = (char *)proto;
29 else d->s_proto = "tcp";
32 if (proto && strlen(proto)) d->s_proto = (char *)proto;
33 else d->s_proto = "tcp";
40 // The following is just a basic wrapping of the stdio
46 // redirected io subsystem for all XS modules
50 static int * dummy_errno()
55 // the rest are the remapped stdio routines
56 static FILE *dummy_stderr()
61 static FILE *dummy_stdin()
66 static FILE *dummy_stdout()
71 static int dummy_globalmode(int mode)
80 #if defined(_WIN32) && !defined(WIN95_OSFHANDLE_FIXED) && defined(_M_IX86)
83 #define EXT_C_FUNC extern "C"
85 #define EXT_C_FUNC extern
88 EXT_C_FUNC int __cdecl _alloc_osfhnd(void);
89 EXT_C_FUNC int __cdecl _set_osfhnd(int fh, long value);
90 EXT_C_FUNC void __cdecl _lock_fhandle(int);
91 EXT_C_FUNC void __cdecl _unlock_fhandle(int);
92 EXT_C_FUNC void __cdecl _unlock(int);
94 #if (_MSC_VER >= 1000)
97 long osfhnd; /* underlying OS file HANDLE */
98 char osfile; /* attributes of file (e.g., open in text mode?) */
99 char pipech; /* one char buffer for handles opened on pipes */
100 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
102 CRITICAL_SECTION lock;
103 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
106 EXT_C_FUNC ioinfo * __pioinfo[];
109 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
110 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
111 #define _osfile(i) (_pioinfo(i)->osfile)
113 extern "C" extern char _osfile[];
114 #endif // (_MSC_VER >= 1000)
116 #define FOPEN 0x01 // file handle open
117 #define FAPPEND 0x20 // file handle opened O_APPEND
118 #define FDEV 0x40 // file handle refers to device
119 #define FTEXT 0x80 // file handle is in text mode
121 #define _STREAM_LOCKS 26 // Table of stream locks
122 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) // Last stream lock
123 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) // Table of fh locks
126 *int _patch_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
129 * This function allocates a free C Runtime file handle and associates
130 * it with the Win32 HANDLE specified by the first parameter. This is a
131 * temperary fix for WIN95's brain damage GetFileType() error on socket
132 * we just bypass that call for socket
135 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
136 * int flags - flags to associate with C Runtime file handle.
139 * returns index of entry in fh, if successful
140 * return -1, if no free entry is found
144 *******************************************************************************/
146 int my_open_osfhandle(long osfhandle, int flags)
149 char fileflags; // _osfile flags
151 // copy relevant flags from second parameter
154 if(flags & _O_APPEND)
155 fileflags |= FAPPEND;
160 // attempt to allocate a C Runtime file handle
161 if((fh = _alloc_osfhnd()) == -1)
163 errno = EMFILE; // too many open files
164 _doserrno = 0L; // not an OS error
165 return -1; // return error to caller
168 // the file is open. now, set the info in _osfhnd array
169 _set_osfhnd(fh, osfhandle);
171 fileflags |= FOPEN; // mark as open
173 #if (_MSC_VER >= 1000)
174 _osfile(fh) = fileflags; // set osfile entry
177 _osfile[fh] = fileflags; // set osfile entry
178 _unlock(fh+_FH_LOCKS); // unlock handle
182 return fh; // return handle
185 int __cdecl stolen_open_osfhandle(long osfhandle, int flags)
187 return _open_osfhandle(osfhandle, flags);
191 long my_get_osfhandle( int filehandle )
193 return _get_osfhandle(filehandle);
196 WIN32_IOSUBSYSTEM win32stdio = {
197 12345678L, // begin of structure;
198 dummy_errno, // (*pfunc_errno)(void);
199 dummy_stdin, // (*pfunc_stdin)(void);
200 dummy_stdout, // (*pfunc_stdout)(void);
201 dummy_stderr, // (*pfunc_stderr)(void);
202 ferror, // (*pfunc_ferror)(FILE *fp);
203 feof, // (*pfunc_feof)(FILE *fp);
204 strerror, // (*strerror)(int e);
205 vfprintf, // (*pfunc_vfprintf)(FILE *pf, const char *format, va_list arg);
206 vprintf, // (*pfunc_vprintf)(const char *format, va_list arg);
207 fread, // (*pfunc_fread)(void *buf, size_t size, size_t count, FILE *pf);
208 fwrite, // (*pfunc_fwrite)(void *buf, size_t size, size_t count, FILE *pf);
209 fopen, // (*pfunc_fopen)(const char *path, const char *mode);
210 fdopen, // (*pfunc_fdopen)(int fh, const char *mode);
211 freopen, // (*pfunc_freopen)(const char *path, const char *mode, FILE *pf);
212 fclose, // (*pfunc_fclose)(FILE *pf);
213 fputs, // (*pfunc_fputs)(const char *s,FILE *pf);
214 fputc, // (*pfunc_fputc)(int c,FILE *pf);
215 ungetc, // (*pfunc_ungetc)(int c,FILE *pf);
216 getc, // (*pfunc_getc)(FILE *pf);
217 fileno, // (*pfunc_fileno)(FILE *pf);
218 clearerr, // (*pfunc_clearerr)(FILE *pf);
219 fflush, // (*pfunc_fflush)(FILE *pf);
220 ftell, // (*pfunc_ftell)(FILE *pf);
221 fseek, // (*pfunc_fseek)(FILE *pf,long offset,int origin);
222 fgetpos, // (*pfunc_fgetpos)(FILE *pf,fpos_t *p);
223 fsetpos, // (*pfunc_fsetpos)(FILE *pf,fpos_t *p);
224 rewind, // (*pfunc_rewind)(FILE *pf);
225 tmpfile, // (*pfunc_tmpfile)(void);
226 abort, // (*pfunc_abort)(void);
227 fstat, // (*pfunc_fstat)(int fd,struct stat *bufptr);
228 stat, // (*pfunc_stat)(const char *name,struct stat *bufptr);
229 _pipe, // (*pfunc_pipe)( int *phandles, unsigned int psize, int textmode );
230 _popen, // (*pfunc_popen)( const char *command, const char *mode );
231 _pclose, // (*pfunc_pclose)( FILE *pf);
232 setmode, // (*pfunc_setmode)( int fd, int mode);
233 lseek, // (*pfunc_lseek)( int fd, long offset, int origin);
234 tell, // (*pfunc_tell)( int fd);
235 dup, // (*pfunc_dup)( int fd);
236 dup2, // (*pfunc_dup2)(int h1, int h2);
237 open, // (*pfunc_open)(const char *path, int oflag,...);
238 close, // (*pfunc_close)(int fd);
239 eof, // (*pfunc_eof)(int fd);
240 read, // (*pfunc_read)(int fd, void *buf, unsigned int cnt);
241 write, // (*pfunc_write)(int fd, const void *buf, unsigned int cnt);
242 dummy_globalmode,// (*pfunc_globalmode)(int mode)
245 87654321L, // end of structure