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