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