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 *type = NULL;
334 if (PL_curcop->cop_io) {
335 type = SvPV_nolen_const(PL_curcop->cop_io);
338 PerlIO_apply_layers(aTHX_ fp,mode,type);
340 if (gimme == G_VOID) {
342 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
345 else if (gimme == G_SCALAR) {
348 PL_rs = &PL_sv_undef;
349 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
350 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
358 SV * const sv = NEWSV(56, 79);
359 if (sv_gets(sv, fp, 0) == Nullch) {
363 XPUSHs(sv_2mortal(sv));
364 if (SvLEN(sv) - SvCUR(sv) > 20) {
365 SvPV_shrink_to_cur(sv);
370 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
371 TAINT; /* "I believe that this is not gratuitous!" */
374 STATUS_NATIVE_CHILD_SET(-1);
375 if (gimme == G_SCALAR)
386 tryAMAGICunTARGET(iter, -1);
388 /* Note that we only ever get here if File::Glob fails to load
389 * without at the same time croaking, for some reason, or if
390 * perl was built with PERL_EXTERNAL_GLOB */
397 * The external globbing program may use things we can't control,
398 * so for security reasons we must assume the worst.
401 taint_proper(PL_no_security, "glob");
405 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
406 PL_last_in_gv = (GV*)*PL_stack_sp--;
408 SAVESPTR(PL_rs); /* This is not permanent, either. */
409 PL_rs = sv_2mortal(newSVpvn("\000", 1));
412 *SvPVX(PL_rs) = '\n';
416 result = do_readline();
423 PL_last_in_gv = cGVOP_gv;
424 return do_readline();
435 do_join(TARG, &PL_sv_no, MARK, SP);
439 else if (SP == MARK) {
446 tmps = SvPV_const(tmpsv, len);
447 if ((!tmps || !len) && PL_errgv) {
448 SV * const error = ERRSV;
449 SvUPGRADE(error, SVt_PV);
450 if (SvPOK(error) && SvCUR(error))
451 sv_catpv(error, "\t...caught");
453 tmps = SvPV_const(tmpsv, len);
456 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
458 Perl_warn(aTHX_ "%"SVf, tmpsv);
470 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
472 if (SP - MARK != 1) {
474 do_join(TARG, &PL_sv_no, MARK, SP);
476 tmps = SvPV_const(tmpsv, len);
482 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
486 SvUPGRADE(error, SVt_PV);
487 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
489 SvSetSV(error,tmpsv);
490 else if (sv_isobject(error)) {
491 HV *stash = SvSTASH(SvRV(error));
492 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
494 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
495 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
502 call_sv((SV*)GvCV(gv),
503 G_SCALAR|G_EVAL|G_KEEPERR);
504 sv_setsv(error,*PL_stack_sp--);
510 if (SvPOK(error) && SvCUR(error))
511 sv_catpv(error, "\t...propagated");
514 tmps = SvPV_const(tmpsv, len);
520 tmpsv = sv_2mortal(newSVpvn("Died", 4));
522 DIE(aTHX_ "%"SVf, tmpsv);
538 GV * const gv = (GV *)*++MARK;
541 DIE(aTHX_ PL_no_usym, "filehandle");
542 if ((io = GvIOp(gv)))
543 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
546 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
548 /* Method's args are same as ours ... */
549 /* ... except handle is replaced by the object */
550 *MARK-- = SvTIED_obj((SV*)io, mg);
554 call_method("OPEN", G_SCALAR);
568 tmps = SvPV_const(sv, len);
569 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
572 PUSHi( (I32)PL_forkprocess );
573 else if (PL_forkprocess == 0) /* we are a new child */
585 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
587 if (gv && (io = GvIO(gv))
588 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
591 XPUSHs(SvTIED_obj((SV*)io, mg));
594 call_method("CLOSE", G_SCALAR);
600 PUSHs(boolSV(do_close(gv, TRUE)));
612 GV * const wgv = (GV*)POPs;
613 GV * const rgv = (GV*)POPs;
618 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
619 DIE(aTHX_ PL_no_usym, "filehandle");
624 do_close(rgv, FALSE);
626 do_close(wgv, FALSE);
628 if (PerlProc_pipe(fd) < 0)
631 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
632 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
633 IoOFP(rstio) = IoIFP(rstio);
634 IoIFP(wstio) = IoOFP(wstio);
635 IoTYPE(rstio) = IoTYPE_RDONLY;
636 IoTYPE(wstio) = IoTYPE_WRONLY;
638 if (!IoIFP(rstio) || !IoOFP(wstio)) {
639 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
640 else PerlLIO_close(fd[0]);
641 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
642 else PerlLIO_close(fd[1]);
645 #if defined(HAS_FCNTL) && defined(F_SETFD)
646 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
647 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
654 DIE(aTHX_ PL_no_func, "pipe");
670 if (gv && (io = GvIO(gv))
671 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
674 XPUSHs(SvTIED_obj((SV*)io, mg));
677 call_method("FILENO", G_SCALAR);
683 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
684 /* Can't do this because people seem to do things like
685 defined(fileno($foo)) to check whether $foo is a valid fh.
686 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
687 report_evil_fh(gv, io, PL_op->op_type);
692 PUSHi(PerlIO_fileno(fp));
704 anum = PerlLIO_umask(0);
705 (void)PerlLIO_umask(anum);
708 anum = PerlLIO_umask(POPi);
709 TAINT_PROPER("umask");
712 /* Only DIE if trying to restrict permissions on "user" (self).
713 * Otherwise it's harmless and more useful to just return undef
714 * since 'group' and 'other' concepts probably don't exist here. */
715 if (MAXARG >= 1 && (POPi & 0700))
716 DIE(aTHX_ "umask not implemented");
717 XPUSHs(&PL_sv_undef);
739 if (gv && (io = GvIO(gv))
740 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
743 XPUSHs(SvTIED_obj((SV*)io, mg));
748 call_method("BINMODE", G_SCALAR);
755 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
756 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
757 report_evil_fh(gv, io, PL_op->op_type);
758 SETERRNO(EBADF,RMS_IFI);
763 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
764 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
765 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
766 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
767 mode_from_discipline(discp),
768 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
788 const I32 markoff = MARK - PL_stack_base;
789 const char *methname;
790 int how = PERL_MAGIC_tied;
794 switch(SvTYPE(varsv)) {
796 methname = "TIEHASH";
797 HvEITER_set((HV *)varsv, 0);
800 methname = "TIEARRAY";
803 #ifdef GV_UNIQUE_CHECK
804 if (GvUNIQUE((GV*)varsv)) {
805 Perl_croak(aTHX_ "Attempt to tie unique GV");
808 methname = "TIEHANDLE";
809 how = PERL_MAGIC_tiedscalar;
810 /* For tied filehandles, we apply tiedscalar magic to the IO
811 slot of the GP rather than the GV itself. AMS 20010812 */
813 GvIOp(varsv) = newIO();
814 varsv = (SV *)GvIOp(varsv);
817 methname = "TIESCALAR";
818 how = PERL_MAGIC_tiedscalar;
822 if (sv_isobject(*MARK)) {
824 PUSHSTACKi(PERLSI_MAGIC);
826 EXTEND(SP,(I32)items);
830 call_method(methname, G_SCALAR);
833 /* Not clear why we don't call call_method here too.
834 * perhaps to get different error message ?
836 stash = gv_stashsv(*MARK, FALSE);
837 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
838 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
842 PUSHSTACKi(PERLSI_MAGIC);
844 EXTEND(SP,(I32)items);
848 call_sv((SV*)GvCV(gv), G_SCALAR);
854 if (sv_isobject(sv)) {
855 sv_unmagic(varsv, how);
856 /* Croak if a self-tie on an aggregate is attempted. */
857 if (varsv == SvRV(sv) &&
858 (SvTYPE(varsv) == SVt_PVAV ||
859 SvTYPE(varsv) == SVt_PVHV))
861 "Self-ties of arrays and hashes are not supported");
862 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
865 SP = PL_stack_base + markoff;
875 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
876 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
878 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
881 if ((mg = SvTIED_mg(sv, how))) {
882 SV * const obj = SvRV(SvTIED_obj(sv, mg));
885 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
886 if (gv && isGV(gv) && (cv = GvCV(gv))) {
888 XPUSHs(SvTIED_obj((SV*)gv, mg));
889 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
892 call_sv((SV *)cv, G_VOID);
896 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
897 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
898 "untie attempted while %"UVuf" inner references still exist",
899 (UV)SvREFCNT(obj) - 1 ) ;
903 sv_unmagic(sv, how) ;
912 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
913 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
915 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
918 if ((mg = SvTIED_mg(sv, how))) {
919 SV *osv = SvTIED_obj(sv, mg);
920 if (osv == mg->mg_obj)
921 osv = sv_mortalcopy(osv);
935 HV * const hv = (HV*)POPs;
936 SV * const sv = sv_mortalcopy(&PL_sv_no);
938 sv_setpv(sv, "AnyDBM_File");
939 stash = gv_stashsv(sv, FALSE);
940 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
942 require_pv("AnyDBM_File.pm");
944 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
945 DIE(aTHX_ "No dbm on this machine");
955 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
957 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
960 call_sv((SV*)GvCV(gv), G_SCALAR);
963 if (!sv_isobject(TOPs)) {
968 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
971 call_sv((SV*)GvCV(gv), G_SCALAR);
975 if (sv_isobject(TOPs)) {
976 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
977 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
994 struct timeval timebuf;
995 struct timeval *tbuf = &timebuf;
998 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1003 # if BYTEORDER & 0xf0000
1004 # define ORDERBYTE (0x88888888 - BYTEORDER)
1006 # define ORDERBYTE (0x4444 - BYTEORDER)
1012 for (i = 1; i <= 3; i++) {
1013 SV * const sv = SP[i];
1016 if (SvREADONLY(sv)) {
1018 sv_force_normal_flags(sv, 0);
1019 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1020 DIE(aTHX_ PL_no_modify);
1023 if (ckWARN(WARN_MISC))
1024 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1025 SvPV_force_nolen(sv); /* force string conversion */
1032 /* little endians can use vecs directly */
1033 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1040 masksize = NFDBITS / NBBY;
1042 masksize = sizeof(long); /* documented int, everyone seems to use long */
1044 Zero(&fd_sets[0], 4, char*);
1047 # if SELECT_MIN_BITS == 1
1048 growsize = sizeof(fd_set);
1050 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1051 # undef SELECT_MIN_BITS
1052 # define SELECT_MIN_BITS __FD_SETSIZE
1054 /* If SELECT_MIN_BITS is greater than one we most probably will want
1055 * to align the sizes with SELECT_MIN_BITS/8 because for example
1056 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1057 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1058 * on (sets/tests/clears bits) is 32 bits. */
1059 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1067 timebuf.tv_sec = (long)value;
1068 value -= (NV)timebuf.tv_sec;
1069 timebuf.tv_usec = (long)(value * 1000000.0);
1072 tbuf = Null(struct timeval*);
1074 for (i = 1; i <= 3; i++) {
1076 if (!SvOK(sv) || SvCUR(sv) == 0) {
1083 Sv_Grow(sv, growsize);
1087 while (++j <= growsize) {
1091 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1093 Newx(fd_sets[i], growsize, char);
1094 for (offset = 0; offset < growsize; offset += masksize) {
1095 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1096 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1099 fd_sets[i] = SvPVX(sv);
1103 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1104 /* Can't make just the (void*) conditional because that would be
1105 * cpp #if within cpp macro, and not all compilers like that. */
1106 nfound = PerlSock_select(
1108 (Select_fd_set_t) fd_sets[1],
1109 (Select_fd_set_t) fd_sets[2],
1110 (Select_fd_set_t) fd_sets[3],
1111 (void*) tbuf); /* Workaround for compiler bug. */
1113 nfound = PerlSock_select(
1115 (Select_fd_set_t) fd_sets[1],
1116 (Select_fd_set_t) fd_sets[2],
1117 (Select_fd_set_t) fd_sets[3],
1120 for (i = 1; i <= 3; i++) {
1123 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1125 for (offset = 0; offset < growsize; offset += masksize) {
1126 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1127 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1129 Safefree(fd_sets[i]);
1136 if (GIMME == G_ARRAY && tbuf) {
1137 value = (NV)(timebuf.tv_sec) +
1138 (NV)(timebuf.tv_usec) / 1000000.0;
1139 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1140 sv_setnv(sv, value);
1144 DIE(aTHX_ "select not implemented");
1149 Perl_setdefout(pTHX_ GV *gv)
1152 (void)SvREFCNT_inc(gv);
1154 SvREFCNT_dec(PL_defoutgv);
1164 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1166 egv = GvEGV(PL_defoutgv);
1171 XPUSHs(&PL_sv_undef);
1173 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1174 if (gvp && *gvp == egv) {
1175 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1179 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1184 if (!GvIO(newdefout))
1185 gv_IOadd(newdefout);
1186 setdefout(newdefout);
1197 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1199 if (gv && (io = GvIO(gv))
1200 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1202 const I32 gimme = GIMME_V;
1204 XPUSHs(SvTIED_obj((SV*)io, mg));
1207 call_method("GETC", gimme);
1210 if (gimme == G_SCALAR)
1211 SvSetMagicSV_nosteal(TARG, TOPs);
1214 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1215 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1216 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1217 report_evil_fh(gv, io, PL_op->op_type);
1218 SETERRNO(EBADF,RMS_IFI);
1222 sv_setpvn(TARG, " ", 1);
1223 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1224 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1225 /* Find out how many bytes the char needs */
1226 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1229 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1230 SvCUR_set(TARG,1+len);
1239 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1242 register PERL_CONTEXT *cx;
1243 const I32 gimme = GIMME_V;
1248 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1250 cx->blk_sub.retop = retop;
1252 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1254 setdefout(gv); /* locally select filehandle so $% et al work */
1286 SV * const tmpsv = sv_newmortal();
1288 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1289 name = SvPV_nolen_const(tmpsv);
1291 DIE(aTHX_ "Undefined format \"%s\" called", name);
1293 DIE(aTHX_ "Not a format reference");
1296 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1298 IoFLAGS(io) &= ~IOf_DIDTOP;
1299 return doform(cv,gv,PL_op->op_next);
1305 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1306 register IO * const io = GvIOp(gv);
1307 PerlIO * const ofp = IoOFP(io);
1311 register PERL_CONTEXT *cx;
1313 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1314 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1317 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1318 PL_formtarget != PL_toptarget)
1322 if (!IoTOP_GV(io)) {
1325 if (!IoTOP_NAME(io)) {
1327 if (!IoFMT_NAME(io))
1328 IoFMT_NAME(io) = savepv(GvNAME(gv));
1329 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1330 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1331 if ((topgv && GvFORM(topgv)) ||
1332 !gv_fetchpv("top",FALSE,SVt_PVFM))
1333 IoTOP_NAME(io) = savesvpv(topname);
1335 IoTOP_NAME(io) = savepvn("top", 3);
1337 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1338 if (!topgv || !GvFORM(topgv)) {
1339 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1342 IoTOP_GV(io) = topgv;
1344 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1345 I32 lines = IoLINES_LEFT(io);
1346 const char *s = SvPVX_const(PL_formtarget);
1347 if (lines <= 0) /* Yow, header didn't even fit!!! */
1349 while (lines-- > 0) {
1350 s = strchr(s, '\n');
1356 const STRLEN save = SvCUR(PL_formtarget);
1357 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1358 do_print(PL_formtarget, ofp);
1359 SvCUR_set(PL_formtarget, save);
1360 sv_chop(PL_formtarget, s);
1361 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1364 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1365 do_print(PL_formfeed, ofp);
1366 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1368 PL_formtarget = PL_toptarget;
1369 IoFLAGS(io) |= IOf_DIDTOP;
1372 DIE(aTHX_ "bad top format reference");
1375 SV * const sv = sv_newmortal();
1377 gv_efullname4(sv, fgv, Nullch, FALSE);
1378 name = SvPV_nolen_const(sv);
1380 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1384 DIE(aTHX_ "Undefined top format called");
1387 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1388 return doform(cv,gv,PL_op);
1392 POPBLOCK(cx,PL_curpm);
1398 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1400 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1401 else if (ckWARN(WARN_CLOSED))
1402 report_evil_fh(gv, io, PL_op->op_type);
1407 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1408 if (ckWARN(WARN_IO))
1409 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1411 if (!do_print(PL_formtarget, fp))
1414 FmLINES(PL_formtarget) = 0;
1415 SvCUR_set(PL_formtarget, 0);
1416 *SvEND(PL_formtarget) = '\0';
1417 if (IoFLAGS(io) & IOf_FLUSH)
1418 (void)PerlIO_flush(fp);
1423 PL_formtarget = PL_bodytarget;
1425 PERL_UNUSED_VAR(newsp);
1426 PERL_UNUSED_VAR(gimme);
1427 return cx->blk_sub.retop;
1432 dVAR; dSP; dMARK; dORIGMARK;
1438 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1440 if (gv && (io = GvIO(gv))
1441 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1443 if (MARK == ORIGMARK) {
1446 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1450 *MARK = SvTIED_obj((SV*)io, mg);
1453 call_method("PRINTF", G_SCALAR);
1456 MARK = ORIGMARK + 1;
1463 if (!(io = GvIO(gv))) {
1464 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1465 report_evil_fh(gv, io, PL_op->op_type);
1466 SETERRNO(EBADF,RMS_IFI);
1469 else if (!(fp = IoOFP(io))) {
1470 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1472 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1473 else if (ckWARN(WARN_CLOSED))
1474 report_evil_fh(gv, io, PL_op->op_type);
1476 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1480 do_sprintf(sv, SP - MARK, MARK + 1);
1481 if (!do_print(sv, fp))
1484 if (IoFLAGS(io) & IOf_FLUSH)
1485 if (PerlIO_flush(fp) == EOF)
1496 PUSHs(&PL_sv_undef);
1503 const int perm = (MAXARG > 3) ? POPi : 0666;
1504 const int mode = POPi;
1505 SV * const sv = POPs;
1506 GV * const gv = (GV *)POPs;
1509 /* Need TIEHANDLE method ? */
1510 const char * const tmps = SvPV_const(sv, len);
1511 /* FIXME? do_open should do const */
1512 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1513 IoLINES(GvIOp(gv)) = 0;
1517 PUSHs(&PL_sv_undef);
1524 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1530 Sock_size_t bufsize;
1538 bool charstart = FALSE;
1539 STRLEN charskip = 0;
1542 GV * const gv = (GV*)*++MARK;
1543 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1544 && gv && (io = GvIO(gv)) )
1546 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1550 *MARK = SvTIED_obj((SV*)io, mg);
1552 call_method("READ", G_SCALAR);
1566 sv_setpvn(bufsv, "", 0);
1567 length = SvIVx(*++MARK);
1570 offset = SvIVx(*++MARK);
1574 if (!io || !IoIFP(io)) {
1575 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1576 report_evil_fh(gv, io, PL_op->op_type);
1577 SETERRNO(EBADF,RMS_IFI);
1580 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1581 buffer = SvPVutf8_force(bufsv, blen);
1582 /* UTF-8 may not have been set if they are all low bytes */
1587 buffer = SvPV_force(bufsv, blen);
1588 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1591 DIE(aTHX_ "Negative length");
1599 if (PL_op->op_type == OP_RECV) {
1600 char namebuf[MAXPATHLEN];
1601 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1602 bufsize = sizeof (struct sockaddr_in);
1604 bufsize = sizeof namebuf;
1606 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1610 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1611 /* 'offset' means 'flags' here */
1612 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1613 (struct sockaddr *)namebuf, &bufsize);
1617 /* Bogus return without padding */
1618 bufsize = sizeof (struct sockaddr_in);
1620 SvCUR_set(bufsv, count);
1621 *SvEND(bufsv) = '\0';
1622 (void)SvPOK_only(bufsv);
1626 /* This should not be marked tainted if the fp is marked clean */
1627 if (!(IoFLAGS(io) & IOf_UNTAINT))
1628 SvTAINTED_on(bufsv);
1630 sv_setpvn(TARG, namebuf, bufsize);
1635 if (PL_op->op_type == OP_RECV)
1636 DIE(aTHX_ PL_no_sock_func, "recv");
1638 if (DO_UTF8(bufsv)) {
1639 /* offset adjust in characters not bytes */
1640 blen = sv_len_utf8(bufsv);
1643 if (-offset > (int)blen)
1644 DIE(aTHX_ "Offset outside string");
1647 if (DO_UTF8(bufsv)) {
1648 /* convert offset-as-chars to offset-as-bytes */
1649 if (offset >= (int)blen)
1650 offset += SvCUR(bufsv) - blen;
1652 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1655 bufsize = SvCUR(bufsv);
1656 /* Allocating length + offset + 1 isn't perfect in the case of reading
1657 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1659 (should be 2 * length + offset + 1, or possibly something longer if
1660 PL_encoding is true) */
1661 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1662 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1663 Zero(buffer+bufsize, offset-bufsize, char);
1665 buffer = buffer + offset;
1667 read_target = bufsv;
1669 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1670 concatenate it to the current buffer. */
1672 /* Truncate the existing buffer to the start of where we will be
1674 SvCUR_set(bufsv, offset);
1676 read_target = sv_newmortal();
1677 SvUPGRADE(read_target, SVt_PV);
1678 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1681 if (PL_op->op_type == OP_SYSREAD) {
1682 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1683 if (IoTYPE(io) == IoTYPE_SOCKET) {
1684 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1690 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1695 #ifdef HAS_SOCKET__bad_code_maybe
1696 if (IoTYPE(io) == IoTYPE_SOCKET) {
1697 char namebuf[MAXPATHLEN];
1698 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1699 bufsize = sizeof (struct sockaddr_in);
1701 bufsize = sizeof namebuf;
1703 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1704 (struct sockaddr *)namebuf, &bufsize);
1709 count = PerlIO_read(IoIFP(io), buffer, length);
1710 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1711 if (count == 0 && PerlIO_error(IoIFP(io)))
1715 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1716 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1719 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1720 *SvEND(read_target) = '\0';
1721 (void)SvPOK_only(read_target);
1722 if (fp_utf8 && !IN_BYTES) {
1723 /* Look at utf8 we got back and count the characters */
1724 const char *bend = buffer + count;
1725 while (buffer < bend) {
1727 skip = UTF8SKIP(buffer);
1730 if (buffer - charskip + skip > bend) {
1731 /* partial character - try for rest of it */
1732 length = skip - (bend-buffer);
1733 offset = bend - SvPVX_const(bufsv);
1745 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1746 provided amount read (count) was what was requested (length)
1748 if (got < wanted && count == length) {
1749 length = wanted - got;
1750 offset = bend - SvPVX_const(bufsv);
1753 /* return value is character count */
1757 else if (buffer_utf8) {
1758 /* Let svcatsv upgrade the bytes we read in to utf8.
1759 The buffer is a mortal so will be freed soon. */
1760 sv_catsv_nomg(bufsv, read_target);
1763 /* This should not be marked tainted if the fp is marked clean */
1764 if (!(IoFLAGS(io) & IOf_UNTAINT))
1765 SvTAINTED_on(bufsv);
1777 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1786 const int op_type = PL_op->op_type;
1789 if (PL_op->op_type == OP_SYSWRITE
1790 && gv && (io = GvIO(gv))
1791 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1795 if (MARK == SP - 1) {
1797 sv = sv_2mortal(newSViv(sv_len(*SP)));
1803 *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
1805 call_method("WRITE", G_SCALAR);
1818 if (op_type == OP_SYSWRITE) {
1820 length = (Size_t) sv_len(bufsv);
1822 #if Size_t_size > IVSIZE
1823 length = (Size_t)SvNVx(*++MARK);
1825 length = (Size_t)SvIVx(*++MARK);
1827 if ((SSize_t)length < 0)
1828 DIE(aTHX_ "Negative length");
1833 if (!io || !IoIFP(io)) {
1835 if (ckWARN(WARN_CLOSED))
1836 report_evil_fh(gv, io, PL_op->op_type);
1837 SETERRNO(EBADF,RMS_IFI);
1841 if (PerlIO_isutf8(IoIFP(io))) {
1842 if (!SvUTF8(bufsv)) {
1843 bufsv = sv_2mortal(newSVsv(bufsv));
1844 buffer = sv_2pvutf8(bufsv, &blen);
1846 buffer = SvPV_const(bufsv, blen);
1849 if (DO_UTF8(bufsv)) {
1850 /* Not modifying source SV, so making a temporary copy. */
1851 bufsv = sv_2mortal(newSVsv(bufsv));
1852 sv_utf8_downgrade(bufsv, FALSE);
1854 buffer = SvPV_const(bufsv, blen);
1857 if (op_type == OP_SYSWRITE) {
1859 if (DO_UTF8(bufsv)) {
1860 /* length and offset are in chars */
1861 blen = sv_len_utf8(bufsv);
1864 offset = SvIVx(*++MARK);
1866 if (-offset > (IV)blen)
1867 DIE(aTHX_ "Offset outside string");
1869 } else if (offset >= (IV)blen && blen > 0)
1870 DIE(aTHX_ "Offset outside string");
1873 if (length > blen - offset)
1874 length = blen - offset;
1875 if (DO_UTF8(bufsv)) {
1876 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1877 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1880 buffer = buffer+offset;
1882 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1883 if (IoTYPE(io) == IoTYPE_SOCKET) {
1884 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1890 /* See the note at doio.c:do_print about filesize limits. --jhi */
1891 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1897 const int flags = SvIVx(*++MARK);
1900 char * const sockbuf = SvPVx(*++MARK, mlen);
1901 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1902 flags, (struct sockaddr *)sockbuf, mlen);
1906 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1911 DIE(aTHX_ PL_no_sock_func, "send");
1917 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1918 #if Size_t_size > IVSIZE
1938 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1940 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1942 if (io && !IoIFP(io)) {
1943 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1945 IoFLAGS(io) &= ~IOf_START;
1946 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1947 sv_setpvn(GvSV(gv), "-", 1);
1948 SvSETMAGIC(GvSV(gv));
1950 else if (!nextargv(gv))
1955 gv = PL_last_in_gv; /* eof */
1958 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1960 if (gv && (io = GvIO(gv))
1961 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1964 XPUSHs(SvTIED_obj((SV*)io, mg));
1967 call_method("EOF", G_SCALAR);
1973 PUSHs(boolSV(!gv || do_eof(gv)));
1985 PL_last_in_gv = (GV*)POPs;
1988 if (gv && (io = GvIO(gv))
1989 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1992 XPUSHs(SvTIED_obj((SV*)io, mg));
1995 call_method("TELL", G_SCALAR);
2001 #if LSEEKSIZE > IVSIZE
2002 PUSHn( do_tell(gv) );
2004 PUSHi( do_tell(gv) );
2014 const int whence = POPi;
2015 #if LSEEKSIZE > IVSIZE
2016 Off_t offset = (Off_t)SvNVx(POPs);
2018 Off_t offset = (Off_t)SvIVx(POPs);
2022 gv = PL_last_in_gv = (GV*)POPs;
2024 if (gv && (io = GvIO(gv))
2025 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2028 XPUSHs(SvTIED_obj((SV*)io, mg));
2029 #if LSEEKSIZE > IVSIZE
2030 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2032 XPUSHs(sv_2mortal(newSViv(offset)));
2034 XPUSHs(sv_2mortal(newSViv(whence)));
2037 call_method("SEEK", G_SCALAR);
2043 if (PL_op->op_type == OP_SEEK)
2044 PUSHs(boolSV(do_seek(gv, offset, whence)));
2046 Off_t sought = do_sysseek(gv, offset, whence);
2048 PUSHs(&PL_sv_undef);
2051 #if LSEEKSIZE > IVSIZE
2056 : newSVpvn(zero_but_true, ZBTLEN);
2057 PUSHs(sv_2mortal(sv));
2066 /* There seems to be no consensus on the length type of truncate()
2067 * and ftruncate(), both off_t and size_t have supporters. In
2068 * general one would think that when using large files, off_t is
2069 * at least as wide as size_t, so using an off_t should be okay. */
2070 /* XXX Configure probe for the length type of *truncate() needed XXX */
2073 #if Off_t_size > IVSIZE
2078 /* Checking for length < 0 is problematic as the type might or
2079 * might not be signed: if it is not, clever compilers will moan. */
2080 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2087 if (PL_op->op_flags & OPf_SPECIAL) {
2088 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2097 TAINT_PROPER("truncate");
2098 if (!(fp = IoIFP(io))) {
2104 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2106 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2116 if (SvTYPE(sv) == SVt_PVGV) {
2117 tmpgv = (GV*)sv; /* *main::FRED for example */
2118 goto do_ftruncate_gv;
2120 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2121 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2122 goto do_ftruncate_gv;
2124 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2125 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2126 goto do_ftruncate_io;
2129 name = SvPV_nolen_const(sv);
2130 TAINT_PROPER("truncate");
2132 if (truncate(name, len) < 0)
2138 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2141 if (my_chsize(tmpfd, len) < 0)
2143 PerlLIO_close(tmpfd);
2152 SETERRNO(EBADF,RMS_IFI);
2161 const unsigned int func = POPu;
2162 const int optype = PL_op->op_type;
2166 IO *io = gv ? GvIOn(gv) : 0;
2168 if (!io || !argsv || !IoIFP(io)) {
2169 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2170 report_evil_fh(gv, io, PL_op->op_type);
2171 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2175 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2178 s = SvPV_force(argsv, len);
2179 need = IOCPARM_LEN(func);
2181 s = Sv_Grow(argsv, need + 1);
2182 SvCUR_set(argsv, need);
2185 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2188 retval = SvIV(argsv);
2189 s = INT2PTR(char*,retval); /* ouch */
2192 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2194 if (optype == OP_IOCTL)
2196 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2198 DIE(aTHX_ "ioctl is not implemented");
2202 DIE(aTHX_ "fcntl is not implemented");
2204 #if defined(OS2) && defined(__EMX__)
2205 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2207 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2211 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2213 if (s[SvCUR(argsv)] != 17)
2214 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2216 s[SvCUR(argsv)] = 0; /* put our null back */
2217 SvSETMAGIC(argsv); /* Assume it has changed */
2226 PUSHp(zero_but_true, ZBTLEN);
2247 if (gv && (io = GvIO(gv)))
2254 (void)PerlIO_flush(fp);
2255 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2258 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2259 report_evil_fh(gv, io, PL_op->op_type);
2261 SETERRNO(EBADF,RMS_IFI);
2266 DIE(aTHX_ PL_no_func, "flock()");
2278 int protocol = POPi;
2284 io = gv ? GvIOn(gv) : NULL;
2287 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2288 report_evil_fh(gv, io, PL_op->op_type);
2290 do_close(gv, FALSE);
2291 SETERRNO(EBADF,LIB_INVARG);
2296 do_close(gv, FALSE);
2298 TAINT_PROPER("socket");
2299 fd = PerlSock_socket(domain, type, protocol);
2302 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2303 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2304 IoTYPE(io) = IoTYPE_SOCKET;
2305 if (!IoIFP(io) || !IoOFP(io)) {
2306 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2307 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2308 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2311 #if defined(HAS_FCNTL) && defined(F_SETFD)
2312 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2316 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2321 DIE(aTHX_ PL_no_sock_func, "socket");
2327 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2333 int protocol = POPi;
2340 io1 = gv1 ? GvIOn(gv1) : NULL;
2341 io2 = gv2 ? GvIOn(gv2) : NULL;
2342 if (!gv1 || !gv2 || !io1 || !io2) {
2343 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2345 report_evil_fh(gv1, io1, PL_op->op_type);
2347 report_evil_fh(gv1, io2, PL_op->op_type);
2350 do_close(gv1, FALSE);
2352 do_close(gv2, FALSE);
2357 do_close(gv1, FALSE);
2359 do_close(gv2, FALSE);
2361 TAINT_PROPER("socketpair");
2362 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2364 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2365 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2366 IoTYPE(io1) = IoTYPE_SOCKET;
2367 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2368 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2369 IoTYPE(io2) = IoTYPE_SOCKET;
2370 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2371 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2372 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2373 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2374 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2375 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2376 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2379 #if defined(HAS_FCNTL) && defined(F_SETFD)
2380 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2381 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2386 DIE(aTHX_ PL_no_sock_func, "socketpair");
2394 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2395 extern void GETPRIVMODE();
2396 extern void GETUSERMODE();
2399 /* OK, so on what platform does bind modify addr? */
2402 register IO *io = GvIOn(gv);
2406 int mpeprivmode = 0;
2409 if (!io || !IoIFP(io))
2412 addr = SvPV_const(addrsv, len);
2413 TAINT_PROPER("bind");
2414 #ifdef MPE /* Deal with MPE bind() peculiarities */
2415 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2416 /* The address *MUST* stupidly be zero. */
2417 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2418 /* PRIV mode is required to bind() to ports < 1024. */
2419 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2420 ((struct sockaddr_in *)addr)->sin_port > 0) {
2421 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2426 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2427 (struct sockaddr *)addr, len) >= 0)
2430 #ifdef MPE /* Switch back to USER mode */
2441 if (ckWARN(WARN_CLOSED))
2442 report_evil_fh(gv, io, PL_op->op_type);
2443 SETERRNO(EBADF,SS_IVCHAN);
2446 DIE(aTHX_ PL_no_sock_func, "bind");
2457 register IO *io = GvIOn(gv);
2460 if (!io || !IoIFP(io))
2463 addr = SvPV_const(addrsv, len);
2464 TAINT_PROPER("connect");
2465 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2471 if (ckWARN(WARN_CLOSED))
2472 report_evil_fh(gv, io, PL_op->op_type);
2473 SETERRNO(EBADF,SS_IVCHAN);
2476 DIE(aTHX_ PL_no_sock_func, "connect");
2486 register IO *io = gv ? GvIOn(gv) : NULL;
2488 if (!gv || !io || !IoIFP(io))
2491 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2497 if (ckWARN(WARN_CLOSED))
2498 report_evil_fh(gv, io, PL_op->op_type);
2499 SETERRNO(EBADF,SS_IVCHAN);
2502 DIE(aTHX_ PL_no_sock_func, "listen");
2514 char namebuf[MAXPATHLEN];
2515 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2516 Sock_size_t len = sizeof (struct sockaddr_in);
2518 Sock_size_t len = sizeof namebuf;
2531 if (!gstio || !IoIFP(gstio))
2535 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2539 do_close(ngv, FALSE);
2540 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2541 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2542 IoTYPE(nstio) = IoTYPE_SOCKET;
2543 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2544 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2545 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2546 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2549 #if defined(HAS_FCNTL) && defined(F_SETFD)
2550 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2554 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2555 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2557 #ifdef __SCO_VERSION__
2558 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2561 PUSHp(namebuf, len);
2565 if (ckWARN(WARN_CLOSED))
2566 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2567 SETERRNO(EBADF,SS_IVCHAN);
2573 DIE(aTHX_ PL_no_sock_func, "accept");
2583 register IO *io = GvIOn(gv);
2585 if (!io || !IoIFP(io))
2588 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2592 if (ckWARN(WARN_CLOSED))
2593 report_evil_fh(gv, io, PL_op->op_type);
2594 SETERRNO(EBADF,SS_IVCHAN);
2597 DIE(aTHX_ PL_no_sock_func, "shutdown");
2605 int optype = PL_op->op_type;
2608 unsigned int optname;
2614 if (optype == OP_GSOCKOPT)
2615 sv = sv_2mortal(NEWSV(22, 257));
2618 optname = (unsigned int) POPi;
2619 lvl = (unsigned int) POPi;
2623 if (!io || !IoIFP(io))
2626 fd = PerlIO_fileno(IoIFP(io));
2630 (void)SvPOK_only(sv);
2634 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2641 #if defined(__SYMBIAN32__)
2642 # define SETSOCKOPT_OPTION_VALUE_T void *
2644 # define SETSOCKOPT_OPTION_VALUE_T const char *
2646 /* XXX TODO: We need to have a proper type (a Configure probe,
2647 * etc.) for what the C headers think of the third argument of
2648 * setsockopt(), the option_value read-only buffer: is it
2649 * a "char *", or a "void *", const or not. Some compilers
2650 * don't take kindly to e.g. assuming that "char *" implicitly
2651 * promotes to a "void *", or to explicitly promoting/demoting
2652 * consts to non/vice versa. The "const void *" is the SUS
2653 * definition, but that does not fly everywhere for the above
2655 SETSOCKOPT_OPTION_VALUE_T buf;
2659 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2663 aint = (int)SvIV(sv);
2664 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2667 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2676 if (ckWARN(WARN_CLOSED))
2677 report_evil_fh(gv, io, optype);
2678 SETERRNO(EBADF,SS_IVCHAN);
2683 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2691 int optype = PL_op->op_type;
2695 register IO *io = GvIOn(gv);
2698 if (!io || !IoIFP(io))
2701 sv = sv_2mortal(NEWSV(22, 257));
2702 (void)SvPOK_only(sv);
2706 fd = PerlIO_fileno(IoIFP(io));
2708 case OP_GETSOCKNAME:
2709 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2712 case OP_GETPEERNAME:
2713 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2715 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2717 static const char nowhere[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
2718 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2719 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2720 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2721 sizeof(u_short) + sizeof(struct in_addr))) {
2728 #ifdef BOGUS_GETNAME_RETURN
2729 /* Interactive Unix, getpeername() and getsockname()
2730 does not return valid namelen */
2731 if (len == BOGUS_GETNAME_RETURN)
2732 len = sizeof(struct sockaddr);
2740 if (ckWARN(WARN_CLOSED))
2741 report_evil_fh(gv, io, optype);
2742 SETERRNO(EBADF,SS_IVCHAN);
2747 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2760 if (PL_op->op_flags & OPf_REF) {
2762 if (PL_op->op_type == OP_LSTAT) {
2763 if (gv != PL_defgv) {
2764 do_fstat_warning_check:
2765 if (ckWARN(WARN_IO))
2766 Perl_warner(aTHX_ packWARN(WARN_IO),
2767 "lstat() on filehandle %s", GvENAME(gv));
2768 } else if (PL_laststype != OP_LSTAT)
2769 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2773 if (gv != PL_defgv) {
2774 PL_laststype = OP_STAT;
2776 sv_setpvn(PL_statname, "", 0);
2777 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2778 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2780 if (PL_laststatval < 0) {
2781 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2782 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2788 if (SvTYPE(sv) == SVt_PVGV) {
2792 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2794 if (PL_op->op_type == OP_LSTAT)
2795 goto do_fstat_warning_check;
2798 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2800 PL_laststype = PL_op->op_type;
2801 if (PL_op->op_type == OP_LSTAT)
2802 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2804 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2805 if (PL_laststatval < 0) {
2806 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2807 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2813 if (gimme != G_ARRAY) {
2814 if (gimme != G_VOID)
2815 XPUSHs(boolSV(max));
2821 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2822 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2823 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2824 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2825 #if Uid_t_size > IVSIZE
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2828 # if Uid_t_sign <= 0
2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2831 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2834 #if Gid_t_size > IVSIZE
2835 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2837 # if Gid_t_sign <= 0
2838 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2840 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2843 #ifdef USE_STAT_RDEV
2844 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2846 PUSHs(sv_2mortal(newSVpvn("", 0)));
2848 #if Off_t_size > IVSIZE
2849 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2851 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2854 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2855 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2856 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2858 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2859 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2860 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2862 #ifdef USE_STAT_BLOCKS
2863 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2864 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2866 PUSHs(sv_2mortal(newSVpvn("", 0)));
2867 PUSHs(sv_2mortal(newSVpvn("", 0)));
2873 /* This macro is used by the stacked filetest operators :
2874 * if the previous filetest failed, short-circuit and pass its value.
2875 * Else, discard it from the stack and continue. --rgs
2877 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2878 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2879 else { (void)POPs; PUTBACK; } \
2885 /* Not const, because things tweak this below. Not bool, because there's
2886 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2887 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2888 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2889 /* Giving some sort of initial value silences compilers. */
2891 int access_mode = R_OK;
2893 int access_mode = 0;
2896 /* access_mode is never used, but leaving use_access in makes the
2897 conditional compiling below much clearer. */
2900 int stat_mode = S_IRUSR;
2902 bool effective = FALSE;
2905 STACKED_FTEST_CHECK;
2907 switch (PL_op->op_type) {
2909 #if !(defined(HAS_ACCESS) && defined(R_OK))
2915 #if defined(HAS_ACCESS) && defined(W_OK)
2920 stat_mode = S_IWUSR;
2924 #if defined(HAS_ACCESS) && defined(X_OK)
2929 stat_mode = S_IXUSR;
2933 #ifdef PERL_EFF_ACCESS
2936 stat_mode = S_IWUSR;
2940 #ifndef PERL_EFF_ACCESS
2948 #ifdef PERL_EFF_ACCESS
2953 stat_mode = S_IXUSR;
2959 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2960 const char *const name = POPpx;
2962 # ifdef PERL_EFF_ACCESS
2963 result = PERL_EFF_ACCESS(name, access_mode);
2965 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
2971 result = access(name, access_mode);
2973 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
2988 if (cando(stat_mode, effective, &PL_statcache))
2996 const int op_type = PL_op->op_type;
2998 STACKED_FTEST_CHECK;
3003 if (op_type == OP_FTIS)
3006 /* You can't dTARGET inside OP_FTIS, because you'll get
3007 "panic: pad_sv po" - the op is not flagged to have a target. */
3011 #if Off_t_size > IVSIZE
3012 PUSHn(PL_statcache.st_size);
3014 PUSHi(PL_statcache.st_size);
3018 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3021 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3024 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3036 /* I believe that all these three are likely to be defined on most every
3037 system these days. */
3039 if(PL_op->op_type == OP_FTSUID)
3043 if(PL_op->op_type == OP_FTSGID)
3047 if(PL_op->op_type == OP_FTSVTX)
3051 STACKED_FTEST_CHECK;
3056 switch (PL_op->op_type) {
3058 if (PL_statcache.st_uid == PL_uid)
3062 if (PL_statcache.st_uid == PL_euid)
3066 if (PL_statcache.st_size == 0)
3070 if (S_ISSOCK(PL_statcache.st_mode))
3074 if (S_ISCHR(PL_statcache.st_mode))
3078 if (S_ISBLK(PL_statcache.st_mode))
3082 if (S_ISREG(PL_statcache.st_mode))
3086 if (S_ISDIR(PL_statcache.st_mode))
3090 if (S_ISFIFO(PL_statcache.st_mode))
3095 if (PL_statcache.st_mode & S_ISUID)
3101 if (PL_statcache.st_mode & S_ISGID)
3107 if (PL_statcache.st_mode & S_ISVTX)
3117 I32 result = my_lstat();
3121 if (S_ISLNK(PL_statcache.st_mode))
3133 STACKED_FTEST_CHECK;
3135 if (PL_op->op_flags & OPf_REF)
3137 else if (isGV(TOPs))
3139 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3140 gv = (GV*)SvRV(POPs);
3142 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3144 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3145 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3146 else if (tmpsv && SvOK(tmpsv)) {
3147 const char *tmps = SvPV_nolen_const(tmpsv);
3155 if (PerlLIO_isatty(fd))
3160 #if defined(atarist) /* this will work with atariST. Configure will
3161 make guesses for other systems. */
3162 # define FILE_base(f) ((f)->_base)
3163 # define FILE_ptr(f) ((f)->_ptr)
3164 # define FILE_cnt(f) ((f)->_cnt)
3165 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3175 register STDCHAR *s;
3181 STACKED_FTEST_CHECK;
3183 if (PL_op->op_flags & OPf_REF)
3185 else if (isGV(TOPs))
3187 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3188 gv = (GV*)SvRV(POPs);
3194 if (gv == PL_defgv) {
3196 io = GvIO(PL_statgv);
3199 goto really_filename;
3204 PL_laststatval = -1;
3205 sv_setpvn(PL_statname, "", 0);
3206 io = GvIO(PL_statgv);
3208 if (io && IoIFP(io)) {
3209 if (! PerlIO_has_base(IoIFP(io)))
3210 DIE(aTHX_ "-T and -B not implemented on filehandles");
3211 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3212 if (PL_laststatval < 0)
3214 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3215 if (PL_op->op_type == OP_FTTEXT)
3220 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3221 i = PerlIO_getc(IoIFP(io));
3223 (void)PerlIO_ungetc(IoIFP(io),i);
3225 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3227 len = PerlIO_get_bufsiz(IoIFP(io));
3228 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3229 /* sfio can have large buffers - limit to 512 */
3234 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3236 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3238 SETERRNO(EBADF,RMS_IFI);
3246 PL_laststype = OP_STAT;
3247 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3248 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3249 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3251 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3254 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3255 if (PL_laststatval < 0) {
3256 (void)PerlIO_close(fp);
3259 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3260 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3261 (void)PerlIO_close(fp);
3263 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3264 RETPUSHNO; /* special case NFS directories */
3265 RETPUSHYES; /* null file is anything */
3270 /* now scan s to look for textiness */
3271 /* XXX ASCII dependent code */
3273 #if defined(DOSISH) || defined(USEMYBINMODE)
3274 /* ignore trailing ^Z on short files */
3275 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3279 for (i = 0; i < len; i++, s++) {
3280 if (!*s) { /* null never allowed in text */
3285 else if (!(isPRINT(*s) || isSPACE(*s)))
3288 else if (*s & 128) {
3290 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3293 /* utf8 characters don't count as odd */
3294 if (UTF8_IS_START(*s)) {
3295 int ulen = UTF8SKIP(s);
3296 if (ulen < len - i) {
3298 for (j = 1; j < ulen; j++) {
3299 if (!UTF8_IS_CONTINUATION(s[j]))
3302 --ulen; /* loop does extra increment */
3312 *s != '\n' && *s != '\r' && *s != '\b' &&
3313 *s != '\t' && *s != '\f' && *s != 27)
3318 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3329 const char *tmps = 0;
3333 SV * const sv = POPs;
3334 if (SvTYPE(sv) == SVt_PVGV) {
3337 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3341 tmps = SvPVx_nolen_const(sv);
3345 if( !gv && (!tmps || !*tmps) ) {
3346 HV * const table = GvHVn(PL_envgv);
3349 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3350 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
3352 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
3357 deprecate("chdir('') or chdir(undef) as chdir()");
3358 tmps = SvPV_nolen_const(*svp);
3362 TAINT_PROPER("chdir");
3367 TAINT_PROPER("chdir");
3370 IO* const io = GvIO(gv);
3373 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3375 else if (IoDIRP(io)) {
3377 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3379 DIE(aTHX_ PL_no_func, "dirfd");
3390 DIE(aTHX_ PL_no_func, "fchdir");
3394 PUSHi( PerlDir_chdir(tmps) >= 0 );
3396 /* Clear the DEFAULT element of ENV so we'll get the new value
3398 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3405 dSP; dMARK; dTARGET;
3406 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3418 TAINT_PROPER("chroot");
3419 PUSHi( chroot(tmps) >= 0 );
3422 DIE(aTHX_ PL_no_func, "chroot");
3430 const char *tmps2 = POPpconstx;
3431 const char *tmps = SvPV_nolen_const(TOPs);
3432 TAINT_PROPER("rename");
3434 anum = PerlLIO_rename(tmps, tmps2);
3436 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3437 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3440 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3441 (void)UNLINK(tmps2);
3442 if (!(anum = link(tmps, tmps2)))
3443 anum = UNLINK(tmps);
3451 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3455 const int op_type = PL_op->op_type;
3459 if (op_type == OP_LINK)
3460 DIE(aTHX_ PL_no_func, "link");
3462 # ifndef HAS_SYMLINK
3463 if (op_type == OP_SYMLINK)
3464 DIE(aTHX_ PL_no_func, "symlink");
3468 const char *tmps2 = POPpconstx;
3469 const char *tmps = SvPV_nolen_const(TOPs);
3470 TAINT_PROPER(PL_op_desc[op_type]);
3472 # if defined(HAS_LINK)
3473 # if defined(HAS_SYMLINK)
3474 /* Both present - need to choose which. */
3475 (op_type == OP_LINK) ?
3476 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3478 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3479 PerlLIO_link(tmps, tmps2);
3482 # if defined(HAS_SYMLINK)
3483 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3484 symlink(tmps, tmps2);
3489 SETi( result >= 0 );
3496 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3506 char buf[MAXPATHLEN];
3509 #ifndef INCOMPLETE_TAINTS
3513 len = readlink(tmps, buf, sizeof(buf) - 1);
3521 RETSETUNDEF; /* just pretend it's a normal file */
3525 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3527 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3529 char * const save_filename = filename;
3535 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3536 strcpy(cmdline, cmd);
3537 strcat(cmdline, " ");
3538 for (s = cmdline + strlen(cmdline); *filename; ) {
3543 myfp = PerlProc_popen(cmdline, "r");
3547 SV *tmpsv = sv_newmortal();
3548 /* Need to save/restore 'PL_rs' ?? */
3549 s = sv_gets(tmpsv, myfp, 0);
3550 (void)PerlProc_pclose(myfp);
3554 #ifdef HAS_SYS_ERRLIST
3559 /* you don't see this */
3561 #ifdef HAS_SYS_ERRLIST
3569 if (instr(s, errmsg)) {
3576 #define EACCES EPERM
3578 if (instr(s, "cannot make"))
3579 SETERRNO(EEXIST,RMS_FEX);
3580 else if (instr(s, "existing file"))
3581 SETERRNO(EEXIST,RMS_FEX);
3582 else if (instr(s, "ile exists"))
3583 SETERRNO(EEXIST,RMS_FEX);
3584 else if (instr(s, "non-exist"))
3585 SETERRNO(ENOENT,RMS_FNF);
3586 else if (instr(s, "does not exist"))
3587 SETERRNO(ENOENT,RMS_FNF);
3588 else if (instr(s, "not empty"))
3589 SETERRNO(EBUSY,SS_DEVOFFLINE);
3590 else if (instr(s, "cannot access"))
3591 SETERRNO(EACCES,RMS_PRV);
3593 SETERRNO(EPERM,RMS_PRV);
3596 else { /* some mkdirs return no failure indication */
3597 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3598 if (PL_op->op_type == OP_RMDIR)
3603 SETERRNO(EACCES,RMS_PRV); /* a guess */
3612 /* This macro removes trailing slashes from a directory name.
3613 * Different operating and file systems take differently to
3614 * trailing slashes. According to POSIX 1003.1 1996 Edition
3615 * any number of trailing slashes should be allowed.
3616 * Thusly we snip them away so that even non-conforming
3617 * systems are happy.
3618 * We should probably do this "filtering" for all
3619 * the functions that expect (potentially) directory names:
3620 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3621 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3623 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3624 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3627 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3628 (tmps) = savepvn((tmps), (len)); \
3648 TRIMSLASHES(tmps,len,copy);
3650 TAINT_PROPER("mkdir");
3652 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3654 SETi( dooneliner("mkdir", tmps) );
3655 oldumask = PerlLIO_umask(0);
3656 PerlLIO_umask(oldumask);
3657 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3671 TRIMSLASHES(tmps,len,copy);
3672 TAINT_PROPER("rmdir");
3674 SETi( PerlDir_rmdir(tmps) >= 0 );
3676 SETi( dooneliner("rmdir", tmps) );
3683 /* Directory calls. */
3687 #if defined(Direntry_t) && defined(HAS_READDIR)
3689 const char *dirname = POPpconstx;
3691 register IO *io = GvIOn(gv);
3697 PerlDir_close(IoDIRP(io));
3698 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3704 SETERRNO(EBADF,RMS_DIR);
3707 DIE(aTHX_ PL_no_dir_func, "opendir");
3713 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3714 DIE(aTHX_ PL_no_dir_func, "readdir");
3716 #if !defined(I_DIRENT) && !defined(VMS)
3717 Direntry_t *readdir (DIR *);
3722 const I32 gimme = GIMME;
3723 GV *gv = (GV *)POPs;
3724 register Direntry_t *dp;
3725 register IO *io = GvIOn(gv);
3727 if (!io || !IoDIRP(io))
3731 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3735 sv = newSVpvn(dp->d_name, dp->d_namlen);
3737 sv = newSVpv(dp->d_name, 0);
3739 #ifndef INCOMPLETE_TAINTS
3740 if (!(IoFLAGS(io) & IOf_UNTAINT))
3743 XPUSHs(sv_2mortal(sv));
3745 while (gimme == G_ARRAY);
3747 if (!dp && gimme != G_ARRAY)
3754 SETERRNO(EBADF,RMS_ISI);
3755 if (GIMME == G_ARRAY)
3764 #if defined(HAS_TELLDIR) || defined(telldir)
3766 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3767 /* XXX netbsd still seemed to.
3768 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3769 --JHI 1999-Feb-02 */
3770 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3771 long telldir (DIR *);
3774 register IO *io = GvIOn(gv);
3776 if (!io || !IoDIRP(io))
3779 PUSHi( PerlDir_tell(IoDIRP(io)) );
3783 SETERRNO(EBADF,RMS_ISI);
3786 DIE(aTHX_ PL_no_dir_func, "telldir");
3792 #if defined(HAS_SEEKDIR) || defined(seekdir)
3796 register IO *io = GvIOn(gv);
3798 if (!io || !IoDIRP(io))
3801 (void)PerlDir_seek(IoDIRP(io), along);
3806 SETERRNO(EBADF,RMS_ISI);
3809 DIE(aTHX_ PL_no_dir_func, "seekdir");
3815 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3818 register IO *io = GvIOn(gv);
3820 if (!io || !IoDIRP(io))
3823 (void)PerlDir_rewind(IoDIRP(io));
3827 SETERRNO(EBADF,RMS_ISI);
3830 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3836 #if defined(Direntry_t) && defined(HAS_READDIR)
3839 register IO *io = GvIOn(gv);
3841 if (!io || !IoDIRP(io))
3844 #ifdef VOID_CLOSEDIR
3845 PerlDir_close(IoDIRP(io));
3847 if (PerlDir_close(IoDIRP(io)) < 0) {
3848 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3857 SETERRNO(EBADF,RMS_IFI);
3860 DIE(aTHX_ PL_no_dir_func, "closedir");
3864 /* Process control. */
3874 PERL_FLUSHALL_FOR_CHILD;
3875 childpid = PerlProc_fork();
3879 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3880 SvREADONLY_off(GvSV(tmpgv));
3881 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3882 SvREADONLY_on(GvSV(tmpgv));
3884 #ifdef THREADS_HAVE_PIDS
3885 PL_ppid = (IV)getppid();
3887 #ifdef PERL_USES_PL_PIDSTATUS
3888 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3894 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3899 PERL_FLUSHALL_FOR_CHILD;
3900 childpid = PerlProc_fork();
3906 DIE(aTHX_ PL_no_func, "fork");
3913 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3918 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3919 childpid = wait4pid(-1, &argflags, 0);
3921 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3926 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3927 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3928 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
3930 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
3935 DIE(aTHX_ PL_no_func, "wait");
3941 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3950 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3951 result = wait4pid(pid, &argflags, optype);
3953 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
3958 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3959 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3960 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
3962 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
3967 DIE(aTHX_ PL_no_func, "waitpid");
3973 dSP; dMARK; dORIGMARK; dTARGET;
3979 while (++MARK <= SP) {
3980 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
3985 TAINT_PROPER("system");
3987 PERL_FLUSHALL_FOR_CHILD;
3988 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
3994 if (PerlProc_pipe(pp) >= 0)
3996 while ((childpid = PerlProc_fork()) == -1) {
3997 if (errno != EAGAIN) {
4002 PerlLIO_close(pp[0]);
4003 PerlLIO_close(pp[1]);
4010 Sigsave_t ihand,qhand; /* place to save signals during system() */
4014 PerlLIO_close(pp[1]);
4016 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4017 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4020 result = wait4pid(childpid, &status, 0);
4021 } while (result == -1 && errno == EINTR);
4023 (void)rsignal_restore(SIGINT, &ihand);
4024 (void)rsignal_restore(SIGQUIT, &qhand);
4026 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4027 do_execfree(); /* free any memory child malloced on fork */
4033 while (n < sizeof(int)) {
4034 n1 = PerlLIO_read(pp[0],
4035 (void*)(((char*)&errkid)+n),
4041 PerlLIO_close(pp[0]);
4042 if (n) { /* Error */
4043 if (n != sizeof(int))
4044 DIE(aTHX_ "panic: kid popen errno read");
4045 errno = errkid; /* Propagate errno from kid */
4046 STATUS_NATIVE_CHILD_SET(-1);
4049 XPUSHi(STATUS_CURRENT);
4053 PerlLIO_close(pp[0]);
4054 #if defined(HAS_FCNTL) && defined(F_SETFD)
4055 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4058 if (PL_op->op_flags & OPf_STACKED) {
4059 SV *really = *++MARK;
4060 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4062 else if (SP - MARK != 1)
4063 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4065 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4069 #else /* ! FORK or VMS or OS/2 */
4072 if (PL_op->op_flags & OPf_STACKED) {
4073 SV *really = *++MARK;
4074 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4075 value = (I32)do_aspawn(really, MARK, SP);
4077 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4080 else if (SP - MARK != 1) {
4081 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4082 value = (I32)do_aspawn(Nullsv, MARK, SP);
4084 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4088 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4090 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4092 STATUS_NATIVE_CHILD_SET(value);
4095 XPUSHi(result ? value : STATUS_CURRENT);
4096 #endif /* !FORK or VMS */
4102 dSP; dMARK; dORIGMARK; dTARGET;
4107 while (++MARK <= SP) {
4108 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4113 TAINT_PROPER("exec");
4115 PERL_FLUSHALL_FOR_CHILD;
4116 if (PL_op->op_flags & OPf_STACKED) {
4117 SV *really = *++MARK;
4118 value = (I32)do_aexec(really, MARK, SP);
4120 else if (SP - MARK != 1)
4122 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4126 (void ) do_aspawn(Nullsv, MARK, SP);
4130 value = (I32)do_aexec(Nullsv, MARK, SP);
4135 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4138 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4141 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4155 # ifdef THREADS_HAVE_PIDS
4156 if (PL_ppid != 1 && getppid() == 1)
4157 /* maybe the parent process has died. Refresh ppid cache */
4161 XPUSHi( getppid() );
4165 DIE(aTHX_ PL_no_func, "getppid");
4181 pgrp = (I32)BSD_GETPGRP(pid);
4183 if (pid != 0 && pid != PerlProc_getpid())
4184 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4190 DIE(aTHX_ PL_no_func, "getpgrp()");
4209 TAINT_PROPER("setpgrp");
4211 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4213 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4214 || (pid != 0 && pid != PerlProc_getpid()))
4216 DIE(aTHX_ "setpgrp can't take arguments");
4218 SETi( setpgrp() >= 0 );
4219 #endif /* USE_BSDPGRP */
4222 DIE(aTHX_ PL_no_func, "setpgrp()");
4228 #ifdef HAS_GETPRIORITY
4232 SETi( getpriority(which, who) );
4235 DIE(aTHX_ PL_no_func, "getpriority()");
4241 #ifdef HAS_SETPRIORITY
4246 TAINT_PROPER("setpriority");
4247 SETi( setpriority(which, who, niceval) >= 0 );
4250 DIE(aTHX_ PL_no_func, "setpriority()");
4260 XPUSHn( time(Null(Time_t*)) );
4262 XPUSHi( time(Null(Time_t*)) );
4273 (void)PerlProc_times(&PL_timesbuf);
4275 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4276 /* struct tms, though same data */
4280 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4281 if (GIMME == G_ARRAY) {
4282 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4283 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4284 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4290 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4292 if (GIMME == G_ARRAY) {
4293 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4294 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4295 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4299 DIE(aTHX_ "times not implemented");
4301 #endif /* HAS_TIMES */
4304 #ifdef LOCALTIME_EDGECASE_BROKEN
4305 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4310 /* No workarounds in the valid range */
4311 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4312 return (localtime (tp));
4314 /* This edge case is to workaround the undefined behaviour, where the
4315 * TIMEZONE makes the time go beyond the defined range.
4316 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4317 * If there is a negative offset in TZ, like MET-1METDST, some broken
4318 * implementations of localtime () (like AIX 5.2) barf with bogus
4320 * 0x7fffffff gmtime 2038-01-19 03:14:07
4321 * 0x7fffffff localtime 1901-12-13 21:45:51
4322 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4323 * 0x3c19137f gmtime 2001-12-13 20:45:51
4324 * 0x3c19137f localtime 2001-12-13 21:45:51
4325 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4326 * Given that legal timezones are typically between GMT-12 and GMT+12
4327 * we turn back the clock 23 hours before calling the localtime
4328 * function, and add those to the return value. This will never cause
4329 * day wrapping problems, since the edge case is Tue Jan *19*
4331 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4334 if (P->tm_hour >= 24) {
4336 P->tm_mday++; /* 18 -> 19 */
4337 P->tm_wday++; /* Mon -> Tue */
4338 P->tm_yday++; /* 18 -> 19 */
4341 } /* S_my_localtime */
4348 const struct tm *tmbuf;
4349 static const char * const dayname[] =
4350 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4351 static const char * const monname[] =
4352 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4353 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4359 when = (Time_t)SvNVx(POPs);
4361 when = (Time_t)SvIVx(POPs);
4364 if (PL_op->op_type == OP_LOCALTIME)
4365 #ifdef LOCALTIME_EDGECASE_BROKEN
4366 tmbuf = S_my_localtime(aTHX_ &when);
4368 tmbuf = localtime(&when);
4371 tmbuf = gmtime(&when);
4373 if (GIMME != G_ARRAY) {
4379 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4380 dayname[tmbuf->tm_wday],
4381 monname[tmbuf->tm_mon],
4386 tmbuf->tm_year + 1900);
4387 PUSHs(sv_2mortal(tsv));
4392 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4393 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4394 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4395 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4396 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4397 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4398 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4399 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4400 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4411 anum = alarm((unsigned int)anum);
4418 DIE(aTHX_ PL_no_func, "alarm");
4429 (void)time(&lasttime);
4434 PerlProc_sleep((unsigned int)duration);
4437 XPUSHi(when - lasttime);
4441 /* Shared memory. */
4442 /* Merged with some message passing. */
4446 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4447 dSP; dMARK; dTARGET;
4448 const int op_type = PL_op->op_type;
4453 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4456 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4459 value = (I32)(do_semop(MARK, SP) >= 0);
4462 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4478 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4479 dSP; dMARK; dTARGET;
4480 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4487 DIE(aTHX_ "System V IPC is not implemented on this machine");
4493 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4494 dSP; dMARK; dTARGET;
4495 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4503 PUSHp(zero_but_true, ZBTLEN);
4511 /* Get system info. */
4515 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4517 I32 which = PL_op->op_type;
4518 register char **elem;
4520 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4521 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4522 struct hostent *gethostbyname(Netdb_name_t);
4523 struct hostent *gethostent(void);
4525 struct hostent *hent;
4529 if (which == OP_GHBYNAME) {
4530 #ifdef HAS_GETHOSTBYNAME
4531 char* name = POPpbytex;
4532 hent = PerlSock_gethostbyname(name);
4534 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4537 else if (which == OP_GHBYADDR) {
4538 #ifdef HAS_GETHOSTBYADDR
4539 int addrtype = POPi;
4542 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4544 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4546 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4550 #ifdef HAS_GETHOSTENT
4551 hent = PerlSock_gethostent();
4553 DIE(aTHX_ PL_no_sock_func, "gethostent");
4556 #ifdef HOST_NOT_FOUND
4558 #ifdef USE_REENTRANT_API
4559 # ifdef USE_GETHOSTENT_ERRNO
4560 h_errno = PL_reentrant_buffer->_gethostent_errno;
4563 STATUS_UNIX_SET(h_errno);
4567 if (GIMME != G_ARRAY) {
4568 PUSHs(sv = sv_newmortal());
4570 if (which == OP_GHBYNAME) {
4572 sv_setpvn(sv, hent->h_addr, hent->h_length);
4575 sv_setpv(sv, (char*)hent->h_name);
4581 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4582 sv_setpv(sv, (char*)hent->h_name);
4583 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4584 for (elem = hent->h_aliases; elem && *elem; elem++) {
4585 sv_catpv(sv, *elem);
4587 sv_catpvn(sv, " ", 1);
4589 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4590 sv_setiv(sv, (IV)hent->h_addrtype);
4591 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4592 len = hent->h_length;
4593 sv_setiv(sv, (IV)len);
4595 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4596 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4597 sv_setpvn(sv, *elem, len);
4600 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4602 sv_setpvn(sv, hent->h_addr, len);
4607 DIE(aTHX_ PL_no_sock_func, "gethostent");
4613 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4615 I32 which = PL_op->op_type;
4616 register char **elem;
4618 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4619 struct netent *getnetbyaddr(Netdb_net_t, int);
4620 struct netent *getnetbyname(Netdb_name_t);
4621 struct netent *getnetent(void);
4623 struct netent *nent;
4625 if (which == OP_GNBYNAME){
4626 #ifdef HAS_GETNETBYNAME
4627 char *name = POPpbytex;
4628 nent = PerlSock_getnetbyname(name);
4630 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4633 else if (which == OP_GNBYADDR) {
4634 #ifdef HAS_GETNETBYADDR
4635 int addrtype = POPi;
4636 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4637 nent = PerlSock_getnetbyaddr(addr, addrtype);
4639 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4643 #ifdef HAS_GETNETENT
4644 nent = PerlSock_getnetent();
4646 DIE(aTHX_ PL_no_sock_func, "getnetent");
4649 #ifdef HOST_NOT_FOUND
4651 #ifdef USE_REENTRANT_API
4652 # ifdef USE_GETNETENT_ERRNO
4653 h_errno = PL_reentrant_buffer->_getnetent_errno;
4656 STATUS_UNIX_SET(h_errno);
4661 if (GIMME != G_ARRAY) {
4662 PUSHs(sv = sv_newmortal());
4664 if (which == OP_GNBYNAME)
4665 sv_setiv(sv, (IV)nent->n_net);
4667 sv_setpv(sv, nent->n_name);
4673 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4674 sv_setpv(sv, nent->n_name);
4675 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4676 for (elem = nent->n_aliases; elem && *elem; elem++) {
4677 sv_catpv(sv, *elem);
4679 sv_catpvn(sv, " ", 1);
4681 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4682 sv_setiv(sv, (IV)nent->n_addrtype);
4683 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4684 sv_setiv(sv, (IV)nent->n_net);
4689 DIE(aTHX_ PL_no_sock_func, "getnetent");
4695 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4697 I32 which = PL_op->op_type;
4698 register char **elem;
4700 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4701 struct protoent *getprotobyname(Netdb_name_t);
4702 struct protoent *getprotobynumber(int);
4703 struct protoent *getprotoent(void);
4705 struct protoent *pent;
4707 if (which == OP_GPBYNAME) {
4708 #ifdef HAS_GETPROTOBYNAME
4709 char* name = POPpbytex;
4710 pent = PerlSock_getprotobyname(name);
4712 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4715 else if (which == OP_GPBYNUMBER) {
4716 #ifdef HAS_GETPROTOBYNUMBER
4718 pent = PerlSock_getprotobynumber(number);
4720 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4724 #ifdef HAS_GETPROTOENT
4725 pent = PerlSock_getprotoent();
4727 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4731 if (GIMME != G_ARRAY) {
4732 PUSHs(sv = sv_newmortal());
4734 if (which == OP_GPBYNAME)
4735 sv_setiv(sv, (IV)pent->p_proto);
4737 sv_setpv(sv, pent->p_name);
4743 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4744 sv_setpv(sv, pent->p_name);
4745 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4746 for (elem = pent->p_aliases; elem && *elem; elem++) {
4747 sv_catpv(sv, *elem);
4749 sv_catpvn(sv, " ", 1);
4751 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4752 sv_setiv(sv, (IV)pent->p_proto);
4757 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4763 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4765 I32 which = PL_op->op_type;
4766 register char **elem;
4768 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4769 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4770 struct servent *getservbyport(int, Netdb_name_t);
4771 struct servent *getservent(void);
4773 struct servent *sent;
4775 if (which == OP_GSBYNAME) {
4776 #ifdef HAS_GETSERVBYNAME
4777 char *proto = POPpbytex;
4778 char *name = POPpbytex;
4780 if (proto && !*proto)
4783 sent = PerlSock_getservbyname(name, proto);
4785 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4788 else if (which == OP_GSBYPORT) {
4789 #ifdef HAS_GETSERVBYPORT
4790 char *proto = POPpbytex;
4791 unsigned short port = (unsigned short)POPu;
4793 if (proto && !*proto)
4797 port = PerlSock_htons(port);
4799 sent = PerlSock_getservbyport(port, proto);
4801 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4805 #ifdef HAS_GETSERVENT
4806 sent = PerlSock_getservent();
4808 DIE(aTHX_ PL_no_sock_func, "getservent");
4812 if (GIMME != G_ARRAY) {
4813 PUSHs(sv = sv_newmortal());
4815 if (which == OP_GSBYNAME) {
4817 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4819 sv_setiv(sv, (IV)(sent->s_port));
4823 sv_setpv(sv, sent->s_name);
4829 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4830 sv_setpv(sv, sent->s_name);
4831 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4832 for (elem = sent->s_aliases; elem && *elem; elem++) {
4833 sv_catpv(sv, *elem);
4835 sv_catpvn(sv, " ", 1);
4837 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4839 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4841 sv_setiv(sv, (IV)(sent->s_port));
4843 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4844 sv_setpv(sv, sent->s_proto);
4849 DIE(aTHX_ PL_no_sock_func, "getservent");
4855 #ifdef HAS_SETHOSTENT
4857 PerlSock_sethostent(TOPi);
4860 DIE(aTHX_ PL_no_sock_func, "sethostent");
4866 #ifdef HAS_SETNETENT
4868 PerlSock_setnetent(TOPi);
4871 DIE(aTHX_ PL_no_sock_func, "setnetent");
4877 #ifdef HAS_SETPROTOENT
4879 PerlSock_setprotoent(TOPi);
4882 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4888 #ifdef HAS_SETSERVENT
4890 PerlSock_setservent(TOPi);
4893 DIE(aTHX_ PL_no_sock_func, "setservent");
4899 #ifdef HAS_ENDHOSTENT
4901 PerlSock_endhostent();
4905 DIE(aTHX_ PL_no_sock_func, "endhostent");
4911 #ifdef HAS_ENDNETENT
4913 PerlSock_endnetent();
4917 DIE(aTHX_ PL_no_sock_func, "endnetent");
4923 #ifdef HAS_ENDPROTOENT
4925 PerlSock_endprotoent();
4929 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4935 #ifdef HAS_ENDSERVENT
4937 PerlSock_endservent();
4941 DIE(aTHX_ PL_no_sock_func, "endservent");
4949 I32 which = PL_op->op_type;
4951 struct passwd *pwent = NULL;
4953 * We currently support only the SysV getsp* shadow password interface.
4954 * The interface is declared in <shadow.h> and often one needs to link
4955 * with -lsecurity or some such.
4956 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
4959 * AIX getpwnam() is clever enough to return the encrypted password
4960 * only if the caller (euid?) is root.
4962 * There are at least three other shadow password APIs. Many platforms
4963 * seem to contain more than one interface for accessing the shadow
4964 * password databases, possibly for compatibility reasons.
4965 * The getsp*() is by far he simplest one, the other two interfaces
4966 * are much more complicated, but also very similar to each other.
4971 * struct pr_passwd *getprpw*();
4972 * The password is in
4973 * char getprpw*(...).ufld.fd_encrypt[]
4974 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
4979 * struct es_passwd *getespw*();
4980 * The password is in
4981 * char *(getespw*(...).ufld.fd_encrypt)
4982 * Mention HAS_GETESPWNAM here so that Configure probes for it.
4985 * struct userpw *getuserpw();
4986 * The password is in
4987 * char *(getuserpw(...)).spw_upw_passwd
4988 * (but the de facto standard getpwnam() should work okay)
4990 * Mention I_PROT here so that Configure probes for it.
4992 * In HP-UX for getprpw*() the manual page claims that one should include
4993 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
4994 * if one includes <shadow.h> as that includes <hpsecurity.h>,
4995 * and pp_sys.c already includes <shadow.h> if there is such.
4997 * Note that <sys/security.h> is already probed for, but currently
4998 * it is only included in special cases.
5000 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5001 * be preferred interface, even though also the getprpw*() interface
5002 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5003 * One also needs to call set_auth_parameters() in main() before
5004 * doing anything else, whether one is using getespw*() or getprpw*().
5006 * Note that accessing the shadow databases can be magnitudes
5007 * slower than accessing the standard databases.
5012 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5013 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5014 * the pw_comment is left uninitialized. */
5015 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5021 char* name = POPpbytex;
5022 pwent = getpwnam(name);
5028 pwent = getpwuid(uid);
5032 # ifdef HAS_GETPWENT
5034 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5035 if (pwent) pwent = getpwnam(pwent->pw_name);
5038 DIE(aTHX_ PL_no_func, "getpwent");
5044 if (GIMME != G_ARRAY) {
5045 PUSHs(sv = sv_newmortal());
5047 if (which == OP_GPWNAM)
5048 # if Uid_t_sign <= 0
5049 sv_setiv(sv, (IV)pwent->pw_uid);
5051 sv_setuv(sv, (UV)pwent->pw_uid);
5054 sv_setpv(sv, pwent->pw_name);
5060 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5061 sv_setpv(sv, pwent->pw_name);
5063 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5065 /* If we have getspnam(), we try to dig up the shadow
5066 * password. If we are underprivileged, the shadow
5067 * interface will set the errno to EACCES or similar,
5068 * and return a null pointer. If this happens, we will
5069 * use the dummy password (usually "*" or "x") from the
5070 * standard password database.
5072 * In theory we could skip the shadow call completely
5073 * if euid != 0 but in practice we cannot know which
5074 * security measures are guarding the shadow databases
5075 * on a random platform.
5077 * Resist the urge to use additional shadow interfaces.
5078 * Divert the urge to writing an extension instead.
5081 /* Some AIX setups falsely(?) detect some getspnam(), which
5082 * has a different API than the Solaris/IRIX one. */
5083 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5085 struct spwd *spwent;
5086 int saverrno; /* Save and restore errno so that
5087 * underprivileged attempts seem
5088 * to have never made the unsccessful
5089 * attempt to retrieve the shadow password. */
5092 spwent = getspnam(pwent->pw_name);
5094 if (spwent && spwent->sp_pwdp)
5095 sv_setpv(sv, spwent->sp_pwdp);
5099 if (!SvPOK(sv)) /* Use the standard password, then. */
5100 sv_setpv(sv, pwent->pw_passwd);
5103 # ifndef INCOMPLETE_TAINTS
5104 /* passwd is tainted because user himself can diddle with it.
5105 * admittedly not much and in a very limited way, but nevertheless. */
5109 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5110 # if Uid_t_sign <= 0
5111 sv_setiv(sv, (IV)pwent->pw_uid);
5113 sv_setuv(sv, (UV)pwent->pw_uid);
5116 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5117 # if Uid_t_sign <= 0
5118 sv_setiv(sv, (IV)pwent->pw_gid);
5120 sv_setuv(sv, (UV)pwent->pw_gid);
5122 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5123 * because of the poor interface of the Perl getpw*(),
5124 * not because there's some standard/convention saying so.
5125 * A better interface would have been to return a hash,
5126 * but we are accursed by our history, alas. --jhi. */
5127 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5129 sv_setiv(sv, (IV)pwent->pw_change);
5132 sv_setiv(sv, (IV)pwent->pw_quota);
5135 sv_setpv(sv, pwent->pw_age);
5140 /* pw_class and pw_comment are mutually exclusive--.
5141 * see the above note for pw_change, pw_quota, and pw_age. */
5142 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5144 sv_setpv(sv, pwent->pw_class);
5147 sv_setpv(sv, pwent->pw_comment);
5151 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5153 sv_setpv(sv, pwent->pw_gecos);
5155 # ifndef INCOMPLETE_TAINTS
5156 /* pw_gecos is tainted because user himself can diddle with it. */
5160 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5161 sv_setpv(sv, pwent->pw_dir);
5163 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5164 sv_setpv(sv, pwent->pw_shell);
5165 # ifndef INCOMPLETE_TAINTS
5166 /* pw_shell is tainted because user himself can diddle with it. */
5171 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5172 sv_setiv(sv, (IV)pwent->pw_expire);
5177 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5183 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5188 DIE(aTHX_ PL_no_func, "setpwent");
5194 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5199 DIE(aTHX_ PL_no_func, "endpwent");
5207 I32 which = PL_op->op_type;
5208 register char **elem;
5210 struct group *grent;
5212 if (which == OP_GGRNAM) {
5213 char* name = POPpbytex;
5214 grent = (struct group *)getgrnam(name);
5216 else if (which == OP_GGRGID) {
5218 grent = (struct group *)getgrgid(gid);
5222 grent = (struct group *)getgrent();
5224 DIE(aTHX_ PL_no_func, "getgrent");
5228 if (GIMME != G_ARRAY) {
5229 PUSHs(sv = sv_newmortal());
5231 if (which == OP_GGRNAM)
5232 sv_setiv(sv, (IV)grent->gr_gid);
5234 sv_setpv(sv, grent->gr_name);
5240 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5241 sv_setpv(sv, grent->gr_name);
5243 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5245 sv_setpv(sv, grent->gr_passwd);
5248 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5249 sv_setiv(sv, (IV)grent->gr_gid);
5251 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5252 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5253 /* In UNICOS/mk (_CRAYMPP) the multithreading
5254 * versions (getgrnam_r, getgrgid_r)
5255 * seem to return an illegal pointer
5256 * as the group members list, gr_mem.
5257 * getgrent() doesn't even have a _r version
5258 * but the gr_mem is poisonous anyway.
5259 * So yes, you cannot get the list of group
5260 * members if building multithreaded in UNICOS/mk. */
5261 for (elem = grent->gr_mem; elem && *elem; elem++) {
5262 sv_catpv(sv, *elem);
5264 sv_catpvn(sv, " ", 1);
5271 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5277 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5282 DIE(aTHX_ PL_no_func, "setgrent");
5288 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5293 DIE(aTHX_ PL_no_func, "endgrent");
5303 if (!(tmps = PerlProc_getlogin()))
5305 PUSHp(tmps, strlen(tmps));
5308 DIE(aTHX_ PL_no_func, "getlogin");
5312 /* Miscellaneous. */
5317 dSP; dMARK; dORIGMARK; dTARGET;
5318 register I32 items = SP - MARK;
5319 unsigned long a[20];
5324 while (++MARK <= SP) {
5325 if (SvTAINTED(*MARK)) {
5331 TAINT_PROPER("syscall");
5334 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5335 * or where sizeof(long) != sizeof(char*). But such machines will
5336 * not likely have syscall implemented either, so who cares?
5338 while (++MARK <= SP) {
5339 if (SvNIOK(*MARK) || !i)
5340 a[i++] = SvIV(*MARK);
5341 else if (*MARK == &PL_sv_undef)
5344 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5350 DIE(aTHX_ "Too many args to syscall");
5352 DIE(aTHX_ "Too few args to syscall");
5354 retval = syscall(a[0]);
5357 retval = syscall(a[0],a[1]);
5360 retval = syscall(a[0],a[1],a[2]);
5363 retval = syscall(a[0],a[1],a[2],a[3]);
5366 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5369 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5372 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5375 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5379 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5382 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5385 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5389 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5393 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5397 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5398 a[10],a[11],a[12],a[13]);
5400 #endif /* atarist */
5406 DIE(aTHX_ PL_no_func, "syscall");
5410 #ifdef FCNTL_EMULATE_FLOCK
5412 /* XXX Emulate flock() with fcntl().
5413 What's really needed is a good file locking module.
5417 fcntl_emulate_flock(int fd, int operation)
5421 switch (operation & ~LOCK_NB) {
5423 flock.l_type = F_RDLCK;
5426 flock.l_type = F_WRLCK;
5429 flock.l_type = F_UNLCK;
5435 flock.l_whence = SEEK_SET;
5436 flock.l_start = flock.l_len = (Off_t)0;
5438 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5441 #endif /* FCNTL_EMULATE_FLOCK */
5443 #ifdef LOCKF_EMULATE_FLOCK
5445 /* XXX Emulate flock() with lockf(). This is just to increase
5446 portability of scripts. The calls are not completely
5447 interchangeable. What's really needed is a good file
5451 /* The lockf() constants might have been defined in <unistd.h>.
5452 Unfortunately, <unistd.h> causes troubles on some mixed
5453 (BSD/POSIX) systems, such as SunOS 4.1.3.
5455 Further, the lockf() constants aren't POSIX, so they might not be
5456 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5457 just stick in the SVID values and be done with it. Sigh.
5461 # define F_ULOCK 0 /* Unlock a previously locked region */
5464 # define F_LOCK 1 /* Lock a region for exclusive use */
5467 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5470 # define F_TEST 3 /* Test a region for other processes locks */
5474 lockf_emulate_flock(int fd, int operation)
5480 /* flock locks entire file so for lockf we need to do the same */
5482 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5483 if (pos > 0) /* is seekable and needs to be repositioned */
5484 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5485 pos = -1; /* seek failed, so don't seek back afterwards */
5488 switch (operation) {
5490 /* LOCK_SH - get a shared lock */
5492 /* LOCK_EX - get an exclusive lock */
5494 i = lockf (fd, F_LOCK, 0);
5497 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5498 case LOCK_SH|LOCK_NB:
5499 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5500 case LOCK_EX|LOCK_NB:
5501 i = lockf (fd, F_TLOCK, 0);
5503 if ((errno == EAGAIN) || (errno == EACCES))
5504 errno = EWOULDBLOCK;
5507 /* LOCK_UN - unlock (non-blocking is a no-op) */
5509 case LOCK_UN|LOCK_NB:
5510 i = lockf (fd, F_ULOCK, 0);
5513 /* Default - can't decipher operation */
5520 if (pos > 0) /* need to restore position of the handle */
5521 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5526 #endif /* LOCKF_EMULATE_FLOCK */
5530 * c-indentation-style: bsd
5532 * indent-tabs-mode: t
5535 * ex: set ts=8 sts=4 sw=4 noet: