1 #define PERL_NO_GET_CONTEXT
2 #define WIN32_LEAN_AND_MEAN
3 #define WIN32IO_IS_STDIO
18 /* Bottom-most level for Win32 case */
22 struct _PerlIO base; /* The generic part */
23 HANDLE h; /* OS level handle */
24 IV refcnt; /* REFCNT for the "fd" this represents */
25 int fd; /* UNIX like file descriptor - index into fdtable */
28 PerlIOWin32 *fdtable[256];
32 PerlIOWin32_popped(PerlIO *f)
34 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
37 *f = PerlIOBase(f)->next;
40 fdtable[s->fd] = NULL;
45 PerlIOWin32_fileno(PerlIO *f)
47 return PerlIOSelf(f,PerlIOWin32)->fd;
51 PerlIOWin32_pushed(PerlIO *f, const char *mode, SV *arg)
53 IV code = PerlIOBase_pushed(f,mode,arg);
56 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
57 s->fd = PerlIO_fileno(PerlIONext(f));
59 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
64 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)
66 const char *tmode = mode;
67 HANDLE h = INVALID_HANDLE_VALUE;
70 /* Close if already open */
71 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
72 (*PerlIOBase(f)->tab->Close)(f);
76 char *path = SvPV_nolen(*args);
80 DWORD attr = FILE_ATTRIBUTE_NORMAL;
83 /* sysopen - imode is UNIX-like O_RDONLY etc.
84 - do_open has converted that back to string form in mode as well
85 - perm is UNIX like permissions
91 /* Normal open - decode mode string */
96 access = GENERIC_READ;
97 create = OPEN_EXISTING;
100 access |= GENERIC_WRITE;
101 create = OPEN_ALWAYS;
107 access = GENERIC_WRITE;
108 create = TRUNCATE_EXISTING;
111 access |= GENERIC_READ;
117 access = GENERIC_WRITE;
118 create = OPEN_ALWAYS;
121 access |= GENERIC_READ;
130 else if (*mode == 't')
134 if (*mode || oflags == -1)
136 SETERRNO(EINVAL,LIB$_INVARG);
139 if (!(access & GENERIC_WRITE))
140 share = FILE_SHARE_READ;
141 h = CreateFile(path,access,share,NULL,create,attr,NULL);
142 if (h == INVALID_HANDLE_VALUE)
144 if (create == TRUNCATE_EXISTING)
145 h = CreateFile(path,access,share = OPEN_ALWAYS,NULL,create,attr,NULL);
151 h = INVALID_HANDLE_VALUE;
152 if (fd >= 0 && fd <= max_open_fd)
154 PerlIOWin32 *s = fdtable[fd];
159 f = PerlIO_allocate(aTHX);
169 h = GetStandardHandle(STD_INPUT_HANDLE);
172 h = GetStandardHandle(STD_OUTPUT_HANDLE);
175 h = GetStandardHandle(STD_ERROR_HANDLE);
181 if (h != INVALID_HANDLE_VALUE)
185 f = PerlIO_allocate(aTHX);
186 s = PerlIOSelf(PerlIO_push(aTHX_ f,self,tmode,PerlIOArg),PerlIOWin32);
193 /* FIXME: pop layers ??? */
199 PerlIOWin32_read(PerlIO *f, void *vbuf, Size_t count)
201 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
203 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
205 if (ReadFile(s->h,vbuf,count,&len,NULL)
211 if (GetLastError() != NO_ERROR)
213 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
219 PerlIOBase(f)->flags |= PERLIO_F_EOF;
226 PerlIOWin32_write(PerlIO *f, const void *vbuf, Size_t count)
228 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
230 if (WriteFile(s->h,vbuf,count,&len,NULL)
236 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
242 PerlIOWin32_seek(PerlIO *f, Off_t offset, int whence)
244 static const DWORD where[3] = { FILE_BEGIN, FILE_CURRENT, FILE_END };
245 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
246 DWORD high = (sizeof(offset) > sizeof(DWORD)) ? (DWORD)(offset >> 32) : 0;
247 DWORD low = (DWORD) offset;
248 DWORD res = SetFilePointer(s->h,low,&high,where[whence]);
249 if (res != 0xFFFFFFFF || GetLastError() != NO_ERROR)
260 PerlIOWin32_tell(PerlIO *f)
262 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
264 DWORD res = SetFilePointer(s->h,0,&high,FILE_CURRENT);
265 if (res != 0xFFFFFFFF || GetLastError() != NO_ERROR)
267 return ((Off_t) high << 32) | res;
273 PerlIOWin32_close(PerlIO *f)
275 PerlIOWin32 *s = PerlIOSelf(f,PerlIOWin32);
278 if (CloseHandle(s->h))
280 s->h = INVALID_HANDLE_VALUE;
284 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
288 PerlIO_funcs PerlIO_win32 = {
303 PerlIOBase_noop_ok, /* flush */
304 PerlIOBase_noop_fail, /* fill */
308 PerlIOBase_setlinebuf,
310 NULL, /* get_bufsiz */
313 NULL, /* set_ptrcnt */