return NULL;
}
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
}
#endif
return NULL;
sfset(sfstdout, SF_SHARE, 0);
}
+/* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
PerlIO *
PerlIO_importFILE(FILE *stdio, const char *mode)
{
return new;
}
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return NULL;
}
}
}
if (load && PL_subname && PL_def_layerlist
&& PL_def_layerlist->cur >= 2) {
- SV *pkgsv = newSVpvn("PerlIO", 6);
- SV *layer = newSVpvn(name, len);
- ENTER;
- /*
- * The two SVs are magically freed by load_module
- */
- Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
- LEAVE;
- return PerlIO_find_layer(aTHX_ name, len, 0);
+ if (PL_in_load_module) {
+ Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
+ return NULL;
+ } else {
+ SV *pkgsv = newSVpvn("PerlIO", 6);
+ SV *layer = newSVpvn(name, len);
+ ENTER;
+ SAVEINT(PL_in_load_module);
+ PL_in_load_module++;
+ /*
+ * The two SVs are magically freed by load_module
+ */
+ Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
+ PL_in_load_module--;
+ LEAVE;
+ return PerlIO_find_layer(aTHX_ name, len, 0);
+ }
}
PerlIO_debug("Cannot find %.*s\n", (int) len, name);
return NULL;
Perl_warner(aTHX_ packWARN(WARN_LAYER),
"perlio: invalid separator character %c%c%c in layer specification list %s",
q, *s, q, s);
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
do {
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
}
else {
PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
else {
PerlIO_debug("Cannot flush f=%p\n", (void*)f);
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
(*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
else
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
}
void
if (PerlIOValid(f))
(*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
else
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
}
int
l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
break;
default:
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
while (*mode) {
l->flags |= PERLIO_F_CRLF;
break;
default:
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
}
*/
oflags |= O_BINARY;
if (*mode || oflags == -1) {
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
oflags = -1;
}
return oflags;
}
}
else {
- SETERRNO(EBADF,SS$_IVCHAN);
+ SETERRNO(EBADF,SS_IVCHAN);
return -1;
}
while (PerlLIO_close(fd) != 0) {
*/
if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
+ int fd = fileno(stdio);
+ char mode[8];
if (flags & PERLIO_DUP_FD) {
- int fd = PerlLIO_dup(fileno(stdio));
- if (fd >= 0) {
- char mode[8];
- stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
+ int dfd = PerlLIO_dup(fileno(stdio));
+ if (dfd >= 0) {
+ stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
+ goto set_this;
}
else {
/* FIXME: To avoid messy error recovery if dup fails
*/
}
}
+ stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
+ set_this:
PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(stdio));
}
errno = EBADF;
return -1;
}
- if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
- /* Do not close it but do flush any buffers */
- return PerlIO_flush(f);
- }
- return (
+ else {
+ int fd = fileno(stdio);
+ int dupfd = -1;
+ IV result;
+ if (PerlIOUnix_refcnt_dec(fd) > 0) {
+ /* File descriptor still in use */
+ if (fd < 3) {
+ /* For STD* handles don't close the stdio at all */
+ return PerlIO_flush(f);
+ }
+ else {
+ /* Tricky - must fclose(stdio) to free memory but not close(fd) */
+ dupfd = PerlLIO_dup(fd);
+ }
+ }
+ result = (
#ifdef SOCKS5_VERSION_NAME
(getsockopt
(PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
PerlSIO_fclose(stdio)
#endif
);
+ if (dupfd >= 0) {
+ /* We need to restore fd from the saved copy */
+ if (PerlLIO_dup2(dupfd,fd) != fd)
+ result = -1;
+ if (PerlLIO_close(dupfd) != 0)
+ result = -1;
+ }
+ return result;
+ }
}
#endif /* USE_STDIO_PTR */
};
+/* Note that calls to PerlIO_exportFILE() are reversed using
+ * PerlIO_releaseFILE(), not importFILE. */
FILE *
PerlIO_exportFILE(PerlIO * f, const char *mode)
{
return PerlIO_exportFILE(f, Nullch);
}
+/* Use this to reverse PerlIO_exportFILE calls. */
void
PerlIO_releaseFILE(PerlIO *p, FILE *f)
{
PerlIO_get_base(f);
if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
return 0;
+ if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
+ if (PerlIO_flush(f) != 0) {
+ return 0;
+ }
+ }
while (count > 0) {
SSize_t avail = b->bufsiz - (b->ptr - b->buf);
if ((SSize_t) count < avail)
* b->posn is file position where b->buf was read, or will be written
*/
Off_t posn = b->posn;
+ if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
+ (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
+#if 1
+ /* As O_APPEND files are normally shared in some sense it is better
+ to flush :
+ */
+ PerlIO_flush(f);
+#else
+ /* when file is NOT shared then this is sufficient */
+ PerlIO_seek(PerlIONext(f),0, SEEK_END);
+#endif
+ posn = b->posn = PerlIO_tell(PerlIONext(f));
+ }
if (b->buf) {
/*
* If buffer is valid adjust position by amount in buffer
b->ptr++; /* say we have read it as far as
* flush() is concerned */
b->buf++; /* Leave space in front of buffer */
+ /* Note as we have moved buf up flush's
+ posn += ptr-buf
+ will naturally make posn point at CR
+ */
b->bufsiz--; /* Buffer is thus smaller */
code = PerlIO_fill(f); /* Fetch some more */
b->bufsiz++; /* Restore size for next time */
b->buf--; /* Point at space */
b->ptr = nl = b->buf; /* Which is what we hand
* off */
- b->posn--; /* Buffer starts here */
*nl = 0xd; /* Fill in the CR */
if (code == 0)
goto test; /* fill() call worked */
if (!page_size) {
#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
{
- SETERRNO(0, SS$_NORMAL);
+ SETERRNO(0, SS_NORMAL);
# ifdef _SC_PAGESIZE
page_size = sysconf(_SC_PAGESIZE);
# else
if (f && len == sizeof(Off_t))
return PerlIO_seek(f, *posn, SEEK_SET);
}
- SETERRNO(EINVAL, SS$_IVCHAN);
+ SETERRNO(EINVAL, SS_IVCHAN);
return -1;
}
#else
#endif
}
}
- SETERRNO(EINVAL, SS$_IVCHAN);
+ SETERRNO(EINVAL, SS_IVCHAN);
return -1;
}
#endif