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