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