This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
dorassign doesn't need --SP;
[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
a0d0e21e
LW
1771PP(pp_send)
1772{
27da23d5 1773 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
1774 GV *gv;
1775 IO *io;
748a9306 1776 SV *bufsv;
83003860 1777 const char *buffer;
64a1bc8e 1778 Size_t length = 0;
8c99d73e 1779 SSize_t retval;
a0d0e21e 1780 STRLEN blen;
1d603a67 1781 MAGIC *mg;
64a1bc8e
NC
1782 const int op_type = PL_op->op_type;
1783
a0d0e21e 1784 gv = (GV*)*++MARK;
14befaf4 1785 if (PL_op->op_type == OP_SYSWRITE
5b468f54
AMS
1786 && gv && (io = GvIO(gv))
1787 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
14befaf4 1788 {
1d603a67 1789 SV *sv;
64a1bc8e
NC
1790
1791 if (MARK == SP - 1) {
1792 EXTEND(SP, 1000);
1793 sv = sv_2mortal(newSViv(sv_len(*SP)));
1794 PUSHs(sv);
1795 PUTBACK;
1796 }
1d603a67 1797
64a1bc8e
NC
1798 PUSHMARK(ORIGMARK);
1799 *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
1d603a67 1800 ENTER;
864dbfa3 1801 call_method("WRITE", G_SCALAR);
1d603a67
GB
1802 LEAVE;
1803 SPAGAIN;
1804 sv = POPs;
1805 SP = ORIGMARK;
1806 PUSHs(sv);
1807 RETURN;
1808 }
a0d0e21e
LW
1809 if (!gv)
1810 goto say_undef;
64a1bc8e 1811
748a9306 1812 bufsv = *++MARK;
64a1bc8e
NC
1813
1814 if (op_type == OP_SYSWRITE) {
1815 if (MARK >= SP) {
1816 length = (Size_t) sv_len(bufsv);
1817 } else {
8c99d73e 1818#if Size_t_size > IVSIZE
64a1bc8e 1819 length = (Size_t)SvNVx(*++MARK);
146174a9 1820#else
64a1bc8e 1821 length = (Size_t)SvIVx(*++MARK);
146174a9 1822#endif
64a1bc8e
NC
1823 if ((SSize_t)length < 0)
1824 DIE(aTHX_ "Negative length");
1825 }
1826 }
748a9306 1827 SETERRNO(0,0);
a0d0e21e
LW
1828 io = GvIO(gv);
1829 if (!io || !IoIFP(io)) {
8c99d73e 1830 retval = -1;
bc37a18f
RG
1831 if (ckWARN(WARN_CLOSED))
1832 report_evil_fh(gv, io, PL_op->op_type);
b5fe5ca2 1833 SETERRNO(EBADF,RMS_IFI);
7d59b7e4
NIS
1834 goto say_undef;
1835 }
1836
1837 if (PerlIO_isutf8(IoIFP(io))) {
6aa2f6a7
NC
1838 if (!SvUTF8(bufsv)) {
1839 bufsv = sv_2mortal(newSVsv(bufsv));
1840 buffer = sv_2pvutf8(bufsv, &blen);
1841 } else
83003860 1842 buffer = SvPV_const(bufsv, blen);
a0d0e21e 1843 }
7d59b7e4 1844 else {
4de5f5ca
JH
1845 if (DO_UTF8(bufsv)) {
1846 /* Not modifying source SV, so making a temporary copy. */
1847 bufsv = sv_2mortal(newSVsv(bufsv));
1848 sv_utf8_downgrade(bufsv, FALSE);
1849 }
83003860 1850 buffer = SvPV_const(bufsv, blen);
7d59b7e4
NIS
1851 }
1852
64a1bc8e 1853 if (op_type == OP_SYSWRITE) {
7d59b7e4
NIS
1854 IV offset;
1855 if (DO_UTF8(bufsv)) {
1856 /* length and offset are in chars */
1857 blen = sv_len_utf8(bufsv);
1858 }
bbce6d69 1859 if (MARK < SP) {
a0d0e21e 1860 offset = SvIVx(*++MARK);
bbce6d69 1861 if (offset < 0) {
eb160463 1862 if (-offset > (IV)blen)
cea2e8a9 1863 DIE(aTHX_ "Offset outside string");
bbce6d69 1864 offset += blen;
eb160463 1865 } else if (offset >= (IV)blen && blen > 0)
cea2e8a9 1866 DIE(aTHX_ "Offset outside string");
bbce6d69 1867 } else
a0d0e21e
LW
1868 offset = 0;
1869 if (length > blen - offset)
1870 length = blen - offset;
7d59b7e4 1871 if (DO_UTF8(bufsv)) {
83003860 1872 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
7d59b7e4
NIS
1873 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1874 }
1875 else {
1876 buffer = buffer+offset;
1877 }
a7092146 1878#ifdef PERL_SOCK_SYSWRITE_IS_SEND
50952442 1879 if (IoTYPE(io) == IoTYPE_SOCKET) {
8c99d73e 1880 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1881 buffer, length, 0);
a7092146
GS
1882 }
1883 else
1884#endif
1885 {
94e4c244 1886 /* See the note at doio.c:do_print about filesize limits. --jhi */
8c99d73e 1887 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
7d59b7e4 1888 buffer, length);
a7092146 1889 }
a0d0e21e
LW
1890 }
1891#ifdef HAS_SOCKET
64a1bc8e
NC
1892 else {
1893 const int flags = SvIVx(*++MARK);
1894 if (SP > MARK) {
1895 STRLEN mlen;
1896 char * const sockbuf = SvPVx(*++MARK, mlen);
1897 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1898 flags, (struct sockaddr *)sockbuf, mlen);
1899 }
1900 else {
1901 retval
1902 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1903 }
a0d0e21e 1904 }
a0d0e21e
LW
1905#else
1906 else
cea2e8a9 1907 DIE(aTHX_ PL_no_sock_func, "send");
a0d0e21e 1908#endif
8c99d73e 1909 if (retval < 0)
a0d0e21e
LW
1910 goto say_undef;
1911 SP = ORIGMARK;
f36eea10
JH
1912 if (DO_UTF8(bufsv))
1913 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
8c99d73e
GS
1914#if Size_t_size > IVSIZE
1915 PUSHn(retval);
1916#else
1917 PUSHi(retval);
1918#endif
a0d0e21e
LW
1919 RETURN;
1920
1921 say_undef:
1922 SP = ORIGMARK;
1923 RETPUSHUNDEF;
1924}
1925
a0d0e21e
LW
1926PP(pp_eof)
1927{
27da23d5 1928 dVAR; dSP;
a0d0e21e 1929 GV *gv;
5b468f54 1930 IO *io;
4592e6ca 1931 MAGIC *mg;
a0d0e21e 1932
32da55ab 1933 if (MAXARG == 0) {
146174a9
CB
1934 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1935 IO *io;
ed2c6b9b 1936 gv = PL_last_in_gv = GvEGV(PL_argvgv);
146174a9
CB
1937 io = GvIO(gv);
1938 if (io && !IoIFP(io)) {
1939 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1940 IoLINES(io) = 0;
1941 IoFLAGS(io) &= ~IOf_START;
2fbb330f 1942 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
146174a9
CB
1943 sv_setpvn(GvSV(gv), "-", 1);
1944 SvSETMAGIC(GvSV(gv));
1945 }
1946 else if (!nextargv(gv))
1947 RETPUSHYES;
1948 }
1949 }
1950 else
1951 gv = PL_last_in_gv; /* eof */
1952 }
a0d0e21e 1953 else
146174a9 1954 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
4592e6ca 1955
5b468f54
AMS
1956 if (gv && (io = GvIO(gv))
1957 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1958 {
4592e6ca 1959 PUSHMARK(SP);
5b468f54 1960 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
1961 PUTBACK;
1962 ENTER;
864dbfa3 1963 call_method("EOF", G_SCALAR);
4592e6ca
NIS
1964 LEAVE;
1965 SPAGAIN;
1966 RETURN;
1967 }
1968
54310121 1969 PUSHs(boolSV(!gv || do_eof(gv)));
a0d0e21e
LW
1970 RETURN;
1971}
1972
1973PP(pp_tell)
1974{
27da23d5 1975 dVAR; dSP; dTARGET;
301e8125 1976 GV *gv;
5b468f54 1977 IO *io;
4592e6ca 1978 MAGIC *mg;
a0d0e21e 1979
c4420975
AL
1980 if (MAXARG != 0)
1981 PL_last_in_gv = (GV*)POPs;
1982 gv = PL_last_in_gv;
4592e6ca 1983
5b468f54
AMS
1984 if (gv && (io = GvIO(gv))
1985 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1986 {
4592e6ca 1987 PUSHMARK(SP);
5b468f54 1988 XPUSHs(SvTIED_obj((SV*)io, mg));
4592e6ca
NIS
1989 PUTBACK;
1990 ENTER;
864dbfa3 1991 call_method("TELL", G_SCALAR);
4592e6ca
NIS
1992 LEAVE;
1993 SPAGAIN;
1994 RETURN;
1995 }
1996
146174a9
CB
1997#if LSEEKSIZE > IVSIZE
1998 PUSHn( do_tell(gv) );
1999#else
a0d0e21e 2000 PUSHi( do_tell(gv) );
146174a9 2001#endif
a0d0e21e
LW
2002 RETURN;
2003}
2004
137443ea
PP
2005PP(pp_sysseek)
2006{
27da23d5 2007 dVAR; dSP;
a0d0e21e 2008 GV *gv;
5b468f54 2009 IO *io;
1df70142 2010 const int whence = POPi;
146174a9
CB
2011#if LSEEKSIZE > IVSIZE
2012 Off_t offset = (Off_t)SvNVx(POPs);
2013#else
d9b3e12d 2014 Off_t offset = (Off_t)SvIVx(POPs);
146174a9 2015#endif
4592e6ca 2016 MAGIC *mg;
a0d0e21e 2017
3280af22 2018 gv = PL_last_in_gv = (GV*)POPs;
4592e6ca 2019
5b468f54
AMS
2020 if (gv && (io = GvIO(gv))
2021 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2022 {
4592e6ca 2023 PUSHMARK(SP);
5b468f54 2024 XPUSHs(SvTIED_obj((SV*)io, mg));
cb50131a
CB
2025#if LSEEKSIZE > IVSIZE
2026 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2027#else
b448e4fe 2028 XPUSHs(sv_2mortal(newSViv(offset)));
cb50131a 2029#endif
b448e4fe 2030 XPUSHs(sv_2mortal(newSViv(whence)));
4592e6ca
NIS
2031 PUTBACK;
2032 ENTER;
864dbfa3 2033 call_method("SEEK", G_SCALAR);
4592e6ca
NIS
2034 LEAVE;
2035 SPAGAIN;
2036 RETURN;
2037 }
2038
533c011a 2039 if (PL_op->op_type == OP_SEEK)
8903cb82
PP
2040 PUSHs(boolSV(do_seek(gv, offset, whence)));
2041 else {
b448e4fe
JH
2042 Off_t sought = do_sysseek(gv, offset, whence);
2043 if (sought < 0)
146174a9
CB
2044 PUSHs(&PL_sv_undef);
2045 else {
b448e4fe 2046 SV* sv = sought ?
146174a9 2047#if LSEEKSIZE > IVSIZE
b448e4fe 2048 newSVnv((NV)sought)
146174a9 2049#else
b448e4fe 2050 newSViv(sought)
146174a9
CB
2051#endif
2052 : newSVpvn(zero_but_true, ZBTLEN);
2053 PUSHs(sv_2mortal(sv));
2054 }
8903cb82 2055 }
a0d0e21e
LW
2056 RETURN;
2057}
2058
2059PP(pp_truncate)
2060{
39644a26 2061 dSP;
8c99d73e
GS
2062 /* There seems to be no consensus on the length type of truncate()
2063 * and ftruncate(), both off_t and size_t have supporters. In
2064 * general one would think that when using large files, off_t is
2065 * at least as wide as size_t, so using an off_t should be okay. */
2066 /* XXX Configure probe for the length type of *truncate() needed XXX */
2067 Off_t len;
a0d0e21e 2068
25342a55 2069#if Off_t_size > IVSIZE
8c99d73e
GS
2070 len = (Off_t)POPn;
2071#else
2072 len = (Off_t)POPi;
2073#endif
2074 /* Checking for length < 0 is problematic as the type might or
301e8125 2075 * might not be signed: if it is not, clever compilers will moan. */
8c99d73e 2076 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
748a9306 2077 SETERRNO(0,0);
d05c1ba0 2078 {
d05c1ba0
JH
2079 int result = 1;
2080 GV *tmpgv;
090bf15b
SR
2081 IO *io;
2082
d05c1ba0 2083 if (PL_op->op_flags & OPf_SPECIAL) {
7a5fd60d 2084 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
d05c1ba0 2085
090bf15b
SR
2086 do_ftruncate_gv:
2087 if (!GvIO(tmpgv))
2088 result = 0;
d05c1ba0 2089 else {
090bf15b
SR
2090 PerlIO *fp;
2091 io = GvIOp(tmpgv);
2092 do_ftruncate_io:
2093 TAINT_PROPER("truncate");
2094 if (!(fp = IoIFP(io))) {
2095 result = 0;
2096 }
2097 else {
2098 PerlIO_flush(fp);
cbdc8872 2099#ifdef HAS_TRUNCATE
090bf15b 2100 if (ftruncate(PerlIO_fileno(fp), len) < 0)
301e8125 2101#else
090bf15b 2102 if (my_chsize(PerlIO_fileno(fp), len) < 0)
cbdc8872 2103#endif
090bf15b
SR
2104 result = 0;
2105 }
d05c1ba0 2106 }
cbdc8872 2107 }
d05c1ba0
JH
2108 else {
2109 SV *sv = POPs;
83003860 2110 const char *name;
7a5fd60d 2111
d05c1ba0
JH
2112 if (SvTYPE(sv) == SVt_PVGV) {
2113 tmpgv = (GV*)sv; /* *main::FRED for example */
090bf15b 2114 goto do_ftruncate_gv;
d05c1ba0
JH
2115 }
2116 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2117 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
090bf15b
SR
2118 goto do_ftruncate_gv;
2119 }
2120 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2121 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2122 goto do_ftruncate_io;
d05c1ba0 2123 }
1e422769 2124
83003860 2125 name = SvPV_nolen_const(sv);
d05c1ba0 2126 TAINT_PROPER("truncate");
cbdc8872 2127#ifdef HAS_TRUNCATE
d05c1ba0
JH
2128 if (truncate(name, len) < 0)
2129 result = 0;
cbdc8872 2130#else
d05c1ba0
JH
2131 {
2132 int tmpfd;
2133
2134 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
cbdc8872 2135 result = 0;
d05c1ba0
JH
2136 else {
2137 if (my_chsize(tmpfd, len) < 0)
2138 result = 0;
2139 PerlLIO_close(tmpfd);
2140 }
cbdc8872 2141 }
a0d0e21e 2142#endif
d05c1ba0 2143 }
a0d0e21e 2144
d05c1ba0
JH
2145 if (result)
2146 RETPUSHYES;
2147 if (!errno)
93189314 2148 SETERRNO(EBADF,RMS_IFI);
d05c1ba0
JH
2149 RETPUSHUNDEF;
2150 }
a0d0e21e
LW
2151}
2152
a0d0e21e
LW
2153PP(pp_ioctl)
2154{
39644a26 2155 dSP; dTARGET;
748a9306 2156 SV *argsv = POPs;
1df70142 2157 const unsigned int func = POPu;
e1ec3a88 2158 const int optype = PL_op->op_type;
a0d0e21e 2159 char *s;
324aa91a 2160 IV retval;
a0d0e21e 2161 GV *gv = (GV*)POPs;
c289d2f7 2162 IO *io = gv ? GvIOn(gv) : 0;
a0d0e21e 2163
748a9306 2164 if (!io || !argsv || !IoIFP(io)) {
c289d2f7
JH
2165 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2166 report_evil_fh(gv, io, PL_op->op_type);
93189314 2167 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
a0d0e21e
LW
2168 RETPUSHUNDEF;
2169 }
2170
748a9306 2171 if (SvPOK(argsv) || !SvNIOK(argsv)) {
a0d0e21e 2172 STRLEN len;
324aa91a 2173 STRLEN need;
748a9306 2174 s = SvPV_force(argsv, len);
324aa91a
HF
2175 need = IOCPARM_LEN(func);
2176 if (len < need) {
2177 s = Sv_Grow(argsv, need + 1);
2178 SvCUR_set(argsv, need);
a0d0e21e
LW
2179 }
2180
748a9306 2181 s[SvCUR(argsv)] = 17; /* a little sanity check here */
a0d0e21e
LW
2182 }
2183 else {
748a9306 2184 retval = SvIV(argsv);
c529f79d 2185 s = INT2PTR(char*,retval); /* ouch */
a0d0e21e
LW
2186 }
2187
2188 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2189
2190 if (optype == OP_IOCTL)
2191#ifdef HAS_IOCTL
76e3520e 2192 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
a0d0e21e 2193#else
cea2e8a9 2194 DIE(aTHX_ "ioctl is not implemented");
a0d0e21e
LW
2195#endif
2196 else
c214f4ad
B
2197#ifndef HAS_FCNTL
2198 DIE(aTHX_ "fcntl is not implemented");
2199#else
55497cff 2200#if defined(OS2) && defined(__EMX__)
760ac839 2201 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
55497cff 2202#else
760ac839 2203 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
301e8125 2204#endif
6652bd42 2205#endif
a0d0e21e 2206
6652bd42 2207#if defined(HAS_IOCTL) || defined(HAS_FCNTL)
748a9306
LW
2208 if (SvPOK(argsv)) {
2209 if (s[SvCUR(argsv)] != 17)
cea2e8a9 2210 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
53e06cf0 2211 OP_NAME(PL_op));
748a9306
LW
2212 s[SvCUR(argsv)] = 0; /* put our null back */
2213 SvSETMAGIC(argsv); /* Assume it has changed */
a0d0e21e
LW
2214 }
2215
2216 if (retval == -1)
2217 RETPUSHUNDEF;
2218 if (retval != 0) {
2219 PUSHi(retval);
2220 }
2221 else {
8903cb82 2222 PUSHp(zero_but_true, ZBTLEN);
a0d0e21e 2223 }
4808266b 2224#endif
c214f4ad 2225 RETURN;
a0d0e21e
LW
2226}
2227
2228PP(pp_flock)
2229{
9cad6237 2230#ifdef FLOCK
39644a26 2231 dSP; dTARGET;
a0d0e21e
LW
2232 I32 value;
2233 int argtype;
2234 GV *gv;
bc37a18f 2235 IO *io = NULL;
760ac839 2236 PerlIO *fp;
16d20bd9 2237
a0d0e21e 2238 argtype = POPi;
32da55ab 2239 if (MAXARG == 0)
3280af22 2240 gv = PL_last_in_gv;
a0d0e21e
LW
2241 else
2242 gv = (GV*)POPs;
bc37a18f
RG
2243 if (gv && (io = GvIO(gv)))
2244 fp = IoIFP(io);
2245 else {
a0d0e21e 2246 fp = Nullfp;
bc37a18f
RG
2247 io = NULL;
2248 }
a0d0e21e 2249 if (fp) {
68dc0745 2250 (void)PerlIO_flush(fp);
76e3520e 2251 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
a0d0e21e 2252 }
cb50131a 2253 else {
bc37a18f
RG
2254 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2255 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 2256 value = 0;
93189314 2257 SETERRNO(EBADF,RMS_IFI);
cb50131a 2258 }
a0d0e21e
LW
2259 PUSHi(value);
2260 RETURN;
2261#else
cea2e8a9 2262 DIE(aTHX_ PL_no_func, "flock()");
a0d0e21e
LW
2263#endif
2264}
2265
2266/* Sockets. */
2267
2268PP(pp_socket)
2269{
a0d0e21e 2270#ifdef HAS_SOCKET
9cad6237 2271 dSP;
a0d0e21e
LW
2272 GV *gv;
2273 register IO *io;
2274 int protocol = POPi;
2275 int type = POPi;
2276 int domain = POPi;
2277 int fd;
2278
2279 gv = (GV*)POPs;
c289d2f7 2280 io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2281
c289d2f7
JH
2282 if (!gv || !io) {
2283 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2284 report_evil_fh(gv, io, PL_op->op_type);
2285 if (IoIFP(io))
2286 do_close(gv, FALSE);
93189314 2287 SETERRNO(EBADF,LIB_INVARG);
a0d0e21e
LW
2288 RETPUSHUNDEF;
2289 }
2290
57171420
BS
2291 if (IoIFP(io))
2292 do_close(gv, FALSE);
2293
a0d0e21e 2294 TAINT_PROPER("socket");
6ad3d225 2295 fd = PerlSock_socket(domain, type, protocol);
a0d0e21e
LW
2296 if (fd < 0)
2297 RETPUSHUNDEF;
460c8493
IZ
2298 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2299 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2300 IoTYPE(io) = IoTYPE_SOCKET;
a0d0e21e 2301 if (!IoIFP(io) || !IoOFP(io)) {
760ac839
LW
2302 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2303 if (IoOFP(io)) PerlIO_close(IoOFP(io));
6ad3d225 2304 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
a0d0e21e
LW
2305 RETPUSHUNDEF;
2306 }
8d2a6795
GS
2307#if defined(HAS_FCNTL) && defined(F_SETFD)
2308 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2309#endif
a0d0e21e 2310
d5ff79b3
OF
2311#ifdef EPOC
2312 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2313#endif
2314
a0d0e21e
LW
2315 RETPUSHYES;
2316#else
cea2e8a9 2317 DIE(aTHX_ PL_no_sock_func, "socket");
a0d0e21e
LW
2318#endif
2319}
2320
2321PP(pp_sockpair)
2322{
c95c94b1 2323#if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
76ffd3b9 2324 dSP;
a0d0e21e
LW
2325 GV *gv1;
2326 GV *gv2;
2327 register IO *io1;
2328 register IO *io2;
2329 int protocol = POPi;
2330 int type = POPi;
2331 int domain = POPi;
2332 int fd[2];
2333
2334 gv2 = (GV*)POPs;
2335 gv1 = (GV*)POPs;
c289d2f7
JH
2336 io1 = gv1 ? GvIOn(gv1) : NULL;
2337 io2 = gv2 ? GvIOn(gv2) : NULL;
2338 if (!gv1 || !gv2 || !io1 || !io2) {
2339 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2340 if (!gv1 || !io1)
2341 report_evil_fh(gv1, io1, PL_op->op_type);
2342 if (!gv2 || !io2)
2343 report_evil_fh(gv1, io2, PL_op->op_type);
2344 }
2345 if (IoIFP(io1))
2346 do_close(gv1, FALSE);
2347 if (IoIFP(io2))
2348 do_close(gv2, FALSE);
a0d0e21e 2349 RETPUSHUNDEF;
c289d2f7 2350 }
a0d0e21e 2351
dc0d0a5f
JH
2352 if (IoIFP(io1))
2353 do_close(gv1, FALSE);
2354 if (IoIFP(io2))
2355 do_close(gv2, FALSE);
57171420 2356
a0d0e21e 2357 TAINT_PROPER("socketpair");
6ad3d225 2358 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
a0d0e21e 2359 RETPUSHUNDEF;
460c8493
IZ
2360 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2361 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
50952442 2362 IoTYPE(io1) = IoTYPE_SOCKET;
460c8493
IZ
2363 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2364 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
50952442 2365 IoTYPE(io2) = IoTYPE_SOCKET;
a0d0e21e 2366 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
760ac839
LW
2367 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2368 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
6ad3d225 2369 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
760ac839
LW
2370 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2371 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
6ad3d225 2372 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
a0d0e21e
LW
2373 RETPUSHUNDEF;
2374 }
8d2a6795
GS
2375#if defined(HAS_FCNTL) && defined(F_SETFD)
2376 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2377 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2378#endif
a0d0e21e
LW
2379
2380 RETPUSHYES;
2381#else
cea2e8a9 2382 DIE(aTHX_ PL_no_sock_func, "socketpair");
a0d0e21e
LW
2383#endif
2384}
2385
2386PP(pp_bind)
2387{
a0d0e21e 2388#ifdef HAS_SOCKET
9cad6237 2389 dSP;
eec2d3df 2390#ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
82b3da69
JH
2391 extern void GETPRIVMODE();
2392 extern void GETUSERMODE();
eec2d3df 2393#endif
748a9306 2394 SV *addrsv = POPs;
349d4f2f
NC
2395 /* OK, so on what platform does bind modify addr? */
2396 const char *addr;
a0d0e21e
LW
2397 GV *gv = (GV*)POPs;
2398 register IO *io = GvIOn(gv);
2399 STRLEN len;
eec2d3df
GS
2400 int bind_ok = 0;
2401#ifdef MPE
2402 int mpeprivmode = 0;
2403#endif
a0d0e21e
LW
2404
2405 if (!io || !IoIFP(io))
2406 goto nuts;
2407
349d4f2f 2408 addr = SvPV_const(addrsv, len);
a0d0e21e 2409 TAINT_PROPER("bind");
eec2d3df
GS
2410#ifdef MPE /* Deal with MPE bind() peculiarities */
2411 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2412 /* The address *MUST* stupidly be zero. */
2413 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2414 /* PRIV mode is required to bind() to ports < 1024. */
2415 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2416 ((struct sockaddr_in *)addr)->sin_port > 0) {
2417 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2418 mpeprivmode = 1;
2419 }
2420 }
2421#endif /* MPE */
2422 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2423 (struct sockaddr *)addr, len) >= 0)
2424 bind_ok = 1;
2425
2426#ifdef MPE /* Switch back to USER mode */
2427 if (mpeprivmode)
2428 GETUSERMODE();
2429#endif /* MPE */
2430
2431 if (bind_ok)
a0d0e21e
LW
2432 RETPUSHYES;
2433 else
2434 RETPUSHUNDEF;
2435
2436nuts:
599cee73 2437 if (ckWARN(WARN_CLOSED))
bc37a18f 2438 report_evil_fh(gv, io, PL_op->op_type);
93189314 2439 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2440 RETPUSHUNDEF;
2441#else
cea2e8a9 2442 DIE(aTHX_ PL_no_sock_func, "bind");
a0d0e21e
LW
2443#endif
2444}
2445
2446PP(pp_connect)
2447{
a0d0e21e 2448#ifdef HAS_SOCKET
9cad6237 2449 dSP;
748a9306 2450 SV *addrsv = POPs;
349d4f2f 2451 const char *addr;
a0d0e21e
LW
2452 GV *gv = (GV*)POPs;
2453 register IO *io = GvIOn(gv);
2454 STRLEN len;
2455
2456 if (!io || !IoIFP(io))
2457 goto nuts;
2458
349d4f2f 2459 addr = SvPV_const(addrsv, len);
a0d0e21e 2460 TAINT_PROPER("connect");
6ad3d225 2461 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
a0d0e21e
LW
2462 RETPUSHYES;
2463 else
2464 RETPUSHUNDEF;
2465
2466nuts:
599cee73 2467 if (ckWARN(WARN_CLOSED))
bc37a18f 2468 report_evil_fh(gv, io, PL_op->op_type);
93189314 2469 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2470 RETPUSHUNDEF;
2471#else
cea2e8a9 2472 DIE(aTHX_ PL_no_sock_func, "connect");
a0d0e21e
LW
2473#endif
2474}
2475
2476PP(pp_listen)
2477{
a0d0e21e 2478#ifdef HAS_SOCKET
9cad6237 2479 dSP;
a0d0e21e
LW
2480 int backlog = POPi;
2481 GV *gv = (GV*)POPs;
c289d2f7 2482 register IO *io = gv ? GvIOn(gv) : NULL;
a0d0e21e 2483
c289d2f7 2484 if (!gv || !io || !IoIFP(io))
a0d0e21e
LW
2485 goto nuts;
2486
6ad3d225 2487 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
a0d0e21e
LW
2488 RETPUSHYES;
2489 else
2490 RETPUSHUNDEF;
2491
2492nuts:
599cee73 2493 if (ckWARN(WARN_CLOSED))
bc37a18f 2494 report_evil_fh(gv, io, PL_op->op_type);
93189314 2495 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2496 RETPUSHUNDEF;
2497#else
cea2e8a9 2498 DIE(aTHX_ PL_no_sock_func, "listen");
a0d0e21e
LW
2499#endif
2500}
2501
2502PP(pp_accept)
2503{
a0d0e21e 2504#ifdef HAS_SOCKET
9cad6237 2505 dSP; dTARGET;
a0d0e21e
LW
2506 GV *ngv;
2507 GV *ggv;
2508 register IO *nstio;
2509 register IO *gstio;
93d47a36
JH
2510 char namebuf[MAXPATHLEN];
2511#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2512 Sock_size_t len = sizeof (struct sockaddr_in);
2513#else
2514 Sock_size_t len = sizeof namebuf;
2515#endif
a0d0e21e
LW
2516 int fd;
2517
2518 ggv = (GV*)POPs;
2519 ngv = (GV*)POPs;
2520
2521 if (!ngv)
2522 goto badexit;
2523 if (!ggv)
2524 goto nuts;
2525
2526 gstio = GvIO(ggv);
2527 if (!gstio || !IoIFP(gstio))
2528 goto nuts;
2529
2530 nstio = GvIOn(ngv);
93d47a36 2531 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
a0d0e21e
LW
2532 if (fd < 0)
2533 goto badexit;
a70048fb
AB
2534 if (IoIFP(nstio))
2535 do_close(ngv, FALSE);
460c8493
IZ
2536 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2537 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
50952442 2538 IoTYPE(nstio) = IoTYPE_SOCKET;
a0d0e21e 2539 if (!IoIFP(nstio) || !IoOFP(nstio)) {
760ac839
LW
2540 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2541 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
6ad3d225 2542 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
a0d0e21e
LW
2543 goto badexit;
2544 }
8d2a6795
GS
2545#if defined(HAS_FCNTL) && defined(F_SETFD)
2546 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2547#endif
a0d0e21e 2548
ed79a026 2549#ifdef EPOC
93d47a36 2550 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
a9f1f6b0 2551 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
ed79a026 2552#endif
381c1bae 2553#ifdef __SCO_VERSION__
93d47a36 2554 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
381c1bae 2555#endif
ed79a026 2556
93d47a36 2557 PUSHp(namebuf, len);
a0d0e21e
LW
2558 RETURN;
2559
2560nuts:
599cee73 2561 if (ckWARN(WARN_CLOSED))
bc37a18f 2562 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
93189314 2563 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2564
2565badexit:
2566 RETPUSHUNDEF;
2567
2568#else
cea2e8a9 2569 DIE(aTHX_ PL_no_sock_func, "accept");
a0d0e21e
LW
2570#endif
2571}
2572
2573PP(pp_shutdown)
2574{
a0d0e21e 2575#ifdef HAS_SOCKET
9cad6237 2576 dSP; dTARGET;
a0d0e21e
LW
2577 int how = POPi;
2578 GV *gv = (GV*)POPs;
2579 register IO *io = GvIOn(gv);
2580
2581 if (!io || !IoIFP(io))
2582 goto nuts;
2583
6ad3d225 2584 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
a0d0e21e
LW
2585 RETURN;
2586
2587nuts:
599cee73 2588 if (ckWARN(WARN_CLOSED))
bc37a18f 2589 report_evil_fh(gv, io, PL_op->op_type);
93189314 2590 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2591 RETPUSHUNDEF;
2592#else
cea2e8a9 2593 DIE(aTHX_ PL_no_sock_func, "shutdown");
a0d0e21e
LW
2594#endif
2595}
2596
a0d0e21e
LW
2597PP(pp_ssockopt)
2598{
a0d0e21e 2599#ifdef HAS_SOCKET
9cad6237 2600 dSP;
533c011a 2601 int optype = PL_op->op_type;
a0d0e21e
LW
2602 SV *sv;
2603 int fd;
2604 unsigned int optname;
2605 unsigned int lvl;
2606 GV *gv;
2607 register IO *io;
1e422769 2608 Sock_size_t len;
a0d0e21e
LW
2609
2610 if (optype == OP_GSOCKOPT)
2611 sv = sv_2mortal(NEWSV(22, 257));
2612 else
2613 sv = POPs;
2614 optname = (unsigned int) POPi;
2615 lvl = (unsigned int) POPi;
2616
2617 gv = (GV*)POPs;
2618 io = GvIOn(gv);
2619 if (!io || !IoIFP(io))
2620 goto nuts;
2621
760ac839 2622 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2623 switch (optype) {
2624 case OP_GSOCKOPT:
748a9306 2625 SvGROW(sv, 257);
a0d0e21e 2626 (void)SvPOK_only(sv);
748a9306
LW
2627 SvCUR_set(sv,256);
2628 *SvEND(sv) ='\0';
1e422769 2629 len = SvCUR(sv);
6ad3d225 2630 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
a0d0e21e 2631 goto nuts2;
1e422769 2632 SvCUR_set(sv, len);
748a9306 2633 *SvEND(sv) ='\0';
a0d0e21e
LW
2634 PUSHs(sv);
2635 break;
2636 case OP_SSOCKOPT: {
1215b447
JH
2637#if defined(__SYMBIAN32__)
2638# define SETSOCKOPT_OPTION_VALUE_T void *
2639#else
2640# define SETSOCKOPT_OPTION_VALUE_T const char *
2641#endif
2642 /* XXX TODO: We need to have a proper type (a Configure probe,
2643 * etc.) for what the C headers think of the third argument of
2644 * setsockopt(), the option_value read-only buffer: is it
2645 * a "char *", or a "void *", const or not. Some compilers
2646 * don't take kindly to e.g. assuming that "char *" implicitly
2647 * promotes to a "void *", or to explicitly promoting/demoting
2648 * consts to non/vice versa. The "const void *" is the SUS
2649 * definition, but that does not fly everywhere for the above
2650 * reasons. */
2651 SETSOCKOPT_OPTION_VALUE_T buf;
1e422769
PP
2652 int aint;
2653 if (SvPOKp(sv)) {
2d8e6c8d 2654 STRLEN l;
1215b447 2655 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2d8e6c8d 2656 len = l;
1e422769 2657 }
56ee1660 2658 else {
a0d0e21e 2659 aint = (int)SvIV(sv);
1215b447 2660 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
a0d0e21e
LW
2661 len = sizeof(int);
2662 }
6ad3d225 2663 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
a0d0e21e 2664 goto nuts2;
3280af22 2665 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2666 }
2667 break;
2668 }
2669 RETURN;
2670
2671nuts:
599cee73 2672 if (ckWARN(WARN_CLOSED))
bc37a18f 2673 report_evil_fh(gv, io, optype);
93189314 2674 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2675nuts2:
2676 RETPUSHUNDEF;
2677
2678#else
af51a00e 2679 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
a0d0e21e
LW
2680#endif
2681}
2682
a0d0e21e
LW
2683PP(pp_getpeername)
2684{
a0d0e21e 2685#ifdef HAS_SOCKET
9cad6237 2686 dSP;
533c011a 2687 int optype = PL_op->op_type;
a0d0e21e
LW
2688 SV *sv;
2689 int fd;
2690 GV *gv = (GV*)POPs;
2691 register IO *io = GvIOn(gv);
1e422769 2692 Sock_size_t len;
a0d0e21e
LW
2693
2694 if (!io || !IoIFP(io))
2695 goto nuts;
2696
2697 sv = sv_2mortal(NEWSV(22, 257));
748a9306 2698 (void)SvPOK_only(sv);
1e422769
PP
2699 len = 256;
2700 SvCUR_set(sv, len);
748a9306 2701 *SvEND(sv) ='\0';
760ac839 2702 fd = PerlIO_fileno(IoIFP(io));
a0d0e21e
LW
2703 switch (optype) {
2704 case OP_GETSOCKNAME:
6ad3d225 2705 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e
LW
2706 goto nuts2;
2707 break;
2708 case OP_GETPEERNAME:
6ad3d225 2709 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
a0d0e21e 2710 goto nuts2;
490ab354
JH
2711#if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2712 {
2713 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";
2714 /* If the call succeeded, make sure we don't have a zeroed port/addr */
349d4f2f 2715 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2fbb330f 2716 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
490ab354 2717 sizeof(u_short) + sizeof(struct in_addr))) {
301e8125 2718 goto nuts2;
490ab354
JH
2719 }
2720 }
2721#endif
a0d0e21e
LW
2722 break;
2723 }
13826f2c
CS
2724#ifdef BOGUS_GETNAME_RETURN
2725 /* Interactive Unix, getpeername() and getsockname()
2726 does not return valid namelen */
1e422769
PP
2727 if (len == BOGUS_GETNAME_RETURN)
2728 len = sizeof(struct sockaddr);
13826f2c 2729#endif
1e422769 2730 SvCUR_set(sv, len);
748a9306 2731 *SvEND(sv) ='\0';
a0d0e21e
LW
2732 PUSHs(sv);
2733 RETURN;
2734
2735nuts:
599cee73 2736 if (ckWARN(WARN_CLOSED))
bc37a18f 2737 report_evil_fh(gv, io, optype);
93189314 2738 SETERRNO(EBADF,SS_IVCHAN);
a0d0e21e
LW
2739nuts2:
2740 RETPUSHUNDEF;
2741
2742#else
af51a00e 2743 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
a0d0e21e
LW
2744#endif
2745}
2746
2747/* Stat calls. */
2748
a0d0e21e
LW
2749PP(pp_stat)
2750{
39644a26 2751 dSP;
2dd78f96 2752 GV *gv;
54310121 2753 I32 gimme;
a0d0e21e
LW
2754 I32 max = 13;
2755
533c011a 2756 if (PL_op->op_flags & OPf_REF) {
2dd78f96 2757 gv = cGVOP_gv;
8a4e5b40 2758 if (PL_op->op_type == OP_LSTAT) {
5d3e98de 2759 if (gv != PL_defgv) {
5d329e6e 2760 do_fstat_warning_check:
5d3e98de 2761 if (ckWARN(WARN_IO))
9014280d 2762 Perl_warner(aTHX_ packWARN(WARN_IO),
5d3e98de
RGS
2763 "lstat() on filehandle %s", GvENAME(gv));
2764 } else if (PL_laststype != OP_LSTAT)
8a4e5b40 2765 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
8a4e5b40
DD
2766 }
2767
748a9306 2768 do_fstat:
2dd78f96 2769 if (gv != PL_defgv) {
3280af22 2770 PL_laststype = OP_STAT;
2dd78f96 2771 PL_statgv = gv;
c69006e4 2772 sv_setpvn(PL_statname, "", 0);
2dd78f96
JH
2773 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2774 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
a0d0e21e 2775 }
9ddeeac9 2776 if (PL_laststatval < 0) {
2dd78f96
JH
2777 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2778 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
a0d0e21e 2779 max = 0;
9ddeeac9 2780 }
a0d0e21e
LW
2781 }
2782 else {
748a9306
LW
2783 SV* sv = POPs;
2784 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 2785 gv = (GV*)sv;
748a9306
LW
2786 goto do_fstat;
2787 }
2788 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 2789 gv = (GV*)SvRV(sv);
5d329e6e
NC
2790 if (PL_op->op_type == OP_LSTAT)
2791 goto do_fstat_warning_check;
748a9306
LW
2792 goto do_fstat;
2793 }
0510663f 2794 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3280af22 2795 PL_statgv = Nullgv;
533c011a
NIS
2796 PL_laststype = PL_op->op_type;
2797 if (PL_op->op_type == OP_LSTAT)
0510663f 2798 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
a0d0e21e 2799 else
0510663f 2800 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
3280af22 2801 if (PL_laststatval < 0) {
0510663f 2802 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
9014280d 2803 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
a0d0e21e
LW
2804 max = 0;
2805 }
2806 }
2807
54310121
PP
2808 gimme = GIMME_V;
2809 if (gimme != G_ARRAY) {
2810 if (gimme != G_VOID)
2811 XPUSHs(boolSV(max));
2812 RETURN;
a0d0e21e
LW
2813 }
2814 if (max) {
36477c24
PP
2815 EXTEND(SP, max);
2816 EXTEND_MORTAL(max);
1ff81528
PL
2817 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2818 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
b448e4fe
JH
2819 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2820 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
146174a9
CB
2821#if Uid_t_size > IVSIZE
2822 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2823#else
23dcd6c8 2824# if Uid_t_sign <= 0
1ff81528 2825 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
23dcd6c8
JH
2826# else
2827 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2828# endif
146174a9 2829#endif
301e8125 2830#if Gid_t_size > IVSIZE
146174a9
CB
2831 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2832#else
23dcd6c8 2833# if Gid_t_sign <= 0
1ff81528 2834 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
23dcd6c8
JH
2835# else
2836 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2837# endif
146174a9 2838#endif
cbdc8872 2839#ifdef USE_STAT_RDEV
1ff81528 2840 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
cbdc8872 2841#else
79cb57f6 2842 PUSHs(sv_2mortal(newSVpvn("", 0)));
cbdc8872 2843#endif
146174a9 2844#if Off_t_size > IVSIZE
4a9d6100 2845 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
146174a9 2846#else
1ff81528 2847 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
146174a9 2848#endif
cbdc8872 2849#ifdef BIG_TIME
172ae379
JH
2850 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2851 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2852 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
cbdc8872 2853#else
1ff81528
PL
2854 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2855 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2856 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
cbdc8872 2857#endif
a0d0e21e 2858#ifdef USE_STAT_BLOCKS
b448e4fe
JH
2859 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2860 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
a0d0e21e 2861#else
79cb57f6
GS
2862 PUSHs(sv_2mortal(newSVpvn("", 0)));
2863 PUSHs(sv_2mortal(newSVpvn("", 0)));
a0d0e21e
LW
2864#endif
2865 }
2866 RETURN;
2867}
2868
fbb0b3b3
RGS
2869/* This macro is used by the stacked filetest operators :
2870 * if the previous filetest failed, short-circuit and pass its value.
2871 * Else, discard it from the stack and continue. --rgs
2872 */
2873#define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2874 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2875 else { (void)POPs; PUTBACK; } \
2876 }
2877
a0d0e21e
LW
2878PP(pp_ftrread)
2879{
9cad6237 2880 I32 result;
af9e49b4
NC
2881 /* Not const, because things tweak this below. Not bool, because there's
2882 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2883#if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2884 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2885 /* Giving some sort of initial value silences compilers. */
2886# ifdef R_OK
2887 int access_mode = R_OK;
2888# else
2889 int access_mode = 0;
2890# endif
5ff3f7a4 2891#else
af9e49b4
NC
2892 /* access_mode is never used, but leaving use_access in makes the
2893 conditional compiling below much clearer. */
2894 I32 use_access = 0;
5ff3f7a4 2895#endif
af9e49b4 2896 int stat_mode = S_IRUSR;
a0d0e21e 2897
af9e49b4 2898 bool effective = FALSE;
2a3ff820 2899 dSP;
af9e49b4 2900
fbb0b3b3 2901 STACKED_FTEST_CHECK;
af9e49b4
NC
2902
2903 switch (PL_op->op_type) {
2904 case OP_FTRREAD:
2905#if !(defined(HAS_ACCESS) && defined(R_OK))
2906 use_access = 0;
2907#endif
2908 break;
2909
2910 case OP_FTRWRITE:
5ff3f7a4 2911#if defined(HAS_ACCESS) && defined(W_OK)
af9e49b4 2912 access_mode = W_OK;
5ff3f7a4 2913#else
af9e49b4 2914 use_access = 0;
5ff3f7a4 2915#endif
af9e49b4
NC
2916 stat_mode = S_IWUSR;
2917 break;
a0d0e21e 2918
af9e49b4 2919 case OP_FTREXEC:
5ff3f7a4 2920#if defined(HAS_ACCESS) && defined(X_OK)
af9e49b4 2921 access_mode = X_OK;
5ff3f7a4 2922#else
af9e49b4 2923 use_access = 0;
5ff3f7a4 2924#endif
af9e49b4
NC
2925 stat_mode = S_IXUSR;
2926 break;
a0d0e21e 2927
af9e49b4 2928 case OP_FTEWRITE:
faee0e31 2929#ifdef PERL_EFF_ACCESS
af9e49b4 2930 access_mode = W_OK;
5ff3f7a4 2931#endif
af9e49b4
NC
2932 stat_mode = S_IWUSR;
2933 /* Fall through */
a0d0e21e 2934
af9e49b4
NC
2935 case OP_FTEREAD:
2936#ifndef PERL_EFF_ACCESS
2937 use_access = 0;
2938#endif
2939 effective = TRUE;
2940 break;
2941
2942
2943 case OP_FTEEXEC:
faee0e31 2944#ifdef PERL_EFF_ACCESS
af9e49b4 2945 access_mode = W_OK;
5ff3f7a4 2946#else
af9e49b4 2947 use_access = 0;
5ff3f7a4 2948#endif
af9e49b4
NC
2949 stat_mode = S_IXUSR;
2950 effective = TRUE;
2951 break;
2952 }
a0d0e21e 2953
af9e49b4
NC
2954 if (use_access) {
2955#if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2956 const char *const name = POPpx;
2957 if (effective) {
2958# ifdef PERL_EFF_ACCESS
2959 result = PERL_EFF_ACCESS(name, access_mode);
2960# else
2961 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
2962 OP_NAME(PL_op));
2963# endif
2964 }
2965 else {
2966# ifdef HAS_ACCESS
2967 result = access(name, access_mode);
2968# else
2969 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
2970# endif
2971 }
5ff3f7a4
GS
2972 if (result == 0)
2973 RETPUSHYES;
2974 if (result < 0)
2975 RETPUSHUNDEF;
2976 RETPUSHNO;
af9e49b4 2977#endif
22865c03 2978 }
af9e49b4 2979
cea2e8a9 2980 result = my_stat();
22865c03 2981 SPAGAIN;
a0d0e21e
LW
2982 if (result < 0)
2983 RETPUSHUNDEF;
af9e49b4 2984 if (cando(stat_mode, effective, &PL_statcache))
a0d0e21e
LW
2985 RETPUSHYES;
2986 RETPUSHNO;
2987}
2988
2989PP(pp_ftis)
2990{
fbb0b3b3 2991 I32 result;
d7f0a2f4 2992 const int op_type = PL_op->op_type;
2a3ff820 2993 dSP;
fbb0b3b3
RGS
2994 STACKED_FTEST_CHECK;
2995 result = my_stat();
2996 SPAGAIN;
a0d0e21e
LW
2997 if (result < 0)
2998 RETPUSHUNDEF;
d7f0a2f4
NC
2999 if (op_type == OP_FTIS)
3000 RETPUSHYES;
957b0e1d 3001 {
d7f0a2f4
NC
3002 /* You can't dTARGET inside OP_FTIS, because you'll get
3003 "panic: pad_sv po" - the op is not flagged to have a target. */
957b0e1d 3004 dTARGET;
d7f0a2f4 3005 switch (op_type) {
957b0e1d
NC
3006 case OP_FTSIZE:
3007#if Off_t_size > IVSIZE
3008 PUSHn(PL_statcache.st_size);
3009#else
3010 PUSHi(PL_statcache.st_size);
3011#endif
3012 break;
3013 case OP_FTMTIME:
3014 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3015 break;
3016 case OP_FTATIME:
3017 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3018 break;
3019 case OP_FTCTIME:
3020 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3021 break;
3022 }
3023 }
3024 RETURN;
a0d0e21e
LW
3025}
3026
a0d0e21e
LW
3027PP(pp_ftrowned)
3028{
fbb0b3b3 3029 I32 result;
2a3ff820 3030 dSP;
17ad201a
NC
3031
3032 /* I believe that all these three are likely to be defined on most every
3033 system these days. */
3034#ifndef S_ISUID
3035 if(PL_op->op_type == OP_FTSUID)
3036 RETPUSHNO;
3037#endif
3038#ifndef S_ISGID
3039 if(PL_op->op_type == OP_FTSGID)
3040 RETPUSHNO;
3041#endif
3042#ifndef S_ISVTX
3043 if(PL_op->op_type == OP_FTSVTX)
3044 RETPUSHNO;
3045#endif
3046
fbb0b3b3
RGS
3047 STACKED_FTEST_CHECK;
3048 result = my_stat();
3049 SPAGAIN;
a0d0e21e
LW
3050 if (result < 0)
3051 RETPUSHUNDEF;
f1cb2d48
NC
3052 switch (PL_op->op_type) {
3053 case OP_FTROWNED:
9ab9fa88 3054 if (PL_statcache.st_uid == PL_uid)
f1cb2d48
NC
3055 RETPUSHYES;
3056 break;
3057 case OP_FTEOWNED:
3058 if (PL_statcache.st_uid == PL_euid)
3059 RETPUSHYES;
3060 break;
3061 case OP_FTZERO:
3062 if (PL_statcache.st_size == 0)
3063 RETPUSHYES;
3064 break;
3065 case OP_FTSOCK:
3066 if (S_ISSOCK(PL_statcache.st_mode))
3067 RETPUSHYES;
3068 break;
3069 case OP_FTCHR:
3070 if (S_ISCHR(PL_statcache.st_mode))
3071 RETPUSHYES;
3072 break;
3073 case OP_FTBLK:
3074 if (S_ISBLK(PL_statcache.st_mode))
3075 RETPUSHYES;
3076 break;
3077 case OP_FTFILE:
3078 if (S_ISREG(PL_statcache.st_mode))
3079 RETPUSHYES;
3080 break;
3081 case OP_FTDIR:
3082 if (S_ISDIR(PL_statcache.st_mode))
3083 RETPUSHYES;
3084 break;
3085 case OP_FTPIPE:
3086 if (S_ISFIFO(PL_statcache.st_mode))
3087 RETPUSHYES;
3088 break;
a0d0e21e 3089#ifdef S_ISUID
17ad201a
NC
3090 case OP_FTSUID:
3091 if (PL_statcache.st_mode & S_ISUID)
3092 RETPUSHYES;
3093 break;
a0d0e21e 3094#endif
a0d0e21e 3095#ifdef S_ISGID
17ad201a
NC
3096 case OP_FTSGID:
3097 if (PL_statcache.st_mode & S_ISGID)
3098 RETPUSHYES;
3099 break;
3100#endif
3101#ifdef S_ISVTX
3102 case OP_FTSVTX:
3103 if (PL_statcache.st_mode & S_ISVTX)
3104 RETPUSHYES;
3105 break;
a0d0e21e 3106#endif
17ad201a 3107 }
a0d0e21e
LW
3108 RETPUSHNO;
3109}
3110
17ad201a 3111PP(pp_ftlink)
a0d0e21e 3112{
17ad201a 3113 I32 result = my_lstat();
39644a26 3114 dSP;
a0d0e21e
LW
3115 if (result < 0)
3116 RETPUSHUNDEF;
17ad201a 3117 if (S_ISLNK(PL_statcache.st_mode))
a0d0e21e 3118 RETPUSHYES;
a0d0e21e
LW
3119 RETPUSHNO;
3120}
3121
3122PP(pp_fttty)
3123{
39644a26 3124 dSP;
a0d0e21e
LW
3125 int fd;
3126 GV *gv;
7a5fd60d 3127 SV *tmpsv = Nullsv;
fb73857a 3128
fbb0b3b3
RGS
3129 STACKED_FTEST_CHECK;
3130
533c011a 3131 if (PL_op->op_flags & OPf_REF)
146174a9 3132 gv = cGVOP_gv;
fb73857a
PP
3133 else if (isGV(TOPs))
3134 gv = (GV*)POPs;
3135 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3136 gv = (GV*)SvRV(POPs);
a0d0e21e 3137 else
7a5fd60d 3138 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
fb73857a 3139
a0d0e21e 3140 if (GvIO(gv) && IoIFP(GvIOp(gv)))
760ac839 3141 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
7a5fd60d 3142 else if (tmpsv && SvOK(tmpsv)) {
349d4f2f 3143 const char *tmps = SvPV_nolen_const(tmpsv);
7a5fd60d
NC
3144 if (isDIGIT(*tmps))
3145 fd = atoi(tmps);
3146 else
3147 RETPUSHUNDEF;
3148 }
a0d0e21e
LW
3149 else
3150 RETPUSHUNDEF;
6ad3d225 3151 if (PerlLIO_isatty(fd))
a0d0e21e
LW
3152 RETPUSHYES;
3153 RETPUSHNO;
3154}
3155
16d20bd9
AD
3156#if defined(atarist) /* this will work with atariST. Configure will
3157 make guesses for other systems. */
3158# define FILE_base(f) ((f)->_base)
3159# define FILE_ptr(f) ((f)->_ptr)
3160# define FILE_cnt(f) ((f)->_cnt)
3161# define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
a0d0e21e
LW
3162#endif
3163
3164PP(pp_fttext)
3165{
39644a26 3166 dSP;
a0d0e21e
LW
3167 I32 i;
3168 I32 len;
3169 I32 odd = 0;
3170 STDCHAR tbuf[512];
3171 register STDCHAR *s;
3172 register IO *io;
5f05dabc
PP
3173 register SV *sv;
3174 GV *gv;
146174a9 3175 PerlIO *fp;
a0d0e21e 3176
fbb0b3b3
RGS
3177 STACKED_FTEST_CHECK;
3178
533c011a 3179 if (PL_op->op_flags & OPf_REF)
146174a9 3180 gv = cGVOP_gv;
5f05dabc
PP
3181 else if (isGV(TOPs))
3182 gv = (GV*)POPs;
3183 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3184 gv = (GV*)SvRV(POPs);
3185 else
3186 gv = Nullgv;
3187
3188 if (gv) {
a0d0e21e 3189 EXTEND(SP, 1);
3280af22
NIS
3190 if (gv == PL_defgv) {
3191 if (PL_statgv)
3192 io = GvIO(PL_statgv);
a0d0e21e 3193 else {
3280af22 3194 sv = PL_statname;
a0d0e21e
LW
3195 goto really_filename;
3196 }
3197 }
3198 else {
3280af22
NIS
3199 PL_statgv = gv;
3200 PL_laststatval = -1;
c69006e4 3201 sv_setpvn(PL_statname, "", 0);
3280af22 3202 io = GvIO(PL_statgv);
a0d0e21e
LW
3203 }
3204 if (io && IoIFP(io)) {
5f05dabc 3205 if (! PerlIO_has_base(IoIFP(io)))
cea2e8a9 3206 DIE(aTHX_ "-T and -B not implemented on filehandles");
3280af22
NIS
3207 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3208 if (PL_laststatval < 0)
5f05dabc 3209 RETPUSHUNDEF;
9cbac4c7 3210 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
533c011a 3211 if (PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3212 RETPUSHNO;
3213 else
3214 RETPUSHYES;
9cbac4c7 3215 }
a20bf0c3 3216 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
760ac839 3217 i = PerlIO_getc(IoIFP(io));
a0d0e21e 3218 if (i != EOF)
760ac839 3219 (void)PerlIO_ungetc(IoIFP(io),i);
a0d0e21e 3220 }
a20bf0c3 3221 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
a0d0e21e 3222 RETPUSHYES;
a20bf0c3
JH
3223 len = PerlIO_get_bufsiz(IoIFP(io));
3224 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
760ac839
LW
3225 /* sfio can have large buffers - limit to 512 */
3226 if (len > 512)
3227 len = 512;
a0d0e21e
LW
3228 }
3229 else {
2dd78f96 3230 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
146174a9 3231 gv = cGVOP_gv;
2dd78f96 3232 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
146174a9 3233 }
93189314 3234 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
3235 RETPUSHUNDEF;
3236 }
3237 }
3238 else {
3239 sv = POPs;
5f05dabc 3240 really_filename:
3280af22 3241 PL_statgv = Nullgv;
5c9aa243 3242 PL_laststype = OP_STAT;
d5263905 3243 sv_setpv(PL_statname, SvPV_nolen_const(sv));
aa07b2f6 3244 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
349d4f2f
NC
3245 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3246 '\n'))
9014280d 3247 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
a0d0e21e
LW
3248 RETPUSHUNDEF;
3249 }
146174a9
CB
3250 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3251 if (PL_laststatval < 0) {
3252 (void)PerlIO_close(fp);
5f05dabc 3253 RETPUSHUNDEF;
146174a9 3254 }
60382766 3255 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
146174a9
CB
3256 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3257 (void)PerlIO_close(fp);
a0d0e21e 3258 if (len <= 0) {
533c011a 3259 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
a0d0e21e
LW
3260 RETPUSHNO; /* special case NFS directories */
3261 RETPUSHYES; /* null file is anything */
3262 }
3263 s = tbuf;
3264 }
3265
3266 /* now scan s to look for textiness */
4633a7c4 3267 /* XXX ASCII dependent code */
a0d0e21e 3268
146174a9
CB
3269#if defined(DOSISH) || defined(USEMYBINMODE)
3270 /* ignore trailing ^Z on short files */
3271 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3272 --len;
3273#endif
3274
a0d0e21e
LW
3275 for (i = 0; i < len; i++, s++) {
3276 if (!*s) { /* null never allowed in text */
3277 odd += len;
3278 break;
3279 }
9d116dd7 3280#ifdef EBCDIC
301e8125 3281 else if (!(isPRINT(*s) || isSPACE(*s)))
9d116dd7
JH
3282 odd++;
3283#else
146174a9
CB
3284 else if (*s & 128) {
3285#ifdef USE_LOCALE
2de3dbcc 3286 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
b3f66c68
GS
3287 continue;
3288#endif
3289 /* utf8 characters don't count as odd */
fd400ab9 3290 if (UTF8_IS_START(*s)) {
b3f66c68
GS
3291 int ulen = UTF8SKIP(s);
3292 if (ulen < len - i) {
3293 int j;
3294 for (j = 1; j < ulen; j++) {
fd400ab9 3295 if (!UTF8_IS_CONTINUATION(s[j]))
b3f66c68
GS
3296 goto not_utf8;
3297 }
3298 --ulen; /* loop does extra increment */
3299 s += ulen;
3300 i += ulen;
3301 continue;
3302 }
3303 }
3304 not_utf8:
3305 odd++;
146174a9 3306 }
a0d0e21e
LW
3307 else if (*s < 32 &&
3308 *s != '\n' && *s != '\r' && *s != '\b' &&
3309 *s != '\t' && *s != '\f' && *s != 27)
3310 odd++;
9d116dd7 3311#endif
a0d0e21e
LW
3312 }
3313
533c011a 3314 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
a0d0e21e
LW
3315 RETPUSHNO;
3316 else
3317 RETPUSHYES;
3318}
3319
a0d0e21e
LW
3320/* File calls. */
3321
3322PP(pp_chdir)
3323{
39644a26 3324 dSP; dTARGET;
c4aca7d0 3325 const char *tmps = 0;
9a957fbc 3326 GV *gv = NULL;
a0d0e21e 3327
c4aca7d0 3328 if( MAXARG == 1 ) {
9a957fbc 3329 SV * const sv = POPs;
c4aca7d0
GA
3330 if (SvTYPE(sv) == SVt_PVGV) {
3331 gv = (GV*)sv;
3332 }
3333 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3334 gv = (GV*)SvRV(sv);
3335 }
3336 else {
3337 tmps = SvPVx_nolen_const(sv);
3338 }
3339 }
35ae6b54 3340
c4aca7d0 3341 if( !gv && (!tmps || !*tmps) ) {
9a957fbc
AL
3342 HV * const table = GvHVn(PL_envgv);
3343 SV **svp;
3344
3345 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3346 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
491527d0 3347#ifdef VMS
9a957fbc 3348 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
491527d0 3349#endif
35ae6b54
MS
3350 )
3351 {
3352 if( MAXARG == 1 )
9014280d 3353 deprecate("chdir('') or chdir(undef) as chdir()");
8c074e2a 3354 tmps = SvPV_nolen_const(*svp);
35ae6b54 3355 }
72f496dc 3356 else {
389ec635 3357 PUSHi(0);
b7ab37f8 3358 TAINT_PROPER("chdir");
389ec635
MS
3359 RETURN;
3360 }
8ea155d1 3361 }
8ea155d1 3362
a0d0e21e 3363 TAINT_PROPER("chdir");
c4aca7d0
GA
3364 if (gv) {
3365#ifdef HAS_FCHDIR
9a957fbc 3366 IO* const io = GvIO(gv);
c4aca7d0
GA
3367 if (io) {
3368 if (IoIFP(io)) {
3369 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3370 }
3371 else if (IoDIRP(io)) {
3372#ifdef HAS_DIRFD
3373 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3374#else
0f1f2428 3375 DIE(aTHX_ PL_no_func, "dirfd");
c4aca7d0
GA
3376#endif
3377 }
3378 else {
3379 PUSHi(0);
3380 }
3381 }
3382 else {
3383 PUSHi(0);
3384 }
3385#else
3386 DIE(aTHX_ PL_no_func, "fchdir");
3387#endif
3388 }
3389 else
b8ffc8df 3390 PUSHi( PerlDir_chdir(tmps) >= 0 );
748a9306
LW
3391#ifdef VMS
3392 /* Clear the DEFAULT element of ENV so we'll get the new value
3393 * in the future. */
6b88bc9c 3394 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
748a9306 3395#endif
a0d0e21e
LW
3396 RETURN;
3397}
3398
3399PP(pp_chown)
3400{
76ffd3b9 3401 dSP; dMARK; dTARGET;
605b9385 3402 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
76ffd3b9 3403
a0d0e21e
LW
3404 SP = MARK;
3405 PUSHi(value);
3406 RETURN;
a0d0e21e
LW
3407}
3408
3409PP(pp_chroot)
3410{
a0d0e21e 3411#ifdef HAS_CHROOT
76ffd3b9 3412 dSP; dTARGET;
d05c1ba0 3413 char *tmps = POPpx;
a0d0e21e
LW
3414 TAINT_PROPER("chroot");
3415 PUSHi( chroot(tmps) >= 0 );
3416 RETURN;
3417#else
cea2e8a9 3418 DIE(aTHX_ PL_no_func, "chroot");
a0d0e21e
LW
3419#endif
3420}
3421
a0d0e21e
LW
3422PP(pp_rename)
3423{
39644a26 3424 dSP; dTARGET;
a0d0e21e 3425 int anum;
e62f0680
NC
3426 const char *tmps2 = POPpconstx;
3427 const char *tmps = SvPV_nolen_const(TOPs);
a0d0e21e
LW
3428 TAINT_PROPER("rename");
3429#ifdef HAS_RENAME
baed7233 3430 anum = PerlLIO_rename(tmps, tmps2);
a0d0e21e 3431#else
6b88bc9c 3432 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
ed969818
WK
3433 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3434 anum = 1;
3435 else {
3654eb6c 3436 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
ed969818
WK
3437 (void)UNLINK(tmps2);
3438 if (!(anum = link(tmps, tmps2)))
3439 anum = UNLINK(tmps);
3440 }
a0d0e21e
LW
3441 }
3442#endif
3443 SETi( anum >= 0 );
3444 RETURN;
3445}
3446
ce6987d0 3447#if defined(HAS_LINK) || defined(HAS_SYMLINK)
a0d0e21e
LW
3448PP(pp_link)
3449{
370f6000 3450 dSP; dTARGET;
ce6987d0
NC
3451 const int op_type = PL_op->op_type;
3452 int result;
a0d0e21e 3453
ce6987d0
NC
3454# ifndef HAS_LINK
3455 if (op_type == OP_LINK)
3456 DIE(aTHX_ PL_no_func, "link");
3457# endif
3458# ifndef HAS_SYMLINK
3459 if (op_type == OP_SYMLINK)
3460 DIE(aTHX_ PL_no_func, "symlink");
3461# endif
3462
3463 {
3464 const char *tmps2 = POPpconstx;
3465 const char *tmps = SvPV_nolen_const(TOPs);
3466 TAINT_PROPER(PL_op_desc[op_type]);
3467 result =
3468# if defined(HAS_LINK)
3469# if defined(HAS_SYMLINK)
3470 /* Both present - need to choose which. */
3471 (op_type == OP_LINK) ?
3472 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3473# else
4a8ebb7f
SH
3474 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3475 PerlLIO_link(tmps, tmps2);
ce6987d0
NC
3476# endif
3477# else
3478# if defined(HAS_SYMLINK)
4a8ebb7f
SH
3479 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3480 symlink(tmps, tmps2);
ce6987d0
NC
3481# endif
3482# endif
3483 }
3484
3485 SETi( result >= 0 );
a0d0e21e 3486 RETURN;
ce6987d0 3487}
a0d0e21e 3488#else
ce6987d0
NC
3489PP(pp_link)
3490<