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 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
202 /* AIX 5.2 and below use mktime for localtime, and defines the edge case
203 * for time 0x7fffffff to be valid only in UTC. AIX 5.3 provides localtime64
204 * available in the 32bit environment, which could warrant Configure
205 * checks in the future.
208 #define LOCALTIME_EDGECASE_BROKEN
211 /* F_OK unused: if stat() cannot find it... */
213 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
214 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
215 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
218 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
219 # ifdef I_SYS_SECURITY
220 # include <sys/security.h>
224 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
227 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
231 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
233 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
237 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
238 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
239 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
242 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
244 const Uid_t ruid = getuid();
245 const Uid_t euid = geteuid();
246 const Gid_t rgid = getgid();
247 const Gid_t egid = getegid();
251 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
252 Perl_croak(aTHX_ "switching effective uid is not implemented");
255 if (setreuid(euid, ruid))
258 if (setresuid(euid, ruid, (Uid_t)-1))
261 Perl_croak(aTHX_ "entering effective uid failed");
264 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
265 Perl_croak(aTHX_ "switching effective gid is not implemented");
268 if (setregid(egid, rgid))
271 if (setresgid(egid, rgid, (Gid_t)-1))
274 Perl_croak(aTHX_ "entering effective gid failed");
277 res = access(path, mode);
280 if (setreuid(ruid, euid))
283 if (setresuid(ruid, euid, (Uid_t)-1))
286 Perl_croak(aTHX_ "leaving effective uid failed");
289 if (setregid(rgid, egid))
292 if (setresgid(rgid, egid, (Gid_t)-1))
295 Perl_croak(aTHX_ "leaving effective gid failed");
300 # define PERL_EFF_ACCESS(p,f) (emulate_eaccess((p), (f)))
303 #if !defined(PERL_EFF_ACCESS)
304 /* With it or without it: anyway you get a warning: either that
305 it is unused, or it is declared static and never defined.
308 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
310 PERL_UNUSED_ARG(path);
311 PERL_UNUSED_ARG(mode);
312 Perl_croak(aTHX_ "switching effective uid is not implemented");
322 const char * const tmps = POPpconstx;
323 const I32 gimme = GIMME_V;
324 const char *mode = "r";
327 if (PL_op->op_private & OPpOPEN_IN_RAW)
329 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
331 fp = PerlProc_popen(tmps, mode);
333 const char * const type = PL_curcop->cop_io ? SvPV_nolen_const(PL_curcop->cop_io) : NULL;
335 PerlIO_apply_layers(aTHX_ fp,mode,type);
337 if (gimme == G_VOID) {
339 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
342 else if (gimme == G_SCALAR) {
345 PL_rs = &PL_sv_undef;
346 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
347 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
355 SV * const sv = newSV(79);
356 if (sv_gets(sv, fp, 0) == NULL) {
360 XPUSHs(sv_2mortal(sv));
361 if (SvLEN(sv) - SvCUR(sv) > 20) {
362 SvPV_shrink_to_cur(sv);
367 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
368 TAINT; /* "I believe that this is not gratuitous!" */
371 STATUS_NATIVE_CHILD_SET(-1);
372 if (gimme == G_SCALAR)
383 tryAMAGICunTARGET(iter, -1);
385 /* Note that we only ever get here if File::Glob fails to load
386 * without at the same time croaking, for some reason, or if
387 * perl was built with PERL_EXTERNAL_GLOB */
394 * The external globbing program may use things we can't control,
395 * so for security reasons we must assume the worst.
398 taint_proper(PL_no_security, "glob");
402 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
403 PL_last_in_gv = (GV*)*PL_stack_sp--;
405 SAVESPTR(PL_rs); /* This is not permanent, either. */
406 PL_rs = sv_2mortal(newSVpvs("\000"));
409 *SvPVX(PL_rs) = '\n';
413 result = do_readline();
421 PL_last_in_gv = cGVOP_gv;
422 return do_readline();
433 do_join(TARG, &PL_sv_no, MARK, SP);
437 else if (SP == MARK) {
444 tmps = SvPV_const(tmpsv, len);
445 if ((!tmps || !len) && PL_errgv) {
446 SV * const error = ERRSV;
447 SvUPGRADE(error, SVt_PV);
448 if (SvPOK(error) && SvCUR(error))
449 sv_catpvs(error, "\t...caught");
451 tmps = SvPV_const(tmpsv, len);
454 tmpsv = sv_2mortal(newSVpvs("Warning: something's wrong"));
456 Perl_warn(aTHX_ "%"SVf, tmpsv);
468 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
470 if (SP - MARK != 1) {
472 do_join(TARG, &PL_sv_no, MARK, SP);
474 tmps = SvPV_const(tmpsv, len);
480 tmps = SvROK(tmpsv) ? NULL : SvPV_const(tmpsv, len);
483 SV * const error = ERRSV;
484 SvUPGRADE(error, SVt_PV);
485 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
487 SvSetSV(error,tmpsv);
488 else if (sv_isobject(error)) {
489 HV * const stash = SvSTASH(SvRV(error));
490 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
492 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
493 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
500 call_sv((SV*)GvCV(gv),
501 G_SCALAR|G_EVAL|G_KEEPERR);
502 sv_setsv(error,*PL_stack_sp--);
508 if (SvPOK(error) && SvCUR(error))
509 sv_catpvs(error, "\t...propagated");
512 tmps = SvPV_const(tmpsv, len);
518 tmpsv = sv_2mortal(newSVpvs("Died"));
520 DIE(aTHX_ "%"SVf, tmpsv);
536 GV * const gv = (GV *)*++MARK;
539 DIE(aTHX_ PL_no_usym, "filehandle");
540 if ((io = GvIOp(gv)))
541 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
544 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
546 /* Method's args are same as ours ... */
547 /* ... except handle is replaced by the object */
548 *MARK-- = SvTIED_obj((SV*)io, mg);
552 call_method("OPEN", G_SCALAR);
566 tmps = SvPV_const(sv, len);
567 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
570 PUSHi( (I32)PL_forkprocess );
571 else if (PL_forkprocess == 0) /* we are a new child */
583 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
585 if (gv && (io = GvIO(gv))
586 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
589 XPUSHs(SvTIED_obj((SV*)io, mg));
592 call_method("CLOSE", G_SCALAR);
598 PUSHs(boolSV(do_close(gv, TRUE)));
611 GV * const wgv = (GV*)POPs;
612 GV * const rgv = (GV*)POPs;
617 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
618 DIE(aTHX_ PL_no_usym, "filehandle");
623 do_close(rgv, FALSE);
625 do_close(wgv, FALSE);
627 if (PerlProc_pipe(fd) < 0)
630 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
631 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
632 IoOFP(rstio) = IoIFP(rstio);
633 IoIFP(wstio) = IoOFP(wstio);
634 IoTYPE(rstio) = IoTYPE_RDONLY;
635 IoTYPE(wstio) = IoTYPE_WRONLY;
637 if (!IoIFP(rstio) || !IoOFP(wstio)) {
638 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
639 else PerlLIO_close(fd[0]);
640 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
641 else PerlLIO_close(fd[1]);
644 #if defined(HAS_FCNTL) && defined(F_SETFD)
645 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
646 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
653 DIE(aTHX_ PL_no_func, "pipe");
669 if (gv && (io = GvIO(gv))
670 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
673 XPUSHs(SvTIED_obj((SV*)io, mg));
676 call_method("FILENO", G_SCALAR);
682 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
683 /* Can't do this because people seem to do things like
684 defined(fileno($foo)) to check whether $foo is a valid fh.
685 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
686 report_evil_fh(gv, io, PL_op->op_type);
691 PUSHi(PerlIO_fileno(fp));
704 anum = PerlLIO_umask(0);
705 (void)PerlLIO_umask(anum);
708 anum = PerlLIO_umask(POPi);
709 TAINT_PROPER("umask");
712 /* Only DIE if trying to restrict permissions on "user" (self).
713 * Otherwise it's harmless and more useful to just return undef
714 * since 'group' and 'other' concepts probably don't exist here. */
715 if (MAXARG >= 1 && (POPi & 0700))
716 DIE(aTHX_ "umask not implemented");
717 XPUSHs(&PL_sv_undef);
739 if (gv && (io = GvIO(gv))
740 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
743 XPUSHs(SvTIED_obj((SV*)io, mg));
748 call_method("BINMODE", G_SCALAR);
755 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
756 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
757 report_evil_fh(gv, io, PL_op->op_type);
758 SETERRNO(EBADF,RMS_IFI);
763 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
764 (discp) ? SvPV_nolen_const(discp) : NULL)) {
765 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
766 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
767 mode_from_discipline(discp),
768 (discp) ? SvPV_nolen_const(discp) : NULL)) {
788 const I32 markoff = MARK - PL_stack_base;
789 const char *methname;
790 int how = PERL_MAGIC_tied;
794 switch(SvTYPE(varsv)) {
796 methname = "TIEHASH";
797 HvEITER_set((HV *)varsv, 0);
800 methname = "TIEARRAY";
803 #ifdef GV_UNIQUE_CHECK
804 if (GvUNIQUE((GV*)varsv)) {
805 Perl_croak(aTHX_ "Attempt to tie unique GV");
808 methname = "TIEHANDLE";
809 how = PERL_MAGIC_tiedscalar;
810 /* For tied filehandles, we apply tiedscalar magic to the IO
811 slot of the GP rather than the GV itself. AMS 20010812 */
813 GvIOp(varsv) = newIO();
814 varsv = (SV *)GvIOp(varsv);
817 methname = "TIESCALAR";
818 how = PERL_MAGIC_tiedscalar;
822 if (sv_isobject(*MARK)) {
824 PUSHSTACKi(PERLSI_MAGIC);
826 EXTEND(SP,(I32)items);
830 call_method(methname, G_SCALAR);
833 /* Not clear why we don't call call_method here too.
834 * perhaps to get different error message ?
836 stash = gv_stashsv(*MARK, FALSE);
837 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
838 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
842 PUSHSTACKi(PERLSI_MAGIC);
844 EXTEND(SP,(I32)items);
848 call_sv((SV*)GvCV(gv), G_SCALAR);
854 if (sv_isobject(sv)) {
855 sv_unmagic(varsv, how);
856 /* Croak if a self-tie on an aggregate is attempted. */
857 if (varsv == SvRV(sv) &&
858 (SvTYPE(varsv) == SVt_PVAV ||
859 SvTYPE(varsv) == SVt_PVHV))
861 "Self-ties of arrays and hashes are not supported");
862 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
865 SP = PL_stack_base + markoff;
875 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
876 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
878 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
881 if ((mg = SvTIED_mg(sv, how))) {
882 SV * const obj = SvRV(SvTIED_obj(sv, mg));
884 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
886 if (gv && isGV(gv) && (cv = GvCV(gv))) {
888 XPUSHs(SvTIED_obj((SV*)gv, mg));
889 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
892 call_sv((SV *)cv, G_VOID);
896 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
897 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
898 "untie attempted while %"UVuf" inner references still exist",
899 (UV)SvREFCNT(obj) - 1 ) ;
903 sv_unmagic(sv, how) ;
913 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
914 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
916 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
919 if ((mg = SvTIED_mg(sv, how))) {
920 SV *osv = SvTIED_obj(sv, mg);
921 if (osv == mg->mg_obj)
922 osv = sv_mortalcopy(osv);
936 HV * const hv = (HV*)POPs;
937 SV * const sv = sv_mortalcopy(&PL_sv_no);
939 sv_setpv(sv, "AnyDBM_File");
940 stash = gv_stashsv(sv, FALSE);
941 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
943 require_pv("AnyDBM_File.pm");
945 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
946 DIE(aTHX_ "No dbm on this machine");
956 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
958 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
961 call_sv((SV*)GvCV(gv), G_SCALAR);
964 if (!sv_isobject(TOPs)) {
969 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
972 call_sv((SV*)GvCV(gv), G_SCALAR);
976 if (sv_isobject(TOPs)) {
977 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
978 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, NULL, 0);
995 struct timeval timebuf;
996 struct timeval *tbuf = &timebuf;
999 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1004 # if BYTEORDER & 0xf0000
1005 # define ORDERBYTE (0x88888888 - BYTEORDER)
1007 # define ORDERBYTE (0x4444 - BYTEORDER)
1013 for (i = 1; i <= 3; i++) {
1014 SV * const sv = SP[i];
1017 if (SvREADONLY(sv)) {
1019 sv_force_normal_flags(sv, 0);
1020 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1021 DIE(aTHX_ PL_no_modify);
1024 if (ckWARN(WARN_MISC))
1025 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1026 SvPV_force_nolen(sv); /* force string conversion */
1033 /* little endians can use vecs directly */
1034 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1041 masksize = NFDBITS / NBBY;
1043 masksize = sizeof(long); /* documented int, everyone seems to use long */
1045 Zero(&fd_sets[0], 4, char*);
1048 # if SELECT_MIN_BITS == 1
1049 growsize = sizeof(fd_set);
1051 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1052 # undef SELECT_MIN_BITS
1053 # define SELECT_MIN_BITS __FD_SETSIZE
1055 /* If SELECT_MIN_BITS is greater than one we most probably will want
1056 * to align the sizes with SELECT_MIN_BITS/8 because for example
1057 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1058 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1059 * on (sets/tests/clears bits) is 32 bits. */
1060 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1068 timebuf.tv_sec = (long)value;
1069 value -= (NV)timebuf.tv_sec;
1070 timebuf.tv_usec = (long)(value * 1000000.0);
1075 for (i = 1; i <= 3; i++) {
1077 if (!SvOK(sv) || SvCUR(sv) == 0) {
1084 Sv_Grow(sv, growsize);
1088 while (++j <= growsize) {
1092 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1094 Newx(fd_sets[i], growsize, char);
1095 for (offset = 0; offset < growsize; offset += masksize) {
1096 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1097 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1100 fd_sets[i] = SvPVX(sv);
1104 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1105 /* Can't make just the (void*) conditional because that would be
1106 * cpp #if within cpp macro, and not all compilers like that. */
1107 nfound = PerlSock_select(
1109 (Select_fd_set_t) fd_sets[1],
1110 (Select_fd_set_t) fd_sets[2],
1111 (Select_fd_set_t) fd_sets[3],
1112 (void*) tbuf); /* Workaround for compiler bug. */
1114 nfound = PerlSock_select(
1116 (Select_fd_set_t) fd_sets[1],
1117 (Select_fd_set_t) fd_sets[2],
1118 (Select_fd_set_t) fd_sets[3],
1121 for (i = 1; i <= 3; i++) {
1124 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1126 for (offset = 0; offset < growsize; offset += masksize) {
1127 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1128 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1130 Safefree(fd_sets[i]);
1137 if (GIMME == G_ARRAY && tbuf) {
1138 value = (NV)(timebuf.tv_sec) +
1139 (NV)(timebuf.tv_usec) / 1000000.0;
1140 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1141 sv_setnv(sv, value);
1145 DIE(aTHX_ "select not implemented");
1150 Perl_setdefout(pTHX_ GV *gv)
1153 SvREFCNT_inc_simple_void(gv);
1155 SvREFCNT_dec(PL_defoutgv);
1163 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1164 GV * egv = GvEGV(PL_defoutgv);
1170 XPUSHs(&PL_sv_undef);
1172 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1173 if (gvp && *gvp == egv) {
1174 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1178 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1183 if (!GvIO(newdefout))
1184 gv_IOadd(newdefout);
1185 setdefout(newdefout);
1196 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1198 if (gv && (io = GvIO(gv))
1199 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1201 const I32 gimme = GIMME_V;
1203 XPUSHs(SvTIED_obj((SV*)io, mg));
1206 call_method("GETC", gimme);
1209 if (gimme == G_SCALAR)
1210 SvSetMagicSV_nosteal(TARG, TOPs);
1213 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1214 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1215 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1216 report_evil_fh(gv, io, PL_op->op_type);
1217 SETERRNO(EBADF,RMS_IFI);
1221 sv_setpvn(TARG, " ", 1);
1222 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1223 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1224 /* Find out how many bytes the char needs */
1225 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1228 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1229 SvCUR_set(TARG,1+len);
1238 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1241 register PERL_CONTEXT *cx;
1242 const I32 gimme = GIMME_V;
1247 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1249 cx->blk_sub.retop = retop;
1251 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1253 setdefout(gv); /* locally select filehandle so $% et al work */
1286 SV * const tmpsv = sv_newmortal();
1288 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1289 name = SvPV_nolen_const(tmpsv);
1291 DIE(aTHX_ "Undefined format \"%s\" called", name);
1293 DIE(aTHX_ "Not a format reference");
1296 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1298 IoFLAGS(io) &= ~IOf_DIDTOP;
1299 return doform(cv,gv,PL_op->op_next);
1305 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1306 register IO * const io = GvIOp(gv);
1307 PerlIO * const ofp = IoOFP(io);
1311 register PERL_CONTEXT *cx;
1313 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1314 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1317 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1318 PL_formtarget != PL_toptarget)
1322 if (!IoTOP_GV(io)) {
1325 if (!IoTOP_NAME(io)) {
1327 if (!IoFMT_NAME(io))
1328 IoFMT_NAME(io) = savepv(GvNAME(gv));
1329 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1330 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1331 if ((topgv && GvFORM(topgv)) ||
1332 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1333 IoTOP_NAME(io) = savesvpv(topname);
1335 IoTOP_NAME(io) = savepvs("top");
1337 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1338 if (!topgv || !GvFORM(topgv)) {
1339 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1342 IoTOP_GV(io) = topgv;
1344 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1345 I32 lines = IoLINES_LEFT(io);
1346 const char *s = SvPVX_const(PL_formtarget);
1347 if (lines <= 0) /* Yow, header didn't even fit!!! */
1349 while (lines-- > 0) {
1350 s = strchr(s, '\n');
1356 const STRLEN save = SvCUR(PL_formtarget);
1357 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1358 do_print(PL_formtarget, ofp);
1359 SvCUR_set(PL_formtarget, save);
1360 sv_chop(PL_formtarget, s);
1361 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1364 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1365 do_print(PL_formfeed, ofp);
1366 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1368 PL_formtarget = PL_toptarget;
1369 IoFLAGS(io) |= IOf_DIDTOP;
1372 DIE(aTHX_ "bad top format reference");
1375 SV * const sv = sv_newmortal();
1377 gv_efullname4(sv, fgv, NULL, FALSE);
1378 name = SvPV_nolen_const(sv);
1380 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1384 DIE(aTHX_ "Undefined top format called");
1387 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1388 return doform(cv,gv,PL_op);
1392 POPBLOCK(cx,PL_curpm);
1398 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1400 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1401 else if (ckWARN(WARN_CLOSED))
1402 report_evil_fh(gv, io, PL_op->op_type);
1407 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1408 if (ckWARN(WARN_IO))
1409 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1411 if (!do_print(PL_formtarget, fp))
1414 FmLINES(PL_formtarget) = 0;
1415 SvCUR_set(PL_formtarget, 0);
1416 *SvEND(PL_formtarget) = '\0';
1417 if (IoFLAGS(io) & IOf_FLUSH)
1418 (void)PerlIO_flush(fp);
1423 PL_formtarget = PL_bodytarget;
1425 PERL_UNUSED_VAR(newsp);
1426 PERL_UNUSED_VAR(gimme);
1427 return cx->blk_sub.retop;
1432 dVAR; dSP; dMARK; dORIGMARK;
1438 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1440 if (gv && (io = GvIO(gv))
1441 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1443 if (MARK == ORIGMARK) {
1446 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1450 *MARK = SvTIED_obj((SV*)io, mg);
1453 call_method("PRINTF", G_SCALAR);
1456 MARK = ORIGMARK + 1;
1463 if (!(io = GvIO(gv))) {
1464 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1465 report_evil_fh(gv, io, PL_op->op_type);
1466 SETERRNO(EBADF,RMS_IFI);
1469 else if (!(fp = IoOFP(io))) {
1470 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1472 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1473 else if (ckWARN(WARN_CLOSED))
1474 report_evil_fh(gv, io, PL_op->op_type);
1476 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1480 do_sprintf(sv, SP - MARK, MARK + 1);
1481 if (!do_print(sv, fp))
1484 if (IoFLAGS(io) & IOf_FLUSH)
1485 if (PerlIO_flush(fp) == EOF)
1496 PUSHs(&PL_sv_undef);
1504 const int perm = (MAXARG > 3) ? POPi : 0666;
1505 const int mode = POPi;
1506 SV * const sv = POPs;
1507 GV * const gv = (GV *)POPs;
1510 /* Need TIEHANDLE method ? */
1511 const char * const tmps = SvPV_const(sv, len);
1512 /* FIXME? do_open should do const */
1513 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1514 IoLINES(GvIOp(gv)) = 0;
1518 PUSHs(&PL_sv_undef);
1525 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1531 Sock_size_t bufsize;
1539 bool charstart = FALSE;
1540 STRLEN charskip = 0;
1543 GV * const gv = (GV*)*++MARK;
1544 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1545 && gv && (io = GvIO(gv)) )
1547 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1551 *MARK = SvTIED_obj((SV*)io, mg);
1553 call_method("READ", G_SCALAR);
1567 sv_setpvn(bufsv, "", 0);
1568 length = SvIVx(*++MARK);
1571 offset = SvIVx(*++MARK);
1575 if (!io || !IoIFP(io)) {
1576 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1577 report_evil_fh(gv, io, PL_op->op_type);
1578 SETERRNO(EBADF,RMS_IFI);
1581 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1582 buffer = SvPVutf8_force(bufsv, blen);
1583 /* UTF-8 may not have been set if they are all low bytes */
1588 buffer = SvPV_force(bufsv, blen);
1589 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1592 DIE(aTHX_ "Negative length");
1600 if (PL_op->op_type == OP_RECV) {
1601 char namebuf[MAXPATHLEN];
1602 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1603 bufsize = sizeof (struct sockaddr_in);
1605 bufsize = sizeof namebuf;
1607 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1611 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1612 /* 'offset' means 'flags' here */
1613 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1614 (struct sockaddr *)namebuf, &bufsize);
1618 /* Bogus return without padding */
1619 bufsize = sizeof (struct sockaddr_in);
1621 SvCUR_set(bufsv, count);
1622 *SvEND(bufsv) = '\0';
1623 (void)SvPOK_only(bufsv);
1627 /* This should not be marked tainted if the fp is marked clean */
1628 if (!(IoFLAGS(io) & IOf_UNTAINT))
1629 SvTAINTED_on(bufsv);
1631 sv_setpvn(TARG, namebuf, bufsize);
1636 if (PL_op->op_type == OP_RECV)
1637 DIE(aTHX_ PL_no_sock_func, "recv");
1639 if (DO_UTF8(bufsv)) {
1640 /* offset adjust in characters not bytes */
1641 blen = sv_len_utf8(bufsv);
1644 if (-offset > (int)blen)
1645 DIE(aTHX_ "Offset outside string");
1648 if (DO_UTF8(bufsv)) {
1649 /* convert offset-as-chars to offset-as-bytes */
1650 if (offset >= (int)blen)
1651 offset += SvCUR(bufsv) - blen;
1653 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1656 bufsize = SvCUR(bufsv);
1657 /* Allocating length + offset + 1 isn't perfect in the case of reading
1658 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1660 (should be 2 * length + offset + 1, or possibly something longer if
1661 PL_encoding is true) */
1662 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1663 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1664 Zero(buffer+bufsize, offset-bufsize, char);
1666 buffer = buffer + offset;
1668 read_target = bufsv;
1670 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1671 concatenate it to the current buffer. */
1673 /* Truncate the existing buffer to the start of where we will be
1675 SvCUR_set(bufsv, offset);
1677 read_target = sv_newmortal();
1678 SvUPGRADE(read_target, SVt_PV);
1679 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1682 if (PL_op->op_type == OP_SYSREAD) {
1683 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1684 if (IoTYPE(io) == IoTYPE_SOCKET) {
1685 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1691 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1696 #ifdef HAS_SOCKET__bad_code_maybe
1697 if (IoTYPE(io) == IoTYPE_SOCKET) {
1698 char namebuf[MAXPATHLEN];
1699 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1700 bufsize = sizeof (struct sockaddr_in);
1702 bufsize = sizeof namebuf;
1704 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1705 (struct sockaddr *)namebuf, &bufsize);
1710 count = PerlIO_read(IoIFP(io), buffer, length);
1711 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1712 if (count == 0 && PerlIO_error(IoIFP(io)))
1716 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1717 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1720 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1721 *SvEND(read_target) = '\0';
1722 (void)SvPOK_only(read_target);
1723 if (fp_utf8 && !IN_BYTES) {
1724 /* Look at utf8 we got back and count the characters */
1725 const char *bend = buffer + count;
1726 while (buffer < bend) {
1728 skip = UTF8SKIP(buffer);
1731 if (buffer - charskip + skip > bend) {
1732 /* partial character - try for rest of it */
1733 length = skip - (bend-buffer);
1734 offset = bend - SvPVX_const(bufsv);
1746 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1747 provided amount read (count) was what was requested (length)
1749 if (got < wanted && count == length) {
1750 length = wanted - got;
1751 offset = bend - SvPVX_const(bufsv);
1754 /* return value is character count */
1758 else if (buffer_utf8) {
1759 /* Let svcatsv upgrade the bytes we read in to utf8.
1760 The buffer is a mortal so will be freed soon. */
1761 sv_catsv_nomg(bufsv, read_target);
1764 /* This should not be marked tainted if the fp is marked clean */
1765 if (!(IoFLAGS(io) & IOf_UNTAINT))
1766 SvTAINTED_on(bufsv);
1778 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1786 const int op_type = PL_op->op_type;
1788 GV *const gv = (GV*)*++MARK;
1789 if (PL_op->op_type == OP_SYSWRITE
1790 && gv && (io = GvIO(gv))
1791 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1795 if (MARK == SP - 1) {
1797 sv = sv_2mortal(newSViv(sv_len(*SP)));
1803 *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
1805 call_method("WRITE", G_SCALAR);
1818 if (op_type == OP_SYSWRITE) {
1820 length = (Size_t) sv_len(bufsv);
1822 #if Size_t_size > IVSIZE
1823 length = (Size_t)SvNVx(*++MARK);
1825 length = (Size_t)SvIVx(*++MARK);
1827 if ((SSize_t)length < 0)
1828 DIE(aTHX_ "Negative length");
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 if (PerlIO_isutf8(IoIFP(io))) {
1842 if (!SvUTF8(bufsv)) {
1843 bufsv = sv_2mortal(newSVsv(bufsv));
1844 buffer = sv_2pvutf8(bufsv, &blen);
1846 buffer = SvPV_const(bufsv, blen);
1849 if (DO_UTF8(bufsv)) {
1850 /* Not modifying source SV, so making a temporary copy. */
1851 bufsv = sv_2mortal(newSVsv(bufsv));
1852 sv_utf8_downgrade(bufsv, FALSE);
1854 buffer = SvPV_const(bufsv, blen);
1857 if (op_type == OP_SYSWRITE) {
1859 if (DO_UTF8(bufsv)) {
1860 /* length and offset are in chars */
1861 blen = sv_len_utf8(bufsv);
1864 offset = SvIVx(*++MARK);
1866 if (-offset > (IV)blen)
1867 DIE(aTHX_ "Offset outside string");
1869 } else if (offset >= (IV)blen && blen > 0)
1870 DIE(aTHX_ "Offset outside string");
1873 if (length > blen - offset)
1874 length = blen - offset;
1875 if (DO_UTF8(bufsv)) {
1876 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1877 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1880 buffer = buffer+offset;
1882 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1883 if (IoTYPE(io) == IoTYPE_SOCKET) {
1884 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1890 /* See the note at doio.c:do_print about filesize limits. --jhi */
1891 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1897 const int flags = SvIVx(*++MARK);
1900 char * const sockbuf = SvPVx(*++MARK, mlen);
1901 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1902 flags, (struct sockaddr *)sockbuf, mlen);
1906 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1911 DIE(aTHX_ PL_no_sock_func, "send");
1917 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1918 #if Size_t_size > IVSIZE
1936 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1938 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1940 if (io && !IoIFP(io)) {
1941 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1943 IoFLAGS(io) &= ~IOf_START;
1944 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
1945 sv_setpvn(GvSV(gv), "-", 1);
1946 SvSETMAGIC(GvSV(gv));
1948 else if (!nextargv(gv))
1953 gv = PL_last_in_gv; /* eof */
1956 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1959 IO * const io = GvIO(gv);
1961 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1963 XPUSHs(SvTIED_obj((SV*)io, mg));
1966 call_method("EOF", G_SCALAR);
1973 PUSHs(boolSV(!gv || do_eof(gv)));
1985 PL_last_in_gv = (GV*)POPs;
1988 if (gv && (io = GvIO(gv))
1989 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1992 XPUSHs(SvTIED_obj((SV*)io, mg));
1995 call_method("TELL", G_SCALAR);
2001 #if LSEEKSIZE > IVSIZE
2002 PUSHn( do_tell(gv) );
2004 PUSHi( do_tell(gv) );
2013 const int whence = POPi;
2014 #if LSEEKSIZE > IVSIZE
2015 const Off_t offset = (Off_t)SvNVx(POPs);
2017 const Off_t offset = (Off_t)SvIVx(POPs);
2021 GV * const gv = PL_last_in_gv = (GV*)POPs;
2023 if (gv && (io = GvIO(gv))
2024 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2027 XPUSHs(SvTIED_obj((SV*)io, mg));
2028 #if LSEEKSIZE > IVSIZE
2029 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2031 XPUSHs(sv_2mortal(newSViv(offset)));
2033 XPUSHs(sv_2mortal(newSViv(whence)));
2036 call_method("SEEK", G_SCALAR);
2042 if (PL_op->op_type == OP_SEEK)
2043 PUSHs(boolSV(do_seek(gv, offset, whence)));
2045 const Off_t sought = do_sysseek(gv, offset, whence);
2047 PUSHs(&PL_sv_undef);
2049 SV* const sv = sought ?
2050 #if LSEEKSIZE > IVSIZE
2055 : newSVpvn(zero_but_true, ZBTLEN);
2056 PUSHs(sv_2mortal(sv));
2066 /* There seems to be no consensus on the length type of truncate()
2067 * and ftruncate(), both off_t and size_t have supporters. In
2068 * general one would think that when using large files, off_t is
2069 * at least as wide as size_t, so using an off_t should be okay. */
2070 /* XXX Configure probe for the length type of *truncate() needed XXX */
2073 #if Off_t_size > IVSIZE
2078 /* Checking for length < 0 is problematic as the type might or
2079 * might not be signed: if it is not, clever compilers will moan. */
2080 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2087 if (PL_op->op_flags & OPf_SPECIAL) {
2088 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2097 TAINT_PROPER("truncate");
2098 if (!(fp = IoIFP(io))) {
2104 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2106 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2113 SV * const sv = POPs;
2116 if (SvTYPE(sv) == SVt_PVGV) {
2117 tmpgv = (GV*)sv; /* *main::FRED for example */
2118 goto do_ftruncate_gv;
2120 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2121 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2122 goto do_ftruncate_gv;
2124 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2125 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2126 goto do_ftruncate_io;
2129 name = SvPV_nolen_const(sv);
2130 TAINT_PROPER("truncate");
2132 if (truncate(name, len) < 0)
2136 const int tmpfd = PerlLIO_open(name, O_RDWR);
2141 if (my_chsize(tmpfd, len) < 0)
2143 PerlLIO_close(tmpfd);
2152 SETERRNO(EBADF,RMS_IFI);
2160 SV * const argsv = POPs;
2161 const unsigned int func = POPu;
2162 const int optype = PL_op->op_type;
2163 GV * const gv = (GV*)POPs;
2164 IO * const io = gv ? GvIOn(gv) : NULL;
2168 if (!io || !argsv || !IoIFP(io)) {
2169 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2170 report_evil_fh(gv, io, PL_op->op_type);
2171 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2175 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2178 s = SvPV_force(argsv, len);
2179 need = IOCPARM_LEN(func);
2181 s = Sv_Grow(argsv, need + 1);
2182 SvCUR_set(argsv, need);
2185 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2188 retval = SvIV(argsv);
2189 s = INT2PTR(char*,retval); /* ouch */
2192 TAINT_PROPER(PL_op_desc[optype]);
2194 if (optype == OP_IOCTL)
2196 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2198 DIE(aTHX_ "ioctl is not implemented");
2202 DIE(aTHX_ "fcntl is not implemented");
2204 #if defined(OS2) && defined(__EMX__)
2205 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2207 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2211 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2213 if (s[SvCUR(argsv)] != 17)
2214 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2216 s[SvCUR(argsv)] = 0; /* put our null back */
2217 SvSETMAGIC(argsv); /* Assume it has changed */
2226 PUSHp(zero_but_true, ZBTLEN);
2239 const int argtype = POPi;
2240 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : (GV*)POPs;
2242 if (gv && (io = GvIO(gv)))
2248 /* XXX Looks to me like io is always NULL at this point */
2250 (void)PerlIO_flush(fp);
2251 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2254 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2255 report_evil_fh(gv, io, PL_op->op_type);
2257 SETERRNO(EBADF,RMS_IFI);
2262 DIE(aTHX_ PL_no_func, "flock()");
2272 const int protocol = POPi;
2273 const int type = POPi;
2274 const int domain = POPi;
2275 GV * const gv = (GV*)POPs;
2276 register IO * const io = gv ? GvIOn(gv) : NULL;
2280 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2281 report_evil_fh(gv, io, PL_op->op_type);
2283 do_close(gv, FALSE);
2284 SETERRNO(EBADF,LIB_INVARG);
2289 do_close(gv, FALSE);
2291 TAINT_PROPER("socket");
2292 fd = PerlSock_socket(domain, type, protocol);
2295 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2296 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2297 IoTYPE(io) = IoTYPE_SOCKET;
2298 if (!IoIFP(io) || !IoOFP(io)) {
2299 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2300 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2301 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2304 #if defined(HAS_FCNTL) && defined(F_SETFD)
2305 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2309 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2314 DIE(aTHX_ PL_no_sock_func, "socket");
2320 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2322 const int protocol = POPi;
2323 const int type = POPi;
2324 const int domain = POPi;
2325 GV * const gv2 = (GV*)POPs;
2326 GV * const gv1 = (GV*)POPs;
2327 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2328 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2331 if (!gv1 || !gv2 || !io1 || !io2) {
2332 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2334 report_evil_fh(gv1, io1, PL_op->op_type);
2336 report_evil_fh(gv1, io2, PL_op->op_type);
2339 do_close(gv1, FALSE);
2341 do_close(gv2, FALSE);
2346 do_close(gv1, FALSE);
2348 do_close(gv2, FALSE);
2350 TAINT_PROPER("socketpair");
2351 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2353 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2354 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2355 IoTYPE(io1) = IoTYPE_SOCKET;
2356 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2357 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2358 IoTYPE(io2) = IoTYPE_SOCKET;
2359 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2360 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2361 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2362 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2363 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2364 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2365 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2368 #if defined(HAS_FCNTL) && defined(F_SETFD)
2369 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2370 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2375 DIE(aTHX_ PL_no_sock_func, "socketpair");
2383 SV * const addrsv = POPs;
2384 /* OK, so on what platform does bind modify addr? */
2386 GV * const gv = (GV*)POPs;
2387 register IO * const io = GvIOn(gv);
2391 if (!io || !IoIFP(io))
2394 addr = SvPV_const(addrsv, len);
2395 TAINT_PROPER("bind");
2396 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2397 (struct sockaddr *)addr, len) >= 0)
2407 if (ckWARN(WARN_CLOSED))
2408 report_evil_fh(gv, io, PL_op->op_type);
2409 SETERRNO(EBADF,SS_IVCHAN);
2412 DIE(aTHX_ PL_no_sock_func, "bind");
2420 SV * const addrsv = POPs;
2421 GV * const gv = (GV*)POPs;
2422 register IO * const io = GvIOn(gv);
2426 if (!io || !IoIFP(io))
2429 addr = SvPV_const(addrsv, len);
2430 TAINT_PROPER("connect");
2431 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2437 if (ckWARN(WARN_CLOSED))
2438 report_evil_fh(gv, io, PL_op->op_type);
2439 SETERRNO(EBADF,SS_IVCHAN);
2442 DIE(aTHX_ PL_no_sock_func, "connect");
2450 const int backlog = POPi;
2451 GV * const gv = (GV*)POPs;
2452 register IO * const io = gv ? GvIOn(gv) : NULL;
2454 if (!gv || !io || !IoIFP(io))
2457 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2463 if (ckWARN(WARN_CLOSED))
2464 report_evil_fh(gv, io, PL_op->op_type);
2465 SETERRNO(EBADF,SS_IVCHAN);
2468 DIE(aTHX_ PL_no_sock_func, "listen");
2478 char namebuf[MAXPATHLEN];
2479 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2480 Sock_size_t len = sizeof (struct sockaddr_in);
2482 Sock_size_t len = sizeof namebuf;
2484 GV * const ggv = (GV*)POPs;
2485 GV * const ngv = (GV*)POPs;
2494 if (!gstio || !IoIFP(gstio))
2498 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2502 do_close(ngv, FALSE);
2503 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2504 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2505 IoTYPE(nstio) = IoTYPE_SOCKET;
2506 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2507 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2508 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2509 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2512 #if defined(HAS_FCNTL) && defined(F_SETFD)
2513 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2517 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2518 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2520 #ifdef __SCO_VERSION__
2521 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2524 PUSHp(namebuf, len);
2528 if (ckWARN(WARN_CLOSED))
2529 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2530 SETERRNO(EBADF,SS_IVCHAN);
2536 DIE(aTHX_ PL_no_sock_func, "accept");
2544 const int how = POPi;
2545 GV * const gv = (GV*)POPs;
2546 register IO * const io = GvIOn(gv);
2548 if (!io || !IoIFP(io))
2551 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2555 if (ckWARN(WARN_CLOSED))
2556 report_evil_fh(gv, io, PL_op->op_type);
2557 SETERRNO(EBADF,SS_IVCHAN);
2560 DIE(aTHX_ PL_no_sock_func, "shutdown");
2568 const int optype = PL_op->op_type;
2569 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2570 const unsigned int optname = (unsigned int) POPi;
2571 const unsigned int lvl = (unsigned int) POPi;
2572 GV * const gv = (GV*)POPs;
2573 register IO * const io = GvIOn(gv);
2577 if (!io || !IoIFP(io))
2580 fd = PerlIO_fileno(IoIFP(io));
2584 (void)SvPOK_only(sv);
2588 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2595 #if defined(__SYMBIAN32__)
2596 # define SETSOCKOPT_OPTION_VALUE_T void *
2598 # define SETSOCKOPT_OPTION_VALUE_T const char *
2600 /* XXX TODO: We need to have a proper type (a Configure probe,
2601 * etc.) for what the C headers think of the third argument of
2602 * setsockopt(), the option_value read-only buffer: is it
2603 * a "char *", or a "void *", const or not. Some compilers
2604 * don't take kindly to e.g. assuming that "char *" implicitly
2605 * promotes to a "void *", or to explicitly promoting/demoting
2606 * consts to non/vice versa. The "const void *" is the SUS
2607 * definition, but that does not fly everywhere for the above
2609 SETSOCKOPT_OPTION_VALUE_T buf;
2613 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2617 aint = (int)SvIV(sv);
2618 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2621 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2630 if (ckWARN(WARN_CLOSED))
2631 report_evil_fh(gv, io, optype);
2632 SETERRNO(EBADF,SS_IVCHAN);
2637 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2645 const int optype = PL_op->op_type;
2646 GV * const gv = (GV*)POPs;
2647 register IO * const io = GvIOn(gv);
2652 if (!io || !IoIFP(io))
2655 sv = sv_2mortal(newSV(257));
2656 (void)SvPOK_only(sv);
2660 fd = PerlIO_fileno(IoIFP(io));
2662 case OP_GETSOCKNAME:
2663 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2666 case OP_GETPEERNAME:
2667 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2669 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2671 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";
2672 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2673 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2674 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2675 sizeof(u_short) + sizeof(struct in_addr))) {
2682 #ifdef BOGUS_GETNAME_RETURN
2683 /* Interactive Unix, getpeername() and getsockname()
2684 does not return valid namelen */
2685 if (len == BOGUS_GETNAME_RETURN)
2686 len = sizeof(struct sockaddr);
2694 if (ckWARN(WARN_CLOSED))
2695 report_evil_fh(gv, io, optype);
2696 SETERRNO(EBADF,SS_IVCHAN);
2701 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2715 if (PL_op->op_flags & OPf_REF) {
2717 if (PL_op->op_type == OP_LSTAT) {
2718 if (gv != PL_defgv) {
2719 do_fstat_warning_check:
2720 if (ckWARN(WARN_IO))
2721 Perl_warner(aTHX_ packWARN(WARN_IO),
2722 "lstat() on filehandle %s", GvENAME(gv));
2723 } else if (PL_laststype != OP_LSTAT)
2724 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2728 if (gv != PL_defgv) {
2729 PL_laststype = OP_STAT;
2731 sv_setpvn(PL_statname, "", 0);
2732 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2733 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2735 if (PL_laststatval < 0) {
2736 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2737 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2742 SV* const sv = POPs;
2743 if (SvTYPE(sv) == SVt_PVGV) {
2747 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2749 if (PL_op->op_type == OP_LSTAT)
2750 goto do_fstat_warning_check;
2753 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2755 PL_laststype = PL_op->op_type;
2756 if (PL_op->op_type == OP_LSTAT)
2757 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2759 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2760 if (PL_laststatval < 0) {
2761 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2762 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2768 if (gimme != G_ARRAY) {
2769 if (gimme != G_VOID)
2770 XPUSHs(boolSV(max));
2776 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2777 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2778 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2779 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2780 #if Uid_t_size > IVSIZE
2781 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2783 # if Uid_t_sign <= 0
2784 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2786 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2789 #if Gid_t_size > IVSIZE
2790 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2792 # if Gid_t_sign <= 0
2793 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2795 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2798 #ifdef USE_STAT_RDEV
2799 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2801 PUSHs(sv_2mortal(newSVpvs("")));
2803 #if Off_t_size > IVSIZE
2804 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2806 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2809 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2810 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2811 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2813 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2814 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2815 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2817 #ifdef USE_STAT_BLOCKS
2818 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2819 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2821 PUSHs(sv_2mortal(newSVpvs("")));
2822 PUSHs(sv_2mortal(newSVpvs("")));
2828 /* This macro is used by the stacked filetest operators :
2829 * if the previous filetest failed, short-circuit and pass its value.
2830 * Else, discard it from the stack and continue. --rgs
2832 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2833 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2834 else { (void)POPs; PUTBACK; } \
2841 /* Not const, because things tweak this below. Not bool, because there's
2842 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2843 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2844 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2845 /* Giving some sort of initial value silences compilers. */
2847 int access_mode = R_OK;
2849 int access_mode = 0;
2852 /* access_mode is never used, but leaving use_access in makes the
2853 conditional compiling below much clearer. */
2856 int stat_mode = S_IRUSR;
2858 bool effective = FALSE;
2861 STACKED_FTEST_CHECK;
2863 switch (PL_op->op_type) {
2865 #if !(defined(HAS_ACCESS) && defined(R_OK))
2871 #if defined(HAS_ACCESS) && defined(W_OK)
2876 stat_mode = S_IWUSR;
2880 #if defined(HAS_ACCESS) && defined(X_OK)
2885 stat_mode = S_IXUSR;
2889 #ifdef PERL_EFF_ACCESS
2892 stat_mode = S_IWUSR;
2896 #ifndef PERL_EFF_ACCESS
2904 #ifdef PERL_EFF_ACCESS
2909 stat_mode = S_IXUSR;
2915 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2916 const char *const name = POPpx;
2918 # ifdef PERL_EFF_ACCESS
2919 result = PERL_EFF_ACCESS(name, access_mode);
2921 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
2927 result = access(name, access_mode);
2929 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
2944 if (cando(stat_mode, effective, &PL_statcache))
2953 const int op_type = PL_op->op_type;
2955 STACKED_FTEST_CHECK;
2960 if (op_type == OP_FTIS)
2963 /* You can't dTARGET inside OP_FTIS, because you'll get
2964 "panic: pad_sv po" - the op is not flagged to have a target. */
2968 #if Off_t_size > IVSIZE
2969 PUSHn(PL_statcache.st_size);
2971 PUSHi(PL_statcache.st_size);
2975 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
2978 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
2981 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
2994 /* I believe that all these three are likely to be defined on most every
2995 system these days. */
2997 if(PL_op->op_type == OP_FTSUID)
3001 if(PL_op->op_type == OP_FTSGID)
3005 if(PL_op->op_type == OP_FTSVTX)
3009 STACKED_FTEST_CHECK;
3014 switch (PL_op->op_type) {
3016 if (PL_statcache.st_uid == PL_uid)
3020 if (PL_statcache.st_uid == PL_euid)
3024 if (PL_statcache.st_size == 0)
3028 if (S_ISSOCK(PL_statcache.st_mode))
3032 if (S_ISCHR(PL_statcache.st_mode))
3036 if (S_ISBLK(PL_statcache.st_mode))
3040 if (S_ISREG(PL_statcache.st_mode))
3044 if (S_ISDIR(PL_statcache.st_mode))
3048 if (S_ISFIFO(PL_statcache.st_mode))
3053 if (PL_statcache.st_mode & S_ISUID)
3059 if (PL_statcache.st_mode & S_ISGID)
3065 if (PL_statcache.st_mode & S_ISVTX)
3076 I32 result = my_lstat();
3080 if (S_ISLNK(PL_statcache.st_mode))
3093 STACKED_FTEST_CHECK;
3095 if (PL_op->op_flags & OPf_REF)
3097 else if (isGV(TOPs))
3099 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3100 gv = (GV*)SvRV(POPs);
3102 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3104 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3105 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3106 else if (tmpsv && SvOK(tmpsv)) {
3107 const char *tmps = SvPV_nolen_const(tmpsv);
3115 if (PerlLIO_isatty(fd))
3120 #if defined(atarist) /* this will work with atariST. Configure will
3121 make guesses for other systems. */
3122 # define FILE_base(f) ((f)->_base)
3123 # define FILE_ptr(f) ((f)->_ptr)
3124 # define FILE_cnt(f) ((f)->_cnt)
3125 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3136 register STDCHAR *s;
3142 STACKED_FTEST_CHECK;
3144 if (PL_op->op_flags & OPf_REF)
3146 else if (isGV(TOPs))
3148 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3149 gv = (GV*)SvRV(POPs);
3155 if (gv == PL_defgv) {
3157 io = GvIO(PL_statgv);
3160 goto really_filename;
3165 PL_laststatval = -1;
3166 sv_setpvn(PL_statname, "", 0);
3167 io = GvIO(PL_statgv);
3169 if (io && IoIFP(io)) {
3170 if (! PerlIO_has_base(IoIFP(io)))
3171 DIE(aTHX_ "-T and -B not implemented on filehandles");
3172 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3173 if (PL_laststatval < 0)
3175 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3176 if (PL_op->op_type == OP_FTTEXT)
3181 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3182 i = PerlIO_getc(IoIFP(io));
3184 (void)PerlIO_ungetc(IoIFP(io),i);
3186 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3188 len = PerlIO_get_bufsiz(IoIFP(io));
3189 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3190 /* sfio can have large buffers - limit to 512 */
3195 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3197 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3199 SETERRNO(EBADF,RMS_IFI);
3207 PL_laststype = OP_STAT;
3208 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3209 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3210 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3212 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3215 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3216 if (PL_laststatval < 0) {
3217 (void)PerlIO_close(fp);
3220 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3221 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3222 (void)PerlIO_close(fp);
3224 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3225 RETPUSHNO; /* special case NFS directories */
3226 RETPUSHYES; /* null file is anything */
3231 /* now scan s to look for textiness */
3232 /* XXX ASCII dependent code */
3234 #if defined(DOSISH) || defined(USEMYBINMODE)
3235 /* ignore trailing ^Z on short files */
3236 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3240 for (i = 0; i < len; i++, s++) {
3241 if (!*s) { /* null never allowed in text */
3246 else if (!(isPRINT(*s) || isSPACE(*s)))
3249 else if (*s & 128) {
3251 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3254 /* utf8 characters don't count as odd */
3255 if (UTF8_IS_START(*s)) {
3256 int ulen = UTF8SKIP(s);
3257 if (ulen < len - i) {
3259 for (j = 1; j < ulen; j++) {
3260 if (!UTF8_IS_CONTINUATION(s[j]))
3263 --ulen; /* loop does extra increment */
3273 *s != '\n' && *s != '\r' && *s != '\b' &&
3274 *s != '\t' && *s != '\f' && *s != 27)
3279 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3290 const char *tmps = NULL;
3294 SV * const sv = POPs;
3295 if (PL_op->op_flags & OPf_SPECIAL) {
3296 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3298 else if (SvTYPE(sv) == SVt_PVGV) {
3301 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3305 tmps = SvPVx_nolen_const(sv);
3309 if( !gv && (!tmps || !*tmps) ) {
3310 HV * const table = GvHVn(PL_envgv);
3313 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3314 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3316 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3321 deprecate("chdir('') or chdir(undef) as chdir()");
3322 tmps = SvPV_nolen_const(*svp);
3326 TAINT_PROPER("chdir");
3331 TAINT_PROPER("chdir");
3334 IO* const io = GvIO(gv);
3337 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3339 else if (IoDIRP(io)) {
3341 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3343 DIE(aTHX_ PL_no_func, "dirfd");
3347 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3348 report_evil_fh(gv, io, PL_op->op_type);
3349 SETERRNO(EBADF, RMS_IFI);
3354 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3355 report_evil_fh(gv, io, PL_op->op_type);
3356 SETERRNO(EBADF,RMS_IFI);
3360 DIE(aTHX_ PL_no_func, "fchdir");
3364 PUSHi( PerlDir_chdir(tmps) >= 0 );
3366 /* Clear the DEFAULT element of ENV so we'll get the new value
3368 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3375 dVAR; dSP; dMARK; dTARGET;
3376 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3387 char * const tmps = POPpx;
3388 TAINT_PROPER("chroot");
3389 PUSHi( chroot(tmps) >= 0 );
3392 DIE(aTHX_ PL_no_func, "chroot");
3400 const char * const tmps2 = POPpconstx;
3401 const char * const tmps = SvPV_nolen_const(TOPs);
3402 TAINT_PROPER("rename");
3404 anum = PerlLIO_rename(tmps, tmps2);
3406 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3407 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3410 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3411 (void)UNLINK(tmps2);
3412 if (!(anum = link(tmps, tmps2)))
3413 anum = UNLINK(tmps);
3421 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3425 const int op_type = PL_op->op_type;
3429 if (op_type == OP_LINK)
3430 DIE(aTHX_ PL_no_func, "link");
3432 # ifndef HAS_SYMLINK
3433 if (op_type == OP_SYMLINK)
3434 DIE(aTHX_ PL_no_func, "symlink");
3438 const char * const tmps2 = POPpconstx;
3439 const char * const tmps = SvPV_nolen_const(TOPs);
3440 TAINT_PROPER(PL_op_desc[op_type]);
3442 # if defined(HAS_LINK)
3443 # if defined(HAS_SYMLINK)
3444 /* Both present - need to choose which. */
3445 (op_type == OP_LINK) ?
3446 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3448 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3449 PerlLIO_link(tmps, tmps2);
3452 # if defined(HAS_SYMLINK)
3453 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3454 symlink(tmps, tmps2);
3459 SETi( result >= 0 );
3466 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3477 char buf[MAXPATHLEN];
3480 #ifndef INCOMPLETE_TAINTS
3484 len = readlink(tmps, buf, sizeof(buf) - 1);
3492 RETSETUNDEF; /* just pretend it's a normal file */
3496 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3498 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3500 char * const save_filename = filename;
3506 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3507 strcpy(cmdline, cmd);
3508 strcat(cmdline, " ");
3509 for (s = cmdline + strlen(cmdline); *filename; ) {
3514 myfp = PerlProc_popen(cmdline, "r");
3518 SV * const tmpsv = sv_newmortal();
3519 /* Need to save/restore 'PL_rs' ?? */
3520 s = sv_gets(tmpsv, myfp, 0);
3521 (void)PerlProc_pclose(myfp);
3525 #ifdef HAS_SYS_ERRLIST
3530 /* you don't see this */
3531 const char * const errmsg =
3532 #ifdef HAS_SYS_ERRLIST
3540 if (instr(s, errmsg)) {
3547 #define EACCES EPERM
3549 if (instr(s, "cannot make"))
3550 SETERRNO(EEXIST,RMS_FEX);
3551 else if (instr(s, "existing file"))
3552 SETERRNO(EEXIST,RMS_FEX);
3553 else if (instr(s, "ile exists"))
3554 SETERRNO(EEXIST,RMS_FEX);
3555 else if (instr(s, "non-exist"))
3556 SETERRNO(ENOENT,RMS_FNF);
3557 else if (instr(s, "does not exist"))
3558 SETERRNO(ENOENT,RMS_FNF);
3559 else if (instr(s, "not empty"))
3560 SETERRNO(EBUSY,SS_DEVOFFLINE);
3561 else if (instr(s, "cannot access"))
3562 SETERRNO(EACCES,RMS_PRV);
3564 SETERRNO(EPERM,RMS_PRV);
3567 else { /* some mkdirs return no failure indication */
3568 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3569 if (PL_op->op_type == OP_RMDIR)
3574 SETERRNO(EACCES,RMS_PRV); /* a guess */
3583 /* This macro removes trailing slashes from a directory name.
3584 * Different operating and file systems take differently to
3585 * trailing slashes. According to POSIX 1003.1 1996 Edition
3586 * any number of trailing slashes should be allowed.
3587 * Thusly we snip them away so that even non-conforming
3588 * systems are happy.
3589 * We should probably do this "filtering" for all
3590 * the functions that expect (potentially) directory names:
3591 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3592 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3594 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3595 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3598 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3599 (tmps) = savepvn((tmps), (len)); \
3609 const int mode = (MAXARG > 1) ? POPi : 0777;
3611 TRIMSLASHES(tmps,len,copy);
3613 TAINT_PROPER("mkdir");
3615 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3619 SETi( dooneliner("mkdir", tmps) );
3620 oldumask = PerlLIO_umask(0);
3621 PerlLIO_umask(oldumask);
3622 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3637 TRIMSLASHES(tmps,len,copy);
3638 TAINT_PROPER("rmdir");
3640 SETi( PerlDir_rmdir(tmps) >= 0 );
3642 SETi( dooneliner("rmdir", tmps) );
3649 /* Directory calls. */
3653 #if defined(Direntry_t) && defined(HAS_READDIR)
3655 const char * const dirname = POPpconstx;
3656 GV * const gv = (GV*)POPs;
3657 register IO * const io = GvIOn(gv);
3663 PerlDir_close(IoDIRP(io));
3664 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3670 SETERRNO(EBADF,RMS_DIR);
3673 DIE(aTHX_ PL_no_dir_func, "opendir");
3679 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3680 DIE(aTHX_ PL_no_dir_func, "readdir");
3682 #if !defined(I_DIRENT) && !defined(VMS)
3683 Direntry_t *readdir (DIR *);
3689 const I32 gimme = GIMME;
3690 GV * const gv = (GV *)POPs;
3691 register const Direntry_t *dp;
3692 register IO * const io = GvIOn(gv);
3694 if (!io || !IoDIRP(io)) {
3695 if(ckWARN(WARN_IO)) {
3696 Perl_warner(aTHX_ packWARN(WARN_IO),
3697 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3703 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3707 sv = newSVpvn(dp->d_name, dp->d_namlen);
3709 sv = newSVpv(dp->d_name, 0);
3711 #ifndef INCOMPLETE_TAINTS
3712 if (!(IoFLAGS(io) & IOf_UNTAINT))
3715 XPUSHs(sv_2mortal(sv));
3717 while (gimme == G_ARRAY);
3719 if (!dp && gimme != G_ARRAY)
3726 SETERRNO(EBADF,RMS_ISI);
3727 if (GIMME == G_ARRAY)
3736 #if defined(HAS_TELLDIR) || defined(telldir)
3738 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3739 /* XXX netbsd still seemed to.
3740 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3741 --JHI 1999-Feb-02 */
3742 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3743 long telldir (DIR *);
3745 GV * const gv = (GV*)POPs;
3746 register IO * const io = GvIOn(gv);
3748 if (!io || !IoDIRP(io)) {
3749 if(ckWARN(WARN_IO)) {
3750 Perl_warner(aTHX_ packWARN(WARN_IO),
3751 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3756 PUSHi( PerlDir_tell(IoDIRP(io)) );
3760 SETERRNO(EBADF,RMS_ISI);
3763 DIE(aTHX_ PL_no_dir_func, "telldir");
3769 #if defined(HAS_SEEKDIR) || defined(seekdir)
3771 const long along = POPl;
3772 GV * const gv = (GV*)POPs;
3773 register IO * const io = GvIOn(gv);
3775 if (!io || !IoDIRP(io)) {
3776 if(ckWARN(WARN_IO)) {
3777 Perl_warner(aTHX_ packWARN(WARN_IO),
3778 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3782 (void)PerlDir_seek(IoDIRP(io), along);
3787 SETERRNO(EBADF,RMS_ISI);
3790 DIE(aTHX_ PL_no_dir_func, "seekdir");
3796 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3798 GV * const gv = (GV*)POPs;
3799 register IO * const io = GvIOn(gv);
3801 if (!io || !IoDIRP(io)) {
3802 if(ckWARN(WARN_IO)) {
3803 Perl_warner(aTHX_ packWARN(WARN_IO),
3804 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3808 (void)PerlDir_rewind(IoDIRP(io));
3812 SETERRNO(EBADF,RMS_ISI);
3815 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3821 #if defined(Direntry_t) && defined(HAS_READDIR)
3823 GV * const gv = (GV*)POPs;
3824 register IO * const io = GvIOn(gv);
3826 if (!io || !IoDIRP(io)) {
3827 if(ckWARN(WARN_IO)) {
3828 Perl_warner(aTHX_ packWARN(WARN_IO),
3829 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3833 #ifdef VOID_CLOSEDIR
3834 PerlDir_close(IoDIRP(io));
3836 if (PerlDir_close(IoDIRP(io)) < 0) {
3837 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3846 SETERRNO(EBADF,RMS_IFI);
3849 DIE(aTHX_ PL_no_dir_func, "closedir");
3853 /* Process control. */
3862 PERL_FLUSHALL_FOR_CHILD;
3863 childpid = PerlProc_fork();
3867 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
3869 SvREADONLY_off(GvSV(tmpgv));
3870 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3871 SvREADONLY_on(GvSV(tmpgv));
3873 #ifdef THREADS_HAVE_PIDS
3874 PL_ppid = (IV)getppid();
3876 #ifdef PERL_USES_PL_PIDSTATUS
3877 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3883 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3888 PERL_FLUSHALL_FOR_CHILD;
3889 childpid = PerlProc_fork();
3895 DIE(aTHX_ PL_no_func, "fork");
3902 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3907 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3908 childpid = wait4pid(-1, &argflags, 0);
3910 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3915 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3916 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3917 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
3919 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
3924 DIE(aTHX_ PL_no_func, "wait");
3930 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3932 const int optype = POPi;
3933 const Pid_t pid = TOPi;
3937 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3938 result = wait4pid(pid, &argflags, optype);
3940 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
3945 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3946 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3947 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
3949 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
3954 DIE(aTHX_ PL_no_func, "waitpid");
3960 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3966 while (++MARK <= SP) {
3967 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
3972 TAINT_PROPER("system");
3974 PERL_FLUSHALL_FOR_CHILD;
3975 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
3981 if (PerlProc_pipe(pp) >= 0)
3983 while ((childpid = PerlProc_fork()) == -1) {
3984 if (errno != EAGAIN) {
3989 PerlLIO_close(pp[0]);
3990 PerlLIO_close(pp[1]);
3997 Sigsave_t ihand,qhand; /* place to save signals during system() */
4001 PerlLIO_close(pp[1]);
4003 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4004 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4007 result = wait4pid(childpid, &status, 0);
4008 } while (result == -1 && errno == EINTR);
4010 (void)rsignal_restore(SIGINT, &ihand);
4011 (void)rsignal_restore(SIGQUIT, &qhand);
4013 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4014 do_execfree(); /* free any memory child malloced on fork */
4020 while (n < sizeof(int)) {
4021 n1 = PerlLIO_read(pp[0],
4022 (void*)(((char*)&errkid)+n),
4028 PerlLIO_close(pp[0]);
4029 if (n) { /* Error */
4030 if (n != sizeof(int))
4031 DIE(aTHX_ "panic: kid popen errno read");
4032 errno = errkid; /* Propagate errno from kid */
4033 STATUS_NATIVE_CHILD_SET(-1);
4036 XPUSHi(STATUS_CURRENT);
4040 PerlLIO_close(pp[0]);
4041 #if defined(HAS_FCNTL) && defined(F_SETFD)
4042 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4045 if (PL_op->op_flags & OPf_STACKED) {
4046 SV * const really = *++MARK;
4047 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4049 else if (SP - MARK != 1)
4050 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4052 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4056 #else /* ! FORK or VMS or OS/2 */
4059 if (PL_op->op_flags & OPf_STACKED) {
4060 SV * const really = *++MARK;
4061 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4062 value = (I32)do_aspawn(really, MARK, SP);
4064 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4067 else if (SP - MARK != 1) {
4068 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4069 value = (I32)do_aspawn(NULL, MARK, SP);
4071 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4075 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4077 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4079 STATUS_NATIVE_CHILD_SET(value);
4082 XPUSHi(result ? value : STATUS_CURRENT);
4083 #endif /* !FORK or VMS */
4089 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4094 while (++MARK <= SP) {
4095 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4100 TAINT_PROPER("exec");
4102 PERL_FLUSHALL_FOR_CHILD;
4103 if (PL_op->op_flags & OPf_STACKED) {
4104 SV * const really = *++MARK;
4105 value = (I32)do_aexec(really, MARK, SP);
4107 else if (SP - MARK != 1)
4109 value = (I32)vms_do_aexec(NULL, MARK, SP);
4113 (void ) do_aspawn(NULL, MARK, SP);
4117 value = (I32)do_aexec(NULL, MARK, SP);
4122 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4125 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4128 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4142 # ifdef THREADS_HAVE_PIDS
4143 if (PL_ppid != 1 && getppid() == 1)
4144 /* maybe the parent process has died. Refresh ppid cache */
4148 XPUSHi( getppid() );
4152 DIE(aTHX_ PL_no_func, "getppid");
4161 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4164 pgrp = (I32)BSD_GETPGRP(pid);
4166 if (pid != 0 && pid != PerlProc_getpid())
4167 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4173 DIE(aTHX_ PL_no_func, "getpgrp()");
4192 TAINT_PROPER("setpgrp");
4194 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4196 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4197 || (pid != 0 && pid != PerlProc_getpid()))
4199 DIE(aTHX_ "setpgrp can't take arguments");
4201 SETi( setpgrp() >= 0 );
4202 #endif /* USE_BSDPGRP */
4205 DIE(aTHX_ PL_no_func, "setpgrp()");
4211 #ifdef HAS_GETPRIORITY
4213 const int who = POPi;
4214 const int which = TOPi;
4215 SETi( getpriority(which, who) );
4218 DIE(aTHX_ PL_no_func, "getpriority()");
4224 #ifdef HAS_SETPRIORITY
4226 const int niceval = POPi;
4227 const int who = POPi;
4228 const int which = TOPi;
4229 TAINT_PROPER("setpriority");
4230 SETi( setpriority(which, who, niceval) >= 0 );
4233 DIE(aTHX_ PL_no_func, "setpriority()");
4243 XPUSHn( time(NULL) );
4245 XPUSHi( time(NULL) );
4257 (void)PerlProc_times(&PL_timesbuf);
4259 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4260 /* struct tms, though same data */
4264 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4265 if (GIMME == G_ARRAY) {
4266 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4267 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4268 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4274 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4276 if (GIMME == G_ARRAY) {
4277 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4278 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4279 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4283 DIE(aTHX_ "times not implemented");
4285 #endif /* HAS_TIMES */
4288 #ifdef LOCALTIME_EDGECASE_BROKEN
4289 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4294 /* No workarounds in the valid range */
4295 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4296 return (localtime (tp));
4298 /* This edge case is to workaround the undefined behaviour, where the
4299 * TIMEZONE makes the time go beyond the defined range.
4300 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4301 * If there is a negative offset in TZ, like MET-1METDST, some broken
4302 * implementations of localtime () (like AIX 5.2) barf with bogus
4304 * 0x7fffffff gmtime 2038-01-19 03:14:07
4305 * 0x7fffffff localtime 1901-12-13 21:45:51
4306 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4307 * 0x3c19137f gmtime 2001-12-13 20:45:51
4308 * 0x3c19137f localtime 2001-12-13 21:45:51
4309 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4310 * Given that legal timezones are typically between GMT-12 and GMT+12
4311 * we turn back the clock 23 hours before calling the localtime
4312 * function, and add those to the return value. This will never cause
4313 * day wrapping problems, since the edge case is Tue Jan *19*
4315 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4318 if (P->tm_hour >= 24) {
4320 P->tm_mday++; /* 18 -> 19 */
4321 P->tm_wday++; /* Mon -> Tue */
4322 P->tm_yday++; /* 18 -> 19 */
4325 } /* S_my_localtime */
4333 const struct tm *tmbuf;
4334 static const char * const dayname[] =
4335 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4336 static const char * const monname[] =
4337 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4338 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4344 when = (Time_t)SvNVx(POPs);
4346 when = (Time_t)SvIVx(POPs);
4349 if (PL_op->op_type == OP_LOCALTIME)
4350 #ifdef LOCALTIME_EDGECASE_BROKEN
4351 tmbuf = S_my_localtime(aTHX_ &when);
4353 tmbuf = localtime(&when);
4356 tmbuf = gmtime(&when);
4358 if (GIMME != G_ARRAY) {
4364 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4365 dayname[tmbuf->tm_wday],
4366 monname[tmbuf->tm_mon],
4371 tmbuf->tm_year + 1900);
4372 PUSHs(sv_2mortal(tsv));
4377 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4378 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4379 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4380 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4381 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4382 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4383 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4384 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4385 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4396 anum = alarm((unsigned int)anum);
4403 DIE(aTHX_ PL_no_func, "alarm");
4414 (void)time(&lasttime);
4419 PerlProc_sleep((unsigned int)duration);
4422 XPUSHi(when - lasttime);
4426 /* Shared memory. */
4427 /* Merged with some message passing. */
4431 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4432 dVAR; dSP; dMARK; dTARGET;
4433 const int op_type = PL_op->op_type;
4438 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4441 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4444 value = (I32)(do_semop(MARK, SP) >= 0);
4447 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4463 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4464 dVAR; dSP; dMARK; dTARGET;
4465 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4472 DIE(aTHX_ "System V IPC is not implemented on this machine");
4478 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4479 dVAR; dSP; dMARK; dTARGET;
4480 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4488 PUSHp(zero_but_true, ZBTLEN);
4496 /* Get system info. */
4500 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4502 I32 which = PL_op->op_type;
4503 register char **elem;
4505 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4506 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4507 struct hostent *gethostbyname(Netdb_name_t);
4508 struct hostent *gethostent(void);
4510 struct hostent *hent;
4514 if (which == OP_GHBYNAME) {
4515 #ifdef HAS_GETHOSTBYNAME
4516 const char* const name = POPpbytex;
4517 hent = PerlSock_gethostbyname(name);
4519 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4522 else if (which == OP_GHBYADDR) {
4523 #ifdef HAS_GETHOSTBYADDR
4524 const int addrtype = POPi;
4525 SV * const addrsv = POPs;
4527 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4529 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4531 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4535 #ifdef HAS_GETHOSTENT
4536 hent = PerlSock_gethostent();
4538 DIE(aTHX_ PL_no_sock_func, "gethostent");
4541 #ifdef HOST_NOT_FOUND
4543 #ifdef USE_REENTRANT_API
4544 # ifdef USE_GETHOSTENT_ERRNO
4545 h_errno = PL_reentrant_buffer->_gethostent_errno;
4548 STATUS_UNIX_SET(h_errno);
4552 if (GIMME != G_ARRAY) {
4553 PUSHs(sv = sv_newmortal());
4555 if (which == OP_GHBYNAME) {
4557 sv_setpvn(sv, hent->h_addr, hent->h_length);
4560 sv_setpv(sv, (char*)hent->h_name);
4566 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4567 sv_setpv(sv, (char*)hent->h_name);
4568 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4569 for (elem = hent->h_aliases; elem && *elem; elem++) {
4570 sv_catpv(sv, *elem);
4574 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4575 sv_setiv(sv, (IV)hent->h_addrtype);
4576 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4577 len = hent->h_length;
4578 sv_setiv(sv, (IV)len);
4580 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4581 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4582 sv_setpvn(sv, *elem, len);
4585 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4587 sv_setpvn(sv, hent->h_addr, len);
4592 DIE(aTHX_ PL_no_sock_func, "gethostent");
4598 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4600 I32 which = PL_op->op_type;
4601 register char **elem;
4603 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4604 struct netent *getnetbyaddr(Netdb_net_t, int);
4605 struct netent *getnetbyname(Netdb_name_t);
4606 struct netent *getnetent(void);
4608 struct netent *nent;
4610 if (which == OP_GNBYNAME){
4611 #ifdef HAS_GETNETBYNAME
4612 const char * const name = POPpbytex;
4613 nent = PerlSock_getnetbyname(name);
4615 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4618 else if (which == OP_GNBYADDR) {
4619 #ifdef HAS_GETNETBYADDR
4620 const int addrtype = POPi;
4621 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4622 nent = PerlSock_getnetbyaddr(addr, addrtype);
4624 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4628 #ifdef HAS_GETNETENT
4629 nent = PerlSock_getnetent();
4631 DIE(aTHX_ PL_no_sock_func, "getnetent");
4634 #ifdef HOST_NOT_FOUND
4636 #ifdef USE_REENTRANT_API
4637 # ifdef USE_GETNETENT_ERRNO
4638 h_errno = PL_reentrant_buffer->_getnetent_errno;
4641 STATUS_UNIX_SET(h_errno);
4646 if (GIMME != G_ARRAY) {
4647 PUSHs(sv = sv_newmortal());
4649 if (which == OP_GNBYNAME)
4650 sv_setiv(sv, (IV)nent->n_net);
4652 sv_setpv(sv, nent->n_name);
4658 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4659 sv_setpv(sv, nent->n_name);
4660 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4661 for (elem = nent->n_aliases; elem && *elem; elem++) {
4662 sv_catpv(sv, *elem);
4666 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4667 sv_setiv(sv, (IV)nent->n_addrtype);
4668 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4669 sv_setiv(sv, (IV)nent->n_net);
4674 DIE(aTHX_ PL_no_sock_func, "getnetent");
4680 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4682 I32 which = PL_op->op_type;
4683 register char **elem;
4685 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4686 struct protoent *getprotobyname(Netdb_name_t);
4687 struct protoent *getprotobynumber(int);
4688 struct protoent *getprotoent(void);
4690 struct protoent *pent;
4692 if (which == OP_GPBYNAME) {
4693 #ifdef HAS_GETPROTOBYNAME
4694 const char* const name = POPpbytex;
4695 pent = PerlSock_getprotobyname(name);
4697 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4700 else if (which == OP_GPBYNUMBER) {
4701 #ifdef HAS_GETPROTOBYNUMBER
4702 const int number = POPi;
4703 pent = PerlSock_getprotobynumber(number);
4705 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4709 #ifdef HAS_GETPROTOENT
4710 pent = PerlSock_getprotoent();
4712 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4716 if (GIMME != G_ARRAY) {
4717 PUSHs(sv = sv_newmortal());
4719 if (which == OP_GPBYNAME)
4720 sv_setiv(sv, (IV)pent->p_proto);
4722 sv_setpv(sv, pent->p_name);
4728 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4729 sv_setpv(sv, pent->p_name);
4730 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4731 for (elem = pent->p_aliases; elem && *elem; elem++) {
4732 sv_catpv(sv, *elem);
4736 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4737 sv_setiv(sv, (IV)pent->p_proto);
4742 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4748 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4750 I32 which = PL_op->op_type;
4751 register char **elem;
4753 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4754 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4755 struct servent *getservbyport(int, Netdb_name_t);
4756 struct servent *getservent(void);
4758 struct servent *sent;
4760 if (which == OP_GSBYNAME) {
4761 #ifdef HAS_GETSERVBYNAME
4762 const char * const proto = POPpbytex;
4763 const char * const name = POPpbytex;
4764 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4766 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4769 else if (which == OP_GSBYPORT) {
4770 #ifdef HAS_GETSERVBYPORT
4771 const char * const proto = POPpbytex;
4772 unsigned short port = (unsigned short)POPu;
4774 port = PerlSock_htons(port);
4776 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4778 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4782 #ifdef HAS_GETSERVENT
4783 sent = PerlSock_getservent();
4785 DIE(aTHX_ PL_no_sock_func, "getservent");
4789 if (GIMME != G_ARRAY) {
4790 PUSHs(sv = sv_newmortal());
4792 if (which == OP_GSBYNAME) {
4794 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4796 sv_setiv(sv, (IV)(sent->s_port));
4800 sv_setpv(sv, sent->s_name);
4806 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4807 sv_setpv(sv, sent->s_name);
4808 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4809 for (elem = sent->s_aliases; elem && *elem; elem++) {
4810 sv_catpv(sv, *elem);
4814 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4816 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4818 sv_setiv(sv, (IV)(sent->s_port));
4820 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4821 sv_setpv(sv, sent->s_proto);
4826 DIE(aTHX_ PL_no_sock_func, "getservent");
4832 #ifdef HAS_SETHOSTENT
4834 PerlSock_sethostent(TOPi);
4837 DIE(aTHX_ PL_no_sock_func, "sethostent");
4843 #ifdef HAS_SETNETENT
4845 PerlSock_setnetent(TOPi);
4848 DIE(aTHX_ PL_no_sock_func, "setnetent");
4854 #ifdef HAS_SETPROTOENT
4856 PerlSock_setprotoent(TOPi);
4859 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4865 #ifdef HAS_SETSERVENT
4867 PerlSock_setservent(TOPi);
4870 DIE(aTHX_ PL_no_sock_func, "setservent");
4876 #ifdef HAS_ENDHOSTENT
4878 PerlSock_endhostent();
4882 DIE(aTHX_ PL_no_sock_func, "endhostent");
4888 #ifdef HAS_ENDNETENT
4890 PerlSock_endnetent();
4894 DIE(aTHX_ PL_no_sock_func, "endnetent");
4900 #ifdef HAS_ENDPROTOENT
4902 PerlSock_endprotoent();
4906 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4912 #ifdef HAS_ENDSERVENT
4914 PerlSock_endservent();
4918 DIE(aTHX_ PL_no_sock_func, "endservent");
4926 I32 which = PL_op->op_type;
4928 struct passwd *pwent = NULL;
4930 * We currently support only the SysV getsp* shadow password interface.
4931 * The interface is declared in <shadow.h> and often one needs to link
4932 * with -lsecurity or some such.
4933 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
4936 * AIX getpwnam() is clever enough to return the encrypted password
4937 * only if the caller (euid?) is root.
4939 * There are at least three other shadow password APIs. Many platforms
4940 * seem to contain more than one interface for accessing the shadow
4941 * password databases, possibly for compatibility reasons.
4942 * The getsp*() is by far he simplest one, the other two interfaces
4943 * are much more complicated, but also very similar to each other.
4948 * struct pr_passwd *getprpw*();
4949 * The password is in
4950 * char getprpw*(...).ufld.fd_encrypt[]
4951 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
4956 * struct es_passwd *getespw*();
4957 * The password is in
4958 * char *(getespw*(...).ufld.fd_encrypt)
4959 * Mention HAS_GETESPWNAM here so that Configure probes for it.
4962 * struct userpw *getuserpw();
4963 * The password is in
4964 * char *(getuserpw(...)).spw_upw_passwd
4965 * (but the de facto standard getpwnam() should work okay)
4967 * Mention I_PROT here so that Configure probes for it.
4969 * In HP-UX for getprpw*() the manual page claims that one should include
4970 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
4971 * if one includes <shadow.h> as that includes <hpsecurity.h>,
4972 * and pp_sys.c already includes <shadow.h> if there is such.
4974 * Note that <sys/security.h> is already probed for, but currently
4975 * it is only included in special cases.
4977 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
4978 * be preferred interface, even though also the getprpw*() interface
4979 * is available) one needs to link with -lsecurity -ldb -laud -lm.
4980 * One also needs to call set_auth_parameters() in main() before
4981 * doing anything else, whether one is using getespw*() or getprpw*().
4983 * Note that accessing the shadow databases can be magnitudes
4984 * slower than accessing the standard databases.
4989 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
4990 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
4991 * the pw_comment is left uninitialized. */
4992 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
4998 const char* const name = POPpbytex;
4999 pwent = getpwnam(name);
5005 pwent = getpwuid(uid);
5009 # ifdef HAS_GETPWENT
5011 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5012 if (pwent) pwent = getpwnam(pwent->pw_name);
5015 DIE(aTHX_ PL_no_func, "getpwent");
5021 if (GIMME != G_ARRAY) {
5022 PUSHs(sv = sv_newmortal());
5024 if (which == OP_GPWNAM)
5025 # if Uid_t_sign <= 0
5026 sv_setiv(sv, (IV)pwent->pw_uid);
5028 sv_setuv(sv, (UV)pwent->pw_uid);
5031 sv_setpv(sv, pwent->pw_name);
5037 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5038 sv_setpv(sv, pwent->pw_name);
5040 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5042 /* If we have getspnam(), we try to dig up the shadow
5043 * password. If we are underprivileged, the shadow
5044 * interface will set the errno to EACCES or similar,
5045 * and return a null pointer. If this happens, we will
5046 * use the dummy password (usually "*" or "x") from the
5047 * standard password database.
5049 * In theory we could skip the shadow call completely
5050 * if euid != 0 but in practice we cannot know which
5051 * security measures are guarding the shadow databases
5052 * on a random platform.
5054 * Resist the urge to use additional shadow interfaces.
5055 * Divert the urge to writing an extension instead.
5058 /* Some AIX setups falsely(?) detect some getspnam(), which
5059 * has a different API than the Solaris/IRIX one. */
5060 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5062 const int saverrno = errno;
5063 const struct spwd * const spwent = getspnam(pwent->pw_name);
5064 /* Save and restore errno so that
5065 * underprivileged attempts seem
5066 * to have never made the unsccessful
5067 * attempt to retrieve the shadow password. */
5069 if (spwent && spwent->sp_pwdp)
5070 sv_setpv(sv, spwent->sp_pwdp);
5074 if (!SvPOK(sv)) /* Use the standard password, then. */
5075 sv_setpv(sv, pwent->pw_passwd);
5078 # ifndef INCOMPLETE_TAINTS
5079 /* passwd is tainted because user himself can diddle with it.
5080 * admittedly not much and in a very limited way, but nevertheless. */
5084 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5085 # if Uid_t_sign <= 0
5086 sv_setiv(sv, (IV)pwent->pw_uid);
5088 sv_setuv(sv, (UV)pwent->pw_uid);
5091 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5092 # if Uid_t_sign <= 0
5093 sv_setiv(sv, (IV)pwent->pw_gid);
5095 sv_setuv(sv, (UV)pwent->pw_gid);
5097 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5098 * because of the poor interface of the Perl getpw*(),
5099 * not because there's some standard/convention saying so.
5100 * A better interface would have been to return a hash,
5101 * but we are accursed by our history, alas. --jhi. */
5102 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5104 sv_setiv(sv, (IV)pwent->pw_change);
5107 sv_setiv(sv, (IV)pwent->pw_quota);
5110 sv_setpv(sv, pwent->pw_age);
5115 /* pw_class and pw_comment are mutually exclusive--.
5116 * see the above note for pw_change, pw_quota, and pw_age. */
5117 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5119 sv_setpv(sv, pwent->pw_class);
5122 sv_setpv(sv, pwent->pw_comment);
5126 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5128 sv_setpv(sv, pwent->pw_gecos);
5130 # ifndef INCOMPLETE_TAINTS
5131 /* pw_gecos is tainted because user himself can diddle with it. */
5135 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5136 sv_setpv(sv, pwent->pw_dir);
5138 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5139 sv_setpv(sv, pwent->pw_shell);
5140 # ifndef INCOMPLETE_TAINTS
5141 /* pw_shell is tainted because user himself can diddle with it. */
5146 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5147 sv_setiv(sv, (IV)pwent->pw_expire);
5152 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5158 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5163 DIE(aTHX_ PL_no_func, "setpwent");
5169 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5174 DIE(aTHX_ PL_no_func, "endpwent");
5182 const I32 which = PL_op->op_type;
5183 const struct group *grent;
5185 if (which == OP_GGRNAM) {
5186 const char* const name = POPpbytex;
5187 grent = (const struct group *)getgrnam(name);
5189 else if (which == OP_GGRGID) {
5190 const Gid_t gid = POPi;
5191 grent = (const struct group *)getgrgid(gid);
5195 grent = (struct group *)getgrent();
5197 DIE(aTHX_ PL_no_func, "getgrent");
5201 if (GIMME != G_ARRAY) {
5202 SV * const sv = sv_newmortal();
5206 if (which == OP_GGRNAM)
5207 sv_setiv(sv, (IV)grent->gr_gid);
5209 sv_setpv(sv, grent->gr_name);
5217 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5218 sv_setpv(sv, grent->gr_name);
5220 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5222 sv_setpv(sv, grent->gr_passwd);
5225 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5226 sv_setiv(sv, (IV)grent->gr_gid);
5228 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5229 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5230 /* In UNICOS/mk (_CRAYMPP) the multithreading
5231 * versions (getgrnam_r, getgrgid_r)
5232 * seem to return an illegal pointer
5233 * as the group members list, gr_mem.
5234 * getgrent() doesn't even have a _r version
5235 * but the gr_mem is poisonous anyway.
5236 * So yes, you cannot get the list of group
5237 * members if building multithreaded in UNICOS/mk. */
5238 for (elem = grent->gr_mem; elem && *elem; elem++) {
5239 sv_catpv(sv, *elem);
5248 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5254 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5259 DIE(aTHX_ PL_no_func, "setgrent");
5265 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5270 DIE(aTHX_ PL_no_func, "endgrent");
5280 if (!(tmps = PerlProc_getlogin()))
5282 PUSHp(tmps, strlen(tmps));
5285 DIE(aTHX_ PL_no_func, "getlogin");
5289 /* Miscellaneous. */
5294 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5295 register I32 items = SP - MARK;
5296 unsigned long a[20];
5301 while (++MARK <= SP) {
5302 if (SvTAINTED(*MARK)) {
5308 TAINT_PROPER("syscall");
5311 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5312 * or where sizeof(long) != sizeof(char*). But such machines will
5313 * not likely have syscall implemented either, so who cares?
5315 while (++MARK <= SP) {
5316 if (SvNIOK(*MARK) || !i)
5317 a[i++] = SvIV(*MARK);
5318 else if (*MARK == &PL_sv_undef)
5321 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5327 DIE(aTHX_ "Too many args to syscall");
5329 DIE(aTHX_ "Too few args to syscall");
5331 retval = syscall(a[0]);
5334 retval = syscall(a[0],a[1]);
5337 retval = syscall(a[0],a[1],a[2]);
5340 retval = syscall(a[0],a[1],a[2],a[3]);
5343 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5346 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5349 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5352 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5356 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5359 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5362 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5366 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5370 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5374 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5375 a[10],a[11],a[12],a[13]);
5377 #endif /* atarist */
5383 DIE(aTHX_ PL_no_func, "syscall");
5387 #ifdef FCNTL_EMULATE_FLOCK
5389 /* XXX Emulate flock() with fcntl().
5390 What's really needed is a good file locking module.
5394 fcntl_emulate_flock(int fd, int operation)
5398 switch (operation & ~LOCK_NB) {
5400 flock.l_type = F_RDLCK;
5403 flock.l_type = F_WRLCK;
5406 flock.l_type = F_UNLCK;
5412 flock.l_whence = SEEK_SET;
5413 flock.l_start = flock.l_len = (Off_t)0;
5415 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5418 #endif /* FCNTL_EMULATE_FLOCK */
5420 #ifdef LOCKF_EMULATE_FLOCK
5422 /* XXX Emulate flock() with lockf(). This is just to increase
5423 portability of scripts. The calls are not completely
5424 interchangeable. What's really needed is a good file
5428 /* The lockf() constants might have been defined in <unistd.h>.
5429 Unfortunately, <unistd.h> causes troubles on some mixed
5430 (BSD/POSIX) systems, such as SunOS 4.1.3.
5432 Further, the lockf() constants aren't POSIX, so they might not be
5433 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5434 just stick in the SVID values and be done with it. Sigh.
5438 # define F_ULOCK 0 /* Unlock a previously locked region */
5441 # define F_LOCK 1 /* Lock a region for exclusive use */
5444 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5447 # define F_TEST 3 /* Test a region for other processes locks */
5451 lockf_emulate_flock(int fd, int operation)
5454 const int save_errno = errno;
5457 /* flock locks entire file so for lockf we need to do the same */
5458 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5459 if (pos > 0) /* is seekable and needs to be repositioned */
5460 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5461 pos = -1; /* seek failed, so don't seek back afterwards */
5464 switch (operation) {
5466 /* LOCK_SH - get a shared lock */
5468 /* LOCK_EX - get an exclusive lock */
5470 i = lockf (fd, F_LOCK, 0);
5473 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5474 case LOCK_SH|LOCK_NB:
5475 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5476 case LOCK_EX|LOCK_NB:
5477 i = lockf (fd, F_TLOCK, 0);
5479 if ((errno == EAGAIN) || (errno == EACCES))
5480 errno = EWOULDBLOCK;
5483 /* LOCK_UN - unlock (non-blocking is a no-op) */
5485 case LOCK_UN|LOCK_NB:
5486 i = lockf (fd, F_ULOCK, 0);
5489 /* Default - can't decipher operation */
5496 if (pos > 0) /* need to restore position of the handle */
5497 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5502 #endif /* LOCKF_EMULATE_FLOCK */
5506 * c-indentation-style: bsd
5508 * indent-tabs-mode: t
5511 * ex: set ts=8 sts=4 sw=4 noet: