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