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