STRLEN olen = len;
char *tend;
int dodup = 0;
+ PerlIO *that_fp = NULL;
type = savepvn(name, len);
tend = type+len;
if (*type == '&') {
duplicity:
- dodup = 1;
+ dodup = PERLIO_DUP_FD;
type++;
if (*type == '=') {
dodup = 0;
goto say_false;
}
if (IoIFP(thatio)) {
- PerlIO *fp = IoIFP(thatio);
+ that_fp = IoIFP(thatio);
/* Flush stdio buffer before dup. --mjd
* Unfortunately SEEK_CURing 0 seems to
* be optimized away on most platforms;
/* sfio fails to clear error on next
sfwrite, contrary to documentation.
-- Nick Clark */
- if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
- PerlIO_clearerr(fp);
+ if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
+ PerlIO_clearerr(that_fp);
#endif
/* On the other hand, do all platforms
* take gracefully to flushing a read-only
* filehandle? Perhaps we should do
* fsetpos(src)+fgetpos(dst)? --nik */
- PerlIO_flush(fp);
- fd = PerlIO_fileno(fp);
+ PerlIO_flush(that_fp);
+ fd = PerlIO_fileno(that_fp);
/* When dup()ing STDIN, STDOUT or STDERR
* explicitly set appropriate access mode */
if (IoIFP(thatio) == PerlIO_stdout()
else
fd = -1;
}
- if (dodup)
- fd = PerlLIO_dup(fd);
- else
- was_fdopen = TRUE;
if (!num_svs)
type = Nullch;
- if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
+ if (that_fp) {
+ fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
+ }
+ else {
if (dodup)
- PerlLIO_close(fd);
+ fd = PerlLIO_dup(fd);
+ else
+ was_fdopen = TRUE;
+ if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
+ if (dodup)
+ PerlLIO_close(fd);
+ }
}
}
} /* & */
if (savefd != fd) {
Pid_t pid;
SV *sv;
+ /* Still a small can-of-worms here if (say) PerlIO::Scalar
+ is assigned to (say) STDOUT - for now let dup2() fail
+ and provide the error
+ */
if (PerlLIO_dup2(fd, savefd) < 0) {
(void)PerlIO_close(fp);
goto say_false;
UNLOCK_FDPID_MUTEX;
(void)SvUPGRADE(sv, SVt_IV);
SvIVX(sv) = pid;
- if (!was_fdopen)
+ if (!was_fdopen) {
PerlIO_close(fp);
+ }
}
fp = saveifp;
PerlIO_clearerr(fp);
} PerlIOEncode;
SV *
-PerlIOEncode_getarg(PerlIO *f)
+PerlIOEncode_getarg(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
- dTHX;
PerlIOEncode *e = PerlIOSelf(f,PerlIOEncode);
SV *sv = &PL_sv_undef;
if (e->enc)
}
PerlIO *
-PerlIOEncode_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *params)
+PerlIOEncode_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *params, int flags)
{
- if ((f = PerlIOBase_dup(aTHX_ f, o, params)))
+ if ((f = PerlIOBase_dup(aTHX_ f, o, params, flags)))
{
PerlIOEncode *fe = PerlIOSelf(f,PerlIOEncode);
PerlIOEncode *oe = PerlIOSelf(o,PerlIOEncode);
}
sv_upgrade(s->var,SVt_PV);
code = PerlIOBase_pushed(f,mode,Nullsv);
+ if ((PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
+ SvCUR(s->var) = 0;
if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND)
- s->posn = SvCUR(SvRV(arg));
+ s->posn = SvCUR(s->var);
else
s->posn = 0;
- if ((PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
- SvCUR(SvRV(arg)) = 0;
return code;
}
return NULL;
}
+SV *
+PerlIOScalar_arg(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
+{
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ SV *var = s->var;
+ if (flags & PERLIO_DUP_CLONE)
+ var = PerlIO_sv_dup(aTHX_ var, param);
+ else if (flags & PERLIO_DUP_FD)
+ {
+ /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
+ var = newSVsv(var);
+ }
+ else
+ {
+ var = SvREFCNT_inc(var);
+ }
+ return newRV_noinc(var);
+}
+
PerlIO *
-PerlIOScalar_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOScalar_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- if ((f = PerlIOBase_dup(aTHX_ f, o, param)))
+ if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags)))
{
PerlIOScalar *fs = PerlIOSelf(f,PerlIOScalar);
PerlIOScalar *os = PerlIOSelf(o,PerlIOScalar);
PerlIOScalar_pushed,
PerlIOScalar_popped,
PerlIOScalar_open,
- NULL,
+ PerlIOScalar_arg,
PerlIOScalar_fileno,
PerlIOScalar_dup,
PerlIOBase_read,
PerlIOBase_clearerr(f);
}
-SV *
-PerlIOVia_getarg(PerlIO *f)
-{
- dTHX;
- PerlIOVia *s = PerlIOSelf(f,PerlIOVia);
- return PerlIOVia_method(aTHX_ f,MYMethod(GETARG),G_SCALAR,Nullsv);
-}
-
IV
PerlIOVia_error(PerlIO *f)
{
return (result) ? SvIV(result) : PerlIOBase_eof(f);
}
+SV *
+PerlIOVia_getarg(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
+{
+ PerlIOVia *s = PerlIOSelf(f,PerlIOVia);
+ return PerlIOVia_method(aTHX_ f,MYMethod(GETARG),G_SCALAR,Nullsv);
+}
+
PerlIO *
-PerlIOVia_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOVia_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- if ((f = PerlIOBase_dup(aTHX_ f, o, param)))
+ if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags)))
{
- /* Most of the fields will lazily set them selves up as needed
+ /* Most of the fields will lazily set themselves up as needed
stash and obj have been set up by the implied push
*/
}
}
$| = 1;
-print "1..20\n";
+print "1..22\n";
my $fh;
my $var = "ok 2\n";
print "# Got [$var], expect [foo]\n";
print "not " unless $var eq "foo";
print "ok 20\n";
+
+# Check that dup'ing the handle works
+
+$var = '';
+
+open $fh, "+>", \$var;
+print $fh "ok 21\n";
+open $dup,'+<&',$fh;
+print $dup "ok 22\n";
+seek($dup,0,0);
+while (<$dup>) {
+ print;
+}
+close($fh);
+close($dup);
+
}
PerlIO *
-PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param)
+PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
#ifndef PERL_MICRO
if (f) {
#undef PerlIO_fdupopen
PerlIO *
-PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param)
+PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
if (f && *f) {
PerlIO_funcs *tab = PerlIOBase(f)->tab;
PerlIO *new;
PerlIO_debug("fdupopen f=%p param=%p\n",f,param);
- new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param);
+ new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
return new;
}
else {
while (l) {
SV *arg =
(l->tab->Getarg) ? (*l->tab->
- Getarg) (&l) : &PL_sv_undef;
+ Getarg) (aTHX_ &l, NULL, 0) : &PL_sv_undef;
PerlIO_list_push(aTHX_ layera, l->tab, arg);
l = *PerlIONext(&l);
}
}
PerlIO *
-PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
PerlIO *nexto = PerlIONext(o);
if (*nexto) {
PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
- f = (*tab->Dup)(aTHX_ f, nexto, param);
+ f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
}
if (f) {
PerlIO_funcs *self = PerlIOBase(o)->tab;
char buf[8];
PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",self->name,f,o,param);
if (self->Getarg) {
- arg = (*self->Getarg)(o);
- if (arg) {
- arg = PerlIO_sv_dup(aTHX_ arg, param);
- }
+ arg = (*self->Getarg)(aTHX_ o,param,flags);
}
f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
- if (!f && arg) {
+ if (arg) {
SvREFCNT_dec(arg);
}
}
}
PerlIO *
-PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
int fd = os->fd;
+ if (flags & PERLIO_DUP_FD) {
+ fd = PerlLIO_dup(fd);
+ }
if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
- f = PerlIOBase_dup(aTHX_ f, o, param);
+ f = PerlIOBase_dup(aTHX_ f, o, param, flags);
if (f) {
/* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
}
PerlIO *
-PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
/* This assumes no layers underneath - which is what
happens, but is not how I remember it. NI-S 2001/10/16
*/
- if ((f = PerlIOBase_dup(aTHX_ f, o, param))) {
+ if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
+ if (flags & PERLIO_DUP_FD) {
+ int fd = PerlLIO_dup(fileno(stdio));
+ if (fd >= 0) {
+ char mode[8];
+ int omode = fcntl(fd, F_GETFL);
+ PerlIO_intmode2str(omode,mode,NULL);
+ stdio = fdopen(fd, mode);
+ }
+ else {
+ /* FIXME: To avoid messy error recovery if dup fails
+ re-use the existing stdio as though flag was not set
+ */
+ }
+ }
PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(stdio));
}
}
PerlIO *
-PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- return PerlIOBase_dup(aTHX_ f, o, param);
+ return PerlIOBase_dup(aTHX_ f, o, param, flags);
}
}
PerlIO *
-PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- return PerlIOBase_dup(aTHX_ f, o, param);
+ return PerlIOBase_dup(aTHX_ f, o, param, flags);
}
#define SEEK_END 2
#endif
+#define PERLIO_DUP_CLONE 1
+#define PERLIO_DUP_FD 2
+
/* --------------------- Now prototypes for functions --------------- */
START_EXTERN_C
extern int PerlIO_setpos(PerlIO *, SV *);
#endif
#ifndef PerlIO_fdupopen
-extern PerlIO *PerlIO_fdupopen(pTHX_ PerlIO *, CLONE_PARAMS *);
+extern PerlIO *PerlIO_fdupopen(pTHX_ PerlIO *, CLONE_PARAMS *, int);
#endif
#if !defined(PerlIO_modestr) && !defined(PERLIO_IS_STDIO)
extern char *PerlIO_modestr(PerlIO *, char *buf);
const char *mode,
int fd, int imode, int perm,
PerlIO *old, int narg, SV **args);
- SV *(*Getarg) (PerlIO *f);
+ SV *(*Getarg) (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags);
IV (*Fileno) (PerlIO *f);
- PerlIO *(*Dup) (pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param);
+ PerlIO *(*Dup) (pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags);
/* Unix-like functions - cf sfio line disciplines */
SSize_t(*Read) (PerlIO *f, void *vbuf, Size_t count);
SSize_t(*Unread) (PerlIO *f, const void *vbuf, Size_t count);
/* Generic, or stub layer functions */
extern IV PerlIOBase_fileno(PerlIO *f);
-extern PerlIO *PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param);
+extern PerlIO *PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags);
extern IV PerlIOBase_pushed(PerlIO *f, const char *mode, SV *arg);
extern IV PerlIOBase_popped(PerlIO *f);
extern SSize_t PerlIOBase_read(PerlIO *f, void *vbuf, Size_t count);
const char *mode, int fd, int imode,
int perm, PerlIO *old, int narg, SV **args);
extern IV PerlIOBuf_pushed(PerlIO *f, const char *mode, SV *arg);
-extern PerlIO *PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param);
+extern PerlIO *PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags);
extern SSize_t PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count);
extern SSize_t PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count);
extern SSize_t PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count);
char *limit = tmpbuf + sizeof(tmpbuf) - 8;
/* each *s can expand to 4 chars + "...\0",
i.e. need room for 8 chars */
-
+
char *s, *end;
for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
int ch = *s & 0xFF;
}
if (hibit) {
STRLEN len;
-
+
len = SvCUR(sv) + 1; /* Plus the \0 */
SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
SvCUR(sv) = len - 1;
mg_set(tmpRef);
-
+
return sv;
}
return ret;
/* create anew and remember what it is */
- ret = PerlIO_fdupopen(aTHX_ fp, param);
+ ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
ptr_table_store(PL_ptr_table, fp, ret);
return ret;
}