1 #define PERL_NO_GET_CONTEXT
2 #define WIN32_LEAN_AND_MEAN
3 #define WIN32IO_IS_STDIO
22 /* Bottom-most level for Win32 case */
26 struct _PerlIO base; /* The generic part */
27 HANDLE h; /* OS level handle */
28 IV refcnt; /* REFCNT for the "fd" this represents */
29 int fd; /* UNIX like file descriptor - index into fdtable */
32 PerlIOWin32 *fdtable[256];
36 PerlIOWin32_popped(pTHX_ PerlIO *f)
38 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
41 *f = PerlIOBase(f)->next;
44 fdtable[s->fd] = NULL;
49 PerlIOWin32_fileno(pTHX_ PerlIO *f)
51 return PerlIOSelf(f,PerlIOWin32)->fd;
55 PerlIOWin32_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
57 IV code = PerlIOBase_pushed(aTHX_ f,mode,arg,tab);
60 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
61 s->fd = PerlIO_fileno(PerlIONext(f));
63 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
68 PerlIOWin32_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args)
70 const char *tmode = mode;
71 HANDLE h = INVALID_HANDLE_VALUE;
74 /* Close if already open */
75 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
76 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
80 char *path = SvPV_nolen(*args);
84 DWORD attr = FILE_ATTRIBUTE_NORMAL;
87 /* sysopen - imode is UNIX-like O_RDONLY etc.
88 - do_open has converted that back to string form in mode as well
89 - perm is UNIX like permissions
95 /* Normal open - decode mode string */
100 access = GENERIC_READ;
101 create = OPEN_EXISTING;
104 access |= GENERIC_WRITE;
105 create = OPEN_ALWAYS;
111 access = GENERIC_WRITE;
112 create = TRUNCATE_EXISTING;
115 access |= GENERIC_READ;
121 access = GENERIC_WRITE;
122 create = OPEN_ALWAYS;
125 access |= GENERIC_READ;
134 else if (*mode == 't')
138 if (*mode || create == -1)
140 SETERRNO(EINVAL,LIB$_INVARG);
143 if (!(access & GENERIC_WRITE))
144 share = FILE_SHARE_READ;
145 h = CreateFile(path,access,share,NULL,create,attr,NULL);
146 if (h == INVALID_HANDLE_VALUE)
148 if (create == TRUNCATE_EXISTING)
149 h = CreateFile(path,access,share,NULL,(create = OPEN_ALWAYS),attr,NULL);
155 h = INVALID_HANDLE_VALUE;
156 if (fd >= 0 && fd <= max_open_fd)
158 PerlIOWin32 *s = fdtable[fd];
163 f = PerlIO_allocate(aTHX);
174 h = GetStdHandle(STD_INPUT_HANDLE);
177 h = GetStdHandle(STD_OUTPUT_HANDLE);
180 h = GetStdHandle(STD_ERROR_HANDLE);
185 if (h != INVALID_HANDLE_VALUE)
186 fd = win32_open_osfhandle((intptr_t) h, PerlIOUnix_oflags(tmode));
191 f = PerlIO_allocate(aTHX);
192 s = PerlIOSelf(PerlIO_push(aTHX_ f,self,tmode,PerlIOArg),PerlIOWin32);
199 if (fd > max_open_fd)
206 /* FIXME: pop layers ??? */
212 PerlIOWin32_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
214 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
216 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
218 if (ReadFile(s->h,vbuf,count,&len,NULL))
224 if (GetLastError() != NO_ERROR)
226 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
232 PerlIOBase(f)->flags |= PERLIO_F_EOF;
239 PerlIOWin32_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
241 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
243 if (WriteFile(s->h,vbuf,count,&len,NULL))
249 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
255 PerlIOWin32_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
257 static const DWORD where[3] = { FILE_BEGIN, FILE_CURRENT, FILE_END };
258 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
259 DWORD high = (sizeof(offset) > sizeof(DWORD)) ? (DWORD)(offset >> 32) : 0;
260 DWORD low = (DWORD) offset;
261 DWORD res = SetFilePointer(s->h,(LONG)low,(LONG *)&high,where[whence]);
262 if (res != 0xFFFFFFFF || GetLastError() != NO_ERROR)
273 PerlIOWin32_tell(pTHX_ PerlIO *f)
275 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
277 DWORD res = SetFilePointer(s->h,0,(LONG *)&high,FILE_CURRENT);
278 if (res != 0xFFFFFFFF || GetLastError() != NO_ERROR)
280 return ((Off_t) high << 32) | res;
286 PerlIOWin32_close(pTHX_ PerlIO *f)
288 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
293 /* This does not do pipes etc. correctly */
294 if (!CloseHandle(s->h))
296 s->h = INVALID_HANDLE_VALUE;
300 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
301 return win32_close(s->fd);
308 PerlIOWin32_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *params, int flags)
310 PerlIOWin32 *os = PerlIOSelf(f,PerlIOWin32);
311 HANDLE proc = GetCurrentProcess();
313 if (DuplicateHandle(proc, os->h, proc, &new, 0, FALSE, DUPLICATE_SAME_ACCESS))
316 int fd = win32_open_osfhandle((intptr_t) new, PerlIOUnix_oflags(PerlIO_modestr(o,mode)));
319 f = PerlIOBase_dup(aTHX_ f, o, params, flags);
322 PerlIOWin32 *fs = PerlIOSelf(f,PerlIOWin32);
327 if (fd > max_open_fd)
343 PERLIO_FUNCS_DECL(PerlIO_win32) = {
344 sizeof(PerlIO_funcs),
361 PerlIOBase_noop_ok, /* flush */
362 PerlIOBase_noop_fail, /* fill */
366 PerlIOBase_setlinebuf,
368 NULL, /* get_bufsiz */
371 NULL, /* set_ptrcnt */