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