This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More ptags tweakage from Stas; now add 'fullpath'
[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
e366b469
PG
1027# if SELECT_MIN_BITS > 1
1028 /* If SELECT_MIN_BITS is greater than one we most probably will want
1029 * to align the sizes with SELECT_MIN_BITS/8 because for example
1030 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1031 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1032 * on (sets/tests/clears bits) is 32 bits. */
1033 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1034# else
1035 growsize = sizeof(fd_set);
1036# endif
1037
a0d0e21e
LW
1038 sv = SP[4];
1039 if (SvOK(sv)) {
1040 value = SvNV(sv);
1041 if (value < 0.0)
1042 value = 0.0;
1043 timebuf.tv_sec = (long)value;
65202027 1044 value -= (NV)timebuf.tv_sec;
a0d0e21e
LW
1045 timebuf.tv_usec = (long)(value * 1000000.0);
1046 }
1047 else
1048 tbuf = Null(struct timeval*);
1049
1050 for (i = 1; i <= 3; i++) {
1051 sv = SP[i];
1052 if (!SvOK(sv)) {
1053 fd_sets[i] = 0;
1054 continue;
1055 }
1056 else if (!SvPOK(sv))
2d8e6c8d 1057 SvPV_force(sv,n_a); /* force string conversion */
a0d0e21e
LW
1058 j = SvLEN(sv);
1059 if (j < growsize) {
1060 Sv_Grow(sv, growsize);
a0d0e21e 1061 }
c07a80fd
PP
1062 j = SvCUR(sv);
1063 s = SvPVX(sv) + j;
1064 while (++j <= growsize) {
1065 *s++ = '\0';
1066 }
1067
a0d0e21e
LW
1068#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1069 s = SvPVX(sv);
1070 New(403, fd_sets[i], growsize, char);
1071 for (offset = 0; offset < growsize; offset += masksize) {
1072 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1073 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1074 }
1075#else
1076 fd_sets[i] = SvPVX(sv);
1077#endif
1078 }
1079
dc4c69d9
JH
1080#ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1081 /* Can't make just the (void*) conditional because that would be
1082 * cpp #if within cpp macro, and not all compilers like that. */
1083 nfound = PerlSock_select(
1084 maxlen * 8,
1085 (Select_fd_set_t) fd_sets[1],
1086 (Select_fd_set_t) fd_sets[2],
1087 (Select_fd_set_t) fd_sets[3],
1088 (void*) tbuf); /* Workaround for compiler bug. */
1089#else
6ad3d225 1090 nfound = PerlSock_select(
a0d0e21e
LW
1091 maxlen * 8,
1092 (Select_fd_set_t) fd_sets[1],
1093 (Select_fd_set_t) fd_sets[2],
1094 (Select_fd_set_t) fd_sets[3],
1095 tbuf);
dc4c69d9 1096#endif
a0d0e21e
LW
1097 for (i = 1; i <= 3; i++) {
1098 if (fd_sets[i]) {
1099 sv = SP[i];
1100#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1101 s = SvPVX(sv);
1102 for (offset = 0; offset < growsize; offset += masksize) {
1103 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1104 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1105 }
1106 Safefree(fd_sets[i]);
1107#endif
1108 SvSETMAGIC(sv);
1109 }
1110 }
1111
1112 PUSHi(nfound);
1113 if (GIMME == G_ARRAY && tbuf) {
65202027
DS
1114 value = (NV)(timebuf.tv_sec) +
1115 (NV)(timebuf.tv_usec) / 1000000.0;
3280af22 1116 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
a0d0e21e
LW
1117 sv_setnv(sv, value);
1118 }
1119 RETURN;
1120#else
cea2e8a9 1121 DIE(aTHX_ "select not implemented");
a0d0e21e
LW
1122#endif
1123}
1124
4633a7c4 1125void
864dbfa3 1126Perl_setdefout(pTHX_ GV *gv)
4633a7c4
LW
1127{
1128 if (gv)
1129 (void)SvREFCNT_inc(gv);
3280af22
NIS
1130 if (PL_defoutgv)
1131 SvREFCNT_dec(PL_defoutgv);
1132 PL_defoutgv = gv;
4633a7c4
LW
1133}
1134
a0d0e21e
LW
1135PP(pp_select)
1136{
39644a26 1137 dSP; dTARGET;
4633a7c4
LW
1138 GV *newdefout, *egv;
1139 HV *hv;
1140
533c011a 1141 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
4633a7c4 1142
3280af22 1143 egv = GvEGV(PL_defoutgv);
4633a7c4 1144 if (!egv)
3280af22 1145 egv = PL_defoutgv;
4633a7c4
LW
1146 hv = GvSTASH(egv);
1147 if (! hv)
3280af22 1148 XPUSHs(&PL_sv_undef);
4633a7c4 1149 else {
cbdc8872 1150 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
f86702cc 1151 if (gvp && *gvp == egv) {
f7aaccc2 1152 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
f86702cc
PP
1153 XPUSHTARG;
1154 }
1155 else {
1156 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1157 }
4633a7c4
LW
1158 }
1159
1160 if (newdefout) {
ded8aa31
GS
1161 if (!GvIO(newdefout))
1162 gv_IOadd(newdefout);
4633a7c4
LW
1163 setdefout(newdefout);
1164 }
1165
a0d0e21e
LW
1166 RETURN;
1167}
1168
1169PP(pp_getc)
1170{
39644a26 1171 dSP; dTARGET;
a0d0e21e 1172 GV *gv;
90133b69 1173 IO *io = NULL;
2ae324a7 1174 MAGIC *mg;
a0d0e21e 1175
32da55ab 1176 if (MAXARG == 0)
3280af22 1177 gv = PL_stdingv;
a0d0e21e
LW
1178 else
1179 gv = (GV*)POPs;
2ae324a7 1180
5b468f54
AMS
1181 if (gv && (io = GvIO(gv))
1182 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1183 {
54310121 1184 I32 gimme = GIMME_V;
2ae324a7 1185 PUSHMARK(SP);
5b468f54 1186 XPUSHs(SvTIED_obj((SV*)io, mg));
2ae324a7
PP
1187 PUTBACK;
1188 ENTER;
864dbfa3 1189 call_method("GETC", gimme);
2ae324a7
PP
1190 LEAVE;
1191 SPAGAIN;
54310121
PP
1192 if (gimme == G_SCALAR)
1193 SvSetMagicSV_nosteal(TARG, TOPs);
2ae324a7
PP
1194 RETURN;
1195 }
90133b69 1196 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
97e322ff
RGS
1197 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1198 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
90133b69 1199 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 1200 SETERRNO(EBADF,RMS_IFI);
a0d0e21e 1201 RETPUSHUNDEF;
90133b69 1202 }
bbce6d69 1203 TAINT;
a0d0e21e 1204 sv_setpv(TARG, " ");
9bc64814 1205 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
7d59b7e4
NIS
1206 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1207 /* Find out how many bytes the char needs */
1208 Size_t len = UTF8SKIP(SvPVX(TARG));
1209 if (len > 1) {
1210 SvGROW(TARG,len+1);
1211 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1212 SvCUR_set(TARG,1+len);
1213 }
1214 SvUTF8_on(TARG);
1215 }
a0d0e21e
LW
1216 PUSHTARG;
1217 RETURN;
1218}
1219
1220PP(pp_read)
1221{
cea2e8a9 1222 return pp_sysread();
a0d0e21e
LW
1223}
1224
76e3520e 1225STATIC OP *
cea2e8a9 1226S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
a0d0e21e 1227{
c09156bb 1228 register PERL_CONTEXT *cx;
54310121 1229 I32 gimme = GIMME_V;
a0d0e21e
LW
1230
1231 ENTER;
1232 SAVETMPS;
1233
1234 push_return(retop);
146174a9 1235 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
a0d0e21e 1236 PUSHFORMAT(cx);
dd2155a4 1237 PAD_SET_CUR(CvPADLIST(cv), 1);
a0d0e21e 1238
4633a7c4 1239 setdefout(gv); /* locally select filehandle so $% et al work */
a0d0e21e
LW
1240 return CvSTART(cv);
1241}
1242
1243PP(pp_enterwrite)
1244{
39644a26 1245 dSP;
a0d0e21e
LW
1246 register GV *gv;
1247 register IO *io;
1248 GV *fgv;
1249 CV *cv;
1250
1251 if (MAXARG == 0)
3280af22 1252 gv = PL_defoutgv;
a0d0e21e
LW
1253 else {
1254 gv = (GV*)POPs;
1255 if (!gv)
3280af22 1256 gv = PL_defoutgv;
a0d0e21e
LW
1257 }
1258 EXTEND(SP, 1);
1259 io = GvIO(gv);
1260 if (!io) {
1261 RETPUSHNO;
1262 }
1263 if (IoFMT_GV(io))
1264 fgv = IoFMT_GV(io);
1265 else
1266 fgv = gv;
1267
1268 cv = GvFORM(fgv);
a0d0e21e 1269 if (!cv) {
2dd78f96 1270 char *name = NULL;
a0d0e21e 1271 if (fgv) {
748a9306 1272 SV *tmpsv = sv_newmortal();
43693395 1273 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
2dd78f96 1274 name = SvPV_nolen(tmpsv);
a0d0e21e 1275 }
2dd78f96
JH
1276 if (name && *name)
1277 DIE(aTHX_ "Undefined format \"%s\" called", name);
cea2e8a9 1278 DIE(aTHX_ "Not a format reference");
a0d0e21e 1279 }
44a8e56a
PP
1280 if (CvCLONE(cv))
1281 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
a0d0e21e 1282
44a8e56a 1283 IoFLAGS(io) &= ~IOf_DIDTOP;
533c011a 1284 return doform(cv,gv,PL_op->op_next);
a0d0e21e
LW
1285}
1286
1287PP(pp_leavewrite)
1288{
39644a26 1289 dSP;
a0d0e21e
LW
1290 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1291 register IO *io = GvIOp(gv);
760ac839
LW
1292 PerlIO *ofp = IoOFP(io);
1293 PerlIO *fp;
a0d0e21e
LW
1294 SV **newsp;
1295 I32 gimme;
c09156bb 1296 register PERL_CONTEXT *cx;
a0d0e21e 1297
760ac839 1298 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
3280af22 1299 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
7ef822cd
JH
1300 if (!io || !ofp)
1301 goto forget_top;
3280af22
NIS
1302 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1303 PL_formtarget != PL_toptarget)
a0d0e21e 1304 {
4633a7c4
LW
1305 GV *fgv;
1306 CV *cv;
a0d0e21e
LW
1307 if (!IoTOP_GV(io)) {
1308 GV *topgv;
46fc3d4c 1309 SV *topname;
a0d0e21e
LW
1310
1311 if (!IoTOP_NAME(io)) {
1312 if (!IoFMT_NAME(io))
1313 IoFMT_NAME(io) = savepv(GvNAME(gv));
cea2e8a9 1314 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
46fc3d4c 1315 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
748a9306 1316 if ((topgv && GvFORM(topgv)) ||
a0d0e21e 1317 !gv_fetchpv("top",FALSE,SVt_PVFM))
46fc3d4c 1318 IoTOP_NAME(io) = savepv(SvPVX(topname));
a0d0e21e
LW
1319 else
1320 IoTOP_NAME(io) = savepv("top");
1321 }
1322 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1323 if (!topgv || !GvFORM(topgv)) {
1324 IoLINES_LEFT(io) = 100000000;
1325 goto forget_top;
1326 }
1327 IoTOP_GV(io) = topgv;
1328 }
748a9306
LW
1329 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1330 I32 lines = IoLINES_LEFT(io);
3280af22 1331 char *s = SvPVX(PL_formtarget);
8e07c86e
AD
1332 if (lines <= 0) /* Yow, header didn't even fit!!! */
1333 goto forget_top;
748a9306
LW
1334 while (lines-- > 0) {
1335 s = strchr(s, '\n');
1336 if (!s)
1337 break;
1338 s++;
1339 }
1340 if (s) {
d75029d0
NIS
1341 STRLEN save = SvCUR(PL_formtarget);
1342 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1343 do_print(PL_formtarget, ofp);
1344 SvCUR_set(PL_formtarget, save);
3280af22
NIS
1345 sv_chop(PL_formtarget, s);
1346 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
748a9306
LW
1347 }
1348 }
a0d0e21e 1349 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
d75029d0 1350 do_print(PL_formfeed, ofp);
a0d0e21e
LW
1351 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1352 IoPAGE(io)++;
3280af22 1353 PL_formtarget = PL_toptarget;
748a9306 1354 IoFLAGS(io) |= IOf_DIDTOP;
4633a7c4
LW
1355 fgv = IoTOP_GV(io);
1356 if (!fgv)
cea2e8a9 1357 DIE(aTHX_ "bad top format reference");
4633a7c4 1358 cv = GvFORM(fgv);
2dd78f96
JH
1359 {
1360 char *name = NULL;
1361 if (!cv) {
1362 SV *sv = sv_newmortal();
1363 gv_efullname4(sv, fgv, Nullch, FALSE);
1364 name = SvPV_nolen(sv);
1365 }
1366 if (name && *name)
1367 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1368 /* why no:
1369 else
1370 DIE(aTHX_ "Undefined top format called");
1371 ?*/
4633a7c4 1372 }
44a8e56a
PP
1373 if (CvCLONE(cv))
1374 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
533c011a 1375 return doform(cv,gv,PL_op);
a0d0e21e
LW
1376 }
1377
1378 forget_top:
3280af22 1379 POPBLOCK(cx,PL_curpm);
a0d0e21e
LW
1380 POPFORMAT(cx);
1381 LEAVE;
1382
1383 fp = IoOFP(io);
1384 if (!fp) {
599cee73 1385 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1386 if (IoIFP(io))
1387 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1388 else if (ckWARN(WARN_CLOSED))
bc37a18f 1389 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1390 }
3280af22 1391 PUSHs(&PL_sv_no);
a0d0e21e
LW
1392 }
1393 else {
3280af22 1394 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
599cee73 1395 if (ckWARN(WARN_IO))
9014280d 1396 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
a0d0e21e 1397 }
d75029d0 1398 if (!do_print(PL_formtarget, fp))
3280af22 1399 PUSHs(&PL_sv_no);
a0d0e21e 1400 else {
3280af22
NIS
1401 FmLINES(PL_formtarget) = 0;
1402 SvCUR_set(PL_formtarget, 0);
1403 *SvEND(PL_formtarget) = '\0';
a0d0e21e 1404 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1405 (void)PerlIO_flush(fp);
3280af22 1406 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1407 }
1408 }
9cbac4c7 1409 /* bad_ofp: */
3280af22 1410 PL_formtarget = PL_bodytarget;
a0d0e21e
LW
1411 PUTBACK;
1412 return pop_return();
1413}
1414
1415PP(pp_prtf)
1416{
39644a26 1417 dSP; dMARK; dORIGMARK;
a0d0e21e
LW
1418 GV *gv;
1419 IO *io;
760ac839 1420 PerlIO *fp;
26db47c4 1421 SV *sv;
46fc3d4c 1422 MAGIC *mg;
a0d0e21e 1423
533c011a 1424 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1425 gv = (GV*)*++MARK;
1426 else
3280af22 1427 gv = PL_defoutgv;
46fc3d4c 1428
5b468f54
AMS
1429 if (gv && (io = GvIO(gv))
1430 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1431 {
46fc3d4c 1432 if (MARK == ORIGMARK) {
4352c267 1433 MEXTEND(SP, 1);
46fc3d4c
PP
1434 ++MARK;
1435 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1436 ++SP;
1437 }
1438 PUSHMARK(MARK - 1);
5b468f54 1439 *MARK = SvTIED_obj((SV*)io, mg);
46fc3d4c
PP
1440 PUTBACK;
1441 ENTER;
864dbfa3 1442 call_method("PRINTF", G_SCALAR);
46fc3d4c
PP
1443 LEAVE;
1444 SPAGAIN;
1445 MARK = ORIGMARK + 1;
1446 *MARK = *SP;
1447 SP = MARK;
1448 RETURN;
1449 }
1450
26db47c4 1451 sv = NEWSV(0,0);
a0d0e21e 1452 if (!(io = GvIO(gv))) {
2dd78f96
JH
1453 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1454 report_evil_fh(gv, io, PL_op->op_type);
93189314 1455 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
1456 goto just_say_no;
1457 }
1458 else if (!(fp = IoOFP(io))) {
599cee73 1459 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
6e6ef6b2
NC
1460 if (IoIFP(io))
1461 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
599cee73 1462 else if (ckWARN(WARN_CLOSED))
bc37a18f 1463 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 1464 }
93189314 1465 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
a0d0e21e
LW
1466 goto just_say_no;
1467 }
1468 else {
1469 do_sprintf(sv, SP - MARK, MARK + 1);
1470 if (!do_print(sv, fp))
1471 goto just_say_no;
1472
1473 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 1474 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
1475 goto just_say_no;
1476 }
1477 SvREFCNT_dec(sv);
1478 SP = ORIGMARK;
3280af22 1479 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1480 RETURN;
1481
1482 just_say_no:
1483 SvREFCNT_dec(sv);
1484 SP = ORIGMARK;
3280af22 1485 PUSHs(&PL_sv_undef);
a0d0e21e
LW
1486 RETURN;
1487}
1488
c07a80fd
PP
1489PP(pp_sysopen)
1490{
39644a26 1491 dSP;
c07a80fd 1492 GV *gv;
c07a80fd
PP
1493 SV *sv;
1494 char *tmps;
1495 STRLEN len;
1496 int mode, perm;
1497
1498 if (MAXARG > 3)
1499 perm = POPi;
1500 else
1501 perm = 0666;
1502 mode = POPi;
1503 sv = POPs;
1504 gv = (GV *)POPs;
1505
4592e6ca
NIS
1506 /* Need TIEHANDLE method ? */
1507
c07a80fd
PP
1508 tmps = SvPV(sv, len);
1509 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1510 IoLINES(GvIOp(gv)) = 0;
3280af22 1511 PUSHs(&PL_sv_yes);
c07a80fd
PP
1512 }
1513 else {
3280af22 1514 PUSHs(&PL_sv_undef);
c07a80fd
PP
1515 }
1516 RETURN;
1517}
1518
a0d0e21e
LW
1519PP(pp_sysread)
1520{
39644a26 1521 dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
1522 int offset;
1523 GV *gv;
1524 IO *io;
1525 char *buffer;
5b54f415 1526 SSize_t length;
eb5c063a 1527 SSize_t count;
1e422769 1528 Sock_size_t bufsize;
748a9306 1529 SV *bufsv;
a0d0e21e 1530 STRLEN blen;
2ae324a7 1531 MAGIC *mg;
eb5c063a
NIS
1532 int fp_utf8;
1533 Size_t got = 0;
1534 Size_t wanted;
1d636c13 1535 bool charstart = FALSE;
87330c3c
JH
1536 STRLEN charskip = 0;
1537 STRLEN skip = 0;
a0d0e21e
LW
1538
1539 gv = (GV*)*++MARK;
5b468f54
AMS
1540 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1541 && gv && (io = GvIO(gv))
1542 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
137443ea 1543 {
2ae324a7
PP
1544 SV *sv;
1545
1546 PUSHMARK(MARK-1);
5b468f54 1547 *MARK = SvTIED_obj((SV*)io, mg);
2ae324a7 1548 ENTER;
864dbfa3 1549 call_method("READ", G_SCALAR);
2ae324a7
PP
1550 LEAVE;
1551 SPAGAIN;
1552 sv = POPs;
1553 SP = ORIGMARK;
1554 PUSHs(sv);
1555 RETURN;
1556 }
1557
a0d0e21e
LW
1558 if (!gv)
1559 goto say_undef;
748a9306 1560 bufsv = *++MARK;
ff68c719
PP
1561 if (! SvOK(bufsv))
1562 sv_setpvn(bufsv, "", 0);
a0d0e21e 1563 length = SvIVx(*++MARK);
748a9306 1564 SETERRNO(0,0);
a0d0e21e
LW
1565 if (MARK < SP)
1566 offset = SvIVx(*++MARK);
1567 else
1568 offset = 0;
1569 io = GvIO(gv);
b5fe5ca2
SR
1570 if (!io || !IoIFP(io)) {
1571 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1572 report_evil_fh(gv, io, PL_op->op_type);
1573 SETERRNO(EBADF,RMS_IFI);
a0d0e21e 1574 goto say_undef;
b5fe5ca2 1575 }
0064a8a9 1576 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
7d59b7e4 1577 buffer = SvPVutf8_force(bufsv, blen);
eb5c063a
NIS
1578 /* UTF8 may not have been set if they are all low bytes */
1579 SvUTF8_on(bufsv);
7d59b7e4
NIS
1580 }
1581 else {
1582 buffer = SvPV_force(bufsv, blen);
1583 }
1584 if (length < 0)
1585 DIE(aTHX_ "Negative length");
eb5c063a 1586 wanted = length;
7d59b7e4 1587
d0965105
JH
1588 charstart = TRUE;
1589 charskip = 0;
87330c3c 1590 skip = 0;
d0965105 1591
a0d0e21e 1592#ifdef HAS_SOCKET
533c011a 1593 if (PL_op->op_type == OP_RECV) {
46fc3d4c 1594 char namebuf[MAXPATHLEN];
17a8c7ba 1595#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
490ab354
JH
1596 bufsize = sizeof (struct sockaddr_in);
1597#else
46fc3d4c 1598 bufsize = sizeof namebuf;
490ab354 1599#endif
abf95952
IZ
1600#ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1601 if (bufsize >= 256)
1602 bufsize = 255;
1603#endif
eb160463 1604 buffer = SvGROW(bufsv, (STRLEN)(length+1));
bbce6d69 1605 /* 'offset' means 'flags' here */
eb5c063a 1606 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
46fc3d4c 1607 (struct sockaddr *)namebuf, &bufsize);
eb5c063a 1608 if (count < 0)
a0d0e21e 1609 RETPUSHUNDEF;
4107cc59
OF
1610#ifdef EPOC
1611 /* Bogus return without padding */
1612 bufsize = sizeof (struct sockaddr_in);
1613#endif
eb5c063a 1614 SvCUR_set(bufsv, count);
748a9306
LW
1615 *SvEND(bufsv) = '\0';
1616 (void)SvPOK_only(bufsv);
eb5c063a
NIS
1617 if (fp_utf8)
1618 SvUTF8_on(bufsv);
748a9306 1619 SvSETMAGIC(bufsv);
aac0dd9a 1620 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1621 if (!(IoFLAGS(io) & IOf_UNTAINT))
1622 SvTAINTED_on(bufsv);
a0d0e21e 1623 SP = ORIGMARK;
46fc3d4c 1624 sv_setpvn(TARG, namebuf, bufsize);
a0d0e21e
LW
1625 PUSHs(TARG);
1626 RETURN;
1627 }
1628#else
911d147d 1629 if (PL_op->op_type == OP_RECV)
cea2e8a9 1630 DIE(aTHX_ PL_no_sock_func, "recv");
a0d0e21e 1631#endif
eb5c063a
NIS
1632 if (DO_UTF8(bufsv)) {
1633 /* offset adjust in characters not bytes */
1634 blen = sv_len_utf8(bufsv);
7d59b7e4 1635 }
bbce6d69 1636 if (offset < 0) {
eb160463 1637 if (-offset > (int)blen)
cea2e8a9 1638 DIE(aTHX_ "Offset outside string");
bbce6d69
PP
1639 offset += blen;
1640 }
eb5c063a
NIS
1641 if (DO_UTF8(bufsv)) {
1642 /* convert offset-as-chars to offset-as-bytes */
1643 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1644 }
1645 more_bytes:
cd52b7b2 1646 bufsize = SvCUR(bufsv);
eb160463 1647 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
cd52b7b2
PP
1648 if (offset > bufsize) { /* Zero any newly allocated space */
1649 Zero(buffer+bufsize, offset-bufsize, char);
1650 }
eb5c063a
NIS
1651 buffer = buffer + offset;
1652
533c011a 1653 if (PL_op->op_type == OP_SYSREAD) {
a7092146 1654#ifdef PERL_SOCK_SYSREAD_IS_RECV
50952442 1655 if (IoTYPE(io) == IoTYPE_SOCKET) {
eb5c063a
NIS
1656 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1657 buffer, length, 0);
a7092146
GS
1658 }
1659 else
1660#endif
1661 {
eb5c063a
NIS
1662 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1663 buffer, length);
a7092146 1664 }
a0d0e21e
LW
1665 }
1666 else
1667#ifdef HAS_SOCKET__bad_code_maybe
50952442 1668 if (IoTYPE(io) == IoTYPE_SOCKET) {
46fc3d4c 1669 char namebuf[MAXPATHLEN];
490ab354
JH
1670#if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1671 bufsize = sizeof (struct sockaddr_in);
1672#else
46fc3d4c 1673 bufsize = sizeof namebuf;
490ab354 1674#endif
eb5c063a 1675 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
46fc3d4c 1676 (struct sockaddr *)namebuf, &bufsize);
a0d0e21e
LW
1677 }
1678 else
1679#endif
3b02c43c 1680 {
eb5c063a
NIS
1681 count = PerlIO_read(IoIFP(io), buffer, length);
1682 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1683 if (count == 0 && PerlIO_error(IoIFP(io)))
1684 count = -1;
3b02c43c 1685 }
eb5c063a 1686 if (count < 0) {
a00b5bd3 1687 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
6e6ef6b2 1688 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
a0d0e21e 1689 goto say_undef;
af8c498a 1690 }
eb5c063a 1691 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
748a9306
LW
1692 *SvEND(bufsv) = '\0';
1693 (void)SvPOK_only(bufsv);
0064a8a9 1694 if (fp_utf8 && !IN_BYTES) {
eb5c063a
NIS
1695 /* Look at utf8 we got back and count the characters */
1696 char *bend = buffer + count;
1697 while (buffer < bend) {
d0965105
JH
1698 if (charstart) {
1699 skip = UTF8SKIP(buffer);
1700 charskip = 0;
1701 }
1702 if (buffer - charskip + skip > bend) {
eb5c063a
NIS
1703 /* partial character - try for rest of it */
1704 length = skip - (bend-buffer);
1705 offset = bend - SvPVX(bufsv);
d0965105
JH
1706 charstart = FALSE;
1707 charskip += count;
eb5c063a
NIS
1708 goto more_bytes;
1709 }
1710 else {
1711 got++;
1712 buffer += skip;
d0965105
JH
1713 charstart = TRUE;
1714 charskip = 0;
eb5c063a
NIS
1715 }
1716 }
1717 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1718 provided amount read (count) was what was requested (length)
1719 */
1720 if (got < wanted && count == length) {
d0965105 1721 length = wanted - got;
eb5c063a
NIS
1722 offset = bend - SvPVX(bufsv);
1723 goto more_bytes;
1724 }
1725 /* return value is character count */
1726 count = got;
1727 SvUTF8_on(bufsv);
1728 }
748a9306 1729 SvSETMAGIC(bufsv);
aac0dd9a 1730 /* This should not be marked tainted if the fp is marked clean */
bbce6d69
PP
1731 if (!(IoFLAGS(io) & IOf_UNTAINT))
1732 SvTAINTED_on(bufsv);
a0d0e21e 1733 SP = ORIGMARK;
eb5c063a 1734 PUSHi(count);
a0d0e21e
LW
1735 RETURN;
1736
1737 say_undef:
1738 SP = ORIGMARK;
1739 RETPUSHUNDEF;
1740}
1741
1742PP(pp_syswrite)
1743{
39644a26 1744 dSP;
092bebab
JH
1745 int items = (SP - PL_stack_base) - TOPMARK;
1746 if (items == 2) {
9f089d78 1747 SV *sv;
092bebab 1748 EXTEND(SP, 1);
9f089d78
SB
1749 sv = sv_2mortal(newSViv(sv_len(*SP)));
1750 PUSHs(sv);
092bebab
JH
1751 PUTBACK;
1752 }
cea2e8a9 1753 return pp_send();
a0d0e21e
LW
1754}
1755
1756PP(pp_send)
1757{
39644a26 1758 dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
1759 GV *gv;
1760 IO *io;
748a9306 1761 SV *bufsv;
a0d0e21e 1762 char *buffer;
8c99d73e
GS
1763 Size_t length;
1764 SSize_t retval;
a0d0e21e 1765 STRLEN blen;
1d603a67 1766 MAGIC *mg;
a0d0e21e
LW
1767
1768 gv = (GV*)*++MARK;
14befaf4 1769 if (PL_op->op_type == OP_SYSWRITE
5b468f54
AMS
1770 && gv && (io = GvIO(gv))
1771 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
14befaf4 1772 {
1d603a67
GB
1773 SV *sv;
1774
1775 PUSHMARK(MARK-1);
5b468f54 1776 *MARK = SvTIED_obj((SV*)io, mg);
1d603a67 1777 ENTER;
864dbfa3 1778 call_method("WRITE", G_SCALAR);
1d603a67
GB
1779 LEAVE;
1780 SPAGAIN;
1781 sv = POPs;
1782 SP = ORIGMARK;
1783 PUSHs(sv);
1784 RETURN;
1785 }
a0d0e21e
LW
1786 if (!gv)
1787 goto say_undef;
748a9306 1788 bufsv = *++MARK;
8c99d73e 1789#if Size_t_size > IVSIZE
3c001241 1790 length = (Size_t)SvNVx(*++MARK);
146174a9 1791#else
3c001241 1792 length = (Size_t)SvIVx(*++MARK);
146174a9 1793#endif
3c001241 1794 if ((SSize_t)length < 0)
cea2e8a9 1795 DIE(aTHX_ "Negative length");
748a9306 1796 SETERRNO(0,0);
a0d0e21e
LW
1797 io = GvIO(gv);
1798 if (!io || !IoIFP(io)) {
8c99d73e 1799 retval = -1;
bc37a18f
RG
1800 if (ckWARN(WARN_CLOSED))
1801 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 1802 SETERRNO(EBADF,RMS_IFI);
7d59b7e4
NIS
1803 goto say_undef;
1804 }
1805
1806 if (PerlIO_isutf8(IoIFP(io))) {
1807 buffer = SvPVutf8(bufsv, blen);
a0d0e21e 1808 }
7d59b7e4 1809 else {
4de5f5ca
JH
1810 if (DO_UTF8(bufsv)) {
1811 /* Not modifying source SV, so making a temporary copy. */
1812 bufsv = sv_2mortal(newSVsv(bufsv));
1813 sv_utf8_downgrade(bufsv, FALSE);
1814 }
1815 buffer = SvPV(bufsv, blen);
7d59b7e4
NIS
1816 }
1817
1818 if (PL_op->op_type == OP_SYSWRITE) {
1819 IV offset;
1820 if (DO_UTF8(bufsv)) {
1821 /* length and offset are in chars */
1822 blen = sv_len_utf8(bufsv);
1823 }
bbce6d69 1824 if (MARK < SP) {
a0d0e21e 1825 offset = SvIVx(*++MARK);
bbce6d69 1826 if (offset < 0) {
eb160463 1827 if (-offset > (IV)blen)
cea2e8a9 1828 DIE(aTHX_ "Offset outside string");
bbce6d69 1829 offset += blen;
eb160463 1830 } else if (offset >= (IV)blen && blen > 0)
cea2e8a9 1831 DIE(aTHX_ "Offset outside string");
bbce6d69 1832 } else
a0d0e21e
LW
1833 offset = 0;
1834 if (length > blen - offset)
1835 length = blen - offset;
7d59b7e4 1836 if (DO_UTF8(bufsv)) {
c8d31a35 1837 buffer = (char*)utf8_hop((U8 *)buffer, offset);
7d59b7e4
NIS
1838 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1839 }
1840 else {
1841 buffer = buffer+offset;
1842 }
a7092146 1843#ifdef PERL_SOCK_SYSWRITE_IS_SEND
50952442 1844 if (IoTYPE(io) == IoTYPE_SOCKET) {
8c99d73e 1845 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1846 buffer, length, 0);
a7092146
GS
1847 }
1848 else
1849#endif
1850 {
94e4c244 1851 /* See the note at doio.c:do_print about filesize limits. --jhi */
8c99d73e 1852 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1853 buffer, length);
a7092146 1854 }
a0d0e21e
LW
1855 }
1856#ifdef HAS_SOCKET
1857 else if (SP > MARK) {
1858 char *sockbuf;
1859 STRLEN mlen;
1860 sockbuf = SvPVx(*++MARK, mlen);
7d59b7e4 1861 /* length is really flags */
8c99d73e
GS
1862 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1863 length, (struct sockaddr *)sockbuf, mlen);
a0d0e21e
LW
1864 }
1865 else
7d59b7e4 1866 /* length is really flags */
8c99d73e 1867 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
a0d0e21e
LW
1868#else
1869 else
cea2e8a9 1870 DIE(aTHX_ PL_no_sock_func, "send");
a0d0e21e 1871#endif
8c99d73e 1872 if (retval < 0)
a0d0e21e
LW
1873 goto say_undef;
1874 SP = ORIGMARK;
f36eea10
JH
1875 if (DO_UTF8(bufsv))
1876 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
8c99d73e
GS
1877#if Size_t_size > IVSIZE
1878 PUSHn(retval);
1879#else
1880 PUSHi(retval);
1881#endif
a0d0e21e
LW
1882 RETURN;
1883
1884 say_undef:
1885 SP = ORIGMARK;
1886 RETPUSHUNDEF;
1887}
1888
1889PP(pp_recv)
1890{
cea2e8a9 1891 return pp_sysread();
a0d0e21e
LW
1892}
1893
1894PP(pp_eof)
1895{
39644a26 1896 dSP;
a0d0e21e 1897 GV *gv;
5b468f54 1898 IO *io;
4592e6ca 1899 MAGIC *mg;
a0d0e21e 1900
32da55ab 1901 if (MAXARG == 0) {
146174a9
CB
1902 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1903 IO *io;
ed2c6b9b 1904 gv = PL_last_in_gv = GvEGV(PL_argvgv);
146174a9
CB
1905 io = GvIO(gv);
1906 if (io && !IoIFP(io)) {
1907 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1908 IoLINES(io) = 0;
1909 IoFLAGS(io) &= ~IOf_START;
1910 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1911 sv_setpvn(GvSV(gv), "-", 1);
1912 SvSETMAGIC(GvSV(gv));
1913 }
1914 else if (!nextargv(gv))
1915 RETPUSHYES;
1916 }
1917 }
1918 else
1919 gv = PL_last_in_gv; /* eof */
1920 }
a0d0e21e 1921 else
146174a9 1922 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
4592e6ca 1923
5b468f54
AMS
1924 if (gv && (io = GvIO(gv))
1925 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1926 {
4592e6ca 1927 PUSHMARK(SP);
5b468f54 1928 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
1929 PUTBACK;
1930 ENTER;
864dbfa3 1931 call_method("EOF", G_SCALAR);
4592e6ca
NIS
1932 LEAVE;
1933 SPAGAIN;
1934 RETURN;
1935 }
1936
54310121 1937 PUSHs(boolSV(!gv || do_eof(gv)));
a0d0e21e
LW
1938 RETURN;
1939}
1940
1941PP(pp_tell)
1942{
39644a26 1943 dSP; dTARGET;
301e8125 1944 GV *gv;
5b468f54 1945 IO *io;
4592e6ca 1946 MAGIC *mg;
a0d0e21e 1947
32da55ab 1948 if (MAXARG == 0)
3280af22 1949 gv = PL_last_in_gv;
a0d0e21e 1950 else
3280af22 1951 gv = PL_last_in_gv = (GV*)POPs;
4592e6ca 1952
5b468f54
AMS
1953 if (gv && (io = GvIO(gv))
1954 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1955 {
4592e6ca 1956 PUSHMARK(SP);
5b468f54 1957 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
1958 PUTBACK;
1959 ENTER;
864dbfa3 1960 call_method("TELL", G_SCALAR);
4592e6ca
NIS
1961 LEAVE;
1962 SPAGAIN;
1963 RETURN;
1964 }
1965
146174a9
CB
1966#if LSEEKSIZE > IVSIZE
1967 PUSHn( do_tell(gv) );
1968#else
a0d0e21e 1969 PUSHi( do_tell(gv) );
146174a9 1970#endif
a0d0e21e
LW
1971 RETURN;
1972}
1973
1974PP(pp_seek)
1975{
cea2e8a9 1976 return pp_sysseek();
137443ea
PP
1977}
1978
1979PP(pp_sysseek)
1980{
39644a26 1981 dSP;
a0d0e21e 1982 GV *gv;
5b468f54 1983 IO *io;
a0d0e21e 1984 int whence = POPi;
146174a9
CB
1985#if LSEEKSIZE > IVSIZE
1986 Off_t offset = (Off_t)SvNVx(POPs);
1987#else
d9b3e12d 1988 Off_t offset = (Off_t)SvIVx(POPs);
146174a9 1989#endif
4592e6ca 1990 MAGIC *mg;
a0d0e21e 1991
3280af22 1992 gv = PL_last_in_gv = (GV*)POPs;
4592e6ca 1993
5b468f54
AMS
1994 if (gv && (io = GvIO(gv))
1995 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1996 {
4592e6ca 1997 PUSHMARK(SP);
5b468f54 1998 XPUSHs(SvTIED_obj((SV*)io, mg));
cb50131a
CB
1999#if LSEEKSIZE > IVSIZE
2000 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2001#else
b448e4fe 2002 XPUSHs(sv_2mortal(newSViv(offset)));
cb50131a 2003#endif
b448e4fe 2004 XPUSHs(sv_2mortal(newSViv(whence)));
4592e6ca
NIS
2005 PUTBACK;
2006 ENTER;
864dbfa3 2007 call_method("SEEK", G_SCALAR);
4592e6ca
NIS
2008 LEAVE;
2009 SPAGAIN;
2010 RETURN;
2011 }
2012
533c011a 2013 if (PL_op->op_type == OP_SEEK)
8903cb82
PP
2014 PUSHs(boolSV(do_seek(gv, offset, whence)));
2015 else {
b448e4fe
JH
2016 Off_t sought = do_sysseek(gv, offset, whence);
2017 if (sought < 0)
146174a9
CB
2018 PUSHs(&PL_sv_undef);
2019 else {
b448e4fe 2020 SV* sv = sought ?
146174a9 2021#if LSEEKSIZE > IVSIZE
b448e4fe 2022 newSVnv((NV)sought)
146174a9 2023#else
b448e4fe 2024 newSViv(sought)
146174a9
CB
2025#endif
2026 : newSVpvn(zero_but_true, ZBTLEN);
2027 PUSHs(sv_2mortal(sv));
2028 }
8903cb82 2029 }
a0d0e21e
LW
2030 RETURN;
2031}
2032
2033PP(pp_truncate)
2034{
39644a26 2035 dSP;
8c99d73e
GS
2036 /* There seems to be no consensus on the length type of truncate()
2037 * and ftruncate(), both off_t and size_t have supporters. In
2038 * general one would think that when using large files, off_t is
2039 * at least as wide as size_t, so using an off_t should be okay. */
2040 /* XXX Configure probe for the length type of *truncate() needed XXX */
2041 Off_t len;
a0d0e21e 2042
25342a55 2043#if Off_t_size > IVSIZE
8c99d73e
GS
2044 len = (Off_t)POPn;
2045#else
2046 len = (Off_t)POPi;
2047#endif
2048 /* Checking for length < 0 is problematic as the type might or
301e8125 2049 * might not be signed: if it is not, clever compilers will moan. */
8c99d73e 2050 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
748a9306 2051 SETERRNO(0,0);
5d94fbed 2052#if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
d05c1ba0
JH
2053 {
2054 STRLEN n_a;
2055 int result = 1;
2056 GV *tmpgv;
090bf15b
SR
2057 IO *io;
2058
d05c1ba0
JH
2059 if (PL_op->op_flags & OPf_SPECIAL) {
2060 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2061
090bf15b
SR
2062 do_ftruncate_gv:
2063 if (!GvIO(tmpgv))
2064 result = 0;
d05c1ba0 2065 else {
090bf15b
SR
2066 PerlIO *fp;
2067 io = GvIOp(tmpgv);
2068 do_ftruncate_io:
2069 TAINT_PROPER("truncate");
2070 if (!(fp = IoIFP(io))) {
2071 result = 0;
2072 }
2073 else {
2074 PerlIO_flush(fp);
cbdc8872 2075#ifdef HAS_TRUNCATE
090bf15b 2076 if (ftruncate(PerlIO_fileno(fp), len) < 0)
301e8125 2077#else
090bf15b 2078 if (my_chsize(PerlIO_fileno(fp), len) < 0)
cbdc8872 2079#endif
090bf15b
SR
2080 result = 0;
2081 }
d05c1ba0 2082 }
cbdc8872 2083 }
d05c1ba0
JH
2084 else {
2085 SV *sv = POPs;
2086 char *name;
72f496dc 2087
d05c1ba0
JH
2088 if (SvTYPE(sv) == SVt_PVGV) {
2089 tmpgv = (GV*)sv; /* *main::FRED for example */
090bf15b 2090 goto do_ftruncate_gv;
d05c1ba0
JH
2091 }
2092 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2093 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
090bf15b
SR
2094 goto do_ftruncate_gv;
2095 }
2096 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2097 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2098 goto do_ftruncate_io;
d05c1ba0 2099 }
1e422769 2100
d05c1ba0
JH
2101 name = SvPV(sv, n_a);
2102 TAINT_PROPER("truncate");
cbdc8872 2103#ifdef HAS_TRUNCATE
d05c1ba0
JH
2104 if (truncate(name, len) < 0)
2105 result = 0;
cbdc8872 2106#else
d05c1ba0
JH
2107 {
2108 int tmpfd;
2109
2110 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
cbdc8872 2111 result = 0;
d05c1ba0
JH
2112 else {
2113 if (my_chsize(tmpfd, len) < 0)
2114 result = 0;
2115 PerlLIO_close(tmpfd);
2116 }
cbdc8872 2117 }
a0d0e21e 2118#endif
d05c1ba0 2119 }
a0d0e21e 2120
d05c1ba0
JH
2121 if (result)
2122 RETPUSHYES;
2123 if (!errno)
93189314 2124 SETERRNO(EBADF,RMS_IFI);
d05c1ba0
JH
2125 RETPUSHUNDEF;
2126 }
a0d0e21e 2127#else
cea2e8a9 2128 DIE(aTHX_ "truncate not implemented");
a0d0e21e
LW
2129#endif
2130}
2131
2132PP(pp_fcntl)
2133{
cea2e8a9 2134 return pp_ioctl();
a0d0e21e
LW
2135}
2136
2137PP(pp_ioctl)
2138{
39644a26 2139 dSP; dTARGET;
748a9306 2140 SV *argsv = POPs;
3bb7c1b4 2141 unsigned int func = POPu;
533c011a 2142 int optype = PL_op->op_type;
a0d0e21e 2143 char *s;
324aa91a 2144 IV retval;
a0d0e21e 2145 GV *gv = (GV*)POPs;
c289d2f7 2146 IO *io = gv ? GvIOn(gv) : 0;
a0d0e21e 2147
748a9306 2148 if (!io || !argsv || !IoIFP(io)) {
c289d2f7
JH
2149 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2150 report_evil_fh(gv, io, PL_op->op_type);
93189314 2151 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
a0d0e21e
LW
2152 RETPUSHUNDEF;
2153 }
2154
748a9306 2155 if (SvPOK(argsv) || !SvNIOK(argsv)) {
a0d0e21e 2156 STRLEN len;
324aa91a 2157 STRLEN need;
748a9306 2158 s = SvPV_force(argsv, len);
324aa91a
HF
2159 need = IOCPARM_LEN(func);
2160 if (len < need) {
2161 s = Sv_Grow(argsv, need + 1);
2162 SvCUR_set(argsv, need);
a0d0e21e
LW
2163 }
2164
748a9306 2165 s[SvCUR(argsv)] = 17; /* a little sanity check here */
a0d0e21e
LW
2166 }
2167 else {
748a9306 2168 retval = SvIV(argsv);
c529f79d 2169 s = INT2PTR(char*,retval); /* ouch */
a0d0e21e
LW
2170 }
2171
2172 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2173
2174 if (optype == OP_IOCTL)
2175#ifdef HAS_IOCTL
76e3520e 2176 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
a0d0e21e 2177#else
cea2e8a9 2178 DIE(aTHX_ "ioctl is not implemented");
a0d0e21e
LW
2179#endif
2180 else
c214f4ad
B
2181#ifndef HAS_FCNTL
2182 DIE(aTHX_ "fcntl is not implemented");
2183#else
55497cff 2184#if defined(OS2) && defined(__EMX__)
760ac839 2185 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
55497cff 2186#else
760ac839 2187 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
301e8125 2188#endif
6652bd42 2189#endif
a0d0e21e 2190
6652bd42 2191#if defined(HAS_IOCTL) || defined(HAS_FCNTL)
748a9306
LW
2192 if (SvPOK(argsv)) {
2193 if (s[SvCUR(argsv)] != 17)
cea2e8a9 2194 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
53e06cf0 2195 OP_NAME(PL_op));
748a9306
LW
2196 s[SvCUR(argsv)] = 0; /* put our null back */
2197 SvSETMAGIC(argsv); /* Assume it has changed */
a0d0e21e
LW
2198 }
2199
2200 if (retval == -1)
2201 RETPUSHUNDEF;
2202 if (retval != 0) {
2203 PUSHi(retval);
2204 }
2205 else {
8903cb82 2206 PUSHp(zero_but_true, ZBTLEN);
a0d0e21e 2207 }
4808266b 2208#endif
c214f4ad 2209 RETURN;
a0d0e21e
LW
2210}
2211
2212PP(pp_flock)
2213{
9cad6237 2214#ifdef FLOCK
39644a26 2215 dSP; dTARGET;
a0d0e21e
LW
2216 I32 value;
2217 int argtype;
2218 GV *gv;
bc37a18f 2219 IO *io = NULL;
760ac839 2220 PerlIO *fp;
16d20bd9 2221
a0d0e21e 2222 argtype = POPi;
32da55ab 2223 if (MAXARG == 0)
3280af22 2224 gv = PL_last_in_gv;
a0d0e21e
LW
2225 else
2226 gv = (GV*)POPs;
bc37a18f
RG
2227 if (gv && (io = GvIO(gv)))
2228 fp = IoIFP(io);
2229 else {
a0d0e21e 2230 fp = Nullfp;
bc37a18f
RG
2231 io = NULL;
2232 }
a0d0e21e 2233 if (fp) {
68dc0745 2234 (void)PerlIO_flush(fp);
76e3520e 2235 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
a0d0e21e 2236 }
cb50131a 2237 else {
bc37a18f
RG
2238 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2239 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 2240 value = 0;
93189314 2241 SETERRNO(EBADF,RMS_IFI);
cb50131a 2242 }
a0d0e21e
LW
2243 PUSHi(value);
2244 RETURN;
2245#else
cea2e8a9 2246 DIE(aTHX_ PL_no_func, "flock()");
a0d0e21e
LW
2247#endif
2248}
2249
2250/* Sockets. */
2251
2252PP(pp_socket)
2253{
a0d0e21e 2254#ifdef HAS_SOCKET
9cad6237 2255 dSP;
a0d0e21e
LW
2256 GV *gv;
2257 register IO *io;
2258 int protocol = POPi;
2259 int type = POPi;
2260 int domain = POPi;
2261 int fd;
2262
2263 gv = (GV*)POPs;
c289d2f7 2264 io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2265
c289d2f7
JH
2266 if (!gv || !io) {
2267 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2268 report_evil_fh(gv, io, PL_op->op_type);
2269 if (IoIFP(io))
2270 do_close(gv, FALSE);
93189314 2271 SETERRNO(EBADF,LIB_INVARG);
a0d0e21e
LW
2272 RETPUSHUNDEF;
2273 }
2274
57171420
BS
2275 if (IoIFP(io))
2276 do_close(gv, FALSE);
2277
a0d0e21e 2278 TAINT_PROPER("socket");
6ad3d225 2279 fd = PerlSock_socket(domain, type, protocol);
a0d0e21e
LW
2280 if (fd < 0)
2281 RETPUSHUNDEF;
479b2847
GS
2282 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2283 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
50952442 2284 IoTYPE(io) = IoTYPE_SOCKET;
a0d0e21e 2285 if (!IoIFP(io) || !IoOFP(io)) {
760ac839
LW
2286 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2287 if (IoOFP(io)) PerlIO_close(IoOFP(io));
6ad3d225 2288 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
a0d0e21e
LW
2289 RETPUSHUNDEF;
2290 }
8d2a6795
GS
2291#if defined(HAS_FCNTL) && defined(F_SETFD)
2292 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2293#endif
a0d0e21e 2294
d5ff79b3
OF
2295#ifdef EPOC
2296 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2297#endif
2298
a0d0e21e
LW
2299 RETPUSHYES;
2300#else
cea2e8a9 2301 DIE(aTHX_ PL_no_sock_func, "socket");
a0d0e21e
LW
2302#endif
2303}
2304
2305PP(pp_sockpair)
2306{
c95c94b1 2307#if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
76ffd3b9 2308 dSP;
a0d0e21e
LW
2309 GV *gv1;
2310 GV *gv2;
2311 register IO *io1;
2312 register IO *io2;
2313 int protocol = POPi;
2314 int type = POPi;
2315 int domain = POPi;
2316 int fd[2];
2317
2318 gv2 = (GV*)POPs;
2319 gv1 = (GV*)POPs;
c289d2f7
JH
2320 io1 = gv1 ? GvIOn(gv1) : NULL;
2321 io2 = gv2 ? GvIOn(gv2) : NULL;
2322 if (!gv1 || !gv2 || !io1 || !io2) {
2323 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2324 if (!gv1 || !io1)
2325 report_evil_fh(gv1, io1, PL_op->op_type);
2326 if (!gv2 || !io2)
2327 report_evil_fh(gv1, io2, PL_op->op_type);
2328 }
2329 if (IoIFP(io1))
2330 do_close(gv1, FALSE);
2331 if (IoIFP(io2))
2332 do_close(gv2, FALSE);
a0d0e21e 2333 RETPUSHUNDEF;
c289d2f7 2334 }
a0d0e21e 2335
dc0d0a5f
JH
2336 if (IoIFP(io1))
2337 do_close(gv1, FALSE);
2338 if (IoIFP(io2))
2339 do_close(gv2, FALSE);
57171420 2340
a0d0e21e 2341 TAINT_PROPER("socketpair");
6ad3d225 2342 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
a0d0e21e 2343 RETPUSHUNDEF;
479b2847
GS
2344 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2345 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
50952442 2346 IoTYPE(io1) = IoTYPE_SOCKET;
479b2847
GS
2347 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2348 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
50952442 2349 IoTYPE(io2) = IoTYPE_SOCKET;
a0d0e21e 2350 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
760ac839
LW
2351 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2352 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
6ad3d225 2353 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
760ac839
LW
2354 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2355 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
6ad3d225 2356 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
a0d0e21e
LW
2357 RETPUSHUNDEF;
2358 }
8d2a6795
GS
2359#if defined(HAS_FCNTL) && defined(F_SETFD)
2360 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2361 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2362#endif
a0d0e21e
LW
2363
2364 RETPUSHYES;
2365#else
cea2e8a9 2366 DIE(aTHX_ PL_no_sock_func, "socketpair");
a0d0e21e
LW
2367#endif
2368}
2369
2370PP(pp_bind)
2371{
a0d0e21e 2372#ifdef HAS_SOCKET
9cad6237 2373 dSP;
eec2d3df 2374#ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
82b3da69
JH
2375 extern void GETPRIVMODE();
2376 extern void GETUSERMODE();
eec2d3df 2377#endif
748a9306 2378 SV *addrsv = POPs;
a0d0e21e
LW
2379 char *addr;
2380 GV *gv = (GV*)POPs;
2381 register IO *io = GvIOn(gv);
2382 STRLEN len;
eec2d3df
GS
2383 int bind_ok = 0;
2384#ifdef MPE
2385 int mpeprivmode = 0;
2386#endif
a0d0e21e
LW
2387
2388 if (!io || !IoIFP(io))
2389 goto nuts;
2390
748a9306 2391 addr = SvPV(addrsv, len);
a0d0e21e 2392 TAINT_PROPER("bind");
eec2d3df
GS
2393#ifdef MPE /* Deal with MPE bind() peculiarities */
2394 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2395 /* The address *MUST* stupidly be zero. */
2396 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2397 /* PRIV mode is required to bind() to ports < 1024. */
2398 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2399 ((struct sockaddr_in *)addr)->sin_port > 0) {
2400 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2401 mpeprivmode = 1;
2402 }
2403 }
2404#endif /* MPE */
2405 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2406 (struct sockaddr *)addr, len) >= 0)
2407 bind_ok = 1;
2408
2409#ifdef MPE /* Switch back to USER mode */
2410 if (mpeprivmode)
2411 GETUSERMODE();
2412#endif /* MPE */
2413
2414 if (bind_ok)
a0d0e21e
LW
2415 RETPUSHYES;
2416 else
2417 RETPUSHUNDEF;
2418
2419nuts:
599cee73 2420 if (ckWARN(WARN_CLOSED))
bc37a18f 2421 report_evil_fh(gv, io, PL_op->op_type);
93189314 2422 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2423 RETPUSHUNDEF;
2424#else
cea2e8a9 2425 DIE(aTHX_ PL_no_sock_func, "bind");
a0d0e21e
LW
2426#endif
2427}
2428
2429PP(pp_connect)
2430{
a0d0e21e 2431#ifdef HAS_SOCKET
9cad6237 2432 dSP;
748a9306 2433 SV *addrsv = POPs;
a0d0e21e
LW
2434 char *addr;
2435 GV *gv = (GV*)POPs;
2436 register IO *io = GvIOn(gv);
2437 STRLEN len;
2438
2439 if (!io || !IoIFP(io))
2440 goto nuts;
2441
748a9306 2442 addr = SvPV(addrsv, len);
a0d0e21e 2443 TAINT_PROPER("connect");
6ad3d225 2444 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
a0d0e21e
LW
2445 RETPUSHYES;
2446 else
2447 RETPUSHUNDEF;
2448
2449nuts:
599cee73 2450 if (ckWARN(WARN_CLOSED))
bc37a18f 2451 report_evil_fh(gv, io, PL_op->op_type);
93189314 2452 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2453 RETPUSHUNDEF;
2454#else
cea2e8a9 2455 DIE(aTHX_ PL_no_sock_func, "connect");
a0d0e21e
LW
2456#endif
2457}
2458
2459PP(pp_listen)
2460{
a0d0e21e 2461#ifdef HAS_SOCKET
9cad6237 2462 dSP;
a0d0e21e
LW
2463 int backlog = POPi;
2464 GV *gv = (GV*)POPs;
c289d2f7 2465 register IO *io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2466
c289d2f7 2467 if (!gv || !io || !IoIFP(io))
a0d0e21e
LW
2468 goto nuts;
2469
6ad3d225 2470 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
a0d0e21e
LW
2471 RETPUSHYES;
2472 else
2473 RETPUSHUNDEF;
2474
2475nuts:
599cee73 2476 if (ckWARN(WARN_CLOSED))
bc37a18f 2477 report_evil_fh(gv, io, PL_op->op_type);
93189314 2478 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2479 RETPUSHUNDEF;
2480#else
cea2e8a9 2481 DIE(aTHX_ PL_no_sock_func, "listen");
a0d0e21e
LW
2482#endif
2483}
2484
2485PP(pp_accept)
2486{
a0d0e21e 2487#ifdef HAS_SOCKET
9cad6237 2488 dSP; dTARGET;
a0d0e21e
LW
2489 GV *ngv;
2490 GV *ggv;
2491 register IO *nstio;
2492 register IO *gstio;
93d47a36
JH
2493 char namebuf[MAXPATHLEN];
2494#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2495 Sock_size_t len = sizeof (struct sockaddr_in);
2496#else
2497 Sock_size_t len = sizeof namebuf;
2498#endif
a0d0e21e
LW
2499 int fd;
2500
2501 ggv = (GV*)POPs;
2502 ngv = (GV*)POPs;
2503
2504 if (!ngv)
2505 goto badexit;
2506 if (!ggv)
2507 goto nuts;
2508
2509 gstio = GvIO(ggv);
2510 if (!gstio || !IoIFP(gstio))
2511 goto nuts;
2512
2513 nstio = GvIOn(ngv);
93d47a36 2514 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
a0d0e21e
LW
2515 if (fd < 0)
2516 goto badexit;
a70048fb
AB
2517 if (IoIFP(nstio))
2518 do_close(ngv, FALSE);
479b2847 2519 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
7e56aaa7 2520 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
50952442 2521 IoTYPE(nstio) = IoTYPE_SOCKET;
a0d0e21e 2522 if (!IoIFP(nstio) || !IoOFP(nstio)) {
760ac839
LW
2523 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2524 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
6ad3d225 2525 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
a0d0e21e
LW
2526 goto badexit;
2527 }
8d2a6795
GS
2528#if defined(HAS_FCNTL) && defined(F_SETFD)
2529 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2530#endif
a0d0e21e 2531
ed79a026 2532#ifdef EPOC
93d47a36 2533 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
a9f1f6b0 2534 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
ed79a026 2535#endif
381c1bae 2536#ifdef __SCO_VERSION__
93d47a36 2537 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
381c1bae 2538#endif
ed79a026 2539
93d47a36 2540 PUSHp(namebuf, len);
a0d0e21e
LW
2541 RETURN;
2542
2543nuts:
599cee73 2544 if (ckWARN(WARN_CLOSED))
bc37a18f 2545 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
93189314 2546 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2547
2548badexit:
2549 RETPUSHUNDEF;
2550
2551#else
cea2e8a9 2552 DIE(aTHX_ PL_no_sock_func, "accept");
a0d0e21e
LW
2553#endif
2554}
2555
2556PP(pp_shutdown)
2557{
a0d0e21e 2558#ifdef HAS_SOCKET
9cad6237 2559 dSP; dTARGET;
a0d0e21e
LW
2560 int how = POPi;
2561 GV *gv = (GV*)POPs;
2562 register IO *io = GvIOn(gv);
2563
2564 if (!io || !IoIFP(io))
2565 goto nuts;
2566
6ad3d225 2567 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
a0d0e21e
LW
2568 RETURN;
2569
2570nuts:
599cee73 2571 if (ckWARN(WARN_CLOSED))
bc37a18f 2572 report_evil_fh(gv, io, PL_op->op_type);
93189314 2573 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2574 RETPUSHUNDEF;
2575#else
cea2e8a9 2576 DIE(aTHX_ PL_no_sock_func, "shutdown");
a0d0e21e
LW
2577#endif
2578}
2579
2580PP(pp_gsockopt)
2581{
2582#ifdef HAS_SOCKET
cea2e8a9 2583 return pp_ssockopt();
a0d0e21e 2584#else
cea2e8a9 2585 DIE(aTHX_ PL_no_sock_func, "getsockopt");
a0d0e21e
LW
2586#endif
2587}
2588
2589PP(pp_ssockopt)
2590{
a0d0e21e 2591#ifdef HAS_SOCKET
9cad6237 2592 dSP;
533c011a 2593 int optype = PL_op->op_type;
a0d0e21e
LW
2594 SV *sv;
2595 int fd;
2596 unsigned int optname;
2597 unsigned int lvl;
2598 GV *gv;
2599 register IO *io;
1e422769 2600 Sock_size_t len;
a0d0e21e
LW
2601
2602 if (optype == OP_GSOCKOPT)
2603 sv = sv_2mortal(NEWSV(22, 257));
2604 else
2605 sv = POPs;
2606 optname = (unsigned int) POPi;
2607 lvl = (unsigned int) POPi;
2608
2609 gv = (GV*)POPs;
2610 io = GvIOn(gv);
2611 if (!io || !IoIFP(io))
2612 goto nuts;
2613
760ac839 2614 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2615 switch (optype) {
2616 case OP_GSOCKOPT:
748a9306 2617 SvGROW(sv, 257);
a0d0e21e 2618 (void)SvPOK_only(sv);
748a9306
LW
2619 SvCUR_set(sv,256);
2620 *SvEND(sv) ='\0';
1e422769 2621 len = SvCUR(sv);
6ad3d225 2622 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
a0d0e21e 2623 goto nuts2;
1e422769 2624 SvCUR_set(sv, len);
748a9306 2625 *SvEND(sv) ='\0';
a0d0e21e
LW
2626 PUSHs(sv);
2627 break;
2628 case OP_SSOCKOPT: {
1e422769
PP
2629 char *buf;
2630 int aint;
2631 if (SvPOKp(sv)) {
2d8e6c8d
GS
2632 STRLEN l;
2633 buf = SvPV(sv, l);
2634 len = l;
1e422769 2635 }
56ee1660 2636 else {
a0d0e21e
LW
2637 aint = (int)SvIV(sv);
2638 buf = (char*)&aint;
2639 len = sizeof(int);
2640 }
6ad3d225 2641 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
a0d0e21e 2642 goto nuts2;
3280af22 2643 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2644 }
2645 break;
2646 }
2647 RETURN;
2648
2649nuts:
599cee73 2650 if (ckWARN(WARN_CLOSED))
bc37a18f 2651 report_evil_fh(gv, io, optype);
93189314 2652 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2653nuts2:
2654 RETPUSHUNDEF;
2655
2656#else
cea2e8a9 2657 DIE(aTHX_ PL_no_sock_func, "setsockopt");
a0d0e21e
LW
2658#endif
2659}
2660
2661PP(pp_getsockname)
2662{
2663#ifdef HAS_SOCKET
cea2e8a9 2664 return pp_getpeername();
a0d0e21e 2665#else
cea2e8a9 2666 DIE(aTHX_ PL_no_sock_func, "getsockname");
a0d0e21e
LW
2667#endif
2668}
2669
2670PP(pp_getpeername)
2671{
a0d0e21e 2672#ifdef HAS_SOCKET
9cad6237 2673 dSP;
533c011a 2674 int optype = PL_op->op_type;
a0d0e21e
LW
2675 SV *sv;
2676 int fd;
2677 GV *gv = (GV*)POPs;
2678 register IO *io = GvIOn(gv);
1e422769 2679 Sock_size_t len;
a0d0e21e
LW
2680
2681 if (!io || !IoIFP(io))
2682 goto nuts;
2683
2684 sv = sv_2mortal(NEWSV(22, 257));
748a9306 2685 (void)SvPOK_only(sv);
1e422769
PP
2686 len = 256;
2687 SvCUR_set(sv, len);
748a9306 2688 *SvEND(sv) ='\0';
760ac839 2689 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2690 switch (optype) {
2691 case OP_GETSOCKNAME:
6ad3d225 2692 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e
LW
2693 goto nuts2;
2694 break;
2695 case OP_GETPEERNAME:
6ad3d225 2696 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e 2697 goto nuts2;
490ab354
JH
2698#if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2699 {
2700 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";
2701 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2702 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2703 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2704 sizeof(u_short) + sizeof(struct in_addr))) {
301e8125 2705 goto nuts2;
490ab354
JH
2706 }
2707 }
2708#endif
a0d0e21e
LW
2709 break;
2710 }
13826f2c
CS
2711#ifdef BOGUS_GETNAME_RETURN
2712 /* Interactive Unix, getpeername() and getsockname()
2713 does not return valid namelen */
1e422769
PP
2714 if (len == BOGUS_GETNAME_RETURN)
2715 len = sizeof(struct sockaddr);
13826f2c 2716#endif
1e422769 2717 SvCUR_set(sv, len);
748a9306 2718 *SvEND(sv) ='\0';
a0d0e21e
LW
2719 PUSHs(sv);
2720 RETURN;
2721
2722nuts:
599cee73 2723 if (ckWARN(WARN_CLOSED))
bc37a18f 2724 report_evil_fh(gv, io, optype);
93189314 2725 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2726nuts2:
2727 RETPUSHUNDEF;
2728
2729#else
cea2e8a9 2730 DIE(aTHX_ PL_no_sock_func, "getpeername");
a0d0e21e
LW
2731#endif
2732}
2733
2734/* Stat calls. */
2735
2736PP(pp_lstat)
2737{
cea2e8a9 2738 return pp_stat();
a0d0e21e
LW
2739}
2740
2741PP(pp_stat)
2742{
39644a26 2743 dSP;
2dd78f96 2744 GV *gv;
54310121 2745 I32 gimme;
a0d0e21e 2746 I32 max = 13;
2d8e6c8d 2747 STRLEN n_a;
a0d0e21e 2748
533c011a 2749 if (PL_op->op_flags & OPf_REF) {
2dd78f96 2750 gv = cGVOP_gv;
8a4e5b40 2751 if (PL_op->op_type == OP_LSTAT) {
5d3e98de
RGS
2752 if (gv != PL_defgv) {
2753 if (ckWARN(WARN_IO))
9014280d 2754 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de
RGS
2755 "lstat() on filehandle %s", GvENAME(gv));
2756 } else if (PL_laststype != OP_LSTAT)
8a4e5b40 2757 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
8a4e5b40
DD
2758 }
2759
748a9306 2760 do_fstat:
2dd78f96 2761 if (gv != PL_defgv) {
3280af22 2762 PL_laststype = OP_STAT;
2dd78f96 2763 PL_statgv = gv;
3280af22 2764 sv_setpv(PL_statname, "");
2dd78f96
JH
2765 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2766 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
a0d0e21e 2767 }
9ddeeac9 2768 if (PL_laststatval < 0) {
2dd78f96
JH
2769 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2770 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
a0d0e21e 2771 max = 0;
9ddeeac9 2772 }
a0d0e21e
LW
2773 }
2774 else {
748a9306
LW
2775 SV* sv = POPs;
2776 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 2777 gv = (GV*)sv;
748a9306
LW
2778 goto do_fstat;
2779 }
2780 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 2781 gv = (GV*)SvRV(sv);
5d3e98de 2782 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
9014280d 2783 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de 2784 "lstat() on filehandle %s", GvENAME(gv));
748a9306
LW
2785 goto do_fstat;
2786 }
2d8e6c8d 2787 sv_setpv(PL_statname, SvPV(sv,n_a));
3280af22 2788 PL_statgv = Nullgv;
a0d0e21e 2789#ifdef HAS_LSTAT
533c011a
NIS
2790 PL_laststype = PL_op->op_type;
2791 if (PL_op->op_type == OP_LSTAT)
2d8e6c8d 2792 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
a0d0e21e
LW
2793 else
2794#endif
2d8e6c8d 2795 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
3280af22 2796 if (PL_laststatval < 0) {
2d8e6c8d 2797 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
9014280d 2798 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
a0d0e21e
LW
2799 max = 0;
2800 }
2801 }
2802
54310121
PP
2803 gimme = GIMME_V;
2804 if (gimme != G_ARRAY) {
2805 if (gimme != G_VOID)
2806 XPUSHs(boolSV(max));
2807 RETURN;
a0d0e21e
LW
2808 }
2809 if (max) {
36477c24
PP
2810 EXTEND(SP, max);
2811 EXTEND_MORTAL(max);
1ff81528
PL
2812 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2813 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
b448e4fe
JH
2814 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2815 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
146174a9
CB
2816#if Uid_t_size > IVSIZE
2817 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2818#else
23dcd6c8 2819# if Uid_t_sign <= 0
1ff81528 2820 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
23dcd6c8
JH
2821# else
2822 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2823# endif
146174a9 2824#endif
301e8125 2825#if Gid_t_size > IVSIZE
146174a9
CB
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2827#else
23dcd6c8 2828# if Gid_t_sign <= 0
1ff81528 2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
23dcd6c8
JH
2830# else
2831 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2832# endif
146174a9 2833#endif
cbdc8872 2834#ifdef USE_STAT_RDEV
1ff81528 2835 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
cbdc8872 2836#else
79cb57f6 2837 PUSHs(sv_2mortal(newSVpvn("", 0)));
cbdc8872 2838#endif
146174a9 2839#if Off_t_size > IVSIZE
4a9d6100 2840 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
146174a9 2841#else
1ff81528 2842 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
146174a9 2843#endif
cbdc8872 2844#ifdef BIG_TIME
172ae379
JH
2845 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2846 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2847 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
cbdc8872 2848#else
1ff81528
PL
2849 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2850 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2851 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
cbdc8872 2852#endif
a0d0e21e 2853#ifdef USE_STAT_BLOCKS
b448e4fe
JH
2854 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2855 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
a0d0e21e 2856#else
79cb57f6
GS
2857 PUSHs(sv_2mortal(newSVpvn("", 0)));
2858 PUSHs(sv_2mortal(newSVpvn("", 0)));
a0d0e21e
LW
2859#endif
2860 }
2861 RETURN;
2862}
2863
2864PP(pp_ftrread)
2865{
9cad6237 2866 I32 result;
2a3ff820 2867 dSP;
5ff3f7a4 2868#if defined(HAS_ACCESS) && defined(R_OK)
2d8e6c8d 2869 STRLEN n_a;
1af34c76 2870 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2871 result = access(POPpx, R_OK);
5ff3f7a4
GS
2872 if (result == 0)
2873 RETPUSHYES;
2874 if (result < 0)
2875 RETPUSHUNDEF;
2876 RETPUSHNO;
22865c03
GS
2877 }
2878 else
cea2e8a9 2879 result = my_stat();
5ff3f7a4 2880#else
cea2e8a9 2881 result = my_stat();
5ff3f7a4 2882#endif
22865c03 2883 SPAGAIN;
a0d0e21e
LW
2884 if (result < 0)
2885 RETPUSHUNDEF;
3280af22 2886 if (cando(S_IRUSR, 0, &PL_statcache))
a0d0e21e
LW
2887 RETPUSHYES;
2888 RETPUSHNO;
2889}
2890
2891PP(pp_ftrwrite)
2892{
9cad6237 2893 I32 result;
2a3ff820 2894 dSP;
5ff3f7a4 2895#if defined(HAS_ACCESS) && defined(W_OK)
2d8e6c8d 2896 STRLEN n_a;
1af34c76 2897 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2898 result = access(POPpx, W_OK);
5ff3f7a4
GS
2899 if (result == 0)
2900 RETPUSHYES;
2901 if (result < 0)
2902 RETPUSHUNDEF;
2903 RETPUSHNO;
22865c03
GS
2904 }
2905 else
cea2e8a9 2906 result = my_stat();
5ff3f7a4 2907#else
cea2e8a9 2908 result = my_stat();
5ff3f7a4 2909#endif
22865c03 2910 SPAGAIN;
a0d0e21e
LW
2911 if (result < 0)
2912 RETPUSHUNDEF;
3280af22 2913 if (cando(S_IWUSR, 0, &PL_statcache))
a0d0e21e
LW
2914 RETPUSHYES;
2915 RETPUSHNO;
2916}
2917
2918PP(pp_ftrexec)
2919{
9cad6237 2920 I32 result;
2a3ff820 2921 dSP;
5ff3f7a4 2922#if defined(HAS_ACCESS) && defined(X_OK)
2d8e6c8d 2923 STRLEN n_a;
1af34c76 2924 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2925 result = access(POPpx, X_OK);
5ff3f7a4
GS
2926 if (result == 0)
2927 RETPUSHYES;
2928 if (result < 0)
2929 RETPUSHUNDEF;
2930 RETPUSHNO;
22865c03
GS
2931 }
2932 else
cea2e8a9 2933 result = my_stat();
5ff3f7a4 2934#else
cea2e8a9 2935 result = my_stat();
5ff3f7a4 2936#endif
22865c03 2937 SPAGAIN;
a0d0e21e
LW
2938 if (result < 0)
2939 RETPUSHUNDEF;
3280af22 2940 if (cando(S_IXUSR, 0, &PL_statcache))
a0d0e21e
LW
2941 RETPUSHYES;
2942 RETPUSHNO;
2943}
2944
2945PP(pp_fteread)
2946{
9cad6237 2947 I32 result;
2a3ff820 2948 dSP;
5ff3f7a4 2949#ifdef PERL_EFF_ACCESS_R_OK
2d8e6c8d 2950 STRLEN n_a;
1af34c76 2951 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2952 result = PERL_EFF_ACCESS_R_OK(POPpx);
5ff3f7a4
GS
2953 if (result == 0)
2954 RETPUSHYES;
2955 if (result < 0)
2956 RETPUSHUNDEF;
2957 RETPUSHNO;
22865c03
GS
2958 }
2959 else
cea2e8a9 2960 result = my_stat();
5ff3f7a4 2961#else
cea2e8a9 2962 result = my_stat();
5ff3f7a4 2963#endif
22865c03 2964 SPAGAIN;
a0d0e21e
LW
2965 if (result < 0)
2966 RETPUSHUNDEF;
3280af22 2967 if (cando(S_IRUSR, 1, &PL_statcache))
a0d0e21e
LW
2968 RETPUSHYES;
2969 RETPUSHNO;
2970}
2971
2972PP(pp_ftewrite)
2973{
9cad6237 2974 I32 result;
2a3ff820 2975 dSP;
5ff3f7a4 2976#ifdef PERL_EFF_ACCESS_W_OK
2d8e6c8d 2977 STRLEN n_a;
1af34c76 2978 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 2979 result = PERL_EFF_ACCESS_W_OK(POPpx);
5ff3f7a4
GS
2980 if (result == 0)
2981 RETPUSHYES;
2982 if (result < 0)
2983 RETPUSHUNDEF;
2984 RETPUSHNO;
22865c03
GS
2985 }
2986 else
cea2e8a9 2987 result = my_stat();
5ff3f7a4 2988#else
cea2e8a9 2989 result = my_stat();
5ff3f7a4 2990#endif
22865c03 2991 SPAGAIN;
a0d0e21e
LW
2992 if (result < 0)
2993 RETPUSHUNDEF;
3280af22 2994 if (cando(S_IWUSR, 1, &PL_statcache))
a0d0e21e
LW
2995 RETPUSHYES;
2996 RETPUSHNO;
2997}
2998
2999PP(pp_fteexec)
3000{
9cad6237 3001 I32 result;
2a3ff820 3002 dSP;
5ff3f7a4 3003#ifdef PERL_EFF_ACCESS_X_OK
2d8e6c8d 3004 STRLEN n_a;
1af34c76 3005 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
43b06338 3006 result = PERL_EFF_ACCESS_X_OK(POPpx);
5ff3f7a4
GS
3007 if (result == 0)
3008 RETPUSHYES;
3009 if (result < 0)
3010 RETPUSHUNDEF;
3011 RETPUSHNO;
22865c03
GS
3012 }
3013 else
cea2e8a9 3014 result = my_stat();
5ff3f7a4 3015#else
cea2e8a9 3016 result = my_stat();
5ff3f7a4 3017#endif
22865c03 3018 SPAGAIN;
a0d0e21e
LW
3019 if (result < 0)
3020 RETPUSHUNDEF;
3280af22 3021 if (cando(S_IXUSR, 1, &PL_statcache))
a0d0e21e
LW
3022 RETPUSHYES;
3023 RETPUSHNO;
3024}
3025
3026PP(pp_ftis)
3027{
9cad6237 3028 I32 result = my_stat();
2a3ff820 3029 dSP;
a0d0e21e
LW
3030 if (result < 0)
3031 RETPUSHUNDEF;
3032 RETPUSHYES;
3033}
3034
3035PP(pp_fteowned)
3036{
cea2e8a9 3037 return pp_ftrowned();
a0d0e21e
LW
3038}
3039
3040PP(pp_ftrowned)
3041{
9cad6237 3042 I32 result = my_stat();
2a3ff820 3043 dSP;
a0d0e21e
LW
3044 if (result < 0)
3045 RETPUSHUNDEF;
146174a9
CB
3046 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3047 PL_euid : PL_uid) )
a0d0e21e
LW
3048 RETPUSHYES;
3049 RETPUSHNO;
3050}
3051
3052PP(pp_ftzero)
3053{
9cad6237 3054 I32 result = my_stat();
2a3ff820 3055 dSP;
a0d0e21e
LW
3056 if (result < 0)
3057 RETPUSHUNDEF;
146174a9 3058 if (PL_statcache.st_size == 0)
a0d0e21e
LW
3059 RETPUSHYES;
3060 RETPUSHNO;
3061}
3062
3063PP(pp_ftsize)
3064{
9cad6237 3065 I32 result = my_stat();
2a3ff820 3066 dSP; dTARGET;
a0d0e21e
LW
3067 if (result < 0)
3068 RETPUSHUNDEF;
146174a9
CB
3069#if Off_t_size > IVSIZE
3070 PUSHn(PL_statcache.st_size);
3071#else
3280af22 3072 PUSHi(PL_statcache.st_size);
146174a9 3073#endif
a0d0e21e
LW
3074 RETURN;
3075}
3076
3077PP(pp_ftmtime)
3078{
9cad6237 3079 I32 result = my_stat();
2a3ff820 3080 dSP; dTARGET;
a0d0e21e
LW
3081 if (result < 0)
3082 RETPUSHUNDEF;
c6419e06 3083 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
a0d0e21e
LW
3084 RETURN;
3085}
3086
3087PP(pp_ftatime)
3088{
9cad6237 3089 I32 result = my_stat();
2a3ff820 3090 dSP; dTARGET;
a0d0e21e
LW
3091 if (result < 0)
3092 RETPUSHUNDEF;
c6419e06 3093 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
a0d0e21e
LW
3094 RETURN;
3095}
3096
3097PP(pp_ftctime)
3098{
9cad6237 3099 I32 result = my_stat();
2a3ff820 3100 dSP; dTARGET;
a0d0e21e
LW
3101 if (result < 0)
3102 RETPUSHUNDEF;
c6419e06 3103 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
a0d0e21e
LW
3104 RETURN;
3105}
3106
3107PP(pp_ftsock)
3108{
9cad6237 3109 I32 result = my_stat();
2a3ff820 3110 dSP;
a0d0e21e
LW
3111 if (result < 0)
3112 RETPUSHUNDEF;
3280af22 3113 if (S_ISSOCK(PL_statcache.st_mode))
a0d0e21e
LW
3114 RETPUSHYES;
3115 RETPUSHNO;
3116}
3117
3118PP(pp_ftchr)
3119{
9cad6237 3120 I32 result = my_stat();
2a3ff820 3121 dSP;
a0d0e21e
LW
3122 if (result < 0)
3123 RETPUSHUNDEF;
3280af22 3124 if (S_ISCHR(PL_statcache.st_mode))
a0d0e21e
LW
3125 RETPUSHYES;
3126 RETPUSHNO;
3127}
3128
3129PP(pp_ftblk)
3130{
9cad6237 3131 I32 result = my_stat();
2a3ff820 3132 dSP;
a0d0e21e
LW
3133 if (result < 0)
3134 RETPUSHUNDEF;
3280af22 3135 if (S_ISBLK(PL_statcache.st_mode))
a0d0e21e
LW
3136 RETPUSHYES;
3137 RETPUSHNO;
3138}
3139
3140PP(pp_ftfile)
3141{
9cad6237 3142 I32 result = my_stat();
2a3ff820 3143 dSP;
a0d0e21e
LW
3144 if (result < 0)
3145 RETPUSHUNDEF;
3280af22 3146 if (S_ISREG(PL_statcache.st_mode))
a0d0e21e
LW
3147 RETPUSHYES;
3148 RETPUSHNO;
3149}
3150
3151PP(pp_ftdir)
3152{
9cad6237 3153 I32 result = my_stat();
2a3ff820 3154 dSP;
a0d0e21e
LW
3155 if (result < 0)
3156 RETPUSHUNDEF;
3280af22 3157 if (S_ISDIR(PL_statcache.st_mode))
a0d0e21e
LW
3158 RETPUSHYES;
3159 RETPUSHNO;
3160}
3161
3162PP(pp_ftpipe)
3163{
9cad6237 3164 I32 result = my_stat();
2a3ff820 3165 dSP;
a0d0e21e
LW
3166 if (result < 0)
3167 RETPUSHUNDEF;
3280af22 3168 if (S_ISFIFO(PL_statcache.st_mode))
a0d0e21e
LW
3169 RETPUSHYES;
3170 RETPUSHNO;
3171}
3172
3173PP(pp_ftlink)
3174{
9cad6237 3175 I32 result = my_lstat();
2a3ff820 3176 dSP;
a0d0e21e
LW
3177 if (result < 0)
3178 RETPUSHUNDEF;
3280af22 3179 if (S_ISLNK(PL_statcache.st_mode))
a0d0e21e
LW
3180 RETPUSHYES;
3181 RETPUSHNO;
3182}
3183
3184PP(pp_ftsuid)
3185{
39644a26 3186 dSP;
a0d0e21e 3187#ifdef S_ISUID
cea2e8a9 3188 I32 result = my_stat();
a0d0e21e
LW
3189 SPAGAIN;
3190 if (result < 0)
3191 RETPUSHUNDEF;
3280af22 3192 if (PL_statcache.st_mode & S_ISUID)
a0d0e21e
LW
3193 RETPUSHYES;
3194#endif
3195 RETPUSHNO;
3196}
3197
3198PP(pp_ftsgid)
3199{
39644a26 3200 dSP;
a0d0e21e 3201#ifdef S_ISGID
cea2e8a9 3202 I32 result = my_stat();
a0d0e21e
LW
3203 SPAGAIN;
3204 if (result < 0)
3205 RETPUSHUNDEF;
3280af22 3206 if (PL_statcache.st_mode & S_ISGID)
a0d0e21e
LW
3207 RETPUSHYES;
3208#endif
3209 RETPUSHNO;
3210}
3211
3212PP(pp_ftsvtx)
3213{
39644a26 3214 dSP;
a0d0e21e 3215#ifdef S_ISVTX
cea2e8a9 3216 I32 result = my_stat();
a0d0e21e
LW
3217 SPAGAIN;
3218 if (result < 0)
3219 RETPUSHUNDEF;
3280af22 3220 if (PL_statcache.st_mode & S_ISVTX)
a0d0e21e
LW
3221 RETPUSHYES;
3222#endif
3223 RETPUSHNO;
3224}
3225
3226PP(pp_fttty)
3227{
39644a26 3228 dSP;
a0d0e21e
LW
3229 int fd;
3230 GV *gv;
fb73857a 3231 char *tmps = Nullch;
2d8e6c8d 3232 STRLEN n_a;
fb73857a 3233
533c011a 3234 if (PL_op->op_flags & OPf_REF)
146174a9 3235 gv = cGVOP_gv;
fb73857a
PP
3236 else if (isGV(TOPs))
3237 gv = (GV*)POPs;
3238 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3239 gv = (GV*)SvRV(POPs);
a0d0e21e 3240 else
2d8e6c8d 3241 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
fb73857a 3242
a0d0e21e 3243 if (GvIO(gv) && IoIFP(GvIOp(gv)))
760ac839 3244 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
fb73857a 3245 else if (tmps && isDIGIT(*tmps))
a0d0e21e
LW
3246 fd = atoi(tmps);
3247 else
3248 RETPUSHUNDEF;
6ad3d225 3249 if (PerlLIO_isatty(fd))
a0d0e21e
LW
3250 RETPUSHYES;
3251 RETPUSHNO;
3252}
3253
16d20bd9
AD
3254#if defined(atarist) /* this will work with atariST. Configure will
3255 make guesses for other systems. */
3256# define FILE_base(f) ((f)->_base)
3257# define FILE_ptr(f) ((f)->_ptr)
3258# define FILE_cnt(f) ((f)->_cnt)
3259# define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
a0d0e21e
LW
3260#endif
3261
3262PP(pp_fttext)
3263{
39644a26 3264 dSP;
a0d0e21e
LW
3265 I32 i;
3266 I32 len;
3267 I32 odd = 0;
3268 STDCHAR tbuf[512];
3269 register STDCHAR *s;
3270 register IO *io;
5f05dabc
PP
3271 register SV *sv;
3272 GV *gv;
2d8e6c8d 3273 STRLEN n_a;
146174a9 3274 PerlIO *fp;
a0d0e21e 3275
533c011a 3276 if (PL_op->op_flags & OPf_REF)
146174a9 3277 gv = cGVOP_gv;
5f05dabc
PP
3278 else if (isGV(TOPs))
3279 gv = (GV*)POPs;
3280 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3281 gv = (GV*)SvRV(POPs);
3282 else
3283 gv = Nullgv;
3284
3285 if (gv) {
a0d0e21e 3286 EXTEND(SP, 1);
3280af22
NIS
3287 if (gv == PL_defgv) {
3288 if (PL_statgv)
3289 io = GvIO(PL_statgv);
a0d0e21e 3290 else {
3280af22 3291 sv = PL_statname;
a0d0e21e
LW
3292 goto really_filename;
3293 }
3294 }
3295 else {
3280af22
NIS
3296 PL_statgv = gv;
3297 PL_laststatval = -1;
3298 sv_setpv(PL_statname, "");
3299 io = GvIO(PL_statgv);
a0d0e21e
LW
3300 }
3301 if (io && IoIFP(io)) {
5f05dabc 3302 if (! PerlIO_has_base(IoIFP(io)))
cea2e8a9 3303 DIE(aTHX_ "-T and -B not implemented on filehandles");
3280af22
NIS
3304 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3305 if (PL_laststatval < 0)
5f05dabc 3306 RETPUSHUNDEF;
9cbac4c7 3307 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
533c011a 3308 if (PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3309 RETPUSHNO;
3310 else
3311 RETPUSHYES;
9cbac4c7 3312 }
a20bf0c3 3313 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
760ac839 3314 i = PerlIO_getc(IoIFP(io));
a0d0e21e 3315 if (i != EOF)
760ac839 3316 (void)PerlIO_ungetc(IoIFP(io),i);
a0d0e21e 3317 }
a20bf0c3 3318 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
a0d0e21e 3319 RETPUSHYES;
a20bf0c3
JH
3320 len = PerlIO_get_bufsiz(IoIFP(io));
3321 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
760ac839
LW
3322 /* sfio can have large buffers - limit to 512 */
3323 if (len > 512)
3324 len = 512;
a0d0e21e
LW
3325 }
3326 else {
2dd78f96 3327 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
146174a9 3328 gv = cGVOP_gv;
2dd78f96 3329 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
146174a9 3330 }
93189314 3331 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
3332 RETPUSHUNDEF;
3333 }
3334 }
3335 else {
3336 sv = POPs;
5f05dabc 3337 really_filename:
3280af22
NIS
3338 PL_statgv = Nullgv;
3339 PL_laststatval = -1;
5c9aa243 3340 PL_laststype = OP_STAT;
2d8e6c8d 3341 sv_setpv(PL_statname, SvPV(sv, n_a));
146174a9 3342 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
f9341440 3343 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
9014280d 3344 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
a0d0e21e
LW
3345 RETPUSHUNDEF;
3346 }
146174a9
CB
3347 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3348 if (PL_laststatval < 0) {
3349 (void)PerlIO_close(fp);
5f05dabc 3350 RETPUSHUNDEF;
146174a9 3351 }
60382766 3352 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
146174a9
CB
3353 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3354 (void)PerlIO_close(fp);
a0d0e21e 3355 if (len <= 0) {
533c011a 3356 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3357 RETPUSHNO; /* special case NFS directories */
3358 RETPUSHYES; /* null file is anything */
3359 }
3360 s = tbuf;
3361 }
3362
3363 /* now scan s to look for textiness */
4633a7c4 3364 /* XXX ASCII dependent code */
a0d0e21e 3365
146174a9
CB
3366#if defined(DOSISH) || defined(USEMYBINMODE)
3367 /* ignore trailing ^Z on short files */
3368 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3369 --len;
3370#endif
3371
a0d0e21e
LW
3372 for (i = 0; i < len; i++, s++) {
3373 if (!*s) { /* null never allowed in text */
3374 odd += len;
3375 break;
3376 }
9d116dd7 3377#ifdef EBCDIC
301e8125 3378 else if (!(isPRINT(*s) || isSPACE(*s)))
9d116dd7
JH
3379 odd++;
3380#else
146174a9
CB
3381 else if (*s & 128) {
3382#ifdef USE_LOCALE
2de3dbcc 3383 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
b3f66c68
GS
3384 continue;
3385#endif
3386 /* utf8 characters don't count as odd */
fd400ab9 3387 if (UTF8_IS_START(*s)) {
b3f66c68
GS
3388 int ulen = UTF8SKIP(s);
3389 if (ulen < len - i) {
3390 int j;
3391 for (j = 1; j < ulen; j++) {
fd400ab9 3392 if (!UTF8_IS_CONTINUATION(s[j]))
b3f66c68
GS
3393 goto not_utf8;
3394 }
3395 --ulen; /* loop does extra increment */
3396 s += ulen;
3397 i += ulen;
3398 continue;
3399 }
3400 }
3401 not_utf8:
3402 odd++;
146174a9 3403 }
a0d0e21e
LW
3404 else if (*s < 32 &&
3405 *s != '\n' && *s != '\r' && *s != '\b' &&
3406 *s != '\t' && *s != '\f' && *s != 27)
3407 odd++;
9d116dd7 3408#endif
a0d0e21e
LW
3409 }
3410
533c011a 3411 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
a0d0e21e
LW
3412 RETPUSHNO;
3413 else
3414 RETPUSHYES;
3415}
3416
3417PP(pp_ftbinary)
3418{
cea2e8a9 3419 return pp_fttext();
a0d0e21e
LW
3420}
3421
3422/* File calls. */
3423
3424PP(pp_chdir)
3425{
39644a26 3426 dSP; dTARGET;
a0d0e21e
LW
3427 char *tmps;
3428 SV **svp;
2d8e6c8d 3429 STRLEN n_a;
a0d0e21e 3430
35ae6b54
MS
3431 if( MAXARG == 1 )
3432 tmps = POPpx;
3433 else
3434 tmps = 0;
3435
3436 if( !tmps || !*tmps ) {
3437 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3438 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
491527d0 3439#ifdef VMS
35ae6b54 3440 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
491527d0 3441#endif
35ae6b54
MS
3442 )
3443 {
3444 if( MAXARG == 1 )
9014280d 3445 deprecate("chdir('') or chdir(undef) as chdir()");
35ae6b54
MS
3446 tmps = SvPV(*svp, n_a);
3447 }
72f496dc 3448 else {
389ec635 3449 PUSHi(0);
b7ab37f8 3450 TAINT_PROPER("chdir");
389ec635
MS
3451 RETURN;
3452 }
8ea155d1 3453 }
8ea155d1 3454
a0d0e21e 3455 TAINT_PROPER("chdir");
6ad3d225 3456 PUSHi( PerlDir_chdir(tmps) >= 0 );
748a9306
LW
3457#ifdef VMS
3458 /* Clear the DEFAULT element of ENV so we'll get the new value
3459 * in the future. */
6b88bc9c 3460 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
748a9306 3461#endif
a0d0e21e
LW
3462 RETURN;
3463}
3464
3465PP(pp_chown)
3466{
a0d0e21e 3467#ifdef HAS_CHOWN
76ffd3b9
IZ
3468 dSP; dMARK; dTARGET;
3469 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3470
a0d0e21e
LW
3471 SP = MARK;
3472 PUSHi(value);
3473 RETURN;
3474#else
0322a713 3475 DIE(aTHX_ PL_no_func, "chown");
a0d0e21e
LW
3476#endif
3477}
3478
3479PP(pp_chroot)
3480{
a0d0e21e 3481#ifdef HAS_CHROOT
76ffd3b9 3482 dSP; dTARGET;
2d8e6c8d 3483 STRLEN n_a;
d05c1ba0 3484 char *tmps = POPpx;
a0d0e21e
LW
3485 TAINT_PROPER("chroot");
3486 PUSHi( chroot(tmps) >= 0 );
3487 RETURN;
3488#else
cea2e8a9 3489 DIE(aTHX_ PL_no_func, "chroot");
a0d0e21e
LW
3490#endif
3491}
3492
3493PP(pp_unlink)
3494{
39644a26 3495 dSP; dMARK; dTARGET;
a0d0e21e 3496 I32 value;
533c011a 3497 value = (I32)apply(PL_op->op_type, MARK, SP);
a0d0e21e
LW
3498 SP = MARK;
3499 PUSHi(value);
3500 RETURN;
3501}
3502
3503PP(pp_chmod)
3504{
39644a26 3505 dSP; dMARK; dTARGET;
a0d0e21e 3506 I32 value;
533c011a 3507 value = (I32)apply(PL_op->op_type, MARK, SP);
a0d0e21e
LW
3508 SP = MARK;
3509 PUSHi(value);
3510 RETURN;
3511}
3512
3513PP(pp_utime)
3514{
39644a26 3515 dSP; dMARK; dTARGET;
a0d0e21e 3516 I32 value;