else {
PerlIO *that_fp = NULL;
int wanted_fd;
+ UV uv;
if (num_svs > 1) {
/* diag_listed_as: More than one argument to '%s' open */
Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
wanted_fd = SvUV(*svp);
num_svs = 0;
}
- else if (isDIGIT(*type)) {
- wanted_fd = atoi(type);
+ else if (isDIGIT(*type)
+ && grok_atoUV(type, &uv, NULL)
+ && uv <= INT_MAX
+ ) {
+ wanted_fd = (int)uv;
}
else {
const IO* thatio;
*s = 'w';
if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
PerlIO_close(fp);
- IoIFP(io) = NULL;
goto say_false;
}
}
}
return TRUE;
-say_false:
+ say_false:
IoIFP(io) = saveifp;
IoOFP(io) = saveofp;
IoTYPE(io) = savetype;
}
PerlIO *
-Perl_nextargv(pTHX_ GV *gv)
+Perl_nextargv(pTHX_ GV *gv, bool nomagicopen)
{
IO * const io = GvIOp(gv);
if (!PL_argvoutgv)
PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
- if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
+ if (io && (IoFLAGS(io) & (IOf_ARGV|IOf_START)) == (IOf_ARGV|IOf_START)) {
IoFLAGS(io) &= ~IOf_START;
if (PL_inplace) {
assert(PL_defoutgv);
SvSETMAGIC(GvSV(gv));
PL_oldname = SvPVx(GvSV(gv), oldlen);
if (LIKELY(!PL_inplace)) {
- if (do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)) {
+ if (nomagicopen
+ ? do_open6(gv, "<", 1, NULL, &GvSV(gv), 1)
+ : do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)
+ ) {
return IoIFP(GvIOp(gv));
}
}
}
return FALSE;
}
- retval = io_close(io, not_implicit);
+ retval = io_close(io, NULL, not_implicit, FALSE);
if (not_implicit) {
IoLINES(io) = 0;
IoPAGE(io) = 0;
}
bool
-Perl_io_close(pTHX_ IO *io, bool not_implicit)
+Perl_io_close(pTHX_ IO *io, GV *gv, bool not_implicit, bool warn_on_fail)
{
bool retval = FALSE;
else {
if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
const bool prev_err = PerlIO_error(IoOFP(io));
+#ifdef USE_PERLIO
+ if (prev_err)
+ PerlIO_restore_errno(IoOFP(io));
+#endif
retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
}
else {
const bool prev_err = PerlIO_error(IoIFP(io));
+#ifdef USE_PERLIO
+ if (prev_err)
+ PerlIO_restore_errno(IoIFP(io));
+#endif
retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
}
}
IoOFP(io) = IoIFP(io) = NULL;
+
+ if (warn_on_fail && !retval) {
+ if (gv)
+ Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
+ "Warning: unable to close filehandle %"
+ HEKf" properly: %"SVf,
+ GvNAME_HEK(gv), get_sv("!",GV_ADD));
+ else
+ Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
+ "Warning: unable to close filehandle "
+ "properly: %"SVf,
+ get_sv("!",GV_ADD));
+ }
}
else if (not_implicit) {
SETERRNO(EBADF,SS_IVCHAN);
PerlIO_set_cnt(IoIFP(io),-1);
}
if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
- if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
+ if (gv != PL_argvgv || !nextargv(gv, FALSE)) /* get another fp handy */
return TRUE;
}
else
}
else {
const char *end;
-fail_discipline:
+ fail_discipline:
end = strchr(s+1, ':');
if (!end)
end = s+len;
#endif
}
else {
+ SETERRNO(EBADF,RMS_IFI);
tot--;
}
}
#endif
}
else {
+ SETERRNO(EBADF,RMS_IFI);
tot--;
}
}
}
/*
- * Local variables:
- * c-indentation-style: bsd
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- *
* ex: set ts=8 sts=4 sw=4 et:
*/