3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
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.
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.
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.
24 * By 'system', we mean ops which interact with the OS, such as pp_open().
28 #define PERL_IN_PP_SYS_C
32 /* Shadow password support for solaris - pdo@cs.umd.edu
33 * Not just Solaris: at least HP-UX, IRIX, Linux.
34 * The API is from SysV.
36 * There are at least two more shadow interfaces,
37 * see the comments in pp_gpwent().
41 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
42 * and another MAXINT from "perl.h" <- <sys/param.h>. */
49 # include <sys/wait.h>
53 # include <sys/resource.h>
62 # include <sys/select.h>
66 /* XXX Configure test needed.
67 h_errno might not be a simple 'int', especially for multi-threaded
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>.
73 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
82 struct passwd *getpwnam (char *);
83 struct passwd *getpwuid (Uid_t);
88 struct passwd *getpwent (void);
89 #elif defined (VMS) && defined (my_getpwent)
90 struct passwd *Perl_my_getpwent (pTHX);
99 struct group *getgrnam (char *);
100 struct group *getgrgid (Gid_t);
104 struct group *getgrent (void);
110 # if defined(_MSC_VER) || defined(__MINGW32__)
111 # include <sys/utime.h>
118 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
121 # define my_chsize PerlLIO_chsize
124 # define my_chsize PerlLIO_chsize
126 I32 my_chsize(int fd, Off_t length);
132 #else /* no flock() */
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)
142 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
143 # define FLOCK fcntl_emulate_flock
144 # define FCNTL_EMULATE_FLOCK
145 # else /* no flock() or fcntl(F_SETLK,...) */
147 # define FLOCK lockf_emulate_flock
148 # define LOCKF_EMULATE_FLOCK
150 # endif /* no flock() or fcntl(F_SETLK,...) */
153 static int FLOCK (int, int);
156 * These are the flock() constants. Since this sytems doesn't have
157 * flock(), the values of the constants are probably not available.
171 # endif /* emulating flock() */
173 #endif /* no flock() */
176 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
178 #if defined(I_SYS_ACCESS) && !defined(R_OK)
179 # include <sys/access.h>
182 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
183 # define FD_CLOEXEC 1 /* NeXT needs this */
189 /* Missing protos on LynxOS */
190 void sethostent(int);
191 void endhostent(void);
193 void endnetent(void);
194 void setprotoent(int);
195 void endprotoent(void);
196 void setservent(int);
197 void endservent(void);
200 #if defined(__osf__) && !defined(_XOPEN_SOURCE_EXTENDED)
201 extern int readlink(const char *, char *, size_t);
202 extern int fchdir(int);
205 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
207 /* AIX 5.2 and below use mktime for localtime, and defines the edge case
208 * for time 0x7fffffff to be valid only in UTC. AIX 5.3 provides localtime64
209 * available in the 32bit environment, which could warrant Configure
210 * checks in the future.
213 #define LOCALTIME_EDGECASE_BROKEN
216 /* F_OK unused: if stat() cannot find it... */
218 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
219 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
220 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
223 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
224 # ifdef I_SYS_SECURITY
225 # include <sys/security.h>
229 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
232 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
236 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
238 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
242 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
243 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
244 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
247 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
249 const Uid_t ruid = getuid();
250 const Uid_t euid = geteuid();
251 const Gid_t rgid = getgid();
252 const Gid_t egid = getegid();
256 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
257 Perl_croak(aTHX_ "switching effective uid is not implemented");
260 if (setreuid(euid, ruid))
263 if (setresuid(euid, ruid, (Uid_t)-1))
266 Perl_croak(aTHX_ "entering effective uid failed");
269 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
270 Perl_croak(aTHX_ "switching effective gid is not implemented");
273 if (setregid(egid, rgid))
276 if (setresgid(egid, rgid, (Gid_t)-1))
279 Perl_croak(aTHX_ "entering effective gid failed");
282 res = access(path, mode);
285 if (setreuid(ruid, euid))
288 if (setresuid(ruid, euid, (Uid_t)-1))
291 Perl_croak(aTHX_ "leaving effective uid failed");
294 if (setregid(rgid, egid))
297 if (setresgid(rgid, egid, (Gid_t)-1))
300 Perl_croak(aTHX_ "leaving effective gid failed");
305 # define PERL_EFF_ACCESS(p,f) (emulate_eaccess((p), (f)))
308 #if !defined(PERL_EFF_ACCESS)
309 /* With it or without it: anyway you get a warning: either that
310 it is unused, or it is declared static and never defined.
313 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
315 PERL_UNUSED_ARG(path);
316 PERL_UNUSED_ARG(mode);
317 Perl_croak(aTHX_ "switching effective uid is not implemented");
327 const char * const tmps = POPpconstx;
328 const I32 gimme = GIMME_V;
329 const char *mode = "r";
332 if (PL_op->op_private & OPpOPEN_IN_RAW)
334 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
336 fp = PerlProc_popen(tmps, mode);
338 const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
340 PerlIO_apply_layers(aTHX_ fp,mode,type);
342 if (gimme == G_VOID) {
344 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
347 else if (gimme == G_SCALAR) {
350 PL_rs = &PL_sv_undef;
351 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
352 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
360 SV * const sv = newSV(79);
361 if (sv_gets(sv, fp, 0) == NULL) {
365 XPUSHs(sv_2mortal(sv));
366 if (SvLEN(sv) - SvCUR(sv) > 20) {
367 SvPV_shrink_to_cur(sv);
372 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
373 TAINT; /* "I believe that this is not gratuitous!" */
376 STATUS_NATIVE_CHILD_SET(-1);
377 if (gimme == G_SCALAR)
388 tryAMAGICunTARGET(iter, -1);
390 /* Note that we only ever get here if File::Glob fails to load
391 * without at the same time croaking, for some reason, or if
392 * perl was built with PERL_EXTERNAL_GLOB */
399 * The external globbing program may use things we can't control,
400 * so for security reasons we must assume the worst.
403 taint_proper(PL_no_security, "glob");
407 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
408 PL_last_in_gv = (GV*)*PL_stack_sp--;
410 SAVESPTR(PL_rs); /* This is not permanent, either. */
411 PL_rs = sv_2mortal(newSVpvs("\000"));
414 *SvPVX(PL_rs) = '\n';
418 result = do_readline();
426 PL_last_in_gv = cGVOP_gv;
427 return do_readline();
438 do_join(TARG, &PL_sv_no, MARK, SP);
442 else if (SP == MARK) {
449 tmps = SvPV_const(tmpsv, len);
450 if ((!tmps || !len) && PL_errgv) {
451 SV * const error = ERRSV;
452 SvUPGRADE(error, SVt_PV);
453 if (SvPOK(error) && SvCUR(error))
454 sv_catpvs(error, "\t...caught");
456 tmps = SvPV_const(tmpsv, len);
459 tmpsv = sv_2mortal(newSVpvs("Warning: something's wrong"));
461 Perl_warn(aTHX_ "%"SVf, (void*)tmpsv);
473 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
475 if (SP - MARK != 1) {
477 do_join(TARG, &PL_sv_no, MARK, SP);
479 tmps = SvPV_const(tmpsv, len);
485 tmps = SvROK(tmpsv) ? NULL : SvPV_const(tmpsv, len);
488 SV * const error = ERRSV;
489 SvUPGRADE(error, SVt_PV);
490 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
492 SvSetSV(error,tmpsv);
493 else if (sv_isobject(error)) {
494 HV * const stash = SvSTASH(SvRV(error));
495 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
497 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
498 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
505 call_sv((SV*)GvCV(gv),
506 G_SCALAR|G_EVAL|G_KEEPERR);
507 sv_setsv(error,*PL_stack_sp--);
513 if (SvPOK(error) && SvCUR(error))
514 sv_catpvs(error, "\t...propagated");
517 tmps = SvPV_const(tmpsv, len);
523 tmpsv = sv_2mortal(newSVpvs("Died"));
525 DIE(aTHX_ "%"SVf, (void*)tmpsv);
541 GV * const gv = (GV *)*++MARK;
544 DIE(aTHX_ PL_no_usym, "filehandle");
545 if ((io = GvIOp(gv))) {
547 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
549 mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
551 /* Method's args are same as ours ... */
552 /* ... except handle is replaced by the object */
553 *MARK-- = SvTIED_obj((SV*)io, mg);
557 call_method("OPEN", G_SCALAR);
571 tmps = SvPV_const(sv, len);
572 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
575 PUSHi( (I32)PL_forkprocess );
576 else if (PL_forkprocess == 0) /* we are a new child */
586 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
589 IO * const io = GvIO(gv);
591 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
594 XPUSHs(SvTIED_obj((SV*)io, mg));
597 call_method("CLOSE", G_SCALAR);
605 PUSHs(boolSV(do_close(gv, TRUE)));
618 GV * const wgv = (GV*)POPs;
619 GV * const rgv = (GV*)POPs;
624 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
625 DIE(aTHX_ PL_no_usym, "filehandle");
630 do_close(rgv, FALSE);
632 do_close(wgv, FALSE);
634 if (PerlProc_pipe(fd) < 0)
637 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
638 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
639 IoOFP(rstio) = IoIFP(rstio);
640 IoIFP(wstio) = IoOFP(wstio);
641 IoTYPE(rstio) = IoTYPE_RDONLY;
642 IoTYPE(wstio) = IoTYPE_WRONLY;
644 if (!IoIFP(rstio) || !IoOFP(wstio)) {
646 PerlIO_close(IoIFP(rstio));
648 PerlLIO_close(fd[0]);
650 PerlIO_close(IoOFP(wstio));
652 PerlLIO_close(fd[1]);
655 #if defined(HAS_FCNTL) && defined(F_SETFD)
656 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
657 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
664 DIE(aTHX_ PL_no_func, "pipe");
680 if (gv && (io = GvIO(gv))
681 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
684 XPUSHs(SvTIED_obj((SV*)io, mg));
687 call_method("FILENO", G_SCALAR);
693 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
694 /* Can't do this because people seem to do things like
695 defined(fileno($foo)) to check whether $foo is a valid fh.
696 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
697 report_evil_fh(gv, io, PL_op->op_type);
702 PUSHi(PerlIO_fileno(fp));
715 anum = PerlLIO_umask(0);
716 (void)PerlLIO_umask(anum);
719 anum = PerlLIO_umask(POPi);
720 TAINT_PROPER("umask");
723 /* Only DIE if trying to restrict permissions on "user" (self).
724 * Otherwise it's harmless and more useful to just return undef
725 * since 'group' and 'other' concepts probably don't exist here. */
726 if (MAXARG >= 1 && (POPi & 0700))
727 DIE(aTHX_ "umask not implemented");
728 XPUSHs(&PL_sv_undef);
749 if (gv && (io = GvIO(gv))) {
750 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
753 XPUSHs(SvTIED_obj((SV*)io, mg));
758 call_method("BINMODE", G_SCALAR);
766 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
767 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
768 report_evil_fh(gv, io, PL_op->op_type);
769 SETERRNO(EBADF,RMS_IFI);
774 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
775 (discp) ? SvPV_nolen_const(discp) : NULL)) {
776 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
777 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
778 mode_from_discipline(discp),
779 (discp) ? SvPV_nolen_const(discp) : NULL)) {
799 const I32 markoff = MARK - PL_stack_base;
800 const char *methname;
801 int how = PERL_MAGIC_tied;
805 switch(SvTYPE(varsv)) {
807 methname = "TIEHASH";
808 HvEITER_set((HV *)varsv, 0);
811 methname = "TIEARRAY";
814 #ifdef GV_UNIQUE_CHECK
815 if (GvUNIQUE((GV*)varsv)) {
816 Perl_croak(aTHX_ "Attempt to tie unique GV");
819 methname = "TIEHANDLE";
820 how = PERL_MAGIC_tiedscalar;
821 /* For tied filehandles, we apply tiedscalar magic to the IO
822 slot of the GP rather than the GV itself. AMS 20010812 */
824 GvIOp(varsv) = newIO();
825 varsv = (SV *)GvIOp(varsv);
828 methname = "TIESCALAR";
829 how = PERL_MAGIC_tiedscalar;
833 if (sv_isobject(*MARK)) {
835 PUSHSTACKi(PERLSI_MAGIC);
837 EXTEND(SP,(I32)items);
841 call_method(methname, G_SCALAR);
844 /* Not clear why we don't call call_method here too.
845 * perhaps to get different error message ?
847 stash = gv_stashsv(*MARK, FALSE);
848 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
849 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
850 methname, (void*)*MARK);
853 PUSHSTACKi(PERLSI_MAGIC);
855 EXTEND(SP,(I32)items);
859 call_sv((SV*)GvCV(gv), G_SCALAR);
865 if (sv_isobject(sv)) {
866 sv_unmagic(varsv, how);
867 /* Croak if a self-tie on an aggregate is attempted. */
868 if (varsv == SvRV(sv) &&
869 (SvTYPE(varsv) == SVt_PVAV ||
870 SvTYPE(varsv) == SVt_PVHV))
872 "Self-ties of arrays and hashes are not supported");
873 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
876 SP = PL_stack_base + markoff;
886 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
887 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
889 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
892 if ((mg = SvTIED_mg(sv, how))) {
893 SV * const obj = SvRV(SvTIED_obj(sv, mg));
895 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
897 if (gv && isGV(gv) && (cv = GvCV(gv))) {
899 XPUSHs(SvTIED_obj((SV*)gv, mg));
900 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
903 call_sv((SV *)cv, G_VOID);
907 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
908 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
909 "untie attempted while %"UVuf" inner references still exist",
910 (UV)SvREFCNT(obj) - 1 ) ;
914 sv_unmagic(sv, how) ;
924 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
925 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
927 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
930 if ((mg = SvTIED_mg(sv, how))) {
931 SV *osv = SvTIED_obj(sv, mg);
932 if (osv == mg->mg_obj)
933 osv = sv_mortalcopy(osv);
947 HV * const hv = (HV*)POPs;
948 SV * const sv = sv_2mortal(newSVpvs("AnyDBM_File"));
949 stash = gv_stashsv(sv, FALSE);
950 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
952 require_pv("AnyDBM_File.pm");
954 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
955 DIE(aTHX_ "No dbm on this machine");
965 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
967 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
970 call_sv((SV*)GvCV(gv), G_SCALAR);
973 if (!sv_isobject(TOPs)) {
978 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
981 call_sv((SV*)GvCV(gv), G_SCALAR);
985 if (sv_isobject(TOPs)) {
986 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
987 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, NULL, 0);
1004 struct timeval timebuf;
1005 struct timeval *tbuf = &timebuf;
1008 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1013 # if BYTEORDER & 0xf0000
1014 # define ORDERBYTE (0x88888888 - BYTEORDER)
1016 # define ORDERBYTE (0x4444 - BYTEORDER)
1022 for (i = 1; i <= 3; i++) {
1023 SV * const sv = SP[i];
1026 if (SvREADONLY(sv)) {
1028 sv_force_normal_flags(sv, 0);
1029 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1030 DIE(aTHX_ PL_no_modify);
1033 if (ckWARN(WARN_MISC))
1034 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1035 SvPV_force_nolen(sv); /* force string conversion */
1042 /* little endians can use vecs directly */
1043 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1050 masksize = NFDBITS / NBBY;
1052 masksize = sizeof(long); /* documented int, everyone seems to use long */
1054 Zero(&fd_sets[0], 4, char*);
1057 # if SELECT_MIN_BITS == 1
1058 growsize = sizeof(fd_set);
1060 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1061 # undef SELECT_MIN_BITS
1062 # define SELECT_MIN_BITS __FD_SETSIZE
1064 /* If SELECT_MIN_BITS is greater than one we most probably will want
1065 * to align the sizes with SELECT_MIN_BITS/8 because for example
1066 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1067 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1068 * on (sets/tests/clears bits) is 32 bits. */
1069 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1077 timebuf.tv_sec = (long)value;
1078 value -= (NV)timebuf.tv_sec;
1079 timebuf.tv_usec = (long)(value * 1000000.0);
1084 for (i = 1; i <= 3; i++) {
1086 if (!SvOK(sv) || SvCUR(sv) == 0) {
1093 Sv_Grow(sv, growsize);
1097 while (++j <= growsize) {
1101 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1103 Newx(fd_sets[i], growsize, char);
1104 for (offset = 0; offset < growsize; offset += masksize) {
1105 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1106 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1109 fd_sets[i] = SvPVX(sv);
1113 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1114 /* Can't make just the (void*) conditional because that would be
1115 * cpp #if within cpp macro, and not all compilers like that. */
1116 nfound = PerlSock_select(
1118 (Select_fd_set_t) fd_sets[1],
1119 (Select_fd_set_t) fd_sets[2],
1120 (Select_fd_set_t) fd_sets[3],
1121 (void*) tbuf); /* Workaround for compiler bug. */
1123 nfound = PerlSock_select(
1125 (Select_fd_set_t) fd_sets[1],
1126 (Select_fd_set_t) fd_sets[2],
1127 (Select_fd_set_t) fd_sets[3],
1130 for (i = 1; i <= 3; i++) {
1133 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1135 for (offset = 0; offset < growsize; offset += masksize) {
1136 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1137 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1139 Safefree(fd_sets[i]);
1146 if (GIMME == G_ARRAY && tbuf) {
1147 value = (NV)(timebuf.tv_sec) +
1148 (NV)(timebuf.tv_usec) / 1000000.0;
1149 PUSHs(sv_2mortal(newSVnv(value)));
1153 DIE(aTHX_ "select not implemented");
1158 Perl_setdefout(pTHX_ GV *gv)
1161 SvREFCNT_inc_simple_void(gv);
1163 SvREFCNT_dec(PL_defoutgv);
1171 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : NULL;
1172 GV * egv = GvEGV(PL_defoutgv);
1178 XPUSHs(&PL_sv_undef);
1180 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1181 if (gvp && *gvp == egv) {
1182 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1186 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1191 if (!GvIO(newdefout))
1192 gv_IOadd(newdefout);
1193 setdefout(newdefout);
1203 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1205 if (gv && (io = GvIO(gv))) {
1206 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1208 const I32 gimme = GIMME_V;
1210 XPUSHs(SvTIED_obj((SV*)io, mg));
1213 call_method("GETC", gimme);
1216 if (gimme == G_SCALAR)
1217 SvSetMagicSV_nosteal(TARG, TOPs);
1221 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1222 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1223 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1224 report_evil_fh(gv, io, PL_op->op_type);
1225 SETERRNO(EBADF,RMS_IFI);
1229 sv_setpvn(TARG, " ", 1);
1230 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1231 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1232 /* Find out how many bytes the char needs */
1233 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1236 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1237 SvCUR_set(TARG,1+len);
1246 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1249 register PERL_CONTEXT *cx;
1250 const I32 gimme = GIMME_V;
1255 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1257 cx->blk_sub.retop = retop;
1259 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1261 setdefout(gv); /* locally select filehandle so $% et al work */
1292 goto not_a_format_reference;
1296 SV * const tmpsv = sv_newmortal();
1298 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1299 name = SvPV_nolen_const(tmpsv);
1301 DIE(aTHX_ "Undefined format \"%s\" called", name);
1303 not_a_format_reference:
1304 DIE(aTHX_ "Not a format reference");
1307 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1309 IoFLAGS(io) &= ~IOf_DIDTOP;
1310 return doform(cv,gv,PL_op->op_next);
1316 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1317 register IO * const io = GvIOp(gv);
1322 register PERL_CONTEXT *cx;
1324 if (!io || !(ofp = IoOFP(io)))
1327 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1328 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1330 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1331 PL_formtarget != PL_toptarget)
1335 if (!IoTOP_GV(io)) {
1338 if (!IoTOP_NAME(io)) {
1340 if (!IoFMT_NAME(io))
1341 IoFMT_NAME(io) = savepv(GvNAME(gv));
1342 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1343 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1344 if ((topgv && GvFORM(topgv)) ||
1345 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1346 IoTOP_NAME(io) = savesvpv(topname);
1348 IoTOP_NAME(io) = savepvs("top");
1350 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1351 if (!topgv || !GvFORM(topgv)) {
1352 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1355 IoTOP_GV(io) = topgv;
1357 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1358 I32 lines = IoLINES_LEFT(io);
1359 const char *s = SvPVX_const(PL_formtarget);
1360 if (lines <= 0) /* Yow, header didn't even fit!!! */
1362 while (lines-- > 0) {
1363 s = strchr(s, '\n');
1369 const STRLEN save = SvCUR(PL_formtarget);
1370 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1371 do_print(PL_formtarget, ofp);
1372 SvCUR_set(PL_formtarget, save);
1373 sv_chop(PL_formtarget, s);
1374 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1377 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1378 do_print(PL_formfeed, ofp);
1379 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1381 PL_formtarget = PL_toptarget;
1382 IoFLAGS(io) |= IOf_DIDTOP;
1385 DIE(aTHX_ "bad top format reference");
1388 SV * const sv = sv_newmortal();
1390 gv_efullname4(sv, fgv, NULL, FALSE);
1391 name = SvPV_nolen_const(sv);
1393 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1395 DIE(aTHX_ "Undefined top format called");
1397 if (cv && CvCLONE(cv))
1398 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1399 return doform(cv, gv, PL_op);
1403 POPBLOCK(cx,PL_curpm);
1409 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1411 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1412 else if (ckWARN(WARN_CLOSED))
1413 report_evil_fh(gv, io, PL_op->op_type);
1418 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1419 if (ckWARN(WARN_IO))
1420 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1422 if (!do_print(PL_formtarget, fp))
1425 FmLINES(PL_formtarget) = 0;
1426 SvCUR_set(PL_formtarget, 0);
1427 *SvEND(PL_formtarget) = '\0';
1428 if (IoFLAGS(io) & IOf_FLUSH)
1429 (void)PerlIO_flush(fp);
1434 PL_formtarget = PL_bodytarget;
1436 PERL_UNUSED_VAR(newsp);
1437 PERL_UNUSED_VAR(gimme);
1438 return cx->blk_sub.retop;
1443 dVAR; dSP; dMARK; dORIGMARK;
1448 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1450 if (gv && (io = GvIO(gv))) {
1451 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1453 if (MARK == ORIGMARK) {
1456 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1460 *MARK = SvTIED_obj((SV*)io, mg);
1463 call_method("PRINTF", G_SCALAR);
1466 MARK = ORIGMARK + 1;
1474 if (!(io = GvIO(gv))) {
1475 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1476 report_evil_fh(gv, io, PL_op->op_type);
1477 SETERRNO(EBADF,RMS_IFI);
1480 else if (!(fp = IoOFP(io))) {
1481 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1483 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1484 else if (ckWARN(WARN_CLOSED))
1485 report_evil_fh(gv, io, PL_op->op_type);
1487 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1491 do_sprintf(sv, SP - MARK, MARK + 1);
1492 if (!do_print(sv, fp))
1495 if (IoFLAGS(io) & IOf_FLUSH)
1496 if (PerlIO_flush(fp) == EOF)
1507 PUSHs(&PL_sv_undef);
1515 const int perm = (MAXARG > 3) ? POPi : 0666;
1516 const int mode = POPi;
1517 SV * const sv = POPs;
1518 GV * const gv = (GV *)POPs;
1521 /* Need TIEHANDLE method ? */
1522 const char * const tmps = SvPV_const(sv, len);
1523 /* FIXME? do_open should do const */
1524 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1525 IoLINES(GvIOp(gv)) = 0;
1529 PUSHs(&PL_sv_undef);
1536 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1542 Sock_size_t bufsize;
1550 bool charstart = FALSE;
1551 STRLEN charskip = 0;
1554 GV * const gv = (GV*)*++MARK;
1555 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1556 && gv && (io = GvIO(gv)) )
1558 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1562 *MARK = SvTIED_obj((SV*)io, mg);
1564 call_method("READ", G_SCALAR);
1578 sv_setpvn(bufsv, "", 0);
1579 length = SvIVx(*++MARK);
1582 offset = SvIVx(*++MARK);
1586 if (!io || !IoIFP(io)) {
1587 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1588 report_evil_fh(gv, io, PL_op->op_type);
1589 SETERRNO(EBADF,RMS_IFI);
1592 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1593 buffer = SvPVutf8_force(bufsv, blen);
1594 /* UTF-8 may not have been set if they are all low bytes */
1599 buffer = SvPV_force(bufsv, blen);
1600 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1603 DIE(aTHX_ "Negative length");
1611 if (PL_op->op_type == OP_RECV) {
1612 char namebuf[MAXPATHLEN];
1613 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1614 bufsize = sizeof (struct sockaddr_in);
1616 bufsize = sizeof namebuf;
1618 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1622 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1623 /* 'offset' means 'flags' here */
1624 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1625 (struct sockaddr *)namebuf, &bufsize);
1629 /* Bogus return without padding */
1630 bufsize = sizeof (struct sockaddr_in);
1632 SvCUR_set(bufsv, count);
1633 *SvEND(bufsv) = '\0';
1634 (void)SvPOK_only(bufsv);
1638 /* This should not be marked tainted if the fp is marked clean */
1639 if (!(IoFLAGS(io) & IOf_UNTAINT))
1640 SvTAINTED_on(bufsv);
1642 sv_setpvn(TARG, namebuf, bufsize);
1647 if (PL_op->op_type == OP_RECV)
1648 DIE(aTHX_ PL_no_sock_func, "recv");
1650 if (DO_UTF8(bufsv)) {
1651 /* offset adjust in characters not bytes */
1652 blen = sv_len_utf8(bufsv);
1655 if (-offset > (int)blen)
1656 DIE(aTHX_ "Offset outside string");
1659 if (DO_UTF8(bufsv)) {
1660 /* convert offset-as-chars to offset-as-bytes */
1661 if (offset >= (int)blen)
1662 offset += SvCUR(bufsv) - blen;
1664 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1667 bufsize = SvCUR(bufsv);
1668 /* Allocating length + offset + 1 isn't perfect in the case of reading
1669 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1671 (should be 2 * length + offset + 1, or possibly something longer if
1672 PL_encoding is true) */
1673 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1674 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1675 Zero(buffer+bufsize, offset-bufsize, char);
1677 buffer = buffer + offset;
1679 read_target = bufsv;
1681 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1682 concatenate it to the current buffer. */
1684 /* Truncate the existing buffer to the start of where we will be
1686 SvCUR_set(bufsv, offset);
1688 read_target = sv_newmortal();
1689 SvUPGRADE(read_target, SVt_PV);
1690 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1693 if (PL_op->op_type == OP_SYSREAD) {
1694 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1695 if (IoTYPE(io) == IoTYPE_SOCKET) {
1696 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1702 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1707 #ifdef HAS_SOCKET__bad_code_maybe
1708 if (IoTYPE(io) == IoTYPE_SOCKET) {
1709 char namebuf[MAXPATHLEN];
1710 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1711 bufsize = sizeof (struct sockaddr_in);
1713 bufsize = sizeof namebuf;
1715 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1716 (struct sockaddr *)namebuf, &bufsize);
1721 count = PerlIO_read(IoIFP(io), buffer, length);
1722 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1723 if (count == 0 && PerlIO_error(IoIFP(io)))
1727 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1728 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1731 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1732 *SvEND(read_target) = '\0';
1733 (void)SvPOK_only(read_target);
1734 if (fp_utf8 && !IN_BYTES) {
1735 /* Look at utf8 we got back and count the characters */
1736 const char *bend = buffer + count;
1737 while (buffer < bend) {
1739 skip = UTF8SKIP(buffer);
1742 if (buffer - charskip + skip > bend) {
1743 /* partial character - try for rest of it */
1744 length = skip - (bend-buffer);
1745 offset = bend - SvPVX_const(bufsv);
1757 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1758 provided amount read (count) was what was requested (length)
1760 if (got < wanted && count == length) {
1761 length = wanted - got;
1762 offset = bend - SvPVX_const(bufsv);
1765 /* return value is character count */
1769 else if (buffer_utf8) {
1770 /* Let svcatsv upgrade the bytes we read in to utf8.
1771 The buffer is a mortal so will be freed soon. */
1772 sv_catsv_nomg(bufsv, read_target);
1775 /* This should not be marked tainted if the fp is marked clean */
1776 if (!(IoFLAGS(io) & IOf_UNTAINT))
1777 SvTAINTED_on(bufsv);
1789 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1795 STRLEN orig_blen_bytes;
1796 const int op_type = PL_op->op_type;
1800 GV *const gv = (GV*)*++MARK;
1801 if (PL_op->op_type == OP_SYSWRITE
1802 && gv && (io = GvIO(gv))) {
1803 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1807 if (MARK == SP - 1) {
1809 sv = sv_2mortal(newSViv(sv_len(*SP)));
1815 *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
1817 call_method("WRITE", G_SCALAR);
1833 if (!io || !IoIFP(io)) {
1835 if (ckWARN(WARN_CLOSED))
1836 report_evil_fh(gv, io, PL_op->op_type);
1837 SETERRNO(EBADF,RMS_IFI);
1841 /* Do this first to trigger any overloading. */
1842 buffer = SvPV_const(bufsv, blen);
1843 orig_blen_bytes = blen;
1844 doing_utf8 = DO_UTF8(bufsv);
1846 if (PerlIO_isutf8(IoIFP(io))) {
1847 if (!SvUTF8(bufsv)) {
1848 /* We don't modify the original scalar. */
1849 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1850 buffer = (char *) tmpbuf;
1854 else if (doing_utf8) {
1855 STRLEN tmplen = blen;
1856 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1859 buffer = (char *) tmpbuf;
1863 assert((char *)result == buffer);
1864 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1868 if (op_type == OP_SYSWRITE) {
1869 Size_t length = 0; /* This length is in characters. */
1875 /* The SV is bytes, and we've had to upgrade it. */
1876 blen_chars = orig_blen_bytes;
1878 /* The SV really is UTF-8. */
1879 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1880 /* Don't call sv_len_utf8 again because it will call magic
1881 or overloading a second time, and we might get back a
1882 different result. */
1883 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1885 /* It's safe, and it may well be cached. */
1886 blen_chars = sv_len_utf8(bufsv);
1894 length = blen_chars;
1896 #if Size_t_size > IVSIZE
1897 length = (Size_t)SvNVx(*++MARK);
1899 length = (Size_t)SvIVx(*++MARK);
1901 if ((SSize_t)length < 0) {
1903 DIE(aTHX_ "Negative length");
1908 offset = SvIVx(*++MARK);
1910 if (-offset > (IV)blen_chars) {
1912 DIE(aTHX_ "Offset outside string");
1914 offset += blen_chars;
1915 } else if (offset >= (IV)blen_chars && blen_chars > 0) {
1917 DIE(aTHX_ "Offset outside string");
1921 if (length > blen_chars - offset)
1922 length = blen_chars - offset;
1924 /* Here we convert length from characters to bytes. */
1925 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1926 /* Either we had to convert the SV, or the SV is magical, or
1927 the SV has overloading, in which case we can't or mustn't
1928 or mustn't call it again. */
1930 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1931 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1933 /* It's a real UTF-8 SV, and it's not going to change under
1934 us. Take advantage of any cache. */
1936 I32 len_I32 = length;
1938 /* Convert the start and end character positions to bytes.
1939 Remember that the second argument to sv_pos_u2b is relative
1941 sv_pos_u2b(bufsv, &start, &len_I32);
1948 buffer = buffer+offset;
1950 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1951 if (IoTYPE(io) == IoTYPE_SOCKET) {
1952 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1958 /* See the note at doio.c:do_print about filesize limits. --jhi */
1959 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1965 const int flags = SvIVx(*++MARK);
1968 char * const sockbuf = SvPVx(*++MARK, mlen);
1969 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1970 flags, (struct sockaddr *)sockbuf, mlen);
1974 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1979 DIE(aTHX_ PL_no_sock_func, "send");
1986 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1989 #if Size_t_size > IVSIZE
2008 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
2010 gv = PL_last_in_gv = GvEGV(PL_argvgv);
2012 if (io && !IoIFP(io)) {
2013 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2015 IoFLAGS(io) &= ~IOf_START;
2016 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2017 sv_setpvn(GvSV(gv), "-", 1);
2018 SvSETMAGIC(GvSV(gv));
2020 else if (!nextargv(gv))
2025 gv = PL_last_in_gv; /* eof */
2028 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
2031 IO * const io = GvIO(gv);
2033 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
2035 XPUSHs(SvTIED_obj((SV*)io, mg));
2038 call_method("EOF", G_SCALAR);
2045 PUSHs(boolSV(!gv || do_eof(gv)));
2056 PL_last_in_gv = (GV*)POPs;
2059 if (gv && (io = GvIO(gv))) {
2060 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
2063 XPUSHs(SvTIED_obj((SV*)io, mg));
2066 call_method("TELL", G_SCALAR);
2073 #if LSEEKSIZE > IVSIZE
2074 PUSHn( do_tell(gv) );
2076 PUSHi( do_tell(gv) );
2084 const int whence = POPi;
2085 #if LSEEKSIZE > IVSIZE
2086 const Off_t offset = (Off_t)SvNVx(POPs);
2088 const Off_t offset = (Off_t)SvIVx(POPs);
2091 GV * const gv = PL_last_in_gv = (GV*)POPs;
2094 if (gv && (io = GvIO(gv))) {
2095 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
2098 XPUSHs(SvTIED_obj((SV*)io, mg));
2099 #if LSEEKSIZE > IVSIZE
2100 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2102 XPUSHs(sv_2mortal(newSViv(offset)));
2104 XPUSHs(sv_2mortal(newSViv(whence)));
2107 call_method("SEEK", G_SCALAR);
2114 if (PL_op->op_type == OP_SEEK)
2115 PUSHs(boolSV(do_seek(gv, offset, whence)));
2117 const Off_t sought = do_sysseek(gv, offset, whence);
2119 PUSHs(&PL_sv_undef);
2121 SV* const sv = sought ?
2122 #if LSEEKSIZE > IVSIZE
2127 : newSVpvn(zero_but_true, ZBTLEN);
2128 PUSHs(sv_2mortal(sv));
2138 /* There seems to be no consensus on the length type of truncate()
2139 * and ftruncate(), both off_t and size_t have supporters. In
2140 * general one would think that when using large files, off_t is
2141 * at least as wide as size_t, so using an off_t should be okay. */
2142 /* XXX Configure probe for the length type of *truncate() needed XXX */
2145 #if Off_t_size > IVSIZE
2150 /* Checking for length < 0 is problematic as the type might or
2151 * might not be signed: if it is not, clever compilers will moan. */
2152 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2159 if (PL_op->op_flags & OPf_SPECIAL) {
2160 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2169 TAINT_PROPER("truncate");
2170 if (!(fp = IoIFP(io))) {
2176 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2178 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2185 SV * const sv = POPs;
2188 if (SvTYPE(sv) == SVt_PVGV) {
2189 tmpgv = (GV*)sv; /* *main::FRED for example */
2190 goto do_ftruncate_gv;
2192 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2193 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2194 goto do_ftruncate_gv;
2196 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2197 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2198 goto do_ftruncate_io;
2201 name = SvPV_nolen_const(sv);
2202 TAINT_PROPER("truncate");
2204 if (truncate(name, len) < 0)
2208 const int tmpfd = PerlLIO_open(name, O_RDWR);
2213 if (my_chsize(tmpfd, len) < 0)
2215 PerlLIO_close(tmpfd);
2224 SETERRNO(EBADF,RMS_IFI);
2232 SV * const argsv = POPs;
2233 const unsigned int func = POPu;
2234 const int optype = PL_op->op_type;
2235 GV * const gv = (GV*)POPs;
2236 IO * const io = gv ? GvIOn(gv) : NULL;
2240 if (!io || !argsv || !IoIFP(io)) {
2241 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2242 report_evil_fh(gv, io, PL_op->op_type);
2243 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2247 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2250 s = SvPV_force(argsv, len);
2251 need = IOCPARM_LEN(func);
2253 s = Sv_Grow(argsv, need + 1);
2254 SvCUR_set(argsv, need);
2257 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2260 retval = SvIV(argsv);
2261 s = INT2PTR(char*,retval); /* ouch */
2264 TAINT_PROPER(PL_op_desc[optype]);
2266 if (optype == OP_IOCTL)
2268 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2270 DIE(aTHX_ "ioctl is not implemented");
2274 DIE(aTHX_ "fcntl is not implemented");
2276 #if defined(OS2) && defined(__EMX__)
2277 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2279 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2283 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2285 if (s[SvCUR(argsv)] != 17)
2286 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2288 s[SvCUR(argsv)] = 0; /* put our null back */
2289 SvSETMAGIC(argsv); /* Assume it has changed */
2298 PUSHp(zero_but_true, ZBTLEN);
2311 const int argtype = POPi;
2312 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : (GV*)POPs;
2314 if (gv && (io = GvIO(gv)))
2320 /* XXX Looks to me like io is always NULL at this point */
2322 (void)PerlIO_flush(fp);
2323 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2326 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2327 report_evil_fh(gv, io, PL_op->op_type);
2329 SETERRNO(EBADF,RMS_IFI);
2334 DIE(aTHX_ PL_no_func, "flock()");
2344 const int protocol = POPi;
2345 const int type = POPi;
2346 const int domain = POPi;
2347 GV * const gv = (GV*)POPs;
2348 register IO * const io = gv ? GvIOn(gv) : NULL;
2352 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2353 report_evil_fh(gv, io, PL_op->op_type);
2354 if (io && IoIFP(io))
2355 do_close(gv, FALSE);
2356 SETERRNO(EBADF,LIB_INVARG);
2361 do_close(gv, FALSE);
2363 TAINT_PROPER("socket");
2364 fd = PerlSock_socket(domain, type, protocol);
2367 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2368 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2369 IoTYPE(io) = IoTYPE_SOCKET;
2370 if (!IoIFP(io) || !IoOFP(io)) {
2371 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2372 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2373 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2376 #if defined(HAS_FCNTL) && defined(F_SETFD)
2377 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2381 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2386 DIE(aTHX_ PL_no_sock_func, "socket");
2392 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2394 const int protocol = POPi;
2395 const int type = POPi;
2396 const int domain = POPi;
2397 GV * const gv2 = (GV*)POPs;
2398 GV * const gv1 = (GV*)POPs;
2399 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2400 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2403 if (!gv1 || !gv2 || !io1 || !io2) {
2404 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2406 report_evil_fh(gv1, io1, PL_op->op_type);
2408 report_evil_fh(gv1, io2, PL_op->op_type);
2410 if (io1 && IoIFP(io1))
2411 do_close(gv1, FALSE);
2412 if (io2 && IoIFP(io2))
2413 do_close(gv2, FALSE);
2418 do_close(gv1, FALSE);
2420 do_close(gv2, FALSE);
2422 TAINT_PROPER("socketpair");
2423 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2425 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2426 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2427 IoTYPE(io1) = IoTYPE_SOCKET;
2428 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2429 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2430 IoTYPE(io2) = IoTYPE_SOCKET;
2431 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2432 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2433 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2434 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2435 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2436 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2437 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2440 #if defined(HAS_FCNTL) && defined(F_SETFD)
2441 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2442 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2447 DIE(aTHX_ PL_no_sock_func, "socketpair");
2455 SV * const addrsv = POPs;
2456 /* OK, so on what platform does bind modify addr? */
2458 GV * const gv = (GV*)POPs;
2459 register IO * const io = GvIOn(gv);
2462 if (!io || !IoIFP(io))
2465 addr = SvPV_const(addrsv, len);
2466 TAINT_PROPER("bind");
2467 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2473 if (ckWARN(WARN_CLOSED))
2474 report_evil_fh(gv, io, PL_op->op_type);
2475 SETERRNO(EBADF,SS_IVCHAN);
2478 DIE(aTHX_ PL_no_sock_func, "bind");
2486 SV * const addrsv = POPs;
2487 GV * const gv = (GV*)POPs;
2488 register IO * const io = GvIOn(gv);
2492 if (!io || !IoIFP(io))
2495 addr = SvPV_const(addrsv, len);
2496 TAINT_PROPER("connect");
2497 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2503 if (ckWARN(WARN_CLOSED))
2504 report_evil_fh(gv, io, PL_op->op_type);
2505 SETERRNO(EBADF,SS_IVCHAN);
2508 DIE(aTHX_ PL_no_sock_func, "connect");
2516 const int backlog = POPi;
2517 GV * const gv = (GV*)POPs;
2518 register IO * const io = gv ? GvIOn(gv) : NULL;
2520 if (!gv || !io || !IoIFP(io))
2523 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2529 if (ckWARN(WARN_CLOSED))
2530 report_evil_fh(gv, io, PL_op->op_type);
2531 SETERRNO(EBADF,SS_IVCHAN);
2534 DIE(aTHX_ PL_no_sock_func, "listen");
2544 char namebuf[MAXPATHLEN];
2545 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2546 Sock_size_t len = sizeof (struct sockaddr_in);
2548 Sock_size_t len = sizeof namebuf;
2550 GV * const ggv = (GV*)POPs;
2551 GV * const ngv = (GV*)POPs;
2560 if (!gstio || !IoIFP(gstio))
2564 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2567 /* Some platforms indicate zero length when an AF_UNIX client is
2568 * not bound. Simulate a non-zero-length sockaddr structure in
2570 namebuf[0] = 0; /* sun_len */
2571 namebuf[1] = AF_UNIX; /* sun_family */
2579 do_close(ngv, FALSE);
2580 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2581 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2582 IoTYPE(nstio) = IoTYPE_SOCKET;
2583 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2584 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2585 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2586 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2589 #if defined(HAS_FCNTL) && defined(F_SETFD)
2590 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2594 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2595 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2597 #ifdef __SCO_VERSION__
2598 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2601 PUSHp(namebuf, len);
2605 if (ckWARN(WARN_CLOSED))
2606 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2607 SETERRNO(EBADF,SS_IVCHAN);
2613 DIE(aTHX_ PL_no_sock_func, "accept");
2621 const int how = POPi;
2622 GV * const gv = (GV*)POPs;
2623 register IO * const io = GvIOn(gv);
2625 if (!io || !IoIFP(io))
2628 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2632 if (ckWARN(WARN_CLOSED))
2633 report_evil_fh(gv, io, PL_op->op_type);
2634 SETERRNO(EBADF,SS_IVCHAN);
2637 DIE(aTHX_ PL_no_sock_func, "shutdown");
2645 const int optype = PL_op->op_type;
2646 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2647 const unsigned int optname = (unsigned int) POPi;
2648 const unsigned int lvl = (unsigned int) POPi;
2649 GV * const gv = (GV*)POPs;
2650 register IO * const io = GvIOn(gv);
2654 if (!io || !IoIFP(io))
2657 fd = PerlIO_fileno(IoIFP(io));
2661 (void)SvPOK_only(sv);
2665 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2672 #if defined(__SYMBIAN32__)
2673 # define SETSOCKOPT_OPTION_VALUE_T void *
2675 # define SETSOCKOPT_OPTION_VALUE_T const char *
2677 /* XXX TODO: We need to have a proper type (a Configure probe,
2678 * etc.) for what the C headers think of the third argument of
2679 * setsockopt(), the option_value read-only buffer: is it
2680 * a "char *", or a "void *", const or not. Some compilers
2681 * don't take kindly to e.g. assuming that "char *" implicitly
2682 * promotes to a "void *", or to explicitly promoting/demoting
2683 * consts to non/vice versa. The "const void *" is the SUS
2684 * definition, but that does not fly everywhere for the above
2686 SETSOCKOPT_OPTION_VALUE_T buf;
2690 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2694 aint = (int)SvIV(sv);
2695 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2698 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2707 if (ckWARN(WARN_CLOSED))
2708 report_evil_fh(gv, io, optype);
2709 SETERRNO(EBADF,SS_IVCHAN);
2714 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2722 const int optype = PL_op->op_type;
2723 GV * const gv = (GV*)POPs;
2724 register IO * const io = GvIOn(gv);
2729 if (!io || !IoIFP(io))
2732 sv = sv_2mortal(newSV(257));
2733 (void)SvPOK_only(sv);
2737 fd = PerlIO_fileno(IoIFP(io));
2739 case OP_GETSOCKNAME:
2740 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2743 case OP_GETPEERNAME:
2744 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2746 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2748 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";
2749 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2750 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2751 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2752 sizeof(u_short) + sizeof(struct in_addr))) {
2759 #ifdef BOGUS_GETNAME_RETURN
2760 /* Interactive Unix, getpeername() and getsockname()
2761 does not return valid namelen */
2762 if (len == BOGUS_GETNAME_RETURN)
2763 len = sizeof(struct sockaddr);
2771 if (ckWARN(WARN_CLOSED))
2772 report_evil_fh(gv, io, optype);
2773 SETERRNO(EBADF,SS_IVCHAN);
2778 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2793 if (PL_op->op_flags & OPf_REF) {
2795 if (PL_op->op_type == OP_LSTAT) {
2796 if (gv != PL_defgv) {
2797 do_fstat_warning_check:
2798 if (ckWARN(WARN_IO))
2799 Perl_warner(aTHX_ packWARN(WARN_IO),
2800 "lstat() on filehandle %s", GvENAME(gv));
2801 } else if (PL_laststype != OP_LSTAT)
2802 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2806 if (gv != PL_defgv) {
2807 PL_laststype = OP_STAT;
2809 sv_setpvn(PL_statname, "", 0);
2816 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2817 } else if (IoDIRP(io)) {
2820 PerlLIO_fstat(dirfd(IoDIRP(io)), &PL_statcache);
2822 DIE(aTHX_ PL_no_func, "dirfd");
2825 PL_laststatval = -1;
2831 if (PL_laststatval < 0) {
2832 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2833 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2838 SV* const sv = POPs;
2839 if (SvTYPE(sv) == SVt_PVGV) {
2842 } else if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2844 if (PL_op->op_type == OP_LSTAT)
2845 goto do_fstat_warning_check;
2847 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2849 if (PL_op->op_type == OP_LSTAT)
2850 goto do_fstat_warning_check;
2851 goto do_fstat_have_io;
2854 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2856 PL_laststype = PL_op->op_type;
2857 if (PL_op->op_type == OP_LSTAT)
2858 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2860 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2861 if (PL_laststatval < 0) {
2862 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2863 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2869 if (gimme != G_ARRAY) {
2870 if (gimme != G_VOID)
2871 XPUSHs(boolSV(max));
2877 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2878 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2879 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2880 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2881 #if Uid_t_size > IVSIZE
2882 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2884 # if Uid_t_sign <= 0
2885 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2887 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2890 #if Gid_t_size > IVSIZE
2891 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2893 # if Gid_t_sign <= 0
2894 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2896 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2899 #ifdef USE_STAT_RDEV
2900 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2902 PUSHs(sv_2mortal(newSVpvs("")));
2904 #if Off_t_size > IVSIZE
2905 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2907 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2910 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2911 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2912 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2914 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2915 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2916 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2918 #ifdef USE_STAT_BLOCKS
2919 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2920 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2922 PUSHs(sv_2mortal(newSVpvs("")));
2923 PUSHs(sv_2mortal(newSVpvs("")));
2929 /* This macro is used by the stacked filetest operators :
2930 * if the previous filetest failed, short-circuit and pass its value.
2931 * Else, discard it from the stack and continue. --rgs
2933 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2934 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2935 else { (void)POPs; PUTBACK; } \
2942 /* Not const, because things tweak this below. Not bool, because there's
2943 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2944 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2945 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2946 /* Giving some sort of initial value silences compilers. */
2948 int access_mode = R_OK;
2950 int access_mode = 0;
2953 /* access_mode is never used, but leaving use_access in makes the
2954 conditional compiling below much clearer. */
2957 int stat_mode = S_IRUSR;
2959 bool effective = FALSE;
2962 STACKED_FTEST_CHECK;
2964 switch (PL_op->op_type) {
2966 #if !(defined(HAS_ACCESS) && defined(R_OK))
2972 #if defined(HAS_ACCESS) && defined(W_OK)
2977 stat_mode = S_IWUSR;
2981 #if defined(HAS_ACCESS) && defined(X_OK)
2986 stat_mode = S_IXUSR;
2990 #ifdef PERL_EFF_ACCESS
2993 stat_mode = S_IWUSR;
2997 #ifndef PERL_EFF_ACCESS
3005 #ifdef PERL_EFF_ACCESS
3010 stat_mode = S_IXUSR;
3016 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3017 const char *const name = POPpx;
3019 # ifdef PERL_EFF_ACCESS
3020 result = PERL_EFF_ACCESS(name, access_mode);
3022 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3028 result = access(name, access_mode);
3030 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3045 if (cando(stat_mode, effective, &PL_statcache))
3054 const int op_type = PL_op->op_type;
3056 STACKED_FTEST_CHECK;
3061 if (op_type == OP_FTIS)
3064 /* You can't dTARGET inside OP_FTIS, because you'll get
3065 "panic: pad_sv po" - the op is not flagged to have a target. */
3069 #if Off_t_size > IVSIZE
3070 PUSHn(PL_statcache.st_size);
3072 PUSHi(PL_statcache.st_size);
3076 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3079 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3082 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3095 /* I believe that all these three are likely to be defined on most every
3096 system these days. */
3098 if(PL_op->op_type == OP_FTSUID)
3102 if(PL_op->op_type == OP_FTSGID)
3106 if(PL_op->op_type == OP_FTSVTX)
3110 STACKED_FTEST_CHECK;
3115 switch (PL_op->op_type) {
3117 if (PL_statcache.st_uid == PL_uid)
3121 if (PL_statcache.st_uid == PL_euid)
3125 if (PL_statcache.st_size == 0)
3129 if (S_ISSOCK(PL_statcache.st_mode))
3133 if (S_ISCHR(PL_statcache.st_mode))
3137 if (S_ISBLK(PL_statcache.st_mode))
3141 if (S_ISREG(PL_statcache.st_mode))
3145 if (S_ISDIR(PL_statcache.st_mode))
3149 if (S_ISFIFO(PL_statcache.st_mode))
3154 if (PL_statcache.st_mode & S_ISUID)
3160 if (PL_statcache.st_mode & S_ISGID)
3166 if (PL_statcache.st_mode & S_ISVTX)
3177 I32 result = my_lstat();
3181 if (S_ISLNK(PL_statcache.st_mode))
3194 STACKED_FTEST_CHECK;
3196 if (PL_op->op_flags & OPf_REF)
3198 else if (isGV(TOPs))
3200 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3201 gv = (GV*)SvRV(POPs);
3203 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3205 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3206 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3207 else if (tmpsv && SvOK(tmpsv)) {
3208 const char *tmps = SvPV_nolen_const(tmpsv);
3216 if (PerlLIO_isatty(fd))
3221 #if defined(atarist) /* this will work with atariST. Configure will
3222 make guesses for other systems. */
3223 # define FILE_base(f) ((f)->_base)
3224 # define FILE_ptr(f) ((f)->_ptr)
3225 # define FILE_cnt(f) ((f)->_cnt)
3226 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3237 register STDCHAR *s;
3243 STACKED_FTEST_CHECK;
3245 if (PL_op->op_flags & OPf_REF)
3247 else if (isGV(TOPs))
3249 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3250 gv = (GV*)SvRV(POPs);
3256 if (gv == PL_defgv) {
3258 io = GvIO(PL_statgv);
3261 goto really_filename;
3266 PL_laststatval = -1;
3267 sv_setpvn(PL_statname, "", 0);
3268 io = GvIO(PL_statgv);
3270 if (io && IoIFP(io)) {
3271 if (! PerlIO_has_base(IoIFP(io)))
3272 DIE(aTHX_ "-T and -B not implemented on filehandles");
3273 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3274 if (PL_laststatval < 0)
3276 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3277 if (PL_op->op_type == OP_FTTEXT)
3282 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3283 i = PerlIO_getc(IoIFP(io));
3285 (void)PerlIO_ungetc(IoIFP(io),i);
3287 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3289 len = PerlIO_get_bufsiz(IoIFP(io));
3290 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3291 /* sfio can have large buffers - limit to 512 */
3296 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3298 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3300 SETERRNO(EBADF,RMS_IFI);
3308 PL_laststype = OP_STAT;
3309 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3310 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3311 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3313 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3316 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3317 if (PL_laststatval < 0) {
3318 (void)PerlIO_close(fp);
3321 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3322 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3323 (void)PerlIO_close(fp);
3325 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3326 RETPUSHNO; /* special case NFS directories */
3327 RETPUSHYES; /* null file is anything */
3332 /* now scan s to look for textiness */
3333 /* XXX ASCII dependent code */
3335 #if defined(DOSISH) || defined(USEMYBINMODE)
3336 /* ignore trailing ^Z on short files */
3337 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3341 for (i = 0; i < len; i++, s++) {
3342 if (!*s) { /* null never allowed in text */
3347 else if (!(isPRINT(*s) || isSPACE(*s)))
3350 else if (*s & 128) {
3352 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3355 /* utf8 characters don't count as odd */
3356 if (UTF8_IS_START(*s)) {
3357 int ulen = UTF8SKIP(s);
3358 if (ulen < len - i) {
3360 for (j = 1; j < ulen; j++) {
3361 if (!UTF8_IS_CONTINUATION(s[j]))
3364 --ulen; /* loop does extra increment */
3374 *s != '\n' && *s != '\r' && *s != '\b' &&
3375 *s != '\t' && *s != '\f' && *s != 27)
3380 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3391 const char *tmps = NULL;
3395 SV * const sv = POPs;
3396 if (PL_op->op_flags & OPf_SPECIAL) {
3397 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3399 else if (SvTYPE(sv) == SVt_PVGV) {
3402 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3406 tmps = SvPVx_nolen_const(sv);
3410 if( !gv && (!tmps || !*tmps) ) {
3411 HV * const table = GvHVn(PL_envgv);
3414 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3415 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3417 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3422 deprecate("chdir('') or chdir(undef) as chdir()");
3423 tmps = SvPV_nolen_const(*svp);
3427 TAINT_PROPER("chdir");
3432 TAINT_PROPER("chdir");
3435 IO* const io = GvIO(gv);
3438 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3440 else if (IoDIRP(io)) {
3442 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3444 DIE(aTHX_ PL_no_func, "dirfd");
3448 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3449 report_evil_fh(gv, io, PL_op->op_type);
3450 SETERRNO(EBADF, RMS_IFI);
3455 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3456 report_evil_fh(gv, io, PL_op->op_type);
3457 SETERRNO(EBADF,RMS_IFI);
3461 DIE(aTHX_ PL_no_func, "fchdir");
3465 PUSHi( PerlDir_chdir(tmps) >= 0 );
3467 /* Clear the DEFAULT element of ENV so we'll get the new value
3469 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3476 dVAR; dSP; dMARK; dTARGET;
3477 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3488 char * const tmps = POPpx;
3489 TAINT_PROPER("chroot");
3490 PUSHi( chroot(tmps) >= 0 );
3493 DIE(aTHX_ PL_no_func, "chroot");
3501 const char * const tmps2 = POPpconstx;
3502 const char * const tmps = SvPV_nolen_const(TOPs);
3503 TAINT_PROPER("rename");
3505 anum = PerlLIO_rename(tmps, tmps2);
3507 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3508 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3511 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3512 (void)UNLINK(tmps2);
3513 if (!(anum = link(tmps, tmps2)))
3514 anum = UNLINK(tmps);
3522 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3526 const int op_type = PL_op->op_type;
3530 if (op_type == OP_LINK)
3531 DIE(aTHX_ PL_no_func, "link");
3533 # ifndef HAS_SYMLINK
3534 if (op_type == OP_SYMLINK)
3535 DIE(aTHX_ PL_no_func, "symlink");
3539 const char * const tmps2 = POPpconstx;
3540 const char * const tmps = SvPV_nolen_const(TOPs);
3541 TAINT_PROPER(PL_op_desc[op_type]);
3543 # if defined(HAS_LINK)
3544 # if defined(HAS_SYMLINK)
3545 /* Both present - need to choose which. */
3546 (op_type == OP_LINK) ?
3547 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3549 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3550 PerlLIO_link(tmps, tmps2);
3553 # if defined(HAS_SYMLINK)
3554 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3555 symlink(tmps, tmps2);
3560 SETi( result >= 0 );
3567 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3578 char buf[MAXPATHLEN];
3581 #ifndef INCOMPLETE_TAINTS
3585 len = readlink(tmps, buf, sizeof(buf) - 1);
3593 RETSETUNDEF; /* just pretend it's a normal file */
3597 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3599 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3601 char * const save_filename = filename;
3606 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3608 Newx(cmdline, size, char);
3609 my_strlcpy(cmdline, cmd, size);
3610 my_strlcat(cmdline, " ", size);
3611 for (s = cmdline + strlen(cmdline); *filename; ) {
3615 if (s - cmdline < size)
3616 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3617 myfp = PerlProc_popen(cmdline, "r");
3621 SV * const tmpsv = sv_newmortal();
3622 /* Need to save/restore 'PL_rs' ?? */
3623 s = sv_gets(tmpsv, myfp, 0);
3624 (void)PerlProc_pclose(myfp);
3628 #ifdef HAS_SYS_ERRLIST
3633 /* you don't see this */
3634 const char * const errmsg =
3635 #ifdef HAS_SYS_ERRLIST
3643 if (instr(s, errmsg)) {
3650 #define EACCES EPERM
3652 if (instr(s, "cannot make"))
3653 SETERRNO(EEXIST,RMS_FEX);
3654 else if (instr(s, "existing file"))
3655 SETERRNO(EEXIST,RMS_FEX);
3656 else if (instr(s, "ile exists"))
3657 SETERRNO(EEXIST,RMS_FEX);
3658 else if (instr(s, "non-exist"))
3659 SETERRNO(ENOENT,RMS_FNF);
3660 else if (instr(s, "does not exist"))
3661 SETERRNO(ENOENT,RMS_FNF);
3662 else if (instr(s, "not empty"))
3663 SETERRNO(EBUSY,SS_DEVOFFLINE);
3664 else if (instr(s, "cannot access"))
3665 SETERRNO(EACCES,RMS_PRV);
3667 SETERRNO(EPERM,RMS_PRV);
3670 else { /* some mkdirs return no failure indication */
3671 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3672 if (PL_op->op_type == OP_RMDIR)
3677 SETERRNO(EACCES,RMS_PRV); /* a guess */
3686 /* This macro removes trailing slashes from a directory name.
3687 * Different operating and file systems take differently to
3688 * trailing slashes. According to POSIX 1003.1 1996 Edition
3689 * any number of trailing slashes should be allowed.
3690 * Thusly we snip them away so that even non-conforming
3691 * systems are happy.
3692 * We should probably do this "filtering" for all
3693 * the functions that expect (potentially) directory names:
3694 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3695 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3697 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3698 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3701 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3702 (tmps) = savepvn((tmps), (len)); \
3712 const int mode = (MAXARG > 1) ? POPi : 0777;
3714 TRIMSLASHES(tmps,len,copy);
3716 TAINT_PROPER("mkdir");
3718 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3722 SETi( dooneliner("mkdir", tmps) );
3723 oldumask = PerlLIO_umask(0);
3724 PerlLIO_umask(oldumask);
3725 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3740 TRIMSLASHES(tmps,len,copy);
3741 TAINT_PROPER("rmdir");
3743 SETi( PerlDir_rmdir(tmps) >= 0 );
3745 SETi( dooneliner("rmdir", tmps) );
3752 /* Directory calls. */
3756 #if defined(Direntry_t) && defined(HAS_READDIR)
3758 const char * const dirname = POPpconstx;
3759 GV * const gv = (GV*)POPs;
3760 register IO * const io = GvIOn(gv);
3766 PerlDir_close(IoDIRP(io));
3767 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3773 SETERRNO(EBADF,RMS_DIR);
3776 DIE(aTHX_ PL_no_dir_func, "opendir");
3782 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3783 DIE(aTHX_ PL_no_dir_func, "readdir");
3785 #if !defined(I_DIRENT) && !defined(VMS)
3786 Direntry_t *readdir (DIR *);
3792 const I32 gimme = GIMME;
3793 GV * const gv = (GV *)POPs;
3794 register const Direntry_t *dp;
3795 register IO * const io = GvIOn(gv);
3797 if (!io || !IoDIRP(io)) {
3798 if(ckWARN(WARN_IO)) {
3799 Perl_warner(aTHX_ packWARN(WARN_IO),
3800 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3806 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3810 sv = newSVpvn(dp->d_name, dp->d_namlen);
3812 sv = newSVpv(dp->d_name, 0);
3814 #ifndef INCOMPLETE_TAINTS
3815 if (!(IoFLAGS(io) & IOf_UNTAINT))
3818 XPUSHs(sv_2mortal(sv));
3819 } while (gimme == G_ARRAY);
3821 if (!dp && gimme != G_ARRAY)
3828 SETERRNO(EBADF,RMS_ISI);
3829 if (GIMME == G_ARRAY)
3838 #if defined(HAS_TELLDIR) || defined(telldir)
3840 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3841 /* XXX netbsd still seemed to.
3842 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3843 --JHI 1999-Feb-02 */
3844 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3845 long telldir (DIR *);
3847 GV * const gv = (GV*)POPs;
3848 register IO * const io = GvIOn(gv);
3850 if (!io || !IoDIRP(io)) {
3851 if(ckWARN(WARN_IO)) {
3852 Perl_warner(aTHX_ packWARN(WARN_IO),
3853 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3858 PUSHi( PerlDir_tell(IoDIRP(io)) );
3862 SETERRNO(EBADF,RMS_ISI);
3865 DIE(aTHX_ PL_no_dir_func, "telldir");
3871 #if defined(HAS_SEEKDIR) || defined(seekdir)
3873 const long along = POPl;
3874 GV * const gv = (GV*)POPs;
3875 register IO * const io = GvIOn(gv);
3877 if (!io || !IoDIRP(io)) {
3878 if(ckWARN(WARN_IO)) {
3879 Perl_warner(aTHX_ packWARN(WARN_IO),
3880 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3884 (void)PerlDir_seek(IoDIRP(io), along);
3889 SETERRNO(EBADF,RMS_ISI);
3892 DIE(aTHX_ PL_no_dir_func, "seekdir");
3898 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3900 GV * const gv = (GV*)POPs;
3901 register IO * const io = GvIOn(gv);
3903 if (!io || !IoDIRP(io)) {
3904 if(ckWARN(WARN_IO)) {
3905 Perl_warner(aTHX_ packWARN(WARN_IO),
3906 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3910 (void)PerlDir_rewind(IoDIRP(io));
3914 SETERRNO(EBADF,RMS_ISI);
3917 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3923 #if defined(Direntry_t) && defined(HAS_READDIR)
3925 GV * const gv = (GV*)POPs;
3926 register IO * const io = GvIOn(gv);
3928 if (!io || !IoDIRP(io)) {
3929 if(ckWARN(WARN_IO)) {
3930 Perl_warner(aTHX_ packWARN(WARN_IO),
3931 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3935 #ifdef VOID_CLOSEDIR
3936 PerlDir_close(IoDIRP(io));
3938 if (PerlDir_close(IoDIRP(io)) < 0) {
3939 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3948 SETERRNO(EBADF,RMS_IFI);
3951 DIE(aTHX_ PL_no_dir_func, "closedir");
3955 /* Process control. */
3964 PERL_FLUSHALL_FOR_CHILD;
3965 childpid = PerlProc_fork();
3969 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
3971 SvREADONLY_off(GvSV(tmpgv));
3972 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3973 SvREADONLY_on(GvSV(tmpgv));
3975 #ifdef THREADS_HAVE_PIDS
3976 PL_ppid = (IV)getppid();
3978 #ifdef PERL_USES_PL_PIDSTATUS
3979 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3985 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3990 PERL_FLUSHALL_FOR_CHILD;
3991 childpid = PerlProc_fork();
3997 DIE(aTHX_ PL_no_func, "fork");
4004 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4009 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4010 childpid = wait4pid(-1, &argflags, 0);
4012 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4017 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4018 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4019 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4021 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4026 DIE(aTHX_ PL_no_func, "wait");
4032 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4034 const int optype = POPi;
4035 const Pid_t pid = TOPi;
4039 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4040 result = wait4pid(pid, &argflags, optype);
4042 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4047 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4048 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4049 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4051 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4056 DIE(aTHX_ PL_no_func, "waitpid");
4062 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4068 while (++MARK <= SP) {
4069 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4074 TAINT_PROPER("system");
4076 PERL_FLUSHALL_FOR_CHILD;
4077 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4083 if (PerlProc_pipe(pp) >= 0)
4085 while ((childpid = PerlProc_fork()) == -1) {
4086 if (errno != EAGAIN) {
4091 PerlLIO_close(pp[0]);
4092 PerlLIO_close(pp[1]);
4099 Sigsave_t ihand,qhand; /* place to save signals during system() */
4103 PerlLIO_close(pp[1]);
4105 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4106 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4109 result = wait4pid(childpid, &status, 0);
4110 } while (result == -1 && errno == EINTR);
4112 (void)rsignal_restore(SIGINT, &ihand);
4113 (void)rsignal_restore(SIGQUIT, &qhand);
4115 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4116 do_execfree(); /* free any memory child malloced on fork */
4123 while (n < sizeof(int)) {
4124 n1 = PerlLIO_read(pp[0],
4125 (void*)(((char*)&errkid)+n),
4131 PerlLIO_close(pp[0]);
4132 if (n) { /* Error */
4133 if (n != sizeof(int))
4134 DIE(aTHX_ "panic: kid popen errno read");
4135 errno = errkid; /* Propagate errno from kid */
4136 STATUS_NATIVE_CHILD_SET(-1);
4139 XPUSHi(STATUS_CURRENT);
4143 PerlLIO_close(pp[0]);
4144 #if defined(HAS_FCNTL) && defined(F_SETFD)
4145 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4148 if (PL_op->op_flags & OPf_STACKED) {
4149 SV * const really = *++MARK;
4150 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4152 else if (SP - MARK != 1)
4153 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4155 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4159 #else /* ! FORK or VMS or OS/2 */
4162 if (PL_op->op_flags & OPf_STACKED) {
4163 SV * const really = *++MARK;
4164 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4165 value = (I32)do_aspawn(really, MARK, SP);
4167 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4170 else if (SP - MARK != 1) {
4171 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4172 value = (I32)do_aspawn(NULL, MARK, SP);
4174 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4178 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4180 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4182 STATUS_NATIVE_CHILD_SET(value);
4185 XPUSHi(result ? value : STATUS_CURRENT);
4186 #endif /* !FORK or VMS */
4192 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4197 while (++MARK <= SP) {
4198 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4203 TAINT_PROPER("exec");
4205 PERL_FLUSHALL_FOR_CHILD;
4206 if (PL_op->op_flags & OPf_STACKED) {
4207 SV * const really = *++MARK;
4208 value = (I32)do_aexec(really, MARK, SP);
4210 else if (SP - MARK != 1)
4212 value = (I32)vms_do_aexec(NULL, MARK, SP);
4216 (void ) do_aspawn(NULL, MARK, SP);
4220 value = (I32)do_aexec(NULL, MARK, SP);
4225 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4228 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4231 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4245 # ifdef THREADS_HAVE_PIDS
4246 if (PL_ppid != 1 && getppid() == 1)
4247 /* maybe the parent process has died. Refresh ppid cache */
4251 XPUSHi( getppid() );
4255 DIE(aTHX_ PL_no_func, "getppid");
4264 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4267 pgrp = (I32)BSD_GETPGRP(pid);
4269 if (pid != 0 && pid != PerlProc_getpid())
4270 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4276 DIE(aTHX_ PL_no_func, "getpgrp()");
4295 TAINT_PROPER("setpgrp");
4297 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4299 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4300 || (pid != 0 && pid != PerlProc_getpid()))
4302 DIE(aTHX_ "setpgrp can't take arguments");
4304 SETi( setpgrp() >= 0 );
4305 #endif /* USE_BSDPGRP */
4308 DIE(aTHX_ PL_no_func, "setpgrp()");
4314 #ifdef HAS_GETPRIORITY
4316 const int who = POPi;
4317 const int which = TOPi;
4318 SETi( getpriority(which, who) );
4321 DIE(aTHX_ PL_no_func, "getpriority()");
4327 #ifdef HAS_SETPRIORITY
4329 const int niceval = POPi;
4330 const int who = POPi;
4331 const int which = TOPi;
4332 TAINT_PROPER("setpriority");
4333 SETi( setpriority(which, who, niceval) >= 0 );
4336 DIE(aTHX_ PL_no_func, "setpriority()");
4346 XPUSHn( time(NULL) );
4348 XPUSHi( time(NULL) );
4360 (void)PerlProc_times(&PL_timesbuf);
4362 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4363 /* struct tms, though same data */
4367 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4368 if (GIMME == G_ARRAY) {
4369 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4370 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4371 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4377 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4379 if (GIMME == G_ARRAY) {
4380 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4381 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4382 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4386 DIE(aTHX_ "times not implemented");
4388 #endif /* HAS_TIMES */
4391 #ifdef LOCALTIME_EDGECASE_BROKEN
4392 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4397 /* No workarounds in the valid range */
4398 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4399 return (localtime (tp));
4401 /* This edge case is to workaround the undefined behaviour, where the
4402 * TIMEZONE makes the time go beyond the defined range.
4403 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4404 * If there is a negative offset in TZ, like MET-1METDST, some broken
4405 * implementations of localtime () (like AIX 5.2) barf with bogus
4407 * 0x7fffffff gmtime 2038-01-19 03:14:07
4408 * 0x7fffffff localtime 1901-12-13 21:45:51
4409 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4410 * 0x3c19137f gmtime 2001-12-13 20:45:51
4411 * 0x3c19137f localtime 2001-12-13 21:45:51
4412 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4413 * Given that legal timezones are typically between GMT-12 and GMT+12
4414 * we turn back the clock 23 hours before calling the localtime
4415 * function, and add those to the return value. This will never cause
4416 * day wrapping problems, since the edge case is Tue Jan *19*
4418 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4421 if (P->tm_hour >= 24) {
4423 P->tm_mday++; /* 18 -> 19 */
4424 P->tm_wday++; /* Mon -> Tue */
4425 P->tm_yday++; /* 18 -> 19 */
4428 } /* S_my_localtime */
4436 const struct tm *tmbuf;
4437 static const char * const dayname[] =
4438 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4439 static const char * const monname[] =
4440 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4441 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4447 when = (Time_t)SvNVx(POPs);
4449 when = (Time_t)SvIVx(POPs);
4452 if (PL_op->op_type == OP_LOCALTIME)
4453 #ifdef LOCALTIME_EDGECASE_BROKEN
4454 tmbuf = S_my_localtime(aTHX_ &when);
4456 tmbuf = localtime(&when);
4459 tmbuf = gmtime(&when);
4461 if (GIMME != G_ARRAY) {
4467 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4468 dayname[tmbuf->tm_wday],
4469 monname[tmbuf->tm_mon],
4474 tmbuf->tm_year + 1900);
4475 PUSHs(sv_2mortal(tsv));
4480 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4481 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4482 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4483 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4484 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4485 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4486 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4487 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4488 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4499 anum = alarm((unsigned int)anum);
4506 DIE(aTHX_ PL_no_func, "alarm");
4517 (void)time(&lasttime);
4522 PerlProc_sleep((unsigned int)duration);
4525 XPUSHi(when - lasttime);
4529 /* Shared memory. */
4530 /* Merged with some message passing. */
4534 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4535 dVAR; dSP; dMARK; dTARGET;
4536 const int op_type = PL_op->op_type;
4541 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4544 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4547 value = (I32)(do_semop(MARK, SP) >= 0);
4550 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4566 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4567 dVAR; dSP; dMARK; dTARGET;
4568 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4575 DIE(aTHX_ "System V IPC is not implemented on this machine");
4581 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4582 dVAR; dSP; dMARK; dTARGET;
4583 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4591 PUSHp(zero_but_true, ZBTLEN);
4599 /* I can't const this further without getting warnings about the types of
4600 various arrays passed in from structures. */
4602 S_space_join_names_mortal(pTHX_ char *const *array)
4606 if (array && *array) {
4607 target = sv_2mortal(newSVpvs(""));
4609 sv_catpv(target, *array);
4612 sv_catpvs(target, " ");
4615 target = sv_mortalcopy(&PL_sv_no);
4620 /* Get system info. */
4624 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4626 I32 which = PL_op->op_type;
4627 register char **elem;
4629 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4630 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4631 struct hostent *gethostbyname(Netdb_name_t);
4632 struct hostent *gethostent(void);
4634 struct hostent *hent;
4638 if (which == OP_GHBYNAME) {
4639 #ifdef HAS_GETHOSTBYNAME
4640 const char* const name = POPpbytex;
4641 hent = PerlSock_gethostbyname(name);
4643 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4646 else if (which == OP_GHBYADDR) {
4647 #ifdef HAS_GETHOSTBYADDR
4648 const int addrtype = POPi;
4649 SV * const addrsv = POPs;
4651 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4653 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4655 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4659 #ifdef HAS_GETHOSTENT
4660 hent = PerlSock_gethostent();
4662 DIE(aTHX_ PL_no_sock_func, "gethostent");
4665 #ifdef HOST_NOT_FOUND
4667 #ifdef USE_REENTRANT_API
4668 # ifdef USE_GETHOSTENT_ERRNO
4669 h_errno = PL_reentrant_buffer->_gethostent_errno;
4672 STATUS_UNIX_SET(h_errno);
4676 if (GIMME != G_ARRAY) {
4677 PUSHs(sv = sv_newmortal());
4679 if (which == OP_GHBYNAME) {
4681 sv_setpvn(sv, hent->h_addr, hent->h_length);
4684 sv_setpv(sv, (char*)hent->h_name);
4690 PUSHs(sv_2mortal(newSVpv((char*)hent->h_name, 0)));
4691 PUSHs(space_join_names_mortal(hent->h_aliases));
4692 PUSHs(sv_2mortal(newSViv((IV)hent->h_addrtype)));
4693 len = hent->h_length;
4694 PUSHs(sv_2mortal(newSViv((IV)len)));
4696 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4697 XPUSHs(sv_2mortal(newSVpvn(*elem, len)));
4701 PUSHs(newSVpvn(hent->h_addr, len));
4703 PUSHs(sv_mortalcopy(&PL_sv_no));
4708 DIE(aTHX_ PL_no_sock_func, "gethostent");
4714 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4716 I32 which = PL_op->op_type;
4718 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4719 struct netent *getnetbyaddr(Netdb_net_t, int);
4720 struct netent *getnetbyname(Netdb_name_t);
4721 struct netent *getnetent(void);
4723 struct netent *nent;
4725 if (which == OP_GNBYNAME){
4726 #ifdef HAS_GETNETBYNAME
4727 const char * const name = POPpbytex;
4728 nent = PerlSock_getnetbyname(name);
4730 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4733 else if (which == OP_GNBYADDR) {
4734 #ifdef HAS_GETNETBYADDR
4735 const int addrtype = POPi;
4736 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4737 nent = PerlSock_getnetbyaddr(addr, addrtype);
4739 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4743 #ifdef HAS_GETNETENT
4744 nent = PerlSock_getnetent();
4746 DIE(aTHX_ PL_no_sock_func, "getnetent");
4749 #ifdef HOST_NOT_FOUND
4751 #ifdef USE_REENTRANT_API
4752 # ifdef USE_GETNETENT_ERRNO
4753 h_errno = PL_reentrant_buffer->_getnetent_errno;
4756 STATUS_UNIX_SET(h_errno);
4761 if (GIMME != G_ARRAY) {
4762 PUSHs(sv = sv_newmortal());
4764 if (which == OP_GNBYNAME)
4765 sv_setiv(sv, (IV)nent->n_net);
4767 sv_setpv(sv, nent->n_name);
4773 PUSHs(sv_2mortal(newSVpv(nent->n_name, 0)));
4774 PUSHs(space_join_names_mortal(nent->n_aliases));
4775 PUSHs(sv_2mortal(newSViv((IV)nent->n_addrtype)));
4776 PUSHs(sv_2mortal(newSViv((IV)nent->n_net)));
4781 DIE(aTHX_ PL_no_sock_func, "getnetent");
4787 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4789 I32 which = PL_op->op_type;
4791 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4792 struct protoent *getprotobyname(Netdb_name_t);
4793 struct protoent *getprotobynumber(int);
4794 struct protoent *getprotoent(void);
4796 struct protoent *pent;
4798 if (which == OP_GPBYNAME) {
4799 #ifdef HAS_GETPROTOBYNAME
4800 const char* const name = POPpbytex;
4801 pent = PerlSock_getprotobyname(name);
4803 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4806 else if (which == OP_GPBYNUMBER) {
4807 #ifdef HAS_GETPROTOBYNUMBER
4808 const int number = POPi;
4809 pent = PerlSock_getprotobynumber(number);
4811 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4815 #ifdef HAS_GETPROTOENT
4816 pent = PerlSock_getprotoent();
4818 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4822 if (GIMME != G_ARRAY) {
4823 PUSHs(sv = sv_newmortal());
4825 if (which == OP_GPBYNAME)
4826 sv_setiv(sv, (IV)pent->p_proto);
4828 sv_setpv(sv, pent->p_name);
4834 PUSHs(sv_2mortal(newSVpv(pent->p_name, 0)));
4835 PUSHs(space_join_names_mortal(pent->p_aliases));
4836 PUSHs(sv_2mortal(newSViv((IV)pent->p_proto)));
4841 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4847 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4849 I32 which = PL_op->op_type;
4851 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4852 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4853 struct servent *getservbyport(int, Netdb_name_t);
4854 struct servent *getservent(void);
4856 struct servent *sent;
4858 if (which == OP_GSBYNAME) {
4859 #ifdef HAS_GETSERVBYNAME
4860 const char * const proto = POPpbytex;
4861 const char * const name = POPpbytex;
4862 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4864 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4867 else if (which == OP_GSBYPORT) {
4868 #ifdef HAS_GETSERVBYPORT
4869 const char * const proto = POPpbytex;
4870 unsigned short port = (unsigned short)POPu;
4872 port = PerlSock_htons(port);
4874 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4876 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4880 #ifdef HAS_GETSERVENT
4881 sent = PerlSock_getservent();
4883 DIE(aTHX_ PL_no_sock_func, "getservent");
4887 if (GIMME != G_ARRAY) {
4888 PUSHs(sv = sv_newmortal());
4890 if (which == OP_GSBYNAME) {
4892 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4894 sv_setiv(sv, (IV)(sent->s_port));
4898 sv_setpv(sv, sent->s_name);
4904 PUSHs(sv_2mortal(newSVpv(sent->s_name, 0)));
4905 PUSHs(space_join_names_mortal(sent->s_aliases));
4907 PUSHs(sv_2mortal(newSViv((IV)PerlSock_ntohs(sent->s_port))));
4909 PUSHs(sv_2mortal(newSViv((IV)(sent->s_port))));
4911 PUSHs(sv_2mortal(newSVpv(sent->s_proto, 0)));
4916 DIE(aTHX_ PL_no_sock_func, "getservent");
4922 #ifdef HAS_SETHOSTENT
4924 PerlSock_sethostent(TOPi);
4927 DIE(aTHX_ PL_no_sock_func, "sethostent");
4933 #ifdef HAS_SETNETENT
4935 PerlSock_setnetent(TOPi);
4938 DIE(aTHX_ PL_no_sock_func, "setnetent");
4944 #ifdef HAS_SETPROTOENT
4946 PerlSock_setprotoent(TOPi);
4949 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4955 #ifdef HAS_SETSERVENT
4957 PerlSock_setservent(TOPi);
4960 DIE(aTHX_ PL_no_sock_func, "setservent");
4966 #ifdef HAS_ENDHOSTENT
4968 PerlSock_endhostent();
4972 DIE(aTHX_ PL_no_sock_func, "endhostent");
4978 #ifdef HAS_ENDNETENT
4980 PerlSock_endnetent();
4984 DIE(aTHX_ PL_no_sock_func, "endnetent");
4990 #ifdef HAS_ENDPROTOENT
4992 PerlSock_endprotoent();
4996 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5002 #ifdef HAS_ENDSERVENT
5004 PerlSock_endservent();
5008 DIE(aTHX_ PL_no_sock_func, "endservent");
5016 I32 which = PL_op->op_type;
5018 struct passwd *pwent = NULL;
5020 * We currently support only the SysV getsp* shadow password interface.
5021 * The interface is declared in <shadow.h> and often one needs to link
5022 * with -lsecurity or some such.
5023 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5026 * AIX getpwnam() is clever enough to return the encrypted password
5027 * only if the caller (euid?) is root.
5029 * There are at least three other shadow password APIs. Many platforms
5030 * seem to contain more than one interface for accessing the shadow
5031 * password databases, possibly for compatibility reasons.
5032 * The getsp*() is by far he simplest one, the other two interfaces
5033 * are much more complicated, but also very similar to each other.
5038 * struct pr_passwd *getprpw*();
5039 * The password is in
5040 * char getprpw*(...).ufld.fd_encrypt[]
5041 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5046 * struct es_passwd *getespw*();
5047 * The password is in
5048 * char *(getespw*(...).ufld.fd_encrypt)
5049 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5052 * struct userpw *getuserpw();
5053 * The password is in
5054 * char *(getuserpw(...)).spw_upw_passwd
5055 * (but the de facto standard getpwnam() should work okay)
5057 * Mention I_PROT here so that Configure probes for it.
5059 * In HP-UX for getprpw*() the manual page claims that one should include
5060 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5061 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5062 * and pp_sys.c already includes <shadow.h> if there is such.
5064 * Note that <sys/security.h> is already probed for, but currently
5065 * it is only included in special cases.
5067 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5068 * be preferred interface, even though also the getprpw*() interface
5069 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5070 * One also needs to call set_auth_parameters() in main() before
5071 * doing anything else, whether one is using getespw*() or getprpw*().
5073 * Note that accessing the shadow databases can be magnitudes
5074 * slower than accessing the standard databases.
5079 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5080 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5081 * the pw_comment is left uninitialized. */
5082 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5088 const char* const name = POPpbytex;
5089 pwent = getpwnam(name);
5095 pwent = getpwuid(uid);
5099 # ifdef HAS_GETPWENT
5101 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5102 if (pwent) pwent = getpwnam(pwent->pw_name);
5105 DIE(aTHX_ PL_no_func, "getpwent");
5111 if (GIMME != G_ARRAY) {
5112 PUSHs(sv = sv_newmortal());
5114 if (which == OP_GPWNAM)
5115 # if Uid_t_sign <= 0
5116 sv_setiv(sv, (IV)pwent->pw_uid);
5118 sv_setuv(sv, (UV)pwent->pw_uid);
5121 sv_setpv(sv, pwent->pw_name);
5127 PUSHs(sv_2mortal(newSVpv(pwent->pw_name, 0)));
5129 PUSHs(sv = sv_2mortal(newSViv(0)));
5130 /* If we have getspnam(), we try to dig up the shadow
5131 * password. If we are underprivileged, the shadow
5132 * interface will set the errno to EACCES or similar,
5133 * and return a null pointer. If this happens, we will
5134 * use the dummy password (usually "*" or "x") from the
5135 * standard password database.
5137 * In theory we could skip the shadow call completely
5138 * if euid != 0 but in practice we cannot know which
5139 * security measures are guarding the shadow databases
5140 * on a random platform.
5142 * Resist the urge to use additional shadow interfaces.
5143 * Divert the urge to writing an extension instead.
5146 /* Some AIX setups falsely(?) detect some getspnam(), which
5147 * has a different API than the Solaris/IRIX one. */
5148 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5150 const int saverrno = errno;
5151 const struct spwd * const spwent = getspnam(pwent->pw_name);
5152 /* Save and restore errno so that
5153 * underprivileged attempts seem
5154 * to have never made the unsccessful
5155 * attempt to retrieve the shadow password. */
5157 if (spwent && spwent->sp_pwdp)
5158 sv_setpv(sv, spwent->sp_pwdp);
5162 if (!SvPOK(sv)) /* Use the standard password, then. */
5163 sv_setpv(sv, pwent->pw_passwd);
5166 # ifndef INCOMPLETE_TAINTS
5167 /* passwd is tainted because user himself can diddle with it.
5168 * admittedly not much and in a very limited way, but nevertheless. */
5172 # if Uid_t_sign <= 0
5173 PUSHs(sv_2mortal(newSViv((IV)pwent->pw_uid)));
5175 PUSHs(sv_2mortal(newSVuv((UV)pwent->pw_uid)));
5178 # if Uid_t_sign <= 0
5179 PUSHs(sv_2mortal(newSViv((IV)pwent->pw_gid)));
5181 PUSHs(sv_2mortal(newSVuv((UV)pwent->pw_gid)));
5183 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5184 * because of the poor interface of the Perl getpw*(),
5185 * not because there's some standard/convention saying so.
5186 * A better interface would have been to return a hash,
5187 * but we are accursed by our history, alas. --jhi. */
5189 PUSHs(sv_2mortal(newSViv((IV)pwent->pw_change)));
5192 PUSHs(sv_2mortal(newSViv((IV)pwent->pw_quota)));
5195 PUSHs(sv_2mortal(newSVpv(pwent->pw_age, 0)));
5197 /* I think that you can never get this compiled, but just in case. */
5198 PUSHs(sv_mortalcopy(&PL_sv_no));
5203 /* pw_class and pw_comment are mutually exclusive--.
5204 * see the above note for pw_change, pw_quota, and pw_age. */
5206 PUSHs(sv_2mortal(newSVpv(pwent->pw_class, 0)));
5209 PUSHs(sv_2mortal(newSVpv(pwent->pw_comment, 0)));
5211 /* I think that you can never get this compiled, but just in case. */
5212 PUSHs(sv_mortalcopy(&PL_sv_no));
5217 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5219 PUSHs(sv_mortalcopy(&PL_sv_no));
5221 # ifndef INCOMPLETE_TAINTS
5222 /* pw_gecos is tainted because user himself can diddle with it. */
5226 PUSHs(sv_2mortal(newSVpv(pwent->pw_dir, 0)));
5228 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5229 # ifndef INCOMPLETE_TAINTS
5230 /* pw_shell is tainted because user himself can diddle with it. */
5235 PUSHs(sv_2mortal(newSViv((IV)pwent->pw_expire)));
5240 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5246 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5251 DIE(aTHX_ PL_no_func, "setpwent");
5257 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5262 DIE(aTHX_ PL_no_func, "endpwent");
5270 const I32 which = PL_op->op_type;
5271 const struct group *grent;
5273 if (which == OP_GGRNAM) {
5274 const char* const name = POPpbytex;
5275 grent = (const struct group *)getgrnam(name);
5277 else if (which == OP_GGRGID) {
5278 const Gid_t gid = POPi;
5279 grent = (const struct group *)getgrgid(gid);
5283 grent = (struct group *)getgrent();
5285 DIE(aTHX_ PL_no_func, "getgrent");
5289 if (GIMME != G_ARRAY) {
5290 SV * const sv = sv_newmortal();
5294 if (which == OP_GGRNAM)
5295 sv_setiv(sv, (IV)grent->gr_gid);
5297 sv_setpv(sv, grent->gr_name);
5303 PUSHs(sv_2mortal(newSVpv(grent->gr_name, 0)));
5306 PUSHs(sv_2mortal(newSVpv(grent->gr_passwd, 0)));
5308 PUSHs(sv_mortalcopy(&PL_sv_no));
5311 PUSHs(sv_2mortal(newSViv((IV)grent->gr_gid)));
5313 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5314 /* In UNICOS/mk (_CRAYMPP) the multithreading
5315 * versions (getgrnam_r, getgrgid_r)
5316 * seem to return an illegal pointer
5317 * as the group members list, gr_mem.
5318 * getgrent() doesn't even have a _r version
5319 * but the gr_mem is poisonous anyway.
5320 * So yes, you cannot get the list of group
5321 * members if building multithreaded in UNICOS/mk. */
5322 PUSHs(space_join_names_mortal(grent->gr_mem));
5328 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5334 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5339 DIE(aTHX_ PL_no_func, "setgrent");
5345 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5350 DIE(aTHX_ PL_no_func, "endgrent");
5360 if (!(tmps = PerlProc_getlogin()))
5362 PUSHp(tmps, strlen(tmps));
5365 DIE(aTHX_ PL_no_func, "getlogin");
5369 /* Miscellaneous. */
5374 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5375 register I32 items = SP - MARK;
5376 unsigned long a[20];
5381 while (++MARK <= SP) {
5382 if (SvTAINTED(*MARK)) {
5388 TAINT_PROPER("syscall");
5391 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5392 * or where sizeof(long) != sizeof(char*). But such machines will
5393 * not likely have syscall implemented either, so who cares?
5395 while (++MARK <= SP) {
5396 if (SvNIOK(*MARK) || !i)
5397 a[i++] = SvIV(*MARK);
5398 else if (*MARK == &PL_sv_undef)
5401 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5407 DIE(aTHX_ "Too many args to syscall");
5409 DIE(aTHX_ "Too few args to syscall");
5411 retval = syscall(a[0]);
5414 retval = syscall(a[0],a[1]);
5417 retval = syscall(a[0],a[1],a[2]);
5420 retval = syscall(a[0],a[1],a[2],a[3]);
5423 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5426 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5429 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5432 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5436 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5439 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5442 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5446 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5450 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5454 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5455 a[10],a[11],a[12],a[13]);
5457 #endif /* atarist */
5463 DIE(aTHX_ PL_no_func, "syscall");
5467 #ifdef FCNTL_EMULATE_FLOCK
5469 /* XXX Emulate flock() with fcntl().
5470 What's really needed is a good file locking module.
5474 fcntl_emulate_flock(int fd, int operation)
5478 switch (operation & ~LOCK_NB) {
5480 flock.l_type = F_RDLCK;
5483 flock.l_type = F_WRLCK;
5486 flock.l_type = F_UNLCK;
5492 flock.l_whence = SEEK_SET;
5493 flock.l_start = flock.l_len = (Off_t)0;
5495 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5498 #endif /* FCNTL_EMULATE_FLOCK */
5500 #ifdef LOCKF_EMULATE_FLOCK
5502 /* XXX Emulate flock() with lockf(). This is just to increase
5503 portability of scripts. The calls are not completely
5504 interchangeable. What's really needed is a good file
5508 /* The lockf() constants might have been defined in <unistd.h>.
5509 Unfortunately, <unistd.h> causes troubles on some mixed
5510 (BSD/POSIX) systems, such as SunOS 4.1.3.
5512 Further, the lockf() constants aren't POSIX, so they might not be
5513 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5514 just stick in the SVID values and be done with it. Sigh.
5518 # define F_ULOCK 0 /* Unlock a previously locked region */
5521 # define F_LOCK 1 /* Lock a region for exclusive use */
5524 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5527 # define F_TEST 3 /* Test a region for other processes locks */
5531 lockf_emulate_flock(int fd, int operation)
5534 const int save_errno = errno;
5537 /* flock locks entire file so for lockf we need to do the same */
5538 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5539 if (pos > 0) /* is seekable and needs to be repositioned */
5540 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5541 pos = -1; /* seek failed, so don't seek back afterwards */
5544 switch (operation) {
5546 /* LOCK_SH - get a shared lock */
5548 /* LOCK_EX - get an exclusive lock */
5550 i = lockf (fd, F_LOCK, 0);
5553 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5554 case LOCK_SH|LOCK_NB:
5555 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5556 case LOCK_EX|LOCK_NB:
5557 i = lockf (fd, F_TLOCK, 0);
5559 if ((errno == EAGAIN) || (errno == EACCES))
5560 errno = EWOULDBLOCK;
5563 /* LOCK_UN - unlock (non-blocking is a no-op) */
5565 case LOCK_UN|LOCK_NB:
5566 i = lockf (fd, F_ULOCK, 0);
5569 /* Default - can't decipher operation */
5576 if (pos > 0) /* need to restore position of the handle */
5577 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5582 #endif /* LOCKF_EMULATE_FLOCK */
5586 * c-indentation-style: bsd
5588 * indent-tabs-mode: t
5591 * ex: set ts=8 sts=4 sw=4 noet: