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