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