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