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