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