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