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