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