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