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