This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid select() on windows destroying errno (used to typically
[perl5.git] / pp_sys.c
CommitLineData
a0d0e21e
LW
1/* pp_sys.c
2 *
4bb101f2
JH
3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
a0d0e21e
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
11/*
12 * But only a short way ahead its floor and the walls on either side were
13 * cloven by a great fissure, out of which the red glare came, now leaping
14 * up, now dying down into darkness; and all the while far below there was
15 * a rumour and a trouble as of great engines throbbing and labouring.
16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_PP_SYS_C
a0d0e21e
LW
20#include "perl.h"
21
f1066039
JH
22#ifdef I_SHADOW
23/* Shadow password support for solaris - pdo@cs.umd.edu
24 * Not just Solaris: at least HP-UX, IRIX, Linux.
3813c136
JH
25 * The API is from SysV.
26 *
27 * There are at least two more shadow interfaces,
28 * see the comments in pp_gpwent().
29 *
30 * --jhi */
31# ifdef __hpux__
c529f79d 32/* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
301e8125 33 * and another MAXINT from "perl.h" <- <sys/param.h>. */
3813c136
JH
34# undef MAXINT
35# endif
36# include <shadow.h>
8c0bfa08
PB
37#endif
38
301e8125
NIS
39#ifdef HAS_SYSCALL
40#ifdef __cplusplus
8ac85365
NIS
41extern "C" int syscall(unsigned long,...);
42#endif
43#endif
44
76c32331
PP
45#ifdef I_SYS_WAIT
46# include <sys/wait.h>
47#endif
48
49#ifdef I_SYS_RESOURCE
50# include <sys/resource.h>
16d20bd9 51#endif
a0d0e21e 52
2986a63f
JH
53#ifdef NETWARE
54NETDB_DEFINE_CONTEXT
55#endif
56
a0d0e21e 57#ifdef HAS_SELECT
1e743fda
JH
58# ifdef I_SYS_SELECT
59# include <sys/select.h>
60# endif
a0d0e21e 61#endif
a0d0e21e 62
dc45a647
MB
63/* XXX Configure test needed.
64 h_errno might not be a simple 'int', especially for multi-threaded
5ff3f7a4
GS
65 applications, see "extern int errno in perl.h". Creating such
66 a test requires taking into account the differences between
67 compiling multithreaded and singlethreaded ($ccflags et al).
68 HOST_NOT_FOUND is typically defined in <netdb.h>.
dc45a647 69*/
cb50131a 70#if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
a0d0e21e
LW
71extern int h_errno;
72#endif
73
74#ifdef HAS_PASSWD
75# ifdef I_PWD
76# include <pwd.h>
77# else
fd8cd3a3 78# if !defined(VMS)
20ce7b12
GS
79 struct passwd *getpwnam (char *);
80 struct passwd *getpwuid (Uid_t);
fd8cd3a3 81# endif
a0d0e21e 82# endif
28e8609d 83# ifdef HAS_GETPWENT
10bc17b6 84#ifndef getpwent
20ce7b12 85 struct passwd *getpwent (void);
c2a8f790
JH
86#elif defined (VMS) && defined (my_getpwent)
87 struct passwd *Perl_my_getpwent (void);
10bc17b6 88#endif
28e8609d 89# endif
a0d0e21e
LW
90#endif
91
92#ifdef HAS_GROUP
93# ifdef I_GRP
94# include <grp.h>
95# else
20ce7b12
GS
96 struct group *getgrnam (char *);
97 struct group *getgrgid (Gid_t);
a0d0e21e 98# endif
28e8609d 99# ifdef HAS_GETGRENT
10bc17b6 100#ifndef getgrent
20ce7b12 101 struct group *getgrent (void);
10bc17b6 102#endif
28e8609d 103# endif
a0d0e21e
LW
104#endif
105
106#ifdef I_UTIME
3730b96e 107# if defined(_MSC_VER) || defined(__MINGW32__)
3fe9a6f1
PP
108# include <sys/utime.h>
109# else
110# include <utime.h>
111# endif
a0d0e21e 112#endif
a0d0e21e 113
cbdc8872 114#ifdef HAS_CHSIZE
cd52b7b2
PP
115# ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
116# undef my_chsize
117# endif
6ad3d225 118# define my_chsize PerlLIO_chsize
cbdc8872
PP
119#endif
120
ff68c719
PP
121#ifdef HAS_FLOCK
122# define FLOCK flock
123#else /* no flock() */
124
36477c24
PP
125 /* fcntl.h might not have been included, even if it exists, because
126 the current Configure only sets I_FCNTL if it's needed to pick up
127 the *_OK constants. Make sure it has been included before testing
128 the fcntl() locking constants. */
129# if defined(HAS_FCNTL) && !defined(I_FCNTL)
130# include <fcntl.h>
131# endif
132
9d9004a9 133# if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
ff68c719
PP
134# define FLOCK fcntl_emulate_flock
135# define FCNTL_EMULATE_FLOCK
136# else /* no flock() or fcntl(F_SETLK,...) */
137# ifdef HAS_LOCKF
138# define FLOCK lockf_emulate_flock
139# define LOCKF_EMULATE_FLOCK
140# endif /* lockf */
141# endif /* no flock() or fcntl(F_SETLK,...) */
142
143# ifdef FLOCK
20ce7b12 144 static int FLOCK (int, int);
ff68c719
PP
145
146 /*
147 * These are the flock() constants. Since this sytems doesn't have
148 * flock(), the values of the constants are probably not available.
149 */
150# ifndef LOCK_SH
151# define LOCK_SH 1
152# endif
153# ifndef LOCK_EX
154# define LOCK_EX 2
155# endif
156# ifndef LOCK_NB
157# define LOCK_NB 4
158# endif
159# ifndef LOCK_UN
160# define LOCK_UN 8
161# endif
162# endif /* emulating flock() */
163
164#endif /* no flock() */
55497cff 165
85ab1d1d
JH
166#define ZBTLEN 10
167static char zero_but_true[ZBTLEN + 1] = "0 but true";
168
5ff3f7a4
GS
169#if defined(I_SYS_ACCESS) && !defined(R_OK)
170# include <sys/access.h>
171#endif
172
c529f79d
CB
173#if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
174# define FD_CLOEXEC 1 /* NeXT needs this */
175#endif
176
a4af207c
JH
177#include "reentr.h"
178
5ff3f7a4
GS
179#undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
180#undef PERL_EFF_ACCESS_W_OK
181#undef PERL_EFF_ACCESS_X_OK
182
183/* F_OK unused: if stat() cannot find it... */
184
185#if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
c955f117 186 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
5ff3f7a4
GS
187# define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
188# define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
189# define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
190#endif
191
192#if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
3813c136 193# ifdef I_SYS_SECURITY
5ff3f7a4
GS
194# include <sys/security.h>
195# endif
c955f117
JH
196# ifdef ACC_SELF
197 /* HP SecureWare */
198# define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
199# define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
200# define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
201# else
202 /* SCO */
203# define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
204# define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
205# define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
206# endif
5ff3f7a4
GS
207#endif
208
209#if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
c955f117 210 /* AIX */
5ff3f7a4
GS
211# define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
212# define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
213# define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
214#endif
215
327c3667
GS
216#if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
217 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
218 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
5ff3f7a4 219/* The Hard Way. */
327c3667 220STATIC int
7f4774ae 221S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
ba106d47 222{
5ff3f7a4
GS
223 Uid_t ruid = getuid();
224 Uid_t euid = geteuid();
225 Gid_t rgid = getgid();
226 Gid_t egid = getegid();
227 int res;
228
146174a9 229 LOCK_CRED_MUTEX;
5ff3f7a4 230#if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
cea2e8a9 231 Perl_croak(aTHX_ "switching effective uid is not implemented");
5ff3f7a4
GS
232#else
233#ifdef HAS_SETREUID
234 if (setreuid(euid, ruid))
235#else
236#ifdef HAS_SETRESUID
237 if (setresuid(euid, ruid, (Uid_t)-1))
238#endif
239#endif
cea2e8a9 240 Perl_croak(aTHX_ "entering effective uid failed");
5ff3f7a4
GS
241#endif
242
243#if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
cea2e8a9 244 Perl_croak(aTHX_ "switching effective gid is not implemented");
5ff3f7a4
GS
245#else
246#ifdef HAS_SETREGID
247 if (setregid(egid, rgid))
248#else
249#ifdef HAS_SETRESGID
250 if (setresgid(egid, rgid, (Gid_t)-1))
251#endif
252#endif
cea2e8a9 253 Perl_croak(aTHX_ "entering effective gid failed");
5ff3f7a4
GS
254#endif
255
256 res = access(path, mode);
257
258#ifdef HAS_SETREUID
259 if (setreuid(ruid, euid))
260#else
261#ifdef HAS_SETRESUID
262 if (setresuid(ruid, euid, (Uid_t)-1))
263#endif
264#endif
cea2e8a9 265 Perl_croak(aTHX_ "leaving effective uid failed");
5ff3f7a4
GS
266
267#ifdef HAS_SETREGID
268 if (setregid(rgid, egid))
269#else
270#ifdef HAS_SETRESGID
271 if (setresgid(rgid, egid, (Gid_t)-1))
272#endif
273#endif
cea2e8a9 274 Perl_croak(aTHX_ "leaving effective gid failed");
146174a9 275 UNLOCK_CRED_MUTEX;
5ff3f7a4
GS
276
277 return res;
278}
279# define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
280# define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
281# define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
282#endif
283
284#if !defined(PERL_EFF_ACCESS_R_OK)
76ffd3b9
IZ
285/* With it or without it: anyway you get a warning: either that
286 it is unused, or it is declared static and never defined.
287 */
327c3667 288STATIC int
7f4774ae 289S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
ba106d47 290{
cea2e8a9 291 Perl_croak(aTHX_ "switching effective uid is not implemented");
5ff3f7a4
GS
292 /*NOTREACHED*/
293 return -1;
294}
295#endif
296
a0d0e21e
LW
297PP(pp_backtick)
298{
39644a26 299 dSP; dTARGET;
760ac839 300 PerlIO *fp;
2d8e6c8d
GS
301 STRLEN n_a;
302 char *tmps = POPpx;
54310121 303 I32 gimme = GIMME_V;
16fe6d59 304 char *mode = "r";
54310121 305
a0d0e21e 306 TAINT_PROPER("``");
16fe6d59
GS
307 if (PL_op->op_private & OPpOPEN_IN_RAW)
308 mode = "rb";
309 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
310 mode = "rt";
311 fp = PerlProc_popen(tmps, mode);
a0d0e21e 312 if (fp) {
ac27b0f5
NIS
313 char *type = NULL;
314 if (PL_curcop->cop_io) {
315 type = SvPV_nolen(PL_curcop->cop_io);
316 }
ac27b0f5
NIS
317 if (type && *type)
318 PerlIO_apply_layers(aTHX_ fp,mode,type);
319
54310121 320 if (gimme == G_VOID) {
96827780
MB
321 char tmpbuf[256];
322 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
54310121
PP
323 /*SUPPRESS 530*/
324 ;
325 }
326 else if (gimme == G_SCALAR) {
fa326138
RG
327 SV *oldrs = PL_rs;
328 PL_rs = &PL_sv_undef;
aa689395 329 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
a0d0e21e
LW
330 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
331 /*SUPPRESS 530*/
332 ;
fa326138 333 PL_rs = oldrs;
a0d0e21e 334 XPUSHs(TARG);
aa689395 335 SvTAINTED_on(TARG);
a0d0e21e
LW
336 }
337 else {
338 SV *sv;
339
340 for (;;) {
8d6dde3e 341 sv = NEWSV(56, 79);
a0d0e21e
LW
342 if (sv_gets(sv, fp, 0) == Nullch) {
343 SvREFCNT_dec(sv);
344 break;
345 }
346 XPUSHs(sv_2mortal(sv));
347 if (SvLEN(sv) - SvCUR(sv) > 20) {
348 SvLEN_set(sv, SvCUR(sv)+1);
349 Renew(SvPVX(sv), SvLEN(sv), char);
350 }
aa689395 351 SvTAINTED_on(sv);
a0d0e21e
LW
352 }
353 }
6ad3d225 354 STATUS_NATIVE_SET(PerlProc_pclose(fp));
aa689395 355 TAINT; /* "I believe that this is not gratuitous!" */
a0d0e21e
LW
356 }
357 else {
f86702cc 358 STATUS_NATIVE_SET(-1);
54310121 359 if (gimme == G_SCALAR)
a0d0e21e
LW
360 RETPUSHUNDEF;
361 }
362
363 RETURN;
364}
365
366PP(pp_glob)
367{
368 OP *result;
f5284f61
IZ
369 tryAMAGICunTARGET(iter, -1);
370
71686f12
GS
371 /* Note that we only ever get here if File::Glob fails to load
372 * without at the same time croaking, for some reason, or if
373 * perl was built with PERL_EXTERNAL_GLOB */
374
a0d0e21e 375 ENTER;
a0d0e21e 376
c90c0ff4 377#ifndef VMS
3280af22 378 if (PL_tainting) {
7bac28a0
PP
379 /*
380 * The external globbing program may use things we can't control,
381 * so for security reasons we must assume the worst.
382 */
383 TAINT;
22c35a8c 384 taint_proper(PL_no_security, "glob");
7bac28a0 385 }
c90c0ff4 386#endif /* !VMS */
7bac28a0 387
3280af22
NIS
388 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
389 PL_last_in_gv = (GV*)*PL_stack_sp--;
a0d0e21e 390
3280af22 391 SAVESPTR(PL_rs); /* This is not permanent, either. */
79cb57f6 392 PL_rs = sv_2mortal(newSVpvn("\000", 1));
c07a80fd
PP
393#ifndef DOSISH
394#ifndef CSH
6b88bc9c 395 *SvPVX(PL_rs) = '\n';
a0d0e21e 396#endif /* !CSH */
55497cff 397#endif /* !DOSISH */
c07a80fd 398
a0d0e21e
LW
399 result = do_readline();
400 LEAVE;
401 return result;
402}
403
a0d0e21e
LW
404PP(pp_rcatline)
405{
146174a9 406 PL_last_in_gv = cGVOP_gv;
a0d0e21e
LW
407 return do_readline();
408}
409
410PP(pp_warn)
411{
39644a26 412 dSP; dMARK;
06bf62c7 413 SV *tmpsv;
a0d0e21e 414 char *tmps;
06bf62c7 415 STRLEN len;
a0d0e21e
LW
416 if (SP - MARK != 1) {
417 dTARGET;
3280af22 418 do_join(TARG, &PL_sv_no, MARK, SP);
06bf62c7 419 tmpsv = TARG;
a0d0e21e
LW
420 SP = MARK + 1;
421 }
422 else {
06bf62c7 423 tmpsv = TOPs;
a0d0e21e 424 }
06bf62c7 425 tmps = SvPV(tmpsv, len);
7b102d90 426 if ((!tmps || !len) && PL_errgv) {
4e6ea2c3
GS
427 SV *error = ERRSV;
428 (void)SvUPGRADE(error, SVt_PV);
429 if (SvPOK(error) && SvCUR(error))
430 sv_catpv(error, "\t...caught");
06bf62c7
GS
431 tmpsv = error;
432 tmps = SvPV(tmpsv, len);
a0d0e21e 433 }
06bf62c7
GS
434 if (!tmps || !len)
435 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
436
cb50131a 437 Perl_warn(aTHX_ "%"SVf, tmpsv);
a0d0e21e
LW
438 RETSETYES;
439}
440
441PP(pp_die)
442{
39644a26 443 dSP; dMARK;
a0d0e21e 444 char *tmps;
06bf62c7
GS
445 SV *tmpsv;
446 STRLEN len;
447 bool multiarg = 0;
96e176bf
CL
448#ifdef VMS
449 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
450#endif
a0d0e21e
LW
451 if (SP - MARK != 1) {
452 dTARGET;
3280af22 453 do_join(TARG, &PL_sv_no, MARK, SP);
06bf62c7
GS
454 tmpsv = TARG;
455 tmps = SvPV(tmpsv, len);
456 multiarg = 1;
a0d0e21e
LW
457 SP = MARK + 1;
458 }
459 else {
4e6ea2c3 460 tmpsv = TOPs;
bf12611a 461 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
a0d0e21e 462 }
06bf62c7 463 if (!tmps || !len) {
4e6ea2c3
GS
464 SV *error = ERRSV;
465 (void)SvUPGRADE(error, SVt_PV);
06bf62c7
GS
466 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
467 if (!multiarg)
4e6ea2c3 468 SvSetSV(error,tmpsv);
06bf62c7 469 else if (sv_isobject(error)) {
05423cc9
GS
470 HV *stash = SvSTASH(SvRV(error));
471 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
472 if (gv) {
146174a9 473 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
b448e4fe 474 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
05423cc9
GS
475 EXTEND(SP, 3);
476 PUSHMARK(SP);
477 PUSHs(error);
478 PUSHs(file);
479 PUSHs(line);
480 PUTBACK;
864dbfa3
GS
481 call_sv((SV*)GvCV(gv),
482 G_SCALAR|G_EVAL|G_KEEPERR);
3280af22 483 sv_setsv(error,*PL_stack_sp--);
05423cc9
GS
484 }
485 }
b3fe4827 486 DIE(aTHX_ Nullformat);
4e6ea2c3
GS
487 }
488 else {
489 if (SvPOK(error) && SvCUR(error))
490 sv_catpv(error, "\t...propagated");
06bf62c7
GS
491 tmpsv = error;
492 tmps = SvPV(tmpsv, len);
4e6ea2c3 493 }
a0d0e21e 494 }
06bf62c7
GS
495 if (!tmps || !len)
496 tmpsv = sv_2mortal(newSVpvn("Died", 4));
497
cb50131a 498 DIE(aTHX_ "%"SVf, tmpsv);
a0d0e21e
LW
499}
500
501/* I/O. */
502
503PP(pp_open)
504{
39644a26 505 dSP;
a567e93b
NIS
506 dMARK; dORIGMARK;
507 dTARGET;
a0d0e21e
LW
508 GV *gv;
509 SV *sv;
5b468f54 510 IO *io;
a0d0e21e
LW
511 char *tmps;
512 STRLEN len;
4592e6ca 513 MAGIC *mg;
a567e93b 514 bool ok;
a0d0e21e 515
a567e93b 516 gv = (GV *)*++MARK;
5f05dabc 517 if (!isGV(gv))
cea2e8a9 518 DIE(aTHX_ PL_no_usym, "filehandle");
5b468f54 519 if ((io = GvIOp(gv)))
36477c24 520 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
853846ea 521
5b468f54 522 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
a567e93b
NIS
523 /* Method's args are same as ours ... */
524 /* ... except handle is replaced by the object */
5b468f54 525 *MARK-- = SvTIED_obj((SV*)io, mg);
a567e93b 526 PUSHMARK(MARK);
4592e6ca
NIS
527 PUTBACK;
528 ENTER;
864dbfa3 529 call_method("OPEN", G_SCALAR);
4592e6ca
NIS
530 LEAVE;
531 SPAGAIN;
532 RETURN;
533 }
534
a567e93b
NIS
535 if (MARK < SP) {
536 sv = *++MARK;
537 }
538 else {
539 sv = GvSV(gv);
540 }
541
a0d0e21e 542 tmps = SvPV(sv, len);
a567e93b
NIS
543 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
544 SP = ORIGMARK;
545 if (ok)
3280af22
NIS
546 PUSHi( (I32)PL_forkprocess );
547 else if (PL_forkprocess == 0) /* we are a new child */
a0d0e21e
LW
548 PUSHi(0);
549 else
550 RETPUSHUNDEF;
551 RETURN;
552}
553
554PP(pp_close)
555{
39644a26 556 dSP;
a0d0e21e 557 GV *gv;
5b468f54 558 IO *io;
1d603a67 559 MAGIC *mg;
a0d0e21e
LW
560
561 if (MAXARG == 0)
3280af22 562 gv = PL_defoutgv;
a0d0e21e
LW
563 else
564 gv = (GV*)POPs;
1d603a67 565
5b468f54
AMS
566 if (gv && (io = GvIO(gv))
567 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
568 {
1d603a67 569 PUSHMARK(SP);
5b468f54 570 XPUSHs(SvTIED_obj((SV*)io, mg));
1d603a67
GB
571 PUTBACK;
572 ENTER;
864dbfa3 573 call_method("CLOSE", G_SCALAR);
1d603a67
GB
574 LEAVE;
575 SPAGAIN;
576 RETURN;
577 }
a0d0e21e 578 EXTEND(SP, 1);
54310121 579 PUSHs(boolSV(do_close(gv, TRUE)));
a0d0e21e
LW
580 RETURN;
581}
582
583PP(pp_pipe_op)
584{
a0d0e21e 585#ifdef HAS_PIPE
9cad6237 586 dSP;
a0d0e21e
LW
587 GV *rgv;
588 GV *wgv;
589 register IO *rstio;
590 register IO *wstio;
591 int fd[2];
592
593 wgv = (GV*)POPs;
594 rgv = (GV*)POPs;
595
596 if (!rgv || !wgv)
597 goto badexit;
598
4633a7c4 599 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
cea2e8a9 600 DIE(aTHX_ PL_no_usym, "filehandle");
a0d0e21e
LW
601 rstio = GvIOn(rgv);
602 wstio = GvIOn(wgv);
603
604 if (IoIFP(rstio))
605 do_close(rgv, FALSE);
606 if (IoIFP(wstio))
607 do_close(wgv, FALSE);
608
6ad3d225 609 if (PerlProc_pipe(fd) < 0)
a0d0e21e
LW
610 goto badexit;
611
479b2847
GS
612 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
613 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
b5ac89c3 614 IoOFP(rstio) = IoIFP(rstio);
a0d0e21e 615 IoIFP(wstio) = IoOFP(wstio);
50952442
JH
616 IoTYPE(rstio) = IoTYPE_RDONLY;
617 IoTYPE(wstio) = IoTYPE_WRONLY;
a0d0e21e
LW
618
619 if (!IoIFP(rstio) || !IoOFP(wstio)) {
760ac839 620 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
6ad3d225 621 else PerlLIO_close(fd[0]);
760ac839 622 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
6ad3d225 623 else PerlLIO_close(fd[1]);
a0d0e21e
LW
624 goto badexit;
625 }
4771b018
GS
626#if defined(HAS_FCNTL) && defined(F_SETFD)
627 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
628 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
629#endif
a0d0e21e
LW
630 RETPUSHYES;
631
632badexit:
633 RETPUSHUNDEF;
634#else
cea2e8a9 635 DIE(aTHX_ PL_no_func, "pipe");
a0d0e21e
LW
636#endif
637}
638
639PP(pp_fileno)
640{
39644a26 641 dSP; dTARGET;
a0d0e21e
LW
642 GV *gv;
643 IO *io;
760ac839 644 PerlIO *fp;
4592e6ca
NIS
645 MAGIC *mg;
646
a0d0e21e
LW
647 if (MAXARG < 1)
648 RETPUSHUNDEF;
649 gv = (GV*)POPs;
4592e6ca 650
5b468f54
AMS
651 if (gv && (io = GvIO(gv))
652 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
653 {
4592e6ca 654 PUSHMARK(SP);
5b468f54 655 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
656 PUTBACK;
657 ENTER;
864dbfa3 658 call_method("FILENO", G_SCALAR);
4592e6ca
NIS
659 LEAVE;
660 SPAGAIN;
661 RETURN;
662 }
663
c289d2f7
JH
664 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
665 /* Can't do this because people seem to do things like
666 defined(fileno($foo)) to check whether $foo is a valid fh.
667 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
668 report_evil_fh(gv, io, PL_op->op_type);
669 */
a0d0e21e 670 RETPUSHUNDEF;
c289d2f7
JH
671 }
672
760ac839 673 PUSHi(PerlIO_fileno(fp));
a0d0e21e
LW
674 RETURN;
675}
676
677PP(pp_umask)
678{
39644a26 679 dSP; dTARGET;
d7e492a4 680#ifdef HAS_UMASK
761237fe 681 Mode_t anum;
a0d0e21e 682
a0d0e21e 683 if (MAXARG < 1) {
6ad3d225
GS
684 anum = PerlLIO_umask(0);
685 (void)PerlLIO_umask(anum);
a0d0e21e
LW
686 }
687 else
6ad3d225 688 anum = PerlLIO_umask(POPi);
a0d0e21e
LW
689 TAINT_PROPER("umask");
690 XPUSHi(anum);
691#else
eec2d3df
GS
692 /* Only DIE if trying to restrict permissions on `user' (self).
693 * Otherwise it's harmless and more useful to just return undef
694 * since 'group' and 'other' concepts probably don't exist here. */
695 if (MAXARG >= 1 && (POPi & 0700))
cea2e8a9 696 DIE(aTHX_ "umask not implemented");
6b88bc9c 697 XPUSHs(&PL_sv_undef);
a0d0e21e
LW
698#endif
699 RETURN;
700}
701
702PP(pp_binmode)
703{
39644a26 704 dSP;
a0d0e21e
LW
705 GV *gv;
706 IO *io;
760ac839 707 PerlIO *fp;
4592e6ca 708 MAGIC *mg;
16fe6d59 709 SV *discp = Nullsv;
a0d0e21e
LW
710
711 if (MAXARG < 1)
712 RETPUSHUNDEF;
60382766 713 if (MAXARG > 1) {
16fe6d59 714 discp = POPs;
60382766 715 }
a0d0e21e 716
301e8125 717 gv = (GV*)POPs;
4592e6ca 718
5b468f54
AMS
719 if (gv && (io = GvIO(gv))
720 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
721 {
4592e6ca 722 PUSHMARK(SP);
5b468f54 723 XPUSHs(SvTIED_obj((SV*)io, mg));
16fe6d59
GS
724 if (discp)
725 XPUSHs(discp);
4592e6ca
NIS
726 PUTBACK;
727 ENTER;
864dbfa3 728 call_method("BINMODE", G_SCALAR);
4592e6ca
NIS
729 LEAVE;
730 SPAGAIN;
731 RETURN;
732 }
a0d0e21e
LW
733
734 EXTEND(SP, 1);
50f846a7 735 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
c289d2f7
JH
736 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
737 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 738 SETERRNO(EBADF,RMS_IFI);
50f846a7
SC
739 RETPUSHUNDEF;
740 }
a0d0e21e 741
40d98b49 742 PUTBACK;
60382766 743 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
40d98b49
JH
744 (discp) ? SvPV_nolen(discp) : Nullch)) {
745 SPAGAIN;
a0d0e21e 746 RETPUSHYES;
40d98b49
JH
747 }
748 else {
749 SPAGAIN;
a0d0e21e 750 RETPUSHUNDEF;
40d98b49 751 }
a0d0e21e
LW
752}
753
754PP(pp_tie)
755{
39644a26 756 dSP;
e336de0d 757 dMARK;
a0d0e21e
LW
758 SV *varsv;
759 HV* stash;
760 GV *gv;
a0d0e21e 761 SV *sv;
3280af22 762 I32 markoff = MARK - PL_stack_base;
a0d0e21e 763 char *methname;
14befaf4 764 int how = PERL_MAGIC_tied;
e336de0d 765 U32 items;
a0d0e21e 766
e336de0d 767 varsv = *++MARK;
6b05c17a
NIS
768 switch(SvTYPE(varsv)) {
769 case SVt_PVHV:
770 methname = "TIEHASH";
03c6e78a 771 HvEITER((HV *)varsv) = Null(HE *);
6b05c17a
NIS
772 break;
773 case SVt_PVAV:
774 methname = "TIEARRAY";
775 break;
776 case SVt_PVGV:
7fb37951
AMS
777#ifdef GV_UNIQUE_CHECK
778 if (GvUNIQUE((GV*)varsv)) {
779 Perl_croak(aTHX_ "Attempt to tie unique GV");
5bd07a3d
DM
780 }
781#endif
6b05c17a 782 methname = "TIEHANDLE";
14befaf4 783 how = PERL_MAGIC_tiedscalar;
5b468f54
AMS
784 /* For tied filehandles, we apply tiedscalar magic to the IO
785 slot of the GP rather than the GV itself. AMS 20010812 */
786 if (!GvIOp(varsv))
787 GvIOp(varsv) = newIO();
788 varsv = (SV *)GvIOp(varsv);
6b05c17a
NIS
789 break;
790 default:
791 methname = "TIESCALAR";
14befaf4 792 how = PERL_MAGIC_tiedscalar;
6b05c17a
NIS
793 break;
794 }
e336de0d
GS
795 items = SP - MARK++;
796 if (sv_isobject(*MARK)) {
6b05c17a 797 ENTER;
e788e7d3 798 PUSHSTACKi(PERLSI_MAGIC);
e336de0d 799 PUSHMARK(SP);
eb160463 800 EXTEND(SP,(I32)items);
e336de0d
GS
801 while (items--)
802 PUSHs(*MARK++);
803 PUTBACK;
864dbfa3 804 call_method(methname, G_SCALAR);
301e8125 805 }
6b05c17a 806 else {
864dbfa3 807 /* Not clear why we don't call call_method here too.
6b05c17a
NIS
808 * perhaps to get different error message ?
809 */
e336de0d 810 stash = gv_stashsv(*MARK, FALSE);
6b05c17a 811 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
35c1215d
NC
812 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
813 methname, *MARK);
6b05c17a
NIS
814 }
815 ENTER;
e788e7d3 816 PUSHSTACKi(PERLSI_MAGIC);
e336de0d 817 PUSHMARK(SP);
eb160463 818 EXTEND(SP,(I32)items);
e336de0d
GS
819 while (items--)
820 PUSHs(*MARK++);
821 PUTBACK;
864dbfa3 822 call_sv((SV*)GvCV(gv), G_SCALAR);
6b05c17a 823 }
a0d0e21e
LW
824 SPAGAIN;
825
826 sv = TOPs;
d3acc0f7 827 POPSTACK;
a0d0e21e 828 if (sv_isobject(sv)) {
33c27489 829 sv_unmagic(varsv, how);
ae21d580 830 /* Croak if a self-tie on an aggregate is attempted. */
b881518d 831 if (varsv == SvRV(sv) &&
d87ebaca
YST
832 (SvTYPE(varsv) == SVt_PVAV ||
833 SvTYPE(varsv) == SVt_PVHV))
ae21d580
JH
834 Perl_croak(aTHX_
835 "Self-ties of arrays and hashes are not supported");
d87ebaca 836 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
a0d0e21e
LW
837 }
838 LEAVE;
3280af22 839 SP = PL_stack_base + markoff;
a0d0e21e
LW
840 PUSHs(sv);
841 RETURN;
842}
843
844PP(pp_untie)
845{
39644a26 846 dSP;
5b468f54 847 MAGIC *mg;
33c27489 848 SV *sv = POPs;
14befaf4
DM
849 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
850 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
55497cff 851
5b468f54
AMS
852 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
853 RETPUSHYES;
854
855 if ((mg = SvTIED_mg(sv, how))) {
a29a5827
NIS
856 SV *obj = SvRV(mg->mg_obj);
857 GV *gv;
858 CV *cv = NULL;
fa2b88e0
JS
859 if (obj) {
860 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
861 isGV(gv) && (cv = GvCV(gv))) {
862 PUSHMARK(SP);
863 XPUSHs(SvTIED_obj((SV*)gv, mg));
864 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
865 PUTBACK;
866 ENTER;
867 call_sv((SV *)cv, G_VOID);
868 LEAVE;
869 SPAGAIN;
870 }
871 else if (ckWARN(WARN_UNTIE)) {
872 if (mg && SvREFCNT(obj) > 1)
9014280d 873 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
fa2b88e0
JS
874 "untie attempted while %"UVuf" inner references still exist",
875 (UV)SvREFCNT(obj) - 1 ) ;
876 }
cbdc8872 877 }
fa2b88e0 878 sv_unmagic(sv, how) ;
cbdc8872 879 }
55497cff 880 RETPUSHYES;
a0d0e21e
LW
881}
882
c07a80fd
PP
883PP(pp_tied)
884{
39644a26 885 dSP;
5b468f54 886 MAGIC *mg;
33c27489 887 SV *sv = POPs;
14befaf4
DM
888 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
889 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
5b468f54
AMS
890
891 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
892 RETPUSHUNDEF;
c07a80fd 893
155aba94 894 if ((mg = SvTIED_mg(sv, how))) {
33c27489
GS
895 SV *osv = SvTIED_obj(sv, mg);
896 if (osv == mg->mg_obj)
897 osv = sv_mortalcopy(osv);
898 PUSHs(osv);
899 RETURN;
c07a80fd 900 }
c07a80fd
PP
901 RETPUSHUNDEF;
902}
903
a0d0e21e
LW
904PP(pp_dbmopen)
905{
39644a26 906 dSP;
a0d0e21e
LW
907 HV *hv;
908 dPOPPOPssrl;
909 HV* stash;
910 GV *gv;
a0d0e21e
LW
911 SV *sv;
912
913 hv = (HV*)POPs;
914
3280af22 915 sv = sv_mortalcopy(&PL_sv_no);
a0d0e21e
LW
916 sv_setpv(sv, "AnyDBM_File");
917 stash = gv_stashsv(sv, FALSE);
8ebc5c01 918 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
a0d0e21e 919 PUTBACK;
864dbfa3 920 require_pv("AnyDBM_File.pm");
a0d0e21e 921 SPAGAIN;
8ebc5c01 922 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
cea2e8a9 923 DIE(aTHX_ "No dbm on this machine");
a0d0e21e
LW
924 }
925
57d3b86d 926 ENTER;
924508f0 927 PUSHMARK(SP);
6b05c17a 928
924508f0 929 EXTEND(SP, 5);
a0d0e21e
LW
930 PUSHs(sv);
931 PUSHs(left);
932 if (SvIV(right))
b448e4fe 933 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
a0d0e21e 934 else
b448e4fe 935 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
a0d0e21e 936 PUSHs(right);
57d3b86d 937 PUTBACK;
864dbfa3 938 call_sv((SV*)GvCV(gv), G_SCALAR);
a0d0e21e
LW
939 SPAGAIN;
940
941 if (!sv_isobject(TOPs)) {
924508f0
GS
942 SP--;
943 PUSHMARK(SP);
a0d0e21e
LW
944 PUSHs(sv);
945 PUSHs(left);
b448e4fe 946 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
a0d0e21e 947 PUSHs(right);
a0d0e21e 948 PUTBACK;
864dbfa3 949 call_sv((SV*)GvCV(gv), G_SCALAR);
a0d0e21e
LW
950 SPAGAIN;
951 }
952
6b05c17a 953 if (sv_isobject(TOPs)) {
14befaf4
DM
954 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
955 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
6b05c17a 956 }
a0d0e21e
LW
957 LEAVE;
958 RETURN;
959}
960
961PP(pp_dbmclose)
962{
cea2e8a9 963 return pp_untie();
a0d0e21e
LW
964}
965
966PP(pp_sselect)
967{
a0d0e21e 968#ifdef HAS_SELECT
9cad6237 969 dSP; dTARGET;
a0d0e21e
LW
970 register I32 i;
971 register I32 j;
972 register char *s;
973 register SV *sv;
65202027 974 NV value;
a0d0e21e
LW
975 I32 maxlen = 0;
976 I32 nfound;
977 struct timeval timebuf;
978 struct timeval *tbuf = &timebuf;
979 I32 growsize;
980 char *fd_sets[4];
2d8e6c8d 981 STRLEN n_a;
a0d0e21e
LW
982#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
983 I32 masksize;
984 I32 offset;
985 I32 k;
986
987# if BYTEORDER & 0xf0000
988# define ORDERBYTE (0x88888888 - BYTEORDER)
989# else
990# define ORDERBYTE (0x4444 - BYTEORDER)
991# endif
992
993#endif
994
995 SP -= 4;
996 for (i = 1; i <= 3; i++) {
997 if (!SvPOK(SP[i]))
998 continue;
999 j = SvCUR(SP[i]);
1000 if (maxlen < j)
1001 maxlen = j;
1002 }
1003
5ff3f7a4 1004/* little endians can use vecs directly */
e366b469 1005#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
5ff3f7a4 1006# ifdef NFDBITS
a0d0e21e 1007
5ff3f7a4
GS
1008# ifndef NBBY
1009# define NBBY 8
1010# endif
a0d0e21e
LW
1011
1012 masksize = NFDBITS / NBBY;
5ff3f7a4 1013# else
a0d0e21e 1014 masksize = sizeof(long); /* documented int, everyone seems to use long */
5ff3f7a4 1015# endif
a0d0e21e
LW
1016 Zero(&fd_sets[0], 4, char*);
1017#endif
1018
e366b469
PG
1019# if SELECT_MIN_BITS > 1
1020 /* If SELECT_MIN_BITS is greater than one we most probably will want
1021 * to align the sizes with SELECT_MIN_BITS/8 because for example
1022 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1023 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1024 * on (sets/tests/clears bits) is 32 bits. */
1025 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1026# else
1027 growsize = sizeof(fd_set);
1028# endif
1029
a0d0e21e
LW
1030 sv = SP[4];
1031 if (SvOK(sv)) {
1032 value = SvNV(sv);
1033 if (value < 0.0)
1034 value = 0.0;
1035 timebuf.tv_sec = (long)value;
65202027 1036 value -= (NV)timebuf.tv_sec;
a0d0e21e
LW
1037 timebuf.tv_usec = (long)(value * 1000000.0);
1038 }
1039 else
1040 tbuf = Null(struct timeval*);
1041
1042 for (i = 1; i <= 3; i++) {
1043 sv = SP[i];
1044 if (!SvOK(sv)) {
1045 fd_sets[i] = 0;
1046 continue;
1047 }
1048 else if (!SvPOK(sv))
2d8e6c8d 1049 SvPV_force(sv,n_a); /* force string conversion */
a0d0e21e
LW
1050 j = SvLEN(sv);
1051 if (j < growsize) {
1052 Sv_Grow(sv, growsize);
a0d0e21e 1053 }
c07a80fd
PP
1054 j = SvCUR(sv);
1055 s = SvPVX(sv) + j;
1056 while (++j <= growsize) {
1057 *s++ = '\0';
1058 }
1059
a0d0e21e
LW
1060#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1061 s = SvPVX(sv);
1062 New(403, fd_sets[i], growsize, char);
1063 for (offset = 0; offset < growsize; offset += masksize) {
1064 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1065 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1066 }
1067#else
1068 fd_sets[i] = SvPVX(sv);
1069#endif
1070 }
1071
dc4c69d9
JH
1072#ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1073 /* Can't make just the (void*) conditional because that would be
1074 * cpp #if within cpp macro, and not all compilers like that. */
1075 nfound = PerlSock_select(
1076 maxlen * 8,
1077 (Select_fd_set_t) fd_sets[1],
1078 (Select_fd_set_t) fd_sets[2],
1079 (Select_fd_set_t) fd_sets[3],
1080 (void*) tbuf); /* Workaround for compiler bug. */
1081#else
6ad3d225 1082 nfound = PerlSock_select(
a0d0e21e
LW
1083 maxlen * 8,
1084 (Select_fd_set_t) fd_sets[1],
1085 (Select_fd_set_t) fd_sets[2],
1086 (Select_fd_set_t) fd_sets[3],
1087 tbuf);
dc4c69d9 1088#endif
a0d0e21e
LW
1089 for (i = 1; i <= 3; i++) {
1090 if (fd_sets[i]) {
1091 sv = SP[i];
1092#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1093 s = SvPVX(sv);
1094 for (offset = 0; offset < growsize; offset += masksize) {
1095 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1096 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1097 }
1098 Safefree(fd_sets[i]);
1099#endif
1100 SvSETMAGIC(sv);
1101 }
1102 }
1103
1104 PUSHi(nfound);
1105 if (GIMME == G_ARRAY && tbuf) {
65202027
DS
1106 value = (NV)(timebuf.tv_sec) +
1107 (NV)(timebuf.tv_usec) / 1000000.0;
3280af22 1108 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
a0d0e21e
LW
1109 sv_setnv(sv, value);
1110 }
1111 RETURN;
1112#else
cea2e8a9 1113 DIE(aTHX_ "select not implemented");
a0d0e21e
LW
1114#endif
1115}
1116
4633a7c4 1117void
864dbfa3 1118Perl_setdefout(pTHX_ GV *gv)
4633a7c4
LW
1119{
1120 if (gv)
1121 (void)SvREFCNT_inc(gv);
3280af22
NIS
1122 if (PL_defoutgv)
1123 SvREFCNT_dec(PL_defoutgv);
1124 PL_defoutgv = gv;
4633a7c4
LW
1125}
1126
a0d0e21e
LW
1127PP(pp_select)
1128{
39644a26 1129 dSP; dTARGET;
4633a7c4
LW
1130 GV *newdefout, *egv;
1131 HV *hv;
1132
533c011a 1133 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
4633a7c4 1134
3280af22 1135 egv = GvEGV(PL_defoutgv);
4633a7c4 1136 if (!egv)
3280af22 1137 egv = PL_defoutgv;
4633a7c4
LW
1138 hv = GvSTASH(egv);
1139 if (! hv)
3280af22 1140 XPUSHs(&PL_sv_undef);
4633a7c4 1141 else {
cbdc8872 1142 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
f86702cc 1143 if (gvp && *gvp == egv) {
f7aaccc2 1144 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
f86702cc
PP
1145 XPUSHTARG;
1146 }
1147 else {
1148 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1149 }
4633a7c4
LW
1150 }
1151
1152 if (newdefout) {
ded8aa31
GS
1153 if (!GvIO(newdefout))
1154 gv_IOadd(newdefout);
4633a7c4
LW
1155 setdefout(newdefout);
1156 }
1157
a0d0e21e
LW
1158 RETURN;
1159}
1160
1161PP(pp_getc)
1162{
39644a26 1163 dSP; dTARGET;
a0d0e21e 1164 GV *gv;
90133b69 1165 IO *io = NULL;
2ae324a7 1166 MAGIC *mg;
a0d0e21e 1167
32da55ab 1168 if (MAXARG == 0)
3280af22 1169 gv = PL_stdingv;
a0d0e21e
LW
1170 else
1171 gv = (GV*)POPs;
2ae324a7 1172
5b468f54
AMS
1173 if (gv && (io = GvIO(gv))
1174 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1175 {
54310121 1176 I32 gimme = GIMME_V;
2ae324a7 1177 PUSHMARK(SP);
5b468f54 1178 XPUSHs(SvTIED_obj((SV*)io, mg));
2ae324a7
PP
1179 PUTBACK;
1180 ENTER;
864dbfa3 1181 call_method("GETC", gimme);
2ae324a7
PP
1182 LEAVE;
1183 SPAGAIN;
54310121
PP
1184 if (gimme == G_SCALAR)
1185 SvSetMagicSV_nosteal(TARG, TOPs);
2ae324a7
PP
1186 RETURN;
1187 }
90133b69 1188 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
97e322ff
RGS
1189 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1190 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
90133b69 1191 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 1192 SETERRNO(EBADF,RMS_IFI);
a0d0e21e 1193 RETPUSHUNDEF;
90133b69 1194 }
bbce6d69 1195 TAINT;
a0d0e21e 1196 sv_setpv(TARG, " ");
9bc64814 1197 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
7d59b7e4
NIS
1198 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1199 /* Find out how many bytes the char needs */
1200 Size_t len = UTF8SKIP(SvPVX(TARG));
1201 if (len > 1) {
1202 SvGROW(TARG,len+1);
1203 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1204 SvCUR_set(TARG,1+len);
1205 }
1206 SvUTF8_on(TARG);
1207 }
a0d0e21e
LW
1208 PUSHTARG;
1209 RETURN;
1210}
1211
1212PP(pp_read)
1213{
cea2e8a9 1214 return pp_sysread();
a0d0e21e
LW
1215}
1216
76e3520e 1217STATIC OP *
cea2e8a9 1218S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
a0d0e21e 1219{
c09156bb 1220 register PERL_CONTEXT *cx;
54310121 1221 I32 gimme = GIMME_V;
a0d0e21e
LW
1222
1223 ENTER;
1224 SAVETMPS;
1225
1226 push_return(retop);
146174a9 1227 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
a0d0e21e 1228 PUSHFORMAT(cx);
dd2155a4 1229 PAD_SET_CUR(CvPADLIST(cv), 1);
a0d0e21e 1230
4633a7c4 1231 setdefout(gv); /* locally select filehandle so $% et al work */
a0d0e21e
LW
1232 return CvSTART(cv);
1233}
1234
1235PP(pp_enterwrite)
1236{
39644a26 1237 dSP;
a0d0e21e
LW
1238 register GV *gv;
1239 register IO *io;
1240 GV *fgv;
1241 CV *cv;
1242
1243 if (MAXARG == 0)
3280af22 1244 gv = PL_defoutgv;
a0d0e21e
LW
1245 else {
1246 gv = (GV*)POPs;
1247 if (!gv)
3280af22 1248 gv = PL_defoutgv;
a0d0e21e
LW
1249 }
1250 EXTEND(SP, 1);
1251 io = GvIO(gv);
1252 if (!io) {
1253 RETPUSHNO;
1254 }
1255 if (IoFMT_GV(io))
1256 fgv = IoFMT_GV(io);
1257 else
1258 fgv = gv;
1259
1260 cv = GvFORM(fgv);
a0d0e21e 1261 if (!cv) {
2dd78f96 1262 char *name = NULL;
a0d0e21e 1263 if (fgv) {
748a9306 1264 SV *tmpsv = sv_newmortal();
43693395 1265 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
2dd78f96 1266 name = SvPV_nolen(tmpsv);
a0d0e21e 1267 }
2dd78f96
JH
1268 if (name && *name)
1269 DIE(aTHX_ "Undefined format \"%s\" called", name);
cea2e8a9 1270 DIE(aTHX_ "Not a format reference");
a0d0e21e 1271 }
44a8e56a
PP
1272 if (CvCLONE(cv))
1273 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
a0d0e21e 1274
44a8e56a 1275 IoFLAGS(io) &= ~IOf_DIDTOP;
533c011a 1276 return doform(cv,gv,PL_op->op_next);
a0d0e21e
LW
1277}
1278
1279PP(pp_leavewrite)
1280{
39644a26 1281 dSP;
a0d0e21e
LW
1282 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1283 register IO *io = GvIOp(gv);
760ac839
LW
1284 PerlIO *ofp = IoOFP(io);
1285 PerlIO *fp;
a0d0e21e
LW
1286 SV **newsp;
1287 I32 gimme;
c09156bb 1288 register PERL_CONTEXT *cx;
a0d0e21e 1289
760ac839 1290 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
3280af22 1291 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
7ef822cd
JH
1292 if (!io || !ofp)
1293 goto forget_top;
3280af22
NIS
1294 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1295 PL_formtarget != PL_toptarget)
a0d0e21e 1296 {
4633a7c4
LW
1297 GV *fgv;
1298 CV *cv;
a0d0e21e
LW
1299 if (!IoTOP_GV(io)) {
1300 GV *topgv;
46fc3d4c 1301 SV *topname;
a0d0e21e
LW
1302
1303 if (!IoTOP_NAME(io)) {
1304 if (!IoFMT_NAME(io))
1305 IoFMT_NAME(io) = savepv(GvNAME(gv));
cea2e8a9 1306 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
46fc3d4c 1307 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
748a9306 1308 if ((topgv && GvFORM(topgv)) ||
a0d0e21e 1309 !gv_fetchpv("top",FALSE,SVt_PVFM))
46fc3d4c 1310 IoTOP_NAME(io) = savepv(SvPVX(topname));
a0d0e21e
LW
1311 else
1312 IoTOP_NAME(io) = savepv("top");
1313 }
1314 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1315 if (!topgv || !GvFORM(topgv)) {
1316 IoLINES_LEFT(io) = 100000000;
1317 goto forget_top;
1318 }
1319 IoTOP_GV(io) = topgv;
1320 }
748a9306
LW
1321 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1322 I32 lines = IoLINES_LEFT(io);
3280af22 1323 char *s = SvPVX(PL_formtarget);
8e07c86e
AD
1324 if (lines <= 0) /* Yow, header didn't even fit!!! */
1325 goto forget_top;
748a9306
LW
1326 while (lines-- > 0) {
1327 s = strchr(s, '\n');
1328 if (!s)
1329 break;
1330 s++;
1331 }
1332 if (s) {
d75029d0
NIS
1333 STRLEN save = SvCUR(PL_formtarget);
1334 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1335 do_print(PL_formtarget, ofp);
1336 SvCUR_set(PL_formtarget, save);
3280af22
NIS
1337 sv_chop(PL_formtarget, s);
1338 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
748a9306
LW
1339 }
1340 }
a0d0e21e 1341 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
d75029d0 1342 do_print(PL_formfeed, ofp);
a0d0e21e
LW
1343 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1344 IoPAGE(io)++;
3280af22 1345 PL_formtarget = PL_toptarget;
748a9306 1346 IoFLAGS(io) |= IOf_DIDTOP;
4633a7c4
LW
1347 fgv = IoTOP_GV(io);
1348 if (!fgv)
cea2e8a9 1349 DIE(aTHX_ "bad top format reference");
4633a7c4 1350 cv = GvFORM(fgv);
2dd78f96
JH
1351 {
1352 char *name = NULL;
1353 if (!cv) {
1354 SV *sv = sv_newmortal();
1355 gv_efullname4(sv, fgv, Nullch, FALSE);
1356 name = SvPV_nolen(sv);
1357 }
1358 if (name && *name)
1359 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1360 /* why no:
1361 else
1362 DIE(aTHX_ "Undefined top format called");
1363 ?*/
4633a7c4 1364 }
44a8e56a
PP
1365 if (CvCLONE(cv))
1366 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
533c011a 1367 return doform(cv,gv,PL_op);
a0d0e21e
LW
1368 }
1369
1370 forget_top:
3280af22 1371 POPBLOCK(cx,PL_curpm);
a0d0e21e
LW
1372 POPFORMAT(cx);
1373 LEAVE;
1374
1375 fp = IoOFP(io);
1376 if (!fp) {
599cee73 1377 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1378 if (IoIFP(io))
1379 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1380 else if (ckWARN(WARN_CLOSED))
bc37a18f 1381 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1382 }
3280af22 1383 PUSHs(&PL_sv_no);
a0d0e21e
LW
1384 }
1385 else {
3280af22 1386 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
599cee73 1387 if (ckWARN(WARN_IO))
9014280d 1388 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
a0d0e21e 1389 }
d75029d0 1390 if (!do_print(PL_formtarget, fp))
3280af22 1391 PUSHs(&PL_sv_no);
a0d0e21e 1392 else {
3280af22
NIS
1393 FmLINES(PL_formtarget) = 0;
1394 SvCUR_set(PL_formtarget, 0);
1395 *SvEND(PL_formtarget) = '\0';
a0d0e21e 1396 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1397 (void)PerlIO_flush(fp);
3280af22 1398 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1399 }
1400 }
9cbac4c7 1401 /* bad_ofp: */
3280af22 1402 PL_formtarget = PL_bodytarget;
a0d0e21e
LW
1403 PUTBACK;
1404 return pop_return();
1405}
1406
1407PP(pp_prtf)
1408{
39644a26 1409 dSP; dMARK; dORIGMARK;
a0d0e21e
LW
1410 GV *gv;
1411 IO *io;
760ac839 1412 PerlIO *fp;
26db47c4 1413 SV *sv;
46fc3d4c 1414 MAGIC *mg;
a0d0e21e 1415
533c011a 1416 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1417 gv = (GV*)*++MARK;
1418 else
3280af22 1419 gv = PL_defoutgv;
46fc3d4c 1420
5b468f54
AMS
1421 if (gv && (io = GvIO(gv))
1422 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1423 {
46fc3d4c 1424 if (MARK == ORIGMARK) {
4352c267 1425 MEXTEND(SP, 1);
46fc3d4c
PP
1426 ++MARK;
1427 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1428 ++SP;
1429 }
1430 PUSHMARK(MARK - 1);
5b468f54 1431 *MARK = SvTIED_obj((SV*)io, mg);
46fc3d4c
PP
1432 PUTBACK;
1433 ENTER;
864dbfa3 1434 call_method("PRINTF", G_SCALAR);
46fc3d4c
PP
1435 LEAVE;
1436 SPAGAIN;
1437 MARK = ORIGMARK + 1;
1438 *MARK = *SP;
1439 SP = MARK;
1440 RETURN;
1441 }
1442
26db47c4 1443 sv = NEWSV(0,0);
a0d0e21e 1444 if (!(io = GvIO(gv))) {
2dd78f96
JH
1445 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1446 report_evil_fh(gv, io, PL_op->op_type);
93189314 1447 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
1448 goto just_say_no;
1449 }
1450 else if (!(fp = IoOFP(io))) {
599cee73 1451 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1452 if (IoIFP(io))
1453 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1454 else if (ckWARN(WARN_CLOSED))
bc37a18f 1455 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1456 }
93189314 1457 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
a0d0e21e
LW
1458 goto just_say_no;
1459 }
1460 else {
1461 do_sprintf(sv, SP - MARK, MARK + 1);
1462 if (!do_print(sv, fp))
1463 goto just_say_no;
1464
1465 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1466 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
1467 goto just_say_no;
1468 }
1469 SvREFCNT_dec(sv);
1470 SP = ORIGMARK;
3280af22 1471 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1472 RETURN;
1473
1474 just_say_no:
1475 SvREFCNT_dec(sv);
1476 SP = ORIGMARK;
3280af22 1477 PUSHs(&PL_sv_undef);
a0d0e21e
LW
1478 RETURN;
1479}
1480
c07a80fd
PP
1481PP(pp_sysopen)
1482{
39644a26 1483 dSP;
c07a80fd 1484 GV *gv;
c07a80fd
PP
1485 SV *sv;
1486 char *tmps;
1487 STRLEN len;
1488 int mode, perm;
1489
1490 if (MAXARG > 3)
1491 perm = POPi;
1492 else
1493 perm = 0666;
1494 mode = POPi;
1495 sv = POPs;
1496 gv = (GV *)POPs;
1497
4592e6ca
NIS
1498 /* Need TIEHANDLE method ? */
1499
c07a80fd
PP
1500 tmps = SvPV(sv, len);
1501 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1502 IoLINES(GvIOp(gv)) = 0;
3280af22 1503 PUSHs(&PL_sv_yes);
c07a80fd
PP
1504 }
1505 else {
3280af22 1506 PUSHs(&PL_sv_undef);
c07a80fd
PP
1507 }
1508 RETURN;
1509}
1510
a0d0e21e
LW
1511PP(pp_sysread)
1512{
39644a26 1513 dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
1514 int offset;
1515 GV *gv;
1516 IO *io;
1517 char *buffer;
5b54f415 1518 SSize_t length;
eb5c063a 1519 SSize_t count;
1e422769 1520 Sock_size_t bufsize;
748a9306 1521 SV *bufsv;
a0d0e21e 1522 STRLEN blen;
2ae324a7 1523 MAGIC *mg;
eb5c063a
NIS
1524 int fp_utf8;
1525 Size_t got = 0;
1526 Size_t wanted;
1d636c13 1527 bool charstart = FALSE;
87330c3c
JH
1528 STRLEN charskip = 0;
1529 STRLEN skip = 0;
a0d0e21e
LW
1530
1531 gv = (GV*)*++MARK;
5b468f54
AMS
1532 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1533 && gv && (io = GvIO(gv))
1534 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
137443ea 1535 {
2ae324a7
PP
1536 SV *sv;
1537
1538 PUSHMARK(MARK-1);
5b468f54 1539 *MARK = SvTIED_obj((SV*)io, mg);
2ae324a7 1540 ENTER;
864dbfa3 1541 call_method("READ", G_SCALAR);
2ae324a7
PP
1542 LEAVE;
1543 SPAGAIN;
1544 sv = POPs;
1545 SP = ORIGMARK;
1546 PUSHs(sv);
1547 RETURN;
1548 }
1549
a0d0e21e
LW
1550 if (!gv)
1551 goto say_undef;
748a9306 1552 bufsv = *++MARK;
ff68c719
PP
1553 if (! SvOK(bufsv))
1554 sv_setpvn(bufsv, "", 0);
a0d0e21e 1555 length = SvIVx(*++MARK);
748a9306 1556 SETERRNO(0,0);
a0d0e21e
LW
1557 if (MARK < SP)
1558 offset = SvIVx(*++MARK);
1559 else
1560 offset = 0;
1561 io = GvIO(gv);
b5fe5ca2
SR
1562 if (!io || !IoIFP(io)) {
1563 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1564 report_evil_fh(gv, io, PL_op->op_type);
1565 SETERRNO(EBADF,RMS_IFI);
a0d0e21e 1566 goto say_undef;
b5fe5ca2 1567 }
0064a8a9 1568 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
7d59b7e4 1569 buffer = SvPVutf8_force(bufsv, blen);
eb5c063a
NIS
1570 /* UTF8 may not have been set if they are all low bytes */
1571 SvUTF8_on(bufsv);
7d59b7e4
NIS
1572 }
1573 else {
1574 buffer = SvPV_force(bufsv, blen);
1575 }
1576 if (length < 0)
1577 DIE(aTHX_ "Negative length");
eb5c063a 1578 wanted = length;
7d59b7e4 1579
d0965105
JH
1580 charstart = TRUE;
1581 charskip = 0;
87330c3c 1582 skip = 0;
d0965105 1583
a0d0e21e 1584#ifdef HAS_SOCKET
533c011a 1585 if (PL_op->op_type == OP_RECV) {
46fc3d4c 1586 char namebuf[MAXPATHLEN];
17a8c7ba 1587#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
490ab354
JH
1588 bufsize = sizeof (struct sockaddr_in);
1589#else
46fc3d4c 1590 bufsize = sizeof namebuf;
490ab354 1591#endif
abf95952
IZ
1592#ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1593 if (bufsize >= 256)
1594 bufsize = 255;
1595#endif
eb160463 1596 buffer = SvGROW(bufsv, (STRLEN)(length+1));
bbce6d69 1597 /* 'offset' means 'flags' here */
eb5c063a 1598 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
46fc3d4c 1599 (struct sockaddr *)namebuf, &bufsize);
eb5c063a 1600 if (count < 0)
a0d0e21e 1601 RETPUSHUNDEF;
4107cc59
OF
1602#ifdef EPOC
1603 /* Bogus return without padding */
1604 bufsize = sizeof (struct sockaddr_in);
1605#endif
eb5c063a 1606 SvCUR_set(bufsv, count);
748a9306
LW
1607 *SvEND(bufsv) = '\0';
1608 (void)SvPOK_only(bufsv);
eb5c063a
NIS
1609 if (fp_utf8)
1610 SvUTF8_on(bufsv);
748a9306 1611 SvSETMAGIC(bufsv);
aac0dd9a 1612 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1613 if (!(IoFLAGS(io) & IOf_UNTAINT))
1614 SvTAINTED_on(bufsv);
a0d0e21e 1615 SP = ORIGMARK;
46fc3d4c 1616 sv_setpvn(TARG, namebuf, bufsize);
a0d0e21e
LW
1617 PUSHs(TARG);
1618 RETURN;
1619 }
1620#else
911d147d 1621 if (PL_op->op_type == OP_RECV)
cea2e8a9 1622 DIE(aTHX_ PL_no_sock_func, "recv");
a0d0e21e 1623#endif
eb5c063a
NIS
1624 if (DO_UTF8(bufsv)) {
1625 /* offset adjust in characters not bytes */
1626 blen = sv_len_utf8(bufsv);
7d59b7e4 1627 }
bbce6d69 1628 if (offset < 0) {
eb160463 1629 if (-offset > (int)blen)
cea2e8a9 1630 DIE(aTHX_ "Offset outside string");
bbce6d69
PP
1631 offset += blen;
1632 }
eb5c063a
NIS
1633 if (DO_UTF8(bufsv)) {
1634 /* convert offset-as-chars to offset-as-bytes */
1635 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1636 }
1637 more_bytes:
cd52b7b2 1638 bufsize = SvCUR(bufsv);
eb160463 1639 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
cd52b7b2
PP
1640 if (offset > bufsize) { /* Zero any newly allocated space */
1641 Zero(buffer+bufsize, offset-bufsize, char);
1642 }
eb5c063a
NIS
1643 buffer = buffer + offset;
1644
533c011a 1645 if (PL_op->op_type == OP_SYSREAD) {
a7092146 1646#ifdef PERL_SOCK_SYSREAD_IS_RECV
50952442 1647 if (IoTYPE(io) == IoTYPE_SOCKET) {
eb5c063a
NIS
1648 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1649 buffer, length, 0);
a7092146
GS
1650 }
1651 else
1652#endif
1653 {
eb5c063a
NIS
1654 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1655 buffer, length);
a7092146 1656 }
a0d0e21e
LW
1657 }
1658 else
1659#ifdef HAS_SOCKET__bad_code_maybe
50952442 1660 if (IoTYPE(io) == IoTYPE_SOCKET) {
46fc3d4c 1661 char namebuf[MAXPATHLEN];
490ab354
JH
1662#if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1663 bufsize = sizeof (struct sockaddr_in);
1664#else
46fc3d4c 1665 bufsize = sizeof namebuf;
490ab354 1666#endif
eb5c063a 1667 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
46fc3d4c 1668 (struct sockaddr *)namebuf, &bufsize);
a0d0e21e
LW
1669 }
1670 else
1671#endif
3b02c43c 1672 {
eb5c063a
NIS
1673 count = PerlIO_read(IoIFP(io), buffer, length);
1674 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1675 if (count == 0 && PerlIO_error(IoIFP(io)))
1676 count = -1;
3b02c43c 1677 }
eb5c063a 1678 if (count < 0) {
a00b5bd3 1679 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
6e6ef6b2 1680 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
a0d0e21e 1681 goto say_undef;
af8c498a 1682 }
eb5c063a 1683 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
748a9306
LW
1684 *SvEND(bufsv) = '\0';
1685 (void)SvPOK_only(bufsv);
0064a8a9 1686 if (fp_utf8 && !IN_BYTES) {
eb5c063a
NIS
1687 /* Look at utf8 we got back and count the characters */
1688 char *bend = buffer + count;
1689 while (buffer < bend) {
d0965105
JH
1690 if (charstart) {
1691 skip = UTF8SKIP(buffer);
1692 charskip = 0;
1693 }
1694 if (buffer - charskip + skip > bend) {
eb5c063a
NIS
1695 /* partial character - try for rest of it */
1696 length = skip - (bend-buffer);
1697 offset = bend - SvPVX(bufsv);
d0965105
JH
1698 charstart = FALSE;
1699 charskip += count;
eb5c063a
NIS
1700 goto more_bytes;
1701 }
1702 else {
1703 got++;
1704 buffer += skip;
d0965105
JH
1705 charstart = TRUE;
1706 charskip = 0;
eb5c063a
NIS
1707 }
1708 }
1709 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1710 provided amount read (count) was what was requested (length)
1711 */
1712 if (got < wanted && count == length) {
d0965105 1713 length = wanted - got;
eb5c063a
NIS
1714 offset = bend - SvPVX(bufsv);
1715 goto more_bytes;
1716 }
1717 /* return value is character count */
1718 count = got;
1719 SvUTF8_on(bufsv);
1720 }
748a9306 1721 SvSETMAGIC(bufsv);
aac0dd9a 1722 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1723 if (!(IoFLAGS(io) & IOf_UNTAINT))
1724 SvTAINTED_on(bufsv);
a0d0e21e 1725 SP = ORIGMARK;
eb5c063a 1726 PUSHi(count);
a0d0e21e
LW
1727 RETURN;
1728
1729 say_undef:
1730 SP = ORIGMARK;
1731 RETPUSHUNDEF;
1732}
1733
1734PP(pp_syswrite)
1735{
39644a26 1736 dSP;
092bebab
JH
1737 int items = (SP - PL_stack_base) - TOPMARK;
1738 if (items == 2) {
9f089d78 1739 SV *sv;
092bebab 1740 EXTEND(SP, 1);
9f089d78
SB
1741 sv = sv_2mortal(newSViv(sv_len(*SP)));
1742 PUSHs(sv);
092bebab
JH
1743 PUTBACK;
1744 }
cea2e8a9 1745 return pp_send();
a0d0e21e
LW
1746}
1747
1748PP(pp_send)
1749{
39644a26 1750 dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
1751 GV *gv;
1752 IO *io;
748a9306 1753 SV *bufsv;
a0d0e21e 1754 char *buffer;
8c99d73e
GS
1755 Size_t length;
1756 SSize_t retval;
a0d0e21e 1757 STRLEN blen;
1d603a67 1758 MAGIC *mg;
a0d0e21e
LW
1759
1760 gv = (GV*)*++MARK;
14befaf4 1761 if (PL_op->op_type == OP_SYSWRITE
5b468f54
AMS
1762 && gv && (io = GvIO(gv))
1763 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
14befaf4 1764 {
1d603a67
GB
1765 SV *sv;
1766
1767 PUSHMARK(MARK-1);
5b468f54 1768 *MARK = SvTIED_obj((SV*)io, mg);
1d603a67 1769 ENTER;
864dbfa3 1770 call_method("WRITE", G_SCALAR);
1d603a67
GB
1771 LEAVE;
1772 SPAGAIN;
1773 sv = POPs;
1774 SP = ORIGMARK;
1775 PUSHs(sv);
1776 RETURN;
1777 }
a0d0e21e
LW
1778 if (!gv)
1779 goto say_undef;
748a9306 1780 bufsv = *++MARK;
8c99d73e 1781#if Size_t_size > IVSIZE
3c001241 1782 length = (Size_t)SvNVx(*++MARK);
146174a9 1783#else
3c001241 1784 length = (Size_t)SvIVx(*++MARK);
146174a9 1785#endif
3c001241 1786 if ((SSize_t)length < 0)
cea2e8a9 1787 DIE(aTHX_ "Negative length");
748a9306 1788 SETERRNO(0,0);
a0d0e21e
LW
1789 io = GvIO(gv);
1790 if (!io || !IoIFP(io)) {
8c99d73e 1791 retval = -1;
bc37a18f
RG
1792 if (ckWARN(WARN_CLOSED))
1793 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 1794 SETERRNO(EBADF,RMS_IFI);
7d59b7e4
NIS
1795 goto say_undef;
1796 }
1797
1798 if (PerlIO_isutf8(IoIFP(io))) {
1799 buffer = SvPVutf8(bufsv, blen);
a0d0e21e 1800 }
7d59b7e4
NIS
1801 else {
1802 if (DO_UTF8(bufsv))
1803 sv_utf8_downgrade(bufsv, FALSE);
1804 buffer = SvPV(bufsv, blen);
1805 }
1806
1807 if (PL_op->op_type == OP_SYSWRITE) {
1808 IV offset;
1809 if (DO_UTF8(bufsv)) {
1810 /* length and offset are in chars */
1811 blen = sv_len_utf8(bufsv);
1812 }
bbce6d69 1813 if (MARK < SP) {
a0d0e21e 1814 offset = SvIVx(*++MARK);
bbce6d69 1815 if (offset < 0) {
eb160463 1816 if (-offset > (IV)blen)
cea2e8a9 1817 DIE(aTHX_ "Offset outside string");
bbce6d69 1818 offset += blen;
eb160463 1819 } else if (offset >= (IV)blen && blen > 0)
cea2e8a9 1820 DIE(aTHX_ "Offset outside string");
bbce6d69 1821 } else
a0d0e21e
LW
1822 offset = 0;
1823 if (length > blen - offset)
1824 length = blen - offset;
7d59b7e4 1825 if (DO_UTF8(bufsv)) {
c8d31a35 1826 buffer = (char*)utf8_hop((U8 *)buffer, offset);
7d59b7e4
NIS
1827 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1828 }
1829 else {
1830 buffer = buffer+offset;
1831 }
a7092146 1832#ifdef PERL_SOCK_SYSWRITE_IS_SEND
50952442 1833 if (IoTYPE(io) == IoTYPE_SOCKET) {
8c99d73e 1834 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1835 buffer, length, 0);
a7092146
GS
1836 }
1837 else
1838#endif
1839 {
94e4c244 1840 /* See the note at doio.c:do_print about filesize limits. --jhi */
8c99d73e 1841 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1842 buffer, length);
a7092146 1843 }
a0d0e21e
LW
1844 }
1845#ifdef HAS_SOCKET
1846 else if (SP > MARK) {
1847 char *sockbuf;
1848 STRLEN mlen;
1849 sockbuf = SvPVx(*++MARK, mlen);
7d59b7e4 1850 /* length is really flags */
8c99d73e
GS
1851 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1852 length, (struct sockaddr *)sockbuf, mlen);
a0d0e21e
LW
1853 }
1854 else
7d59b7e4 1855 /* length is really flags */
8c99d73e 1856 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
a0d0e21e
LW
1857#else
1858 else
cea2e8a9 1859 DIE(aTHX_ PL_no_sock_func, "send");
a0d0e21e 1860#endif
8c99d73e 1861 if (retval < 0)
a0d0e21e
LW
1862 goto say_undef;
1863 SP = ORIGMARK;
f36eea10
JH
1864 if (DO_UTF8(bufsv))
1865 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
8c99d73e
GS
1866#if Size_t_size > IVSIZE
1867 PUSHn(retval);
1868#else
1869 PUSHi(retval);
1870#endif
a0d0e21e
LW
1871 RETURN;
1872
1873 say_undef:
1874 SP = ORIGMARK;
1875 RETPUSHUNDEF;
1876}
1877
1878PP(pp_recv)
1879{
cea2e8a9 1880 return pp_sysread();
a0d0e21e
LW
1881}
1882
1883PP(pp_eof)
1884{
39644a26 1885 dSP;
a0d0e21e 1886 GV *gv;
5b468f54 1887 IO *io;
4592e6ca 1888 MAGIC *mg;
a0d0e21e 1889
32da55ab 1890 if (MAXARG == 0) {
146174a9
CB
1891 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1892 IO *io;
ed2c6b9b 1893 gv = PL_last_in_gv = GvEGV(PL_argvgv);
146174a9
CB
1894 io = GvIO(gv);
1895 if (io && !IoIFP(io)) {
1896 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1897 IoLINES(io) = 0;
1898 IoFLAGS(io) &= ~IOf_START;
1899 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1900 sv_setpvn(GvSV(gv), "-", 1);
1901 SvSETMAGIC(GvSV(gv));
1902 }
1903 else if (!nextargv(gv))
1904 RETPUSHYES;
1905 }
1906 }
1907 else
1908 gv = PL_last_in_gv; /* eof */
1909 }
a0d0e21e 1910 else
146174a9 1911 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
4592e6ca 1912
5b468f54
AMS
1913 if (gv && (io = GvIO(gv))
1914 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1915 {
4592e6ca 1916 PUSHMARK(SP);
5b468f54 1917 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
1918 PUTBACK;
1919 ENTER;
864dbfa3 1920 call_method("EOF", G_SCALAR);
4592e6ca
NIS
1921 LEAVE;
1922 SPAGAIN;
1923 RETURN;
1924 }
1925
54310121 1926 PUSHs(boolSV(!gv || do_eof(gv)));
a0d0e21e
LW
1927 RETURN;
1928}
1929
1930PP(pp_tell)
1931{
39644a26 1932 dSP; dTARGET;
301e8125 1933 GV *gv;
5b468f54 1934 IO *io;
4592e6ca 1935 MAGIC *mg;
a0d0e21e 1936
32da55ab 1937 if (MAXARG == 0)
3280af22 1938 gv = PL_last_in_gv;
a0d0e21e 1939 else
3280af22 1940 gv = PL_last_in_gv = (GV*)POPs;
4592e6ca 1941
5b468f54
AMS
1942 if (gv && (io = GvIO(gv))
1943 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1944 {
4592e6ca 1945 PUSHMARK(SP);
5b468f54 1946 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
1947 PUTBACK;
1948 ENTER;
864dbfa3 1949 call_method("TELL", G_SCALAR);
4592e6ca
NIS
1950 LEAVE;
1951 SPAGAIN;
1952 RETURN;
1953 }
1954
146174a9
CB
1955#if LSEEKSIZE > IVSIZE
1956 PUSHn( do_tell(gv) );
1957#else
a0d0e21e 1958 PUSHi( do_tell(gv) );
146174a9 1959#endif
a0d0e21e
LW
1960 RETURN;
1961}
1962
1963PP(pp_seek)
1964{
cea2e8a9 1965 return pp_sysseek();
137443ea
PP
1966}
1967
1968PP(pp_sysseek)
1969{
39644a26 1970 dSP;
a0d0e21e 1971 GV *gv;
5b468f54 1972 IO *io;
a0d0e21e 1973 int whence = POPi;
146174a9
CB
1974#if LSEEKSIZE > IVSIZE
1975 Off_t offset = (Off_t)SvNVx(POPs);
1976#else
d9b3e12d 1977 Off_t offset = (Off_t)SvIVx(POPs);
146174a9 1978#endif
4592e6ca 1979 MAGIC *mg;
a0d0e21e 1980
3280af22 1981 gv = PL_last_in_gv = (GV*)POPs;
4592e6ca 1982
5b468f54
AMS
1983 if (gv && (io = GvIO(gv))
1984 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1985 {
4592e6ca 1986 PUSHMARK(SP);
5b468f54 1987 XPUSHs(SvTIED_obj((SV*)io, mg));
cb50131a
CB
1988#if LSEEKSIZE > IVSIZE
1989 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1990#else
b448e4fe 1991 XPUSHs(sv_2mortal(newSViv(offset)));
cb50131a 1992#endif
b448e4fe 1993 XPUSHs(sv_2mortal(newSViv(whence)));
4592e6ca
NIS
1994 PUTBACK;
1995 ENTER;
864dbfa3 1996 call_method("SEEK", G_SCALAR);
4592e6ca
NIS
1997 LEAVE;
1998 SPAGAIN;
1999 RETURN;
2000 }
2001
533c011a 2002 if (PL_op->op_type == OP_SEEK)
8903cb82
PP
2003 PUSHs(boolSV(do_seek(gv, offset, whence)));
2004 else {
b448e4fe
JH
2005 Off_t sought = do_sysseek(gv, offset, whence);
2006 if (sought < 0)
146174a9
CB
2007 PUSHs(&PL_sv_undef);
2008 else {
b448e4fe 2009 SV* sv = sought ?
146174a9 2010#if LSEEKSIZE > IVSIZE
b448e4fe 2011 newSVnv((NV)sought)
146174a9 2012#else
b448e4fe 2013 newSViv(sought)
146174a9
CB
2014#endif
2015 : newSVpvn(zero_but_true, ZBTLEN);
2016 PUSHs(sv_2mortal(sv));
2017 }
8903cb82 2018 }
a0d0e21e
LW
2019 RETURN;
2020}
2021
2022PP(pp_truncate)
2023{
39644a26 2024 dSP;
8c99d73e
GS
2025 /* There seems to be no consensus on the length type of truncate()
2026 * and ftruncate(), both off_t and size_t have supporters. In
2027 * general one would think that when using large files, off_t is
2028 * at least as wide as size_t, so using an off_t should be okay. */
2029 /* XXX Configure probe for the length type of *truncate() needed XXX */
2030 Off_t len;
a0d0e21e 2031
25342a55 2032#if Off_t_size > IVSIZE
8c99d73e
GS
2033 len = (Off_t)POPn;
2034#else
2035 len = (Off_t)POPi;
2036#endif
2037 /* Checking for length < 0 is problematic as the type might or
301e8125 2038 * might not be signed: if it is not, clever compilers will moan. */
8c99d73e 2039 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
748a9306 2040 SETERRNO(0,0);
5d94fbed 2041#if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
d05c1ba0
JH
2042 {
2043 STRLEN n_a;
2044 int result = 1;
2045 GV *tmpgv;
090bf15b
SR
2046 IO *io;
2047
d05c1ba0
JH
2048 if (PL_op->op_flags & OPf_SPECIAL) {
2049 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2050
090bf15b
SR
2051 do_ftruncate_gv:
2052 if (!GvIO(tmpgv))
2053 result = 0;
d05c1ba0 2054 else {
090bf15b
SR
2055 PerlIO *fp;
2056 io = GvIOp(tmpgv);
2057 do_ftruncate_io:
2058 TAINT_PROPER("truncate");
2059 if (!(fp = IoIFP(io))) {
2060 result = 0;
2061 }
2062 else {
2063 PerlIO_flush(fp);
cbdc8872 2064#ifdef HAS_TRUNCATE
090bf15b 2065 if (ftruncate(PerlIO_fileno(fp), len) < 0)
301e8125 2066#else
090bf15b 2067 if (my_chsize(PerlIO_fileno(fp), len) < 0)
cbdc8872 2068#endif
090bf15b
SR
2069 result = 0;
2070 }
d05c1ba0 2071 }
cbdc8872 2072 }
d05c1ba0
JH
2073 else {
2074 SV *sv = POPs;
2075 char *name;
72f496dc 2076
d05c1ba0
JH
2077 if (SvTYPE(sv) == SVt_PVGV) {
2078 tmpgv = (GV*)sv; /* *main::FRED for example */
090bf15b 2079 goto do_ftruncate_gv;
d05c1ba0
JH
2080 }
2081 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2082 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
090bf15b
SR
2083 goto do_ftruncate_gv;
2084 }
2085 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2086 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2087 goto do_ftruncate_io;
d05c1ba0 2088 }
1e422769 2089
d05c1ba0
JH
2090 name = SvPV(sv, n_a);
2091 TAINT_PROPER("truncate");
cbdc8872 2092#ifdef HAS_TRUNCATE
d05c1ba0
JH
2093 if (truncate(name, len) < 0)
2094 result = 0;
cbdc8872 2095#else
d05c1ba0
JH
2096 {
2097 int tmpfd;
2098
2099 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
cbdc8872 2100 result = 0;
d05c1ba0
JH
2101 else {
2102 if (my_chsize(tmpfd, len) < 0)
2103 result = 0;
2104 PerlLIO_close(tmpfd);
2105 }
cbdc8872 2106 }
a0d0e21e 2107#endif
d05c1ba0 2108 }
a0d0e21e 2109
d05c1ba0
JH
2110 if (result)
2111 RETPUSHYES;
2112 if (!errno)
93189314 2113 SETERRNO(EBADF,RMS_IFI);
d05c1ba0
JH
2114 RETPUSHUNDEF;
2115 }
a0d0e21e 2116#else
cea2e8a9 2117 DIE(aTHX_ "truncate not implemented");
a0d0e21e
LW
2118#endif
2119}
2120
2121PP(pp_fcntl)
2122{
cea2e8a9 2123 return pp_ioctl();
a0d0e21e
LW
2124}
2125
2126PP(pp_ioctl)
2127{
39644a26 2128 dSP; dTARGET;
748a9306 2129 SV *argsv = POPs;
3bb7c1b4 2130 unsigned int func = POPu;
533c011a 2131 int optype = PL_op->op_type;
a0d0e21e 2132 char *s;
324aa91a 2133 IV retval;
a0d0e21e 2134 GV *gv = (GV*)POPs;
c289d2f7 2135 IO *io = gv ? GvIOn(gv) : 0;
a0d0e21e 2136
748a9306 2137 if (!io || !argsv || !IoIFP(io)) {
c289d2f7
JH
2138 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2139 report_evil_fh(gv, io, PL_op->op_type);
93189314 2140 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
a0d0e21e
LW
2141 RETPUSHUNDEF;
2142 }
2143
748a9306 2144 if (SvPOK(argsv) || !SvNIOK(argsv)) {
a0d0e21e 2145 STRLEN len;
324aa91a 2146 STRLEN need;
748a9306 2147 s = SvPV_force(argsv, len);
324aa91a
HF
2148 need = IOCPARM_LEN(func);
2149 if (len < need) {
2150 s = Sv_Grow(argsv, need + 1);
2151 SvCUR_set(argsv, need);
a0d0e21e
LW
2152 }
2153
748a9306 2154 s[SvCUR(argsv)] = 17; /* a little sanity check here */
a0d0e21e
LW
2155 }
2156 else {
748a9306 2157 retval = SvIV(argsv);
c529f79d 2158 s = INT2PTR(char*,retval); /* ouch */
a0d0e21e
LW
2159 }
2160
2161 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2162
2163 if (optype == OP_IOCTL)
2164#ifdef HAS_IOCTL
76e3520e 2165 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
a0d0e21e 2166#else
cea2e8a9 2167 DIE(aTHX_ "ioctl is not implemented");
a0d0e21e
LW
2168#endif
2169 else
c214f4ad
B
2170#ifndef HAS_FCNTL
2171 DIE(aTHX_ "fcntl is not implemented");
2172#else
55497cff 2173#if defined(OS2) && defined(__EMX__)
760ac839 2174 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
55497cff 2175#else
760ac839 2176 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
301e8125 2177#endif
a0d0e21e 2178
748a9306
LW
2179 if (SvPOK(argsv)) {
2180 if (s[SvCUR(argsv)] != 17)
cea2e8a9 2181 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
53e06cf0 2182 OP_NAME(PL_op));
748a9306
LW
2183 s[SvCUR(argsv)] = 0; /* put our null back */
2184 SvSETMAGIC(argsv); /* Assume it has changed */
a0d0e21e
LW
2185 }
2186
2187 if (retval == -1)
2188 RETPUSHUNDEF;
2189 if (retval != 0) {
2190 PUSHi(retval);
2191 }
2192 else {
8903cb82 2193 PUSHp(zero_but_true, ZBTLEN);
a0d0e21e 2194 }
4808266b 2195#endif
c214f4ad 2196 RETURN;
a0d0e21e
LW
2197}
2198
2199PP(pp_flock)
2200{
9cad6237 2201#ifdef FLOCK
39644a26 2202 dSP; dTARGET;
a0d0e21e
LW
2203 I32 value;
2204 int argtype;
2205 GV *gv;
bc37a18f 2206 IO *io = NULL;
760ac839 2207 PerlIO *fp;
16d20bd9 2208
a0d0e21e 2209 argtype = POPi;
32da55ab 2210 if (MAXARG == 0)
3280af22 2211 gv = PL_last_in_gv;
a0d0e21e
LW
2212 else
2213 gv = (GV*)POPs;
bc37a18f
RG
2214 if (gv && (io = GvIO(gv)))
2215 fp = IoIFP(io);
2216 else {
a0d0e21e 2217 fp = Nullfp;
bc37a18f
RG
2218 io = NULL;
2219 }
a0d0e21e 2220 if (fp) {
68dc0745 2221 (void)PerlIO_flush(fp);
76e3520e 2222 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
a0d0e21e 2223 }
cb50131a 2224 else {
bc37a18f
RG
2225 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2226 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 2227 value = 0;
93189314 2228 SETERRNO(EBADF,RMS_IFI);
cb50131a 2229 }
a0d0e21e
LW
2230 PUSHi(value);
2231 RETURN;
2232#else
cea2e8a9 2233 DIE(aTHX_ PL_no_func, "flock()");
a0d0e21e
LW
2234#endif
2235}
2236
2237/* Sockets. */
2238
2239PP(pp_socket)
2240{
a0d0e21e 2241#ifdef HAS_SOCKET
9cad6237 2242 dSP;
a0d0e21e
LW
2243 GV *gv;
2244 register IO *io;
2245 int protocol = POPi;
2246 int type = POPi;
2247 int domain = POPi;
2248 int fd;
2249
2250 gv = (GV*)POPs;
c289d2f7 2251 io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2252
c289d2f7
JH
2253 if (!gv || !io) {
2254 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2255 report_evil_fh(gv, io, PL_op->op_type);
2256 if (IoIFP(io))
2257 do_close(gv, FALSE);
93189314 2258 SETERRNO(EBADF,LIB_INVARG);
a0d0e21e
LW
2259 RETPUSHUNDEF;
2260 }
2261
57171420
BS
2262 if (IoIFP(io))
2263 do_close(gv, FALSE);
2264
a0d0e21e 2265 TAINT_PROPER("socket");
6ad3d225 2266 fd = PerlSock_socket(domain, type, protocol);
a0d0e21e
LW
2267 if (fd < 0)
2268 RETPUSHUNDEF;
479b2847
GS
2269 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2270 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
50952442 2271 IoTYPE(io) = IoTYPE_SOCKET;
a0d0e21e 2272 if (!IoIFP(io) || !IoOFP(io)) {
760ac839
LW
2273 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2274 if (IoOFP(io)) PerlIO_close(IoOFP(io));
6ad3d225 2275 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
a0d0e21e
LW
2276 RETPUSHUNDEF;
2277 }
8d2a6795
GS
2278#if defined(HAS_FCNTL) && defined(F_SETFD)
2279 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2280#endif
a0d0e21e 2281
d5ff79b3
OF
2282#ifdef EPOC
2283 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2284#endif
2285
a0d0e21e
LW
2286 RETPUSHYES;
2287#else
cea2e8a9 2288 DIE(aTHX_ PL_no_sock_func, "socket");
a0d0e21e
LW
2289#endif
2290}
2291
2292PP(pp_sockpair)
2293{
c95c94b1 2294#if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
76ffd3b9 2295 dSP;
a0d0e21e
LW
2296 GV *gv1;
2297 GV *gv2;
2298 register IO *io1;
2299 register IO *io2;
2300 int protocol = POPi;
2301 int type = POPi;
2302 int domain = POPi;
2303 int fd[2];
2304
2305 gv2 = (GV*)POPs;
2306 gv1 = (GV*)POPs;
c289d2f7
JH
2307 io1 = gv1 ? GvIOn(gv1) : NULL;
2308 io2 = gv2 ? GvIOn(gv2) : NULL;
2309 if (!gv1 || !gv2 || !io1 || !io2) {
2310 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2311 if (!gv1 || !io1)
2312 report_evil_fh(gv1, io1, PL_op->op_type);
2313 if (!gv2 || !io2)
2314 report_evil_fh(gv1, io2, PL_op->op_type);
2315 }
2316 if (IoIFP(io1))
2317 do_close(gv1, FALSE);
2318 if (IoIFP(io2))
2319 do_close(gv2, FALSE);
a0d0e21e 2320 RETPUSHUNDEF;
c289d2f7 2321 }
a0d0e21e 2322
dc0d0a5f
JH
2323 if (IoIFP(io1))
2324 do_close(gv1, FALSE);
2325 if (IoIFP(io2))
2326 do_close(gv2, FALSE);
57171420 2327
a0d0e21e 2328 TAINT_PROPER("socketpair");
6ad3d225 2329 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
a0d0e21e 2330 RETPUSHUNDEF;
479b2847
GS
2331 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2332 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
50952442 2333 IoTYPE(io1) = IoTYPE_SOCKET;
479b2847
GS
2334 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2335 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
50952442 2336 IoTYPE(io2) = IoTYPE_SOCKET;
a0d0e21e 2337 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
760ac839
LW
2338 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2339 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
6ad3d225 2340 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
760ac839
LW
2341 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2342 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
6ad3d225 2343 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
a0d0e21e
LW
2344 RETPUSHUNDEF;
2345 }
8d2a6795
GS
2346#if defined(HAS_FCNTL) && defined(F_SETFD)
2347 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2348 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2349#endif
a0d0e21e
LW
2350
2351 RETPUSHYES;
2352#else
cea2e8a9 2353 DIE(aTHX_ PL_no_sock_func, "socketpair");
a0d0e21e
LW
2354#endif
2355}
2356
2357PP(pp_bind)
2358{
a0d0e21e 2359#ifdef HAS_SOCKET
9cad6237 2360 dSP;
eec2d3df 2361#ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
82b3da69
JH
2362 extern void GETPRIVMODE();
2363 extern void GETUSERMODE();
eec2d3df 2364#endif
748a9306 2365 SV *addrsv = POPs;
a0d0e21e
LW
2366 char *addr;
2367 GV *gv = (GV*)POPs;
2368 register IO *io = GvIOn(gv);
2369 STRLEN len;
eec2d3df
GS
2370 int bind_ok = 0;
2371#ifdef MPE
2372 int mpeprivmode = 0;
2373#endif
a0d0e21e
LW
2374
2375 if (!io || !IoIFP(io))
2376 goto nuts;
2377
748a9306 2378 addr = SvPV(addrsv, len);
a0d0e21e 2379 TAINT_PROPER("bind");
eec2d3df
GS
2380#ifdef MPE /* Deal with MPE bind() peculiarities */
2381 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2382 /* The address *MUST* stupidly be zero. */
2383 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2384 /* PRIV mode is required to bind() to ports < 1024. */
2385 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2386 ((struct sockaddr_in *)addr)->sin_port > 0) {
2387 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2388 mpeprivmode = 1;
2389 }
2390 }
2391#endif /* MPE */
2392 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2393 (struct sockaddr *)addr, len) >= 0)
2394 bind_ok = 1;
2395
2396#ifdef MPE /* Switch back to USER mode */
2397 if (mpeprivmode)
2398 GETUSERMODE();
2399#endif /* MPE */
2400
2401 if (bind_ok)
a0d0e21e
LW
2402 RETPUSHYES;
2403 else
2404 RETPUSHUNDEF;
2405
2406nuts:
599cee73 2407 if (ckWARN(WARN_CLOSED))
bc37a18f 2408 report_evil_fh(gv, io, PL_op->op_type);
93189314 2409 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2410 RETPUSHUNDEF;
2411#else
cea2e8a9 2412 DIE(aTHX_ PL_no_sock_func, "bind");
a0d0e21e
LW
2413#endif
2414}
2415
2416PP(pp_connect)
2417{
a0d0e21e 2418#ifdef HAS_SOCKET
9cad6237 2419 dSP;
748a9306 2420 SV *addrsv = POPs;
a0d0e21e
LW
2421 char *addr;
2422 GV *gv = (GV*)POPs;
2423 register IO *io = GvIOn(gv);
2424 STRLEN len;
2425
2426 if (!io || !IoIFP(io))
2427 goto nuts;
2428
748a9306 2429 addr = SvPV(addrsv, len);
a0d0e21e 2430 TAINT_PROPER("connect");
6ad3d225 2431 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
a0d0e21e
LW
2432 RETPUSHYES;
2433 else
2434 RETPUSHUNDEF;
2435
2436nuts:
599cee73 2437 if (ckWARN(WARN_CLOSED))
bc37a18f 2438 report_evil_fh(gv, io, PL_op->op_type);
93189314 2439 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2440 RETPUSHUNDEF;
2441#else
cea2e8a9 2442 DIE(aTHX_ PL_no_sock_func, "connect");
a0d0e21e
LW
2443#endif
2444}
2445
2446PP(pp_listen)
2447{
a0d0e21e 2448#ifdef HAS_SOCKET
9cad6237 2449 dSP;
a0d0e21e
LW
2450 int backlog = POPi;
2451 GV *gv = (GV*)POPs;
c289d2f7 2452 register IO *io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2453
c289d2f7 2454 if (!gv || !io || !IoIFP(io))
a0d0e21e
LW
2455 goto nuts;
2456
6ad3d225 2457 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
a0d0e21e
LW
2458 RETPUSHYES;
2459 else
2460 RETPUSHUNDEF;
2461
2462nuts:
599cee73 2463 if (ckWARN(WARN_CLOSED))
bc37a18f 2464 report_evil_fh(gv, io, PL_op->op_type);
93189314 2465 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2466 RETPUSHUNDEF;
2467#else
cea2e8a9 2468 DIE(aTHX_ PL_no_sock_func, "listen");
a0d0e21e
LW
2469#endif
2470}
2471
2472PP(pp_accept)
2473{
a0d0e21e 2474#ifdef HAS_SOCKET
9cad6237 2475 dSP; dTARGET;
a0d0e21e
LW
2476 GV *ngv;
2477 GV *ggv;
2478 register IO *nstio;
2479 register IO *gstio;
4633a7c4 2480 struct sockaddr saddr; /* use a struct to avoid alignment problems */
1e422769 2481 Sock_size_t len = sizeof saddr;
a0d0e21e
LW
2482 int fd;
2483
2484 ggv = (GV*)POPs;
2485 ngv = (GV*)POPs;
2486
2487 if (!ngv)
2488 goto badexit;
2489 if (!ggv)
2490 goto nuts;
2491
2492 gstio = GvIO(ggv);
2493 if (!gstio || !IoIFP(gstio))
2494 goto nuts;
2495
2496 nstio = GvIOn(ngv);
6ad3d225 2497 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
a0d0e21e
LW
2498 if (fd < 0)
2499 goto badexit;
a70048fb
AB
2500 if (IoIFP(nstio))
2501 do_close(ngv, FALSE);
479b2847 2502 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
7e56aaa7 2503 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
50952442 2504 IoTYPE(nstio) = IoTYPE_SOCKET;
a0d0e21e 2505 if (!IoIFP(nstio) || !IoOFP(nstio)) {
760ac839
LW
2506 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2507 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
6ad3d225 2508 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
a0d0e21e
LW
2509 goto badexit;
2510 }
8d2a6795
GS
2511#if defined(HAS_FCNTL) && defined(F_SETFD)
2512 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2513#endif
a0d0e21e 2514
ed79a026 2515#ifdef EPOC
a9f1f6b0
OF
2516 len = sizeof saddr; /* EPOC somehow truncates info */
2517 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
ed79a026 2518#endif
381c1bae
RGS
2519#ifdef __SCO_VERSION__
2520 len = sizeof saddr; /* OpenUNIX 8 somehow truncates info */
2521#endif
ed79a026 2522
748a9306 2523 PUSHp((char *)&saddr, len);
a0d0e21e
LW
2524 RETURN;
2525
2526nuts:
599cee73 2527 if (ckWARN(WARN_CLOSED))
bc37a18f 2528 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
93189314 2529 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2530
2531badexit:
2532 RETPUSHUNDEF;
2533
2534#else
cea2e8a9 2535 DIE(aTHX_ PL_no_sock_func, "accept");
a0d0e21e
LW
2536#endif
2537}
2538
2539PP(pp_shutdown)
2540{
a0d0e21e 2541#ifdef HAS_SOCKET
9cad6237 2542 dSP; dTARGET;
a0d0e21e
LW
2543 int how = POPi;
2544 GV *gv = (GV*)POPs;
2545 register IO *io = GvIOn(gv);
2546
2547 if (!io || !IoIFP(io))
2548 goto nuts;
2549
6ad3d225 2550 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
a0d0e21e
LW
2551 RETURN;
2552
2553nuts:
599cee73 2554 if (ckWARN(WARN_CLOSED))
bc37a18f 2555 report_evil_fh(gv, io, PL_op->op_type);
93189314 2556 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2557 RETPUSHUNDEF;
2558#else
cea2e8a9 2559 DIE(aTHX_ PL_no_sock_func, "shutdown");
a0d0e21e
LW
2560#endif
2561}
2562
2563PP(pp_gsockopt)
2564{
2565#ifdef HAS_SOCKET
cea2e8a9 2566 return pp_ssockopt();
a0d0e21e 2567#else
cea2e8a9 2568 DIE(aTHX_ PL_no_sock_func, "getsockopt");
a0d0e21e
LW
2569#endif
2570}
2571
2572PP(pp_ssockopt)
2573{
a0d0e21e 2574#ifdef HAS_SOCKET
9cad6237 2575 dSP;
533c011a 2576 int optype = PL_op->op_type;
a0d0e21e
LW
2577 SV *sv;
2578 int fd;
2579 unsigned int optname;
2580 unsigned int lvl;
2581 GV *gv;
2582 register IO *io;
1e422769 2583 Sock_size_t len;
a0d0e21e
LW
2584
2585 if (optype == OP_GSOCKOPT)
2586 sv = sv_2mortal(NEWSV(22, 257));
2587 else
2588 sv = POPs;
2589 optname = (unsigned int) POPi;
2590 lvl = (unsigned int) POPi;
2591
2592 gv = (GV*)POPs;
2593 io = GvIOn(gv);
2594 if (!io || !IoIFP(io))
2595 goto nuts;
2596
760ac839 2597 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2598 switch (optype) {
2599 case OP_GSOCKOPT:
748a9306 2600 SvGROW(sv, 257);
a0d0e21e 2601 (void)SvPOK_only(sv);
748a9306
LW
2602 SvCUR_set(sv,256);
2603 *SvEND(sv) ='\0';
1e422769 2604 len = SvCUR(sv);
6ad3d225 2605 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
a0d0e21e 2606 goto nuts2;
1e422769 2607 SvCUR_set(sv, len);
748a9306 2608 *SvEND(sv) ='\0';
a0d0e21e
LW
2609 PUSHs(sv);
2610 break;
2611 case OP_SSOCKOPT: {
1e422769
PP
2612 char *buf;
2613 int aint;
2614 if (SvPOKp(sv)) {
2d8e6c8d
GS
2615 STRLEN l;
2616 buf = SvPV(sv, l);
2617 len = l;
1e422769 2618 }
56ee1660 2619 else {
a0d0e21e
LW
2620 aint = (int)SvIV(sv);
2621 buf = (char*)&aint;
2622 len = sizeof(int);
2623 }
6ad3d225 2624 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
a0d0e21e 2625 goto nuts2;
3280af22 2626 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2627 }
2628 break;
2629 }
2630 RETURN;
2631
2632nuts:
599cee73 2633 if (ckWARN(WARN_CLOSED))
bc37a18f 2634 report_evil_fh(gv, io, optype);
93189314 2635 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2636nuts2:
2637 RETPUSHUNDEF;
2638
2639#else
cea2e8a9 2640 DIE(aTHX_ PL_no_sock_func, "setsockopt");
a0d0e21e
LW
2641#endif
2642}
2643
2644PP(pp_getsockname)
2645{
2646#ifdef HAS_SOCKET
cea2e8a9 2647 return pp_getpeername();
a0d0e21e 2648#else
cea2e8a9 2649 DIE(aTHX_ PL_no_sock_func, "getsockname");
a0d0e21e
LW
2650#endif
2651}
2652
2653PP(pp_getpeername)
2654{
a0d0e21e 2655#ifdef HAS_SOCKET
9cad6237 2656 dSP;
533c011a 2657 int optype = PL_op->op_type;
a0d0e21e
LW
2658 SV *sv;
2659 int fd;
2660 GV *gv = (GV*)POPs;
2661 register IO *io = GvIOn(gv);
1e422769 2662 Sock_size_t len;
a0d0e21e
LW
2663
2664 if (!io || !IoIFP(io))
2665 goto nuts;
2666
2667 sv = sv_2mortal(NEWSV(22, 257));
748a9306 2668 (void)SvPOK_only(sv);
1e422769
PP
2669 len = 256;
2670 SvCUR_set(sv, len);
748a9306 2671 *SvEND(sv) ='\0';
760ac839 2672 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2673 switch (optype) {
2674 case OP_GETSOCKNAME:
6ad3d225 2675 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e
LW
2676 goto nuts2;
2677 break;
2678 case OP_GETPEERNAME:
6ad3d225 2679 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e 2680 goto nuts2;
490ab354
JH
2681#if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2682 {
2683 static const char nowhere[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
2684 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2685 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2686 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2687 sizeof(u_short) + sizeof(struct in_addr))) {
301e8125 2688 goto nuts2;
490ab354
JH
2689 }
2690 }
2691#endif
a0d0e21e
LW
2692 break;
2693 }
13826f2c
CS
2694#ifdef BOGUS_GETNAME_RETURN
2695 /* Interactive Unix, getpeername() and getsockname()
2696 does not return valid namelen */
1e422769
PP
2697 if (len == BOGUS_GETNAME_RETURN)
2698 len = sizeof(struct sockaddr);
13826f2c 2699#endif
1e422769 2700 SvCUR_set(sv, len);
748a9306 2701 *SvEND(sv) ='\0';
a0d0e21e
LW
2702 PUSHs(sv);
2703 RETURN;
2704
2705nuts:
599cee73 2706 if (ckWARN(WARN_CLOSED))
bc37a18f 2707 report_evil_fh(gv, io, optype);
93189314 2708 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2709nuts2:
2710 RETPUSHUNDEF;
2711
2712#else
cea2e8a9 2713 DIE(aTHX_ PL_no_sock_func, "getpeername");
a0d0e21e
LW
2714#endif
2715}
2716
2717/* Stat calls. */
2718
2719PP(pp_lstat)
2720{
cea2e8a9 2721 return pp_stat();
a0d0e21e
LW
2722}
2723
2724PP(pp_stat)
2725{
39644a26 2726 dSP;
2dd78f96 2727 GV *gv;
54310121 2728 I32 gimme;
a0d0e21e 2729 I32 max = 13;
2d8e6c8d 2730 STRLEN n_a;
a0d0e21e 2731
533c011a 2732 if (PL_op->op_flags & OPf_REF) {
2dd78f96 2733 gv = cGVOP_gv;
8a4e5b40 2734 if (PL_op->op_type == OP_LSTAT) {
5d3e98de
RGS
2735 if (gv != PL_defgv) {
2736 if (ckWARN(WARN_IO))
9014280d 2737 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de
RGS
2738 "lstat() on filehandle %s", GvENAME(gv));
2739 } else if (PL_laststype != OP_LSTAT)
8a4e5b40 2740 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
8a4e5b40
DD
2741 }
2742
748a9306 2743 do_fstat:
2dd78f96 2744 if (gv != PL_defgv) {
3280af22 2745 PL_laststype = OP_STAT;
2dd78f96 2746 PL_statgv = gv;
3280af22 2747 sv_setpv(PL_statname, "");
2dd78f96
JH
2748 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2749 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
a0d0e21e 2750 }
9ddeeac9 2751 if (PL_laststatval < 0) {
2dd78f96
JH
2752 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2753 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
a0d0e21e 2754 max = 0;
9ddeeac9 2755 }
a0d0e21e
LW
2756 }
2757 else {
748a9306
LW
2758 SV* sv = POPs;
2759 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 2760 gv = (GV*)sv;
748a9306
LW
2761 goto do_fstat;
2762 }
2763 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 2764 gv = (GV*)SvRV(sv);
5d3e98de 2765 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
9014280d 2766 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de 2767 "lstat() on filehandle %s", GvENAME(gv));
748a9306
LW
2768 goto do_fstat;
2769 }
2d8e6c8d 2770 sv_setpv(PL_statname, SvPV(sv,n_a));
3280af22 2771 PL_statgv = Nullgv;
a0d0e21e 2772#ifdef HAS_LSTAT
533c011a
NIS
2773 PL_laststype = PL_op->op_type;
2774 if (PL_op->op_type == OP_LSTAT)
2d8e6c8d 2775 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
a0d0e21e
LW
2776 else
2777#endif
2d8e6c8d 2778 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
3280af22 2779 if (PL_laststatval < 0) {
2d8e6c8d 2780 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
9014280d 2781 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
a0d0e21e
LW
2782 max = 0;
2783 }
2784 }
2785
54310121
PP
2786 gimme = GIMME_V;
2787 if (gimme != G_ARRAY) {
2788 if (gimme != G_VOID)
2789 XPUSHs(boolSV(max));
2790 RETURN;
a0d0e21e
LW
2791 }
2792 if (max) {
36477c24
PP
2793 EXTEND(SP, max);
2794 EXTEND_MORTAL(max);
1ff81528
PL
2795 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2796 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
b448e4fe
JH
2797 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2798 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
146174a9
CB
2799#if Uid_t_size > IVSIZE
2800 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2801#else
23dcd6c8 2802# if Uid_t_sign <= 0
1ff81528 2803 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
23dcd6c8
JH
2804# else
2805 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2806# endif
146174a9 2807#endif
301e8125 2808#if Gid_t_size > IVSIZE
146174a9
CB
2809 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2810#else
23dcd6c8 2811# if Gid_t_sign <= 0
1ff81528 2812 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
23dcd6c8
JH
2813# else
2814 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2815# endif
146174a9 2816#endif
cbdc8872 2817#ifdef USE_STAT_RDEV
1ff81528 2818 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
cbdc8872 2819#else
79cb57f6 2820 PUSHs(sv_2mortal(newSVpvn("", 0)));
cbdc8872 2821#endif
146174a9 2822#if Off_t_size > IVSIZE
4a9d6100 2823 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
146174a9 2824#else
1ff81528 2825 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
146174a9 2826#endif
cbdc8872 2827#ifdef BIG_TIME
172ae379
JH
2828 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2829 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2830 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
cbdc8872 2831#else
1ff81528
PL
2832 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2833 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2834 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
cbdc8872 2835#endif
a0d0e21e 2836#ifdef USE_STAT_BLOCKS
b448e4fe
JH
2837 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2838 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
a0d0e21e 2839#else
79cb57f6
GS
2840 PUSHs(sv_2mortal(newSVpvn("", 0)));
2841 PUSHs(sv_2mortal(newSVpvn("", 0)));
a0d0e21e
LW
2842#endif
2843 }
2844 RETURN;
2845}
2846
2847PP(pp_ftrread)
2848{
9cad6237 2849 I32 result;
2a3ff820 2850 dSP;
5ff3f7a4 2851#if defined(HAS_ACCESS) && defined(R_OK)
2d8e6c8d 2852 STRLEN n_a;
1af34c76 2853 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2854 result = access(POPpx, R_OK);
5ff3f7a4
GS
2855 if (result == 0)
2856 RETPUSHYES;
2857 if (result < 0)
2858 RETPUSHUNDEF;
2859 RETPUSHNO;
22865c03
GS
2860 }
2861 else
cea2e8a9 2862 result = my_stat();
5ff3f7a4 2863#else
cea2e8a9 2864 result = my_stat();
5ff3f7a4 2865#endif
22865c03 2866 SPAGAIN;
a0d0e21e
LW
2867 if (result < 0)
2868 RETPUSHUNDEF;
3280af22 2869 if (cando(S_IRUSR, 0, &PL_statcache))
a0d0e21e
LW
2870 RETPUSHYES;
2871 RETPUSHNO;
2872}
2873
2874PP(pp_ftrwrite)
2875{
9cad6237 2876 I32 result;
2a3ff820 2877 dSP;
5ff3f7a4 2878#if defined(HAS_ACCESS) && defined(W_OK)
2d8e6c8d 2879 STRLEN n_a;
1af34c76 2880 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2881 result = access(POPpx, W_OK);
5ff3f7a4
GS
2882 if (result == 0)
2883 RETPUSHYES;
2884 if (result < 0)
2885 RETPUSHUNDEF;
2886 RETPUSHNO;
22865c03
GS
2887 }
2888 else
cea2e8a9 2889 result = my_stat();
5ff3f7a4 2890#else
cea2e8a9 2891 result = my_stat();
5ff3f7a4 2892#endif
22865c03 2893 SPAGAIN;
a0d0e21e
LW
2894 if (result < 0)
2895 RETPUSHUNDEF;
3280af22 2896 if (cando(S_IWUSR, 0, &PL_statcache))
a0d0e21e
LW
2897 RETPUSHYES;
2898 RETPUSHNO;
2899}
2900
2901PP(pp_ftrexec)
2902{
9cad6237 2903 I32 result;
2a3ff820 2904 dSP;
5ff3f7a4 2905#if defined(HAS_ACCESS) && defined(X_OK)
2d8e6c8d 2906 STRLEN n_a;
1af34c76 2907 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2908 result = access(POPpx, X_OK);
5ff3f7a4
GS
2909 if (result == 0)
2910 RETPUSHYES;
2911 if (result < 0)
2912 RETPUSHUNDEF;
2913 RETPUSHNO;
22865c03
GS
2914 }
2915 else
cea2e8a9 2916 result = my_stat();
5ff3f7a4 2917#else
cea2e8a9 2918 result = my_stat();
5ff3f7a4 2919#endif
22865c03 2920 SPAGAIN;
a0d0e21e
LW
2921 if (result < 0)
2922 RETPUSHUNDEF;
3280af22 2923 if (cando(S_IXUSR, 0, &PL_statcache))
a0d0e21e
LW
2924 RETPUSHYES;
2925 RETPUSHNO;
2926}
2927
2928PP(pp_fteread)
2929{
9cad6237 2930 I32 result;
2a3ff820 2931 dSP;
5ff3f7a4 2932#ifdef PERL_EFF_ACCESS_R_OK
2d8e6c8d 2933 STRLEN n_a;
1af34c76 2934 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2935 result = PERL_EFF_ACCESS_R_OK(POPpx);
5ff3f7a4
GS
2936 if (result == 0)
2937 RETPUSHYES;
2938 if (result < 0)
2939 RETPUSHUNDEF;
2940 RETPUSHNO;
22865c03
GS
2941 }
2942 else
cea2e8a9 2943 result = my_stat();
5ff3f7a4 2944#else
cea2e8a9 2945 result = my_stat();
5ff3f7a4 2946#endif
22865c03 2947 SPAGAIN;
a0d0e21e
LW
2948 if (result < 0)
2949 RETPUSHUNDEF;
3280af22 2950 if (cando(S_IRUSR, 1, &PL_statcache))
a0d0e21e
LW
2951 RETPUSHYES;
2952 RETPUSHNO;
2953}
2954
2955PP(pp_ftewrite)
2956{
9cad6237 2957 I32 result;
2a3ff820 2958 dSP;
5ff3f7a4 2959#ifdef PERL_EFF_ACCESS_W_OK
2d8e6c8d 2960 STRLEN n_a;
1af34c76 2961 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2962 result = PERL_EFF_ACCESS_W_OK(POPpx);
5ff3f7a4
GS
2963 if (result == 0)
2964 RETPUSHYES;
2965 if (result < 0)
2966 RETPUSHUNDEF;
2967 RETPUSHNO;
22865c03
GS
2968 }
2969 else
cea2e8a9 2970 result = my_stat();
5ff3f7a4 2971#else
cea2e8a9 2972 result = my_stat();
5ff3f7a4 2973#endif
22865c03 2974 SPAGAIN;
a0d0e21e
LW
2975 if (result < 0)
2976 RETPUSHUNDEF;
3280af22 2977 if (cando(S_IWUSR, 1, &PL_statcache))
a0d0e21e
LW
2978 RETPUSHYES;
2979 RETPUSHNO;
2980}
2981
2982PP(pp_fteexec)
2983{
9cad6237 2984 I32 result;
2a3ff820 2985 dSP;
5ff3f7a4 2986#ifdef PERL_EFF_ACCESS_X_OK
2d8e6c8d 2987 STRLEN n_a;
1af34c76 2988 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2989 result = PERL_EFF_ACCESS_X_OK(POPpx);
5ff3f7a4
GS
2990 if (result == 0)
2991 RETPUSHYES;
2992 if (result < 0)
2993 RETPUSHUNDEF;
2994 RETPUSHNO;
22865c03
GS
2995 }
2996 else
cea2e8a9 2997 result = my_stat();
5ff3f7a4 2998#else
cea2e8a9 2999 result = my_stat();
5ff3f7a4 3000#endif
22865c03 3001 SPAGAIN;
a0d0e21e
LW
3002 if (result < 0)
3003 RETPUSHUNDEF;
3280af22 3004 if (cando(S_IXUSR, 1, &PL_statcache))
a0d0e21e
LW
3005 RETPUSHYES;
3006 RETPUSHNO;
3007}
3008
3009PP(pp_ftis)
3010{
9cad6237 3011 I32 result = my_stat();
2a3ff820 3012 dSP;
a0d0e21e
LW
3013 if (result < 0)
3014 RETPUSHUNDEF;
3015 RETPUSHYES;
3016}
3017
3018PP(pp_fteowned)
3019{
cea2e8a9 3020 return pp_ftrowned();
a0d0e21e
LW
3021}
3022
3023PP(pp_ftrowned)
3024{
9cad6237 3025 I32 result = my_stat();
2a3ff820 3026 dSP;
a0d0e21e
LW
3027 if (result < 0)
3028 RETPUSHUNDEF;
146174a9
CB
3029 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3030 PL_euid : PL_uid) )
a0d0e21e
LW
3031 RETPUSHYES;
3032 RETPUSHNO;
3033}
3034
3035PP(pp_ftzero)
3036{
9cad6237 3037 I32 result = my_stat();
2a3ff820 3038 dSP;
a0d0e21e
LW
3039 if (result < 0)
3040 RETPUSHUNDEF;
146174a9 3041 if (PL_statcache.st_size == 0)
a0d0e21e
LW
3042 RETPUSHYES;
3043 RETPUSHNO;
3044}
3045
3046PP(pp_ftsize)
3047{
9cad6237 3048 I32 result = my_stat();
2a3ff820 3049 dSP; dTARGET;
a0d0e21e
LW
3050 if (result < 0)
3051 RETPUSHUNDEF;
146174a9
CB
3052#if Off_t_size > IVSIZE
3053 PUSHn(PL_statcache.st_size);
3054#else
3280af22 3055 PUSHi(PL_statcache.st_size);
146174a9 3056#endif
a0d0e21e
LW
3057 RETURN;
3058}
3059
3060PP(pp_ftmtime)
3061{
9cad6237 3062 I32 result = my_stat();
2a3ff820 3063 dSP; dTARGET;
a0d0e21e
LW
3064 if (result < 0)
3065 RETPUSHUNDEF;
c6419e06 3066 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
a0d0e21e
LW
3067 RETURN;
3068}
3069
3070PP(pp_ftatime)
3071{
9cad6237 3072 I32 result = my_stat();
2a3ff820 3073 dSP; dTARGET;
a0d0e21e
LW
3074 if (result < 0)
3075 RETPUSHUNDEF;
c6419e06 3076 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
a0d0e21e
LW
3077 RETURN;
3078}
3079
3080PP(pp_ftctime)
3081{
9cad6237 3082 I32 result = my_stat();
2a3ff820 3083 dSP; dTARGET;
a0d0e21e
LW
3084 if (result < 0)
3085 RETPUSHUNDEF;
c6419e06 3086 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
a0d0e21e
LW
3087 RETURN;
3088}
3089
3090PP(pp_ftsock)
3091{
9cad6237 3092 I32 result = my_stat();
2a3ff820 3093 dSP;
a0d0e21e
LW
3094 if (result < 0)
3095 RETPUSHUNDEF;
3280af22 3096 if (S_ISSOCK(PL_statcache.st_mode))
a0d0e21e
LW
3097 RETPUSHYES;
3098 RETPUSHNO;
3099}
3100
3101PP(pp_ftchr)
3102{
9cad6237 3103 I32 result = my_stat();
2a3ff820 3104 dSP;
a0d0e21e
LW
3105 if (result < 0)
3106 RETPUSHUNDEF;
3280af22 3107 if (S_ISCHR(PL_statcache.st_mode))
a0d0e21e
LW
3108 RETPUSHYES;
3109 RETPUSHNO;
3110}
3111
3112PP(pp_ftblk)
3113{
9cad6237 3114 I32 result = my_stat();
2a3ff820 3115 dSP;
a0d0e21e
LW
3116 if (result < 0)
3117 RETPUSHUNDEF;
3280af22 3118 if (S_ISBLK(PL_statcache.st_mode))
a0d0e21e
LW
3119 RETPUSHYES;
3120 RETPUSHNO;
3121}
3122
3123PP(pp_ftfile)
3124{
9cad6237 3125 I32 result = my_stat();
2a3ff820 3126 dSP;
a0d0e21e
LW
3127 if (result < 0)
3128 RETPUSHUNDEF;
3280af22 3129 if (S_ISREG(PL_statcache.st_mode))
a0d0e21e
LW
3130 RETPUSHYES;
3131 RETPUSHNO;
3132}
3133
3134PP(pp_ftdir)
3135{
9cad6237 3136 I32 result = my_stat();
2a3ff820 3137 dSP;
a0d0e21e
LW
3138 if (result < 0)
3139 RETPUSHUNDEF;
3280af22 3140 if (S_ISDIR(PL_statcache.st_mode))
a0d0e21e
LW
3141 RETPUSHYES;
3142 RETPUSHNO;
3143}
3144
3145PP(pp_ftpipe)
3146{
9cad6237 3147 I32 result = my_stat();
2a3ff820 3148 dSP;
a0d0e21e
LW
3149 if (result < 0)
3150 RETPUSHUNDEF;
3280af22 3151 if (S_ISFIFO(PL_statcache.st_mode))
a0d0e21e
LW
3152 RETPUSHYES;
3153 RETPUSHNO;
3154}
3155
3156PP(pp_ftlink)
3157{
9cad6237 3158 I32 result = my_lstat();
2a3ff820 3159 dSP;
a0d0e21e
LW
3160 if (result < 0)
3161 RETPUSHUNDEF;
3280af22 3162 if (S_ISLNK(PL_statcache.st_mode))
a0d0e21e
LW
3163 RETPUSHYES;
3164 RETPUSHNO;
3165}
3166
3167PP(pp_ftsuid)
3168{
39644a26 3169 dSP;
a0d0e21e 3170#ifdef S_ISUID
cea2e8a9 3171 I32 result = my_stat();
a0d0e21e
LW
3172 SPAGAIN;
3173 if (result < 0)
3174 RETPUSHUNDEF;
3280af22 3175 if (PL_statcache.st_mode & S_ISUID)
a0d0e21e
LW
3176 RETPUSHYES;
3177#endif
3178 RETPUSHNO;
3179}
3180
3181PP(pp_ftsgid)
3182{
39644a26 3183 dSP;
a0d0e21e 3184#ifdef S_ISGID
cea2e8a9 3185 I32 result = my_stat();
a0d0e21e
LW
3186 SPAGAIN;
3187 if (result < 0)
3188 RETPUSHUNDEF;
3280af22 3189 if (PL_statcache.st_mode & S_ISGID)
a0d0e21e
LW
3190 RETPUSHYES;
3191#endif
3192 RETPUSHNO;
3193}
3194
3195PP(pp_ftsvtx)
3196{
39644a26 3197 dSP;
a0d0e21e 3198#ifdef S_ISVTX
cea2e8a9 3199 I32 result = my_stat();
a0d0e21e
LW
3200 SPAGAIN;
3201 if (result < 0)
3202 RETPUSHUNDEF;
3280af22 3203 if (PL_statcache.st_mode & S_ISVTX)
a0d0e21e
LW
3204 RETPUSHYES;
3205#endif
3206 RETPUSHNO;
3207}
3208
3209PP(pp_fttty)
3210{
39644a26 3211 dSP;
a0d0e21e
LW
3212 int fd;
3213 GV *gv;
fb73857a 3214 char *tmps = Nullch;
2d8e6c8d 3215 STRLEN n_a;
fb73857a 3216
533c011a 3217 if (PL_op->op_flags & OPf_REF)
146174a9 3218 gv = cGVOP_gv;
fb73857a
PP
3219 else if (isGV(TOPs))
3220 gv = (GV*)POPs;
3221 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3222 gv = (GV*)SvRV(POPs);
a0d0e21e 3223 else
2d8e6c8d 3224 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
fb73857a 3225
a0d0e21e 3226 if (GvIO(gv) && IoIFP(GvIOp(gv)))
760ac839 3227 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
fb73857a 3228 else if (tmps && isDIGIT(*tmps))
a0d0e21e
LW
3229 fd = atoi(tmps);
3230 else
3231 RETPUSHUNDEF;
6ad3d225 3232 if (PerlLIO_isatty(fd))
a0d0e21e
LW
3233 RETPUSHYES;
3234 RETPUSHNO;
3235}
3236
16d20bd9
AD
3237#if defined(atarist) /* this will work with atariST. Configure will
3238 make guesses for other systems. */
3239# define FILE_base(f) ((f)->_base)
3240# define FILE_ptr(f) ((f)->_ptr)
3241# define FILE_cnt(f) ((f)->_cnt)
3242# define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
a0d0e21e
LW
3243#endif
3244
3245PP(pp_fttext)
3246{
39644a26 3247 dSP;
a0d0e21e
LW
3248 I32 i;
3249 I32 len;
3250 I32 odd = 0;
3251 STDCHAR tbuf[512];
3252 register STDCHAR *s;
3253 register IO *io;
5f05dabc
PP
3254 register SV *sv;
3255 GV *gv;
2d8e6c8d 3256 STRLEN n_a;
146174a9 3257 PerlIO *fp;
a0d0e21e 3258
533c011a 3259 if (PL_op->op_flags & OPf_REF)
146174a9 3260 gv = cGVOP_gv;
5f05dabc
PP
3261 else if (isGV(TOPs))
3262 gv = (GV*)POPs;
3263 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3264 gv = (GV*)SvRV(POPs);
3265 else
3266 gv = Nullgv;
3267
3268 if (gv) {
a0d0e21e 3269 EXTEND(SP, 1);
3280af22
NIS
3270 if (gv == PL_defgv) {
3271 if (PL_statgv)
3272 io = GvIO(PL_statgv);
a0d0e21e 3273 else {
3280af22 3274 sv = PL_statname;
a0d0e21e
LW
3275 goto really_filename;
3276 }
3277 }
3278 else {
3280af22
NIS
3279 PL_statgv = gv;
3280 PL_laststatval = -1;
3281 sv_setpv(PL_statname, "");
3282 io = GvIO(PL_statgv);
a0d0e21e
LW
3283 }
3284 if (io && IoIFP(io)) {
5f05dabc 3285 if (! PerlIO_has_base(IoIFP(io)))
cea2e8a9 3286 DIE(aTHX_ "-T and -B not implemented on filehandles");
3280af22
NIS
3287 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3288 if (PL_laststatval < 0)
5f05dabc 3289 RETPUSHUNDEF;
9cbac4c7 3290 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
533c011a 3291 if (PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3292 RETPUSHNO;
3293 else
3294 RETPUSHYES;
9cbac4c7 3295 }
a20bf0c3 3296 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
760ac839 3297 i = PerlIO_getc(IoIFP(io));
a0d0e21e 3298 if (i != EOF)
760ac839 3299 (void)PerlIO_ungetc(IoIFP(io),i);
a0d0e21e 3300 }
a20bf0c3 3301 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
a0d0e21e 3302 RETPUSHYES;
a20bf0c3
JH
3303 len = PerlIO_get_bufsiz(IoIFP(io));
3304 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
760ac839
LW
3305 /* sfio can have large buffers - limit to 512 */
3306 if (len > 512)
3307 len = 512;
a0d0e21e
LW
3308 }
3309 else {
2dd78f96 3310 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
146174a9 3311 gv = cGVOP_gv;
2dd78f96 3312 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
146174a9 3313 }
93189314 3314 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
3315 RETPUSHUNDEF;
3316 }
3317 }
3318 else {
3319 sv = POPs;
5f05dabc 3320 really_filename:
3280af22
NIS
3321 PL_statgv = Nullgv;
3322 PL_laststatval = -1;
5c9aa243 3323 PL_laststype = OP_STAT;
2d8e6c8d 3324 sv_setpv(PL_statname, SvPV(sv, n_a));
146174a9 3325 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
2d8e6c8d 3326 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
9014280d 3327 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
a0d0e21e
LW
3328 RETPUSHUNDEF;
3329 }
146174a9
CB
3330 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3331 if (PL_laststatval < 0) {
3332 (void)PerlIO_close(fp);
5f05dabc 3333 RETPUSHUNDEF;
146174a9 3334 }
60382766 3335 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
146174a9
CB
3336 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3337 (void)PerlIO_close(fp);
a0d0e21e 3338 if (len <= 0) {
533c011a 3339 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3340 RETPUSHNO; /* special case NFS directories */
3341 RETPUSHYES; /* null file is anything */
3342 }
3343 s = tbuf;
3344 }
3345
3346 /* now scan s to look for textiness */
4633a7c4 3347 /* XXX ASCII dependent code */
a0d0e21e 3348
146174a9
CB
3349#if defined(DOSISH) || defined(USEMYBINMODE)
3350 /* ignore trailing ^Z on short files */
3351 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3352 --len;
3353#endif
3354
a0d0e21e
LW
3355 for (i = 0; i < len; i++, s++) {
3356 if (!*s) { /* null never allowed in text */
3357 odd += len;
3358 break;
3359 }
9d116dd7 3360#ifdef EBCDIC
301e8125 3361 else if (!(isPRINT(*s) || isSPACE(*s)))
9d116dd7
JH
3362 odd++;
3363#else
146174a9
CB
3364 else if (*s & 128) {
3365#ifdef USE_LOCALE
2de3dbcc 3366 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
b3f66c68
GS
3367 continue;
3368#endif
3369 /* utf8 characters don't count as odd */
fd400ab9 3370 if (UTF8_IS_START(*s)) {
b3f66c68
GS
3371 int ulen = UTF8SKIP(s);
3372 if (ulen < len - i) {
3373 int j;
3374 for (j = 1; j < ulen; j++) {
fd400ab9 3375 if (!UTF8_IS_CONTINUATION(s[j]))
b3f66c68
GS
3376 goto not_utf8;
3377 }
3378 --ulen; /* loop does extra increment */
3379 s += ulen;
3380 i += ulen;
3381 continue;
3382 }
3383 }
3384 not_utf8:
3385 odd++;
146174a9 3386 }
a0d0e21e
LW
3387 else if (*s < 32 &&
3388 *s != '\n' && *s != '\r' && *s != '\b' &&
3389 *s != '\t' && *s != '\f' && *s != 27)
3390 odd++;
9d116dd7 3391#endif
a0d0e21e
LW
3392 }
3393
533c011a 3394 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
a0d0e21e
LW
3395 RETPUSHNO;
3396 else
3397 RETPUSHYES;
3398}
3399
3400PP(pp_ftbinary)
3401{
cea2e8a9 3402 return pp_fttext();
a0d0e21e
LW
3403}
3404
3405/* File calls. */
3406
3407PP(pp_chdir)
3408{
39644a26 3409 dSP; dTARGET;
a0d0e21e
LW
3410 char *tmps;
3411 SV **svp;
2d8e6c8d 3412 STRLEN n_a;
a0d0e21e 3413
35ae6b54
MS
3414 if( MAXARG == 1 )
3415 tmps = POPpx;
3416 else
3417 tmps = 0;
3418
3419 if( !tmps || !*tmps ) {
3420 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3421 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
491527d0 3422#ifdef VMS
35ae6b54 3423 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
491527d0 3424#endif
35ae6b54
MS
3425 )
3426 {
3427 if( MAXARG == 1 )
9014280d 3428 deprecate("chdir('') or chdir(undef) as chdir()");
35ae6b54
MS
3429 tmps = SvPV(*svp, n_a);
3430 }
72f496dc 3431 else {
389ec635 3432 PUSHi(0);
b7ab37f8 3433 TAINT_PROPER("chdir");
389ec635
MS
3434 RETURN;
3435 }
8ea155d1 3436 }
8ea155d1 3437
a0d0e21e 3438 TAINT_PROPER("chdir");
6ad3d225 3439 PUSHi( PerlDir_chdir(tmps) >= 0 );
748a9306
LW
3440#ifdef VMS
3441 /* Clear the DEFAULT element of ENV so we'll get the new value
3442 * in the future. */
6b88bc9c 3443 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
748a9306 3444#endif
a0d0e21e
LW
3445 RETURN;
3446}
3447
3448PP(pp_chown)
3449{
a0d0e21e 3450#ifdef HAS_CHOWN
76ffd3b9
IZ
3451 dSP; dMARK; dTARGET;
3452 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3453
a0d0e21e
LW
3454 SP = MARK;
3455 PUSHi(value);
3456 RETURN;
3457#else
0322a713 3458 DIE(aTHX_ PL_no_func, "chown");
a0d0e21e
LW
3459#endif
3460}
3461
3462PP(pp_chroot)
3463{
a0d0e21e 3464#ifdef HAS_CHROOT
76ffd3b9 3465 dSP; dTARGET;
2d8e6c8d 3466 STRLEN n_a;
d05c1ba0 3467 char *tmps = POPpx;
a0d0e21e
LW
3468 TAINT_PROPER("chroot");
3469 PUSHi( chroot(tmps) >= 0 );
3470 RETURN;
3471#else
cea2e8a9 3472 DIE(aTHX_ PL_no_func, "chroot");
a0d0e21e
LW
3473#endif
3474}
3475
3476PP(pp_unlink)
3477{
39644a26 3478 dSP; dMARK; dTARGET;
a0d0e21e 3479 I32 value;
533c011a 3480 value = (I32)apply(PL_op->op_type, MARK, SP);
a0d0e21e
LW
3481 SP = MARK;
3482 PUSHi(value);
3483 RETURN;
3484}
3485
3486PP(pp_chmod)
3487{
39644a26 3488 dSP; dMARK; dTARGET;
a0d0e21e 3489 I32 value;
533c011a 3490 value = (I32)apply(PL_op->op_type, MARK, SP);
a0d0e21e
LW
3491 SP = MARK;
3492 PUSHi(value);
3493 RETURN;
3494}
3495
3496PP(pp_utime)
3497{
39644a26 3498 dSP; dMARK; dTARGET;
a0d0e21e 3499 I32 value;
533c011a 3500 value = (I32)apply(PL_op->op_type, MARK, SP);
a0d0e21e
LW
3501 SP = MARK;
3502 PUSHi(value);
3503 RETURN;
3504}
3505
3506PP(pp_rename)
3507{
39644a26 3508 dSP; dTARGET;
a0d0e21e 3509 int anum;
2d8e6c8d 3510 STRLEN n_a;
a0d0e21e 3511
2d8e6c8d
GS
3512 char *tmps2 = POPpx;
3513 char *tmps = SvPV(TOPs, n_a);
a0d0e21e
LW
3514 TAINT_PROPER("rename");
3515#ifdef HAS_RENAME
baed7233 3516