}
PerlIO *
-PerlIO_importFILE(FILE *stdio, int fl)
+PerlIO_importFILE(FILE *stdio, const char *mode)
{
int fd = fileno(stdio);
- PerlIO *r = PerlIO_fdopen(fd, "r+");
- return r;
+ if (!mode || !*mode) {
+ mmode = "r+";
+ }
+ return PerlIO_fdopen(fd, mode);
}
FILE *
IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
if (*PerlIONext(f)) {
- /* We never call down so any pending stuff now */
+ /* We never call down so do any pending stuff now */
PerlIO_flush(PerlIONext(f));
s->fd = PerlIO_fileno(PerlIONext(f));
/*
PerlIOStdio_mode(mode, tmode));
if (stdio) {
s->stdio = stdio;
- /* We never call down so any pending stuff now */
+ /* We never call down so do any pending stuff now */
PerlIO_flush(PerlIONext(f));
}
else
PerlIO *
-PerlIO_importFILE(FILE *stdio, int fl)
+PerlIO_importFILE(FILE *stdio, const char *mode)
{
dTHX;
PerlIO *f = NULL;
if (stdio) {
- /* We need to probe to see how we can open the stream
- so start with read/write and then try write and read
- we dup() so that we can fclose without loosing the fd.
-
- Note that the errno value set by a failing fdopen
- varies between stdio implementations.
- */
- int fd = PerlLIO_dup(fileno(stdio));
- char *mode = "r+";
- FILE *f2 = fdopen(fd, mode);
PerlIOStdio *s;
- if (!f2) {
- mode = "w";
- f2 = fdopen(fd, mode);
- }
- if (!f2) {
- mode = "r";
- f2 = fdopen(fd, mode);
- }
- if (!f2) {
- /* Don't seem to be able to open */
- PerlLIO_close(fd);
- return f;
+ if (!mode || !*mode) {
+ /* We need to probe to see how we can open the stream
+ so start with read/write and then try write and read
+ we dup() so that we can fclose without loosing the fd.
+
+ Note that the errno value set by a failing fdopen
+ varies between stdio implementations.
+ */
+ int fd = PerlLIO_dup(fileno(stdio));
+ FILE *f2 = fdopen(fd, (mode = "r+"));
+ if (!f2) {
+ f2 = fdopen(fd, (mode = "w"));
+ }
+ if (!f2) {
+ f2 = fdopen(fd, (mode = "r"));
+ }
+ if (!f2) {
+ /* Don't seem to be able to open */
+ PerlLIO_close(fd);
+ return f;
+ }
+ fclose(f2);
}
- fclose(f2);
s = PerlIOSelf(PerlIO_push
(aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
mode, Nullsv), PerlIOStdio);
};
FILE *
-PerlIO_exportFILE(PerlIO *f, int fl)
+PerlIO_exportFILE(PerlIO *f, const char *mode)
{
dTHX;
FILE *stdio;
char buf[8];
PerlIO_flush(f);
- stdio = fdopen(PerlIO_fileno(f), PerlIO_modestr(f,buf));
+ if (!mode || !*mode) {
+ mode = PerlIO_modestr(f,buf);
+ }
+ stdio = fdopen(PerlIO_fileno(f), mode);
if (stdio) {
PerlIOStdio *s =
PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv),
}
l = *PerlIONext(&l);
}
- return PerlIO_exportFILE(f, 0);
+ /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
+ return PerlIO_exportFILE(f, Nullch);
}
void
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
int fd = PerlIO_fileno(f);
- Off_t posn;
if (fd >= 0 && PerlLIO_isatty(fd)) {
PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
}
- posn = PerlIO_tell(PerlIONext(f));
- if (posn != (Off_t) - 1) {
- b->posn = posn;
+ if (*PerlIONext(f)) {
+ Off_t posn = PerlIO_tell(PerlIONext(f));
+ if (posn != (Off_t) - 1) {
+ b->posn = posn;
+ }
}
return PerlIOBase_pushed(aTHX_ f, mode, arg);
}
*/
b->posn += (b->ptr - buf);
if (b->ptr < b->end) {
- /*
- * We did not consume all of it
+ /* We did not consume all of it - try and seek downstream to
+ our logical position
*/
- if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
+ if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
/* Reload n as some layers may pop themselves on seek */
b->posn = PerlIO_tell(n = PerlIONext(f));
}
else {
+ /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
+ data is lost for good - so return saying "ok" having undone
+ the position adjust
+ */
+ b->posn -= (b->ptr - buf);
return code;
}
}
b->ptr = b->end = b->buf;
PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
/* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
- /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
if (PerlIOValid(n) && PerlIO_flush(n) != 0)
code = -1;
return code;
PerlIO *n = PerlIONext(f);
SSize_t avail;
/*
- * FIXME: doing the down-stream flush maybe sub-optimal if it causes
- * pre-read data in stdio buffer to be discarded.
- * However, skipping the flush also skips _our_ hosekeeping
- * and breaks tell tests. So we do the flush.
+ * Down-stream flush is defined not to loose read data so is harmless.
+ * we would not normally be fill'ing if there was data left in anycase.
*/
if (PerlIO_flush(f) != 0)
return -1;
PerlIO_get_base(f); /* allocate via vtable */
b->ptr = b->end = b->buf;
+
+ if (!PerlIOValid(n)) {
+ PerlIOBase(f)->flags |= PERLIO_F_EOF;
+ return -1;
+ }
+
if (PerlIO_fast_gets(n)) {
/*
* Layer below is also buffered. We do _NOT_ want to call its
extern PerlIO *PerlIO_fdopen(int, const char *);
#endif
#ifndef PerlIO_importFILE
-extern PerlIO *PerlIO_importFILE(FILE *, int);
+extern PerlIO *PerlIO_importFILE(FILE *, const char *);
#endif
#ifndef PerlIO_exportFILE
-extern FILE *PerlIO_exportFILE(PerlIO *, int);
+extern FILE *PerlIO_exportFILE(PerlIO *, const char *);
#endif
#ifndef PerlIO_findFILE
extern FILE *PerlIO_findFILE(PerlIO *);
struct _PerlIO {
PerlIOl *next; /* Lower layer */
PerlIO_funcs *tab; /* Functions for this layer */
- IV flags; /* Various flags for state */
+ U32 flags; /* Various flags for state */
};
/*--------------------------------------------------------------------------------------*/
char *PerlIO_get_base(PerlIO *f);
int PerlIO_get_bufsiz(PerlIO *f);
- PerlIO *PerlIO_importFILE(FILE *stdio, int flags);
+ PerlIO *PerlIO_importFILE(FILE *stdio, const char *mode);
FILE *PerlIO_exportFILE(PerlIO *f, int flags);
FILE *PerlIO_findFILE(PerlIO *f);
void PerlIO_releaseFILE(PerlIO *f,FILE *stdio);
These correspond to fread() and fwrite(). Note that arguments are
different, there is only one "count" and order has "file"
-first. Returns a byte count if successful (which may be zero), returns
+first. Returns a byte count if successful (which may be zero or positive), returns
negative value and sets C<errno> on error. Depending on
implementation C<errno> may be C<EINTR> if operation was interrupted
by a signal.
This corresponds to fflush(). Sends any buffered write data to the
underlying file. If called with C<NULL> this may flush all open
-streams (or core dump). Calling on a handle open for read only, or on
-which last operation was a read of some kind may lead to undefined
-behaviour.
+streams (or core dump with some USE_STDIO implementattions).
+Calling on a handle open for read only, or on which last operation was a read of some kind
+may lead to undefined behaviour on some USE_STDIO implementations.
+The USE_PERLIO (layers) implementation tries to behave better: it flushes all open streams
+when passed C<NULL>, and attempts to retain data on read streams either in the buffer
+or by seeking the handle to the current logical position.
=item B<PerlIO_seek(f,offset,whence)>
=over 4
-=item B<PerlIO_importFILE(f,flags)>
+=item B<PerlIO_importFILE(f,mode)>
Used to get a PerlIO * from a FILE *.
-The flags argument was meant to be used for read vs write vs
-read/write information. In hindsight it would have been better to make
-it a char *mode as in fopen/freopen. Flags are currently ignored, and
-code attempts to empirically determine the mode in which I<f> is open.
+The mode argument should be a string as would be passed to fopen/PerlIO_open.
+If it is NULL then - for legacy support - the code will (depending upon
+the platform and the implementation) either attempt to empirically determine the mode in
+which I<f> is open, or use "r+" to indicate a read/write stream.
Once called the FILE * should I<ONLY> be closed by calling
C<PerlIO_close()> on the returned PerlIO *.
The PerlIO is set to textmode. Use PerlIO_binmode if this is
not the desired mode.
-=item B<PerlIO_exportFILE(f,flags)>
+=item B<PerlIO_exportFILE(f,mode)>
Given a PerlIO * create a 'native' FILE * suitable for passing to code
expecting to be compiled and linked with ANSI C I<stdio.h>.
-The flags argument was meant to be used for read vs write vs
-read/write information. In hindsight it would have been better to make
-it a char *mode as in fopen/freopen. Flags are ignored and the
-FILE * is opened in same mode as the PerlIO *.
+The mode argument should be a string as would be passed to fopen/PerlIO_open.
+If it is NULL then - for legacy support - the FILE * is opened
+in same mode as the PerlIO *.
The fact that such a FILE * has been 'exported' is recorded, (normally by
pushing a new :stdio "layer" onto the PerlIO *), which may affect future
The new interface to the USE_PERLIO implementation. The layers ":crlf"
and ":raw" are only ones allowed for other implementations and those
-are silently ignored. Use PerlIO_binmode() below for the portable
-case.
+are silently ignored. (As of perl5.8 ":raw" is deprecated.)
+Use PerlIO_binmode() below for the portable case.
=item PerlIO_binmode(f,ptype,imode,layers)
B<imode> is C<O_BINARY> or C<O_TEXT>.
-B<layers> is a string of layers to apply, only ":raw" or :"crlf" make
-sense in the non USE_PERLIO case.
+B<layers> is a string of layers to apply, only ":crlf" makes sense in the non USE_PERLIO
+case. (As of perl5.8 ":raw" is deprecated in favour of passing NULL.)
Portable cases are:
- PerlIO_binmode(f,ptype,O_BINARY,":raw");
+ PerlIO_binmode(f,ptype,O_BINARY,Nullch);
and
PerlIO_binmode(f,ptype,O_TEXT,":crlf");
=item Co-existence with stdio
-B<PerlIO_importFILE(f,flags)>, B<PerlIO_exportFILE(f,flags)>,
+B<PerlIO_importFILE(f,mode)>, B<PerlIO_exportFILE(f,mode)>,
B<PerlIO_releaseFILE(p,f)>, B<PerlIO_findFILE(f)>
=item "Fast gets" Functions
=item Mac OS X
+=item OS/2 Test Failures
+
=item op/sprintf tests 91, 129, and 130
=item Solaris 2.5
=over 4
+=item NOTE
+
=item SYNOPSIS
=back
=back
+=item PERL 5.8.0 BROKEN IN AMIGAOS
+
=item AUTHORS
=item SEE ALSO
PERL, PERL_CORE, PERLMAINCC, PERL_ARCHLIB, PERL_LIB, PERL_MALLOC_OK,
PERLRUN, PERLRUNINST, PERL_SRC, PERM_RW, PERM_RWX, PL_FILES, PM, PMLIBDIRS,
PM_FILTER, POLLUTE, PPM_INSTALL_EXEC, PPM_INSTALL_SCRIPT, PREFIX,
-PREREQ_PM, PREREQ_FATAL, PREREQ_PRINT, PRINT_PREREQ, SITEPREFIX, SKIP,
+PREREQ_FATAL, PREREQ_PM, PREREQ_PRINT, PRINT_PREREQ, SITEPREFIX, SKIP,
TYPEMAPS, VENDORPREFIX, VERBINST, VERSION, VERSION_FROM, XS, XSOPT,
XSPROTOARG, XS_VERSION