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