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