This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Minor grammar fix by Uri Guttman
[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
eec2d3df
GS
717 /* Only DIE if trying to restrict permissions on `user' (self).
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;
3280af22 794 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;
14befaf4
DM
881 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
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;
14befaf4
DM
920 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
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;
4633a7c4
LW
1169 GV *newdefout, *egv;
1170 HV *hv;
1171
533c011a 1172 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 1301 if (!cv) {
2dd78f96 1302 char *name = NULL;
a0d0e21e 1303 if (fgv) {
748a9306 1304 SV *tmpsv = sv_newmortal();
43693395 1305 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
2dd78f96 1306 name = SvPV_nolen(tmpsv);
a0d0e21e 1307 }
2dd78f96
JH
1308 if (name && *name)
1309 DIE(aTHX_ "Undefined format \"%s\" called", name);
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);
2dd78f96
JH
1391 {
1392 char *name = NULL;
1393 if (!cv) {
1394 SV *sv = sv_newmortal();
1395 gv_efullname4(sv, fgv, Nullch, FALSE);
1396 name = SvPV_nolen(sv);
1397 }
1398 if (name && *name)
1399 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1400 /* why no:
1401 else
1402 DIE(aTHX_ "Undefined top format called");
1403 ?*/
4633a7c4 1404 }
44a8e56a
PP
1405 if (CvCLONE(cv))
1406 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
533c011a 1407 return doform(cv,gv,PL_op);
a0d0e21e
LW
1408 }
1409
1410 forget_top:
3280af22 1411 POPBLOCK(cx,PL_curpm);
a0d0e21e
LW
1412 POPFORMAT(cx);
1413 LEAVE;
1414
1415 fp = IoOFP(io);
1416 if (!fp) {
599cee73 1417 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1418 if (IoIFP(io))
1419 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1420 else if (ckWARN(WARN_CLOSED))
bc37a18f 1421 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1422 }
3280af22 1423 PUSHs(&PL_sv_no);
a0d0e21e
LW
1424 }
1425 else {
3280af22 1426 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
599cee73 1427 if (ckWARN(WARN_IO))
9014280d 1428 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
a0d0e21e 1429 }
d75029d0 1430 if (!do_print(PL_formtarget, fp))
3280af22 1431 PUSHs(&PL_sv_no);
a0d0e21e 1432 else {
3280af22
NIS
1433 FmLINES(PL_formtarget) = 0;
1434 SvCUR_set(PL_formtarget, 0);
1435 *SvEND(PL_formtarget) = '\0';
a0d0e21e 1436 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1437 (void)PerlIO_flush(fp);
3280af22 1438 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1439 }
1440 }
9cbac4c7 1441 /* bad_ofp: */
3280af22 1442 PL_formtarget = PL_bodytarget;
a0d0e21e 1443 PUTBACK;
f39bc417 1444 return cx->blk_sub.retop;
a0d0e21e
LW
1445}
1446
1447PP(pp_prtf)
1448{
27da23d5 1449 dVAR; dSP; dMARK; dORIGMARK;
a0d0e21e
LW
1450 GV *gv;
1451 IO *io;
760ac839 1452 PerlIO *fp;
26db47c4 1453 SV *sv;
46fc3d4c 1454 MAGIC *mg;
a0d0e21e 1455
533c011a 1456 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1457 gv = (GV*)*++MARK;
1458 else
3280af22 1459 gv = PL_defoutgv;
46fc3d4c 1460
5b468f54
AMS
1461 if (gv && (io = GvIO(gv))
1462 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1463 {
46fc3d4c 1464 if (MARK == ORIGMARK) {
4352c267 1465 MEXTEND(SP, 1);
46fc3d4c
PP
1466 ++MARK;
1467 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1468 ++SP;
1469 }
1470 PUSHMARK(MARK - 1);
5b468f54 1471 *MARK = SvTIED_obj((SV*)io, mg);
46fc3d4c
PP
1472 PUTBACK;
1473 ENTER;
864dbfa3 1474 call_method("PRINTF", G_SCALAR);
46fc3d4c
PP
1475 LEAVE;
1476 SPAGAIN;
1477 MARK = ORIGMARK + 1;
1478 *MARK = *SP;
1479 SP = MARK;
1480 RETURN;
1481 }
1482
26db47c4 1483 sv = NEWSV(0,0);
a0d0e21e 1484 if (!(io = GvIO(gv))) {
2dd78f96
JH
1485 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1486 report_evil_fh(gv, io, PL_op->op_type);
93189314 1487 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
1488 goto just_say_no;
1489 }
1490 else if (!(fp = IoOFP(io))) {
599cee73 1491 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1492 if (IoIFP(io))
1493 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1494 else if (ckWARN(WARN_CLOSED))
bc37a18f 1495 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1496 }
93189314 1497 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
a0d0e21e
LW
1498 goto just_say_no;
1499 }
1500 else {
1501 do_sprintf(sv, SP - MARK, MARK + 1);
1502 if (!do_print(sv, fp))
1503 goto just_say_no;
1504
1505 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1506 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
1507 goto just_say_no;
1508 }
1509 SvREFCNT_dec(sv);
1510 SP = ORIGMARK;
3280af22 1511 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1512 RETURN;
1513
1514 just_say_no:
1515 SvREFCNT_dec(sv);
1516 SP = ORIGMARK;
3280af22 1517 PUSHs(&PL_sv_undef);
a0d0e21e
LW
1518 RETURN;
1519}
1520
c07a80fd
PP
1521PP(pp_sysopen)
1522{
39644a26 1523 dSP;
c07a80fd 1524 GV *gv;
c07a80fd
PP
1525 SV *sv;
1526 char *tmps;
1527 STRLEN len;
1528 int mode, perm;
1529
1530 if (MAXARG > 3)
1531 perm = POPi;
1532 else
1533 perm = 0666;
1534 mode = POPi;
1535 sv = POPs;
1536 gv = (GV *)POPs;
1537
4592e6ca
NIS
1538 /* Need TIEHANDLE method ? */
1539
c07a80fd
PP
1540 tmps = SvPV(sv, len);
1541 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1542 IoLINES(GvIOp(gv)) = 0;
3280af22 1543 PUSHs(&PL_sv_yes);
c07a80fd
PP
1544 }
1545 else {
3280af22 1546 PUSHs(&PL_sv_undef);
c07a80fd
PP
1547 }
1548 RETURN;
1549}
1550
a0d0e21e
LW
1551PP(pp_sysread)
1552{
27da23d5 1553 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
1554 int offset;
1555 GV *gv;
1556 IO *io;
1557 char *buffer;
5b54f415 1558 SSize_t length;
eb5c063a 1559 SSize_t count;
1e422769 1560 Sock_size_t bufsize;
748a9306 1561 SV *bufsv;
a0d0e21e 1562 STRLEN blen;
2ae324a7 1563 MAGIC *mg;
eb5c063a 1564 int fp_utf8;
1dd30107
NC
1565 int buffer_utf8;
1566 SV *read_target;
eb5c063a
NIS
1567 Size_t got = 0;
1568 Size_t wanted;
1d636c13 1569 bool charstart = FALSE;
87330c3c
JH
1570 STRLEN charskip = 0;
1571 STRLEN skip = 0;
a0d0e21e
LW
1572
1573 gv = (GV*)*++MARK;
5b468f54
AMS
1574 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1575 && gv && (io = GvIO(gv))
1576 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
137443ea 1577 {
2ae324a7
PP
1578 SV *sv;
1579
1580 PUSHMARK(MARK-1);
5b468f54 1581 *MARK = SvTIED_obj((SV*)io, mg);
2ae324a7 1582 ENTER;
864dbfa3 1583 call_method("READ", G_SCALAR);
2ae324a7
PP
1584 LEAVE;
1585 SPAGAIN;
1586 sv = POPs;
1587 SP = ORIGMARK;
1588 PUSHs(sv);
1589 RETURN;
1590 }
1591
a0d0e21e
LW
1592 if (!gv)
1593 goto say_undef;
748a9306 1594 bufsv = *++MARK;
ff68c719
PP
1595 if (! SvOK(bufsv))
1596 sv_setpvn(bufsv, "", 0);
a0d0e21e 1597 length = SvIVx(*++MARK);
748a9306 1598 SETERRNO(0,0);
a0d0e21e
LW
1599 if (MARK < SP)
1600 offset = SvIVx(*++MARK);
1601 else
1602 offset = 0;
1603 io = GvIO(gv);
b5fe5ca2
SR
1604 if (!io || !IoIFP(io)) {
1605 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1606 report_evil_fh(gv, io, PL_op->op_type);
1607 SETERRNO(EBADF,RMS_IFI);
a0d0e21e 1608 goto say_undef;
b5fe5ca2 1609 }
0064a8a9 1610 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
7d59b7e4 1611 buffer = SvPVutf8_force(bufsv, blen);
1e54db1a 1612 /* UTF-8 may not have been set if they are all low bytes */
eb5c063a 1613 SvUTF8_on(bufsv);
9b9d7ce8 1614 buffer_utf8 = 0;
7d59b7e4
NIS
1615 }
1616 else {
1617 buffer = SvPV_force(bufsv, blen);
1dd30107 1618 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
7d59b7e4
NIS
1619 }
1620 if (length < 0)
1621 DIE(aTHX_ "Negative length");
eb5c063a 1622 wanted = length;
7d59b7e4 1623
d0965105
JH
1624 charstart = TRUE;
1625 charskip = 0;
87330c3c 1626 skip = 0;
d0965105 1627
a0d0e21e 1628#ifdef HAS_SOCKET
533c011a 1629 if (PL_op->op_type == OP_RECV) {
46fc3d4c 1630 char namebuf[MAXPATHLEN];
17a8c7ba 1631#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
490ab354
JH
1632 bufsize = sizeof (struct sockaddr_in);
1633#else
46fc3d4c 1634 bufsize = sizeof namebuf;
490ab354 1635#endif
abf95952
IZ
1636#ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1637 if (bufsize >= 256)
1638 bufsize = 255;
1639#endif
eb160463 1640 buffer = SvGROW(bufsv, (STRLEN)(length+1));
bbce6d69 1641 /* 'offset' means 'flags' here */
eb5c063a 1642 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
46fc3d4c 1643 (struct sockaddr *)namebuf, &bufsize);
eb5c063a 1644 if (count < 0)
a0d0e21e 1645 RETPUSHUNDEF;
4107cc59
OF
1646#ifdef EPOC
1647 /* Bogus return without padding */
1648 bufsize = sizeof (struct sockaddr_in);
1649#endif
eb5c063a 1650 SvCUR_set(bufsv, count);
748a9306
LW
1651 *SvEND(bufsv) = '\0';
1652 (void)SvPOK_only(bufsv);
eb5c063a
NIS
1653 if (fp_utf8)
1654 SvUTF8_on(bufsv);
748a9306 1655 SvSETMAGIC(bufsv);
aac0dd9a 1656 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1657 if (!(IoFLAGS(io) & IOf_UNTAINT))
1658 SvTAINTED_on(bufsv);
a0d0e21e 1659 SP = ORIGMARK;
46fc3d4c 1660 sv_setpvn(TARG, namebuf, bufsize);
a0d0e21e
LW
1661 PUSHs(TARG);
1662 RETURN;
1663 }
1664#else
911d147d 1665 if (PL_op->op_type == OP_RECV)
cea2e8a9 1666 DIE(aTHX_ PL_no_sock_func, "recv");
a0d0e21e 1667#endif
eb5c063a
NIS
1668 if (DO_UTF8(bufsv)) {
1669 /* offset adjust in characters not bytes */
1670 blen = sv_len_utf8(bufsv);
7d59b7e4 1671 }
bbce6d69 1672 if (offset < 0) {
eb160463 1673 if (-offset > (int)blen)
cea2e8a9 1674 DIE(aTHX_ "Offset outside string");
bbce6d69
PP
1675 offset += blen;
1676 }
eb5c063a
NIS
1677 if (DO_UTF8(bufsv)) {
1678 /* convert offset-as-chars to offset-as-bytes */
6960c29a
CH
1679 if (offset >= (int)blen)
1680 offset += SvCUR(bufsv) - blen;
1681 else
1682 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
eb5c063a
NIS
1683 }
1684 more_bytes:
cd52b7b2 1685 bufsize = SvCUR(bufsv);
1dd30107
NC
1686 /* Allocating length + offset + 1 isn't perfect in the case of reading
1687 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1688 unduly.
1689 (should be 2 * length + offset + 1, or possibly something longer if
1690 PL_encoding is true) */
eb160463 1691 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
27da23d5 1692 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
cd52b7b2
PP
1693 Zero(buffer+bufsize, offset-bufsize, char);
1694 }
eb5c063a 1695 buffer = buffer + offset;
1dd30107
NC
1696 if (!buffer_utf8) {
1697 read_target = bufsv;
1698 } else {
1699 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1700 concatenate it to the current buffer. */
1701
1702 /* Truncate the existing buffer to the start of where we will be
1703 reading to: */
1704 SvCUR_set(bufsv, offset);
1705
1706 read_target = sv_newmortal();
8569b9dc 1707 (void)SvUPGRADE(read_target, SVt_PV);
fe2774ed 1708 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1dd30107 1709 }
eb5c063a 1710
533c011a 1711 if (PL_op->op_type == OP_SYSREAD) {
a7092146 1712#ifdef PERL_SOCK_SYSREAD_IS_RECV
50952442 1713 if (IoTYPE(io) == IoTYPE_SOCKET) {
eb5c063a
NIS
1714 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1715 buffer, length, 0);
a7092146
GS
1716 }
1717 else
1718#endif
1719 {
eb5c063a
NIS
1720 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1721 buffer, length);
a7092146 1722 }
a0d0e21e
LW
1723 }
1724 else
1725#ifdef HAS_SOCKET__bad_code_maybe
50952442 1726 if (IoTYPE(io) == IoTYPE_SOCKET) {
46fc3d4c 1727 char namebuf[MAXPATHLEN];
490ab354
JH
1728#if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1729 bufsize = sizeof (struct sockaddr_in);
1730#else
46fc3d4c 1731 bufsize = sizeof namebuf;
490ab354 1732#endif
eb5c063a 1733 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
46fc3d4c 1734 (struct sockaddr *)namebuf, &bufsize);
a0d0e21e
LW
1735 }
1736 else
1737#endif
3b02c43c 1738 {
eb5c063a
NIS
1739 count = PerlIO_read(IoIFP(io), buffer, length);
1740 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1741 if (count == 0 && PerlIO_error(IoIFP(io)))
1742 count = -1;
3b02c43c 1743 }
eb5c063a 1744 if (count < 0) {
a00b5bd3 1745 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
6e6ef6b2 1746 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
a0d0e21e 1747 goto say_undef;
af8c498a 1748 }
1dd30107
NC
1749 SvCUR_set(read_target, count+(buffer - SvPVX(read_target)));
1750 *SvEND(read_target) = '\0';
1751 (void)SvPOK_only(read_target);
0064a8a9 1752 if (fp_utf8 && !IN_BYTES) {
eb5c063a
NIS
1753 /* Look at utf8 we got back and count the characters */
1754 char *bend = buffer + count;
1755 while (buffer < bend) {
d0965105
JH
1756 if (charstart) {
1757 skip = UTF8SKIP(buffer);
1758 charskip = 0;
1759 }
1760 if (buffer - charskip + skip > bend) {
eb5c063a
NIS
1761 /* partial character - try for rest of it */
1762 length = skip - (bend-buffer);
1763 offset = bend - SvPVX(bufsv);
d0965105
JH
1764 charstart = FALSE;
1765 charskip += count;
eb5c063a
NIS
1766 goto more_bytes;
1767 }
1768 else {
1769 got++;
1770 buffer += skip;
d0965105
JH
1771 charstart = TRUE;
1772 charskip = 0;
eb5c063a
NIS
1773 }
1774 }
1775 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1776 provided amount read (count) was what was requested (length)
1777 */
1778 if (got < wanted && count == length) {
d0965105 1779 length = wanted - got;
eb5c063a
NIS
1780 offset = bend - SvPVX(bufsv);
1781 goto more_bytes;
1782 }
1783 /* return value is character count */
1784 count = got;
1785 SvUTF8_on(bufsv);
1786 }
1dd30107
NC
1787 else if (buffer_utf8) {
1788 /* Let svcatsv upgrade the bytes we read in to utf8.
1789 The buffer is a mortal so will be freed soon. */
1790 sv_catsv_nomg(bufsv, read_target);
1791 }
748a9306 1792 SvSETMAGIC(bufsv);
aac0dd9a 1793 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1794 if (!(IoFLAGS(io) & IOf_UNTAINT))
1795 SvTAINTED_on(bufsv);
a0d0e21e 1796 SP = ORIGMARK;
eb5c063a 1797 PUSHi(count);
a0d0e21e
LW
1798 RETURN;
1799
1800 say_undef:
1801 SP = ORIGMARK;
1802 RETPUSHUNDEF;
1803}
1804
1805PP(pp_syswrite)
1806{
27da23d5 1807 dVAR; dSP;
092bebab
JH
1808 int items = (SP - PL_stack_base) - TOPMARK;
1809 if (items == 2) {
9f089d78 1810 SV *sv;
092bebab 1811 EXTEND(SP, 1);
9f089d78
SB
1812 sv = sv_2mortal(newSViv(sv_len(*SP)));
1813 PUSHs(sv);
092bebab
JH
1814 PUTBACK;
1815 }
cea2e8a9 1816 return pp_send();
a0d0e21e
LW
1817}
1818
1819PP(pp_send)
1820{
27da23d5 1821 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
1822 GV *gv;
1823 IO *io;
748a9306 1824 SV *bufsv;
a0d0e21e 1825 char *buffer;
8c99d73e
GS
1826 Size_t length;
1827 SSize_t retval;
a0d0e21e 1828 STRLEN blen;
1d603a67 1829 MAGIC *mg;
a0d0e21e
LW
1830
1831 gv = (GV*)*++MARK;
14befaf4 1832 if (PL_op->op_type == OP_SYSWRITE
5b468f54
AMS
1833 && gv && (io = GvIO(gv))
1834 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
14befaf4 1835 {
1d603a67
GB
1836 SV *sv;
1837
1838 PUSHMARK(MARK-1);
5b468f54 1839 *MARK = SvTIED_obj((SV*)io, mg);
1d603a67 1840 ENTER;
864dbfa3 1841 call_method("WRITE", G_SCALAR);
1d603a67
GB
1842 LEAVE;
1843 SPAGAIN;
1844 sv = POPs;
1845 SP = ORIGMARK;
1846 PUSHs(sv);
1847 RETURN;
1848 }
a0d0e21e
LW
1849 if (!gv)
1850 goto say_undef;
748a9306 1851 bufsv = *++MARK;
8c99d73e 1852#if Size_t_size > IVSIZE
3c001241 1853 length = (Size_t)SvNVx(*++MARK);
146174a9 1854#else
3c001241 1855 length = (Size_t)SvIVx(*++MARK);
146174a9 1856#endif
3c001241 1857 if ((SSize_t)length < 0)
cea2e8a9 1858 DIE(aTHX_ "Negative length");
748a9306 1859 SETERRNO(0,0);
a0d0e21e
LW
1860 io = GvIO(gv);
1861 if (!io || !IoIFP(io)) {
8c99d73e 1862 retval = -1;
bc37a18f
RG
1863 if (ckWARN(WARN_CLOSED))
1864 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 1865 SETERRNO(EBADF,RMS_IFI);
7d59b7e4
NIS
1866 goto say_undef;
1867 }
1868
1869 if (PerlIO_isutf8(IoIFP(io))) {
6aa2f6a7
NC
1870 if (!SvUTF8(bufsv)) {
1871 bufsv = sv_2mortal(newSVsv(bufsv));
1872 buffer = sv_2pvutf8(bufsv, &blen);
1873 } else
1874 buffer = SvPV(bufsv, blen);
a0d0e21e 1875 }
7d59b7e4 1876 else {
4de5f5ca
JH
1877 if (DO_UTF8(bufsv)) {
1878 /* Not modifying source SV, so making a temporary copy. */
1879 bufsv = sv_2mortal(newSVsv(bufsv));
1880 sv_utf8_downgrade(bufsv, FALSE);
1881 }
1882 buffer = SvPV(bufsv, blen);
7d59b7e4
NIS
1883 }
1884
1885 if (PL_op->op_type == OP_SYSWRITE) {
1886 IV offset;
1887 if (DO_UTF8(bufsv)) {
1888 /* length and offset are in chars */
1889 blen = sv_len_utf8(bufsv);
1890 }
bbce6d69 1891 if (MARK < SP) {
a0d0e21e 1892 offset = SvIVx(*++MARK);
bbce6d69 1893 if (offset < 0) {
eb160463 1894 if (-offset > (IV)blen)
cea2e8a9 1895 DIE(aTHX_ "Offset outside string");
bbce6d69 1896 offset += blen;
eb160463 1897 } else if (offset >= (IV)blen && blen > 0)
cea2e8a9 1898 DIE(aTHX_ "Offset outside string");
bbce6d69 1899 } else
a0d0e21e
LW
1900 offset = 0;
1901 if (length > blen - offset)
1902 length = blen - offset;
7d59b7e4 1903 if (DO_UTF8(bufsv)) {
c8d31a35 1904 buffer = (char*)utf8_hop((U8 *)buffer, offset);
7d59b7e4
NIS
1905 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1906 }
1907 else {
1908 buffer = buffer+offset;
1909 }
a7092146 1910#ifdef PERL_SOCK_SYSWRITE_IS_SEND
50952442 1911 if (IoTYPE(io) == IoTYPE_SOCKET) {
8c99d73e 1912 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1913 buffer, length, 0);
a7092146
GS
1914 }
1915 else
1916#endif
1917 {
94e4c244 1918 /* See the note at doio.c:do_print about filesize limits. --jhi */
8c99d73e 1919 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1920 buffer, length);
a7092146 1921 }
a0d0e21e
LW
1922 }
1923#ifdef HAS_SOCKET
1924 else if (SP > MARK) {
1925 char *sockbuf;
1926 STRLEN mlen;
1927 sockbuf = SvPVx(*++MARK, mlen);
7d59b7e4 1928 /* length is really flags */
8c99d73e
GS
1929 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1930 length, (struct sockaddr *)sockbuf, mlen);
a0d0e21e
LW
1931 }
1932 else
7d59b7e4 1933 /* length is really flags */
8c99d73e 1934 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
a0d0e21e
LW
1935#else
1936 else
cea2e8a9 1937 DIE(aTHX_ PL_no_sock_func, "send");
a0d0e21e 1938#endif
8c99d73e 1939 if (retval < 0)
a0d0e21e
LW
1940 goto say_undef;
1941 SP = ORIGMARK;
f36eea10
JH
1942 if (DO_UTF8(bufsv))
1943 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
8c99d73e
GS
1944#if Size_t_size > IVSIZE
1945 PUSHn(retval);
1946#else
1947 PUSHi(retval);
1948#endif
a0d0e21e
LW
1949 RETURN;
1950
1951 say_undef:
1952 SP = ORIGMARK;
1953 RETPUSHUNDEF;
1954}
1955
1956PP(pp_recv)
1957{
cea2e8a9 1958 return pp_sysread();
a0d0e21e
LW
1959}
1960
1961PP(pp_eof)
1962{
27da23d5 1963 dVAR; dSP;
a0d0e21e 1964 GV *gv;
5b468f54 1965 IO *io;
4592e6ca 1966 MAGIC *mg;
a0d0e21e 1967
32da55ab 1968 if (MAXARG == 0) {
146174a9
CB
1969 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1970 IO *io;
ed2c6b9b 1971 gv = PL_last_in_gv = GvEGV(PL_argvgv);
146174a9
CB
1972 io = GvIO(gv);
1973 if (io && !IoIFP(io)) {
1974 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1975 IoLINES(io) = 0;
1976 IoFLAGS(io) &= ~IOf_START;
dd374669 1977 do_open(gv, (char *)"-", 1, FALSE, O_RDONLY, 0, Nullfp);
146174a9
CB
1978 sv_setpvn(GvSV(gv), "-", 1);
1979 SvSETMAGIC(GvSV(gv));
1980 }
1981 else if (!nextargv(gv))
1982 RETPUSHYES;
1983 }
1984 }
1985 else
1986 gv = PL_last_in_gv; /* eof */
1987 }
a0d0e21e 1988 else
146174a9 1989 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
4592e6ca 1990
5b468f54
AMS
1991 if (gv && (io = GvIO(gv))
1992 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1993 {
4592e6ca 1994 PUSHMARK(SP);
5b468f54 1995 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
1996 PUTBACK;
1997 ENTER;
864dbfa3 1998 call_method("EOF", G_SCALAR);
4592e6ca
NIS
1999 LEAVE;
2000 SPAGAIN;
2001 RETURN;
2002 }
2003
54310121 2004 PUSHs(boolSV(!gv || do_eof(gv)));
a0d0e21e
LW
2005 RETURN;
2006}
2007
2008PP(pp_tell)
2009{
27da23d5 2010 dVAR; dSP; dTARGET;
301e8125 2011 GV *gv;
5b468f54 2012 IO *io;
4592e6ca 2013 MAGIC *mg;
a0d0e21e 2014
32da55ab 2015 if (MAXARG == 0)
3280af22 2016 gv = PL_last_in_gv;
a0d0e21e 2017 else
3280af22 2018 gv = PL_last_in_gv = (GV*)POPs;
4592e6ca 2019
5b468f54
AMS
2020 if (gv && (io = GvIO(gv))
2021 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2022 {
4592e6ca 2023 PUSHMARK(SP);
5b468f54 2024 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
2025 PUTBACK;
2026 ENTER;
864dbfa3 2027 call_method("TELL", G_SCALAR);
4592e6ca
NIS
2028 LEAVE;
2029 SPAGAIN;
2030 RETURN;
2031 }
2032
146174a9
CB
2033#if LSEEKSIZE > IVSIZE
2034 PUSHn( do_tell(gv) );
2035#else
a0d0e21e 2036 PUSHi( do_tell(gv) );
146174a9 2037#endif
a0d0e21e
LW
2038 RETURN;
2039}
2040
2041PP(pp_seek)
2042{
cea2e8a9 2043 return pp_sysseek();
137443ea
PP
2044}
2045
2046PP(pp_sysseek)
2047{
27da23d5 2048 dVAR; dSP;
a0d0e21e 2049 GV *gv;
5b468f54 2050 IO *io;
a0d0e21e 2051 int whence = POPi;
146174a9
CB
2052#if LSEEKSIZE > IVSIZE
2053 Off_t offset = (Off_t)SvNVx(POPs);
2054#else
d9b3e12d 2055 Off_t offset = (Off_t)SvIVx(POPs);
146174a9 2056#endif
4592e6ca 2057 MAGIC *mg;
a0d0e21e 2058
3280af22 2059 gv = PL_last_in_gv = (GV*)POPs;
4592e6ca 2060
5b468f54
AMS
2061 if (gv && (io = GvIO(gv))
2062 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2063 {
4592e6ca 2064 PUSHMARK(SP);
5b468f54 2065 XPUSHs(SvTIED_obj((SV*)io, mg));
cb50131a
CB
2066#if LSEEKSIZE > IVSIZE
2067 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2068#else
b448e4fe 2069 XPUSHs(sv_2mortal(newSViv(offset)));
cb50131a 2070#endif
b448e4fe 2071 XPUSHs(sv_2mortal(newSViv(whence)));
4592e6ca
NIS
2072 PUTBACK;
2073 ENTER;
864dbfa3 2074 call_method("SEEK", G_SCALAR);
4592e6ca
NIS
2075 LEAVE;
2076 SPAGAIN;
2077 RETURN;
2078 }
2079
533c011a 2080 if (PL_op->op_type == OP_SEEK)
8903cb82
PP
2081 PUSHs(boolSV(do_seek(gv, offset, whence)));
2082 else {
b448e4fe
JH
2083 Off_t sought = do_sysseek(gv, offset, whence);
2084 if (sought < 0)
146174a9
CB
2085 PUSHs(&PL_sv_undef);
2086 else {
b448e4fe 2087 SV* sv = sought ?
146174a9 2088#if LSEEKSIZE > IVSIZE
b448e4fe 2089 newSVnv((NV)sought)
146174a9 2090#else
b448e4fe 2091 newSViv(sought)
146174a9
CB
2092#endif
2093 : newSVpvn(zero_but_true, ZBTLEN);
2094 PUSHs(sv_2mortal(sv));
2095 }
8903cb82 2096 }
a0d0e21e
LW
2097 RETURN;
2098}
2099
2100PP(pp_truncate)
2101{
39644a26 2102 dSP;
8c99d73e
GS
2103 /* There seems to be no consensus on the length type of truncate()
2104 * and ftruncate(), both off_t and size_t have supporters. In
2105 * general one would think that when using large files, off_t is
2106 * at least as wide as size_t, so using an off_t should be okay. */
2107 /* XXX Configure probe for the length type of *truncate() needed XXX */
2108 Off_t len;
a0d0e21e 2109
25342a55 2110#if Off_t_size > IVSIZE
8c99d73e
GS
2111 len = (Off_t)POPn;
2112#else
2113 len = (Off_t)POPi;
2114#endif
2115 /* Checking for length < 0 is problematic as the type might or
301e8125 2116 * might not be signed: if it is not, clever compilers will moan. */
8c99d73e 2117 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
748a9306 2118 SETERRNO(0,0);
d05c1ba0 2119 {
d05c1ba0
JH
2120 int result = 1;
2121 GV *tmpgv;
090bf15b
SR
2122 IO *io;
2123
d05c1ba0 2124 if (PL_op->op_flags & OPf_SPECIAL) {
7a5fd60d 2125 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
d05c1ba0 2126
090bf15b
SR
2127 do_ftruncate_gv:
2128 if (!GvIO(tmpgv))
2129 result = 0;
d05c1ba0 2130 else {
090bf15b
SR
2131 PerlIO *fp;
2132 io = GvIOp(tmpgv);
2133 do_ftruncate_io:
2134 TAINT_PROPER("truncate");
2135 if (!(fp = IoIFP(io))) {
2136 result = 0;
2137 }
2138 else {
2139 PerlIO_flush(fp);
cbdc8872 2140#ifdef HAS_TRUNCATE
090bf15b 2141 if (ftruncate(PerlIO_fileno(fp), len) < 0)
301e8125 2142#else
090bf15b 2143 if (my_chsize(PerlIO_fileno(fp), len) < 0)
cbdc8872 2144#endif
090bf15b
SR
2145 result = 0;
2146 }
d05c1ba0 2147 }
cbdc8872 2148 }
d05c1ba0
JH
2149 else {
2150 SV *sv = POPs;
2151 char *name;
7a5fd60d
NC
2152 STRLEN n_a;
2153
d05c1ba0
JH
2154 if (SvTYPE(sv) == SVt_PVGV) {
2155 tmpgv = (GV*)sv; /* *main::FRED for example */
090bf15b 2156 goto do_ftruncate_gv;
d05c1ba0
JH
2157 }
2158 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2159 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
090bf15b
SR
2160 goto do_ftruncate_gv;
2161 }
2162 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2163 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2164 goto do_ftruncate_io;
d05c1ba0 2165 }
1e422769 2166
d05c1ba0
JH
2167 name = SvPV(sv, n_a);
2168 TAINT_PROPER("truncate");
cbdc8872 2169#ifdef HAS_TRUNCATE
d05c1ba0
JH
2170 if (truncate(name, len) < 0)
2171 result = 0;
cbdc8872 2172#else
d05c1ba0
JH
2173 {
2174 int tmpfd;
2175
2176 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
cbdc8872 2177 result = 0;
d05c1ba0
JH
2178 else {
2179 if (my_chsize(tmpfd, len) < 0)
2180 result = 0;
2181 PerlLIO_close(tmpfd);
2182 }
cbdc8872 2183 }
a0d0e21e 2184#endif
d05c1ba0 2185 }
a0d0e21e 2186
d05c1ba0
JH
2187 if (result)
2188 RETPUSHYES;
2189 if (!errno)
93189314 2190 SETERRNO(EBADF,RMS_IFI);
d05c1ba0
JH
2191 RETPUSHUNDEF;
2192 }
a0d0e21e
LW
2193}
2194
2195PP(pp_fcntl)
2196{
cea2e8a9 2197 return pp_ioctl();
a0d0e21e
LW
2198}
2199
2200PP(pp_ioctl)
2201{
39644a26 2202 dSP; dTARGET;
748a9306 2203 SV *argsv = POPs;
3bb7c1b4 2204 unsigned int func = POPu;
e1ec3a88 2205 const int optype = PL_op->op_type;
a0d0e21e 2206 char *s;
324aa91a 2207 IV retval;
a0d0e21e 2208 GV *gv = (GV*)POPs;
c289d2f7 2209 IO *io = gv ? GvIOn(gv) : 0;
a0d0e21e 2210
748a9306 2211 if (!io || !argsv || !IoIFP(io)) {
c289d2f7
JH
2212 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2213 report_evil_fh(gv, io, PL_op->op_type);
93189314 2214 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
a0d0e21e
LW
2215 RETPUSHUNDEF;
2216 }
2217
748a9306 2218 if (SvPOK(argsv) || !SvNIOK(argsv)) {
a0d0e21e 2219 STRLEN len;
324aa91a 2220 STRLEN need;
748a9306 2221 s = SvPV_force(argsv, len);
324aa91a
HF
2222 need = IOCPARM_LEN(func);
2223 if (len < need) {
2224 s = Sv_Grow(argsv, need + 1);
2225 SvCUR_set(argsv, need);
a0d0e21e
LW
2226 }
2227
748a9306 2228 s[SvCUR(argsv)] = 17; /* a little sanity check here */
a0d0e21e
LW
2229 }
2230 else {
748a9306 2231 retval = SvIV(argsv);
c529f79d 2232 s = INT2PTR(char*,retval); /* ouch */
a0d0e21e
LW
2233 }
2234
2235 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2236
2237 if (optype == OP_IOCTL)
2238#ifdef HAS_IOCTL
76e3520e 2239 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
a0d0e21e 2240#else
cea2e8a9 2241 DIE(aTHX_ "ioctl is not implemented");
a0d0e21e
LW
2242#endif
2243 else
c214f4ad
B
2244#ifndef HAS_FCNTL
2245 DIE(aTHX_ "fcntl is not implemented");
2246#else
55497cff 2247#if defined(OS2) && defined(__EMX__)
760ac839 2248 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
55497cff 2249#else
760ac839 2250 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
301e8125 2251#endif
6652bd42 2252#endif
a0d0e21e 2253
6652bd42 2254#if defined(HAS_IOCTL) || defined(HAS_FCNTL)
748a9306
LW
2255 if (SvPOK(argsv)) {
2256 if (s[SvCUR(argsv)] != 17)
cea2e8a9 2257 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
53e06cf0 2258 OP_NAME(PL_op));
748a9306
LW
2259 s[SvCUR(argsv)] = 0; /* put our null back */
2260 SvSETMAGIC(argsv); /* Assume it has changed */
a0d0e21e
LW
2261 }
2262
2263 if (retval == -1)
2264 RETPUSHUNDEF;
2265 if (retval != 0) {
2266 PUSHi(retval);
2267 }
2268 else {
8903cb82 2269 PUSHp(zero_but_true, ZBTLEN);
a0d0e21e 2270 }
4808266b 2271#endif
c214f4ad 2272 RETURN;
a0d0e21e
LW
2273}
2274
2275PP(pp_flock)
2276{
9cad6237 2277#ifdef FLOCK
39644a26 2278 dSP; dTARGET;
a0d0e21e
LW
2279 I32 value;
2280 int argtype;
2281 GV *gv;
bc37a18f 2282 IO *io = NULL;
760ac839 2283 PerlIO *fp;
16d20bd9 2284
a0d0e21e 2285 argtype = POPi;
32da55ab 2286 if (MAXARG == 0)
3280af22 2287 gv = PL_last_in_gv;
a0d0e21e
LW
2288 else
2289 gv = (GV*)POPs;
bc37a18f
RG
2290 if (gv && (io = GvIO(gv)))
2291 fp = IoIFP(io);
2292 else {
a0d0e21e 2293 fp = Nullfp;
bc37a18f
RG
2294 io = NULL;
2295 }
a0d0e21e 2296 if (fp) {
68dc0745 2297 (void)PerlIO_flush(fp);
76e3520e 2298 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
a0d0e21e 2299 }
cb50131a 2300 else {
bc37a18f
RG
2301 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2302 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 2303 value = 0;
93189314 2304 SETERRNO(EBADF,RMS_IFI);
cb50131a 2305 }
a0d0e21e
LW
2306 PUSHi(value);
2307 RETURN;
2308#else
cea2e8a9 2309 DIE(aTHX_ PL_no_func, "flock()");
a0d0e21e
LW
2310#endif
2311}
2312
2313/* Sockets. */
2314
2315PP(pp_socket)
2316{
a0d0e21e 2317#ifdef HAS_SOCKET
9cad6237 2318 dSP;
a0d0e21e
LW
2319 GV *gv;
2320 register IO *io;
2321 int protocol = POPi;
2322 int type = POPi;
2323 int domain = POPi;
2324 int fd;
2325
2326 gv = (GV*)POPs;
c289d2f7 2327 io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2328
c289d2f7
JH
2329 if (!gv || !io) {
2330 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2331 report_evil_fh(gv, io, PL_op->op_type);
2332 if (IoIFP(io))
2333 do_close(gv, FALSE);
93189314 2334 SETERRNO(EBADF,LIB_INVARG);
a0d0e21e
LW
2335 RETPUSHUNDEF;
2336 }
2337
57171420
BS
2338 if (IoIFP(io))
2339 do_close(gv, FALSE);
2340
a0d0e21e 2341 TAINT_PROPER("socket");
6ad3d225 2342 fd = PerlSock_socket(domain, type, protocol);
a0d0e21e
LW
2343 if (fd < 0)
2344 RETPUSHUNDEF;
460c8493
IZ
2345 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2346 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2347 IoTYPE(io) = IoTYPE_SOCKET;
a0d0e21e 2348 if (!IoIFP(io) || !IoOFP(io)) {
760ac839
LW
2349 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2350 if (IoOFP(io)) PerlIO_close(IoOFP(io));
6ad3d225 2351 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
a0d0e21e
LW
2352 RETPUSHUNDEF;
2353 }
8d2a6795
GS
2354#if defined(HAS_FCNTL) && defined(F_SETFD)
2355 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2356#endif
a0d0e21e 2357
d5ff79b3
OF
2358#ifdef EPOC
2359 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2360#endif
2361
a0d0e21e
LW
2362 RETPUSHYES;
2363#else
cea2e8a9 2364 DIE(aTHX_ PL_no_sock_func, "socket");
a0d0e21e
LW
2365#endif
2366}
2367
2368PP(pp_sockpair)
2369{
c95c94b1 2370#if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
76ffd3b9 2371 dSP;
a0d0e21e
LW
2372 GV *gv1;
2373 GV *gv2;
2374 register IO *io1;
2375 register IO *io2;
2376 int protocol = POPi;
2377 int type = POPi;
2378 int domain = POPi;
2379 int fd[2];
2380
2381 gv2 = (GV*)POPs;
2382 gv1 = (GV*)POPs;
c289d2f7
JH
2383 io1 = gv1 ? GvIOn(gv1) : NULL;
2384 io2 = gv2 ? GvIOn(gv2) : NULL;
2385 if (!gv1 || !gv2 || !io1 || !io2) {
2386 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2387 if (!gv1 || !io1)
2388 report_evil_fh(gv1, io1, PL_op->op_type);
2389 if (!gv2 || !io2)
2390 report_evil_fh(gv1, io2, PL_op->op_type);
2391 }
2392 if (IoIFP(io1))
2393 do_close(gv1, FALSE);
2394 if (IoIFP(io2))
2395 do_close(gv2, FALSE);
a0d0e21e 2396 RETPUSHUNDEF;
c289d2f7 2397 }
a0d0e21e 2398
dc0d0a5f
JH
2399 if (IoIFP(io1))
2400 do_close(gv1, FALSE);
2401 if (IoIFP(io2))
2402 do_close(gv2, FALSE);
57171420 2403
a0d0e21e 2404 TAINT_PROPER("socketpair");
6ad3d225 2405 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
a0d0e21e 2406 RETPUSHUNDEF;
460c8493
IZ
2407 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2408 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
50952442 2409 IoTYPE(io1) = IoTYPE_SOCKET;
460c8493
IZ
2410 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2411 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
50952442 2412 IoTYPE(io2) = IoTYPE_SOCKET;
a0d0e21e 2413 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
760ac839
LW
2414 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2415 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
6ad3d225 2416 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
760ac839
LW
2417 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2418 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
6ad3d225 2419 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
a0d0e21e
LW
2420 RETPUSHUNDEF;
2421 }
8d2a6795
GS
2422#if defined(HAS_FCNTL) && defined(F_SETFD)
2423 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2424 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2425#endif
a0d0e21e
LW
2426
2427 RETPUSHYES;
2428#else
cea2e8a9 2429 DIE(aTHX_ PL_no_sock_func, "socketpair");
a0d0e21e
LW
2430#endif
2431}
2432
2433PP(pp_bind)
2434{
a0d0e21e 2435#ifdef HAS_SOCKET
9cad6237 2436 dSP;
eec2d3df 2437#ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
82b3da69
JH
2438 extern void GETPRIVMODE();
2439 extern void GETUSERMODE();
eec2d3df 2440#endif
748a9306 2441 SV *addrsv = POPs;
a0d0e21e
LW
2442 char *addr;
2443 GV *gv = (GV*)POPs;
2444 register IO *io = GvIOn(gv);
2445 STRLEN len;
eec2d3df
GS
2446 int bind_ok = 0;
2447#ifdef MPE
2448 int mpeprivmode = 0;
2449#endif
a0d0e21e
LW
2450
2451 if (!io || !IoIFP(io))
2452 goto nuts;
2453
748a9306 2454 addr = SvPV(addrsv, len);
a0d0e21e 2455 TAINT_PROPER("bind");
eec2d3df
GS
2456#ifdef MPE /* Deal with MPE bind() peculiarities */
2457 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2458 /* The address *MUST* stupidly be zero. */
2459 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2460 /* PRIV mode is required to bind() to ports < 1024. */
2461 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2462 ((struct sockaddr_in *)addr)->sin_port > 0) {
2463 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2464 mpeprivmode = 1;
2465 }
2466 }
2467#endif /* MPE */
2468 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2469 (struct sockaddr *)addr, len) >= 0)
2470 bind_ok = 1;
2471
2472#ifdef MPE /* Switch back to USER mode */
2473 if (mpeprivmode)
2474 GETUSERMODE();
2475#endif /* MPE */
2476
2477 if (bind_ok)
a0d0e21e
LW
2478 RETPUSHYES;
2479 else
2480 RETPUSHUNDEF;
2481
2482nuts:
599cee73 2483 if (ckWARN(WARN_CLOSED))
bc37a18f 2484 report_evil_fh(gv, io, PL_op->op_type);
93189314 2485 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2486 RETPUSHUNDEF;
2487#else
cea2e8a9 2488 DIE(aTHX_ PL_no_sock_func, "bind");
a0d0e21e
LW
2489#endif
2490}
2491
2492PP(pp_connect)
2493{
a0d0e21e 2494#ifdef HAS_SOCKET
9cad6237 2495 dSP;
748a9306 2496 SV *addrsv = POPs;
a0d0e21e
LW
2497 char *addr;
2498 GV *gv = (GV*)POPs;
2499 register IO *io = GvIOn(gv);
2500 STRLEN len;
2501
2502 if (!io || !IoIFP(io))
2503 goto nuts;
2504
748a9306 2505 addr = SvPV(addrsv, len);
a0d0e21e 2506 TAINT_PROPER("connect");
6ad3d225 2507 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
a0d0e21e
LW
2508 RETPUSHYES;
2509 else
2510 RETPUSHUNDEF;
2511
2512nuts:
599cee73 2513 if (ckWARN(WARN_CLOSED))
bc37a18f 2514 report_evil_fh(gv, io, PL_op->op_type);
93189314 2515 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2516 RETPUSHUNDEF;
2517#else
cea2e8a9 2518 DIE(aTHX_ PL_no_sock_func, "connect");
a0d0e21e
LW
2519#endif
2520}
2521
2522PP(pp_listen)
2523{
a0d0e21e 2524#ifdef HAS_SOCKET
9cad6237 2525 dSP;
a0d0e21e
LW
2526 int backlog = POPi;
2527 GV *gv = (GV*)POPs;
c289d2f7 2528 register IO *io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2529
c289d2f7 2530 if (!gv || !io || !IoIFP(io))
a0d0e21e
LW
2531 goto nuts;
2532
6ad3d225 2533 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
a0d0e21e
LW
2534 RETPUSHYES;
2535 else
2536 RETPUSHUNDEF;
2537
2538nuts:
599cee73 2539 if (ckWARN(WARN_CLOSED))
bc37a18f 2540 report_evil_fh(gv, io, PL_op->op_type);
93189314 2541 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2542 RETPUSHUNDEF;
2543#else
cea2e8a9 2544 DIE(aTHX_ PL_no_sock_func, "listen");
a0d0e21e
LW
2545#endif
2546}
2547
2548PP(pp_accept)
2549{
a0d0e21e 2550#ifdef HAS_SOCKET
9cad6237 2551 dSP; dTARGET;
a0d0e21e
LW
2552 GV *ngv;
2553 GV *ggv;
2554 register IO *nstio;
2555 register IO *gstio;
93d47a36
JH
2556 char namebuf[MAXPATHLEN];
2557#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2558 Sock_size_t len = sizeof (struct sockaddr_in);
2559#else
2560 Sock_size_t len = sizeof namebuf;
2561#endif
a0d0e21e
LW
2562 int fd;
2563
2564 ggv = (GV*)POPs;
2565 ngv = (GV*)POPs;
2566
2567 if (!ngv)
2568 goto badexit;
2569 if (!ggv)
2570 goto nuts;
2571
2572 gstio = GvIO(ggv);
2573 if (!gstio || !IoIFP(gstio))
2574 goto nuts;
2575
2576 nstio = GvIOn(ngv);
93d47a36 2577 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
a0d0e21e
LW
2578 if (fd < 0)
2579 goto badexit;
a70048fb
AB
2580 if (IoIFP(nstio))
2581 do_close(ngv, FALSE);
460c8493
IZ
2582 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2583 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2584 IoTYPE(nstio) = IoTYPE_SOCKET;
a0d0e21e 2585 if (!IoIFP(nstio) || !IoOFP(nstio)) {
760ac839
LW
2586 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2587 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
6ad3d225 2588 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
a0d0e21e
LW
2589 goto badexit;
2590 }
8d2a6795
GS
2591#if defined(HAS_FCNTL) && defined(F_SETFD)
2592 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2593#endif
a0d0e21e 2594
ed79a026 2595#ifdef EPOC
93d47a36 2596 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
a9f1f6b0 2597 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
ed79a026 2598#endif
381c1bae 2599#ifdef __SCO_VERSION__
93d47a36 2600 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
381c1bae 2601#endif
ed79a026 2602
93d47a36 2603 PUSHp(namebuf, len);
a0d0e21e
LW
2604 RETURN;
2605
2606nuts:
599cee73 2607 if (ckWARN(WARN_CLOSED))
bc37a18f 2608 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
93189314 2609 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2610
2611badexit:
2612 RETPUSHUNDEF;
2613
2614#else
cea2e8a9 2615 DIE(aTHX_ PL_no_sock_func, "accept");
a0d0e21e
LW
2616#endif
2617}
2618
2619PP(pp_shutdown)
2620{
a0d0e21e 2621#ifdef HAS_SOCKET
9cad6237 2622 dSP; dTARGET;
a0d0e21e
LW
2623 int how = POPi;
2624 GV *gv = (GV*)POPs;
2625 register IO *io = GvIOn(gv);
2626
2627 if (!io || !IoIFP(io))
2628 goto nuts;
2629
6ad3d225 2630 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
a0d0e21e
LW
2631 RETURN;
2632
2633nuts:
599cee73 2634 if (ckWARN(WARN_CLOSED))
bc37a18f 2635 report_evil_fh(gv, io, PL_op->op_type);
93189314 2636 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2637 RETPUSHUNDEF;
2638#else
cea2e8a9 2639 DIE(aTHX_ PL_no_sock_func, "shutdown");
a0d0e21e
LW
2640#endif
2641}
2642
2643PP(pp_gsockopt)
2644{
2645#ifdef HAS_SOCKET
cea2e8a9 2646 return pp_ssockopt();
a0d0e21e 2647#else
cea2e8a9 2648 DIE(aTHX_ PL_no_sock_func, "getsockopt");
a0d0e21e
LW
2649#endif
2650}
2651
2652PP(pp_ssockopt)
2653{
a0d0e21e 2654#ifdef HAS_SOCKET
9cad6237 2655 dSP;
533c011a 2656 int optype = PL_op->op_type;
a0d0e21e
LW
2657 SV *sv;
2658 int fd;
2659 unsigned int optname;
2660 unsigned int lvl;
2661 GV *gv;
2662 register IO *io;
1e422769 2663 Sock_size_t len;
a0d0e21e
LW
2664
2665 if (optype == OP_GSOCKOPT)
2666 sv = sv_2mortal(NEWSV(22, 257));
2667 else
2668 sv = POPs;
2669 optname = (unsigned int) POPi;
2670 lvl = (unsigned int) POPi;
2671
2672 gv = (GV*)POPs;
2673 io = GvIOn(gv);
2674 if (!io || !IoIFP(io))
2675 goto nuts;
2676
760ac839 2677 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2678 switch (optype) {
2679 case OP_GSOCKOPT:
748a9306 2680 SvGROW(sv, 257);
a0d0e21e 2681 (void)SvPOK_only(sv);
748a9306
LW
2682 SvCUR_set(sv,256);
2683 *SvEND(sv) ='\0';
1e422769 2684 len = SvCUR(sv);
6ad3d225 2685 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
a0d0e21e 2686 goto nuts2;
1e422769 2687 SvCUR_set(sv, len);
748a9306 2688 *SvEND(sv) ='\0';
a0d0e21e
LW
2689 PUSHs(sv);
2690 break;
2691 case OP_SSOCKOPT: {
1e422769
PP
2692 char *buf;
2693 int aint;
2694 if (SvPOKp(sv)) {
2d8e6c8d
GS
2695 STRLEN l;
2696 buf = SvPV(sv, l);
2697 len = l;
1e422769 2698 }
56ee1660 2699 else {
a0d0e21e
LW
2700 aint = (int)SvIV(sv);
2701 buf = (char*)&aint;
2702 len = sizeof(int);
2703 }
6ad3d225 2704 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
a0d0e21e 2705 goto nuts2;
3280af22 2706 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2707 }
2708 break;
2709 }
2710 RETURN;
2711
2712nuts:
599cee73 2713 if (ckWARN(WARN_CLOSED))
bc37a18f 2714 report_evil_fh(gv, io, optype);
93189314 2715 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2716nuts2:
2717 RETPUSHUNDEF;
2718
2719#else
cea2e8a9 2720 DIE(aTHX_ PL_no_sock_func, "setsockopt");
a0d0e21e
LW
2721#endif
2722}
2723
2724PP(pp_getsockname)
2725{
2726#ifdef HAS_SOCKET
cea2e8a9 2727 return pp_getpeername();
a0d0e21e 2728#else
cea2e8a9 2729 DIE(aTHX_ PL_no_sock_func, "getsockname");
a0d0e21e
LW
2730#endif
2731}
2732
2733PP(pp_getpeername)
2734{
a0d0e21e 2735#ifdef HAS_SOCKET
9cad6237 2736 dSP;
533c011a 2737 int optype = PL_op->op_type;
a0d0e21e
LW
2738 SV *sv;
2739 int fd;
2740 GV *gv = (GV*)POPs;
2741 register IO *io = GvIOn(gv);
1e422769 2742 Sock_size_t len;
a0d0e21e
LW
2743
2744 if (!io || !IoIFP(io))
2745 goto nuts;
2746
2747 sv = sv_2mortal(NEWSV(22, 257));
748a9306 2748 (void)SvPOK_only(sv);
1e422769
PP
2749 len = 256;
2750 SvCUR_set(sv, len);
748a9306 2751 *SvEND(sv) ='\0';
760ac839 2752 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2753 switch (optype) {
2754 case OP_GETSOCKNAME:
6ad3d225 2755 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e
LW
2756 goto nuts2;
2757 break;
2758 case OP_GETPEERNAME:
6ad3d225 2759 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e 2760 goto nuts2;
490ab354
JH
2761#if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2762 {
2763 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";
2764 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2765 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2766 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2767 sizeof(u_short) + sizeof(struct in_addr))) {
301e8125 2768 goto nuts2;
490ab354
JH
2769 }
2770 }
2771#endif
a0d0e21e
LW
2772 break;
2773 }
13826f2c
CS
2774#ifdef BOGUS_GETNAME_RETURN
2775 /* Interactive Unix, getpeername() and getsockname()
2776 does not return valid namelen */
1e422769
PP
2777 if (len == BOGUS_GETNAME_RETURN)
2778 len = sizeof(struct sockaddr);
13826f2c 2779#endif
1e422769 2780 SvCUR_set(sv, len);
748a9306 2781 *SvEND(sv) ='\0';
a0d0e21e
LW
2782 PUSHs(sv);
2783 RETURN;
2784
2785nuts:
599cee73 2786 if (ckWARN(WARN_CLOSED))
bc37a18f 2787 report_evil_fh(gv, io, optype);
93189314 2788 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2789nuts2:
2790 RETPUSHUNDEF;
2791
2792#else
cea2e8a9 2793 DIE(aTHX_ PL_no_sock_func, "getpeername");
a0d0e21e
LW
2794#endif
2795}
2796
2797/* Stat calls. */
2798
2799PP(pp_lstat)
2800{
cea2e8a9 2801 return pp_stat();
a0d0e21e
LW
2802}
2803
2804PP(pp_stat)
2805{
39644a26 2806 dSP;
2dd78f96 2807 GV *gv;
54310121 2808 I32 gimme;
a0d0e21e 2809 I32 max = 13;
2d8e6c8d 2810 STRLEN n_a;
a0d0e21e 2811
533c011a 2812 if (PL_op->op_flags & OPf_REF) {
2dd78f96 2813 gv = cGVOP_gv;
8a4e5b40 2814 if (PL_op->op_type == OP_LSTAT) {
5d3e98de
RGS
2815 if (gv != PL_defgv) {
2816 if (ckWARN(WARN_IO))
9014280d 2817 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de
RGS
2818 "lstat() on filehandle %s", GvENAME(gv));
2819 } else if (PL_laststype != OP_LSTAT)
8a4e5b40 2820 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
8a4e5b40
DD
2821 }
2822
748a9306 2823 do_fstat:
2dd78f96 2824 if (gv != PL_defgv) {
3280af22 2825 PL_laststype = OP_STAT;
2dd78f96 2826 PL_statgv = gv;
c69006e4 2827 sv_setpvn(PL_statname, "", 0);
2dd78f96
JH
2828 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2829 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
a0d0e21e 2830 }
9ddeeac9 2831 if (PL_laststatval < 0) {
2dd78f96
JH
2832 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2833 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
a0d0e21e 2834 max = 0;
9ddeeac9 2835 }
a0d0e21e
LW
2836 }
2837 else {
748a9306
LW
2838 SV* sv = POPs;
2839 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 2840 gv = (GV*)sv;
748a9306
LW
2841 goto do_fstat;
2842 }
2843 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 2844 gv = (GV*)SvRV(sv);
5d3e98de 2845 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
9014280d 2846 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de 2847 "lstat() on filehandle %s", GvENAME(gv));
748a9306
LW
2848 goto do_fstat;
2849 }
2d8e6c8d 2850 sv_setpv(PL_statname, SvPV(sv,n_a));
3280af22 2851 PL_statgv = Nullgv;
533c011a
NIS
2852 PL_laststype = PL_op->op_type;
2853 if (PL_op->op_type == OP_LSTAT)
2d8e6c8d 2854 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
a0d0e21e 2855 else
2d8e6c8d 2856 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
3280af22 2857 if (PL_laststatval < 0) {
2d8e6c8d 2858 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
9014280d 2859 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
a0d0e21e
LW
2860 max = 0;
2861 }
2862 }
2863
54310121
PP
2864 gimme = GIMME_V;
2865 if (gimme != G_ARRAY) {
2866 if (gimme != G_VOID)
2867 XPUSHs(boolSV(max));
2868 RETURN;
a0d0e21e
LW
2869 }
2870 if (max) {
36477c24
PP
2871 EXTEND(SP, max);
2872 EXTEND_MORTAL(max);
1ff81528
PL
2873 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2874 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
b448e4fe
JH
2875 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2876 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
146174a9
CB
2877#if Uid_t_size > IVSIZE
2878 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2879#else
23dcd6c8 2880# if Uid_t_sign <= 0
1ff81528 2881 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
23dcd6c8
JH
2882# else
2883 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2884# endif
146174a9 2885#endif
301e8125 2886#if Gid_t_size > IVSIZE
146174a9
CB
2887 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2888#else
23dcd6c8 2889# if Gid_t_sign <= 0
1ff81528 2890 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
23dcd6c8
JH
2891# else
2892 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2893# endif
146174a9 2894#endif
cbdc8872 2895#ifdef USE_STAT_RDEV
1ff81528 2896 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
cbdc8872 2897#else
79cb57f6 2898 PUSHs(sv_2mortal(newSVpvn("", 0)));
cbdc8872 2899#endif
146174a9 2900#if Off_t_size > IVSIZE
4a9d6100 2901 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
146174a9 2902#else
1ff81528 2903 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
146174a9 2904#endif
cbdc8872 2905#ifdef BIG_TIME
172ae379
JH
2906 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2907 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2908 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
cbdc8872 2909#else
1ff81528
PL
2910 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2911 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2912 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
cbdc8872 2913#endif
a0d0e21e 2914#ifdef USE_STAT_BLOCKS
b448e4fe
JH
2915 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2916 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
a0d0e21e 2917#else
79cb57f6
GS
2918 PUSHs(sv_2mortal(newSVpvn("", 0)));
2919 PUSHs(sv_2mortal(newSVpvn("", 0)));
a0d0e21e
LW
2920#endif
2921 }
2922 RETURN;
2923}
2924
fbb0b3b3
RGS
2925/* This macro is used by the stacked filetest operators :
2926 * if the previous filetest failed, short-circuit and pass its value.
2927 * Else, discard it from the stack and continue. --rgs
2928 */
2929#define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2930 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2931 else { (void)POPs; PUTBACK; } \
2932 }
2933
a0d0e21e
LW
2934PP(pp_ftrread)
2935{
9cad6237 2936 I32 result;
2a3ff820 2937 dSP;
fbb0b3b3 2938 STACKED_FTEST_CHECK;
5ff3f7a4 2939#if defined(HAS_ACCESS) && defined(R_OK)
1af34c76 2940 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 2941 STRLEN n_a;
43b06338 2942 result = access(POPpx, R_OK);
5ff3f7a4
GS
2943 if (result == 0)
2944 RETPUSHYES;
2945 if (result < 0)
2946 RETPUSHUNDEF;
2947 RETPUSHNO;
22865c03
GS
2948 }
2949 else
cea2e8a9 2950 result = my_stat();
5ff3f7a4 2951#else
cea2e8a9 2952 result = my_stat();
5ff3f7a4 2953#endif
22865c03 2954 SPAGAIN;
a0d0e21e
LW
2955 if (result < 0)
2956 RETPUSHUNDEF;
3280af22 2957 if (cando(S_IRUSR, 0, &PL_statcache))
a0d0e21e
LW
2958 RETPUSHYES;
2959 RETPUSHNO;
2960}
2961
2962PP(pp_ftrwrite)
2963{
9cad6237 2964 I32 result;
2a3ff820 2965 dSP;
fbb0b3b3 2966 STACKED_FTEST_CHECK;
5ff3f7a4 2967#if defined(HAS_ACCESS) && defined(W_OK)
1af34c76 2968 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 2969 STRLEN n_a;
43b06338 2970 result = access(POPpx, W_OK);
5ff3f7a4
GS
2971 if (result == 0)
2972 RETPUSHYES;
2973 if (result < 0)
2974 RETPUSHUNDEF;
2975 RETPUSHNO;
22865c03
GS
2976 }
2977 else
cea2e8a9 2978 result = my_stat();
5ff3f7a4 2979#else
cea2e8a9 2980 result = my_stat();
5ff3f7a4 2981#endif
22865c03 2982 SPAGAIN;
a0d0e21e
LW
2983 if (result < 0)
2984 RETPUSHUNDEF;
3280af22 2985 if (cando(S_IWUSR, 0, &PL_statcache))
a0d0e21e
LW
2986 RETPUSHYES;
2987 RETPUSHNO;
2988}
2989
2990PP(pp_ftrexec)
2991{
9cad6237 2992 I32 result;
2a3ff820 2993 dSP;
fbb0b3b3 2994 STACKED_FTEST_CHECK;
5ff3f7a4 2995#if defined(HAS_ACCESS) && defined(X_OK)
1af34c76 2996 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 2997 STRLEN n_a;
43b06338 2998 result = access(POPpx, X_OK);
5ff3f7a4
GS
2999 if (result == 0)
3000 RETPUSHYES;
3001 if (result < 0)
3002 RETPUSHUNDEF;
3003 RETPUSHNO;
22865c03
GS
3004 }
3005 else
cea2e8a9 3006 result = my_stat();
5ff3f7a4 3007#else
cea2e8a9 3008 result = my_stat();
5ff3f7a4 3009#endif
22865c03 3010 SPAGAIN;
a0d0e21e
LW
3011 if (result < 0)
3012 RETPUSHUNDEF;
3280af22 3013 if (cando(S_IXUSR, 0, &PL_statcache))
a0d0e21e
LW
3014 RETPUSHYES;
3015 RETPUSHNO;
3016}
3017
3018PP(pp_fteread)
3019{
9cad6237 3020 I32 result;
2a3ff820 3021 dSP;
fbb0b3b3 3022 STACKED_FTEST_CHECK;
5ff3f7a4 3023#ifdef PERL_EFF_ACCESS_R_OK
1af34c76 3024 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 3025 STRLEN n_a;
43b06338 3026 result = PERL_EFF_ACCESS_R_OK(POPpx);
5ff3f7a4
GS
3027 if (result == 0)
3028 RETPUSHYES;
3029 if (result < 0)
3030 RETPUSHUNDEF;
3031 RETPUSHNO;
22865c03
GS
3032 }
3033 else
cea2e8a9 3034 result = my_stat();
5ff3f7a4 3035#else
cea2e8a9 3036 result = my_stat();
5ff3f7a4 3037#endif
22865c03 3038 SPAGAIN;
a0d0e21e
LW
3039 if (result < 0)
3040 RETPUSHUNDEF;
3280af22 3041 if (cando(S_IRUSR, 1, &PL_statcache))
a0d0e21e
LW
3042 RETPUSHYES;
3043 RETPUSHNO;
3044}
3045
3046PP(pp_ftewrite)
3047{
9cad6237 3048 I32 result;
2a3ff820 3049 dSP;
fbb0b3b3 3050 STACKED_FTEST_CHECK;
5ff3f7a4 3051#ifdef PERL_EFF_ACCESS_W_OK
1af34c76 3052 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 3053 STRLEN n_a;
43b06338 3054 result = PERL_EFF_ACCESS_W_OK(POPpx);
5ff3f7a4
GS
3055 if (result == 0)
3056 RETPUSHYES;
3057 if (result < 0)
3058 RETPUSHUNDEF;
3059 RETPUSHNO;
22865c03
GS
3060 }
3061 else
cea2e8a9 3062 result = my_stat();
5ff3f7a4 3063#else
cea2e8a9 3064 result = my_stat();
5ff3f7a4 3065#endif
22865c03 3066 SPAGAIN;
a0d0e21e
LW
3067 if (result < 0)
3068 RETPUSHUNDEF;
3280af22 3069 if (cando(S_IWUSR, 1, &PL_statcache))
a0d0e21e
LW
3070 RETPUSHYES;
3071 RETPUSHNO;
3072}
3073
3074PP(pp_fteexec)
3075{
9cad6237 3076 I32 result;
2a3ff820 3077 dSP;
fbb0b3b3 3078 STACKED_FTEST_CHECK;
5ff3f7a4 3079#ifdef PERL_EFF_ACCESS_X_OK
1af34c76 3080 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 3081 STRLEN n_a;
43b06338 3082 result = PERL_EFF_ACCESS_X_OK(POPpx);
5ff3f7a4
GS
3083 if (result == 0)
3084 RETPUSHYES;
3085 if (result < 0)
3086 RETPUSHUNDEF;
3087 RETPUSHNO;
22865c03
GS
3088 }
3089 else
cea2e8a9 3090 result = my_stat();
5ff3f7a4 3091#else
cea2e8a9 3092 result = my_stat();
5ff3f7a4 3093#endif
22865c03 3094 SPAGAIN;
a0d0e21e
LW
3095 if (result < 0)
3096 RETPUSHUNDEF;
3280af22 3097 if (cando(S_IXUSR, 1, &PL_statcache))
a0d0e21e
LW
3098 RETPUSHYES;
3099 RETPUSHNO;
3100}
3101
3102PP(pp_ftis)
3103{
fbb0b3b3 3104 I32 result;
2a3ff820 3105 dSP;
fbb0b3b3
RGS
3106 STACKED_FTEST_CHECK;
3107 result = my_stat();
3108 SPAGAIN;
a0d0e21e
LW
3109 if (result < 0)
3110 RETPUSHUNDEF;
3111 RETPUSHYES;
3112}
3113
3114PP(pp_fteowned)
3115{
cea2e8a9 3116 return pp_ftrowned();
a0d0e21e
LW
3117}
3118
3119PP(pp_ftrowned)
3120{
fbb0b3b3 3121 I32 result;
2a3ff820 3122 dSP;
fbb0b3b3
RGS
3123 STACKED_FTEST_CHECK;
3124 result = my_stat();
3125 SPAGAIN;
a0d0e21e
LW
3126 if (result < 0)
3127 RETPUSHUNDEF;
146174a9
CB
3128 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3129 PL_euid : PL_uid) )
a0d0e21e
LW
3130 RETPUSHYES;
3131 RETPUSHNO;
3132}
3133
3134PP(pp_ftzero)
3135{
fbb0b3b3 3136 I32 result;
2a3ff820 3137 dSP;
fbb0b3b3
RGS
3138 STACKED_FTEST_CHECK;
3139 result = my_stat();
3140 SPAGAIN;
a0d0e21e
LW
3141 if (result < 0)
3142 RETPUSHUNDEF;
146174a9 3143 if (PL_statcache.st_size == 0)
a0d0e21e
LW
3144 RETPUSHYES;
3145 RETPUSHNO;
3146}
3147
3148PP(pp_ftsize)
3149{
fbb0b3b3 3150 I32 result;
2a3ff820 3151 dSP; dTARGET;
fbb0b3b3
RGS
3152 STACKED_FTEST_CHECK;
3153 result = my_stat();
3154 SPAGAIN;
a0d0e21e
LW
3155 if (result < 0)
3156 RETPUSHUNDEF;
146174a9
CB
3157#if Off_t_size > IVSIZE
3158 PUSHn(PL_statcache.st_size);
3159#else
3280af22 3160 PUSHi(PL_statcache.st_size);
146174a9 3161#endif
a0d0e21e
LW
3162 RETURN;
3163}
3164
3165PP(pp_ftmtime)
3166{
fbb0b3b3 3167 I32 result;
2a3ff820 3168 dSP; dTARGET;
fbb0b3b3
RGS
3169 STACKED_FTEST_CHECK;
3170 result = my_stat();
3171 SPAGAIN;
a0d0e21e
LW
3172 if (result < 0)
3173 RETPUSHUNDEF;
a80d47c2 3174 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
a0d0e21e
LW
3175 RETURN;
3176}
3177
3178PP(pp_ftatime)
3179{
fbb0b3b3 3180 I32 result;
2a3ff820 3181 dSP; dTARGET;
fbb0b3b3
RGS
3182 STACKED_FTEST_CHECK;
3183 result = my_stat();
3184 SPAGAIN;
a0d0e21e
LW
3185 if (result < 0)
3186 RETPUSHUNDEF;
a80d47c2 3187 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
a0d0e21e
LW
3188 RETURN;
3189}
3190
3191PP(pp_ftctime)
3192{
fbb0b3b3 3193 I32 result;
2a3ff820 3194 dSP; dTARGET;
fbb0b3b3
RGS
3195 STACKED_FTEST_CHECK;
3196 result = my_stat();
3197 SPAGAIN;
a0d0e21e
LW
3198 if (result < 0)
3199 RETPUSHUNDEF;
a80d47c2 3200 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
a0d0e21e
LW
3201 RETURN;
3202}
3203
3204PP(pp_ftsock)
3205{
fbb0b3b3 3206 I32 result;
2a3ff820 3207 dSP;
fbb0b3b3
RGS
3208 STACKED_FTEST_CHECK;
3209 result = my_stat();
3210 SPAGAIN;
a0d0e21e
LW
3211 if (result < 0)
3212 RETPUSHUNDEF;
3280af22 3213 if (S_ISSOCK(PL_statcache.st_mode))
a0d0e21e
LW
3214 RETPUSHYES;
3215 RETPUSHNO;
3216}
3217
3218PP(pp_ftchr)
3219{
fbb0b3b3 3220 I32 result;
2a3ff820 3221 dSP;
fbb0b3b3
RGS
3222 STACKED_FTEST_CHECK;
3223 result = my_stat();
3224 SPAGAIN;
a0d0e21e
LW
3225 if (result < 0)
3226 RETPUSHUNDEF;
3280af22 3227 if (S_ISCHR(PL_statcache.st_mode))
a0d0e21e
LW
3228 RETPUSHYES;
3229 RETPUSHNO;
3230}
3231
3232PP(pp_ftblk)
3233{
fbb0b3b3 3234 I32 result;
2a3ff820 3235 dSP;
fbb0b3b3
RGS
3236 STACKED_FTEST_CHECK;
3237 result = my_stat();
3238 SPAGAIN;
a0d0e21e
LW
3239 if (result < 0)
3240 RETPUSHUNDEF;
3280af22 3241 if (S_ISBLK(PL_statcache.st_mode))
a0d0e21e
LW
3242 RETPUSHYES;
3243 RETPUSHNO;
3244}
3245
3246PP(pp_ftfile)
3247{
fbb0b3b3 3248 I32 result;
2a3ff820 3249 dSP;
fbb0b3b3
RGS
3250 STACKED_FTEST_CHECK;
3251 result = my_stat();
3252 SPAGAIN;
a0d0e21e
LW
3253 if (result < 0)
3254 RETPUSHUNDEF;
3280af22 3255 if (S_ISREG(PL_statcache.st_mode))
a0d0e21e
LW
3256 RETPUSHYES;
3257 RETPUSHNO;
3258}
3259
3260PP(pp_ftdir)
3261{
fbb0b3b3 3262 I32 result;
2a3ff820 3263 dSP;
fbb0b3b3
RGS
3264 STACKED_FTEST_CHECK;
3265 result = my_stat();
3266 SPAGAIN;
a0d0e21e
LW
3267 if (result < 0)
3268 RETPUSHUNDEF;
3280af22 3269 if (S_ISDIR(PL_statcache.st_mode))
a0d0e21e
LW
3270 RETPUSHYES;
3271 RETPUSHNO;
3272}
3273
3274PP(pp_ftpipe)
3275{
fbb0b3b3 3276 I32 result;
2a3ff820 3277 dSP;
fbb0b3b3
RGS
3278 STACKED_FTEST_CHECK;
3279 result = my_stat();
3280 SPAGAIN;
a0d0e21e
LW
3281 if (result < 0)
3282 RETPUSHUNDEF;
3280af22 3283 if (S_ISFIFO(PL_statcache.st_mode))
a0d0e21e
LW
3284 RETPUSHYES;
3285 RETPUSHNO;
3286}
3287
3288PP(pp_ftlink)
3289{
9cad6237 3290 I32 result = my_lstat();
2a3ff820 3291 dSP;
a0d0e21e
LW
3292 if (result < 0)
3293 RETPUSHUNDEF;
3280af22 3294 if (S_ISLNK(PL_statcache.st_mode))
a0d0e21e
LW
3295 RETPUSHYES;
3296 RETPUSHNO;
3297}
3298
3299PP(pp_ftsuid)
3300{
39644a26 3301 dSP;
a0d0e21e 3302#ifdef S_ISUID
fbb0b3b3
RGS
3303 I32 result;
3304 STACKED_FTEST_CHECK;
3305 result = my_stat();
a0d0e21e
LW
3306 SPAGAIN;
3307 if (result < 0)
3308 RETPUSHUNDEF;
3280af22 3309 if (PL_statcache.st_mode & S_ISUID)
a0d0e21e
LW
3310 RETPUSHYES;
3311#endif
3312 RETPUSHNO;
3313}
3314
3315PP(pp_ftsgid)
3316{
39644a26 3317 dSP;
a0d0e21e 3318#ifdef S_ISGID
fbb0b3b3
RGS
3319 I32 result;
3320 STACKED_FTEST_CHECK;
3321 result = my_stat();
a0d0e21e
LW
3322 SPAGAIN;
3323 if (result < 0)
3324 RETPUSHUNDEF;
3280af22 3325 if (PL_statcache.st_mode & S_ISGID)
a0d0e21e
LW
3326 RETPUSHYES;
3327#endif
3328 RETPUSHNO;
3329}
3330
3331PP(pp_ftsvtx)
3332{
39644a26 3333 dSP;
a0d0e21e 3334#ifdef S_ISVTX
fbb0b3b3
RGS
3335 I32 result;
3336 STACKED_FTEST_CHECK;
3337 result = my_stat();
a0d0e21e
LW
3338 SPAGAIN;
3339 if (result < 0)
3340 RETPUSHUNDEF;
3280af22 3341 if (PL_statcache.st_mode & S_ISVTX)
a0d0e21e
LW
3342 RETPUSHYES;
3343#endif
3344 RETPUSHNO;
3345}
3346
3347PP(pp_fttty)
3348{
39644a26 3349 dSP;
a0d0e21e
LW
3350 int fd;
3351 GV *gv;
7a5fd60d 3352 SV *tmpsv = Nullsv;
fb73857a 3353
fbb0b3b3
RGS
3354 STACKED_FTEST_CHECK;
3355
533c011a 3356 if (PL_op->op_flags & OPf_REF)
146174a9 3357 gv = cGVOP_gv;
fb73857a
PP
3358 else if (isGV(TOPs))
3359 gv = (GV*)POPs;
3360 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3361 gv = (GV*)SvRV(POPs);
a0d0e21e 3362 else
7a5fd60d 3363 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
fb73857a 3364
a0d0e21e 3365 if (GvIO(gv) && IoIFP(GvIOp(gv)))
760ac839 3366 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
7a5fd60d
NC
3367 else if (tmpsv && SvOK(tmpsv)) {
3368 STRLEN n_a;
3369 char *tmps = SvPV(tmpsv, n_a);
3370 if (isDIGIT(*tmps))
3371 fd = atoi(tmps);
3372 else
3373 RETPUSHUNDEF;
3374 }
a0d0e21e
LW
3375 else
3376 RETPUSHUNDEF;
6ad3d225 3377 if (PerlLIO_isatty(fd))
a0d0e21e
LW
3378 RETPUSHYES;
3379 RETPUSHNO;
3380}
3381
16d20bd9
AD
3382#if defined(atarist) /* this will work with atariST. Configure will
3383 make guesses for other systems. */
3384# define FILE_base(f) ((f)->_base)
3385# define FILE_ptr(f) ((f)->_ptr)
3386# define FILE_cnt(f) ((f)->_cnt)
3387# define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
a0d0e21e
LW
3388#endif
3389
3390PP(pp_fttext)
3391{
39644a26 3392 dSP;
a0d0e21e
LW
3393 I32 i;
3394 I32 len;
3395 I32 odd = 0;
3396 STDCHAR tbuf[512];
3397 register STDCHAR *s;
3398 register IO *io;
5f05dabc
PP
3399 register SV *sv;
3400 GV *gv;
2d8e6c8d 3401 STRLEN n_a;
146174a9 3402 PerlIO *fp;
a0d0e21e 3403
fbb0b3b3
RGS
3404 STACKED_FTEST_CHECK;
3405
533c011a 3406 if (PL_op->op_flags & OPf_REF)
146174a9 3407 gv = cGVOP_gv;
5f05dabc
PP
3408 else if (isGV(TOPs))
3409 gv = (GV*)POPs;
3410 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3411 gv = (GV*)SvRV(POPs);
3412 else
3413 gv = Nullgv;
3414
3415 if (gv) {
a0d0e21e 3416 EXTEND(SP, 1);
3280af22
NIS
3417 if (gv == PL_defgv) {
3418 if (PL_statgv)
3419 io = GvIO(PL_statgv);
a0d0e21e 3420 else {
3280af22 3421 sv = PL_statname;
a0d0e21e
LW
3422 goto really_filename;
3423 }
3424 }
3425 else {
3280af22
NIS
3426 PL_statgv = gv;
3427 PL_laststatval = -1;
c69006e4 3428 sv_setpvn(PL_statname, "", 0);
3280af22 3429 io = GvIO(PL_statgv);
a0d0e21e
LW
3430 }
3431 if (io && IoIFP(io)) {
5f05dabc 3432 if (! PerlIO_has_base(IoIFP(io)))
cea2e8a9 3433 DIE(aTHX_ "-T and -B not implemented on filehandles");
3280af22
NIS
3434 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3435 if (PL_laststatval < 0)
5f05dabc 3436 RETPUSHUNDEF;
9cbac4c7 3437 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
533c011a 3438 if (PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3439 RETPUSHNO;
3440 else
3441 RETPUSHYES;
9cbac4c7 3442 }
a20bf0c3 3443 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
760ac839 3444 i = PerlIO_getc(IoIFP(io));
a0d0e21e 3445 if (i != EOF)
760ac839 3446 (void)PerlIO_ungetc(IoIFP(io),i);
a0d0e21e 3447 }
a20bf0c3 3448 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
a0d0e21e 3449 RETPUSHYES;
a20bf0c3
JH
3450 len = PerlIO_get_bufsiz(IoIFP(io));
3451 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
760ac839
LW
3452 /* sfio can have large buffers - limit to 512 */
3453 if (len > 512)
3454 len = 512;
a0d0e21e
LW
3455 }
3456 else {
2dd78f96 3457 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
146174a9 3458 gv = cGVOP_gv;
2dd78f96 3459 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
146174a9 3460 }
93189314 3461 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
3462 RETPUSHUNDEF;
3463 }
3464 }
3465 else {
3466 sv = POPs;
5f05dabc 3467 really_filename:
3280af22 3468 PL_statgv = Nullgv;
5c9aa243 3469 PL_laststype = OP_STAT;
2d8e6c8d 3470 sv_setpv(PL_statname, SvPV(sv, n_a));
146174a9 3471 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
f9341440 3472 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
9014280d 3473 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
a0d0e21e
LW
3474 RETPUSHUNDEF;
3475 }
146174a9
CB
3476 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3477 if (PL_laststatval < 0) {
3478 (void)PerlIO_close(fp);
5f05dabc 3479 RETPUSHUNDEF;
146174a9 3480 }
60382766 3481 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
146174a9
CB
3482 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3483 (void)PerlIO_close(fp);
a0d0e21e 3484 if (len <= 0) {
533c011a 3485 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3486 RETPUSHNO; /* special case NFS directories */
3487 RETPUSHYES; /* null file is anything */
3488 }
3489 s = tbuf;
3490 }
3491
3492 /* now scan s to look for textiness */
4633a7c4 3493 /* XXX ASCII dependent code */
a0d0e21e 3494
146174a9
CB
3495#if defined(DOSISH) || defined(USEMYBINMODE)
3496 /* ignore trailing ^Z on short files */
3497 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3498 --len;
3499#endif
3500
a0d0e21e
LW
3501 for (i = 0; i < len; i++, s++) {
3502 if (!*s) { /* null never allowed in text */
3503 odd += len;
3504 break;
3505 }
9d116dd7 3506#ifdef EBCDIC
301e8125 3507 else if (!(isPRINT(*s) || isSPACE(*s)))
9d116dd7
JH
3508 odd++;
3509#else