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