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