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