This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix PerlLIO_chsize() for platforms that don't have chsize()
[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;
e1ec3a88 325 const 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";
dd374669 332 fp = PerlProc_popen(tmps, (char *)mode);
a0d0e21e 333 if (fp) {
e1ec3a88 334 const char *type = NULL;
ac27b0f5
NIS
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;
e1ec3a88 436 const 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;
e1ec3a88 466 const 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;
e1ec3a88 793 const 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;
dd374669 1971 do_open(gv, (char *)"-", 1, FALSE, O_RDONLY, 0, Nullfp);
146174a9
CB
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);
d05c1ba0 2113 {
d05c1ba0
JH
2114 int result = 1;
2115 GV *tmpgv;
090bf15b
SR
2116 IO *io;
2117
d05c1ba0 2118 if (PL_op->op_flags & OPf_SPECIAL) {
7a5fd60d 2119 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
d05c1ba0 2120
090bf15b
SR
2121 do_ftruncate_gv:
2122 if (!GvIO(tmpgv))
2123 result = 0;
d05c1ba0 2124 else {
090bf15b
SR
2125 PerlIO *fp;
2126 io = GvIOp(tmpgv);
2127 do_ftruncate_io:
2128 TAINT_PROPER("truncate");
2129 if (!(fp = IoIFP(io))) {
2130 result = 0;
2131 }
2132 else {
2133 PerlIO_flush(fp);
cbdc8872 2134#ifdef HAS_TRUNCATE
090bf15b 2135 if (ftruncate(PerlIO_fileno(fp), len) < 0)
301e8125 2136#else
090bf15b 2137 if (my_chsize(PerlIO_fileno(fp), len) < 0)
cbdc8872 2138#endif
090bf15b
SR
2139 result = 0;
2140 }
d05c1ba0 2141 }
cbdc8872 2142 }
d05c1ba0
JH
2143 else {
2144 SV *sv = POPs;
2145 char *name;
7a5fd60d
NC
2146 STRLEN n_a;
2147
d05c1ba0
JH
2148 if (SvTYPE(sv) == SVt_PVGV) {
2149 tmpgv = (GV*)sv; /* *main::FRED for example */
090bf15b 2150 goto do_ftruncate_gv;
d05c1ba0
JH
2151 }
2152 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2153 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
090bf15b
SR
2154 goto do_ftruncate_gv;
2155 }
2156 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2157 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2158 goto do_ftruncate_io;
d05c1ba0 2159 }
1e422769 2160
d05c1ba0
JH
2161 name = SvPV(sv, n_a);
2162 TAINT_PROPER("truncate");
cbdc8872 2163#ifdef HAS_TRUNCATE
d05c1ba0
JH
2164 if (truncate(name, len) < 0)
2165 result = 0;
cbdc8872 2166#else
d05c1ba0
JH
2167 {
2168 int tmpfd;
2169
2170 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
cbdc8872 2171 result = 0;
d05c1ba0
JH
2172 else {
2173 if (my_chsize(tmpfd, len) < 0)
2174 result = 0;
2175 PerlLIO_close(tmpfd);
2176 }
cbdc8872 2177 }
a0d0e21e 2178#endif
d05c1ba0 2179 }
a0d0e21e 2180
d05c1ba0
JH
2181 if (result)
2182 RETPUSHYES;
2183 if (!errno)
93189314 2184 SETERRNO(EBADF,RMS_IFI);
d05c1ba0
JH
2185 RETPUSHUNDEF;
2186 }
a0d0e21e
LW
2187}
2188
2189PP(pp_fcntl)
2190{
cea2e8a9 2191 return pp_ioctl();
a0d0e21e
LW
2192}
2193
2194PP(pp_ioctl)
2195{
39644a26 2196 dSP; dTARGET;
748a9306 2197 SV *argsv = POPs;
3bb7c1b4 2198 unsigned int func = POPu;
e1ec3a88 2199 const int optype = PL_op->op_type;
a0d0e21e 2200 char *s;
324aa91a 2201 IV retval;
a0d0e21e 2202 GV *gv = (GV*)POPs;
c289d2f7 2203 IO *io = gv ? GvIOn(gv) : 0;
a0d0e21e 2204
748a9306 2205 if (!io || !argsv || !IoIFP(io)) {
c289d2f7
JH
2206 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2207 report_evil_fh(gv, io, PL_op->op_type);
93189314 2208 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
a0d0e21e
LW
2209 RETPUSHUNDEF;
2210 }
2211
748a9306 2212 if (SvPOK(argsv) || !SvNIOK(argsv)) {
a0d0e21e 2213 STRLEN len;
324aa91a 2214 STRLEN need;
748a9306 2215 s = SvPV_force(argsv, len);
324aa91a
HF
2216 need = IOCPARM_LEN(func);
2217 if (len < need) {
2218 s = Sv_Grow(argsv, need + 1);
2219 SvCUR_set(argsv, need);
a0d0e21e
LW
2220 }
2221
748a9306 2222 s[SvCUR(argsv)] = 17; /* a little sanity check here */
a0d0e21e
LW
2223 }
2224 else {
748a9306 2225 retval = SvIV(argsv);
c529f79d 2226 s = INT2PTR(char*,retval); /* ouch */
a0d0e21e
LW
2227 }
2228
2229 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2230
2231 if (optype == OP_IOCTL)
2232#ifdef HAS_IOCTL
76e3520e 2233 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
a0d0e21e 2234#else
cea2e8a9 2235 DIE(aTHX_ "ioctl is not implemented");
a0d0e21e
LW
2236#endif
2237 else
c214f4ad
B
2238#ifndef HAS_FCNTL
2239 DIE(aTHX_ "fcntl is not implemented");
2240#else
55497cff 2241#if defined(OS2) && defined(__EMX__)
760ac839 2242 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
55497cff 2243#else
760ac839 2244 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
301e8125 2245#endif
6652bd42 2246#endif
a0d0e21e 2247
6652bd42 2248#if defined(HAS_IOCTL) || defined(HAS_FCNTL)
748a9306
LW
2249 if (SvPOK(argsv)) {
2250 if (s[SvCUR(argsv)] != 17)
cea2e8a9 2251 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
53e06cf0 2252 OP_NAME(PL_op));
748a9306
LW
2253 s[SvCUR(argsv)] = 0; /* put our null back */
2254 SvSETMAGIC(argsv); /* Assume it has changed */
a0d0e21e
LW
2255 }
2256
2257 if (retval == -1)
2258 RETPUSHUNDEF;
2259 if (retval != 0) {
2260 PUSHi(retval);
2261 }
2262 else {
8903cb82 2263 PUSHp(zero_but_true, ZBTLEN);
a0d0e21e 2264 }
4808266b 2265#endif
c214f4ad 2266 RETURN;
a0d0e21e
LW
2267}
2268
2269PP(pp_flock)
2270{
9cad6237 2271#ifdef FLOCK
39644a26 2272 dSP; dTARGET;
a0d0e21e
LW
2273 I32 value;
2274 int argtype;
2275 GV *gv;
bc37a18f 2276 IO *io = NULL;
760ac839 2277 PerlIO *fp;
16d20bd9 2278
a0d0e21e 2279 argtype = POPi;
32da55ab 2280 if (MAXARG == 0)
3280af22 2281 gv = PL_last_in_gv;
a0d0e21e
LW
2282 else
2283 gv = (GV*)POPs;
bc37a18f
RG
2284 if (gv && (io = GvIO(gv)))
2285 fp = IoIFP(io);
2286 else {
a0d0e21e 2287 fp = Nullfp;
bc37a18f
RG
2288 io = NULL;
2289 }
a0d0e21e 2290 if (fp) {
68dc0745 2291 (void)PerlIO_flush(fp);
76e3520e 2292 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
a0d0e21e 2293 }
cb50131a 2294 else {
bc37a18f
RG
2295 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2296 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 2297 value = 0;
93189314 2298 SETERRNO(EBADF,RMS_IFI);
cb50131a 2299 }
a0d0e21e
LW
2300 PUSHi(value);
2301 RETURN;
2302#else
cea2e8a9 2303 DIE(aTHX_ PL_no_func, "flock()");
a0d0e21e
LW
2304#endif
2305}
2306
2307/* Sockets. */
2308
2309PP(pp_socket)
2310{
a0d0e21e 2311#ifdef HAS_SOCKET
9cad6237 2312 dSP;
a0d0e21e
LW
2313 GV *gv;
2314 register IO *io;
2315 int protocol = POPi;
2316 int type = POPi;
2317 int domain = POPi;
2318 int fd;
2319
2320 gv = (GV*)POPs;
c289d2f7 2321 io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2322
c289d2f7
JH
2323 if (!gv || !io) {
2324 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2325 report_evil_fh(gv, io, PL_op->op_type);
2326 if (IoIFP(io))
2327 do_close(gv, FALSE);
93189314 2328 SETERRNO(EBADF,LIB_INVARG);
a0d0e21e
LW
2329 RETPUSHUNDEF;
2330 }
2331
57171420
BS
2332 if (IoIFP(io))
2333 do_close(gv, FALSE);
2334
a0d0e21e 2335 TAINT_PROPER("socket");
6ad3d225 2336 fd = PerlSock_socket(domain, type, protocol);
a0d0e21e
LW
2337 if (fd < 0)
2338 RETPUSHUNDEF;
460c8493
IZ
2339 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2340 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2341 IoTYPE(io) = IoTYPE_SOCKET;
a0d0e21e 2342 if (!IoIFP(io) || !IoOFP(io)) {
760ac839
LW
2343 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2344 if (IoOFP(io)) PerlIO_close(IoOFP(io));
6ad3d225 2345 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
a0d0e21e
LW
2346 RETPUSHUNDEF;
2347 }
8d2a6795
GS
2348#if defined(HAS_FCNTL) && defined(F_SETFD)
2349 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2350#endif
a0d0e21e 2351
d5ff79b3
OF
2352#ifdef EPOC
2353 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2354#endif
2355
a0d0e21e
LW
2356 RETPUSHYES;
2357#else
cea2e8a9 2358 DIE(aTHX_ PL_no_sock_func, "socket");
a0d0e21e
LW
2359#endif
2360}
2361
2362PP(pp_sockpair)
2363{
c95c94b1 2364#if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
76ffd3b9 2365 dSP;
a0d0e21e
LW
2366 GV *gv1;
2367 GV *gv2;
2368 register IO *io1;
2369 register IO *io2;
2370 int protocol = POPi;
2371 int type = POPi;
2372 int domain = POPi;
2373 int fd[2];
2374
2375 gv2 = (GV*)POPs;
2376 gv1 = (GV*)POPs;
c289d2f7
JH
2377 io1 = gv1 ? GvIOn(gv1) : NULL;
2378 io2 = gv2 ? GvIOn(gv2) : NULL;
2379 if (!gv1 || !gv2 || !io1 || !io2) {
2380 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2381 if (!gv1 || !io1)
2382 report_evil_fh(gv1, io1, PL_op->op_type);
2383 if (!gv2 || !io2)
2384 report_evil_fh(gv1, io2, PL_op->op_type);
2385 }
2386 if (IoIFP(io1))
2387 do_close(gv1, FALSE);
2388 if (IoIFP(io2))
2389 do_close(gv2, FALSE);
a0d0e21e 2390 RETPUSHUNDEF;
c289d2f7 2391 }
a0d0e21e 2392
dc0d0a5f
JH
2393 if (IoIFP(io1))
2394 do_close(gv1, FALSE);
2395 if (IoIFP(io2))
2396 do_close(gv2, FALSE);
57171420 2397
a0d0e21e 2398 TAINT_PROPER("socketpair");
6ad3d225 2399 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
a0d0e21e 2400 RETPUSHUNDEF;
460c8493
IZ
2401 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2402 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
50952442 2403 IoTYPE(io1) = IoTYPE_SOCKET;
460c8493
IZ
2404 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2405 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
50952442 2406 IoTYPE(io2) = IoTYPE_SOCKET;
a0d0e21e 2407 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
760ac839
LW
2408 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2409 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
6ad3d225 2410 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
760ac839
LW
2411 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2412 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
6ad3d225 2413 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
a0d0e21e
LW
2414 RETPUSHUNDEF;
2415 }
8d2a6795
GS
2416#if defined(HAS_FCNTL) && defined(F_SETFD)
2417 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2418 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2419#endif
a0d0e21e
LW
2420
2421 RETPUSHYES;
2422#else
cea2e8a9 2423 DIE(aTHX_ PL_no_sock_func, "socketpair");
a0d0e21e
LW
2424#endif
2425}
2426
2427PP(pp_bind)
2428{
a0d0e21e 2429#ifdef HAS_SOCKET
9cad6237 2430 dSP;
eec2d3df 2431#ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
82b3da69
JH
2432 extern void GETPRIVMODE();
2433 extern void GETUSERMODE();
eec2d3df 2434#endif
748a9306 2435 SV *addrsv = POPs;
a0d0e21e
LW
2436 char *addr;
2437 GV *gv = (GV*)POPs;
2438 register IO *io = GvIOn(gv);
2439 STRLEN len;
eec2d3df
GS
2440 int bind_ok = 0;
2441#ifdef MPE
2442 int mpeprivmode = 0;
2443#endif
a0d0e21e
LW
2444
2445 if (!io || !IoIFP(io))
2446 goto nuts;
2447
748a9306 2448 addr = SvPV(addrsv, len);
a0d0e21e 2449 TAINT_PROPER("bind");
eec2d3df
GS
2450#ifdef MPE /* Deal with MPE bind() peculiarities */
2451 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2452 /* The address *MUST* stupidly be zero. */
2453 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2454 /* PRIV mode is required to bind() to ports < 1024. */
2455 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2456 ((struct sockaddr_in *)addr)->sin_port > 0) {
2457 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2458 mpeprivmode = 1;
2459 }
2460 }
2461#endif /* MPE */
2462 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2463 (struct sockaddr *)addr, len) >= 0)
2464 bind_ok = 1;
2465
2466#ifdef MPE /* Switch back to USER mode */
2467 if (mpeprivmode)
2468 GETUSERMODE();
2469#endif /* MPE */
2470
2471 if (bind_ok)
a0d0e21e
LW
2472 RETPUSHYES;
2473 else
2474 RETPUSHUNDEF;
2475
2476nuts:
599cee73 2477 if (ckWARN(WARN_CLOSED))
bc37a18f 2478 report_evil_fh(gv, io, PL_op->op_type);
93189314 2479 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2480 RETPUSHUNDEF;
2481#else
cea2e8a9 2482 DIE(aTHX_ PL_no_sock_func, "bind");
a0d0e21e
LW
2483#endif
2484}
2485
2486PP(pp_connect)
2487{
a0d0e21e 2488#ifdef HAS_SOCKET
9cad6237 2489 dSP;
748a9306 2490 SV *addrsv = POPs;
a0d0e21e
LW
2491 char *addr;
2492 GV *gv = (GV*)POPs;
2493 register IO *io = GvIOn(gv);
2494 STRLEN len;
2495
2496 if (!io || !IoIFP(io))
2497 goto nuts;
2498
748a9306 2499 addr = SvPV(addrsv, len);
a0d0e21e 2500 TAINT_PROPER("connect");
6ad3d225 2501 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
a0d0e21e
LW
2502 RETPUSHYES;
2503 else
2504 RETPUSHUNDEF;
2505
2506nuts:
599cee73 2507 if (ckWARN(WARN_CLOSED))
bc37a18f 2508 report_evil_fh(gv, io, PL_op->op_type);
93189314 2509 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2510 RETPUSHUNDEF;
2511#else
cea2e8a9 2512 DIE(aTHX_ PL_no_sock_func, "connect");
a0d0e21e
LW
2513#endif
2514}
2515
2516PP(pp_listen)
2517{
a0d0e21e 2518#ifdef HAS_SOCKET
9cad6237 2519 dSP;
a0d0e21e
LW
2520 int backlog = POPi;
2521 GV *gv = (GV*)POPs;
c289d2f7 2522 register IO *io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2523
c289d2f7 2524 if (!gv || !io || !IoIFP(io))
a0d0e21e
LW
2525 goto nuts;
2526
6ad3d225 2527 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
a0d0e21e
LW
2528 RETPUSHYES;
2529 else
2530 RETPUSHUNDEF;
2531
2532nuts:
599cee73 2533 if (ckWARN(WARN_CLOSED))
bc37a18f 2534 report_evil_fh(gv, io, PL_op->op_type);
93189314 2535 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2536 RETPUSHUNDEF;
2537#else
cea2e8a9 2538 DIE(aTHX_ PL_no_sock_func, "listen");
a0d0e21e
LW
2539#endif
2540}
2541
2542PP(pp_accept)
2543{
a0d0e21e 2544#ifdef HAS_SOCKET
9cad6237 2545 dSP; dTARGET;
a0d0e21e
LW
2546 GV *ngv;
2547 GV *ggv;
2548 register IO *nstio;
2549 register IO *gstio;
93d47a36
JH
2550 char namebuf[MAXPATHLEN];
2551#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2552 Sock_size_t len = sizeof (struct sockaddr_in);
2553#else
2554 Sock_size_t len = sizeof namebuf;
2555#endif
a0d0e21e
LW
2556 int fd;
2557
2558 ggv = (GV*)POPs;
2559 ngv = (GV*)POPs;
2560
2561 if (!ngv)
2562 goto badexit;
2563 if (!ggv)
2564 goto nuts;
2565
2566 gstio = GvIO(ggv);
2567 if (!gstio || !IoIFP(gstio))
2568 goto nuts;
2569
2570 nstio = GvIOn(ngv);
93d47a36 2571 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
a0d0e21e
LW
2572 if (fd < 0)
2573 goto badexit;
a70048fb
AB
2574 if (IoIFP(nstio))
2575 do_close(ngv, FALSE);
460c8493
IZ
2576 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2577 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2578 IoTYPE(nstio) = IoTYPE_SOCKET;
a0d0e21e 2579 if (!IoIFP(nstio) || !IoOFP(nstio)) {
760ac839
LW
2580 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2581 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
6ad3d225 2582 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
a0d0e21e
LW
2583 goto badexit;
2584 }
8d2a6795
GS
2585#if defined(HAS_FCNTL) && defined(F_SETFD)
2586 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2587#endif
a0d0e21e 2588
ed79a026 2589#ifdef EPOC
93d47a36 2590 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
a9f1f6b0 2591 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
ed79a026 2592#endif
381c1bae 2593#ifdef __SCO_VERSION__
93d47a36 2594 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
381c1bae 2595#endif
ed79a026 2596
93d47a36 2597 PUSHp(namebuf, len);
a0d0e21e
LW
2598 RETURN;
2599
2600nuts:
599cee73 2601 if (ckWARN(WARN_CLOSED))
bc37a18f 2602 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
93189314 2603 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2604
2605badexit:
2606 RETPUSHUNDEF;
2607
2608#else
cea2e8a9 2609 DIE(aTHX_ PL_no_sock_func, "accept");
a0d0e21e
LW
2610#endif
2611}
2612
2613PP(pp_shutdown)
2614{
a0d0e21e 2615#ifdef HAS_SOCKET
9cad6237 2616 dSP; dTARGET;
a0d0e21e
LW
2617 int how = POPi;
2618 GV *gv = (GV*)POPs;
2619 register IO *io = GvIOn(gv);
2620
2621 if (!io || !IoIFP(io))
2622 goto nuts;
2623
6ad3d225 2624 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
a0d0e21e
LW
2625 RETURN;
2626
2627nuts:
599cee73 2628 if (ckWARN(WARN_CLOSED))
bc37a18f 2629 report_evil_fh(gv, io, PL_op->op_type);
93189314 2630 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2631 RETPUSHUNDEF;
2632#else
cea2e8a9 2633 DIE(aTHX_ PL_no_sock_func, "shutdown");
a0d0e21e
LW
2634#endif
2635}
2636
2637PP(pp_gsockopt)
2638{
2639#ifdef HAS_SOCKET
cea2e8a9 2640 return pp_ssockopt();
a0d0e21e 2641#else
cea2e8a9 2642 DIE(aTHX_ PL_no_sock_func, "getsockopt");
a0d0e21e
LW
2643#endif
2644}
2645
2646PP(pp_ssockopt)
2647{
a0d0e21e 2648#ifdef HAS_SOCKET
9cad6237 2649 dSP;
533c011a 2650 int optype = PL_op->op_type;
a0d0e21e
LW
2651 SV *sv;
2652 int fd;
2653 unsigned int optname;
2654 unsigned int lvl;
2655 GV *gv;
2656 register IO *io;
1e422769 2657 Sock_size_t len;
a0d0e21e
LW
2658
2659 if (optype == OP_GSOCKOPT)
2660 sv = sv_2mortal(NEWSV(22, 257));
2661 else
2662 sv = POPs;
2663 optname = (unsigned int) POPi;
2664 lvl = (unsigned int) POPi;
2665
2666 gv = (GV*)POPs;
2667 io = GvIOn(gv);
2668 if (!io || !IoIFP(io))
2669 goto nuts;
2670
760ac839 2671 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2672 switch (optype) {
2673 case OP_GSOCKOPT:
748a9306 2674 SvGROW(sv, 257);
a0d0e21e 2675 (void)SvPOK_only(sv);
748a9306
LW
2676 SvCUR_set(sv,256);
2677 *SvEND(sv) ='\0';
1e422769 2678 len = SvCUR(sv);
6ad3d225 2679 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
a0d0e21e 2680 goto nuts2;
1e422769 2681 SvCUR_set(sv, len);
748a9306 2682 *SvEND(sv) ='\0';
a0d0e21e
LW
2683 PUSHs(sv);
2684 break;
2685 case OP_SSOCKOPT: {
1e422769
PP
2686 char *buf;
2687 int aint;
2688 if (SvPOKp(sv)) {
2d8e6c8d
GS
2689 STRLEN l;
2690 buf = SvPV(sv, l);
2691 len = l;
1e422769 2692 }
56ee1660 2693 else {
a0d0e21e
LW
2694 aint = (int)SvIV(sv);
2695 buf = (char*)&aint;
2696 len = sizeof(int);
2697 }
6ad3d225 2698 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
a0d0e21e 2699 goto nuts2;
3280af22 2700 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2701 }
2702 break;
2703 }
2704 RETURN;
2705
2706nuts:
599cee73 2707 if (ckWARN(WARN_CLOSED))
bc37a18f 2708 report_evil_fh(gv, io, optype);
93189314 2709 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2710nuts2:
2711 RETPUSHUNDEF;
2712
2713#else
cea2e8a9 2714 DIE(aTHX_ PL_no_sock_func, "setsockopt");
a0d0e21e
LW
2715#endif
2716}
2717
2718PP(pp_getsockname)
2719{
2720#ifdef HAS_SOCKET
cea2e8a9 2721 return pp_getpeername();
a0d0e21e 2722#else
cea2e8a9 2723 DIE(aTHX_ PL_no_sock_func, "getsockname");
a0d0e21e
LW
2724#endif
2725}
2726
2727PP(pp_getpeername)
2728{
a0d0e21e 2729#ifdef HAS_SOCKET
9cad6237 2730 dSP;
533c011a 2731 int optype = PL_op->op_type;
a0d0e21e
LW
2732 SV *sv;
2733 int fd;
2734 GV *gv = (GV*)POPs;
2735 register IO *io = GvIOn(gv);
1e422769 2736 Sock_size_t len;
a0d0e21e
LW
2737
2738 if (!io || !IoIFP(io))
2739 goto nuts;
2740
2741 sv = sv_2mortal(NEWSV(22, 257));
748a9306 2742 (void)SvPOK_only(sv);
1e422769
PP
2743 len = 256;
2744 SvCUR_set(sv, len);
748a9306 2745 *SvEND(sv) ='\0';
760ac839 2746 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2747 switch (optype) {
2748 case OP_GETSOCKNAME:
6ad3d225 2749 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e
LW
2750 goto nuts2;
2751 break;
2752 case OP_GETPEERNAME:
6ad3d225 2753 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e 2754 goto nuts2;
490ab354
JH
2755#if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2756 {
2757 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";
2758 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2759 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2760 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2761 sizeof(u_short) + sizeof(struct in_addr))) {
301e8125 2762 goto nuts2;
490ab354
JH
2763 }
2764 }
2765#endif
a0d0e21e
LW
2766 break;
2767 }
13826f2c
CS
2768#ifdef BOGUS_GETNAME_RETURN
2769 /* Interactive Unix, getpeername() and getsockname()
2770 does not return valid namelen */
1e422769
PP
2771 if (len == BOGUS_GETNAME_RETURN)
2772 len = sizeof(struct sockaddr);
13826f2c 2773#endif
1e422769 2774 SvCUR_set(sv, len);
748a9306 2775 *SvEND(sv) ='\0';
a0d0e21e
LW
2776 PUSHs(sv);
2777 RETURN;
2778
2779nuts:
599cee73 2780 if (ckWARN(WARN_CLOSED))
bc37a18f 2781 report_evil_fh(gv, io, optype);
93189314 2782 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2783nuts2:
2784 RETPUSHUNDEF;
2785
2786#else
cea2e8a9 2787 DIE(aTHX_ PL_no_sock_func, "getpeername");
a0d0e21e
LW
2788#endif
2789}
2790
2791/* Stat calls. */
2792
2793PP(pp_lstat)
2794{
cea2e8a9 2795 return pp_stat();
a0d0e21e
LW
2796}
2797
2798PP(pp_stat)
2799{
39644a26 2800 dSP;
2dd78f96 2801 GV *gv;
54310121 2802 I32 gimme;
a0d0e21e 2803 I32 max = 13;
2d8e6c8d 2804 STRLEN n_a;
a0d0e21e 2805
533c011a 2806 if (PL_op->op_flags & OPf_REF) {
2dd78f96 2807 gv = cGVOP_gv;
8a4e5b40 2808 if (PL_op->op_type == OP_LSTAT) {
5d3e98de
RGS
2809 if (gv != PL_defgv) {
2810 if (ckWARN(WARN_IO))
9014280d 2811 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de
RGS
2812 "lstat() on filehandle %s", GvENAME(gv));
2813 } else if (PL_laststype != OP_LSTAT)
8a4e5b40 2814 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
8a4e5b40
DD
2815 }
2816
748a9306 2817 do_fstat:
2dd78f96 2818 if (gv != PL_defgv) {
3280af22 2819 PL_laststype = OP_STAT;
2dd78f96 2820 PL_statgv = gv;
3280af22 2821 sv_setpv(PL_statname, "");
2dd78f96
JH
2822 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2823 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
a0d0e21e 2824 }
9ddeeac9 2825 if (PL_laststatval < 0) {
2dd78f96
JH
2826 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2827 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
a0d0e21e 2828 max = 0;
9ddeeac9 2829 }
a0d0e21e
LW
2830 }
2831 else {
748a9306
LW
2832 SV* sv = POPs;
2833 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 2834 gv = (GV*)sv;
748a9306
LW
2835 goto do_fstat;
2836 }
2837 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 2838 gv = (GV*)SvRV(sv);
5d3e98de 2839 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
9014280d 2840 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de 2841 "lstat() on filehandle %s", GvENAME(gv));
748a9306
LW
2842 goto do_fstat;
2843 }
2d8e6c8d 2844 sv_setpv(PL_statname, SvPV(sv,n_a));
3280af22 2845 PL_statgv = Nullgv;
533c011a
NIS
2846 PL_laststype = PL_op->op_type;
2847 if (PL_op->op_type == OP_LSTAT)
2d8e6c8d 2848 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
a0d0e21e 2849 else
2d8e6c8d 2850 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
3280af22 2851 if (PL_laststatval < 0) {
2d8e6c8d 2852 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
9014280d 2853 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
a0d0e21e
LW
2854 max = 0;
2855 }
2856 }
2857
54310121
PP
2858 gimme = GIMME_V;
2859 if (gimme != G_ARRAY) {
2860 if (gimme != G_VOID)
2861 XPUSHs(boolSV(max));
2862 RETURN;
a0d0e21e
LW
2863 }
2864 if (max) {
36477c24
PP
2865 EXTEND(SP, max);
2866 EXTEND_MORTAL(max);
1ff81528
PL
2867 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2868 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
b448e4fe
JH
2869 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2870 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
146174a9
CB
2871#if Uid_t_size > IVSIZE
2872 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2873#else
23dcd6c8 2874# if Uid_t_sign <= 0
1ff81528 2875 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
23dcd6c8
JH
2876# else
2877 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2878# endif
146174a9 2879#endif
301e8125 2880#if Gid_t_size > IVSIZE
146174a9
CB
2881 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2882#else
23dcd6c8 2883# if Gid_t_sign <= 0
1ff81528 2884 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
23dcd6c8
JH
2885# else
2886 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2887# endif
146174a9 2888#endif
cbdc8872 2889#ifdef USE_STAT_RDEV
1ff81528 2890 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
cbdc8872 2891#else
79cb57f6 2892 PUSHs(sv_2mortal(newSVpvn("", 0)));
cbdc8872 2893#endif
146174a9 2894#if Off_t_size > IVSIZE
4a9d6100 2895 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
146174a9 2896#else
1ff81528 2897 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
146174a9 2898#endif
cbdc8872 2899#ifdef BIG_TIME
172ae379
JH
2900 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2901 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2902 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
cbdc8872 2903#else
1ff81528
PL
2904 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2905 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2906 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
cbdc8872 2907#endif
a0d0e21e 2908#ifdef USE_STAT_BLOCKS
b448e4fe
JH
2909 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2910 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
a0d0e21e 2911#else
79cb57f6
GS
2912 PUSHs(sv_2mortal(newSVpvn("", 0)));
2913 PUSHs(sv_2mortal(newSVpvn("", 0)));
a0d0e21e
LW
2914#endif
2915 }
2916 RETURN;
2917}
2918
fbb0b3b3
RGS
2919/* This macro is used by the stacked filetest operators :
2920 * if the previous filetest failed, short-circuit and pass its value.
2921 * Else, discard it from the stack and continue. --rgs
2922 */
2923#define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2924 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2925 else { (void)POPs; PUTBACK; } \
2926 }
2927
a0d0e21e
LW
2928PP(pp_ftrread)
2929{
9cad6237 2930 I32 result;
2a3ff820 2931 dSP;
fbb0b3b3 2932 STACKED_FTEST_CHECK;
5ff3f7a4 2933#if defined(HAS_ACCESS) && defined(R_OK)
1af34c76 2934 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 2935 STRLEN n_a;
43b06338 2936 result = access(POPpx, R_OK);
5ff3f7a4
GS
2937 if (result == 0)
2938 RETPUSHYES;
2939 if (result < 0)
2940 RETPUSHUNDEF;
2941 RETPUSHNO;
22865c03
GS
2942 }
2943 else
cea2e8a9 2944 result = my_stat();
5ff3f7a4 2945#else
cea2e8a9 2946 result = my_stat();
5ff3f7a4 2947#endif
22865c03 2948 SPAGAIN;
a0d0e21e
LW
2949 if (result < 0)
2950 RETPUSHUNDEF;
3280af22 2951 if (cando(S_IRUSR, 0, &PL_statcache))
a0d0e21e
LW
2952 RETPUSHYES;
2953 RETPUSHNO;
2954}
2955
2956PP(pp_ftrwrite)
2957{
9cad6237 2958 I32 result;
2a3ff820 2959 dSP;
fbb0b3b3 2960 STACKED_FTEST_CHECK;
5ff3f7a4 2961#if defined(HAS_ACCESS) && defined(W_OK)
1af34c76 2962 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 2963 STRLEN n_a;
43b06338 2964 result = access(POPpx, W_OK);
5ff3f7a4
GS
2965 if (result == 0)
2966 RETPUSHYES;
2967 if (result < 0)
2968 RETPUSHUNDEF;
2969 RETPUSHNO;
22865c03
GS
2970 }
2971 else
cea2e8a9 2972 result = my_stat();
5ff3f7a4 2973#else
cea2e8a9 2974 result = my_stat();
5ff3f7a4 2975#endif
22865c03 2976 SPAGAIN;
a0d0e21e
LW
2977 if (result < 0)
2978 RETPUSHUNDEF;
3280af22 2979 if (cando(S_IWUSR, 0, &PL_statcache))
a0d0e21e
LW
2980 RETPUSHYES;
2981 RETPUSHNO;
2982}
2983
2984PP(pp_ftrexec)
2985{
9cad6237 2986 I32 result;
2a3ff820 2987 dSP;
fbb0b3b3 2988 STACKED_FTEST_CHECK;
5ff3f7a4 2989#if defined(HAS_ACCESS) && defined(X_OK)
1af34c76 2990 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 2991 STRLEN n_a;
43b06338 2992 result = access(POPpx, X_OK);
5ff3f7a4
GS
2993 if (result == 0)
2994 RETPUSHYES;
2995 if (result < 0)
2996 RETPUSHUNDEF;
2997 RETPUSHNO;
22865c03
GS
2998 }
2999 else
cea2e8a9 3000 result = my_stat();
5ff3f7a4 3001#else
cea2e8a9 3002 result = my_stat();
5ff3f7a4 3003#endif
22865c03 3004 SPAGAIN;
a0d0e21e
LW
3005 if (result < 0)
3006 RETPUSHUNDEF;
3280af22 3007 if (cando(S_IXUSR, 0, &PL_statcache))
a0d0e21e
LW
3008 RETPUSHYES;
3009 RETPUSHNO;
3010}
3011
3012PP(pp_fteread)
3013{
9cad6237 3014 I32 result;
2a3ff820 3015 dSP;
fbb0b3b3 3016 STACKED_FTEST_CHECK;
5ff3f7a4 3017#ifdef PERL_EFF_ACCESS_R_OK
1af34c76 3018 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 3019 STRLEN n_a;
43b06338 3020 result = PERL_EFF_ACCESS_R_OK(POPpx);
5ff3f7a4
GS
3021 if (result == 0)
3022 RETPUSHYES;
3023 if (result < 0)
3024 RETPUSHUNDEF;
3025 RETPUSHNO;
22865c03
GS
3026 }
3027 else
cea2e8a9 3028 result = my_stat();
5ff3f7a4 3029#else
cea2e8a9 3030 result = my_stat();
5ff3f7a4 3031#endif
22865c03 3032 SPAGAIN;
a0d0e21e
LW
3033 if (result < 0)
3034 RETPUSHUNDEF;
3280af22 3035 if (cando(S_IRUSR, 1, &PL_statcache))
a0d0e21e
LW
3036 RETPUSHYES;
3037 RETPUSHNO;
3038}
3039
3040PP(pp_ftewrite)
3041{
9cad6237 3042 I32 result;
2a3ff820 3043 dSP;
fbb0b3b3 3044 STACKED_FTEST_CHECK;
5ff3f7a4 3045#ifdef PERL_EFF_ACCESS_W_OK
1af34c76 3046 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 3047 STRLEN n_a;
43b06338 3048 result = PERL_EFF_ACCESS_W_OK(POPpx);
5ff3f7a4
GS
3049 if (result == 0)
3050 RETPUSHYES;
3051 if (result < 0)
3052 RETPUSHUNDEF;
3053 RETPUSHNO;
22865c03
GS
3054 }
3055 else
cea2e8a9 3056 result = my_stat();
5ff3f7a4 3057#else
cea2e8a9 3058 result = my_stat();
5ff3f7a4 3059#endif
22865c03 3060 SPAGAIN;
a0d0e21e
LW
3061 if (result < 0)
3062 RETPUSHUNDEF;
3280af22 3063 if (cando(S_IWUSR, 1, &PL_statcache))
a0d0e21e
LW
3064 RETPUSHYES;
3065 RETPUSHNO;
3066}
3067
3068PP(pp_fteexec)
3069{
9cad6237 3070 I32 result;
2a3ff820 3071 dSP;
fbb0b3b3 3072 STACKED_FTEST_CHECK;
5ff3f7a4 3073#ifdef PERL_EFF_ACCESS_X_OK
1af34c76 3074 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
fbb0b3b3 3075 STRLEN n_a;
43b06338 3076 result = PERL_EFF_ACCESS_X_OK(POPpx);
5ff3f7a4
GS
3077 if (result == 0)
3078 RETPUSHYES;
3079 if (result < 0)
3080 RETPUSHUNDEF;
3081 RETPUSHNO;
22865c03
GS
3082 }
3083 else
cea2e8a9 3084 result = my_stat();
5ff3f7a4 3085#else
cea2e8a9 3086 result = my_stat();
5ff3f7a4 3087#endif
22865c03 3088 SPAGAIN;
a0d0e21e
LW
3089 if (result < 0)
3090 RETPUSHUNDEF;
3280af22 3091 if (cando(S_IXUSR, 1, &PL_statcache))
a0d0e21e
LW
3092 RETPUSHYES;
3093 RETPUSHNO;
3094}
3095
3096PP(pp_ftis)
3097{
fbb0b3b3 3098 I32 result;
2a3ff820 3099 dSP;
fbb0b3b3
RGS
3100 STACKED_FTEST_CHECK;
3101 result = my_stat();
3102 SPAGAIN;
a0d0e21e
LW
3103 if (result < 0)
3104 RETPUSHUNDEF;
3105 RETPUSHYES;
3106}
3107
3108PP(pp_fteowned)
3109{
cea2e8a9 3110 return pp_ftrowned();
a0d0e21e
LW
3111}
3112
3113PP(pp_ftrowned)
3114{
fbb0b3b3 3115 I32 result;
2a3ff820 3116 dSP;
fbb0b3b3
RGS
3117 STACKED_FTEST_CHECK;
3118 result = my_stat();
3119 SPAGAIN;
a0d0e21e
LW
3120 if (result < 0)
3121 RETPUSHUNDEF;
146174a9
CB
3122 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3123 PL_euid : PL_uid) )
a0d0e21e
LW
3124 RETPUSHYES;
3125 RETPUSHNO;
3126}
3127
3128PP(pp_ftzero)
3129{
fbb0b3b3 3130 I32 result;
2a3ff820 3131 dSP;
fbb0b3b3
RGS
3132 STACKED_FTEST_CHECK;
3133 result = my_stat();
3134 SPAGAIN;
a0d0e21e
LW
3135 if (result < 0)
3136 RETPUSHUNDEF;
146174a9 3137 if (PL_statcache.st_size == 0)
a0d0e21e
LW
3138 RETPUSHYES;
3139 RETPUSHNO;
3140}
3141
3142PP(pp_ftsize)
3143{
fbb0b3b3 3144 I32 result;
2a3ff820 3145 dSP; dTARGET;
fbb0b3b3
RGS
3146 STACKED_FTEST_CHECK;
3147 result = my_stat();
3148 SPAGAIN;
a0d0e21e
LW
3149 if (result < 0)
3150 RETPUSHUNDEF;
146174a9
CB
3151#if Off_t_size > IVSIZE
3152 PUSHn(PL_statcache.st_size);
3153#else
3280af22 3154 PUSHi(PL_statcache.st_size);
146174a9 3155#endif
a0d0e21e
LW
3156 RETURN;
3157}
3158
3159PP(pp_ftmtime)
3160{
fbb0b3b3 3161 I32 result;
2a3ff820 3162 dSP; dTARGET;
fbb0b3b3
RGS
3163 STACKED_FTEST_CHECK;
3164 result = my_stat();
3165 SPAGAIN;
a0d0e21e
LW
3166 if (result < 0)
3167 RETPUSHUNDEF;
a80d47c2 3168 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
a0d0e21e
LW
3169 RETURN;
3170}
3171
3172PP(pp_ftatime)
3173{
fbb0b3b3 3174 I32 result;
2a3ff820 3175 dSP; dTARGET;
fbb0b3b3
RGS
3176 STACKED_FTEST_CHECK;
3177 result = my_stat();
3178 SPAGAIN;
a0d0e21e
LW
3179 if (result < 0)
3180 RETPUSHUNDEF;
a80d47c2 3181 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
a0d0e21e
LW
3182 RETURN;
3183}
3184
3185PP(pp_ftctime)
3186{
fbb0b3b3 3187 I32 result;
2a3ff820 3188 dSP; dTARGET;
fbb0b3b3
RGS
3189 STACKED_FTEST_CHECK;
3190 result = my_stat();
3191 SPAGAIN;
a0d0e21e
LW
3192 if (result < 0)
3193 RETPUSHUNDEF;
a80d47c2 3194 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
a0d0e21e
LW
3195 RETURN;
3196}
3197
3198PP(pp_ftsock)
3199{
fbb0b3b3 3200 I32 result;
2a3ff820 3201 dSP;
fbb0b3b3
RGS
3202 STACKED_FTEST_CHECK;
3203 result = my_stat();
3204 SPAGAIN;
a0d0e21e
LW
3205 if (result < 0)
3206 RETPUSHUNDEF;
3280af22 3207 if (S_ISSOCK(PL_statcache.st_mode))
a0d0e21e
LW
3208 RETPUSHYES;
3209 RETPUSHNO;
3210}
3211
3212PP(pp_ftchr)
3213{
fbb0b3b3 3214 I32 result;
2a3ff820 3215 dSP;
fbb0b3b3
RGS
3216 STACKED_FTEST_CHECK;
3217 result = my_stat();
3218 SPAGAIN;
a0d0e21e
LW
3219 if (result < 0)
3220 RETPUSHUNDEF;
3280af22 3221 if (S_ISCHR(PL_statcache.st_mode))
a0d0e21e
LW
3222 RETPUSHYES;
3223 RETPUSHNO;
3224}
3225
3226PP(pp_ftblk)
3227{
fbb0b3b3 3228 I32 result;
2a3ff820 3229 dSP;
fbb0b3b3
RGS
3230 STACKED_FTEST_CHECK;
3231 result = my_stat();
3232 SPAGAIN;
a0d0e21e
LW
3233 if (result < 0)
3234 RETPUSHUNDEF;
3280af22 3235 if (S_ISBLK(PL_statcache.st_mode))
a0d0e21e
LW
3236 RETPUSHYES;
3237 RETPUSHNO;
3238}
3239
3240PP(pp_ftfile)
3241{
fbb0b3b3 3242 I32 result;
2a3ff820 3243 dSP;
fbb0b3b3
RGS
3244 STACKED_FTEST_CHECK;
3245 result = my_stat();
3246 SPAGAIN;
a0d0e21e
LW
3247 if (result < 0)
3248 RETPUSHUNDEF;
3280af22 3249 if (S_ISREG(PL_statcache.st_mode))
a0d0e21e
LW
3250 RETPUSHYES;
3251 RETPUSHNO;
3252}
3253
3254PP(pp_ftdir)
3255{
fbb0b3b3 3256 I32 result;
2a3ff820 3257 dSP;
fbb0b3b3
RGS
3258 STACKED_FTEST_CHECK;
3259 result = my_stat();
3260 SPAGAIN;
a0d0e21e
LW
3261 if (result < 0)
3262 RETPUSHUNDEF;
3280af22 3263 if (S_ISDIR(PL_statcache.st_mode))
a0d0e21e
LW
3264 RETPUSHYES;
3265 RETPUSHNO;
3266}
3267
3268PP(pp_ftpipe)
3269{
fbb0b3b3 3270 I32 result;
2a3ff820 3271 dSP;
fbb0b3b3
RGS
3272 STACKED_FTEST_CHECK;
3273 result = my_stat();
3274 SPAGAIN;
a0d0e21e
LW
3275 if (result < 0)
3276 RETPUSHUNDEF;
3280af22 3277 if (S_ISFIFO(PL_statcache.st_mode))
a0d0e21e
LW
3278 RETPUSHYES;
3279 RETPUSHNO;
3280}
3281
3282PP(pp_ftlink)
3283{
9cad6237 3284 I32 result = my_lstat();
2a3ff820 3285 dSP;
a0d0e21e
LW
3286 if (result < 0)
3287 RETPUSHUNDEF;
3280af22 3288 if (S_ISLNK(PL_statcache.st_mode))
a0d0e21e
LW
3289 RETPUSHYES;
3290 RETPUSHNO;
3291}
3292
3293PP(pp_ftsuid)
3294{
39644a26 3295 dSP;
a0d0e21e 3296#ifdef S_ISUID
fbb0b3b3
RGS
3297 I32 result;
3298 STACKED_FTEST_CHECK;
3299 result = my_stat();
a0d0e21e
LW
3300 SPAGAIN;
3301 if (result < 0)
3302 RETPUSHUNDEF;
3280af22 3303 if (PL_statcache.st_mode & S_ISUID)
a0d0e21e
LW
3304 RETPUSHYES;
3305#endif
3306 RETPUSHNO;
3307}
3308
3309PP(pp_ftsgid)
3310{
39644a26 3311 dSP;
a0d0e21e 3312#ifdef S_ISGID
fbb0b3b3
RGS
3313 I32 result;
3314 STACKED_FTEST_CHECK;
3315 result = my_stat();
a0d0e21e
LW
3316 SPAGAIN;
3317 if (result < 0)
3318 RETPUSHUNDEF;
3280af22 3319 if (PL_statcache.st_mode & S_ISGID)
a0d0e21e
LW
3320 RETPUSHYES;
3321#endif
3322 RETPUSHNO;
3323}
3324
3325PP(pp_ftsvtx)
3326{
39644a26 3327 dSP;
a0d0e21e 3328#ifdef S_ISVTX
fbb0b3b3
RGS
3329 I32 result;
3330 STACKED_FTEST_CHECK;
3331 result = my_stat();
a0d0e21e
LW
3332 SPAGAIN;
3333 if (result < 0)
3334 RETPUSHUNDEF;
3280af22 3335 if (PL_statcache.st_mode & S_ISVTX)
a0d0e21e
LW
3336 RETPUSHYES;
3337#endif
3338 RETPUSHNO;
3339}
3340
3341PP(pp_fttty)
3342{
39644a26 3343 dSP;
a0d0e21e
LW
3344 int fd;
3345 GV *gv;
7a5fd60d 3346 SV *tmpsv = Nullsv;
fb73857a 3347
fbb0b3b3
RGS
3348 STACKED_FTEST_CHECK;
3349
533c011a 3350 if (PL_op->op_flags & OPf_REF)
146174a9 3351 gv = cGVOP_gv;
fb73857a
PP
3352 else if (isGV(TOPs))
3353 gv = (GV*)POPs;
3354 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3355 gv = (GV*)SvRV(POPs);
a0d0e21e 3356 else
7a5fd60d 3357 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
fb73857a 3358
a0d0e21e 3359 if (GvIO(gv) && IoIFP(GvIOp(gv)))
760ac839 3360 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
7a5fd60d
NC
3361 else if (tmpsv && SvOK(tmpsv)) {
3362 STRLEN n_a;
3363 char *tmps = SvPV(tmpsv, n_a);
3364 if (isDIGIT(*tmps))
3365 fd = atoi(tmps);
3366 else
3367 RETPUSHUNDEF;
3368 }
a0d0e21e
LW
3369 else
3370 RETPUSHUNDEF;
6ad3d225 3371 if (PerlLIO_isatty(fd))
a0d0e21e
LW
3372 RETPUSHYES;
3373 RETPUSHNO;
3374}
3375
16d20bd9
AD
3376#if defined(atarist) /* this will work with atariST. Configure will
3377 make guesses for other systems. */
3378# define FILE_base(f) ((f)->_base)
3379# define FILE_ptr(f) ((f)->_ptr)
3380# define FILE_cnt(f) ((f)->_cnt)
3381# define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
a0d0e21e
LW
3382#endif
3383
3384PP(pp_fttext)
3385{
39644a26 3386 dSP;
a0d0e21e
LW
3387 I32 i;
3388 I32 len;
3389 I32 odd = 0;
3390 STDCHAR tbuf[512];
3391 register STDCHAR *s;
3392 register IO *io;
5f05dabc
PP
3393 register SV *sv;
3394 GV *gv;
2d8e6c8d 3395 STRLEN n_a;
146174a9 3396 PerlIO *fp;
a0d0e21e 3397
fbb0b3b3
RGS
3398 STACKED_FTEST_CHECK;
3399
533c011a 3400 if (PL_op->op_flags & OPf_REF)
146174a9 3401 gv = cGVOP_gv;
5f05dabc
PP
3402 else if (isGV(TOPs))
3403 gv = (GV*)POPs;
3404 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3405 gv = (GV*)SvRV(POPs);
3406 else
3407 gv = Nullgv;
3408
3409 if (gv) {
a0d0e21e 3410 EXTEND(SP, 1);
3280af22
NIS
3411 if (gv == PL_defgv) {
3412 if (PL_statgv)
3413 io = GvIO(PL_statgv);
a0d0e21e 3414 else {
3280af22 3415 sv = PL_statname;
a0d0e21e
LW
3416 goto really_filename;
3417 }
3418 }
3419 else {
3280af22
NIS
3420 PL_statgv = gv;
3421 PL_laststatval = -1;
3422 sv_setpv(PL_statname, "");
3423 io = GvIO(PL_statgv);
a0d0e21e
LW
3424 }
3425 if (io && IoIFP(io)) {
5f05dabc 3426 if (! PerlIO_has_base(IoIFP(io)))
cea2e8a9 3427 DIE(aTHX_ "-T and -B not implemented on filehandles");
3280af22
NIS
3428 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3429 if (PL_laststatval < 0)
5f05dabc 3430 RETPUSHUNDEF;
9cbac4c7 3431 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
533c011a 3432 if (PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3433 RETPUSHNO;
3434 else
3435 RETPUSHYES;
9cbac4c7 3436 }
a20bf0c3 3437 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
760ac839 3438 i = PerlIO_getc(IoIFP(io));
a0d0e21e 3439 if (i != EOF)
760ac839 3440 (void)PerlIO_ungetc(IoIFP(io),i);
a0d0e21e 3441 }
a20bf0c3 3442 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
a0d0e21e 3443 RETPUSHYES;
a20bf0c3
JH
3444 len = PerlIO_get_bufsiz(IoIFP(io));
3445 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
760ac839
LW
3446 /* sfio can have large buffers - limit to 512 */
3447 if (len > 512)
3448 len = 512;
a0d0e21e
LW
3449 }
3450 else {
2dd78f96 3451 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
146174a9 3452 gv = cGVOP_gv;
2dd78f96 3453 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
146174a9 3454 }
93189314 3455 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
3456 RETPUSHUNDEF;
3457 }
3458 }
3459 else {
3460 sv = POPs;
5f05dabc 3461 really_filename:
3280af22 3462 PL_statgv = Nullgv;
5c9aa243 3463 PL_laststype = OP_STAT;
2d8e6c8d 3464 sv_setpv(PL_statname, SvPV(sv, n_a));
146174a9 3465 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
f9341440 3466 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
9014280d 3467 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
a0d0e21e
LW
3468 RETPUSHUNDEF;
3469 }
146174a9
CB
3470 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3471 if (PL_laststatval < 0) {
3472 (void)PerlIO_close(fp);
5f05dabc 3473 RETPUSHUNDEF;
146174a9 3474 }
60382766 3475 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
146174a9
CB
3476 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3477 (void)PerlIO_close(fp);
a0d0e21e 3478 if (len <= 0) {
533c011a 3479 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3480 RETPUSHNO; /* special case NFS directories */
3481 RETPUSHYES; /* null file is anything */
3482 }
3483 s = tbuf;
3484 }
3485
3486 /* now scan s to look for textiness */
4633a7c4 3487 /* XXX ASCII dependent code */
a0d0e21e 3488
146174a9
CB
3489#if defined(DOSISH) || defined(USEMYBINMODE)
3490 /* ignore trailing ^Z on short files */
3491 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3492 --len;
3493#endif
3494
a0d0e21e
LW
3495 for (i = 0; i < len; i++, s++) {
3496 if (!*s) { /* null never allowed in text */
3497 odd += len;
3498 break;
3499 }
9d116dd7 3500#ifdef EBCDIC
301e8125 3501 else if (!(isPRINT(*s) || isSPACE(*s)))
9d116dd7
JH
3502 odd++;
3503#else
146174a9
CB
3504 else if (*s & 128) {
3505#ifdef USE_LOCALE
2de3dbcc 3506 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
b3f66c68
GS
3507 continue;
3508#endif
3509 /* utf8 characters don't count as odd */
fd400ab9 3510 if (UTF8_IS_START(*s)) {
b3f66c68
GS
3511 int ulen = UTF8SKIP(s);
3512 if (ulen < len - i) {
3513 int j;
3514 for (j = 1; j < ulen; j++) {
fd400ab9 3515 if (!UTF8_IS_CONTINUATION(s[j]))
b3f66c68
GS
3516 goto not_utf8;