3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 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)
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)) != Nullch)
355 SV * const sv = NEWSV(56, 79);
356 if (sv_gets(sv, fp, 0) == Nullch) {
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(newSVpvn("\000", 1));
409 *SvPVX(PL_rs) = '\n';
413 result = do_readline();
420 PL_last_in_gv = cGVOP_gv;
421 return do_readline();
432 do_join(TARG, &PL_sv_no, MARK, SP);
436 else if (SP == MARK) {
443 tmps = SvPV_const(tmpsv, len);
444 if ((!tmps || !len) && PL_errgv) {
445 SV * const error = ERRSV;
446 SvUPGRADE(error, SVt_PV);
447 if (SvPOK(error) && SvCUR(error))
448 sv_catpv(error, "\t...caught");
450 tmps = SvPV_const(tmpsv, len);
453 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
455 Perl_warn(aTHX_ "%"SVf, tmpsv);
467 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
469 if (SP - MARK != 1) {
471 do_join(TARG, &PL_sv_no, MARK, SP);
473 tmps = SvPV_const(tmpsv, len);
479 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
482 SV * const error = ERRSV;
483 SvUPGRADE(error, SVt_PV);
484 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
486 SvSetSV(error,tmpsv);
487 else if (sv_isobject(error)) {
488 HV * const stash = SvSTASH(SvRV(error));
489 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
491 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
492 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
499 call_sv((SV*)GvCV(gv),
500 G_SCALAR|G_EVAL|G_KEEPERR);
501 sv_setsv(error,*PL_stack_sp--);
507 if (SvPOK(error) && SvCUR(error))
508 sv_catpv(error, "\t...propagated");
511 tmps = SvPV_const(tmpsv, len);
517 tmpsv = sv_2mortal(newSVpvn("Died", 4));
519 DIE(aTHX_ "%"SVf, tmpsv);
535 GV * const gv = (GV *)*++MARK;
538 DIE(aTHX_ PL_no_usym, "filehandle");
539 if ((io = GvIOp(gv)))
540 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
543 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
545 /* Method's args are same as ours ... */
546 /* ... except handle is replaced by the object */
547 *MARK-- = SvTIED_obj((SV*)io, mg);
551 call_method("OPEN", G_SCALAR);
565 tmps = SvPV_const(sv, len);
566 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
569 PUSHi( (I32)PL_forkprocess );
570 else if (PL_forkprocess == 0) /* we are a new child */
582 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
584 if (gv && (io = GvIO(gv))
585 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
588 XPUSHs(SvTIED_obj((SV*)io, mg));
591 call_method("CLOSE", G_SCALAR);
597 PUSHs(boolSV(do_close(gv, TRUE)));
609 GV * const wgv = (GV*)POPs;
610 GV * const rgv = (GV*)POPs;
615 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
616 DIE(aTHX_ PL_no_usym, "filehandle");
621 do_close(rgv, FALSE);
623 do_close(wgv, FALSE);
625 if (PerlProc_pipe(fd) < 0)
628 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
629 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
630 IoOFP(rstio) = IoIFP(rstio);
631 IoIFP(wstio) = IoOFP(wstio);
632 IoTYPE(rstio) = IoTYPE_RDONLY;
633 IoTYPE(wstio) = IoTYPE_WRONLY;
635 if (!IoIFP(rstio) || !IoOFP(wstio)) {
636 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
637 else PerlLIO_close(fd[0]);
638 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
639 else PerlLIO_close(fd[1]);
642 #if defined(HAS_FCNTL) && defined(F_SETFD)
643 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
644 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
651 DIE(aTHX_ PL_no_func, "pipe");
667 if (gv && (io = GvIO(gv))
668 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
671 XPUSHs(SvTIED_obj((SV*)io, mg));
674 call_method("FILENO", G_SCALAR);
680 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
681 /* Can't do this because people seem to do things like
682 defined(fileno($foo)) to check whether $foo is a valid fh.
683 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
684 report_evil_fh(gv, io, PL_op->op_type);
689 PUSHi(PerlIO_fileno(fp));
701 anum = PerlLIO_umask(0);
702 (void)PerlLIO_umask(anum);
705 anum = PerlLIO_umask(POPi);
706 TAINT_PROPER("umask");
709 /* Only DIE if trying to restrict permissions on "user" (self).
710 * Otherwise it's harmless and more useful to just return undef
711 * since 'group' and 'other' concepts probably don't exist here. */
712 if (MAXARG >= 1 && (POPi & 0700))
713 DIE(aTHX_ "umask not implemented");
714 XPUSHs(&PL_sv_undef);
736 if (gv && (io = GvIO(gv))
737 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
740 XPUSHs(SvTIED_obj((SV*)io, mg));
745 call_method("BINMODE", G_SCALAR);
752 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
753 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
754 report_evil_fh(gv, io, PL_op->op_type);
755 SETERRNO(EBADF,RMS_IFI);
760 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
761 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
762 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
763 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
764 mode_from_discipline(discp),
765 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
785 const I32 markoff = MARK - PL_stack_base;
786 const char *methname;
787 int how = PERL_MAGIC_tied;
791 switch(SvTYPE(varsv)) {
793 methname = "TIEHASH";
794 HvEITER_set((HV *)varsv, 0);
797 methname = "TIEARRAY";
800 #ifdef GV_UNIQUE_CHECK
801 if (GvUNIQUE((GV*)varsv)) {
802 Perl_croak(aTHX_ "Attempt to tie unique GV");
805 methname = "TIEHANDLE";
806 how = PERL_MAGIC_tiedscalar;
807 /* For tied filehandles, we apply tiedscalar magic to the IO
808 slot of the GP rather than the GV itself. AMS 20010812 */
810 GvIOp(varsv) = newIO();
811 varsv = (SV *)GvIOp(varsv);
814 methname = "TIESCALAR";
815 how = PERL_MAGIC_tiedscalar;
819 if (sv_isobject(*MARK)) {
821 PUSHSTACKi(PERLSI_MAGIC);
823 EXTEND(SP,(I32)items);
827 call_method(methname, G_SCALAR);
830 /* Not clear why we don't call call_method here too.
831 * perhaps to get different error message ?
833 stash = gv_stashsv(*MARK, FALSE);
834 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
835 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
839 PUSHSTACKi(PERLSI_MAGIC);
841 EXTEND(SP,(I32)items);
845 call_sv((SV*)GvCV(gv), G_SCALAR);
851 if (sv_isobject(sv)) {
852 sv_unmagic(varsv, how);
853 /* Croak if a self-tie on an aggregate is attempted. */
854 if (varsv == SvRV(sv) &&
855 (SvTYPE(varsv) == SVt_PVAV ||
856 SvTYPE(varsv) == SVt_PVHV))
858 "Self-ties of arrays and hashes are not supported");
859 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
862 SP = PL_stack_base + markoff;
872 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
873 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
875 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
878 if ((mg = SvTIED_mg(sv, how))) {
879 SV * const obj = SvRV(SvTIED_obj(sv, mg));
881 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
883 if (gv && isGV(gv) && (cv = GvCV(gv))) {
885 XPUSHs(SvTIED_obj((SV*)gv, mg));
886 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
889 call_sv((SV *)cv, G_VOID);
893 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
894 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
895 "untie attempted while %"UVuf" inner references still exist",
896 (UV)SvREFCNT(obj) - 1 ) ;
900 sv_unmagic(sv, how) ;
909 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
910 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
912 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
915 if ((mg = SvTIED_mg(sv, how))) {
916 SV *osv = SvTIED_obj(sv, mg);
917 if (osv == mg->mg_obj)
918 osv = sv_mortalcopy(osv);
932 HV * const hv = (HV*)POPs;
933 SV * const sv = sv_mortalcopy(&PL_sv_no);
935 sv_setpv(sv, "AnyDBM_File");
936 stash = gv_stashsv(sv, FALSE);
937 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
939 require_pv("AnyDBM_File.pm");
941 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
942 DIE(aTHX_ "No dbm on this machine");
952 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
954 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
957 call_sv((SV*)GvCV(gv), G_SCALAR);
960 if (!sv_isobject(TOPs)) {
965 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
968 call_sv((SV*)GvCV(gv), G_SCALAR);
972 if (sv_isobject(TOPs)) {
973 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
974 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
991 struct timeval timebuf;
992 struct timeval *tbuf = &timebuf;
995 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1000 # if BYTEORDER & 0xf0000
1001 # define ORDERBYTE (0x88888888 - BYTEORDER)
1003 # define ORDERBYTE (0x4444 - BYTEORDER)
1009 for (i = 1; i <= 3; i++) {
1010 SV * const sv = SP[i];
1013 if (SvREADONLY(sv)) {
1015 sv_force_normal_flags(sv, 0);
1016 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1017 DIE(aTHX_ PL_no_modify);
1020 if (ckWARN(WARN_MISC))
1021 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1022 SvPV_force_nolen(sv); /* force string conversion */
1029 /* little endians can use vecs directly */
1030 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1037 masksize = NFDBITS / NBBY;
1039 masksize = sizeof(long); /* documented int, everyone seems to use long */
1041 Zero(&fd_sets[0], 4, char*);
1044 # if SELECT_MIN_BITS == 1
1045 growsize = sizeof(fd_set);
1047 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1048 # undef SELECT_MIN_BITS
1049 # define SELECT_MIN_BITS __FD_SETSIZE
1051 /* If SELECT_MIN_BITS is greater than one we most probably will want
1052 * to align the sizes with SELECT_MIN_BITS/8 because for example
1053 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1054 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1055 * on (sets/tests/clears bits) is 32 bits. */
1056 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1064 timebuf.tv_sec = (long)value;
1065 value -= (NV)timebuf.tv_sec;
1066 timebuf.tv_usec = (long)(value * 1000000.0);
1069 tbuf = Null(struct timeval*);
1071 for (i = 1; i <= 3; i++) {
1073 if (!SvOK(sv) || SvCUR(sv) == 0) {
1080 Sv_Grow(sv, growsize);
1084 while (++j <= growsize) {
1088 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1090 Newx(fd_sets[i], growsize, char);
1091 for (offset = 0; offset < growsize; offset += masksize) {
1092 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1093 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1096 fd_sets[i] = SvPVX(sv);
1100 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1101 /* Can't make just the (void*) conditional because that would be
1102 * cpp #if within cpp macro, and not all compilers like that. */
1103 nfound = PerlSock_select(
1105 (Select_fd_set_t) fd_sets[1],
1106 (Select_fd_set_t) fd_sets[2],
1107 (Select_fd_set_t) fd_sets[3],
1108 (void*) tbuf); /* Workaround for compiler bug. */
1110 nfound = PerlSock_select(
1112 (Select_fd_set_t) fd_sets[1],
1113 (Select_fd_set_t) fd_sets[2],
1114 (Select_fd_set_t) fd_sets[3],
1117 for (i = 1; i <= 3; i++) {
1120 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1122 for (offset = 0; offset < growsize; offset += masksize) {
1123 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1124 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1126 Safefree(fd_sets[i]);
1133 if (GIMME == G_ARRAY && tbuf) {
1134 value = (NV)(timebuf.tv_sec) +
1135 (NV)(timebuf.tv_usec) / 1000000.0;
1136 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1137 sv_setnv(sv, value);
1141 DIE(aTHX_ "select not implemented");
1146 Perl_setdefout(pTHX_ GV *gv)
1149 (void)SvREFCNT_inc(gv);
1151 SvREFCNT_dec(PL_defoutgv);
1159 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1160 GV * egv = GvEGV(PL_defoutgv);
1166 XPUSHs(&PL_sv_undef);
1168 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1169 if (gvp && *gvp == egv) {
1170 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1174 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1179 if (!GvIO(newdefout))
1180 gv_IOadd(newdefout);
1181 setdefout(newdefout);
1192 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1194 if (gv && (io = GvIO(gv))
1195 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1197 const I32 gimme = GIMME_V;
1199 XPUSHs(SvTIED_obj((SV*)io, mg));
1202 call_method("GETC", gimme);
1205 if (gimme == G_SCALAR)
1206 SvSetMagicSV_nosteal(TARG, TOPs);
1209 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1210 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1211 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1212 report_evil_fh(gv, io, PL_op->op_type);
1213 SETERRNO(EBADF,RMS_IFI);
1217 sv_setpvn(TARG, " ", 1);
1218 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1219 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1220 /* Find out how many bytes the char needs */
1221 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1224 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1225 SvCUR_set(TARG,1+len);
1234 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1237 register PERL_CONTEXT *cx;
1238 const I32 gimme = GIMME_V;
1243 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1245 cx->blk_sub.retop = retop;
1247 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1249 setdefout(gv); /* locally select filehandle so $% et al work */
1281 SV * const tmpsv = sv_newmortal();
1283 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1284 name = SvPV_nolen_const(tmpsv);
1286 DIE(aTHX_ "Undefined format \"%s\" called", name);
1288 DIE(aTHX_ "Not a format reference");
1291 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1293 IoFLAGS(io) &= ~IOf_DIDTOP;
1294 return doform(cv,gv,PL_op->op_next);
1300 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1301 register IO * const io = GvIOp(gv);
1302 PerlIO * const ofp = IoOFP(io);
1306 register PERL_CONTEXT *cx;
1308 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1309 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1312 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1313 PL_formtarget != PL_toptarget)
1317 if (!IoTOP_GV(io)) {
1320 if (!IoTOP_NAME(io)) {
1322 if (!IoFMT_NAME(io))
1323 IoFMT_NAME(io) = savepv(GvNAME(gv));
1324 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1325 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1326 if ((topgv && GvFORM(topgv)) ||
1327 !gv_fetchpv("top", 0, SVt_PVFM))
1328 IoTOP_NAME(io) = savesvpv(topname);
1330 IoTOP_NAME(io) = savepvn("top", 3);
1332 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1333 if (!topgv || !GvFORM(topgv)) {
1334 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1337 IoTOP_GV(io) = topgv;
1339 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1340 I32 lines = IoLINES_LEFT(io);
1341 const char *s = SvPVX_const(PL_formtarget);
1342 if (lines <= 0) /* Yow, header didn't even fit!!! */
1344 while (lines-- > 0) {
1345 s = strchr(s, '\n');
1351 const STRLEN save = SvCUR(PL_formtarget);
1352 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1353 do_print(PL_formtarget, ofp);
1354 SvCUR_set(PL_formtarget, save);
1355 sv_chop(PL_formtarget, s);
1356 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1359 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1360 do_print(PL_formfeed, ofp);
1361 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1363 PL_formtarget = PL_toptarget;
1364 IoFLAGS(io) |= IOf_DIDTOP;
1367 DIE(aTHX_ "bad top format reference");
1370 SV * const sv = sv_newmortal();
1372 gv_efullname4(sv, fgv, Nullch, FALSE);
1373 name = SvPV_nolen_const(sv);
1375 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1379 DIE(aTHX_ "Undefined top format called");
1382 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1383 return doform(cv,gv,PL_op);
1387 POPBLOCK(cx,PL_curpm);
1393 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1395 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1396 else if (ckWARN(WARN_CLOSED))
1397 report_evil_fh(gv, io, PL_op->op_type);
1402 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1403 if (ckWARN(WARN_IO))
1404 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1406 if (!do_print(PL_formtarget, fp))
1409 FmLINES(PL_formtarget) = 0;
1410 SvCUR_set(PL_formtarget, 0);
1411 *SvEND(PL_formtarget) = '\0';
1412 if (IoFLAGS(io) & IOf_FLUSH)
1413 (void)PerlIO_flush(fp);
1418 PL_formtarget = PL_bodytarget;
1420 PERL_UNUSED_VAR(newsp);
1421 PERL_UNUSED_VAR(gimme);
1422 return cx->blk_sub.retop;
1427 dVAR; dSP; dMARK; dORIGMARK;
1433 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1435 if (gv && (io = GvIO(gv))
1436 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1438 if (MARK == ORIGMARK) {
1441 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1445 *MARK = SvTIED_obj((SV*)io, mg);
1448 call_method("PRINTF", G_SCALAR);
1451 MARK = ORIGMARK + 1;
1458 if (!(io = GvIO(gv))) {
1459 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1460 report_evil_fh(gv, io, PL_op->op_type);
1461 SETERRNO(EBADF,RMS_IFI);
1464 else if (!(fp = IoOFP(io))) {
1465 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1467 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1468 else if (ckWARN(WARN_CLOSED))
1469 report_evil_fh(gv, io, PL_op->op_type);
1471 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1475 do_sprintf(sv, SP - MARK, MARK + 1);
1476 if (!do_print(sv, fp))
1479 if (IoFLAGS(io) & IOf_FLUSH)
1480 if (PerlIO_flush(fp) == EOF)
1491 PUSHs(&PL_sv_undef);
1498 const int perm = (MAXARG > 3) ? POPi : 0666;
1499 const int mode = POPi;
1500 SV * const sv = POPs;
1501 GV * const gv = (GV *)POPs;
1504 /* Need TIEHANDLE method ? */
1505 const char * const tmps = SvPV_const(sv, len);
1506 /* FIXME? do_open should do const */
1507 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1508 IoLINES(GvIOp(gv)) = 0;
1512 PUSHs(&PL_sv_undef);
1519 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1525 Sock_size_t bufsize;
1533 bool charstart = FALSE;
1534 STRLEN charskip = 0;
1537 GV * const gv = (GV*)*++MARK;
1538 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1539 && gv && (io = GvIO(gv)) )
1541 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1545 *MARK = SvTIED_obj((SV*)io, mg);
1547 call_method("READ", G_SCALAR);
1561 sv_setpvn(bufsv, "", 0);
1562 length = SvIVx(*++MARK);
1565 offset = SvIVx(*++MARK);
1569 if (!io || !IoIFP(io)) {
1570 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1571 report_evil_fh(gv, io, PL_op->op_type);
1572 SETERRNO(EBADF,RMS_IFI);
1575 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1576 buffer = SvPVutf8_force(bufsv, blen);
1577 /* UTF-8 may not have been set if they are all low bytes */
1582 buffer = SvPV_force(bufsv, blen);
1583 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1586 DIE(aTHX_ "Negative length");
1594 if (PL_op->op_type == OP_RECV) {
1595 char namebuf[MAXPATHLEN];
1596 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1597 bufsize = sizeof (struct sockaddr_in);
1599 bufsize = sizeof namebuf;
1601 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1605 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1606 /* 'offset' means 'flags' here */
1607 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1608 (struct sockaddr *)namebuf, &bufsize);
1612 /* Bogus return without padding */
1613 bufsize = sizeof (struct sockaddr_in);
1615 SvCUR_set(bufsv, count);
1616 *SvEND(bufsv) = '\0';
1617 (void)SvPOK_only(bufsv);
1621 /* This should not be marked tainted if the fp is marked clean */
1622 if (!(IoFLAGS(io) & IOf_UNTAINT))
1623 SvTAINTED_on(bufsv);
1625 sv_setpvn(TARG, namebuf, bufsize);
1630 if (PL_op->op_type == OP_RECV)
1631 DIE(aTHX_ PL_no_sock_func, "recv");
1633 if (DO_UTF8(bufsv)) {
1634 /* offset adjust in characters not bytes */
1635 blen = sv_len_utf8(bufsv);
1638 if (-offset > (int)blen)
1639 DIE(aTHX_ "Offset outside string");
1642 if (DO_UTF8(bufsv)) {
1643 /* convert offset-as-chars to offset-as-bytes */
1644 if (offset >= (int)blen)
1645 offset += SvCUR(bufsv) - blen;
1647 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1650 bufsize = SvCUR(bufsv);
1651 /* Allocating length + offset + 1 isn't perfect in the case of reading
1652 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1654 (should be 2 * length + offset + 1, or possibly something longer if
1655 PL_encoding is true) */
1656 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1657 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1658 Zero(buffer+bufsize, offset-bufsize, char);
1660 buffer = buffer + offset;
1662 read_target = bufsv;
1664 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1665 concatenate it to the current buffer. */
1667 /* Truncate the existing buffer to the start of where we will be
1669 SvCUR_set(bufsv, offset);
1671 read_target = sv_newmortal();
1672 SvUPGRADE(read_target, SVt_PV);
1673 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1676 if (PL_op->op_type == OP_SYSREAD) {
1677 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1678 if (IoTYPE(io) == IoTYPE_SOCKET) {
1679 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1685 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1690 #ifdef HAS_SOCKET__bad_code_maybe
1691 if (IoTYPE(io) == IoTYPE_SOCKET) {
1692 char namebuf[MAXPATHLEN];
1693 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1694 bufsize = sizeof (struct sockaddr_in);
1696 bufsize = sizeof namebuf;
1698 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1699 (struct sockaddr *)namebuf, &bufsize);
1704 count = PerlIO_read(IoIFP(io), buffer, length);
1705 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1706 if (count == 0 && PerlIO_error(IoIFP(io)))
1710 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1711 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1714 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1715 *SvEND(read_target) = '\0';
1716 (void)SvPOK_only(read_target);
1717 if (fp_utf8 && !IN_BYTES) {
1718 /* Look at utf8 we got back and count the characters */
1719 const char *bend = buffer + count;
1720 while (buffer < bend) {
1722 skip = UTF8SKIP(buffer);
1725 if (buffer - charskip + skip > bend) {
1726 /* partial character - try for rest of it */
1727 length = skip - (bend-buffer);
1728 offset = bend - SvPVX_const(bufsv);
1740 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1741 provided amount read (count) was what was requested (length)
1743 if (got < wanted && count == length) {
1744 length = wanted - got;
1745 offset = bend - SvPVX_const(bufsv);
1748 /* return value is character count */
1752 else if (buffer_utf8) {
1753 /* Let svcatsv upgrade the bytes we read in to utf8.
1754 The buffer is a mortal so will be freed soon. */
1755 sv_catsv_nomg(bufsv, read_target);
1758 /* This should not be marked tainted if the fp is marked clean */
1759 if (!(IoFLAGS(io) & IOf_UNTAINT))
1760 SvTAINTED_on(bufsv);
1772 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1780 const int op_type = PL_op->op_type;
1782 GV *const gv = (GV*)*++MARK;
1783 if (PL_op->op_type == OP_SYSWRITE
1784 && gv && (io = GvIO(gv))
1785 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1789 if (MARK == SP - 1) {
1791 sv = sv_2mortal(newSViv(sv_len(*SP)));
1797 *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
1799 call_method("WRITE", G_SCALAR);
1812 if (op_type == OP_SYSWRITE) {
1814 length = (Size_t) sv_len(bufsv);
1816 #if Size_t_size > IVSIZE
1817 length = (Size_t)SvNVx(*++MARK);
1819 length = (Size_t)SvIVx(*++MARK);
1821 if ((SSize_t)length < 0)
1822 DIE(aTHX_ "Negative length");
1827 if (!io || !IoIFP(io)) {
1829 if (ckWARN(WARN_CLOSED))
1830 report_evil_fh(gv, io, PL_op->op_type);
1831 SETERRNO(EBADF,RMS_IFI);
1835 if (PerlIO_isutf8(IoIFP(io))) {
1836 if (!SvUTF8(bufsv)) {
1837 bufsv = sv_2mortal(newSVsv(bufsv));
1838 buffer = sv_2pvutf8(bufsv, &blen);
1840 buffer = SvPV_const(bufsv, blen);
1843 if (DO_UTF8(bufsv)) {
1844 /* Not modifying source SV, so making a temporary copy. */
1845 bufsv = sv_2mortal(newSVsv(bufsv));
1846 sv_utf8_downgrade(bufsv, FALSE);
1848 buffer = SvPV_const(bufsv, blen);
1851 if (op_type == OP_SYSWRITE) {
1853 if (DO_UTF8(bufsv)) {
1854 /* length and offset are in chars */
1855 blen = sv_len_utf8(bufsv);
1858 offset = SvIVx(*++MARK);
1860 if (-offset > (IV)blen)
1861 DIE(aTHX_ "Offset outside string");
1863 } else if (offset >= (IV)blen && blen > 0)
1864 DIE(aTHX_ "Offset outside string");
1867 if (length > blen - offset)
1868 length = blen - offset;
1869 if (DO_UTF8(bufsv)) {
1870 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1871 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1874 buffer = buffer+offset;
1876 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1877 if (IoTYPE(io) == IoTYPE_SOCKET) {
1878 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1884 /* See the note at doio.c:do_print about filesize limits. --jhi */
1885 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1891 const int flags = SvIVx(*++MARK);
1894 char * const sockbuf = SvPVx(*++MARK, mlen);
1895 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1896 flags, (struct sockaddr *)sockbuf, mlen);
1900 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1905 DIE(aTHX_ PL_no_sock_func, "send");
1911 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1912 #if Size_t_size > IVSIZE
1932 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1934 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1936 if (io && !IoIFP(io)) {
1937 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1939 IoFLAGS(io) &= ~IOf_START;
1940 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1941 sv_setpvn(GvSV(gv), "-", 1);
1942 SvSETMAGIC(GvSV(gv));
1944 else if (!nextargv(gv))
1949 gv = PL_last_in_gv; /* eof */
1952 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1954 if (gv && (io = GvIO(gv))
1955 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1958 XPUSHs(SvTIED_obj((SV*)io, mg));
1961 call_method("EOF", G_SCALAR);
1967 PUSHs(boolSV(!gv || do_eof(gv)));
1979 PL_last_in_gv = (GV*)POPs;
1982 if (gv && (io = GvIO(gv))
1983 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1986 XPUSHs(SvTIED_obj((SV*)io, mg));
1989 call_method("TELL", G_SCALAR);
1995 #if LSEEKSIZE > IVSIZE
1996 PUSHn( do_tell(gv) );
1998 PUSHi( do_tell(gv) );
2007 const int whence = POPi;
2008 #if LSEEKSIZE > IVSIZE
2009 const Off_t offset = (Off_t)SvNVx(POPs);
2011 const Off_t offset = (Off_t)SvIVx(POPs);
2015 GV * const gv = PL_last_in_gv = (GV*)POPs;
2017 if (gv && (io = GvIO(gv))
2018 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2021 XPUSHs(SvTIED_obj((SV*)io, mg));
2022 #if LSEEKSIZE > IVSIZE
2023 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2025 XPUSHs(sv_2mortal(newSViv(offset)));
2027 XPUSHs(sv_2mortal(newSViv(whence)));
2030 call_method("SEEK", G_SCALAR);
2036 if (PL_op->op_type == OP_SEEK)
2037 PUSHs(boolSV(do_seek(gv, offset, whence)));
2039 const Off_t sought = do_sysseek(gv, offset, whence);
2041 PUSHs(&PL_sv_undef);
2043 SV* const sv = sought ?
2044 #if LSEEKSIZE > IVSIZE
2049 : newSVpvn(zero_but_true, ZBTLEN);
2050 PUSHs(sv_2mortal(sv));
2059 /* There seems to be no consensus on the length type of truncate()
2060 * and ftruncate(), both off_t and size_t have supporters. In
2061 * general one would think that when using large files, off_t is
2062 * at least as wide as size_t, so using an off_t should be okay. */
2063 /* XXX Configure probe for the length type of *truncate() needed XXX */
2066 #if Off_t_size > IVSIZE
2071 /* Checking for length < 0 is problematic as the type might or
2072 * might not be signed: if it is not, clever compilers will moan. */
2073 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2080 if (PL_op->op_flags & OPf_SPECIAL) {
2081 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2090 TAINT_PROPER("truncate");
2091 if (!(fp = IoIFP(io))) {
2097 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2099 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2106 SV * const sv = POPs;
2109 if (SvTYPE(sv) == SVt_PVGV) {
2110 tmpgv = (GV*)sv; /* *main::FRED for example */
2111 goto do_ftruncate_gv;
2113 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2114 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2115 goto do_ftruncate_gv;
2117 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2118 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2119 goto do_ftruncate_io;
2122 name = SvPV_nolen_const(sv);
2123 TAINT_PROPER("truncate");
2125 if (truncate(name, len) < 0)
2129 const int tmpfd = PerlLIO_open(name, O_RDWR);
2134 if (my_chsize(tmpfd, len) < 0)
2136 PerlLIO_close(tmpfd);
2145 SETERRNO(EBADF,RMS_IFI);
2153 SV * const argsv = POPs;
2154 const unsigned int func = POPu;
2155 const int optype = PL_op->op_type;
2156 GV * const gv = (GV*)POPs;
2157 IO * const io = gv ? GvIOn(gv) : Null(IO*);
2161 if (!io || !argsv || !IoIFP(io)) {
2162 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2163 report_evil_fh(gv, io, PL_op->op_type);
2164 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2168 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2171 s = SvPV_force(argsv, len);
2172 need = IOCPARM_LEN(func);
2174 s = Sv_Grow(argsv, need + 1);
2175 SvCUR_set(argsv, need);
2178 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2181 retval = SvIV(argsv);
2182 s = INT2PTR(char*,retval); /* ouch */
2185 TAINT_PROPER(PL_op_desc[optype]);
2187 if (optype == OP_IOCTL)
2189 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2191 DIE(aTHX_ "ioctl is not implemented");
2195 DIE(aTHX_ "fcntl is not implemented");
2197 #if defined(OS2) && defined(__EMX__)
2198 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2200 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2204 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2206 if (s[SvCUR(argsv)] != 17)
2207 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2209 s[SvCUR(argsv)] = 0; /* put our null back */
2210 SvSETMAGIC(argsv); /* Assume it has changed */
2219 PUSHp(zero_but_true, ZBTLEN);
2232 const int argtype = POPi;
2233 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : (GV*)POPs;
2235 if (gv && (io = GvIO(gv)))
2241 /* XXX Looks to me like io is always NULL at this point */
2243 (void)PerlIO_flush(fp);
2244 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2247 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2248 report_evil_fh(gv, io, PL_op->op_type);
2250 SETERRNO(EBADF,RMS_IFI);
2255 DIE(aTHX_ PL_no_func, "flock()");
2265 const int protocol = POPi;
2266 const int type = POPi;
2267 const int domain = POPi;
2268 GV * const gv = (GV*)POPs;
2269 register IO * const io = gv ? GvIOn(gv) : NULL;
2273 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2274 report_evil_fh(gv, io, PL_op->op_type);
2276 do_close(gv, FALSE);
2277 SETERRNO(EBADF,LIB_INVARG);
2282 do_close(gv, FALSE);
2284 TAINT_PROPER("socket");
2285 fd = PerlSock_socket(domain, type, protocol);
2288 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2289 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2290 IoTYPE(io) = IoTYPE_SOCKET;
2291 if (!IoIFP(io) || !IoOFP(io)) {
2292 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2293 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2294 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2297 #if defined(HAS_FCNTL) && defined(F_SETFD)
2298 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2302 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2307 DIE(aTHX_ PL_no_sock_func, "socket");
2313 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2315 const int protocol = POPi;
2316 const int type = POPi;
2317 const int domain = POPi;
2318 GV * const gv2 = (GV*)POPs;
2319 GV * const gv1 = (GV*)POPs;
2320 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2321 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2324 if (!gv1 || !gv2 || !io1 || !io2) {
2325 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2327 report_evil_fh(gv1, io1, PL_op->op_type);
2329 report_evil_fh(gv1, io2, PL_op->op_type);
2332 do_close(gv1, FALSE);
2334 do_close(gv2, FALSE);
2339 do_close(gv1, FALSE);
2341 do_close(gv2, FALSE);
2343 TAINT_PROPER("socketpair");
2344 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2346 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2347 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2348 IoTYPE(io1) = IoTYPE_SOCKET;
2349 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2350 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2351 IoTYPE(io2) = IoTYPE_SOCKET;
2352 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2353 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2354 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2355 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2356 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2357 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2358 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2361 #if defined(HAS_FCNTL) && defined(F_SETFD)
2362 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2363 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2368 DIE(aTHX_ PL_no_sock_func, "socketpair");
2376 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2377 extern void GETPRIVMODE();
2378 extern void GETUSERMODE();
2380 SV * const addrsv = POPs;
2381 /* OK, so on what platform does bind modify addr? */
2383 GV * const gv = (GV*)POPs;
2384 register IO * const io = GvIOn(gv);
2388 int mpeprivmode = 0;
2391 if (!io || !IoIFP(io))
2394 addr = SvPV_const(addrsv, len);
2395 TAINT_PROPER("bind");
2396 #ifdef MPE /* Deal with MPE bind() peculiarities */
2397 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2398 /* The address *MUST* stupidly be zero. */
2399 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2400 /* PRIV mode is required to bind() to ports < 1024. */
2401 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2402 ((struct sockaddr_in *)addr)->sin_port > 0) {
2403 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2408 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2409 (struct sockaddr *)addr, len) >= 0)
2412 #ifdef MPE /* Switch back to USER mode */
2423 if (ckWARN(WARN_CLOSED))
2424 report_evil_fh(gv, io, PL_op->op_type);
2425 SETERRNO(EBADF,SS_IVCHAN);
2428 DIE(aTHX_ PL_no_sock_func, "bind");
2436 SV * const addrsv = POPs;
2437 GV * const gv = (GV*)POPs;
2438 register IO * const io = GvIOn(gv);
2442 if (!io || !IoIFP(io))
2445 addr = SvPV_const(addrsv, len);
2446 TAINT_PROPER("connect");
2447 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2453 if (ckWARN(WARN_CLOSED))
2454 report_evil_fh(gv, io, PL_op->op_type);
2455 SETERRNO(EBADF,SS_IVCHAN);
2458 DIE(aTHX_ PL_no_sock_func, "connect");
2466 const int backlog = POPi;
2467 GV * const gv = (GV*)POPs;
2468 register IO * const io = gv ? GvIOn(gv) : NULL;
2470 if (!gv || !io || !IoIFP(io))
2473 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2479 if (ckWARN(WARN_CLOSED))
2480 report_evil_fh(gv, io, PL_op->op_type);
2481 SETERRNO(EBADF,SS_IVCHAN);
2484 DIE(aTHX_ PL_no_sock_func, "listen");
2494 char namebuf[MAXPATHLEN];
2495 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2496 Sock_size_t len = sizeof (struct sockaddr_in);
2498 Sock_size_t len = sizeof namebuf;
2500 GV * const ggv = (GV*)POPs;
2501 GV * const ngv = (GV*)POPs;
2510 if (!gstio || !IoIFP(gstio))
2514 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2518 do_close(ngv, FALSE);
2519 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2520 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2521 IoTYPE(nstio) = IoTYPE_SOCKET;
2522 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2523 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2524 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2525 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2528 #if defined(HAS_FCNTL) && defined(F_SETFD)
2529 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2533 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2534 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2536 #ifdef __SCO_VERSION__
2537 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2540 PUSHp(namebuf, len);
2544 if (ckWARN(WARN_CLOSED))
2545 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2546 SETERRNO(EBADF,SS_IVCHAN);
2552 DIE(aTHX_ PL_no_sock_func, "accept");
2560 const int how = POPi;
2561 GV * const gv = (GV*)POPs;
2562 register IO * const io = GvIOn(gv);
2564 if (!io || !IoIFP(io))
2567 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2571 if (ckWARN(WARN_CLOSED))
2572 report_evil_fh(gv, io, PL_op->op_type);
2573 SETERRNO(EBADF,SS_IVCHAN);
2576 DIE(aTHX_ PL_no_sock_func, "shutdown");
2584 const int optype = PL_op->op_type;
2585 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(NEWSV(22, 257)) : POPs;
2586 const unsigned int optname = (unsigned int) POPi;
2587 const unsigned int lvl = (unsigned int) POPi;
2588 GV * const gv = (GV*)POPs;
2589 register IO * const io = GvIOn(gv);
2593 if (!io || !IoIFP(io))
2596 fd = PerlIO_fileno(IoIFP(io));
2600 (void)SvPOK_only(sv);
2604 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2611 #if defined(__SYMBIAN32__)
2612 # define SETSOCKOPT_OPTION_VALUE_T void *
2614 # define SETSOCKOPT_OPTION_VALUE_T const char *
2616 /* XXX TODO: We need to have a proper type (a Configure probe,
2617 * etc.) for what the C headers think of the third argument of
2618 * setsockopt(), the option_value read-only buffer: is it
2619 * a "char *", or a "void *", const or not. Some compilers
2620 * don't take kindly to e.g. assuming that "char *" implicitly
2621 * promotes to a "void *", or to explicitly promoting/demoting
2622 * consts to non/vice versa. The "const void *" is the SUS
2623 * definition, but that does not fly everywhere for the above
2625 SETSOCKOPT_OPTION_VALUE_T buf;
2629 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2633 aint = (int)SvIV(sv);
2634 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2637 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2646 if (ckWARN(WARN_CLOSED))
2647 report_evil_fh(gv, io, optype);
2648 SETERRNO(EBADF,SS_IVCHAN);
2653 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2661 const int optype = PL_op->op_type;
2662 GV * const gv = (GV*)POPs;
2663 register IO * const io = GvIOn(gv);
2668 if (!io || !IoIFP(io))
2671 sv = sv_2mortal(NEWSV(22, 257));
2672 (void)SvPOK_only(sv);
2676 fd = PerlIO_fileno(IoIFP(io));
2678 case OP_GETSOCKNAME:
2679 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2682 case OP_GETPEERNAME:
2683 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2685 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2687 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";
2688 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2689 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2690 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2691 sizeof(u_short) + sizeof(struct in_addr))) {
2698 #ifdef BOGUS_GETNAME_RETURN
2699 /* Interactive Unix, getpeername() and getsockname()
2700 does not return valid namelen */
2701 if (len == BOGUS_GETNAME_RETURN)
2702 len = sizeof(struct sockaddr);
2710 if (ckWARN(WARN_CLOSED))
2711 report_evil_fh(gv, io, optype);
2712 SETERRNO(EBADF,SS_IVCHAN);
2717 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2730 if (PL_op->op_flags & OPf_REF) {
2732 if (PL_op->op_type == OP_LSTAT) {
2733 if (gv != PL_defgv) {
2734 do_fstat_warning_check:
2735 if (ckWARN(WARN_IO))
2736 Perl_warner(aTHX_ packWARN(WARN_IO),
2737 "lstat() on filehandle %s", GvENAME(gv));
2738 } else if (PL_laststype != OP_LSTAT)
2739 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2743 if (gv != PL_defgv) {
2744 PL_laststype = OP_STAT;
2746 sv_setpvn(PL_statname, "", 0);
2747 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2748 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2750 if (PL_laststatval < 0) {
2751 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2752 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2757 SV* const sv = POPs;
2758 if (SvTYPE(sv) == SVt_PVGV) {
2762 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2764 if (PL_op->op_type == OP_LSTAT)
2765 goto do_fstat_warning_check;
2768 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2770 PL_laststype = PL_op->op_type;
2771 if (PL_op->op_type == OP_LSTAT)
2772 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2774 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2775 if (PL_laststatval < 0) {
2776 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2777 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2783 if (gimme != G_ARRAY) {
2784 if (gimme != G_VOID)
2785 XPUSHs(boolSV(max));
2791 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2792 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2793 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2794 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2795 #if Uid_t_size > IVSIZE
2796 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2798 # if Uid_t_sign <= 0
2799 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2801 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2804 #if Gid_t_size > IVSIZE
2805 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2807 # if Gid_t_sign <= 0
2808 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2810 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2813 #ifdef USE_STAT_RDEV
2814 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2816 PUSHs(sv_2mortal(newSVpvn("", 0)));
2818 #if Off_t_size > IVSIZE
2819 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2821 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2824 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2825 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2828 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2830 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2832 #ifdef USE_STAT_BLOCKS
2833 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2834 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2836 PUSHs(sv_2mortal(newSVpvn("", 0)));
2837 PUSHs(sv_2mortal(newSVpvn("", 0)));
2843 /* This macro is used by the stacked filetest operators :
2844 * if the previous filetest failed, short-circuit and pass its value.
2845 * Else, discard it from the stack and continue. --rgs
2847 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2848 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2849 else { (void)POPs; PUTBACK; } \
2855 /* Not const, because things tweak this below. Not bool, because there's
2856 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2857 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2858 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2859 /* Giving some sort of initial value silences compilers. */
2861 int access_mode = R_OK;
2863 int access_mode = 0;
2866 /* access_mode is never used, but leaving use_access in makes the
2867 conditional compiling below much clearer. */
2870 int stat_mode = S_IRUSR;
2872 bool effective = FALSE;
2875 STACKED_FTEST_CHECK;
2877 switch (PL_op->op_type) {
2879 #if !(defined(HAS_ACCESS) && defined(R_OK))
2885 #if defined(HAS_ACCESS) && defined(W_OK)
2890 stat_mode = S_IWUSR;
2894 #if defined(HAS_ACCESS) && defined(X_OK)
2899 stat_mode = S_IXUSR;
2903 #ifdef PERL_EFF_ACCESS
2906 stat_mode = S_IWUSR;
2910 #ifndef PERL_EFF_ACCESS
2918 #ifdef PERL_EFF_ACCESS
2923 stat_mode = S_IXUSR;
2929 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2930 const char *const name = POPpx;
2932 # ifdef PERL_EFF_ACCESS
2933 result = PERL_EFF_ACCESS(name, access_mode);
2935 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
2941 result = access(name, access_mode);
2943 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
2958 if (cando(stat_mode, effective, &PL_statcache))
2966 const int op_type = PL_op->op_type;
2968 STACKED_FTEST_CHECK;
2973 if (op_type == OP_FTIS)
2976 /* You can't dTARGET inside OP_FTIS, because you'll get
2977 "panic: pad_sv po" - the op is not flagged to have a target. */
2981 #if Off_t_size > IVSIZE
2982 PUSHn(PL_statcache.st_size);
2984 PUSHi(PL_statcache.st_size);
2988 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
2991 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
2994 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3006 /* I believe that all these three are likely to be defined on most every
3007 system these days. */
3009 if(PL_op->op_type == OP_FTSUID)
3013 if(PL_op->op_type == OP_FTSGID)
3017 if(PL_op->op_type == OP_FTSVTX)
3021 STACKED_FTEST_CHECK;
3026 switch (PL_op->op_type) {
3028 if (PL_statcache.st_uid == PL_uid)
3032 if (PL_statcache.st_uid == PL_euid)
3036 if (PL_statcache.st_size == 0)
3040 if (S_ISSOCK(PL_statcache.st_mode))
3044 if (S_ISCHR(PL_statcache.st_mode))
3048 if (S_ISBLK(PL_statcache.st_mode))
3052 if (S_ISREG(PL_statcache.st_mode))
3056 if (S_ISDIR(PL_statcache.st_mode))
3060 if (S_ISFIFO(PL_statcache.st_mode))
3065 if (PL_statcache.st_mode & S_ISUID)
3071 if (PL_statcache.st_mode & S_ISGID)
3077 if (PL_statcache.st_mode & S_ISVTX)
3087 I32 result = my_lstat();
3091 if (S_ISLNK(PL_statcache.st_mode))
3103 STACKED_FTEST_CHECK;
3105 if (PL_op->op_flags & OPf_REF)
3107 else if (isGV(TOPs))
3109 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3110 gv = (GV*)SvRV(POPs);
3112 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3114 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3115 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3116 else if (tmpsv && SvOK(tmpsv)) {
3117 const char *tmps = SvPV_nolen_const(tmpsv);
3125 if (PerlLIO_isatty(fd))
3130 #if defined(atarist) /* this will work with atariST. Configure will
3131 make guesses for other systems. */
3132 # define FILE_base(f) ((f)->_base)
3133 # define FILE_ptr(f) ((f)->_ptr)
3134 # define FILE_cnt(f) ((f)->_cnt)
3135 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3145 register STDCHAR *s;
3151 STACKED_FTEST_CHECK;
3153 if (PL_op->op_flags & OPf_REF)
3155 else if (isGV(TOPs))
3157 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3158 gv = (GV*)SvRV(POPs);
3164 if (gv == PL_defgv) {
3166 io = GvIO(PL_statgv);
3169 goto really_filename;
3174 PL_laststatval = -1;
3175 sv_setpvn(PL_statname, "", 0);
3176 io = GvIO(PL_statgv);
3178 if (io && IoIFP(io)) {
3179 if (! PerlIO_has_base(IoIFP(io)))
3180 DIE(aTHX_ "-T and -B not implemented on filehandles");
3181 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3182 if (PL_laststatval < 0)
3184 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3185 if (PL_op->op_type == OP_FTTEXT)
3190 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3191 i = PerlIO_getc(IoIFP(io));
3193 (void)PerlIO_ungetc(IoIFP(io),i);
3195 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3197 len = PerlIO_get_bufsiz(IoIFP(io));
3198 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3199 /* sfio can have large buffers - limit to 512 */
3204 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3206 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3208 SETERRNO(EBADF,RMS_IFI);
3216 PL_laststype = OP_STAT;
3217 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3218 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3219 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3221 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3224 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3225 if (PL_laststatval < 0) {
3226 (void)PerlIO_close(fp);
3229 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3230 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3231 (void)PerlIO_close(fp);
3233 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3234 RETPUSHNO; /* special case NFS directories */
3235 RETPUSHYES; /* null file is anything */
3240 /* now scan s to look for textiness */
3241 /* XXX ASCII dependent code */
3243 #if defined(DOSISH) || defined(USEMYBINMODE)
3244 /* ignore trailing ^Z on short files */
3245 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3249 for (i = 0; i < len; i++, s++) {
3250 if (!*s) { /* null never allowed in text */
3255 else if (!(isPRINT(*s) || isSPACE(*s)))
3258 else if (*s & 128) {
3260 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3263 /* utf8 characters don't count as odd */
3264 if (UTF8_IS_START(*s)) {
3265 int ulen = UTF8SKIP(s);
3266 if (ulen < len - i) {
3268 for (j = 1; j < ulen; j++) {
3269 if (!UTF8_IS_CONTINUATION(s[j]))
3272 --ulen; /* loop does extra increment */
3282 *s != '\n' && *s != '\r' && *s != '\b' &&
3283 *s != '\t' && *s != '\f' && *s != 27)
3288 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3299 const char *tmps = NULL;
3303 SV * const sv = POPs;
3304 if (SvTYPE(sv) == SVt_PVGV) {
3307 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3311 tmps = SvPVx_nolen_const(sv);
3315 if( !gv && (!tmps || !*tmps) ) {
3316 HV * const table = GvHVn(PL_envgv);
3319 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3320 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
3322 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
3327 deprecate("chdir('') or chdir(undef) as chdir()");
3328 tmps = SvPV_nolen_const(*svp);
3332 TAINT_PROPER("chdir");
3337 TAINT_PROPER("chdir");
3340 IO* const io = GvIO(gv);
3343 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3345 else if (IoDIRP(io)) {
3347 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3349 DIE(aTHX_ PL_no_func, "dirfd");
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 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]);
3476 char buf[MAXPATHLEN];
3479 #ifndef INCOMPLETE_TAINTS
3483 len = readlink(tmps, buf, sizeof(buf) - 1);
3491 RETSETUNDEF; /* just pretend it's a normal file */
3495 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3497 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3499 char * const save_filename = filename;
3505 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3506 strcpy(cmdline, cmd);
3507 strcat(cmdline, " ");
3508 for (s = cmdline + strlen(cmdline); *filename; ) {
3513 myfp = PerlProc_popen(cmdline, "r");
3517 SV * const tmpsv = sv_newmortal();
3518 /* Need to save/restore 'PL_rs' ?? */
3519 s = sv_gets(tmpsv, myfp, 0);
3520 (void)PerlProc_pclose(myfp);
3524 #ifdef HAS_SYS_ERRLIST
3529 /* you don't see this */
3531 #ifdef HAS_SYS_ERRLIST
3539 if (instr(s, errmsg)) {
3546 #define EACCES EPERM
3548 if (instr(s, "cannot make"))
3549 SETERRNO(EEXIST,RMS_FEX);
3550 else if (instr(s, "existing file"))
3551 SETERRNO(EEXIST,RMS_FEX);
3552 else if (instr(s, "ile exists"))
3553 SETERRNO(EEXIST,RMS_FEX);
3554 else if (instr(s, "non-exist"))
3555 SETERRNO(ENOENT,RMS_FNF);
3556 else if (instr(s, "does not exist"))
3557 SETERRNO(ENOENT,RMS_FNF);
3558 else if (instr(s, "not empty"))
3559 SETERRNO(EBUSY,SS_DEVOFFLINE);
3560 else if (instr(s, "cannot access"))
3561 SETERRNO(EACCES,RMS_PRV);
3563 SETERRNO(EPERM,RMS_PRV);
3566 else { /* some mkdirs return no failure indication */
3567 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3568 if (PL_op->op_type == OP_RMDIR)
3573 SETERRNO(EACCES,RMS_PRV); /* a guess */
3582 /* This macro removes trailing slashes from a directory name.
3583 * Different operating and file systems take differently to
3584 * trailing slashes. According to POSIX 1003.1 1996 Edition
3585 * any number of trailing slashes should be allowed.
3586 * Thusly we snip them away so that even non-conforming
3587 * systems are happy.
3588 * We should probably do this "filtering" for all
3589 * the functions that expect (potentially) directory names:
3590 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3591 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3593 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3594 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3597 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3598 (tmps) = savepvn((tmps), (len)); \
3608 const int mode = (MAXARG > 1) ? POPi : 0777;
3610 TRIMSLASHES(tmps,len,copy);
3612 TAINT_PROPER("mkdir");
3614 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3618 SETi( dooneliner("mkdir", tmps) );
3619 oldumask = PerlLIO_umask(0);
3620 PerlLIO_umask(oldumask);
3621 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3636 TRIMSLASHES(tmps,len,copy);
3637 TAINT_PROPER("rmdir");
3639 SETi( PerlDir_rmdir(tmps) >= 0 );
3641 SETi( dooneliner("rmdir", tmps) );
3648 /* Directory calls. */
3652 #if defined(Direntry_t) && defined(HAS_READDIR)
3654 const char * const dirname = POPpconstx;
3655 GV * const gv = (GV*)POPs;
3656 register IO * const io = GvIOn(gv);
3662 PerlDir_close(IoDIRP(io));
3663 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3669 SETERRNO(EBADF,RMS_DIR);
3672 DIE(aTHX_ PL_no_dir_func, "opendir");
3678 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3679 DIE(aTHX_ PL_no_dir_func, "readdir");
3681 #if !defined(I_DIRENT) && !defined(VMS)
3682 Direntry_t *readdir (DIR *);
3687 const I32 gimme = GIMME;
3688 GV * const gv = (GV *)POPs;
3689 register const Direntry_t *dp;
3690 register IO * const io = GvIOn(gv);
3692 if (!io || !IoDIRP(io))
3696 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3700 sv = newSVpvn(dp->d_name, dp->d_namlen);
3702 sv = newSVpv(dp->d_name, 0);
3704 #ifndef INCOMPLETE_TAINTS
3705 if (!(IoFLAGS(io) & IOf_UNTAINT))
3708 XPUSHs(sv_2mortal(sv));
3710 while (gimme == G_ARRAY);
3712 if (!dp && gimme != G_ARRAY)
3719 SETERRNO(EBADF,RMS_ISI);
3720 if (GIMME == G_ARRAY)
3729 #if defined(HAS_TELLDIR) || defined(telldir)
3731 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3732 /* XXX netbsd still seemed to.
3733 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3734 --JHI 1999-Feb-02 */
3735 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3736 long telldir (DIR *);
3738 GV * const gv = (GV*)POPs;
3739 register IO * const io = GvIOn(gv);
3741 if (!io || !IoDIRP(io))
3744 PUSHi( PerlDir_tell(IoDIRP(io)) );
3748 SETERRNO(EBADF,RMS_ISI);
3751 DIE(aTHX_ PL_no_dir_func, "telldir");
3757 #if defined(HAS_SEEKDIR) || defined(seekdir)
3759 const long along = POPl;
3760 GV * const gv = (GV*)POPs;
3761 register IO * const io = GvIOn(gv);
3763 if (!io || !IoDIRP(io))
3766 (void)PerlDir_seek(IoDIRP(io), along);
3771 SETERRNO(EBADF,RMS_ISI);
3774 DIE(aTHX_ PL_no_dir_func, "seekdir");
3780 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3782 GV * const gv = (GV*)POPs;
3783 register IO * const io = GvIOn(gv);
3785 if (!io || !IoDIRP(io))
3788 (void)PerlDir_rewind(IoDIRP(io));
3792 SETERRNO(EBADF,RMS_ISI);
3795 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3801 #if defined(Direntry_t) && defined(HAS_READDIR)
3803 GV * const gv = (GV*)POPs;
3804 register IO * const io = GvIOn(gv);
3806 if (!io || !IoDIRP(io))
3809 #ifdef VOID_CLOSEDIR
3810 PerlDir_close(IoDIRP(io));
3812 if (PerlDir_close(IoDIRP(io)) < 0) {
3813 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3822 SETERRNO(EBADF,RMS_IFI);
3825 DIE(aTHX_ PL_no_dir_func, "closedir");
3829 /* Process control. */
3838 PERL_FLUSHALL_FOR_CHILD;
3839 childpid = PerlProc_fork();
3843 GV * const tmpgv = gv_fetchpv("$", GV_ADD, SVt_PV);
3845 SvREADONLY_off(GvSV(tmpgv));
3846 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3847 SvREADONLY_on(GvSV(tmpgv));
3849 #ifdef THREADS_HAVE_PIDS
3850 PL_ppid = (IV)getppid();
3852 #ifdef PERL_USES_PL_PIDSTATUS
3853 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3859 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3864 PERL_FLUSHALL_FOR_CHILD;
3865 childpid = PerlProc_fork();
3871 DIE(aTHX_ PL_no_func, "fork");
3878 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3883 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3884 childpid = wait4pid(-1, &argflags, 0);
3886 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3891 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3892 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3893 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
3895 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
3900 DIE(aTHX_ PL_no_func, "wait");
3906 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3908 const int optype = POPi;
3909 const Pid_t pid = TOPi;
3913 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3914 result = wait4pid(pid, &argflags, optype);
3916 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
3921 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3922 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3923 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
3925 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
3930 DIE(aTHX_ PL_no_func, "waitpid");
3936 dSP; dMARK; dORIGMARK; dTARGET;
3942 while (++MARK <= SP) {
3943 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
3948 TAINT_PROPER("system");
3950 PERL_FLUSHALL_FOR_CHILD;
3951 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
3957 if (PerlProc_pipe(pp) >= 0)
3959 while ((childpid = PerlProc_fork()) == -1) {
3960 if (errno != EAGAIN) {
3965 PerlLIO_close(pp[0]);
3966 PerlLIO_close(pp[1]);
3973 Sigsave_t ihand,qhand; /* place to save signals during system() */
3977 PerlLIO_close(pp[1]);
3979 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
3980 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
3983 result = wait4pid(childpid, &status, 0);
3984 } while (result == -1 && errno == EINTR);
3986 (void)rsignal_restore(SIGINT, &ihand);
3987 (void)rsignal_restore(SIGQUIT, &qhand);
3989 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
3990 do_execfree(); /* free any memory child malloced on fork */
3996 while (n < sizeof(int)) {
3997 n1 = PerlLIO_read(pp[0],
3998 (void*)(((char*)&errkid)+n),
4004 PerlLIO_close(pp[0]);
4005 if (n) { /* Error */
4006 if (n != sizeof(int))
4007 DIE(aTHX_ "panic: kid popen errno read");
4008 errno = errkid; /* Propagate errno from kid */
4009 STATUS_NATIVE_CHILD_SET(-1);
4012 XPUSHi(STATUS_CURRENT);
4016 PerlLIO_close(pp[0]);
4017 #if defined(HAS_FCNTL) && defined(F_SETFD)
4018 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4021 if (PL_op->op_flags & OPf_STACKED) {
4022 SV * const really = *++MARK;
4023 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4025 else if (SP - MARK != 1)
4026 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4028 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4032 #else /* ! FORK or VMS or OS/2 */
4035 if (PL_op->op_flags & OPf_STACKED) {
4036 SV * const really = *++MARK;
4037 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4038 value = (I32)do_aspawn(really, MARK, SP);
4040 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4043 else if (SP - MARK != 1) {
4044 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4045 value = (I32)do_aspawn(Nullsv, MARK, SP);
4047 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4051 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4053 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4055 STATUS_NATIVE_CHILD_SET(value);
4058 XPUSHi(result ? value : STATUS_CURRENT);
4059 #endif /* !FORK or VMS */
4065 dSP; dMARK; dORIGMARK; dTARGET;
4070 while (++MARK <= SP) {
4071 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4076 TAINT_PROPER("exec");
4078 PERL_FLUSHALL_FOR_CHILD;
4079 if (PL_op->op_flags & OPf_STACKED) {
4080 SV * const really = *++MARK;
4081 value = (I32)do_aexec(really, MARK, SP);
4083 else if (SP - MARK != 1)
4085 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4089 (void ) do_aspawn(Nullsv, MARK, SP);
4093 value = (I32)do_aexec(Nullsv, MARK, SP);
4098 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4101 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4104 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4118 # ifdef THREADS_HAVE_PIDS
4119 if (PL_ppid != 1 && getppid() == 1)
4120 /* maybe the parent process has died. Refresh ppid cache */
4124 XPUSHi( getppid() );
4128 DIE(aTHX_ PL_no_func, "getppid");
4137 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4140 pgrp = (I32)BSD_GETPGRP(pid);
4142 if (pid != 0 && pid != PerlProc_getpid())
4143 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4149 DIE(aTHX_ PL_no_func, "getpgrp()");
4168 TAINT_PROPER("setpgrp");
4170 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4172 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4173 || (pid != 0 && pid != PerlProc_getpid()))
4175 DIE(aTHX_ "setpgrp can't take arguments");
4177 SETi( setpgrp() >= 0 );
4178 #endif /* USE_BSDPGRP */
4181 DIE(aTHX_ PL_no_func, "setpgrp()");
4187 #ifdef HAS_GETPRIORITY
4189 const int who = POPi;
4190 const int which = TOPi;
4191 SETi( getpriority(which, who) );
4194 DIE(aTHX_ PL_no_func, "getpriority()");
4200 #ifdef HAS_SETPRIORITY
4202 const int niceval = POPi;
4203 const int who = POPi;
4204 const int which = TOPi;
4205 TAINT_PROPER("setpriority");
4206 SETi( setpriority(which, who, niceval) >= 0 );
4209 DIE(aTHX_ PL_no_func, "setpriority()");
4219 XPUSHn( time(Null(Time_t*)) );
4221 XPUSHi( time(Null(Time_t*)) );
4232 (void)PerlProc_times(&PL_timesbuf);
4234 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4235 /* struct tms, though same data */
4239 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4240 if (GIMME == G_ARRAY) {
4241 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4242 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4243 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4249 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4251 if (GIMME == G_ARRAY) {
4252 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4253 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4254 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4258 DIE(aTHX_ "times not implemented");
4260 #endif /* HAS_TIMES */
4263 #ifdef LOCALTIME_EDGECASE_BROKEN
4264 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4269 /* No workarounds in the valid range */
4270 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4271 return (localtime (tp));
4273 /* This edge case is to workaround the undefined behaviour, where the
4274 * TIMEZONE makes the time go beyond the defined range.
4275 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4276 * If there is a negative offset in TZ, like MET-1METDST, some broken
4277 * implementations of localtime () (like AIX 5.2) barf with bogus
4279 * 0x7fffffff gmtime 2038-01-19 03:14:07
4280 * 0x7fffffff localtime 1901-12-13 21:45:51
4281 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4282 * 0x3c19137f gmtime 2001-12-13 20:45:51
4283 * 0x3c19137f localtime 2001-12-13 21:45:51
4284 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4285 * Given that legal timezones are typically between GMT-12 and GMT+12
4286 * we turn back the clock 23 hours before calling the localtime
4287 * function, and add those to the return value. This will never cause
4288 * day wrapping problems, since the edge case is Tue Jan *19*
4290 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4293 if (P->tm_hour >= 24) {
4295 P->tm_mday++; /* 18 -> 19 */
4296 P->tm_wday++; /* Mon -> Tue */
4297 P->tm_yday++; /* 18 -> 19 */
4300 } /* S_my_localtime */
4307 const struct tm *tmbuf;
4308 static const char * const dayname[] =
4309 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4310 static const char * const monname[] =
4311 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4312 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4318 when = (Time_t)SvNVx(POPs);
4320 when = (Time_t)SvIVx(POPs);
4323 if (PL_op->op_type == OP_LOCALTIME)
4324 #ifdef LOCALTIME_EDGECASE_BROKEN
4325 tmbuf = S_my_localtime(aTHX_ &when);
4327 tmbuf = localtime(&when);
4330 tmbuf = gmtime(&when);
4332 if (GIMME != G_ARRAY) {
4338 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4339 dayname[tmbuf->tm_wday],
4340 monname[tmbuf->tm_mon],
4345 tmbuf->tm_year + 1900);
4346 PUSHs(sv_2mortal(tsv));
4351 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4352 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4353 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4354 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4355 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4356 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4357 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4358 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4359 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4370 anum = alarm((unsigned int)anum);
4377 DIE(aTHX_ PL_no_func, "alarm");
4388 (void)time(&lasttime);
4393 PerlProc_sleep((unsigned int)duration);
4396 XPUSHi(when - lasttime);
4400 /* Shared memory. */
4401 /* Merged with some message passing. */
4405 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4406 dSP; dMARK; dTARGET;
4407 const int op_type = PL_op->op_type;
4412 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4415 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4418 value = (I32)(do_semop(MARK, SP) >= 0);
4421 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4437 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4438 dSP; dMARK; dTARGET;
4439 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4446 DIE(aTHX_ "System V IPC is not implemented on this machine");
4452 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4453 dSP; dMARK; dTARGET;
4454 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4462 PUSHp(zero_but_true, ZBTLEN);
4470 /* Get system info. */
4474 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4476 I32 which = PL_op->op_type;
4477 register char **elem;
4479 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4480 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4481 struct hostent *gethostbyname(Netdb_name_t);
4482 struct hostent *gethostent(void);
4484 struct hostent *hent;
4488 if (which == OP_GHBYNAME) {
4489 #ifdef HAS_GETHOSTBYNAME
4490 const char* const name = POPpbytex;
4491 hent = PerlSock_gethostbyname(name);
4493 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4496 else if (which == OP_GHBYADDR) {
4497 #ifdef HAS_GETHOSTBYADDR
4498 const int addrtype = POPi;
4499 SV * const addrsv = POPs;
4501 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4503 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4505 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4509 #ifdef HAS_GETHOSTENT
4510 hent = PerlSock_gethostent();
4512 DIE(aTHX_ PL_no_sock_func, "gethostent");
4515 #ifdef HOST_NOT_FOUND
4517 #ifdef USE_REENTRANT_API
4518 # ifdef USE_GETHOSTENT_ERRNO
4519 h_errno = PL_reentrant_buffer->_gethostent_errno;
4522 STATUS_UNIX_SET(h_errno);
4526 if (GIMME != G_ARRAY) {
4527 PUSHs(sv = sv_newmortal());
4529 if (which == OP_GHBYNAME) {
4531 sv_setpvn(sv, hent->h_addr, hent->h_length);
4534 sv_setpv(sv, (char*)hent->h_name);
4540 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4541 sv_setpv(sv, (char*)hent->h_name);
4542 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4543 for (elem = hent->h_aliases; elem && *elem; elem++) {
4544 sv_catpv(sv, *elem);
4546 sv_catpvn(sv, " ", 1);
4548 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4549 sv_setiv(sv, (IV)hent->h_addrtype);
4550 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4551 len = hent->h_length;
4552 sv_setiv(sv, (IV)len);
4554 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4555 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4556 sv_setpvn(sv, *elem, len);
4559 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4561 sv_setpvn(sv, hent->h_addr, len);
4566 DIE(aTHX_ PL_no_sock_func, "gethostent");
4572 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4574 I32 which = PL_op->op_type;
4575 register char **elem;
4577 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4578 struct netent *getnetbyaddr(Netdb_net_t, int);
4579 struct netent *getnetbyname(Netdb_name_t);
4580 struct netent *getnetent(void);
4582 struct netent *nent;
4584 if (which == OP_GNBYNAME){
4585 #ifdef HAS_GETNETBYNAME
4586 const char * const name = POPpbytex;
4587 nent = PerlSock_getnetbyname(name);
4589 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4592 else if (which == OP_GNBYADDR) {
4593 #ifdef HAS_GETNETBYADDR
4594 const int addrtype = POPi;
4595 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4596 nent = PerlSock_getnetbyaddr(addr, addrtype);
4598 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4602 #ifdef HAS_GETNETENT
4603 nent = PerlSock_getnetent();
4605 DIE(aTHX_ PL_no_sock_func, "getnetent");
4608 #ifdef HOST_NOT_FOUND
4610 #ifdef USE_REENTRANT_API
4611 # ifdef USE_GETNETENT_ERRNO
4612 h_errno = PL_reentrant_buffer->_getnetent_errno;
4615 STATUS_UNIX_SET(h_errno);
4620 if (GIMME != G_ARRAY) {
4621 PUSHs(sv = sv_newmortal());
4623 if (which == OP_GNBYNAME)
4624 sv_setiv(sv, (IV)nent->n_net);
4626 sv_setpv(sv, nent->n_name);
4632 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4633 sv_setpv(sv, nent->n_name);
4634 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4635 for (elem = nent->n_aliases; elem && *elem; elem++) {
4636 sv_catpv(sv, *elem);
4638 sv_catpvn(sv, " ", 1);
4640 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4641 sv_setiv(sv, (IV)nent->n_addrtype);
4642 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4643 sv_setiv(sv, (IV)nent->n_net);
4648 DIE(aTHX_ PL_no_sock_func, "getnetent");
4654 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4656 I32 which = PL_op->op_type;
4657 register char **elem;
4659 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4660 struct protoent *getprotobyname(Netdb_name_t);
4661 struct protoent *getprotobynumber(int);
4662 struct protoent *getprotoent(void);
4664 struct protoent *pent;
4666 if (which == OP_GPBYNAME) {
4667 #ifdef HAS_GETPROTOBYNAME
4668 const char* const name = POPpbytex;
4669 pent = PerlSock_getprotobyname(name);
4671 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4674 else if (which == OP_GPBYNUMBER) {
4675 #ifdef HAS_GETPROTOBYNUMBER
4676 const int number = POPi;
4677 pent = PerlSock_getprotobynumber(number);
4679 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4683 #ifdef HAS_GETPROTOENT
4684 pent = PerlSock_getprotoent();
4686 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4690 if (GIMME != G_ARRAY) {
4691 PUSHs(sv = sv_newmortal());
4693 if (which == OP_GPBYNAME)
4694 sv_setiv(sv, (IV)pent->p_proto);
4696 sv_setpv(sv, pent->p_name);
4702 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4703 sv_setpv(sv, pent->p_name);
4704 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4705 for (elem = pent->p_aliases; elem && *elem; elem++) {
4706 sv_catpv(sv, *elem);
4708 sv_catpvn(sv, " ", 1);
4710 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4711 sv_setiv(sv, (IV)pent->p_proto);
4716 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4722 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4724 I32 which = PL_op->op_type;
4725 register char **elem;
4727 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4728 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4729 struct servent *getservbyport(int, Netdb_name_t);
4730 struct servent *getservent(void);
4732 struct servent *sent;
4734 if (which == OP_GSBYNAME) {
4735 #ifdef HAS_GETSERVBYNAME
4736 const char * const proto = POPpbytex;
4737 const char * const name = POPpbytex;
4738 sent = PerlSock_getservbyname(name, (proto && !*proto) ? Nullch : proto);
4740 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4743 else if (which == OP_GSBYPORT) {
4744 #ifdef HAS_GETSERVBYPORT
4745 const char * const proto = POPpbytex;
4746 unsigned short port = (unsigned short)POPu;
4748 port = PerlSock_htons(port);
4750 sent = PerlSock_getservbyport(port, (proto && !*proto) ? Nullch : proto);
4752 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4756 #ifdef HAS_GETSERVENT
4757 sent = PerlSock_getservent();
4759 DIE(aTHX_ PL_no_sock_func, "getservent");
4763 if (GIMME != G_ARRAY) {
4764 PUSHs(sv = sv_newmortal());
4766 if (which == OP_GSBYNAME) {
4768 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4770 sv_setiv(sv, (IV)(sent->s_port));
4774 sv_setpv(sv, sent->s_name);
4780 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4781 sv_setpv(sv, sent->s_name);
4782 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4783 for (elem = sent->s_aliases; elem && *elem; elem++) {
4784 sv_catpv(sv, *elem);
4786 sv_catpvn(sv, " ", 1);
4788 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4790 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4792 sv_setiv(sv, (IV)(sent->s_port));
4794 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4795 sv_setpv(sv, sent->s_proto);
4800 DIE(aTHX_ PL_no_sock_func, "getservent");
4806 #ifdef HAS_SETHOSTENT
4808 PerlSock_sethostent(TOPi);
4811 DIE(aTHX_ PL_no_sock_func, "sethostent");
4817 #ifdef HAS_SETNETENT
4819 PerlSock_setnetent(TOPi);
4822 DIE(aTHX_ PL_no_sock_func, "setnetent");
4828 #ifdef HAS_SETPROTOENT
4830 PerlSock_setprotoent(TOPi);
4833 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4839 #ifdef HAS_SETSERVENT
4841 PerlSock_setservent(TOPi);
4844 DIE(aTHX_ PL_no_sock_func, "setservent");
4850 #ifdef HAS_ENDHOSTENT
4852 PerlSock_endhostent();
4856 DIE(aTHX_ PL_no_sock_func, "endhostent");
4862 #ifdef HAS_ENDNETENT
4864 PerlSock_endnetent();
4868 DIE(aTHX_ PL_no_sock_func, "endnetent");
4874 #ifdef HAS_ENDPROTOENT
4876 PerlSock_endprotoent();
4880 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4886 #ifdef HAS_ENDSERVENT
4888 PerlSock_endservent();
4892 DIE(aTHX_ PL_no_sock_func, "endservent");
4900 I32 which = PL_op->op_type;
4902 struct passwd *pwent = NULL;
4904 * We currently support only the SysV getsp* shadow password interface.
4905 * The interface is declared in <shadow.h> and often one needs to link
4906 * with -lsecurity or some such.
4907 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
4910 * AIX getpwnam() is clever enough to return the encrypted password
4911 * only if the caller (euid?) is root.
4913 * There are at least three other shadow password APIs. Many platforms
4914 * seem to contain more than one interface for accessing the shadow
4915 * password databases, possibly for compatibility reasons.
4916 * The getsp*() is by far he simplest one, the other two interfaces
4917 * are much more complicated, but also very similar to each other.
4922 * struct pr_passwd *getprpw*();
4923 * The password is in
4924 * char getprpw*(...).ufld.fd_encrypt[]
4925 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
4930 * struct es_passwd *getespw*();
4931 * The password is in
4932 * char *(getespw*(...).ufld.fd_encrypt)
4933 * Mention HAS_GETESPWNAM here so that Configure probes for it.
4936 * struct userpw *getuserpw();
4937 * The password is in
4938 * char *(getuserpw(...)).spw_upw_passwd
4939 * (but the de facto standard getpwnam() should work okay)
4941 * Mention I_PROT here so that Configure probes for it.
4943 * In HP-UX for getprpw*() the manual page claims that one should include
4944 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
4945 * if one includes <shadow.h> as that includes <hpsecurity.h>,
4946 * and pp_sys.c already includes <shadow.h> if there is such.
4948 * Note that <sys/security.h> is already probed for, but currently
4949 * it is only included in special cases.
4951 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
4952 * be preferred interface, even though also the getprpw*() interface
4953 * is available) one needs to link with -lsecurity -ldb -laud -lm.
4954 * One also needs to call set_auth_parameters() in main() before
4955 * doing anything else, whether one is using getespw*() or getprpw*().
4957 * Note that accessing the shadow databases can be magnitudes
4958 * slower than accessing the standard databases.
4963 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
4964 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
4965 * the pw_comment is left uninitialized. */
4966 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
4972 const char* const name = POPpbytex;
4973 pwent = getpwnam(name);
4979 pwent = getpwuid(uid);
4983 # ifdef HAS_GETPWENT
4985 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
4986 if (pwent) pwent = getpwnam(pwent->pw_name);
4989 DIE(aTHX_ PL_no_func, "getpwent");
4995 if (GIMME != G_ARRAY) {
4996 PUSHs(sv = sv_newmortal());
4998 if (which == OP_GPWNAM)
4999 # if Uid_t_sign <= 0
5000 sv_setiv(sv, (IV)pwent->pw_uid);
5002 sv_setuv(sv, (UV)pwent->pw_uid);
5005 sv_setpv(sv, pwent->pw_name);
5011 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5012 sv_setpv(sv, pwent->pw_name);
5014 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5016 /* If we have getspnam(), we try to dig up the shadow
5017 * password. If we are underprivileged, the shadow
5018 * interface will set the errno to EACCES or similar,
5019 * and return a null pointer. If this happens, we will
5020 * use the dummy password (usually "*" or "x") from the
5021 * standard password database.
5023 * In theory we could skip the shadow call completely
5024 * if euid != 0 but in practice we cannot know which
5025 * security measures are guarding the shadow databases
5026 * on a random platform.
5028 * Resist the urge to use additional shadow interfaces.
5029 * Divert the urge to writing an extension instead.
5032 /* Some AIX setups falsely(?) detect some getspnam(), which
5033 * has a different API than the Solaris/IRIX one. */
5034 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5036 const int saverrno = errno;
5037 const struct spwd * const spwent = getspnam(pwent->pw_name);
5038 /* Save and restore errno so that
5039 * underprivileged attempts seem
5040 * to have never made the unsccessful
5041 * attempt to retrieve the shadow password. */
5043 if (spwent && spwent->sp_pwdp)
5044 sv_setpv(sv, spwent->sp_pwdp);
5048 if (!SvPOK(sv)) /* Use the standard password, then. */
5049 sv_setpv(sv, pwent->pw_passwd);
5052 # ifndef INCOMPLETE_TAINTS
5053 /* passwd is tainted because user himself can diddle with it.
5054 * admittedly not much and in a very limited way, but nevertheless. */
5058 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5059 # if Uid_t_sign <= 0
5060 sv_setiv(sv, (IV)pwent->pw_uid);
5062 sv_setuv(sv, (UV)pwent->pw_uid);
5065 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5066 # if Uid_t_sign <= 0
5067 sv_setiv(sv, (IV)pwent->pw_gid);
5069 sv_setuv(sv, (UV)pwent->pw_gid);
5071 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5072 * because of the poor interface of the Perl getpw*(),
5073 * not because there's some standard/convention saying so.
5074 * A better interface would have been to return a hash,
5075 * but we are accursed by our history, alas. --jhi. */
5076 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5078 sv_setiv(sv, (IV)pwent->pw_change);
5081 sv_setiv(sv, (IV)pwent->pw_quota);
5084 sv_setpv(sv, pwent->pw_age);
5089 /* pw_class and pw_comment are mutually exclusive--.
5090 * see the above note for pw_change, pw_quota, and pw_age. */
5091 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5093 sv_setpv(sv, pwent->pw_class);
5096 sv_setpv(sv, pwent->pw_comment);
5100 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5102 sv_setpv(sv, pwent->pw_gecos);
5104 # ifndef INCOMPLETE_TAINTS
5105 /* pw_gecos is tainted because user himself can diddle with it. */
5109 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5110 sv_setpv(sv, pwent->pw_dir);
5112 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5113 sv_setpv(sv, pwent->pw_shell);
5114 # ifndef INCOMPLETE_TAINTS
5115 /* pw_shell is tainted because user himself can diddle with it. */
5120 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5121 sv_setiv(sv, (IV)pwent->pw_expire);
5126 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5132 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5137 DIE(aTHX_ PL_no_func, "setpwent");
5143 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5148 DIE(aTHX_ PL_no_func, "endpwent");
5156 I32 which = PL_op->op_type;
5157 register char **elem;
5159 struct group *grent;
5161 if (which == OP_GGRNAM) {
5162 const char* const name = POPpbytex;
5163 grent = (struct group *)getgrnam(name);
5165 else if (which == OP_GGRGID) {
5166 const Gid_t gid = POPi;
5167 grent = (struct group *)getgrgid(gid);
5171 grent = (struct group *)getgrent();
5173 DIE(aTHX_ PL_no_func, "getgrent");
5177 if (GIMME != G_ARRAY) {
5178 PUSHs(sv = sv_newmortal());
5180 if (which == OP_GGRNAM)
5181 sv_setiv(sv, (IV)grent->gr_gid);
5183 sv_setpv(sv, grent->gr_name);
5189 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5190 sv_setpv(sv, grent->gr_name);
5192 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5194 sv_setpv(sv, grent->gr_passwd);
5197 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5198 sv_setiv(sv, (IV)grent->gr_gid);
5200 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5201 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5202 /* In UNICOS/mk (_CRAYMPP) the multithreading
5203 * versions (getgrnam_r, getgrgid_r)
5204 * seem to return an illegal pointer
5205 * as the group members list, gr_mem.
5206 * getgrent() doesn't even have a _r version
5207 * but the gr_mem is poisonous anyway.
5208 * So yes, you cannot get the list of group
5209 * members if building multithreaded in UNICOS/mk. */
5210 for (elem = grent->gr_mem; elem && *elem; elem++) {
5211 sv_catpv(sv, *elem);
5213 sv_catpvn(sv, " ", 1);
5220 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5226 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5231 DIE(aTHX_ PL_no_func, "setgrent");
5237 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5242 DIE(aTHX_ PL_no_func, "endgrent");
5252 if (!(tmps = PerlProc_getlogin()))
5254 PUSHp(tmps, strlen(tmps));
5257 DIE(aTHX_ PL_no_func, "getlogin");
5261 /* Miscellaneous. */
5266 dSP; dMARK; dORIGMARK; dTARGET;
5267 register I32 items = SP - MARK;
5268 unsigned long a[20];
5273 while (++MARK <= SP) {
5274 if (SvTAINTED(*MARK)) {
5280 TAINT_PROPER("syscall");
5283 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5284 * or where sizeof(long) != sizeof(char*). But such machines will
5285 * not likely have syscall implemented either, so who cares?
5287 while (++MARK <= SP) {
5288 if (SvNIOK(*MARK) || !i)
5289 a[i++] = SvIV(*MARK);
5290 else if (*MARK == &PL_sv_undef)
5293 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5299 DIE(aTHX_ "Too many args to syscall");
5301 DIE(aTHX_ "Too few args to syscall");
5303 retval = syscall(a[0]);
5306 retval = syscall(a[0],a[1]);
5309 retval = syscall(a[0],a[1],a[2]);
5312 retval = syscall(a[0],a[1],a[2],a[3]);
5315 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5318 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5321 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5324 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5328 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5331 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5334 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5338 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5342 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5346 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5347 a[10],a[11],a[12],a[13]);
5349 #endif /* atarist */
5355 DIE(aTHX_ PL_no_func, "syscall");
5359 #ifdef FCNTL_EMULATE_FLOCK
5361 /* XXX Emulate flock() with fcntl().
5362 What's really needed is a good file locking module.
5366 fcntl_emulate_flock(int fd, int operation)
5370 switch (operation & ~LOCK_NB) {
5372 flock.l_type = F_RDLCK;
5375 flock.l_type = F_WRLCK;
5378 flock.l_type = F_UNLCK;
5384 flock.l_whence = SEEK_SET;
5385 flock.l_start = flock.l_len = (Off_t)0;
5387 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5390 #endif /* FCNTL_EMULATE_FLOCK */
5392 #ifdef LOCKF_EMULATE_FLOCK
5394 /* XXX Emulate flock() with lockf(). This is just to increase
5395 portability of scripts. The calls are not completely
5396 interchangeable. What's really needed is a good file
5400 /* The lockf() constants might have been defined in <unistd.h>.
5401 Unfortunately, <unistd.h> causes troubles on some mixed
5402 (BSD/POSIX) systems, such as SunOS 4.1.3.
5404 Further, the lockf() constants aren't POSIX, so they might not be
5405 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5406 just stick in the SVID values and be done with it. Sigh.
5410 # define F_ULOCK 0 /* Unlock a previously locked region */
5413 # define F_LOCK 1 /* Lock a region for exclusive use */
5416 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5419 # define F_TEST 3 /* Test a region for other processes locks */
5423 lockf_emulate_flock(int fd, int operation)
5426 const int save_errno = errno;
5429 /* flock locks entire file so for lockf we need to do the same */
5430 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5431 if (pos > 0) /* is seekable and needs to be repositioned */
5432 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5433 pos = -1; /* seek failed, so don't seek back afterwards */
5436 switch (operation) {
5438 /* LOCK_SH - get a shared lock */
5440 /* LOCK_EX - get an exclusive lock */
5442 i = lockf (fd, F_LOCK, 0);
5445 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5446 case LOCK_SH|LOCK_NB:
5447 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5448 case LOCK_EX|LOCK_NB:
5449 i = lockf (fd, F_TLOCK, 0);
5451 if ((errno == EAGAIN) || (errno == EACCES))
5452 errno = EWOULDBLOCK;
5455 /* LOCK_UN - unlock (non-blocking is a no-op) */
5457 case LOCK_UN|LOCK_NB:
5458 i = lockf (fd, F_ULOCK, 0);
5461 /* Default - can't decipher operation */
5468 if (pos > 0) /* need to restore position of the handle */
5469 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5474 #endif /* LOCKF_EMULATE_FLOCK */
5478 * c-indentation-style: bsd
5480 * indent-tabs-mode: t
5483 * ex: set ts=8 sts=4 sw=4 noet: