This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge the implementation of B::{main_root,main_start} using ALIAS.
[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
13be902c 508 if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(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:
13be902c 828 case SVt_PVLV:
9aa8b008 829 if (isGV_with_GP(varsv) && !SvFAKE(varsv)) {
6e592b3a
BM
830 methname = "TIEHANDLE";
831 how = PERL_MAGIC_tiedscalar;
832 /* For tied filehandles, we apply tiedscalar magic to the IO
833 slot of the GP rather than the GV itself. AMS 20010812 */
834 if (!GvIOp(varsv))
835 GvIOp(varsv) = newIO();
ad64d0ec 836 varsv = MUTABLE_SV(GvIOp(varsv));
6e592b3a
BM
837 break;
838 }
839 /* FALL THROUGH */
6b05c17a
NIS
840 default:
841 methname = "TIESCALAR";
14befaf4 842 how = PERL_MAGIC_tiedscalar;
6b05c17a
NIS
843 break;
844 }
e336de0d 845 items = SP - MARK++;
a91d1d42 846 if (sv_isobject(*MARK)) { /* Calls GET magic. */
d343c3ef 847 ENTER_with_name("call_TIE");
e788e7d3 848 PUSHSTACKi(PERLSI_MAGIC);
e336de0d 849 PUSHMARK(SP);
eb160463 850 EXTEND(SP,(I32)items);
e336de0d
GS
851 while (items--)
852 PUSHs(*MARK++);
853 PUTBACK;
864dbfa3 854 call_method(methname, G_SCALAR);
301e8125 855 }
6b05c17a 856 else {
086d2913
NC
857 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
858 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
859 * wrong error message, and worse case, supreme action at a distance.
860 * (Sorry obfuscation writers. You're not going to be given this one.)
6b05c17a 861 */
a91d1d42
VP
862 STRLEN len;
863 const char *name = SvPV_nomg_const(*MARK, len);
864 stash = gv_stashpvn(name, len, 0);
6b05c17a 865 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
35c1215d 866 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
a91d1d42 867 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
6b05c17a 868 }
d343c3ef 869 ENTER_with_name("call_TIE");
e788e7d3 870 PUSHSTACKi(PERLSI_MAGIC);
e336de0d 871 PUSHMARK(SP);
eb160463 872 EXTEND(SP,(I32)items);
e336de0d
GS
873 while (items--)
874 PUSHs(*MARK++);
875 PUTBACK;
ad64d0ec 876 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
6b05c17a 877 }
a0d0e21e
LW
878 SPAGAIN;
879
880 sv = TOPs;
d3acc0f7 881 POPSTACK;
a0d0e21e 882 if (sv_isobject(sv)) {
33c27489 883 sv_unmagic(varsv, how);
ae21d580 884 /* Croak if a self-tie on an aggregate is attempted. */
b881518d 885 if (varsv == SvRV(sv) &&
d87ebaca
YST
886 (SvTYPE(varsv) == SVt_PVAV ||
887 SvTYPE(varsv) == SVt_PVHV))
ae21d580
JH
888 Perl_croak(aTHX_
889 "Self-ties of arrays and hashes are not supported");
a0714e2c 890 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
a0d0e21e 891 }
d343c3ef 892 LEAVE_with_name("call_TIE");
3280af22 893 SP = PL_stack_base + markoff;
a0d0e21e
LW
894 PUSHs(sv);
895 RETURN;
896}
897
898PP(pp_untie)
899{
27da23d5 900 dVAR; dSP;
5b468f54 901 MAGIC *mg;
33c27489 902 SV *sv = POPs;
1df70142 903 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
14befaf4 904 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
55497cff 905
83074801 906 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
5b468f54
AMS
907 RETPUSHYES;
908
65eba18f 909 if ((mg = SvTIED_mg(sv, how))) {
1b6737cc 910 SV * const obj = SvRV(SvTIED_obj(sv, mg));
fa2b88e0 911 if (obj) {
c4420975 912 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
0bd48802 913 CV *cv;
c4420975 914 if (gv && isGV(gv) && (cv = GvCV(gv))) {
fa2b88e0 915 PUSHMARK(SP);
c33ef3ac 916 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
6e449a3a 917 mXPUSHi(SvREFCNT(obj) - 1);
fa2b88e0 918 PUTBACK;
d343c3ef 919 ENTER_with_name("call_UNTIE");
ad64d0ec 920 call_sv(MUTABLE_SV(cv), G_VOID);
d343c3ef 921 LEAVE_with_name("call_UNTIE");
fa2b88e0
JS
922 SPAGAIN;
923 }
a2a5de95
NC
924 else if (mg && SvREFCNT(obj) > 1) {
925 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
926 "untie attempted while %"UVuf" inner references still exist",
927 (UV)SvREFCNT(obj) - 1 ) ;
c4420975 928 }
cbdc8872
PP
929 }
930 }
38193a09 931 sv_unmagic(sv, how) ;
55497cff 932 RETPUSHYES;
a0d0e21e
LW
933}
934
c07a80fd
PP
935PP(pp_tied)
936{
97aff369 937 dVAR;
39644a26 938 dSP;
1b6737cc 939 const MAGIC *mg;
33c27489 940 SV *sv = POPs;
1df70142 941 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
14befaf4 942 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
5b468f54 943
8752206e 944 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
5b468f54 945 RETPUSHUNDEF;
c07a80fd 946
155aba94 947 if ((mg = SvTIED_mg(sv, how))) {
33c27489
GS
948 SV *osv = SvTIED_obj(sv, mg);
949 if (osv == mg->mg_obj)
950 osv = sv_mortalcopy(osv);
951 PUSHs(osv);
952 RETURN;
c07a80fd 953 }
c07a80fd
PP
954 RETPUSHUNDEF;
955}
956
a0d0e21e
LW
957PP(pp_dbmopen)
958{
27da23d5 959 dVAR; dSP;
a0d0e21e
LW
960 dPOPPOPssrl;
961 HV* stash;
07822e36 962 GV *gv = NULL;
a0d0e21e 963
85fbaab2 964 HV * const hv = MUTABLE_HV(POPs);
84bafc02 965 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
da51bb9b 966 stash = gv_stashsv(sv, 0);
8ebc5c01 967 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
a0d0e21e 968 PUTBACK;
864dbfa3 969 require_pv("AnyDBM_File.pm");
a0d0e21e 970 SPAGAIN;
eff494dd 971 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
cea2e8a9 972 DIE(aTHX_ "No dbm on this machine");
a0d0e21e
LW
973 }
974
57d3b86d 975 ENTER;
924508f0 976 PUSHMARK(SP);
6b05c17a 977
924508f0 978 EXTEND(SP, 5);
a0d0e21e
LW
979 PUSHs(sv);
980 PUSHs(left);
981 if (SvIV(right))
6e449a3a 982 mPUSHu(O_RDWR|O_CREAT);
a0d0e21e 983 else
6e449a3a 984 mPUSHu(O_RDWR);
a0d0e21e 985 PUSHs(right);
57d3b86d 986 PUTBACK;
ad64d0ec 987 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
a0d0e21e
LW
988 SPAGAIN;
989
990 if (!sv_isobject(TOPs)) {
924508f0
GS
991 SP--;
992 PUSHMARK(SP);
a0d0e21e
LW
993 PUSHs(sv);
994 PUSHs(left);
6e449a3a 995 mPUSHu(O_RDONLY);
a0d0e21e 996 PUSHs(right);
a0d0e21e 997 PUTBACK;
ad64d0ec 998 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
a0d0e21e
LW
999 SPAGAIN;
1000 }
1001
6b05c17a 1002 if (sv_isobject(TOPs)) {
ad64d0ec
NC
1003 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1004 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
6b05c17a 1005 }
a0d0e21e
LW
1006 LEAVE;
1007 RETURN;
1008}
1009
a0d0e21e
LW
1010PP(pp_sselect)
1011{
a0d0e21e 1012#ifdef HAS_SELECT
97aff369 1013 dVAR; dSP; dTARGET;
a0d0e21e
LW
1014 register I32 i;
1015 register I32 j;
1016 register char *s;
1017 register SV *sv;
65202027 1018 NV value;
a0d0e21e
LW
1019 I32 maxlen = 0;
1020 I32 nfound;
1021 struct timeval timebuf;
1022 struct timeval *tbuf = &timebuf;
1023 I32 growsize;
1024 char *fd_sets[4];
1025#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1026 I32 masksize;
1027 I32 offset;
1028 I32 k;
1029
1030# if BYTEORDER & 0xf0000
1031# define ORDERBYTE (0x88888888 - BYTEORDER)
1032# else
1033# define ORDERBYTE (0x4444 - BYTEORDER)
1034# endif
1035
1036#endif
1037
1038 SP -= 4;
1039 for (i = 1; i <= 3; i++) {
c4420975 1040 SV * const sv = SP[i];
15547071
GA
1041 if (!SvOK(sv))
1042 continue;
1043 if (SvREADONLY(sv)) {
729c079f
NC
1044 if (SvIsCOW(sv))
1045 sv_force_normal_flags(sv, 0);
15547071 1046 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
6ad8f254 1047 Perl_croak_no_modify(aTHX);
729c079f 1048 }
4ef2275c 1049 if (!SvPOK(sv)) {
a2a5de95 1050 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
4ef2275c
GA
1051 SvPV_force_nolen(sv); /* force string conversion */
1052 }
729c079f 1053 j = SvCUR(sv);
a0d0e21e
LW
1054 if (maxlen < j)
1055 maxlen = j;
1056 }
1057
5ff3f7a4 1058/* little endians can use vecs directly */
e366b469 1059#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
5ff3f7a4 1060# ifdef NFDBITS
a0d0e21e 1061
5ff3f7a4
GS
1062# ifndef NBBY
1063# define NBBY 8
1064# endif
a0d0e21e
LW
1065
1066 masksize = NFDBITS / NBBY;
5ff3f7a4 1067# else
a0d0e21e 1068 masksize = sizeof(long); /* documented int, everyone seems to use long */
5ff3f7a4 1069# endif
a0d0e21e
LW
1070 Zero(&fd_sets[0], 4, char*);
1071#endif
1072
ad517f75
MHM
1073# if SELECT_MIN_BITS == 1
1074 growsize = sizeof(fd_set);
1075# else
1076# if defined(__GLIBC__) && defined(__FD_SETSIZE)
1077# undef SELECT_MIN_BITS
1078# define SELECT_MIN_BITS __FD_SETSIZE
1079# endif
e366b469
PG
1080 /* If SELECT_MIN_BITS is greater than one we most probably will want
1081 * to align the sizes with SELECT_MIN_BITS/8 because for example
1082 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1083 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1084 * on (sets/tests/clears bits) is 32 bits. */
1085 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
e366b469
PG
1086# endif
1087
a0d0e21e
LW
1088 sv = SP[4];
1089 if (SvOK(sv)) {
1090 value = SvNV(sv);
1091 if (value < 0.0)
1092 value = 0.0;
1093 timebuf.tv_sec = (long)value;
65202027 1094 value -= (NV)timebuf.tv_sec;
a0d0e21e
LW
1095 timebuf.tv_usec = (long)(value * 1000000.0);
1096 }
1097 else
4608196e 1098 tbuf = NULL;
a0d0e21e
LW
1099
1100 for (i = 1; i <= 3; i++) {
1101 sv = SP[i];
15547071 1102 if (!SvOK(sv) || SvCUR(sv) == 0) {
a0d0e21e
LW
1103 fd_sets[i] = 0;
1104 continue;
1105 }
4ef2275c 1106 assert(SvPOK(sv));
a0d0e21e
LW
1107 j = SvLEN(sv);
1108 if (j < growsize) {
1109 Sv_Grow(sv, growsize);
a0d0e21e 1110 }
c07a80fd
PP
1111 j = SvCUR(sv);
1112 s = SvPVX(sv) + j;
1113 while (++j <= growsize) {
1114 *s++ = '\0';
1115 }
1116
a0d0e21e
LW
1117#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1118 s = SvPVX(sv);
a02a5408 1119 Newx(fd_sets[i], growsize, char);
a0d0e21e
LW
1120 for (offset = 0; offset < growsize; offset += masksize) {
1121 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1122 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1123 }
1124#else
1125 fd_sets[i] = SvPVX(sv);
1126#endif
1127 }
1128
dc4c69d9
JH
1129#ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1130 /* Can't make just the (void*) conditional because that would be
1131 * cpp #if within cpp macro, and not all compilers like that. */
1132 nfound = PerlSock_select(
1133 maxlen * 8,
1134 (Select_fd_set_t) fd_sets[1],
1135 (Select_fd_set_t) fd_sets[2],
1136 (Select_fd_set_t) fd_sets[3],
1137 (void*) tbuf); /* Workaround for compiler bug. */
1138#else
6ad3d225 1139 nfound = PerlSock_select(
a0d0e21e
LW
1140 maxlen * 8,
1141 (Select_fd_set_t) fd_sets[1],
1142 (Select_fd_set_t) fd_sets[2],
1143 (Select_fd_set_t) fd_sets[3],
1144 tbuf);
dc4c69d9 1145#endif
a0d0e21e
LW
1146 for (i = 1; i <= 3; i++) {
1147 if (fd_sets[i]) {
1148 sv = SP[i];
1149#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1150 s = SvPVX(sv);
1151 for (offset = 0; offset < growsize; offset += masksize) {
1152 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1153 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1154 }
1155 Safefree(fd_sets[i]);
1156#endif
1157 SvSETMAGIC(sv);
1158 }
1159 }
1160
4189264e 1161 PUSHi(nfound);
a0d0e21e 1162 if (GIMME == G_ARRAY && tbuf) {
65202027
DS
1163 value = (NV)(timebuf.tv_sec) +
1164 (NV)(timebuf.tv_usec) / 1000000.0;
6e449a3a 1165 mPUSHn(value);
a0d0e21e
LW
1166 }
1167 RETURN;
1168#else
cea2e8a9 1169 DIE(aTHX_ "select not implemented");
a0d0e21e
LW
1170#endif
1171}
1172
8226a3d7
NC
1173/*
1174=for apidoc setdefout
1175
1176Sets PL_defoutgv, the default file handle for output, to the passed in
1177typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1178count of the passed in typeglob is increased by one, and the reference count
1179of the typeglob that PL_defoutgv points to is decreased by one.
1180
1181=cut
1182*/
1183
4633a7c4 1184void
864dbfa3 1185Perl_setdefout(pTHX_ GV *gv)
4633a7c4 1186{
97aff369 1187 dVAR;
b37c2d43 1188 SvREFCNT_inc_simple_void(gv);
ef8d46e8 1189 SvREFCNT_dec(PL_defoutgv);
3280af22 1190 PL_defoutgv = gv;
4633a7c4
LW
1191}
1192
a0d0e21e
LW
1193PP(pp_select)
1194{
97aff369 1195 dVAR; dSP; dTARGET;
4633a7c4 1196 HV *hv;
159b6efe 1197 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
099be4f1 1198 GV * egv = GvEGVx(PL_defoutgv);
4633a7c4 1199
4633a7c4 1200 if (!egv)
3280af22 1201 egv = PL_defoutgv;
099be4f1 1202 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
4633a7c4 1203 if (! hv)
3280af22 1204 XPUSHs(&PL_sv_undef);
4633a7c4 1205 else {
c4420975 1206 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
f86702cc 1207 if (gvp && *gvp == egv) {
bd61b366 1208 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
f86702cc
PP
1209 XPUSHTARG;
1210 }
1211 else {
ad64d0ec 1212 mXPUSHs(newRV(MUTABLE_SV(egv)));
f86702cc 1213 }
4633a7c4
LW
1214 }
1215
1216 if (newdefout) {
ded8aa31
GS
1217 if (!GvIO(newdefout))
1218 gv_IOadd(newdefout);
4633a7c4
LW
1219 setdefout(newdefout);
1220 }
1221
a0d0e21e
LW
1222 RETURN;
1223}
1224
1225PP(pp_getc)
1226{
27da23d5 1227 dVAR; dSP; dTARGET;
90133b69 1228 IO *io = NULL;
159b6efe 1229 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
2ae324a7 1230
ac3697cd
NC
1231 if (MAXARG == 0)
1232 EXTEND(SP, 1);
1233
a79db61d 1234 if (gv && (io = GvIO(gv))) {
ad64d0ec 1235 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
a79db61d 1236 if (mg) {
0240605e 1237 const U32 gimme = GIMME_V;
f4adce6b 1238 S_tied_handle_method(aTHX_ "GETC", SP, io, mg, gimme);
0240605e
NC
1239 if (gimme == G_SCALAR) {
1240 SPAGAIN;
a79db61d 1241 SvSetMagicSV_nosteal(TARG, TOPs);
0240605e
NC
1242 }
1243 return NORMAL;
a79db61d 1244 }
2ae324a7 1245 }
90133b69 1246 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
041457d9
DM
1247 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1248 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
90133b69 1249 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 1250 SETERRNO(EBADF,RMS_IFI);
a0d0e21e 1251 RETPUSHUNDEF;
90133b69 1252 }
bbce6d69 1253 TAINT;
76f68e9b 1254 sv_setpvs(TARG, " ");
9bc64814 1255 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
7d59b7e4
NIS
1256 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1257 /* Find out how many bytes the char needs */
aa07b2f6 1258 Size_t len = UTF8SKIP(SvPVX_const(TARG));
7d59b7e4
NIS
1259 if (len > 1) {
1260 SvGROW(TARG,len+1);
1261 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1262 SvCUR_set(TARG,1+len);
1263 }
1264 SvUTF8_on(TARG);
1265 }
a0d0e21e
LW
1266 PUSHTARG;
1267 RETURN;
1268}
1269
76e3520e 1270STATIC OP *
cea2e8a9 1271S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
a0d0e21e 1272{
27da23d5 1273 dVAR;
c09156bb 1274 register PERL_CONTEXT *cx;
f54cb97a 1275 const I32 gimme = GIMME_V;
a0d0e21e 1276
7918f24d
NC
1277 PERL_ARGS_ASSERT_DOFORM;
1278
7b190374
NC
1279 if (cv && CvCLONE(cv))
1280 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1281
a0d0e21e
LW
1282 ENTER;
1283 SAVETMPS;
1284
146174a9 1285 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
10067d9a 1286 PUSHFORMAT(cx, retop);
fd617465
DM
1287 SAVECOMPPAD();
1288 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
a0d0e21e 1289
4633a7c4 1290 setdefout(gv); /* locally select filehandle so $% et al work */
a0d0e21e
LW
1291 return CvSTART(cv);
1292}
1293
1294PP(pp_enterwrite)
1295{
97aff369 1296 dVAR;
39644a26 1297 dSP;
a0d0e21e
LW
1298 register GV *gv;
1299 register IO *io;
1300 GV *fgv;
07822e36
JH
1301 CV *cv = NULL;
1302 SV *tmpsv = NULL;
a0d0e21e 1303
2addaaf3 1304 if (MAXARG == 0) {
3280af22 1305 gv = PL_defoutgv;
2addaaf3
NC
1306 EXTEND(SP, 1);
1307 }
a0d0e21e 1308 else {
159b6efe 1309 gv = MUTABLE_GV(POPs);
a0d0e21e 1310 if (!gv)
3280af22 1311 gv = PL_defoutgv;
a0d0e21e 1312 }
a0d0e21e
LW
1313 io = GvIO(gv);
1314 if (!io) {
1315 RETPUSHNO;
1316 }
1317 if (IoFMT_GV(io))
1318 fgv = IoFMT_GV(io);
1319 else
1320 fgv = gv;
1321
a79db61d
AL
1322 if (!fgv)
1323 goto not_a_format_reference;
1324
a0d0e21e 1325 cv = GvFORM(fgv);
a0d0e21e 1326 if (!cv) {
f4a7049d 1327 const char *name;
10edeb5d 1328 tmpsv = sv_newmortal();
f4a7049d
NC
1329 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1330 name = SvPV_nolen_const(tmpsv);
1331 if (name && *name)
1332 DIE(aTHX_ "Undefined format \"%s\" called", name);
a79db61d
AL
1333
1334 not_a_format_reference:
cea2e8a9 1335 DIE(aTHX_ "Not a format reference");
a0d0e21e 1336 }
44a8e56a 1337 IoFLAGS(io) &= ~IOf_DIDTOP;
533c011a 1338 return doform(cv,gv,PL_op->op_next);
a0d0e21e
LW
1339}
1340
1341PP(pp_leavewrite)
1342{
27da23d5 1343 dVAR; dSP;
f9c764c5 1344 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1b6737cc 1345 register IO * const io = GvIOp(gv);
8b8cacda 1346 PerlIO *ofp;
760ac839 1347 PerlIO *fp;
8772537c
AL
1348 SV **newsp;
1349 I32 gimme;
c09156bb 1350 register PERL_CONTEXT *cx;
8f89e5a9 1351 OP *retop;
a0d0e21e 1352
8b8cacda 1353 if (!io || !(ofp = IoOFP(io)))
1354 goto forget_top;
1355
760ac839 1356 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
3280af22 1357 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
8b8cacda 1358
3280af22
NIS
1359 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1360 PL_formtarget != PL_toptarget)
a0d0e21e 1361 {
4633a7c4
LW
1362 GV *fgv;
1363 CV *cv;
a0d0e21e
LW
1364 if (!IoTOP_GV(io)) {
1365 GV *topgv;
a0d0e21e
LW
1366
1367 if (!IoTOP_NAME(io)) {
1b6737cc 1368 SV *topname;
a0d0e21e
LW
1369 if (!IoFMT_NAME(io))
1370 IoFMT_NAME(io) = savepv(GvNAME(gv));
0bd0581c 1371 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
f776e3cd 1372 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
748a9306 1373 if ((topgv && GvFORM(topgv)) ||
fafc274c 1374 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
2e0de35c 1375 IoTOP_NAME(io) = savesvpv(topname);
a0d0e21e 1376 else
89529cee 1377 IoTOP_NAME(io) = savepvs("top");
a0d0e21e 1378 }
f776e3cd 1379 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
a0d0e21e 1380 if (!topgv || !GvFORM(topgv)) {
b929a54b 1381 IoLINES_LEFT(io) = IoPAGE_LEN(io);
a0d0e21e
LW
1382 goto forget_top;
1383 }
1384 IoTOP_GV(io) = topgv;
1385 }
748a9306
LW
1386 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1387 I32 lines = IoLINES_LEFT(io);
504618e9 1388 const char *s = SvPVX_const(PL_formtarget);
8e07c86e
AD
1389 if (lines <= 0) /* Yow, header didn't even fit!!! */
1390 goto forget_top;
748a9306
LW
1391 while (lines-- > 0) {
1392 s = strchr(s, '\n');
1393 if (!s)
1394 break;
1395 s++;
1396 }
1397 if (s) {
f54cb97a 1398 const STRLEN save = SvCUR(PL_formtarget);
aa07b2f6 1399 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
d75029d0
NIS
1400 do_print(PL_formtarget, ofp);
1401 SvCUR_set(PL_formtarget, save);
3280af22
NIS
1402 sv_chop(PL_formtarget, s);
1403 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
748a9306
LW
1404 }
1405 }
a0d0e21e 1406 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
d75029d0 1407 do_print(PL_formfeed, ofp);
a0d0e21e
LW
1408 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1409 IoPAGE(io)++;
3280af22 1410 PL_formtarget = PL_toptarget;
748a9306 1411 IoFLAGS(io) |= IOf_DIDTOP;
4633a7c4
LW
1412 fgv = IoTOP_GV(io);
1413 if (!fgv)
cea2e8a9 1414 DIE(aTHX_ "bad top format reference");
4633a7c4 1415 cv = GvFORM(fgv);
1df70142
AL
1416 if (!cv) {
1417 SV * const sv = sv_newmortal();
b464bac0 1418 const char *name;
bd61b366 1419 gv_efullname4(sv, fgv, NULL, FALSE);
e62f0680 1420 name = SvPV_nolen_const(sv);
2dd78f96 1421 if (name && *name)
0e528f24
JH
1422 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1423 else
1424 DIE(aTHX_ "Undefined top format called");
4633a7c4 1425 }
0e528f24 1426 return doform(cv, gv, PL_op);
a0d0e21e
LW
1427 }
1428
1429 forget_top:
3280af22 1430 POPBLOCK(cx,PL_curpm);
a0d0e21e 1431 POPFORMAT(cx);
8f89e5a9 1432 retop = cx->blk_sub.retop;
a0d0e21e
LW
1433 LEAVE;
1434
1435 fp = IoOFP(io);
1436 if (!fp) {
599cee73 1437 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1438 if (IoIFP(io))
1439 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1440 else if (ckWARN(WARN_CLOSED))
bc37a18f 1441 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1442 }
3280af22 1443 PUSHs(&PL_sv_no);
a0d0e21e
LW
1444 }
1445 else {
3280af22 1446 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
a2a5de95 1447 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
a0d0e21e 1448 }
d75029d0 1449 if (!do_print(PL_formtarget, fp))
3280af22 1450 PUSHs(&PL_sv_no);
a0d0e21e 1451 else {
3280af22
NIS
1452 FmLINES(PL_formtarget) = 0;
1453 SvCUR_set(PL_formtarget, 0);
1454 *SvEND(PL_formtarget) = '\0';
a0d0e21e 1455 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1456 (void)PerlIO_flush(fp);
3280af22 1457 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1458 }
1459 }
9cbac4c7 1460 /* bad_ofp: */
3280af22 1461 PL_formtarget = PL_bodytarget;
a0d0e21e 1462 PUTBACK;
29033a8a
SH
1463 PERL_UNUSED_VAR(newsp);
1464 PERL_UNUSED_VAR(gimme);
8f89e5a9 1465 return retop;
a0d0e21e
LW
1466}
1467
1468PP(pp_prtf)
1469{
27da23d5 1470 dVAR; dSP; dMARK; dORIGMARK;
a0d0e21e 1471 IO *io;
760ac839 1472 PerlIO *fp;
26db47c4 1473 SV *sv;
a0d0e21e 1474
159b6efe
NC
1475 GV * const gv
1476 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
46fc3d4c 1477
a79db61d 1478 if (gv && (io = GvIO(gv))) {
ad64d0ec 1479 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
a79db61d
AL
1480 if (mg) {
1481 if (MARK == ORIGMARK) {
1482 MEXTEND(SP, 1);
1483 ++MARK;
1484 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1485 ++SP;
1486 }
1487 PUSHMARK(MARK - 1);
ad64d0ec 1488 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
a79db61d
AL
1489 PUTBACK;
1490 ENTER;
1491 call_method("PRINTF", G_SCALAR);
1492 LEAVE;
1493 SPAGAIN;
1494 MARK = ORIGMARK + 1;
1495 *MARK = *SP;
1496 SP = MARK;
1497 RETURN;
1498 }
46fc3d4c
PP
1499 }
1500
561b68a9 1501 sv = newSV(0);
a0d0e21e 1502 if (!(io = GvIO(gv))) {
2dd78f96
JH
1503 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1504 report_evil_fh(gv, io, PL_op->op_type);
93189314 1505 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
1506 goto just_say_no;
1507 }
1508 else if (!(fp = IoOFP(io))) {
599cee73 1509 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1510 if (IoIFP(io))
1511 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1512 else if (ckWARN(WARN_CLOSED))
bc37a18f 1513 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1514 }
93189314 1515 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
a0d0e21e
LW
1516 goto just_say_no;
1517 }
1518 else {
20ee07fb
RGS
1519 if (SvTAINTED(MARK[1]))
1520 TAINT_PROPER("printf");
a0d0e21e
LW
1521 do_sprintf(sv, SP - MARK, MARK + 1);
1522 if (!do_print(sv, fp))
1523 goto just_say_no;
1524
1525 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1526 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
1527 goto just_say_no;
1528 }
1529 SvREFCNT_dec(sv);
1530 SP = ORIGMARK;
3280af22 1531 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1532 RETURN;
1533
1534 just_say_no:
1535 SvREFCNT_dec(sv);
1536 SP = ORIGMARK;
3280af22 1537 PUSHs(&PL_sv_undef);
a0d0e21e
LW
1538 RETURN;
1539}
1540
c07a80fd
PP
1541PP(pp_sysopen)
1542{
97aff369 1543 dVAR;
39644a26 1544 dSP;
1df70142
AL
1545 const int perm = (MAXARG > 3) ? POPi : 0666;
1546 const int mode = POPi;
1b6737cc 1547 SV * const sv = POPs;
159b6efe 1548 GV * const gv = MUTABLE_GV(POPs);
1b6737cc 1549 STRLEN len;
c07a80fd 1550
4592e6ca 1551 /* Need TIEHANDLE method ? */
1b6737cc 1552 const char * const tmps = SvPV_const(sv, len);
e62f0680 1553 /* FIXME? do_open should do const */
4608196e 1554 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
c07a80fd 1555 IoLINES(GvIOp(gv)) = 0;
3280af22 1556 PUSHs(&PL_sv_yes);
c07a80fd
PP
1557 }
1558 else {
3280af22 1559 PUSHs(&PL_sv_undef);
c07a80fd
PP
1560 }
1561 RETURN;
1562}
1563
a0d0e21e
LW
1564PP(pp_sysread)
1565{
27da23d5 1566 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e 1567 int offset;
a0d0e21e
LW
1568 IO *io;
1569 char *buffer;
5b54f415 1570 SSize_t length;
eb5c063a 1571 SSize_t count;
1e422769 1572 Sock_size_t bufsize;
748a9306 1573 SV *bufsv;
a0d0e21e 1574 STRLEN blen;
eb5c063a 1575 int fp_utf8;
1dd30107
NC
1576 int buffer_utf8;
1577 SV *read_target;
eb5c063a
NIS
1578 Size_t got = 0;
1579 Size_t wanted;
1d636c13 1580 bool charstart = FALSE;
87330c3c
JH
1581 STRLEN charskip = 0;
1582 STRLEN skip = 0;
a0d0e21e 1583
159b6efe 1584 GV * const gv = MUTABLE_GV(*++MARK);
5b468f54 1585 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1b6737cc 1586 && gv && (io = GvIO(gv)) )
137443ea 1587 {
ad64d0ec 1588 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1b6737cc
AL
1589 if (mg) {
1590 SV *sv;
1591 PUSHMARK(MARK-1);
ad64d0ec 1592 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1b6737cc
AL
1593 ENTER;
1594 call_method("READ", G_SCALAR);
1595 LEAVE;
1596 SPAGAIN;
1597 sv = POPs;
1598 SP = ORIGMARK;
1599 PUSHs(sv);
1600 RETURN;
1601 }
2ae324a7
PP
1602 }
1603
a0d0e21e
LW
1604 if (!gv)
1605 goto say_undef;
748a9306 1606 bufsv = *++MARK;
ff68c719 1607 if (! SvOK(bufsv))
76f68e9b 1608 sv_setpvs(bufsv, "");
a0d0e21e 1609 length = SvIVx(*++MARK);
748a9306 1610 SETERRNO(0,0);
a0d0e21e
LW
1611 if (MARK < SP)
1612 offset = SvIVx(*++MARK);
1613 else
1614 offset = 0;
1615 io = GvIO(gv);
b5fe5ca2
SR
1616 if (!io || !IoIFP(io)) {
1617 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1618 report_evil_fh(gv, io, PL_op->op_type);
1619 SETERRNO(EBADF,RMS_IFI);
a0d0e21e 1620 goto say_undef;
b5fe5ca2 1621 }
0064a8a9 1622 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
7d59b7e4 1623 buffer = SvPVutf8_force(bufsv, blen);
1e54db1a 1624 /* UTF-8 may not have been set if they are all low bytes */
eb5c063a 1625 SvUTF8_on(bufsv);
9b9d7ce8 1626 buffer_utf8 = 0;
7d59b7e4
NIS
1627 }
1628 else {
1629 buffer = SvPV_force(bufsv, blen);
1dd30107 1630 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
7d59b7e4
NIS
1631 }
1632 if (length < 0)
1633 DIE(aTHX_ "Negative length");
eb5c063a 1634 wanted = length;
7d59b7e4 1635
d0965105
JH
1636 charstart = TRUE;
1637 charskip = 0;
87330c3c 1638 skip = 0;
d0965105 1639
a0d0e21e 1640#ifdef HAS_SOCKET
533c011a 1641 if (PL_op->op_type == OP_RECV) {
46fc3d4c 1642 char namebuf[MAXPATHLEN];
17a8c7ba 1643#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
490ab354
JH
1644 bufsize = sizeof (struct sockaddr_in);
1645#else
46fc3d4c 1646 bufsize = sizeof namebuf;
490ab354 1647#endif
abf95952
IZ
1648#ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1649 if (bufsize >= 256)
1650 bufsize = 255;
1651#endif
eb160463 1652 buffer = SvGROW(bufsv, (STRLEN)(length+1));
bbce6d69 1653 /* 'offset' means 'flags' here */
eb5c063a 1654 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
10edeb5d 1655 (struct sockaddr *)namebuf, &bufsize);
eb5c063a 1656 if (count < 0)
a0d0e21e 1657 RETPUSHUNDEF;
4107cc59
OF
1658#ifdef EPOC
1659 /* Bogus return without padding */
1660 bufsize = sizeof (struct sockaddr_in);
1661#endif
eb5c063a 1662 SvCUR_set(bufsv, count);
748a9306
LW
1663 *SvEND(bufsv) = '\0';
1664 (void)SvPOK_only(bufsv);
eb5c063a
NIS
1665 if (fp_utf8)
1666 SvUTF8_on(bufsv);
748a9306 1667 SvSETMAGIC(bufsv);
aac0dd9a 1668 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1669 if (!(IoFLAGS(io) & IOf_UNTAINT))
1670 SvTAINTED_on(bufsv);
a0d0e21e 1671 SP = ORIGMARK;
46fc3d4c 1672 sv_setpvn(TARG, namebuf, bufsize);
a0d0e21e
LW
1673 PUSHs(TARG);
1674 RETURN;
1675 }
1676#else
911d147d 1677 if (PL_op->op_type == OP_RECV)
cea2e8a9 1678 DIE(aTHX_ PL_no_sock_func, "recv");
a0d0e21e 1679#endif
eb5c063a
NIS
1680 if (DO_UTF8(bufsv)) {
1681 /* offset adjust in characters not bytes */
1682 blen = sv_len_utf8(bufsv);
7d59b7e4 1683 }
bbce6d69 1684 if (offset < 0) {
eb160463 1685 if (-offset > (int)blen)
cea2e8a9 1686 DIE(aTHX_ "Offset outside string");
bbce6d69
PP
1687 offset += blen;
1688 }
eb5c063a
NIS
1689 if (DO_UTF8(bufsv)) {
1690 /* convert offset-as-chars to offset-as-bytes */
6960c29a
CH
1691 if (offset >= (int)blen)
1692 offset += SvCUR(bufsv) - blen;
1693 else
1694 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
eb5c063a
NIS
1695 }
1696 more_bytes:
cd52b7b2 1697 bufsize = SvCUR(bufsv);
1dd30107
NC
1698 /* Allocating length + offset + 1 isn't perfect in the case of reading
1699 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1700 unduly.
1701 (should be 2 * length + offset + 1, or possibly something longer if
1702 PL_encoding is true) */
eb160463 1703 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
27da23d5 1704 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
cd52b7b2
PP
1705 Zero(buffer+bufsize, offset-bufsize, char);
1706 }
eb5c063a 1707 buffer = buffer + offset;
1dd30107
NC
1708 if (!buffer_utf8) {
1709 read_target = bufsv;
1710 } else {
1711 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1712 concatenate it to the current buffer. */
1713
1714 /* Truncate the existing buffer to the start of where we will be
1715 reading to: */
1716 SvCUR_set(bufsv, offset);
1717
1718 read_target = sv_newmortal();
862a34c6 1719 SvUPGRADE(read_target, SVt_PV);
fe2774ed 1720 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1dd30107 1721 }
eb5c063a 1722
533c011a 1723 if (PL_op->op_type == OP_SYSREAD) {
a7092146 1724#ifdef PERL_SOCK_SYSREAD_IS_RECV
50952442 1725 if (IoTYPE(io) == IoTYPE_SOCKET) {
eb5c063a
NIS
1726 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1727 buffer, length, 0);
a7092146
GS
1728 }
1729 else
1730#endif
1731 {
eb5c063a
NIS
1732 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1733 buffer, length);
a7092146 1734 }
a0d0e21e
LW
1735 }
1736 else
1737#ifdef HAS_SOCKET__bad_code_maybe
50952442 1738 if (IoTYPE(io) == IoTYPE_SOCKET) {
46fc3d4c 1739 char namebuf[MAXPATHLEN];
490ab354
JH
1740#if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1741 bufsize = sizeof (struct sockaddr_in);
1742#else
46fc3d4c 1743 bufsize = sizeof namebuf;
490ab354 1744#endif
eb5c063a 1745 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
46fc3d4c 1746 (struct sockaddr *)namebuf, &bufsize);
a0d0e21e
LW
1747 }
1748 else
1749#endif
3b02c43c 1750 {
eb5c063a
NIS
1751 count = PerlIO_read(IoIFP(io), buffer, length);
1752 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1753 if (count == 0 && PerlIO_error(IoIFP(io)))
1754 count = -1;
3b02c43c 1755 }
eb5c063a 1756 if (count < 0) {
a00b5bd3 1757 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
6e6ef6b2 1758 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
a0d0e21e 1759 goto say_undef;
af8c498a 1760 }
aa07b2f6 1761 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1dd30107
NC
1762 *SvEND(read_target) = '\0';
1763 (void)SvPOK_only(read_target);
0064a8a9 1764 if (fp_utf8 && !IN_BYTES) {
eb5c063a 1765 /* Look at utf8 we got back and count the characters */
1df70142 1766 const char *bend = buffer + count;
eb5c063a 1767 while (buffer < bend) {
d0965105
JH
1768 if (charstart) {
1769 skip = UTF8SKIP(buffer);
1770 charskip = 0;
1771 }
1772 if (buffer - charskip + skip > bend) {
eb5c063a
NIS
1773 /* partial character - try for rest of it */
1774 length = skip - (bend-buffer);
aa07b2f6 1775 offset = bend - SvPVX_const(bufsv);
d0965105
JH
1776 charstart = FALSE;
1777 charskip += count;
eb5c063a
NIS
1778 goto more_bytes;
1779 }
1780 else {
1781 got++;
1782 buffer += skip;
d0965105
JH
1783 charstart = TRUE;
1784 charskip = 0;
eb5c063a
NIS
1785 }
1786 }
1787 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1788 provided amount read (count) was what was requested (length)
1789 */
1790 if (got < wanted && count == length) {
d0965105 1791 length = wanted - got;
aa07b2f6 1792 offset = bend - SvPVX_const(bufsv);
eb5c063a
NIS
1793 goto more_bytes;
1794 }
1795 /* return value is character count */
1796 count = got;
1797 SvUTF8_on(bufsv);
1798 }
1dd30107
NC
1799 else if (buffer_utf8) {
1800 /* Let svcatsv upgrade the bytes we read in to utf8.
1801 The buffer is a mortal so will be freed soon. */
1802 sv_catsv_nomg(bufsv, read_target);
1803 }
748a9306 1804 SvSETMAGIC(bufsv);
aac0dd9a 1805 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1806 if (!(IoFLAGS(io) & IOf_UNTAINT))
1807 SvTAINTED_on(bufsv);
a0d0e21e 1808 SP = ORIGMARK;
eb5c063a 1809 PUSHi(count);
a0d0e21e
LW
1810 RETURN;
1811
1812 say_undef:
1813 SP = ORIGMARK;
1814 RETPUSHUNDEF;
1815}
1816
a0d0e21e
LW
1817PP(pp_send)
1818{
27da23d5 1819 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e 1820 IO *io;
748a9306 1821 SV *bufsv;
83003860 1822 const char *buffer;
8c99d73e 1823 SSize_t retval;
a0d0e21e 1824 STRLEN blen;
c9cb0f41 1825 STRLEN orig_blen_bytes;
64a1bc8e 1826 const int op_type = PL_op->op_type;
c9cb0f41
NC
1827 bool doing_utf8;
1828 U8 *tmpbuf = NULL;
64a1bc8e 1829
159b6efe 1830 GV *const gv = MUTABLE_GV(*++MARK);
14befaf4 1831 if (PL_op->op_type == OP_SYSWRITE
a79db61d 1832 && gv && (io = GvIO(gv))) {
ad64d0ec 1833 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
a79db61d
AL
1834 if (mg) {
1835 SV *sv;
64a1bc8e 1836
a79db61d 1837 if (MARK == SP - 1) {
9b6474b6
VP
1838 sv = *SP;
1839 mXPUSHi(sv_len(sv));
a79db61d
AL
1840 PUTBACK;
1841 }
1842
1843 PUSHMARK(ORIGMARK);
ad64d0ec 1844 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
a79db61d
AL
1845 ENTER;
1846 call_method("WRITE", G_SCALAR);
1847 LEAVE;
1848 SPAGAIN;
1849 sv = POPs;
1850 SP = ORIGMARK;
64a1bc8e 1851 PUSHs(sv);
a79db61d 1852 RETURN;
64a1bc8e 1853 }
1d603a67 1854 }
a0d0e21e
LW
1855 if (!gv)
1856 goto say_undef;
64a1bc8e 1857
748a9306 1858 bufsv = *++MARK;
64a1bc8e 1859
748a9306 1860 SETERRNO(0,0);
a0d0e21e 1861 io = GvIO(gv);
cf167416 1862 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
8c99d73e 1863 retval = -1;
cf167416
RGS
1864 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1865 if (io && IoIFP(io))
1866 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1867 else
1868 report_evil_fh(gv, io, PL_op->op_type);
1869 }
b5fe5ca2 1870 SETERRNO(EBADF,RMS_IFI);
7d59b7e4
NIS
1871 goto say_undef;
1872 }
1873
c9cb0f41
NC
1874 /* Do this first to trigger any overloading. */
1875 buffer = SvPV_const(bufsv, blen);
1876 orig_blen_bytes = blen;
1877 doing_utf8 = DO_UTF8(bufsv);
1878
7d59b7e4 1879 if (PerlIO_isutf8(IoIFP(io))) {
6aa2f6a7 1880 if (!SvUTF8(bufsv)) {
c9cb0f41
NC
1881 /* We don't modify the original scalar. */
1882 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1883 buffer = (char *) tmpbuf;
1884 doing_utf8 = TRUE;
1885 }
a0d0e21e 1886 }
c9cb0f41
NC
1887 else if (doing_utf8) {
1888 STRLEN tmplen = blen;
a79db61d 1889 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
c9cb0f41
NC
1890 if (!doing_utf8) {
1891 tmpbuf = result;
1892 buffer = (char *) tmpbuf;
1893 blen = tmplen;
1894 }
1895 else {
1896 assert((char *)result == buffer);
1897 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1898 }
7d59b7e4
NIS
1899 }
1900
64a1bc8e 1901 if (op_type == OP_SYSWRITE) {
c9cb0f41
NC
1902 Size_t length = 0; /* This length is in characters. */
1903 STRLEN blen_chars;
7d59b7e4 1904 IV offset;
c9cb0f41
NC
1905
1906 if (doing_utf8) {
1907 if (tmpbuf) {
1908 /* The SV is bytes, and we've had to upgrade it. */
1909 blen_chars = orig_blen_bytes;
1910 } else {
1911 /* The SV really is UTF-8. */
1912 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1913 /* Don't call sv_len_utf8 again because it will call magic
1914 or overloading a second time, and we might get back a
1915 different result. */
9a206dfd 1916 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
c9cb0f41
NC
1917 } else {
1918 /* It's safe, and it may well be cached. */
1919 blen_chars = sv_len_utf8(bufsv);
1920 }
1921 }
1922 } else {
1923 blen_chars = blen;
1924 }
1925
1926 if (MARK >= SP) {
1927 length = blen_chars;
1928 } else {
1929#if Size_t_size > IVSIZE
1930 length = (Size_t)SvNVx(*++MARK);
1931#else
1932 length = (Size_t)SvIVx(*++MARK);
1933#endif
4b0c4b6f
NC
1934 if ((SSize_t)length < 0) {
1935 Safefree(tmpbuf);
c9cb0f41 1936 DIE(aTHX_ "Negative length");
4b0c4b6f 1937 }
7d59b7e4 1938 }
c9cb0f41 1939
bbce6d69 1940 if (MARK < SP) {
a0d0e21e 1941 offset = SvIVx(*++MARK);
bbce6d69 1942 if (offset < 0) {
4b0c4b6f
NC
1943 if (-offset > (IV)blen_chars) {
1944 Safefree(tmpbuf);
cea2e8a9 1945 DIE(aTHX_ "Offset outside string");
4b0c4b6f 1946 }
c9cb0f41 1947 offset += blen_chars;
3c946528 1948 } else if (offset > (IV)blen_chars) {
4b0c4b6f 1949 Safefree(tmpbuf);
cea2e8a9 1950 DIE(aTHX_ "Offset outside string");
4b0c4b6f 1951 }
bbce6d69 1952 } else
a0d0e21e 1953 offset = 0;
c9cb0f41
NC
1954 if (length > blen_chars - offset)
1955 length = blen_chars - offset;
1956 if (doing_utf8) {
1957 /* Here we convert length from characters to bytes. */
1958 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1959 /* Either we had to convert the SV, or the SV is magical, or
1960 the SV has overloading, in which case we can't or mustn't
1961 or mustn't call it again. */
1962
1963 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1964 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1965 } else {
1966 /* It's a real UTF-8 SV, and it's not going to change under
1967 us. Take advantage of any cache. */
1968 I32 start = offset;
1969 I32 len_I32 = length;
1970
1971 /* Convert the start and end character positions to bytes.
1972 Remember that the second argument to sv_pos_u2b is relative
1973 to the first. */
1974 sv_pos_u2b(bufsv, &start, &len_I32);
1975
1976 buffer += start;
1977 length = len_I32;
1978 }
7d59b7e4
NIS
1979 }
1980 else {
1981 buffer = buffer+offset;
1982 }
a7092146 1983#ifdef PERL_SOCK_SYSWRITE_IS_SEND
50952442 1984 if (IoTYPE(io) == IoTYPE_SOCKET) {
8c99d73e 1985 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1986 buffer, length, 0);
a7092146
GS
1987 }
1988 else
1989#endif
1990 {
94e4c244 1991 /* See the note at doio.c:do_print about filesize limits. --jhi */
8c99d73e 1992 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1993 buffer, length);
a7092146 1994 }
a0d0e21e
LW
1995 }
1996#ifdef HAS_SOCKET
64a1bc8e
NC
1997 else {
1998 const int flags = SvIVx(*++MARK);
1999 if (SP > MARK) {
2000 STRLEN mlen;
2001 char * const sockbuf = SvPVx(*++MARK, mlen);
2002 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
2003 flags, (struct sockaddr *)sockbuf, mlen);
2004 }
2005 else {
2006 retval
2007 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
2008 }
a0d0e21e 2009 }
a0d0e21e
LW
2010#else
2011 else
cea2e8a9 2012 DIE(aTHX_ PL_no_sock_func, "send");
a0d0e21e 2013#endif
c9cb0f41 2014
8c99d73e 2015 if (retval < 0)
a0d0e21e
LW
2016 goto say_undef;
2017 SP = ORIGMARK;
c9cb0f41 2018 if (doing_utf8)
f36eea10 2019 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
4b0c4b6f 2020
a79db61d 2021 Safefree(tmpbuf);
8c99d73e
GS
2022#if Size_t_size > IVSIZE
2023 PUSHn(retval);
2024#else
2025 PUSHi(retval);
2026#endif
a0d0e21e
LW
2027 RETURN;
2028
2029 say_undef:
a79db61d 2030 Safefree(tmpbuf);
a0d0e21e
LW
2031 SP = ORIGMARK;
2032 RETPUSHUNDEF;
2033}
2034
a0d0e21e
LW
2035PP(pp_eof)
2036{
27da23d5 2037 dVAR; dSP;
a0d0e21e 2038 GV *gv;
32e65323
CS
2039 IO *io;
2040 MAGIC *mg;
bc0c81ca
NC
2041 /*
2042 * in Perl 5.12 and later, the additional parameter is a bitmask:
2043 * 0 = eof
2044 * 1 = eof(FH)
2045 * 2 = eof() <- ARGV magic
2046 *
2047 * I'll rely on the compiler's trace flow analysis to decide whether to
2048 * actually assign this out here, or punt it into the only block where it is
2049 * used. Doing it out here is DRY on the condition logic.
2050 */
2051 unsigned int which;
a0d0e21e 2052
bc0c81ca 2053 if (MAXARG) {
32e65323 2054 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
bc0c81ca
NC
2055 which = 1;
2056 }
b5f55170
NC
2057 else {
2058 EXTEND(SP, 1);
2059
bc0c81ca 2060 if (PL_op->op_flags & OPf_SPECIAL) {
b5f55170 2061 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
bc0c81ca
NC
2062 which = 2;
2063 }
2064 else {
b5f55170 2065 gv = PL_last_in_gv; /* eof */
bc0c81ca
NC
2066 which = 0;
2067 }
b5f55170 2068 }
32e65323
CS
2069
2070 if (!gv)
2071 RETPUSHNO;
2072
2073 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
74f0b550 2074 return tied_handle_method1("EOF", SP, io, mg, newSVuv(which));
146174a9 2075 }
4592e6ca 2076
32e65323
CS
2077 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2078 if (io && !IoIFP(io)) {
2079 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2080 IoLINES(io) = 0;
2081 IoFLAGS(io) &= ~IOf_START;
2082 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2083 if (GvSV(gv))
2084 sv_setpvs(GvSV(gv), "-");
2085 else
2086 GvSV(gv) = newSVpvs("-");
2087 SvSETMAGIC(GvSV(gv));
2088 }
2089 else if (!nextargv(gv))
2090 RETPUSHYES;
6136c704 2091 }
4592e6ca
NIS
2092 }
2093
32e65323 2094 PUSHs(boolSV(do_eof(gv)));
a0d0e21e
LW
2095 RETURN;
2096}
2097
2098PP(pp_tell)
2099{
27da23d5 2100 dVAR; dSP; dTARGET;
301e8125 2101 GV *gv;
5b468f54 2102 IO *io;
a0d0e21e 2103
c4420975 2104 if (MAXARG != 0)
159b6efe 2105 PL_last_in_gv = MUTABLE_GV(POPs);
ac3697cd
NC
2106 else
2107 EXTEND(SP, 1);
c4420975 2108 gv = PL_last_in_gv;
4592e6ca 2109
a79db61d 2110 if (gv && (io = GvIO(gv))) {
ad64d0ec 2111 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
a79db61d 2112 if (mg) {
ebc1fde6 2113 return tied_handle_method("TELL", SP, io, mg);
a79db61d 2114 }
4592e6ca 2115 }
f4817f32 2116 else if (!gv) {
f03173f2
RGS
2117 if (!errno)
2118 SETERRNO(EBADF,RMS_IFI);
2119 PUSHi(-1);
2120 RETURN;
2121 }
4592e6ca 2122
146174a9
CB
2123#if LSEEKSIZE > IVSIZE
2124 PUSHn( do_tell(gv) );
2125#else
a0d0e21e 2126 PUSHi( do_tell(gv) );
146174a9 2127#endif
a0d0e21e
LW
2128 RETURN;
2129}
2130
137443ea
PP
2131PP(pp_sysseek)
2132{
27da23d5 2133 dVAR; dSP;
1df70142 2134 const int whence = POPi;
146174a9 2135#if LSEEKSIZE > IVSIZE
7452cf6a 2136 const Off_t offset = (Off_t)SvNVx(POPs);
146174a9 2137#else
7452cf6a 2138 const Off_t offset = (Off_t)SvIVx(POPs);
146174a9 2139#endif
a0d0e21e 2140
159b6efe 2141 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
a79db61d 2142 IO *io;
4592e6ca 2143
a79db61d 2144 if (gv && (io = GvIO(gv))) {
ad64d0ec 2145 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
a79db61d 2146 if (mg) {
cb50131a 2147#if LSEEKSIZE > IVSIZE
74f0b550 2148 SV *const offset_sv = newSVnv((NV) offset);
cb50131a 2149#else
74f0b550 2150 SV *const offset_sv = newSViv(offset);
cb50131a 2151#endif
bc0c81ca
NC
2152
2153 return tied_handle_method2("SEEK", SP, io, mg, offset_sv,
74f0b550 2154 newSViv(whence));
a79db61d 2155 }
4592e6ca
NIS
2156 }
2157
533c011a 2158 if (PL_op->op_type == OP_SEEK)
8903cb82
PP
2159 PUSHs(boolSV(do_seek(gv, offset, whence)));
2160 else {
0bcc34c2 2161 const Off_t sought = do_sysseek(gv, offset, whence);
b448e4fe 2162 if (sought < 0)
146174a9
CB
2163 PUSHs(&PL_sv_undef);
2164 else {
7452cf6a 2165 SV* const sv = sought ?
146174a9 2166#if LSEEKSIZE > IVSIZE
b448e4fe 2167 newSVnv((NV)sought)
146174a9 2168#else
b448e4fe 2169 newSViv(sought)
146174a9
CB
2170#endif
2171 : newSVpvn(zero_but_true, ZBTLEN);
6e449a3a 2172 mPUSHs(sv);
146174a9 2173 }
8903cb82 2174 }
a0d0e21e
LW
2175 RETURN;
2176}
2177
2178PP(pp_truncate)
2179{
97aff369 2180 dVAR;
39644a26 2181 dSP;
8c99d73e
GS
2182 /* There seems to be no consensus on the length type of truncate()
2183 * and ftruncate(), both off_t and size_t have supporters. In
2184 * general one would think that when using large files, off_t is
2185 * at least as wide as size_t, so using an off_t should be okay. */
2186 /* XXX Configure probe for the length type of *truncate() needed XXX */
0bcc34c2 2187 Off_t len;
a0d0e21e 2188
25342a55 2189#if Off_t_size > IVSIZE
0bcc34c2 2190 len = (Off_t)POPn;
8c99d73e 2191#else
0bcc34c2 2192 len = (Off_t)POPi;
8c99d73e
GS
2193#endif
2194 /* Checking for length < 0 is problematic as the type might or
301e8125 2195 * might not be signed: if it is not, clever compilers will moan. */
8c99d73e 2196 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
748a9306 2197 SETERRNO(0,0);
d05c1ba0 2198 {
d05c1ba0
JH
2199 int result = 1;
2200 GV *tmpgv;
090bf15b
SR
2201 IO *io;
2202
d05c1ba0 2203 if (PL_op->op_flags & OPf_SPECIAL) {
f776e3cd 2204 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
d05c1ba0 2205
090bf15b
SR
2206 do_ftruncate_gv:
2207 if (!GvIO(tmpgv))
2208 result = 0;
d05c1ba0 2209 else {
090bf15b
SR
2210 PerlIO *fp;
2211 io = GvIOp(tmpgv);
2212 do_ftruncate_io:
2213 TAINT_PROPER("truncate");
2214 if (!(fp = IoIFP(io))) {
2215 result = 0;
2216 }
2217 else {
2218 PerlIO_flush(fp);
cbdc8872 2219#ifdef HAS_TRUNCATE
090bf15b 2220 if (ftruncate(PerlIO_fileno(fp), len) < 0)
301e8125 2221#else
090bf15b 2222 if (my_chsize(PerlIO_fileno(fp), len) < 0)
cbdc8872 2223#endif
090bf15b
SR
2224 result = 0;
2225 }
d05c1ba0 2226 }
cbdc8872 2227 }
d05c1ba0 2228 else {
7452cf6a 2229 SV * const sv = POPs;
83003860 2230 const char *name;
7a5fd60d 2231
6e592b3a 2232 if (isGV_with_GP(sv)) {
159b6efe 2233 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
090bf15b 2234 goto do_ftruncate_gv;
d05c1ba0 2235 }
6e592b3a 2236 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
159b6efe 2237 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
090bf15b
SR
2238 goto do_ftruncate_gv;
2239 }
2240 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
a45c7426 2241 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
090bf15b 2242 goto do_ftruncate_io;
d05c1ba0 2243 }
1e422769 2244
83003860 2245 name = SvPV_nolen_const(sv);
d05c1ba0 2246 TAINT_PROPER("truncate");
cbdc8872 2247#ifdef HAS_TRUNCATE
d05c1ba0
JH
2248 if (truncate(name, len) < 0)
2249 result = 0;
cbdc8872 2250#else
d05c1ba0 2251 {
7452cf6a 2252 const int tmpfd = PerlLIO_open(name, O_RDWR);
d05c1ba0 2253
7452cf6a 2254 if (tmpfd < 0)
cbdc8872 2255 result = 0;
d05c1ba0
JH
2256 else {
2257 if (my_chsize(tmpfd, len) < 0)
2258 result = 0;
2259 PerlLIO_close(tmpfd);
2260 }
cbdc8872 2261 }
a0d0e21e 2262#endif
d05c1ba0 2263 }
a0d0e21e 2264
d05c1ba0
JH
2265 if (result)
2266 RETPUSHYES;
2267 if (!errno)
93189314 2268 SETERRNO(EBADF,RMS_IFI);
d05c1ba0
JH
2269 RETPUSHUNDEF;
2270 }
a0d0e21e
LW
2271}
2272
a0d0e21e
LW
2273PP(pp_ioctl)
2274{
97aff369 2275 dVAR; dSP; dTARGET;
7452cf6a 2276 SV * const argsv = POPs;
1df70142 2277 const unsigned int func = POPu;
e1ec3a88 2278 const int optype = PL_op->op_type;
159b6efe 2279 GV * const gv = MUTABLE_GV(POPs);
4608196e 2280 IO * const io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2281 char *s;
324aa91a 2282 IV retval;
a0d0e21e 2283
748a9306 2284 if (!io || !argsv || !IoIFP(io)) {
c289d2f7
JH
2285 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2286 report_evil_fh(gv, io, PL_op->op_type);
93189314 2287 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
a0d0e21e
LW
2288 RETPUSHUNDEF;
2289 }
2290
748a9306 2291 if (SvPOK(argsv) || !SvNIOK(argsv)) {
a0d0e21e 2292 STRLEN len;
324aa91a 2293 STRLEN need;
748a9306 2294 s = SvPV_force(argsv, len);
324aa91a
HF
2295 need = IOCPARM_LEN(func);
2296 if (len < need) {
2297 s = Sv_Grow(argsv, need + 1);
2298 SvCUR_set(argsv, need);
a0d0e21e
LW
2299 }
2300
748a9306 2301 s[SvCUR(argsv)] = 17; /* a little sanity check here */
a0d0e21e
LW
2302 }
2303 else {
748a9306 2304 retval = SvIV(argsv);
c529f79d 2305 s = INT2PTR(char*,retval); /* ouch */
a0d0e21e
LW
2306 }
2307
ed4b2e6b 2308 TAINT_PROPER(PL_op_desc[optype]);
a0d0e21e
LW
2309
2310 if (optype == OP_IOCTL)
2311#ifdef HAS_IOCTL
76e3520e 2312 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
a0d0e21e 2313#else
cea2e8a9 2314 DIE(aTHX_ "ioctl is not implemented");
a0d0e21e
LW
2315#endif
2316 else
c214f4ad
B
2317#ifndef HAS_FCNTL
2318 DIE(aTHX_ "fcntl is not implemented");
2319#else
55497cff 2320#if defined(OS2) && defined(__EMX__)
760ac839 2321 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
55497cff 2322#else
760ac839 2323 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
301e8125 2324#endif
6652bd42 2325#endif
a0d0e21e 2326
6652bd42 2327#if defined(HAS_IOCTL) || defined(HAS_FCNTL)
748a9306
LW
2328 if (SvPOK(argsv)) {
2329 if (s[SvCUR(argsv)] != 17)
cea2e8a9 2330 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
53e06cf0 2331 OP_NAME(PL_op));
748a9306
LW
2332 s[SvCUR(argsv)] = 0; /* put our null back */
2333 SvSETMAGIC(argsv); /* Assume it has changed */
a0d0e21e
LW
2334 }
2335
2336 if (retval == -1)
2337 RETPUSHUNDEF;
2338 if (retval != 0) {
2339 PUSHi(retval);
2340 }
2341 else {
8903cb82 2342 PUSHp(zero_but_true, ZBTLEN);
a0d0e21e 2343 }
4808266b 2344#endif
c214f4ad 2345 RETURN;
a0d0e21e
LW
2346}
2347
2348PP(pp_flock)
2349{
9cad6237 2350#ifdef FLOCK
97aff369 2351 dVAR; dSP; dTARGET;
a0d0e21e 2352 I32 value;
bc37a18f 2353 IO *io = NULL;
760ac839 2354 PerlIO *fp;
7452cf6a 2355 const int argtype = POPi;
159b6efe 2356 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
16d20bd9 2357
bc37a18f
RG
2358 if (gv && (io = GvIO(gv)))
2359 fp = IoIFP(io);
2360 else {
4608196e 2361 fp = NULL;
bc37a18f
RG
2362 io = NULL;
2363 }
0bcc34c2 2364 /* XXX Looks to me like io is always NULL at this point */
a0d0e21e 2365 if (fp) {
68dc0745 2366 (void)PerlIO_flush(fp);
76e3520e 2367 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
a0d0e21e 2368 }
cb50131a 2369 else {
bc37a18f
RG
2370 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2371 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 2372 value = 0;
93189314 2373 SETERRNO(EBADF,RMS_IFI);
cb50131a 2374 }
a0d0e21e
LW
2375 PUSHi(value);
2376 RETURN;
2377#else
cea2e8a9 2378 DIE(aTHX_ PL_no_func, "flock()");
a0d0e21e
LW
2379#endif
2380}
2381
2382/* Sockets. */
2383
2384PP(pp_socket)
2385{
a0d0e21e 2386#ifdef HAS_SOCKET
97aff369 2387 dVAR; dSP;
7452cf6a
AL
2388 const int protocol = POPi;
2389 const int type = POPi;
2390 const int domain = POPi;
159b6efe 2391 GV * const gv = MUTABLE_GV(POPs);
7452cf6a 2392 register IO * const io = gv ? GvIOn(gv) : NULL;
a0d0e21e
LW
2393 int fd;
2394
c289d2f7
JH
2395 if (!gv || !io) {
2396 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2397 report_evil_fh(gv, io, PL_op->op_type);
5ee74a84 2398 if (io && IoIFP(io))
c289d2f7 2399 do_close(gv, FALSE);
93189314 2400 SETERRNO(EBADF,LIB_INVARG);
a0d0e21e
LW
2401 RETPUSHUNDEF;
2402 }
2403
57171420
BS
2404 if (IoIFP(io))
2405 do_close(gv, FALSE);
2406
a0d0e21e 2407 TAINT_PROPER("socket");
6ad3d225 2408 fd = PerlSock_socket(domain, type, protocol);
a0d0e21e
LW
2409 if (fd < 0)
2410 RETPUSHUNDEF;
460c8493
IZ
2411 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2412 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2413 IoTYPE(io) = IoTYPE_SOCKET;
a0d0e21e 2414 if (!IoIFP(io) || !IoOFP(io)) {
760ac839
LW
2415 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2416 if (IoOFP(io)) PerlIO_close(IoOFP(io));
6ad3d225 2417 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
a0d0e21e
LW
2418 RETPUSHUNDEF;
2419 }
8d2a6795
GS
2420#if defined(HAS_FCNTL) && defined(F_SETFD)
2421 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2422#endif
a0d0e21e 2423
d5ff79b3
OF
2424#ifdef EPOC
2425 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2426#endif
2427
a0d0e21e
LW
2428 RETPUSHYES;
2429#else
cea2e8a9 2430 DIE(aTHX_ PL_no_sock_func, "socket");
a0d0e21e
LW
2431#endif
2432}
2433
2434PP(pp_sockpair)
2435{
c95c94b1 2436#if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
97aff369 2437 dVAR; dSP;
7452cf6a
AL
2438 const int protocol = POPi;
2439 const int type = POPi;
2440 const int domain = POPi;
159b6efe
NC
2441 GV * const gv2 = MUTABLE_GV(POPs);
2442 GV * const gv1 = MUTABLE_GV(POPs);
7452cf6a
AL
2443 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2444 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
a0d0e21e
LW
2445 int fd[2];
2446
c289d2f7
JH
2447 if (!gv1 || !gv2 || !io1 || !io2) {
2448 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2449 if (!gv1 || !io1)
2450 report_evil_fh(gv1, io1, PL_op->op_type);
2451 if (!gv2 || !io2)
2452 report_evil_fh(gv1, io2, PL_op->op_type);
2453 }
5ee74a84 2454 if (io1 && IoIFP(io1))
c289d2f7 2455 do_close(gv1, FALSE);
5ee74a84 2456 if (io2 && IoIFP(io2))
c289d2f7 2457 do_close(gv2, FALSE);
a0d0e21e 2458 RETPUSHUNDEF;
c289d2f7 2459 }
a0d0e21e 2460
dc0d0a5f
JH
2461 if (IoIFP(io1))
2462 do_close(gv1, FALSE);
2463 if (IoIFP(io2))
2464 do_close(gv2, FALSE);
57171420 2465
a0d0e21e 2466 TAINT_PROPER("socketpair");
6ad3d225 2467 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
a0d0e21e 2468 RETPUSHUNDEF;
460c8493
IZ
2469 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2470 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
50952442 2471 IoTYPE(io1) = IoTYPE_SOCKET;
460c8493
IZ
2472 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2473 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
50952442 2474 IoTYPE(io2) = IoTYPE_SOCKET;
a0d0e21e 2475 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
760ac839
LW
2476 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2477 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
6ad3d225 2478 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
760ac839
LW
2479 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2480 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
6ad3d225 2481 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
a0d0e21e
LW
2482 RETPUSHUNDEF;
2483 }
8d2a6795
GS
2484#if defined(HAS_FCNTL) && defined(F_SETFD)
2485 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2486 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2487#endif
a0d0e21e
LW
2488
2489 RETPUSHYES;
2490#else
cea2e8a9 2491 DIE(aTHX_ PL_no_sock_func, "socketpair");
a0d0e21e
LW
2492#endif
2493}
2494
2495PP(pp_bind)
2496{
a0d0e21e 2497#ifdef HAS_SOCKET
97aff369 2498 dVAR; dSP;
7452cf6a 2499 SV * const addrsv = POPs;
349d4f2f
NC
2500 /* OK, so on what platform does bind modify addr? */
2501 const char *addr;
159b6efe 2502 GV * const gv = MUTABLE_GV(POPs);
7452cf6a 2503 register IO * const io = GvIOn(gv);
a0d0e21e
LW
2504 STRLEN len;
2505
2506 if (!io || !IoIFP(io))
2507 goto nuts;
2508
349d4f2f 2509 addr = SvPV_const(addrsv, len);
a0d0e21e 2510 TAINT_PROPER("bind");
a79db61d 2511 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
a0d0e21e
LW
2512 RETPUSHYES;
2513 else
2514 RETPUSHUNDEF;
2515
2516nuts:
599cee73 2517 if (ckWARN(WARN_CLOSED))
bc37a18f 2518 report_evil_fh(gv, io, PL_op->op_type);
93189314 2519 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2520 RETPUSHUNDEF;
2521#else
cea2e8a9 2522 DIE(aTHX_ PL_no_sock_func, "bind");
a0d0e21e
LW
2523#endif
2524}
2525
2526PP(pp_connect)
2527{
a0d0e21e 2528#ifdef HAS_SOCKET
97aff369 2529 dVAR; dSP;
7452cf6a 2530 SV * const addrsv = POPs;
159b6efe 2531 GV * const gv = MUTABLE_GV(POPs);
7452cf6a 2532 register IO * const io = GvIOn(gv);
349d4f2f 2533 const char *addr;
a0d0e21e
LW
2534 STRLEN len;
2535
2536 if (!io || !IoIFP(io))
2537 goto nuts;
2538
349d4f2f 2539 addr = SvPV_const(addrsv, len);
a0d0e21e 2540 TAINT_PROPER("connect");
6ad3d225 2541 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
a0d0e21e
LW
2542 RETPUSHYES;
2543 else
2544 RETPUSHUNDEF;
2545
2546nuts:
599cee73 2547 if (ckWARN(WARN_CLOSED))
bc37a18f 2548 report_evil_fh(gv, io, PL_op->op_type);
93189314 2549 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2550 RETPUSHUNDEF;
2551#else
cea2e8a9 2552 DIE(aTHX_ PL_no_sock_func, "connect");
a0d0e21e
LW
2553#endif
2554}
2555
2556PP(pp_listen)
2557{
a0d0e21e 2558#ifdef HAS_SOCKET
97aff369 2559 dVAR; dSP;
7452cf6a 2560 const int backlog = POPi;
159b6efe 2561 GV * const gv = MUTABLE_GV(POPs);
7452cf6a 2562 register IO * const io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2563
c289d2f7 2564 if (!gv || !io || !IoIFP(io))
a0d0e21e
LW
2565 goto nuts;
2566
6ad3d225 2567 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
a0d0e21e
LW
2568 RETPUSHYES;
2569 else
2570 RETPUSHUNDEF;
2571
2572nuts:
599cee73 2573 if (ckWARN(WARN_CLOSED))
bc37a18f 2574 report_evil_fh(gv, io, PL_op->op_type);
93189314 2575 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2576 RETPUSHUNDEF;
2577#else
cea2e8a9 2578 DIE(aTHX_ PL_no_sock_func, "listen");
a0d0e21e
LW
2579#endif
2580}
2581
2582PP(pp_accept)
2583{
a0d0e21e 2584#ifdef HAS_SOCKET
97aff369 2585 dVAR; dSP; dTARGET;
a0d0e21e
LW
2586 register IO *nstio;
2587 register IO *gstio;
93d47a36
JH
2588 char namebuf[MAXPATHLEN];
2589#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2590 Sock_size_t len = sizeof (struct sockaddr_in);
2591#else
2592 Sock_size_t len = sizeof namebuf;
2593#endif
159b6efe
NC
2594 GV * const ggv = MUTABLE_GV(POPs);
2595 GV * const ngv = MUTABLE_GV(POPs);
a0d0e21e
LW
2596 int fd;
2597
a0d0e21e
LW
2598 if (!ngv)
2599 goto badexit;
2600 if (!ggv)
2601 goto nuts;
2602
2603 gstio = GvIO(ggv);
2604 if (!gstio || !IoIFP(gstio))
2605 goto nuts;
2606
2607 nstio = GvIOn(ngv);
93d47a36 2608 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
e294cc5d
JH
2609#if defined(OEMVS)
2610 if (len == 0) {
2611 /* Some platforms indicate zero length when an AF_UNIX client is
2612 * not bound. Simulate a non-zero-length sockaddr structure in
2613 * this case. */
2614 namebuf[0] = 0; /* sun_len */
2615 namebuf[1] = AF_UNIX; /* sun_family */
2616 len = 2;
2617 }
2618#endif
2619
a0d0e21e
LW
2620 if (fd < 0)
2621 goto badexit;
a70048fb
AB
2622 if (IoIFP(nstio))
2623 do_close(ngv, FALSE);
460c8493
IZ
2624 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2625 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2626 IoTYPE(nstio) = IoTYPE_SOCKET;
a0d0e21e 2627 if (!IoIFP(nstio) || !IoOFP(nstio)) {
760ac839
LW
2628 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2629 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
6ad3d225 2630 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
a0d0e21e
LW
2631 goto badexit;
2632 }
8d2a6795
GS
2633#if defined(HAS_FCNTL) && defined(F_SETFD)
2634 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2635#endif
a0d0e21e 2636
ed79a026 2637#ifdef EPOC
93d47a36 2638 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
a9f1f6b0 2639 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
ed79a026 2640#endif
381c1bae 2641#ifdef __SCO_VERSION__
93d47a36 2642 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
381c1bae 2643#endif
ed79a026 2644
93d47a36 2645 PUSHp(namebuf, len);
a0d0e21e
LW
2646 RETURN;
2647
2648nuts:
599cee73 2649 if (ckWARN(WARN_CLOSED))
bc37a18f 2650 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
93189314 2651 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2652
2653badexit:
2654 RETPUSHUNDEF;
2655
2656#else
cea2e8a9 2657 DIE(aTHX_ PL_no_sock_func, "accept");
a0d0e21e
LW
2658#endif
2659}
2660
2661PP(pp_shutdown)
2662{
a0d0e21e 2663#ifdef HAS_SOCKET
97aff369 2664 dVAR; dSP; dTARGET;
7452cf6a 2665 const int how = POPi;
159b6efe 2666 GV * const gv = MUTABLE_GV(POPs);
7452cf6a 2667 register IO * const io = GvIOn(gv);
a0d0e21e
LW
2668
2669 if (!io || !IoIFP(io))
2670 goto nuts;
2671
6ad3d225 2672 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
a0d0e21e
LW
2673 RETURN;
2674
2675nuts:
599cee73 2676 if (ckWARN(WARN_CLOSED))
bc37a18f 2677 report_evil_fh(gv, io, PL_op->op_type);
93189314 2678 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2679 RETPUSHUNDEF;
2680#else
cea2e8a9 2681 DIE(aTHX_ PL_no_sock_func, "shutdown");
a0d0e21e
LW
2682#endif
2683}
2684
a0d0e21e
LW
2685PP(pp_ssockopt)
2686{
a0d0e21e 2687#ifdef HAS_SOCKET
97aff369 2688 dVAR; dSP;
7452cf6a 2689 const int optype = PL_op->op_type;
561b68a9 2690 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
7452cf6a
AL
2691 const unsigned int optname = (unsigned int) POPi;
2692 const unsigned int lvl = (unsigned int) POPi;
159b6efe 2693 GV * const gv = MUTABLE_GV(POPs);
7452cf6a 2694 register IO * const io = GvIOn(gv);
a0d0e21e 2695 int fd;
1e422769 2696 Sock_size_t len;
a0d0e21e 2697
a0d0e21e
LW
2698 if (!io || !IoIFP(io))
2699 goto nuts;
2700
760ac839 2701 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2702 switch (optype) {
2703 case OP_GSOCKOPT:
748a9306 2704 SvGROW(sv, 257);
a0d0e21e 2705 (void)SvPOK_only(sv);
748a9306
LW
2706 SvCUR_set(sv,256);
2707 *SvEND(sv) ='\0';
1e422769 2708 len = SvCUR(sv);
6ad3d225 2709 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
a0d0e21e 2710 goto nuts2;
1e422769 2711 SvCUR_set(sv, len);
748a9306 2712 *SvEND(sv) ='\0';
a0d0e21e
LW
2713 PUSHs(sv);
2714 break;
2715 case OP_SSOCKOPT: {
1215b447
JH
2716#if defined(__SYMBIAN32__)
2717# define SETSOCKOPT_OPTION_VALUE_T void *
2718#else
2719# define SETSOCKOPT_OPTION_VALUE_T const char *
2720#endif
2721 /* XXX TODO: We need to have a proper type (a Configure probe,
2722 * etc.) for what the C headers think of the third argument of
2723 * setsockopt(), the option_value read-only buffer: is it
2724 * a "char *", or a "void *", const or not. Some compilers
2725 * don't take kindly to e.g. assuming that "char *" implicitly
2726 * promotes to a "void *", or to explicitly promoting/demoting
2727 * consts to non/vice versa. The "const void *" is the SUS
2728 * definition, but that does not fly everywhere for the above
2729 * reasons. */
2730 SETSOCKOPT_OPTION_VALUE_T buf;
1e422769
PP
2731 int aint;
2732 if (SvPOKp(sv)) {
2d8e6c8d 2733 STRLEN l;
1215b447 2734 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2d8e6c8d 2735 len = l;
1e422769 2736 }
56ee1660 2737 else {
a0d0e21e 2738 aint = (int)SvIV(sv);
1215b447 2739 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
a0d0e21e
LW
2740 len = sizeof(int);
2741 }
6ad3d225 2742 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
a0d0e21e 2743 goto nuts2;
3280af22 2744 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2745 }
2746 break;
2747 }
2748 RETURN;
2749
2750nuts:
599cee73 2751 if (ckWARN(WARN_CLOSED))
bc37a18f 2752 report_evil_fh(gv, io, optype);
93189314 2753 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2754nuts2:
2755 RETPUSHUNDEF;
2756
2757#else
af51a00e 2758 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
a0d0e21e
LW
2759#endif
2760}
2761
a0d0e21e
LW
2762PP(pp_getpeername)
2763{
a0d0e21e 2764#ifdef HAS_SOCKET
97aff369 2765 dVAR; dSP;
7452cf6a 2766 const int optype = PL_op->op_type;
159b6efe 2767 GV * const gv = MUTABLE_GV(POPs);
7452cf6a
AL
2768 register IO * const io = GvIOn(gv);
2769 Sock_size_t len;
a0d0e21e
LW
2770 SV *sv;
2771 int fd;
a0d0e21e
LW
2772
2773 if (!io || !IoIFP(io))
2774 goto nuts;
2775
561b68a9 2776 sv = sv_2mortal(newSV(257));
748a9306 2777 (void)SvPOK_only(sv);
1e422769
PP
2778 len = 256;
2779 SvCUR_set(sv, len);
748a9306 2780 *SvEND(sv) ='\0';
760ac839 2781 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2782 switch (optype) {
2783 case OP_GETSOCKNAME:
6ad3d225 2784 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e
LW
2785 goto nuts2;
2786 break;
2787 case OP_GETPEERNAME:
6ad3d225 2788 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e 2789 goto nuts2;
490ab354
JH
2790#if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2791 {
2792 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";
2793 /* If the call succeeded, make sure we don't have a zeroed port/addr */
349d4f2f 2794 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2fbb330f 2795 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
490ab354 2796 sizeof(u_short) + sizeof(struct in_addr))) {
301e8125 2797 goto nuts2;
490ab354
JH
2798 }
2799 }
2800#endif
a0d0e21e
LW
2801 break;
2802 }
13826f2c
CS
2803#ifdef BOGUS_GETNAME_RETURN
2804 /* Interactive Unix, getpeername() and getsockname()
2805 does not return valid namelen */
1e422769
PP
2806 if (len == BOGUS_GETNAME_RETURN)
2807 len = sizeof(struct sockaddr);
13826f2c 2808#endif
1e422769 2809 SvCUR_set(sv, len);
748a9306 2810 *SvEND(sv) ='\0';
a0d0e21e
LW
2811 PUSHs(sv);
2812 RETURN;
2813
2814nuts:
599cee73 2815 if (ckWARN(WARN_CLOSED))
bc37a18f 2816 report_evil_fh(gv, io, optype);
93189314 2817 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2818nuts2:
2819 RETPUSHUNDEF;
2820
2821#else
af51a00e 2822 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
a0d0e21e
LW
2823#endif
2824}
2825
2826/* Stat calls. */
2827
a0d0e21e
LW
2828PP(pp_stat)
2829{
97aff369 2830 dVAR;
39644a26 2831 dSP;
10edeb5d 2832 GV *gv = NULL;
ad02613c 2833 IO *io;
54310121 2834 I32 gimme;
a0d0e21e
LW
2835 I32 max = 13;
2836
533c011a 2837 if (PL_op->op_flags & OPf_REF) {
2dd78f96 2838 gv = cGVOP_gv;
8a4e5b40 2839 if (PL_op->op_type == OP_LSTAT) {
5d3e98de 2840 if (gv != PL_defgv) {
5d329e6e 2841 do_fstat_warning_check:
a2a5de95
NC
2842 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2843 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
5d3e98de 2844 } else if (PL_laststype != OP_LSTAT)
8a4e5b40 2845 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
8a4e5b40
DD
2846 }
2847
748a9306 2848 do_fstat:
2dd78f96 2849 if (gv != PL_defgv) {
3280af22 2850 PL_laststype = OP_STAT;
2dd78f96 2851 PL_statgv = gv;
76f68e9b 2852 sv_setpvs(PL_statname, "");
5228a96c 2853 if(gv) {
ad02613c
SP
2854 io = GvIO(gv);
2855 do_fstat_have_io:
5228a96c
SP
2856 if (io) {
2857 if (IoIFP(io)) {
2858 PL_laststatval =
2859 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2860 } else if (IoDIRP(io)) {
5228a96c 2861 PL_laststatval =
3497a01f 2862 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
5228a96c
SP
2863 } else {
2864 PL_laststatval = -1;
2865 }
2866 }
2867 }
2868 }
2869
9ddeeac9 2870 if (PL_laststatval < 0) {
2dd78f96
JH
2871 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2872 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
a0d0e21e 2873 max = 0;
9ddeeac9 2874 }
a0d0e21e
LW
2875 }
2876 else {
7452cf6a 2877 SV* const sv = POPs;
6e592b3a 2878 if (isGV_with_GP(sv)) {
159b6efe 2879 gv = MUTABLE_GV(sv);
748a9306 2880 goto do_fstat;
6e592b3a 2881 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
159b6efe 2882 gv = MUTABLE_GV(SvRV(sv));
ad02613c
SP
2883 if (PL_op->op_type == OP_LSTAT)
2884 goto do_fstat_warning_check;
2885 goto do_fstat;
2886 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
a45c7426 2887 io = MUTABLE_IO(SvRV(sv));
ad02613c
SP
2888 if (PL_op->op_type == OP_LSTAT)
2889 goto do_fstat_warning_check;
2890 goto do_fstat_have_io;
2891 }
2892
0510663f 2893 sv_setpv(PL_statname, SvPV_nolen_const(sv));
a0714e2c 2894 PL_statgv = NULL;
533c011a
NIS
2895 PL_laststype = PL_op->op_type;
2896 if (PL_op->op_type == OP_LSTAT)
0510663f 2897 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
a0d0e21e 2898 else
0510663f 2899 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
3280af22 2900 if (PL_laststatval < 0) {
0510663f 2901 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
9014280d 2902 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
a0d0e21e
LW
2903 max = 0;
2904 }
2905 }
2906
54310121
PP
2907 gimme = GIMME_V;
2908 if (gimme != G_ARRAY) {
2909 if (gimme != G_VOID)
2910 XPUSHs(boolSV(max));
2911 RETURN;
a0d0e21e
LW
2912 }
2913 if (max) {
36477c24
PP
2914 EXTEND(SP, max);
2915 EXTEND_MORTAL(max);
6e449a3a
MHM
2916 mPUSHi(PL_statcache.st_dev);
2917 mPUSHi(PL_statcache.st_ino);
2918 mPUSHu(PL_statcache.st_mode);
2919 mPUSHu(PL_statcache.st_nlink);
146174a9 2920#if Uid_t_size > IVSIZE
6e449a3a 2921 mPUSHn(PL_statcache.st_uid);
146174a9 2922#else
23dcd6c8 2923# if Uid_t_sign <= 0
6e449a3a 2924 mPUSHi(PL_statcache.st_uid);
23dcd6c8 2925# else
6e449a3a 2926 mPUSHu(PL_statcache.st_uid);
23dcd6c8 2927# endif
146174a9 2928#endif
301e8125 2929#if Gid_t_size > IVSIZE
6e449a3a 2930 mPUSHn(PL_statcache.st_gid);
146174a9 2931#else
23dcd6c8 2932# if Gid_t_sign <= 0
6e449a3a 2933 mPUSHi(PL_statcache.st_gid);
23dcd6c8 2934# else
6e449a3a 2935 mPUSHu(PL_statcache.st_gid);
23dcd6c8 2936# endif
146174a9 2937#endif
cbdc8872 2938#ifdef USE_STAT_RDEV
6e449a3a 2939 mPUSHi(PL_statcache.st_rdev);
cbdc8872 2940#else
84bafc02 2941 PUSHs(newSVpvs_flags("", SVs_TEMP));
cbdc8872 2942#endif
146174a9 2943#if Off_t_size > IVSIZE
6e449a3a 2944 mPUSHn(PL_statcache.st_size);
146174a9 2945#else
6e449a3a 2946 mPUSHi(PL_statcache.st_size);
146174a9 2947#endif
cbdc8872 2948#ifdef BIG_TIME
6e449a3a
MHM
2949 mPUSHn(PL_statcache.st_atime);
2950 mPUSHn(PL_statcache.st_mtime);
2951 mPUSHn(PL_statcache.st_ctime);
cbdc8872 2952#else
6e449a3a
MHM
2953 mPUSHi(PL_statcache.st_atime);
2954 mPUSHi(PL_statcache.st_mtime);
2955 mPUSHi(PL_statcache.st_ctime);
cbdc8872 2956#endif
a0d0e21e 2957#ifdef USE_STAT_BLOCKS
6e449a3a
MHM
2958 mPUSHu(PL_statcache.st_blksize);
2959 mPUSHu(PL_statcache.st_blocks);
a0d0e21e 2960#else
84bafc02
NC
2961 PUSHs(newSVpvs_flags("", SVs_TEMP));
2962 PUSHs(newSVpvs_flags("", SVs_TEMP));
a0d0e21e
LW
2963#endif
2964 }
2965 RETURN;
2966}
2967
6f1401dc
DM
2968#define tryAMAGICftest_MG(chr) STMT_START { \
2969 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2970 && S_try_amagic_ftest(aTHX_ chr)) \
2971 return NORMAL; \
2972 } STMT_END
2973
2974STATIC bool
2975S_try_amagic_ftest(pTHX_ char chr) {
2976 dVAR;
2977 dSP;
2978 SV* const arg = TOPs;
2979
2980 assert(chr != '?');
2981 SvGETMAGIC(arg);
2982
2983 if ((PL_op->op_flags & OPf_KIDS)
2984 && SvAMAGIC(TOPs))
2985 {
2986 const char tmpchr = chr;
2987 const OP *next;
2988 SV * const tmpsv = amagic_call(arg,
2989 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2990 ftest_amg, AMGf_unary);
2991
2992 if (!tmpsv)
2993 return FALSE;
2994
2995 SPAGAIN;
2996
2997 next = PL_op->op_next;
2998 if (next->op_type >= OP_FTRREAD &&
2999 next->op_type <= OP_FTBINARY &&
3000 next->op_private & OPpFT_STACKED
3001 ) {
3002 if (SvTRUE(tmpsv))
3003 /* leave the object alone */
3004 return TRUE;
3005 }
3006
3007 SETs(tmpsv);
3008 PUTBACK;
3009 return TRUE;
3010 }
3011 return FALSE;
3012}
3013
3014
fbb0b3b3
RGS
3015/* This macro is used by the stacked filetest operators :
3016 * if the previous filetest failed, short-circuit and pass its value.
3017 * Else, discard it from the stack and continue. --rgs
3018 */
3019#define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
d724f706 3020 if (!SvTRUE(TOPs)) { RETURN; } \
fbb0b3b3
RGS
3021 else { (void)POPs; PUTBACK; } \
3022 }
3023
a0d0e21e
LW
3024PP(pp_ftrread)
3025{
97aff369 3026 dVAR;
9cad6237 3027 I32 result;
af9e49b4
NC
3028 /* Not const, because things tweak this below. Not bool, because there's
3029 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
3030#if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3031 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3032 /* Giving some sort of initial value silences compilers. */
3033# ifdef R_OK
3034 int access_mode = R_OK;
3035# else
3036 int access_mode = 0;
3037# endif
5ff3f7a4 3038#else
af9e49b4
NC
3039 /* access_mode is never used, but leaving use_access in makes the
3040 conditional compiling below much clearer. */
3041 I32 use_access = 0;
5ff3f7a4 3042#endif
af9e49b4 3043 int stat_mode = S_IRUSR;
a0d0e21e 3044
af9e49b4 3045 bool effective = FALSE;
07fe7c6a 3046 char opchar = '?';
2a3ff820 3047 dSP;
af9e49b4 3048
7fb13887
BM
3049 switch (PL_op->op_type) {
3050 case OP_FTRREAD: opchar = 'R'; break;
3051 case OP_FTRWRITE: opchar = 'W'; break;
3052 case OP_FTREXEC: opchar = 'X'; break;
3053 case OP_FTEREAD: opchar = 'r'; break;
3054 case OP_FTEWRITE: opchar = 'w'; break;
3055 case OP_FTEEXEC: opchar = 'x'; break;
3056 }
6f1401dc 3057 tryAMAGICftest_MG(opchar);
7fb13887 3058
fbb0b3b3 3059 STACKED_FTEST_CHECK;
af9e49b4
NC
3060
3061 switch (PL_op->op_type) {
3062 case OP_FTRREAD:
3063#if !(defined(HAS_ACCESS) && defined(R_OK))
3064 use_access = 0;
3065#endif
3066 break;
3067
3068 case OP_FTRWRITE:
5ff3f7a4 3069#if defined(HAS_ACCESS) && defined(W_OK)
af9e49b4 3070 access_mode = W_OK;
5ff3f7a4 3071#else
af9e49b4 3072 use_access = 0;
5ff3f7a4 3073#endif
af9e49b4
NC
3074 stat_mode = S_IWUSR;
3075 break;
a0d0e21e 3076
af9e49b4 3077 case OP_FTREXEC:
5ff3f7a4 3078#if defined(HAS_ACCESS) && defined(X_OK)
af9e49b4 3079 access_mode = X_OK;
5ff3f7a4 3080#else
af9e49b4 3081 use_access = 0;
5ff3f7a4 3082#endif
af9e49b4
NC
3083 stat_mode = S_IXUSR;
3084 break;
a0d0e21e 3085
af9e49b4 3086 case OP_FTEWRITE:
faee0e31 3087#ifdef PERL_EFF_ACCESS
af9e49b4 3088 access_mode = W_OK;
5ff3f7a4 3089#endif
af9e49b4 3090 stat_mode = S_IWUSR;
7fb13887 3091 /* fall through */
a0d0e21e 3092
af9e49b4
NC
3093 case OP_FTEREAD:
3094#ifndef PERL_EFF_ACCESS
3095 use_access = 0;
3096#endif
3097 effective = TRUE;
3098 break;
3099
af9e49b4 3100 case OP_FTEEXEC:
faee0e31 3101#ifdef PERL_EFF_ACCESS
b376053d 3102 access_mode = X_OK;
5ff3f7a4 3103#else
af9e49b4 3104 use_access = 0;
5ff3f7a4 3105#endif
af9e49b4
NC
3106 stat_mode = S_IXUSR;
3107 effective = TRUE;
3108 break;
3109 }
a0d0e21e 3110
af9e49b4
NC
3111 if (use_access) {
3112#if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2c2f35ab 3113 const char *name = POPpx;
af9e49b4
NC
3114 if (effective) {
3115# ifdef PERL_EFF_ACCESS
3116 result = PERL_EFF_ACCESS(name, access_mode);
3117# else
3118 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3119 OP_NAME(PL_op));
3120# endif
3121 }
3122 else {
3123# ifdef HAS_ACCESS
3124 result = access(name, access_mode);
3125# else
3126 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3127# endif
3128 }
5ff3f7a4
GS
3129 if (result == 0)
3130 RETPUSHYES;
3131 if (result < 0)
3132 RETPUSHUNDEF;
3133 RETPUSHNO;
af9e49b4 3134#endif
22865c03 3135 }
af9e49b4 3136
40c852de 3137 result = my_stat_flags(0);
22865c03 3138 SPAGAIN;
a0d0e21e
LW
3139 if (result < 0)
3140 RETPUSHUNDEF;
af9e49b4 3141 if (cando(stat_mode, effective, &PL_statcache))
a0d0e21e
LW
3142 RETPUSHYES;
3143 RETPUSHNO;
3144}
3145
3146PP(pp_ftis)
3147{
97aff369 3148 dVAR;
fbb0b3b3 3149 I32 result;
d7f0a2f4 3150 const int op_type = PL_op->op_type;
07fe7c6a 3151 char opchar = '?';
2a3ff820 3152 dSP;
07fe7c6a
BM
3153
3154 switch (op_type) {
3155 case OP_FTIS: opchar = 'e'; break;
3156 case OP_FTSIZE: opchar = 's'; break;
3157 case OP_FTMTIME: opchar = 'M'; break;
3158 case OP_FTCTIME: opchar = 'C'; break;
3159 case OP_FTATIME: opchar = 'A'; break;
3160 }
6f1401dc 3161 tryAMAGICftest_MG(opchar);
07fe7c6a 3162
fbb0b3b3 3163 STACKED_FTEST_CHECK;
7fb13887 3164
40c852de 3165 result = my_stat_flags(0);
fbb0b3b3 3166 SPAGAIN;
a0d0e21e
LW
3167 if (result < 0)
3168 RETPUSHUNDEF;
d7f0a2f4
NC
3169 if (op_type == OP_FTIS)
3170 RETPUSHYES;
957b0e1d 3171 {
d7f0a2f4
NC
3172 /* You can't dTARGET inside OP_FTIS, because you'll get
3173 "panic: pad_sv po" - the op is not flagged to have a target. */
957b0e1d 3174 dTARGET;
d7f0a2f4 3175 switch (op_type) {
957b0e1d
NC
3176 case OP_FTSIZE:
3177#if Off_t_size > IVSIZE
3178 PUSHn(PL_statcache.st_size);
3179#else
3180 PUSHi(PL_statcache.st_size);
3181#endif
3182 break;
3183 case OP_FTMTIME:
3184 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3185 break;
3186 case OP_FTATIME:
3187 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3188 break;
3189 case OP_FTCTIME:
3190 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3191 break;
3192 }
3193 }
3194 RETURN;
a0d0e21e
LW
3195}
3196
a0d0e21e
LW
3197PP(pp_ftrowned)
3198{
97aff369 3199 dVAR;
fbb0b3b3 3200 I32 result;
07fe7c6a 3201 char opchar = '?';
2a3ff820 3202 dSP;
17ad201a 3203
7fb13887
BM
3204 switch (PL_op->op_type) {
3205 case OP_FTROWNED: opchar = 'O'; break;
3206 case OP_FTEOWNED: opchar = 'o'; break;
3207 case OP_FTZERO: opchar = 'z'; break;
3208 case OP_FTSOCK: opchar = 'S'; break;
3209 case OP_FTCHR: opchar = 'c'; break;
3210 case OP_FTBLK: opchar = 'b'; break;
3211 case OP_FTFILE: opchar = 'f'; break;
3212 case OP_FTDIR: opchar = 'd'; break;
3213 case OP_FTPIPE: opchar = 'p'; break;
3214 case OP_FTSUID: opchar = 'u'; break;
3215 case OP_FTSGID: opchar = 'g'; break;
3216 case OP_FTSVTX: opchar = 'k'; break;
3217 }
6f1401dc 3218 tryAMAGICftest_MG(opchar);
7fb13887 3219
1b0124a7
JD
3220 STACKED_FTEST_CHECK;
3221
17ad201a
NC
3222 /* I believe that all these three are likely to be defined on most every
3223 system these days. */
3224#ifndef S_ISUID
c410dd6a 3225 if(PL_op->op_type == OP_FTSUID) {
1b0124a7
JD
3226 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3227 (void) POPs;
17ad201a 3228 RETPUSHNO;
c410dd6a 3229 }
17ad201a
NC
3230#endif
3231#ifndef S_ISGID
c410dd6a 3232 if(PL_op->op_type == OP_FTSGID) {
1b0124a7
JD
3233 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3234 (void) POPs;
17ad201a 3235 RETPUSHNO;
c410dd6a 3236 }
17ad201a
NC
3237#endif
3238#ifndef S_ISVTX
c410dd6a 3239 if(PL_op->op_type == OP_FTSVTX) {
1b0124a7
JD
3240 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3241 (void) POPs;
17ad201a 3242 RETPUSHNO;
c410dd6a 3243 }
17ad201a
NC
3244#endif
3245
40c852de 3246 result = my_stat_flags(0);
fbb0b3b3 3247 SPAGAIN;
a0d0e21e
LW
3248 if (result < 0)
3249 RETPUSHUNDEF;
f1cb2d48
NC
3250 switch (PL_op->op_type) {
3251 case OP_FTROWNED:
9ab9fa88 3252 if (PL_statcache.st_uid == PL_uid)
f1cb2d48
NC
3253 RETPUSHYES;
3254 break;
3255 case OP_FTEOWNED:
3256 if (PL_statcache.st_uid == PL_euid)
3257 RETPUSHYES;
3258 break;
3259 case OP_FTZERO:
3260 if (PL_statcache.st_size == 0)
3261 RETPUSHYES;
3262 break;
3263 case OP_FTSOCK:
3264 if (S_ISSOCK(PL_statcache.st_mode))
3265 RETPUSHYES;
3266 break;
3267 case OP_FTCHR:
3268 if (S_ISCHR(PL_statcache.st_mode))
3269 RETPUSHYES;
3270 break;
3271 case OP_FTBLK:
3272 if (S_ISBLK(PL_statcache.st_mode))
3273 RETPUSHYES;
3274 break;
3275 case OP_FTFILE:
3276 if (S_ISREG(PL_statcache.st_mode))
3277 RETPUSHYES;
3278 break;
3279 case OP_FTDIR:
3280 if (S_ISDIR(PL_statcache.st_mode))
3281 RETPUSHYES;
3282 break;
3283 case OP_FTPIPE:
3284 if (S_ISFIFO(PL_statcache.st_mode))
3285 RETPUSHYES;
3286 break;
a0d0e21e 3287#ifdef S_ISUID
17ad201a
NC
3288 case OP_FTSUID:
3289 if (PL_statcache.st_mode & S_ISUID)
3290 RETPUSHYES;
3291 break;
a0d0e21e 3292#endif
a0d0e21e 3293#ifdef S_ISGID
17ad201a
NC
3294 case OP_FTSGID:
3295 if (PL_statcache.st_mode & S_ISGID)
3296 RETPUSHYES;
3297 break;
3298#endif
3299#ifdef S_ISVTX
3300 case OP_FTSVTX:
3301 if (PL_statcache.st_mode & S_ISVTX)
3302 RETPUSHYES;
3303 break;
a0d0e21e 3304#endif
17ad201a 3305 }
a0d0e21e
LW
3306 RETPUSHNO;
3307}
3308
17ad201a 3309PP(pp_ftlink)
a0d0e21e 3310{
97aff369 3311 dVAR;
39644a26 3312 dSP;
500ff13f 3313 I32 result;
07fe7c6a 3314
6f1401dc 3315 tryAMAGICftest_MG('l');
40c852de 3316 result = my_lstat_flags(0);
500ff13f
BM
3317 SPAGAIN;
3318
a0d0e21e
LW
3319 if (result < 0)
3320 RETPUSHUNDEF;
17ad201a 3321 if (S_ISLNK(PL_statcache.st_mode))
a0d0e21e 3322 RETPUSHYES;
a0d0e21e
LW
3323 RETPUSHNO;
3324}
3325
3326PP(pp_fttty)
3327{
97aff369 3328 dVAR;
39644a26 3329 dSP;
a0d0e21e
LW
3330 int fd;
3331 GV *gv;
a0714e2c 3332 SV *tmpsv = NULL;
0784aae0 3333 char *name = NULL;
40c852de 3334 STRLEN namelen;
fb73857a 3335
6f1401dc 3336 tryAMAGICftest_MG('t');
07fe7c6a 3337
fbb0b3b3
RGS
3338 STACKED_FTEST_CHECK;
3339
533c011a 3340 if (PL_op->op_flags & OPf_REF)
146174a9 3341 gv = cGVOP_gv;
13be902c 3342 else if (isGV_with_GP(TOPs))
159b6efe 3343 gv = MUTABLE_GV(POPs);
fb73857a 3344 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
159b6efe 3345 gv = MUTABLE_GV(SvRV(POPs));
40c852de
DM
3346 else {
3347 tmpsv = POPs;
3348 name = SvPV_nomg(tmpsv, namelen);
3349 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3350 }
fb73857a 3351
a0d0e21e 3352 if (GvIO(gv) && IoIFP(GvIOp(gv)))
760ac839 3353 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
7a5fd60d 3354 else if (tmpsv && SvOK(tmpsv)) {
40c852de
DM
3355 if (isDIGIT(*name))
3356 fd = atoi(name);
7a5fd60d
NC
3357 else
3358 RETPUSHUNDEF;
3359 }
a0d0e21e
LW
3360 else
3361 RETPUSHUNDEF;
6ad3d225 3362 if (PerlLIO_isatty(fd))
a0d0e21e
LW
3363 RETPUSHYES;
3364 RETPUSHNO;
3365}
3366
16d20bd9
AD
3367#if defined(atarist) /* this will work with atariST. Configure will
3368 make guesses for other systems. */
3369# define FILE_base(f) ((f)->_base)
3370# define FILE_ptr(f) ((f)->_ptr)
3371# define FILE_cnt(f) ((f)->_cnt)
3372# define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
a0d0e21e
LW
3373#endif
3374
3375PP(pp_fttext)
3376{
97aff369 3377 dVAR;
39644a26 3378 dSP;
a0d0e21e
LW
3379 I32 i;
3380 I32 len;
3381 I32 odd = 0;
3382 STDCHAR tbuf[512];
3383 register STDCHAR *s;
3384 register IO *io;
5f05dabc
PP
3385 register SV *sv;
3386 GV *gv;
146174a9 3387 PerlIO *fp;
a0d0e21e 3388
6f1401dc 3389 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
07fe7c6a 3390
fbb0b3b3
RGS
3391 STACKED_FTEST_CHECK;
3392
533c011a 3393 if (PL_op->op_flags & OPf_REF)
146174a9 3394 gv = cGVOP_gv;
13be902c 3395 else if (isGV_with_GP(TOPs))
159b6efe 3396 gv = MUTABLE_GV(POPs);
5f05dabc 3397 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
159b6efe 3398 gv = MUTABLE_GV(SvRV(POPs));
5f05dabc 3399 else
a0714e2c 3400 gv = NULL;
5f05dabc
PP
3401
3402 if (gv) {
a0d0e21e 3403 EXTEND(SP, 1);
3280af22
NIS
3404 if (gv == PL_defgv) {
3405 if (PL_statgv)
3406 io = GvIO(PL_statgv);
a0d0e21e 3407 else {
3280af22 3408 sv = PL_statname;
a0d0e21e
LW
3409 goto really_filename;
3410 }
3411 }
3412 else {
3280af22
NIS
3413 PL_statgv = gv;
3414 PL_laststatval = -1;
76f68e9b 3415 sv_setpvs(PL_statname, "");
3280af22 3416 io = GvIO(PL_statgv);
a0d0e21e
LW
3417 }
3418 if (io && IoIFP(io)) {
5f05dabc 3419 if (! PerlIO_has_base(IoIFP(io)))
cea2e8a9 3420 DIE(aTHX_ "-T and -B not implemented on filehandles");
3280af22
NIS
3421 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3422 if (PL_laststatval < 0)
5f05dabc 3423 RETPUSHUNDEF;
9cbac4c7 3424 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
533c011a 3425 if (PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3426 RETPUSHNO;
3427 else
3428 RETPUSHYES;