Clean up and document API for hashes
[p5sagit/p5-mst-13.2.git] / win32 / win32aux.cpp
1
2 #ifdef __cplusplus
3 extern "C" {
4 #endif
5
6 #define WIN32_LEAN_AND_MEAN
7 #define WIN32IO_IS_STDIO
8 #define EXT
9 #include <windows.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <io.h>
13 #include <sys/stat.h>
14 #include <sys/socket.h>
15 #include <fcntl.h>
16 #include <assert.h>
17 #include <errno.h>
18
19 #include "win32iop.h"
20
21 struct servent* win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
22 {
23         try {
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";
30                 }
31         catch (...) {
32                 if (proto && strlen(proto)) d->s_proto = (char *)proto;
33                 else d->s_proto = "tcp";
34                 }
35
36         return d;
37 }
38 #ifdef GARY
39 //
40 // The following is just a basic wrapping of the stdio
41 //
42 //
43 //
44 //
45 //
46 //  redirected io subsystem for all XS modules
47 //
48 //
49
50 static int * dummy_errno()
51 {
52         return (&(errno));
53 }
54
55 // the rest are the remapped stdio routines
56 static FILE *dummy_stderr()
57 {
58         return stderr;
59 }
60
61 static FILE *dummy_stdin()
62 {
63         return stdin;
64 }
65
66 static FILE *dummy_stdout()
67 {
68         return stdout;
69 }
70
71 static int dummy_globalmode(int mode)
72 {
73         int o = _fmode;
74         _fmode = mode;
75
76         return o;
77 }
78
79
80 #if defined(_WIN32) && !defined(WIN95_OSFHANDLE_FIXED) && defined(_M_IX86)
81
82 #       ifdef __cplusplus
83 #define EXT_C_FUNC      extern "C"
84 #       else
85 #define EXT_C_FUNC      extern
86 #       endif
87
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);
93
94 #if     (_MSC_VER >= 1000)
95         typedef struct
96         {
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)
101         int lockinitflag;
102         CRITICAL_SECTION lock;
103 #endif  /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
104         }       ioinfo;
105
106         EXT_C_FUNC ioinfo * __pioinfo[];
107
108         #define IOINFO_L2E                      5
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)
112 #else
113         extern "C" extern char _osfile[];
114 #endif  // (_MSC_VER >= 1000)
115
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
120
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
124
125 /***
126 *int _patch_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
127 *
128 *Purpose:
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
133 *
134 *Entry:
135 *       long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
136 *       int flags      - flags to associate with C Runtime file handle.
137 *
138 *Exit:
139 *       returns index of entry in fh, if successful
140 *       return -1, if no free entry is found
141 *
142 *Exceptions:
143 *
144 *******************************************************************************/
145
146 int my_open_osfhandle(long osfhandle, int flags)
147 {
148         int fh;
149         char fileflags;         // _osfile flags 
150
151         // copy relevant flags from second parameter 
152         fileflags = FDEV;
153
154         if(flags & _O_APPEND)
155                 fileflags |= FAPPEND;
156
157         if(flags & _O_TEXT)
158                 fileflags |= FTEXT;
159
160         // attempt to allocate a C Runtime file handle
161         if((fh = _alloc_osfhnd()) == -1)
162         {
163                 errno = EMFILE;         // too many open files 
164                 _doserrno = 0L;         // not an OS error
165                 return -1;                      // return error to caller
166         }
167
168         // the file is open. now, set the info in _osfhnd array
169         _set_osfhnd(fh, osfhandle);
170
171         fileflags |= FOPEN;                     // mark as open
172
173 #if (_MSC_VER >= 1000)
174         _osfile(fh) = fileflags;        // set osfile entry
175         _unlock_fhandle(fh);
176 #else
177         _osfile[fh] = fileflags;        // set osfile entry
178         _unlock(fh+_FH_LOCKS);          // unlock handle
179 #endif
180
181
182         return fh;                                      // return handle
183 }
184 #else
185 int __cdecl stolen_open_osfhandle(long osfhandle, int flags)
186 {
187         return _open_osfhandle(osfhandle, flags);
188 }
189 #endif  // _M_IX86
190
191 long my_get_osfhandle( int filehandle )
192 {
193         return _get_osfhandle(filehandle);
194 }
195
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)
243         my_open_osfhandle,
244         my_get_osfhandle,
245         87654321L,              //  end of structure
246 };
247 #endif
248
249 #ifdef __cplusplus
250 }
251 #endif