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