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