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