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