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