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