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