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