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