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