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