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();
433 if (SP - MARK != 1) {
435 do_join(TARG, &PL_sv_no, MARK, SP);
442 tmps = SvPV_const(tmpsv, len);
443 if ((!tmps || !len) && PL_errgv) {
444 SV * const error = ERRSV;
445 SvUPGRADE(error, SVt_PV);
446 if (SvPOK(error) && SvCUR(error))
447 sv_catpv(error, "\t...caught");
449 tmps = SvPV_const(tmpsv, len);
452 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
454 Perl_warn(aTHX_ "%"SVf, tmpsv);
466 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
468 if (SP - MARK != 1) {
470 do_join(TARG, &PL_sv_no, MARK, SP);
472 tmps = SvPV_const(tmpsv, len);
478 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
482 SvUPGRADE(error, SVt_PV);
483 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
485 SvSetSV(error,tmpsv);
486 else if (sv_isobject(error)) {
487 HV *stash = SvSTASH(SvRV(error));
488 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
490 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
491 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
498 call_sv((SV*)GvCV(gv),
499 G_SCALAR|G_EVAL|G_KEEPERR);
500 sv_setsv(error,*PL_stack_sp--);
506 if (SvPOK(error) && SvCUR(error))
507 sv_catpv(error, "\t...propagated");
510 tmps = SvPV_const(tmpsv, len);
516 tmpsv = sv_2mortal(newSVpvn("Died", 4));
518 DIE(aTHX_ "%"SVf, tmpsv);
534 GV * const gv = (GV *)*++MARK;
537 DIE(aTHX_ PL_no_usym, "filehandle");
538 if ((io = GvIOp(gv)))
539 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
542 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
544 /* Method's args are same as ours ... */
545 /* ... except handle is replaced by the object */
546 *MARK-- = SvTIED_obj((SV*)io, mg);
550 call_method("OPEN", G_SCALAR);
564 tmps = SvPV_const(sv, len);
565 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
568 PUSHi( (I32)PL_forkprocess );
569 else if (PL_forkprocess == 0) /* we are a new child */
581 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
583 if (gv && (io = GvIO(gv))
584 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
587 XPUSHs(SvTIED_obj((SV*)io, mg));
590 call_method("CLOSE", G_SCALAR);
596 PUSHs(boolSV(do_close(gv, TRUE)));
608 GV * const wgv = (GV*)POPs;
609 GV * const rgv = (GV*)POPs;
614 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
615 DIE(aTHX_ PL_no_usym, "filehandle");
620 do_close(rgv, FALSE);
622 do_close(wgv, FALSE);
624 if (PerlProc_pipe(fd) < 0)
627 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
628 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
629 IoOFP(rstio) = IoIFP(rstio);
630 IoIFP(wstio) = IoOFP(wstio);
631 IoTYPE(rstio) = IoTYPE_RDONLY;
632 IoTYPE(wstio) = IoTYPE_WRONLY;
634 if (!IoIFP(rstio) || !IoOFP(wstio)) {
635 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
636 else PerlLIO_close(fd[0]);
637 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
638 else PerlLIO_close(fd[1]);
641 #if defined(HAS_FCNTL) && defined(F_SETFD)
642 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
643 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
650 DIE(aTHX_ PL_no_func, "pipe");
666 if (gv && (io = GvIO(gv))
667 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
670 XPUSHs(SvTIED_obj((SV*)io, mg));
673 call_method("FILENO", G_SCALAR);
679 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
680 /* Can't do this because people seem to do things like
681 defined(fileno($foo)) to check whether $foo is a valid fh.
682 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
683 report_evil_fh(gv, io, PL_op->op_type);
688 PUSHi(PerlIO_fileno(fp));
700 anum = PerlLIO_umask(0);
701 (void)PerlLIO_umask(anum);
704 anum = PerlLIO_umask(POPi);
705 TAINT_PROPER("umask");
708 /* Only DIE if trying to restrict permissions on "user" (self).
709 * Otherwise it's harmless and more useful to just return undef
710 * since 'group' and 'other' concepts probably don't exist here. */
711 if (MAXARG >= 1 && (POPi & 0700))
712 DIE(aTHX_ "umask not implemented");
713 XPUSHs(&PL_sv_undef);
735 if (gv && (io = GvIO(gv))
736 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
739 XPUSHs(SvTIED_obj((SV*)io, mg));
744 call_method("BINMODE", G_SCALAR);
751 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
752 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
753 report_evil_fh(gv, io, PL_op->op_type);
754 SETERRNO(EBADF,RMS_IFI);
759 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
760 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
761 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
762 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
763 mode_from_discipline(discp),
764 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
784 const I32 markoff = MARK - PL_stack_base;
785 const char *methname;
786 int how = PERL_MAGIC_tied;
790 switch(SvTYPE(varsv)) {
792 methname = "TIEHASH";
793 HvEITER_set((HV *)varsv, 0);
796 methname = "TIEARRAY";
799 #ifdef GV_UNIQUE_CHECK
800 if (GvUNIQUE((GV*)varsv)) {
801 Perl_croak(aTHX_ "Attempt to tie unique GV");
804 methname = "TIEHANDLE";
805 how = PERL_MAGIC_tiedscalar;
806 /* For tied filehandles, we apply tiedscalar magic to the IO
807 slot of the GP rather than the GV itself. AMS 20010812 */
809 GvIOp(varsv) = newIO();
810 varsv = (SV *)GvIOp(varsv);
813 methname = "TIESCALAR";
814 how = PERL_MAGIC_tiedscalar;
818 if (sv_isobject(*MARK)) {
820 PUSHSTACKi(PERLSI_MAGIC);
822 EXTEND(SP,(I32)items);
826 call_method(methname, G_SCALAR);
829 /* Not clear why we don't call call_method here too.
830 * perhaps to get different error message ?
832 stash = gv_stashsv(*MARK, FALSE);
833 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
834 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
838 PUSHSTACKi(PERLSI_MAGIC);
840 EXTEND(SP,(I32)items);
844 call_sv((SV*)GvCV(gv), G_SCALAR);
850 if (sv_isobject(sv)) {
851 sv_unmagic(varsv, how);
852 /* Croak if a self-tie on an aggregate is attempted. */
853 if (varsv == SvRV(sv) &&
854 (SvTYPE(varsv) == SVt_PVAV ||
855 SvTYPE(varsv) == SVt_PVHV))
857 "Self-ties of arrays and hashes are not supported");
858 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
861 SP = PL_stack_base + markoff;
871 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
872 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
874 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
877 if ((mg = SvTIED_mg(sv, how))) {
878 SV * const obj = SvRV(SvTIED_obj(sv, mg));
881 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
882 if (gv && isGV(gv) && (cv = GvCV(gv))) {
884 XPUSHs(SvTIED_obj((SV*)gv, mg));
885 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
888 call_sv((SV *)cv, G_VOID);
892 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
893 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
894 "untie attempted while %"UVuf" inner references still exist",
895 (UV)SvREFCNT(obj) - 1 ) ;
899 sv_unmagic(sv, how) ;
908 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
909 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
911 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
914 if ((mg = SvTIED_mg(sv, how))) {
915 SV *osv = SvTIED_obj(sv, mg);
916 if (osv == mg->mg_obj)
917 osv = sv_mortalcopy(osv);
931 HV * const hv = (HV*)POPs;
932 SV * const sv = sv_mortalcopy(&PL_sv_no);
934 sv_setpv(sv, "AnyDBM_File");
935 stash = gv_stashsv(sv, FALSE);
936 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
938 require_pv("AnyDBM_File.pm");
940 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
941 DIE(aTHX_ "No dbm on this machine");
951 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
953 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
956 call_sv((SV*)GvCV(gv), G_SCALAR);
959 if (!sv_isobject(TOPs)) {
964 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
967 call_sv((SV*)GvCV(gv), G_SCALAR);
971 if (sv_isobject(TOPs)) {
972 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
973 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
990 struct timeval timebuf;
991 struct timeval *tbuf = &timebuf;
994 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
999 # if BYTEORDER & 0xf0000
1000 # define ORDERBYTE (0x88888888 - BYTEORDER)
1002 # define ORDERBYTE (0x4444 - BYTEORDER)
1008 for (i = 1; i <= 3; i++) {
1009 SV * const sv = SP[i];
1012 if (SvREADONLY(sv)) {
1014 sv_force_normal_flags(sv, 0);
1015 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1016 DIE(aTHX_ PL_no_modify);
1019 if (ckWARN(WARN_MISC))
1020 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1021 SvPV_force_nolen(sv); /* force string conversion */
1028 /* little endians can use vecs directly */
1029 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1036 masksize = NFDBITS / NBBY;
1038 masksize = sizeof(long); /* documented int, everyone seems to use long */
1040 Zero(&fd_sets[0], 4, char*);
1043 # if SELECT_MIN_BITS == 1
1044 growsize = sizeof(fd_set);
1046 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1047 # undef SELECT_MIN_BITS
1048 # define SELECT_MIN_BITS __FD_SETSIZE
1050 /* If SELECT_MIN_BITS is greater than one we most probably will want
1051 * to align the sizes with SELECT_MIN_BITS/8 because for example
1052 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1053 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1054 * on (sets/tests/clears bits) is 32 bits. */
1055 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1063 timebuf.tv_sec = (long)value;
1064 value -= (NV)timebuf.tv_sec;
1065 timebuf.tv_usec = (long)(value * 1000000.0);
1068 tbuf = Null(struct timeval*);
1070 for (i = 1; i <= 3; i++) {
1072 if (!SvOK(sv) || SvCUR(sv) == 0) {
1079 Sv_Grow(sv, growsize);
1083 while (++j <= growsize) {
1087 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1089 Newx(fd_sets[i], growsize, char);
1090 for (offset = 0; offset < growsize; offset += masksize) {
1091 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1092 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1095 fd_sets[i] = SvPVX(sv);
1099 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1100 /* Can't make just the (void*) conditional because that would be
1101 * cpp #if within cpp macro, and not all compilers like that. */
1102 nfound = PerlSock_select(
1104 (Select_fd_set_t) fd_sets[1],
1105 (Select_fd_set_t) fd_sets[2],
1106 (Select_fd_set_t) fd_sets[3],
1107 (void*) tbuf); /* Workaround for compiler bug. */
1109 nfound = PerlSock_select(
1111 (Select_fd_set_t) fd_sets[1],
1112 (Select_fd_set_t) fd_sets[2],
1113 (Select_fd_set_t) fd_sets[3],
1116 for (i = 1; i <= 3; i++) {
1119 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1121 for (offset = 0; offset < growsize; offset += masksize) {
1122 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1123 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1125 Safefree(fd_sets[i]);
1132 if (GIMME == G_ARRAY && tbuf) {
1133 value = (NV)(timebuf.tv_sec) +
1134 (NV)(timebuf.tv_usec) / 1000000.0;
1135 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1136 sv_setnv(sv, value);
1140 DIE(aTHX_ "select not implemented");
1145 Perl_setdefout(pTHX_ GV *gv)
1148 (void)SvREFCNT_inc(gv);
1150 SvREFCNT_dec(PL_defoutgv);
1160 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1162 egv = GvEGV(PL_defoutgv);
1167 XPUSHs(&PL_sv_undef);
1169 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1170 if (gvp && *gvp == egv) {
1171 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1175 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1180 if (!GvIO(newdefout))
1181 gv_IOadd(newdefout);
1182 setdefout(newdefout);
1193 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1195 if (gv && (io = GvIO(gv))
1196 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1198 const I32 gimme = GIMME_V;
1200 XPUSHs(SvTIED_obj((SV*)io, mg));
1203 call_method("GETC", gimme);
1206 if (gimme == G_SCALAR)
1207 SvSetMagicSV_nosteal(TARG, TOPs);
1210 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1211 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1212 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1213 report_evil_fh(gv, io, PL_op->op_type);
1214 SETERRNO(EBADF,RMS_IFI);
1218 sv_setpvn(TARG, " ", 1);
1219 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1220 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1221 /* Find out how many bytes the char needs */
1222 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1225 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1226 SvCUR_set(TARG,1+len);
1235 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1238 register PERL_CONTEXT *cx;
1239 const I32 gimme = GIMME_V;
1244 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1246 cx->blk_sub.retop = retop;
1248 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1250 setdefout(gv); /* locally select filehandle so $% et al work */
1282 SV * const tmpsv = sv_newmortal();
1284 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1285 name = SvPV_nolen_const(tmpsv);
1287 DIE(aTHX_ "Undefined format \"%s\" called", name);
1289 DIE(aTHX_ "Not a format reference");
1292 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1294 IoFLAGS(io) &= ~IOf_DIDTOP;
1295 return doform(cv,gv,PL_op->op_next);
1301 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1302 register IO * const io = GvIOp(gv);
1303 PerlIO * const ofp = IoOFP(io);
1307 register PERL_CONTEXT *cx;
1309 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1310 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1313 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1314 PL_formtarget != PL_toptarget)
1318 if (!IoTOP_GV(io)) {
1321 if (!IoTOP_NAME(io)) {
1323 if (!IoFMT_NAME(io))
1324 IoFMT_NAME(io) = savepv(GvNAME(gv));
1325 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1326 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1327 if ((topgv && GvFORM(topgv)) ||
1328 !gv_fetchpv("top",FALSE,SVt_PVFM))
1329 IoTOP_NAME(io) = savesvpv(topname);
1331 IoTOP_NAME(io) = savepvn("top", 3);
1333 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1334 if (!topgv || !GvFORM(topgv)) {
1335 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1338 IoTOP_GV(io) = topgv;
1340 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1341 I32 lines = IoLINES_LEFT(io);
1342 const char *s = SvPVX_const(PL_formtarget);
1343 if (lines <= 0) /* Yow, header didn't even fit!!! */
1345 while (lines-- > 0) {
1346 s = strchr(s, '\n');
1352 const STRLEN save = SvCUR(PL_formtarget);
1353 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1354 do_print(PL_formtarget, ofp);
1355 SvCUR_set(PL_formtarget, save);
1356 sv_chop(PL_formtarget, s);
1357 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1360 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1361 do_print(PL_formfeed, ofp);
1362 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1364 PL_formtarget = PL_toptarget;
1365 IoFLAGS(io) |= IOf_DIDTOP;
1368 DIE(aTHX_ "bad top format reference");
1371 SV * const sv = sv_newmortal();
1373 gv_efullname4(sv, fgv, Nullch, FALSE);
1374 name = SvPV_nolen_const(sv);
1376 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1380 DIE(aTHX_ "Undefined top format called");
1383 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1384 return doform(cv,gv,PL_op);
1388 POPBLOCK(cx,PL_curpm);
1394 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1396 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1397 else if (ckWARN(WARN_CLOSED))
1398 report_evil_fh(gv, io, PL_op->op_type);
1403 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1404 if (ckWARN(WARN_IO))
1405 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1407 if (!do_print(PL_formtarget, fp))
1410 FmLINES(PL_formtarget) = 0;
1411 SvCUR_set(PL_formtarget, 0);
1412 *SvEND(PL_formtarget) = '\0';
1413 if (IoFLAGS(io) & IOf_FLUSH)
1414 (void)PerlIO_flush(fp);
1419 PL_formtarget = PL_bodytarget;
1421 PERL_UNUSED_VAR(newsp);
1422 PERL_UNUSED_VAR(gimme);
1423 return cx->blk_sub.retop;
1428 dVAR; dSP; dMARK; dORIGMARK;
1434 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1436 if (gv && (io = GvIO(gv))
1437 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1439 if (MARK == ORIGMARK) {
1442 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1446 *MARK = SvTIED_obj((SV*)io, mg);
1449 call_method("PRINTF", G_SCALAR);
1452 MARK = ORIGMARK + 1;
1459 if (!(io = GvIO(gv))) {
1460 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1461 report_evil_fh(gv, io, PL_op->op_type);
1462 SETERRNO(EBADF,RMS_IFI);
1465 else if (!(fp = IoOFP(io))) {
1466 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1468 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1469 else if (ckWARN(WARN_CLOSED))
1470 report_evil_fh(gv, io, PL_op->op_type);
1472 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1476 do_sprintf(sv, SP - MARK, MARK + 1);
1477 if (!do_print(sv, fp))
1480 if (IoFLAGS(io) & IOf_FLUSH)
1481 if (PerlIO_flush(fp) == EOF)
1492 PUSHs(&PL_sv_undef);
1499 const int perm = (MAXARG > 3) ? POPi : 0666;
1500 const int mode = POPi;
1501 SV * const sv = POPs;
1502 GV * const gv = (GV *)POPs;
1505 /* Need TIEHANDLE method ? */
1506 const char * const tmps = SvPV_const(sv, len);
1507 /* FIXME? do_open should do const */
1508 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1509 IoLINES(GvIOp(gv)) = 0;
1513 PUSHs(&PL_sv_undef);
1520 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1526 Sock_size_t bufsize;
1534 bool charstart = FALSE;
1535 STRLEN charskip = 0;
1538 GV * const gv = (GV*)*++MARK;
1539 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1540 && gv && (io = GvIO(gv)) )
1542 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1546 *MARK = SvTIED_obj((SV*)io, mg);
1548 call_method("READ", G_SCALAR);
1562 sv_setpvn(bufsv, "", 0);
1563 length = SvIVx(*++MARK);
1566 offset = SvIVx(*++MARK);
1570 if (!io || !IoIFP(io)) {
1571 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1572 report_evil_fh(gv, io, PL_op->op_type);
1573 SETERRNO(EBADF,RMS_IFI);
1576 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1577 buffer = SvPVutf8_force(bufsv, blen);
1578 /* UTF-8 may not have been set if they are all low bytes */
1583 buffer = SvPV_force(bufsv, blen);
1584 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1587 DIE(aTHX_ "Negative length");
1595 if (PL_op->op_type == OP_RECV) {
1596 char namebuf[MAXPATHLEN];
1597 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1598 bufsize = sizeof (struct sockaddr_in);
1600 bufsize = sizeof namebuf;
1602 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1606 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1607 /* 'offset' means 'flags' here */
1608 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1609 (struct sockaddr *)namebuf, &bufsize);
1613 /* Bogus return without padding */
1614 bufsize = sizeof (struct sockaddr_in);
1616 SvCUR_set(bufsv, count);
1617 *SvEND(bufsv) = '\0';
1618 (void)SvPOK_only(bufsv);
1622 /* This should not be marked tainted if the fp is marked clean */
1623 if (!(IoFLAGS(io) & IOf_UNTAINT))
1624 SvTAINTED_on(bufsv);
1626 sv_setpvn(TARG, namebuf, bufsize);
1631 if (PL_op->op_type == OP_RECV)
1632 DIE(aTHX_ PL_no_sock_func, "recv");
1634 if (DO_UTF8(bufsv)) {
1635 /* offset adjust in characters not bytes */
1636 blen = sv_len_utf8(bufsv);
1639 if (-offset > (int)blen)
1640 DIE(aTHX_ "Offset outside string");
1643 if (DO_UTF8(bufsv)) {
1644 /* convert offset-as-chars to offset-as-bytes */
1645 if (offset >= (int)blen)
1646 offset += SvCUR(bufsv) - blen;
1648 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1651 bufsize = SvCUR(bufsv);
1652 /* Allocating length + offset + 1 isn't perfect in the case of reading
1653 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1655 (should be 2 * length + offset + 1, or possibly something longer if
1656 PL_encoding is true) */
1657 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1658 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1659 Zero(buffer+bufsize, offset-bufsize, char);
1661 buffer = buffer + offset;
1663 read_target = bufsv;
1665 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1666 concatenate it to the current buffer. */
1668 /* Truncate the existing buffer to the start of where we will be
1670 SvCUR_set(bufsv, offset);
1672 read_target = sv_newmortal();
1673 SvUPGRADE(read_target, SVt_PV);
1674 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1677 if (PL_op->op_type == OP_SYSREAD) {
1678 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1679 if (IoTYPE(io) == IoTYPE_SOCKET) {
1680 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1686 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1691 #ifdef HAS_SOCKET__bad_code_maybe
1692 if (IoTYPE(io) == IoTYPE_SOCKET) {
1693 char namebuf[MAXPATHLEN];
1694 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1695 bufsize = sizeof (struct sockaddr_in);
1697 bufsize = sizeof namebuf;
1699 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1700 (struct sockaddr *)namebuf, &bufsize);
1705 count = PerlIO_read(IoIFP(io), buffer, length);
1706 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1707 if (count == 0 && PerlIO_error(IoIFP(io)))
1711 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1712 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1715 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1716 *SvEND(read_target) = '\0';
1717 (void)SvPOK_only(read_target);
1718 if (fp_utf8 && !IN_BYTES) {
1719 /* Look at utf8 we got back and count the characters */
1720 const char *bend = buffer + count;
1721 while (buffer < bend) {
1723 skip = UTF8SKIP(buffer);
1726 if (buffer - charskip + skip > bend) {
1727 /* partial character - try for rest of it */
1728 length = skip - (bend-buffer);
1729 offset = bend - SvPVX_const(bufsv);
1741 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1742 provided amount read (count) was what was requested (length)
1744 if (got < wanted && count == length) {
1745 length = wanted - got;
1746 offset = bend - SvPVX_const(bufsv);
1749 /* return value is character count */
1753 else if (buffer_utf8) {
1754 /* Let svcatsv upgrade the bytes we read in to utf8.
1755 The buffer is a mortal so will be freed soon. */
1756 sv_catsv_nomg(bufsv, read_target);
1759 /* This should not be marked tainted if the fp is marked clean */
1760 if (!(IoFLAGS(io) & IOf_UNTAINT))
1761 SvTAINTED_on(bufsv);
1774 const int items = (SP - PL_stack_base) - TOPMARK;
1778 sv = sv_2mortal(newSViv(sv_len(*SP)));
1787 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1798 if (PL_op->op_type == OP_SYSWRITE
1799 && gv && (io = GvIO(gv))
1800 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1805 *MARK = SvTIED_obj((SV*)io, mg);
1807 call_method("WRITE", G_SCALAR);
1818 #if Size_t_size > IVSIZE
1819 length = (Size_t)SvNVx(*++MARK);
1821 length = (Size_t)SvIVx(*++MARK);
1823 if ((SSize_t)length < 0)
1824 DIE(aTHX_ "Negative length");
1827 if (!io || !IoIFP(io)) {
1829 if (ckWARN(WARN_CLOSED))
1830 report_evil_fh(gv, io, PL_op->op_type);
1831 SETERRNO(EBADF,RMS_IFI);
1835 if (PerlIO_isutf8(IoIFP(io))) {
1836 if (!SvUTF8(bufsv)) {
1837 bufsv = sv_2mortal(newSVsv(bufsv));
1838 buffer = sv_2pvutf8(bufsv, &blen);
1840 buffer = SvPV_const(bufsv, blen);
1843 if (DO_UTF8(bufsv)) {
1844 /* Not modifying source SV, so making a temporary copy. */
1845 bufsv = sv_2mortal(newSVsv(bufsv));
1846 sv_utf8_downgrade(bufsv, FALSE);
1848 buffer = SvPV_const(bufsv, blen);
1851 if (PL_op->op_type == OP_SYSWRITE) {
1853 if (DO_UTF8(bufsv)) {
1854 /* length and offset are in chars */
1855 blen = sv_len_utf8(bufsv);
1858 offset = SvIVx(*++MARK);
1860 if (-offset > (IV)blen)
1861 DIE(aTHX_ "Offset outside string");
1863 } else if (offset >= (IV)blen && blen > 0)
1864 DIE(aTHX_ "Offset outside string");
1867 if (length > blen - offset)
1868 length = blen - offset;
1869 if (DO_UTF8(bufsv)) {
1870 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1871 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1874 buffer = buffer+offset;
1876 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1877 if (IoTYPE(io) == IoTYPE_SOCKET) {
1878 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1884 /* See the note at doio.c:do_print about filesize limits. --jhi */
1885 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1890 else if (SP > MARK) {
1892 char * const sockbuf = SvPVx(*++MARK, mlen);
1893 /* length is really flags */
1894 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1895 length, (struct sockaddr *)sockbuf, mlen);
1898 /* length is really flags */
1899 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1902 DIE(aTHX_ PL_no_sock_func, "send");
1908 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1909 #if Size_t_size > IVSIZE
1929 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1931 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1933 if (io && !IoIFP(io)) {
1934 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1936 IoFLAGS(io) &= ~IOf_START;
1937 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1938 sv_setpvn(GvSV(gv), "-", 1);
1939 SvSETMAGIC(GvSV(gv));
1941 else if (!nextargv(gv))
1946 gv = PL_last_in_gv; /* eof */
1949 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1951 if (gv && (io = GvIO(gv))
1952 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1955 XPUSHs(SvTIED_obj((SV*)io, mg));
1958 call_method("EOF", G_SCALAR);
1964 PUSHs(boolSV(!gv || do_eof(gv)));
1976 PL_last_in_gv = (GV*)POPs;
1979 if (gv && (io = GvIO(gv))
1980 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1983 XPUSHs(SvTIED_obj((SV*)io, mg));
1986 call_method("TELL", G_SCALAR);
1992 #if LSEEKSIZE > IVSIZE
1993 PUSHn( do_tell(gv) );
1995 PUSHi( do_tell(gv) );
2005 const int whence = POPi;
2006 #if LSEEKSIZE > IVSIZE
2007 Off_t offset = (Off_t)SvNVx(POPs);
2009 Off_t offset = (Off_t)SvIVx(POPs);
2013 gv = PL_last_in_gv = (GV*)POPs;
2015 if (gv && (io = GvIO(gv))
2016 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2019 XPUSHs(SvTIED_obj((SV*)io, mg));
2020 #if LSEEKSIZE > IVSIZE
2021 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2023 XPUSHs(sv_2mortal(newSViv(offset)));
2025 XPUSHs(sv_2mortal(newSViv(whence)));
2028 call_method("SEEK", G_SCALAR);
2034 if (PL_op->op_type == OP_SEEK)
2035 PUSHs(boolSV(do_seek(gv, offset, whence)));
2037 Off_t sought = do_sysseek(gv, offset, whence);
2039 PUSHs(&PL_sv_undef);
2042 #if LSEEKSIZE > IVSIZE
2047 : newSVpvn(zero_but_true, ZBTLEN);
2048 PUSHs(sv_2mortal(sv));
2057 /* There seems to be no consensus on the length type of truncate()
2058 * and ftruncate(), both off_t and size_t have supporters. In
2059 * general one would think that when using large files, off_t is
2060 * at least as wide as size_t, so using an off_t should be okay. */
2061 /* XXX Configure probe for the length type of *truncate() needed XXX */
2064 #if Off_t_size > IVSIZE
2069 /* Checking for length < 0 is problematic as the type might or
2070 * might not be signed: if it is not, clever compilers will moan. */
2071 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2078 if (PL_op->op_flags & OPf_SPECIAL) {
2079 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2088 TAINT_PROPER("truncate");
2089 if (!(fp = IoIFP(io))) {
2095 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2097 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2107 if (SvTYPE(sv) == SVt_PVGV) {
2108 tmpgv = (GV*)sv; /* *main::FRED for example */
2109 goto do_ftruncate_gv;
2111 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2112 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2113 goto do_ftruncate_gv;
2115 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2116 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2117 goto do_ftruncate_io;
2120 name = SvPV_nolen_const(sv);
2121 TAINT_PROPER("truncate");
2123 if (truncate(name, len) < 0)
2129 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2132 if (my_chsize(tmpfd, len) < 0)
2134 PerlLIO_close(tmpfd);
2143 SETERRNO(EBADF,RMS_IFI);
2152 const unsigned int func = POPu;
2153 const int optype = PL_op->op_type;
2157 IO *io = gv ? GvIOn(gv) : 0;
2159 if (!io || !argsv || !IoIFP(io)) {
2160 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2161 report_evil_fh(gv, io, PL_op->op_type);
2162 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2166 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2169 s = SvPV_force(argsv, len);
2170 need = IOCPARM_LEN(func);
2172 s = Sv_Grow(argsv, need + 1);
2173 SvCUR_set(argsv, need);
2176 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2179 retval = SvIV(argsv);
2180 s = INT2PTR(char*,retval); /* ouch */
2183 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2185 if (optype == OP_IOCTL)
2187 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2189 DIE(aTHX_ "ioctl is not implemented");
2193 DIE(aTHX_ "fcntl is not implemented");
2195 #if defined(OS2) && defined(__EMX__)
2196 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2198 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2202 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2204 if (s[SvCUR(argsv)] != 17)
2205 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2207 s[SvCUR(argsv)] = 0; /* put our null back */
2208 SvSETMAGIC(argsv); /* Assume it has changed */
2217 PUSHp(zero_but_true, ZBTLEN);
2238 if (gv && (io = GvIO(gv)))
2245 (void)PerlIO_flush(fp);
2246 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2249 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2250 report_evil_fh(gv, io, PL_op->op_type);
2252 SETERRNO(EBADF,RMS_IFI);
2257 DIE(aTHX_ PL_no_func, "flock()");
2269 int protocol = POPi;
2275 io = gv ? GvIOn(gv) : NULL;
2278 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2279 report_evil_fh(gv, io, PL_op->op_type);
2281 do_close(gv, FALSE);
2282 SETERRNO(EBADF,LIB_INVARG);
2287 do_close(gv, FALSE);
2289 TAINT_PROPER("socket");
2290 fd = PerlSock_socket(domain, type, protocol);
2293 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2294 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2295 IoTYPE(io) = IoTYPE_SOCKET;
2296 if (!IoIFP(io) || !IoOFP(io)) {
2297 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2298 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2299 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2302 #if defined(HAS_FCNTL) && defined(F_SETFD)
2303 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2307 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2312 DIE(aTHX_ PL_no_sock_func, "socket");
2318 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2324 int protocol = POPi;
2331 io1 = gv1 ? GvIOn(gv1) : NULL;
2332 io2 = gv2 ? GvIOn(gv2) : NULL;
2333 if (!gv1 || !gv2 || !io1 || !io2) {
2334 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2336 report_evil_fh(gv1, io1, PL_op->op_type);
2338 report_evil_fh(gv1, io2, PL_op->op_type);
2341 do_close(gv1, FALSE);
2343 do_close(gv2, FALSE);
2348 do_close(gv1, FALSE);
2350 do_close(gv2, FALSE);
2352 TAINT_PROPER("socketpair");
2353 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2355 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2356 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2357 IoTYPE(io1) = IoTYPE_SOCKET;
2358 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2359 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2360 IoTYPE(io2) = IoTYPE_SOCKET;
2361 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2362 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2363 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2364 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2365 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2366 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2367 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2370 #if defined(HAS_FCNTL) && defined(F_SETFD)
2371 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2372 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2377 DIE(aTHX_ PL_no_sock_func, "socketpair");
2385 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2386 extern void GETPRIVMODE();
2387 extern void GETUSERMODE();
2390 /* OK, so on what platform does bind modify addr? */
2393 register IO *io = GvIOn(gv);
2397 int mpeprivmode = 0;
2400 if (!io || !IoIFP(io))
2403 addr = SvPV_const(addrsv, len);
2404 TAINT_PROPER("bind");
2405 #ifdef MPE /* Deal with MPE bind() peculiarities */
2406 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2407 /* The address *MUST* stupidly be zero. */
2408 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2409 /* PRIV mode is required to bind() to ports < 1024. */
2410 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2411 ((struct sockaddr_in *)addr)->sin_port > 0) {
2412 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2417 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2418 (struct sockaddr *)addr, len) >= 0)
2421 #ifdef MPE /* Switch back to USER mode */
2432 if (ckWARN(WARN_CLOSED))
2433 report_evil_fh(gv, io, PL_op->op_type);
2434 SETERRNO(EBADF,SS_IVCHAN);
2437 DIE(aTHX_ PL_no_sock_func, "bind");
2448 register IO *io = GvIOn(gv);
2451 if (!io || !IoIFP(io))
2454 addr = SvPV_const(addrsv, len);
2455 TAINT_PROPER("connect");
2456 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2462 if (ckWARN(WARN_CLOSED))
2463 report_evil_fh(gv, io, PL_op->op_type);
2464 SETERRNO(EBADF,SS_IVCHAN);
2467 DIE(aTHX_ PL_no_sock_func, "connect");
2477 register IO *io = gv ? GvIOn(gv) : NULL;
2479 if (!gv || !io || !IoIFP(io))
2482 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2488 if (ckWARN(WARN_CLOSED))
2489 report_evil_fh(gv, io, PL_op->op_type);
2490 SETERRNO(EBADF,SS_IVCHAN);
2493 DIE(aTHX_ PL_no_sock_func, "listen");
2505 char namebuf[MAXPATHLEN];
2506 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2507 Sock_size_t len = sizeof (struct sockaddr_in);
2509 Sock_size_t len = sizeof namebuf;
2522 if (!gstio || !IoIFP(gstio))
2526 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2530 do_close(ngv, FALSE);
2531 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2532 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2533 IoTYPE(nstio) = IoTYPE_SOCKET;
2534 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2535 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2536 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2537 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2540 #if defined(HAS_FCNTL) && defined(F_SETFD)
2541 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2545 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2546 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2548 #ifdef __SCO_VERSION__
2549 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2552 PUSHp(namebuf, len);
2556 if (ckWARN(WARN_CLOSED))
2557 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2558 SETERRNO(EBADF,SS_IVCHAN);
2564 DIE(aTHX_ PL_no_sock_func, "accept");
2574 register IO *io = GvIOn(gv);
2576 if (!io || !IoIFP(io))
2579 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2583 if (ckWARN(WARN_CLOSED))
2584 report_evil_fh(gv, io, PL_op->op_type);
2585 SETERRNO(EBADF,SS_IVCHAN);
2588 DIE(aTHX_ PL_no_sock_func, "shutdown");
2596 int optype = PL_op->op_type;
2599 unsigned int optname;
2605 if (optype == OP_GSOCKOPT)
2606 sv = sv_2mortal(NEWSV(22, 257));
2609 optname = (unsigned int) POPi;
2610 lvl = (unsigned int) POPi;
2614 if (!io || !IoIFP(io))
2617 fd = PerlIO_fileno(IoIFP(io));
2621 (void)SvPOK_only(sv);
2625 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2632 #if defined(__SYMBIAN32__)
2633 # define SETSOCKOPT_OPTION_VALUE_T void *
2635 # define SETSOCKOPT_OPTION_VALUE_T const char *
2637 /* XXX TODO: We need to have a proper type (a Configure probe,
2638 * etc.) for what the C headers think of the third argument of
2639 * setsockopt(), the option_value read-only buffer: is it
2640 * a "char *", or a "void *", const or not. Some compilers
2641 * don't take kindly to e.g. assuming that "char *" implicitly
2642 * promotes to a "void *", or to explicitly promoting/demoting
2643 * consts to non/vice versa. The "const void *" is the SUS
2644 * definition, but that does not fly everywhere for the above
2646 SETSOCKOPT_OPTION_VALUE_T buf;
2650 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2654 aint = (int)SvIV(sv);
2655 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2658 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2667 if (ckWARN(WARN_CLOSED))
2668 report_evil_fh(gv, io, optype);
2669 SETERRNO(EBADF,SS_IVCHAN);
2674 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2682 int optype = PL_op->op_type;
2686 register IO *io = GvIOn(gv);
2689 if (!io || !IoIFP(io))
2692 sv = sv_2mortal(NEWSV(22, 257));
2693 (void)SvPOK_only(sv);
2697 fd = PerlIO_fileno(IoIFP(io));
2699 case OP_GETSOCKNAME:
2700 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2703 case OP_GETPEERNAME:
2704 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2706 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2708 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";
2709 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2710 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2711 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2712 sizeof(u_short) + sizeof(struct in_addr))) {
2719 #ifdef BOGUS_GETNAME_RETURN
2720 /* Interactive Unix, getpeername() and getsockname()
2721 does not return valid namelen */
2722 if (len == BOGUS_GETNAME_RETURN)
2723 len = sizeof(struct sockaddr);
2731 if (ckWARN(WARN_CLOSED))
2732 report_evil_fh(gv, io, optype);
2733 SETERRNO(EBADF,SS_IVCHAN);
2738 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2751 if (PL_op->op_flags & OPf_REF) {
2753 if (PL_op->op_type == OP_LSTAT) {
2754 if (gv != PL_defgv) {
2755 do_fstat_warning_check:
2756 if (ckWARN(WARN_IO))
2757 Perl_warner(aTHX_ packWARN(WARN_IO),
2758 "lstat() on filehandle %s", GvENAME(gv));
2759 } else if (PL_laststype != OP_LSTAT)
2760 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2764 if (gv != PL_defgv) {
2765 PL_laststype = OP_STAT;
2767 sv_setpvn(PL_statname, "", 0);
2768 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2769 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2771 if (PL_laststatval < 0) {
2772 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2773 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2779 if (SvTYPE(sv) == SVt_PVGV) {
2783 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2785 if (PL_op->op_type == OP_LSTAT)
2786 goto do_fstat_warning_check;
2789 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2791 PL_laststype = PL_op->op_type;
2792 if (PL_op->op_type == OP_LSTAT)
2793 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2795 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2796 if (PL_laststatval < 0) {
2797 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2798 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2804 if (gimme != G_ARRAY) {
2805 if (gimme != G_VOID)
2806 XPUSHs(boolSV(max));
2812 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2813 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2814 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2815 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2816 #if Uid_t_size > IVSIZE
2817 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2819 # if Uid_t_sign <= 0
2820 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2822 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2825 #if Gid_t_size > IVSIZE
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2828 # if Gid_t_sign <= 0
2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2831 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2834 #ifdef USE_STAT_RDEV
2835 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2837 PUSHs(sv_2mortal(newSVpvn("", 0)));
2839 #if Off_t_size > IVSIZE
2840 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2842 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2845 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2846 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2847 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2849 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2850 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2851 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2853 #ifdef USE_STAT_BLOCKS
2854 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2855 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2857 PUSHs(sv_2mortal(newSVpvn("", 0)));
2858 PUSHs(sv_2mortal(newSVpvn("", 0)));
2864 /* This macro is used by the stacked filetest operators :
2865 * if the previous filetest failed, short-circuit and pass its value.
2866 * Else, discard it from the stack and continue. --rgs
2868 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2869 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2870 else { (void)POPs; PUTBACK; } \
2876 /* Not const, because things tweak this below. Not bool, because there's
2877 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2878 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2879 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2880 /* Giving some sort of initial value silences compilers. */
2882 int access_mode = R_OK;
2884 int access_mode = 0;
2887 /* access_mode is never used, but leaving use_access in makes the
2888 conditional compiling below much clearer. */
2891 int stat_mode = S_IRUSR;
2893 bool effective = FALSE;
2896 STACKED_FTEST_CHECK;
2898 switch (PL_op->op_type) {
2900 #if !(defined(HAS_ACCESS) && defined(R_OK))
2906 #if defined(HAS_ACCESS) && defined(W_OK)
2911 stat_mode = S_IWUSR;
2915 #if defined(HAS_ACCESS) && defined(X_OK)
2920 stat_mode = S_IXUSR;
2924 #ifdef PERL_EFF_ACCESS
2927 stat_mode = S_IWUSR;
2931 #ifndef PERL_EFF_ACCESS
2939 #ifdef PERL_EFF_ACCESS
2944 stat_mode = S_IXUSR;
2950 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2951 const char *const name = POPpx;
2953 # ifdef PERL_EFF_ACCESS
2954 result = PERL_EFF_ACCESS(name, access_mode);
2956 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
2962 result = access(name, access_mode);
2964 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
2979 if (cando(stat_mode, effective, &PL_statcache))
2987 const int op_type = PL_op->op_type;
2989 STACKED_FTEST_CHECK;
2994 if (op_type == OP_FTIS)
2997 /* You can't dTARGET inside OP_FTIS, because you'll get
2998 "panic: pad_sv po" - the op is not flagged to have a target. */
3002 #if Off_t_size > IVSIZE
3003 PUSHn(PL_statcache.st_size);
3005 PUSHi(PL_statcache.st_size);
3009 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3012 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3015 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3027 /* I believe that all these three are likely to be defined on most every
3028 system these days. */
3030 if(PL_op->op_type == OP_FTSUID)
3034 if(PL_op->op_type == OP_FTSGID)
3038 if(PL_op->op_type == OP_FTSVTX)
3042 STACKED_FTEST_CHECK;
3047 switch (PL_op->op_type) {
3049 if (PL_statcache.st_uid == PL_uid)
3053 if (PL_statcache.st_uid == PL_euid)
3057 if (PL_statcache.st_size == 0)
3061 if (S_ISSOCK(PL_statcache.st_mode))
3065 if (S_ISCHR(PL_statcache.st_mode))
3069 if (S_ISBLK(PL_statcache.st_mode))
3073 if (S_ISREG(PL_statcache.st_mode))
3077 if (S_ISDIR(PL_statcache.st_mode))
3081 if (S_ISFIFO(PL_statcache.st_mode))
3086 if (PL_statcache.st_mode & S_ISUID)
3092 if (PL_statcache.st_mode & S_ISGID)
3098 if (PL_statcache.st_mode & S_ISVTX)
3108 I32 result = my_lstat();
3112 if (S_ISLNK(PL_statcache.st_mode))
3124 STACKED_FTEST_CHECK;
3126 if (PL_op->op_flags & OPf_REF)
3128 else if (isGV(TOPs))
3130 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3131 gv = (GV*)SvRV(POPs);
3133 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3135 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3136 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3137 else if (tmpsv && SvOK(tmpsv)) {
3138 const char *tmps = SvPV_nolen_const(tmpsv);
3146 if (PerlLIO_isatty(fd))
3151 #if defined(atarist) /* this will work with atariST. Configure will
3152 make guesses for other systems. */
3153 # define FILE_base(f) ((f)->_base)
3154 # define FILE_ptr(f) ((f)->_ptr)
3155 # define FILE_cnt(f) ((f)->_cnt)
3156 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3166 register STDCHAR *s;
3172 STACKED_FTEST_CHECK;
3174 if (PL_op->op_flags & OPf_REF)
3176 else if (isGV(TOPs))
3178 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3179 gv = (GV*)SvRV(POPs);
3185 if (gv == PL_defgv) {
3187 io = GvIO(PL_statgv);
3190 goto really_filename;
3195 PL_laststatval = -1;
3196 sv_setpvn(PL_statname, "", 0);
3197 io = GvIO(PL_statgv);
3199 if (io && IoIFP(io)) {
3200 if (! PerlIO_has_base(IoIFP(io)))
3201 DIE(aTHX_ "-T and -B not implemented on filehandles");
3202 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3203 if (PL_laststatval < 0)
3205 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3206 if (PL_op->op_type == OP_FTTEXT)
3211 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3212 i = PerlIO_getc(IoIFP(io));
3214 (void)PerlIO_ungetc(IoIFP(io),i);
3216 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3218 len = PerlIO_get_bufsiz(IoIFP(io));
3219 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3220 /* sfio can have large buffers - limit to 512 */
3225 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3227 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3229 SETERRNO(EBADF,RMS_IFI);
3237 PL_laststype = OP_STAT;
3238 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3239 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3240 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3242 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3245 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3246 if (PL_laststatval < 0) {
3247 (void)PerlIO_close(fp);
3250 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3251 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3252 (void)PerlIO_close(fp);
3254 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3255 RETPUSHNO; /* special case NFS directories */
3256 RETPUSHYES; /* null file is anything */
3261 /* now scan s to look for textiness */
3262 /* XXX ASCII dependent code */
3264 #if defined(DOSISH) || defined(USEMYBINMODE)
3265 /* ignore trailing ^Z on short files */
3266 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3270 for (i = 0; i < len; i++, s++) {
3271 if (!*s) { /* null never allowed in text */
3276 else if (!(isPRINT(*s) || isSPACE(*s)))
3279 else if (*s & 128) {
3281 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3284 /* utf8 characters don't count as odd */
3285 if (UTF8_IS_START(*s)) {
3286 int ulen = UTF8SKIP(s);
3287 if (ulen < len - i) {
3289 for (j = 1; j < ulen; j++) {
3290 if (!UTF8_IS_CONTINUATION(s[j]))
3293 --ulen; /* loop does extra increment */
3303 *s != '\n' && *s != '\r' && *s != '\b' &&
3304 *s != '\t' && *s != '\f' && *s != 27)
3309 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3320 const char *tmps = 0;
3324 SV * const sv = POPs;
3325 if (SvTYPE(sv) == SVt_PVGV) {
3328 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3332 tmps = SvPVx_nolen_const(sv);
3336 if( !gv && (!tmps || !*tmps) ) {
3337 HV * const table = GvHVn(PL_envgv);
3340 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3341 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
3343 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
3348 deprecate("chdir('') or chdir(undef) as chdir()");
3349 tmps = SvPV_nolen_const(*svp);
3353 TAINT_PROPER("chdir");
3358 TAINT_PROPER("chdir");
3361 IO* const io = GvIO(gv);
3364 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3366 else if (IoDIRP(io)) {
3368 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3370 DIE(aTHX_ PL_no_func, "dirfd");
3381 DIE(aTHX_ PL_no_func, "fchdir");
3385 PUSHi( PerlDir_chdir(tmps) >= 0 );
3387 /* Clear the DEFAULT element of ENV so we'll get the new value
3389 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3396 dSP; dMARK; dTARGET;
3397 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3409 TAINT_PROPER("chroot");
3410 PUSHi( chroot(tmps) >= 0 );
3413 DIE(aTHX_ PL_no_func, "chroot");
3421 const char *tmps2 = POPpconstx;
3422 const char *tmps = SvPV_nolen_const(TOPs);
3423 TAINT_PROPER("rename");
3425 anum = PerlLIO_rename(tmps, tmps2);
3427 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3428 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3431 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3432 (void)UNLINK(tmps2);
3433 if (!(anum = link(tmps, tmps2)))
3434 anum = UNLINK(tmps);
3442 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3446 const int op_type = PL_op->op_type;
3450 if (op_type == OP_LINK)
3451 DIE(aTHX_ PL_no_func, "link");
3453 # ifndef HAS_SYMLINK
3454 if (op_type == OP_SYMLINK)
3455 DIE(aTHX_ PL_no_func, "symlink");
3459 const char *tmps2 = POPpconstx;
3460 const char *tmps = SvPV_nolen_const(TOPs);
3461 TAINT_PROPER(PL_op_desc[op_type]);
3463 # if defined(HAS_LINK)
3464 # if defined(HAS_SYMLINK)
3465 /* Both present - need to choose which. */
3466 (op_type == OP_LINK) ?
3467 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3469 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3470 PerlLIO_link(tmps, tmps2);
3473 # if defined(HAS_SYMLINK)
3474 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3475 symlink(tmps, tmps2);
3480 SETi( result >= 0 );
3487 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3497 char buf[MAXPATHLEN];
3500 #ifndef INCOMPLETE_TAINTS
3504 len = readlink(tmps, buf, sizeof(buf) - 1);
3512 RETSETUNDEF; /* just pretend it's a normal file */
3516 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3518 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3520 char * const save_filename = filename;
3526 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3527 strcpy(cmdline, cmd);
3528 strcat(cmdline, " ");
3529 for (s = cmdline + strlen(cmdline); *filename; ) {
3534 myfp = PerlProc_popen(cmdline, "r");
3538 SV *tmpsv = sv_newmortal();
3539 /* Need to save/restore 'PL_rs' ?? */
3540 s = sv_gets(tmpsv, myfp, 0);
3541 (void)PerlProc_pclose(myfp);
3545 #ifdef HAS_SYS_ERRLIST
3550 /* you don't see this */
3552 #ifdef HAS_SYS_ERRLIST
3560 if (instr(s, errmsg)) {
3567 #define EACCES EPERM
3569 if (instr(s, "cannot make"))
3570 SETERRNO(EEXIST,RMS_FEX);
3571 else if (instr(s, "existing file"))
3572 SETERRNO(EEXIST,RMS_FEX);
3573 else if (instr(s, "ile exists"))
3574 SETERRNO(EEXIST,RMS_FEX);
3575 else if (instr(s, "non-exist"))
3576 SETERRNO(ENOENT,RMS_FNF);
3577 else if (instr(s, "does not exist"))
3578 SETERRNO(ENOENT,RMS_FNF);
3579 else if (instr(s, "not empty"))
3580 SETERRNO(EBUSY,SS_DEVOFFLINE);
3581 else if (instr(s, "cannot access"))
3582 SETERRNO(EACCES,RMS_PRV);
3584 SETERRNO(EPERM,RMS_PRV);
3587 else { /* some mkdirs return no failure indication */
3588 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3589 if (PL_op->op_type == OP_RMDIR)
3594 SETERRNO(EACCES,RMS_PRV); /* a guess */
3603 /* This macro removes trailing slashes from a directory name.
3604 * Different operating and file systems take differently to
3605 * trailing slashes. According to POSIX 1003.1 1996 Edition
3606 * any number of trailing slashes should be allowed.
3607 * Thusly we snip them away so that even non-conforming
3608 * systems are happy.
3609 * We should probably do this "filtering" for all
3610 * the functions that expect (potentially) directory names:
3611 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3612 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3614 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3615 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3618 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3619 (tmps) = savepvn((tmps), (len)); \
3639 TRIMSLASHES(tmps,len,copy);
3641 TAINT_PROPER("mkdir");
3643 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3645 SETi( dooneliner("mkdir", tmps) );
3646 oldumask = PerlLIO_umask(0);
3647 PerlLIO_umask(oldumask);
3648 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3662 TRIMSLASHES(tmps,len,copy);
3663 TAINT_PROPER("rmdir");
3665 SETi( PerlDir_rmdir(tmps) >= 0 );
3667 SETi( dooneliner("rmdir", tmps) );
3674 /* Directory calls. */
3678 #if defined(Direntry_t) && defined(HAS_READDIR)
3680 const char *dirname = POPpconstx;
3682 register IO *io = GvIOn(gv);
3688 PerlDir_close(IoDIRP(io));
3689 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3695 SETERRNO(EBADF,RMS_DIR);
3698 DIE(aTHX_ PL_no_dir_func, "opendir");
3704 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3705 DIE(aTHX_ PL_no_dir_func, "readdir");
3707 #if !defined(I_DIRENT) && !defined(VMS)
3708 Direntry_t *readdir (DIR *);
3713 const I32 gimme = GIMME;
3714 GV *gv = (GV *)POPs;
3715 register Direntry_t *dp;
3716 register IO *io = GvIOn(gv);
3718 if (!io || !IoDIRP(io))
3722 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3726 sv = newSVpvn(dp->d_name, dp->d_namlen);
3728 sv = newSVpv(dp->d_name, 0);
3730 #ifndef INCOMPLETE_TAINTS
3731 if (!(IoFLAGS(io) & IOf_UNTAINT))
3734 XPUSHs(sv_2mortal(sv));
3736 while (gimme == G_ARRAY);
3738 if (!dp && gimme != G_ARRAY)
3745 SETERRNO(EBADF,RMS_ISI);
3746 if (GIMME == G_ARRAY)
3755 #if defined(HAS_TELLDIR) || defined(telldir)
3757 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3758 /* XXX netbsd still seemed to.
3759 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3760 --JHI 1999-Feb-02 */
3761 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3762 long telldir (DIR *);
3765 register IO *io = GvIOn(gv);
3767 if (!io || !IoDIRP(io))
3770 PUSHi( PerlDir_tell(IoDIRP(io)) );
3774 SETERRNO(EBADF,RMS_ISI);
3777 DIE(aTHX_ PL_no_dir_func, "telldir");
3783 #if defined(HAS_SEEKDIR) || defined(seekdir)
3787 register IO *io = GvIOn(gv);
3789 if (!io || !IoDIRP(io))
3792 (void)PerlDir_seek(IoDIRP(io), along);
3797 SETERRNO(EBADF,RMS_ISI);
3800 DIE(aTHX_ PL_no_dir_func, "seekdir");
3806 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3809 register IO *io = GvIOn(gv);
3811 if (!io || !IoDIRP(io))
3814 (void)PerlDir_rewind(IoDIRP(io));
3818 SETERRNO(EBADF,RMS_ISI);
3821 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3827 #if defined(Direntry_t) && defined(HAS_READDIR)
3830 register IO *io = GvIOn(gv);
3832 if (!io || !IoDIRP(io))
3835 #ifdef VOID_CLOSEDIR
3836 PerlDir_close(IoDIRP(io));
3838 if (PerlDir_close(IoDIRP(io)) < 0) {
3839 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3848 SETERRNO(EBADF,RMS_IFI);
3851 DIE(aTHX_ PL_no_dir_func, "closedir");
3855 /* Process control. */
3865 PERL_FLUSHALL_FOR_CHILD;
3866 childpid = PerlProc_fork();
3870 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3871 SvREADONLY_off(GvSV(tmpgv));
3872 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3873 SvREADONLY_on(GvSV(tmpgv));
3875 #ifdef THREADS_HAVE_PIDS
3876 PL_ppid = (IV)getppid();
3878 #ifdef PERL_USES_PL_PIDSTATUS
3879 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3885 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3890 PERL_FLUSHALL_FOR_CHILD;
3891 childpid = PerlProc_fork();
3897 DIE(aTHX_ PL_no_func, "fork");
3904 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3909 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3910 childpid = wait4pid(-1, &argflags, 0);
3912 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3917 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3918 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3919 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
3921 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
3926 DIE(aTHX_ PL_no_func, "wait");
3932 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3941 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3942 result = wait4pid(pid, &argflags, optype);
3944 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
3949 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3950 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3951 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
3953 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
3958 DIE(aTHX_ PL_no_func, "waitpid");
3964 dSP; dMARK; dORIGMARK; dTARGET;
3970 while (++MARK <= SP) {
3971 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
3976 TAINT_PROPER("system");
3978 PERL_FLUSHALL_FOR_CHILD;
3979 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
3985 if (PerlProc_pipe(pp) >= 0)
3987 while ((childpid = PerlProc_fork()) == -1) {
3988 if (errno != EAGAIN) {
3993 PerlLIO_close(pp[0]);
3994 PerlLIO_close(pp[1]);
4001 Sigsave_t ihand,qhand; /* place to save signals during system() */
4005 PerlLIO_close(pp[1]);
4007 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4008 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4011 result = wait4pid(childpid, &status, 0);
4012 } while (result == -1 && errno == EINTR);
4014 (void)rsignal_restore(SIGINT, &ihand);
4015 (void)rsignal_restore(SIGQUIT, &qhand);
4017 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4018 do_execfree(); /* free any memory child malloced on fork */
4024 while (n < sizeof(int)) {
4025 n1 = PerlLIO_read(pp[0],
4026 (void*)(((char*)&errkid)+n),
4032 PerlLIO_close(pp[0]);
4033 if (n) { /* Error */
4034 if (n != sizeof(int))
4035 DIE(aTHX_ "panic: kid popen errno read");
4036 errno = errkid; /* Propagate errno from kid */
4037 STATUS_NATIVE_CHILD_SET(-1);
4040 PUSHi(STATUS_CURRENT);
4044 PerlLIO_close(pp[0]);
4045 #if defined(HAS_FCNTL) && defined(F_SETFD)
4046 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4049 if (PL_op->op_flags & OPf_STACKED) {
4050 SV *really = *++MARK;
4051 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4053 else if (SP - MARK != 1)
4054 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4056 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4060 #else /* ! FORK or VMS or OS/2 */
4063 if (PL_op->op_flags & OPf_STACKED) {
4064 SV *really = *++MARK;
4065 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4066 value = (I32)do_aspawn(really, MARK, SP);
4068 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4071 else if (SP - MARK != 1) {
4072 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4073 value = (I32)do_aspawn(Nullsv, MARK, SP);
4075 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4079 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4081 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4083 STATUS_NATIVE_CHILD_SET(value);
4086 PUSHi(result ? value : STATUS_CURRENT);
4087 #endif /* !FORK or VMS */
4093 dSP; dMARK; dORIGMARK; dTARGET;
4098 while (++MARK <= SP) {
4099 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4104 TAINT_PROPER("exec");
4106 PERL_FLUSHALL_FOR_CHILD;
4107 if (PL_op->op_flags & OPf_STACKED) {
4108 SV *really = *++MARK;
4109 value = (I32)do_aexec(really, MARK, SP);
4111 else if (SP - MARK != 1)
4113 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4117 (void ) do_aspawn(Nullsv, MARK, SP);
4121 value = (I32)do_aexec(Nullsv, MARK, SP);
4126 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4129 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4132 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4146 # ifdef THREADS_HAVE_PIDS
4147 if (PL_ppid != 1 && getppid() == 1)
4148 /* maybe the parent process has died. Refresh ppid cache */
4152 XPUSHi( getppid() );
4156 DIE(aTHX_ PL_no_func, "getppid");
4172 pgrp = (I32)BSD_GETPGRP(pid);
4174 if (pid != 0 && pid != PerlProc_getpid())
4175 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4181 DIE(aTHX_ PL_no_func, "getpgrp()");
4200 TAINT_PROPER("setpgrp");
4202 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4204 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4205 || (pid != 0 && pid != PerlProc_getpid()))
4207 DIE(aTHX_ "setpgrp can't take arguments");
4209 SETi( setpgrp() >= 0 );
4210 #endif /* USE_BSDPGRP */
4213 DIE(aTHX_ PL_no_func, "setpgrp()");
4219 #ifdef HAS_GETPRIORITY
4223 SETi( getpriority(which, who) );
4226 DIE(aTHX_ PL_no_func, "getpriority()");
4232 #ifdef HAS_SETPRIORITY
4237 TAINT_PROPER("setpriority");
4238 SETi( setpriority(which, who, niceval) >= 0 );
4241 DIE(aTHX_ PL_no_func, "setpriority()");
4251 XPUSHn( time(Null(Time_t*)) );
4253 XPUSHi( time(Null(Time_t*)) );
4264 (void)PerlProc_times(&PL_timesbuf);
4266 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4267 /* struct tms, though same data */
4271 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4272 if (GIMME == G_ARRAY) {
4273 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4274 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4275 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4281 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4283 if (GIMME == G_ARRAY) {
4284 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4285 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4286 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4290 DIE(aTHX_ "times not implemented");
4292 #endif /* HAS_TIMES */
4295 #ifdef LOCALTIME_EDGECASE_BROKEN
4296 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4301 /* No workarounds in the valid range */
4302 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4303 return (localtime (tp));
4305 /* This edge case is to workaround the undefined behaviour, where the
4306 * TIMEZONE makes the time go beyond the defined range.
4307 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4308 * If there is a negative offset in TZ, like MET-1METDST, some broken
4309 * implementations of localtime () (like AIX 5.2) barf with bogus
4311 * 0x7fffffff gmtime 2038-01-19 03:14:07
4312 * 0x7fffffff localtime 1901-12-13 21:45:51
4313 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4314 * 0x3c19137f gmtime 2001-12-13 20:45:51
4315 * 0x3c19137f localtime 2001-12-13 21:45:51
4316 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4317 * Given that legal timezones are typically between GMT-12 and GMT+12
4318 * we turn back the clock 23 hours before calling the localtime
4319 * function, and add those to the return value. This will never cause
4320 * day wrapping problems, since the edge case is Tue Jan *19*
4322 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4325 if (P->tm_hour >= 24) {
4327 P->tm_mday++; /* 18 -> 19 */
4328 P->tm_wday++; /* Mon -> Tue */
4329 P->tm_yday++; /* 18 -> 19 */
4332 } /* S_my_localtime */
4339 const struct tm *tmbuf;
4340 static const char * const dayname[] =
4341 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4342 static const char * const monname[] =
4343 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4344 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4350 when = (Time_t)SvNVx(POPs);
4352 when = (Time_t)SvIVx(POPs);
4355 if (PL_op->op_type == OP_LOCALTIME)
4356 #ifdef LOCALTIME_EDGECASE_BROKEN
4357 tmbuf = S_my_localtime(aTHX_ &when);
4359 tmbuf = localtime(&when);
4362 tmbuf = gmtime(&when);
4364 if (GIMME != G_ARRAY) {
4370 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4371 dayname[tmbuf->tm_wday],
4372 monname[tmbuf->tm_mon],
4377 tmbuf->tm_year + 1900);
4378 PUSHs(sv_2mortal(tsv));
4383 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4384 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4385 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4386 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4387 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4388 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4389 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4390 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4391 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4402 anum = alarm((unsigned int)anum);
4409 DIE(aTHX_ PL_no_func, "alarm");
4420 (void)time(&lasttime);
4425 PerlProc_sleep((unsigned int)duration);
4428 XPUSHi(when - lasttime);
4432 /* Shared memory. */
4433 /* Merged with some message passing. */
4437 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4438 dSP; dMARK; dTARGET;
4439 const int op_type = PL_op->op_type;
4444 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4447 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4450 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4466 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4467 dSP; dMARK; dTARGET;
4468 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4475 DIE(aTHX_ "System V IPC is not implemented on this machine");
4481 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4482 dSP; dMARK; dTARGET;
4483 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4491 PUSHp(zero_but_true, ZBTLEN);
4501 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4502 dSP; dMARK; dTARGET;
4503 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4512 /* Get system info. */
4516 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4518 I32 which = PL_op->op_type;
4519 register char **elem;
4521 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4522 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4523 struct hostent *gethostbyname(Netdb_name_t);
4524 struct hostent *gethostent(void);
4526 struct hostent *hent;
4530 if (which == OP_GHBYNAME) {
4531 #ifdef HAS_GETHOSTBYNAME
4532 char* name = POPpbytex;
4533 hent = PerlSock_gethostbyname(name);
4535 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4538 else if (which == OP_GHBYADDR) {
4539 #ifdef HAS_GETHOSTBYADDR
4540 int addrtype = POPi;
4543 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4545 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4547 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4551 #ifdef HAS_GETHOSTENT
4552 hent = PerlSock_gethostent();
4554 DIE(aTHX_ PL_no_sock_func, "gethostent");
4557 #ifdef HOST_NOT_FOUND
4559 #ifdef USE_REENTRANT_API
4560 # ifdef USE_GETHOSTENT_ERRNO
4561 h_errno = PL_reentrant_buffer->_gethostent_errno;
4564 STATUS_UNIX_SET(h_errno);
4568 if (GIMME != G_ARRAY) {
4569 PUSHs(sv = sv_newmortal());
4571 if (which == OP_GHBYNAME) {
4573 sv_setpvn(sv, hent->h_addr, hent->h_length);
4576 sv_setpv(sv, (char*)hent->h_name);
4582 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4583 sv_setpv(sv, (char*)hent->h_name);
4584 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4585 for (elem = hent->h_aliases; elem && *elem; elem++) {
4586 sv_catpv(sv, *elem);
4588 sv_catpvn(sv, " ", 1);
4590 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4591 sv_setiv(sv, (IV)hent->h_addrtype);
4592 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4593 len = hent->h_length;
4594 sv_setiv(sv, (IV)len);
4596 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4597 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4598 sv_setpvn(sv, *elem, len);
4601 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4603 sv_setpvn(sv, hent->h_addr, len);
4608 DIE(aTHX_ PL_no_sock_func, "gethostent");
4614 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4616 I32 which = PL_op->op_type;
4617 register char **elem;
4619 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4620 struct netent *getnetbyaddr(Netdb_net_t, int);
4621 struct netent *getnetbyname(Netdb_name_t);
4622 struct netent *getnetent(void);
4624 struct netent *nent;
4626 if (which == OP_GNBYNAME){
4627 #ifdef HAS_GETNETBYNAME
4628 char *name = POPpbytex;
4629 nent = PerlSock_getnetbyname(name);
4631 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4634 else if (which == OP_GNBYADDR) {
4635 #ifdef HAS_GETNETBYADDR
4636 int addrtype = POPi;
4637 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4638 nent = PerlSock_getnetbyaddr(addr, addrtype);
4640 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4644 #ifdef HAS_GETNETENT
4645 nent = PerlSock_getnetent();
4647 DIE(aTHX_ PL_no_sock_func, "getnetent");
4650 #ifdef HOST_NOT_FOUND
4652 #ifdef USE_REENTRANT_API
4653 # ifdef USE_GETNETENT_ERRNO
4654 h_errno = PL_reentrant_buffer->_getnetent_errno;
4657 STATUS_UNIX_SET(h_errno);
4662 if (GIMME != G_ARRAY) {
4663 PUSHs(sv = sv_newmortal());
4665 if (which == OP_GNBYNAME)
4666 sv_setiv(sv, (IV)nent->n_net);
4668 sv_setpv(sv, nent->n_name);
4674 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4675 sv_setpv(sv, nent->n_name);
4676 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4677 for (elem = nent->n_aliases; elem && *elem; elem++) {
4678 sv_catpv(sv, *elem);
4680 sv_catpvn(sv, " ", 1);
4682 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4683 sv_setiv(sv, (IV)nent->n_addrtype);
4684 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4685 sv_setiv(sv, (IV)nent->n_net);
4690 DIE(aTHX_ PL_no_sock_func, "getnetent");
4696 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4698 I32 which = PL_op->op_type;
4699 register char **elem;
4701 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4702 struct protoent *getprotobyname(Netdb_name_t);
4703 struct protoent *getprotobynumber(int);
4704 struct protoent *getprotoent(void);
4706 struct protoent *pent;
4708 if (which == OP_GPBYNAME) {
4709 #ifdef HAS_GETPROTOBYNAME
4710 char* name = POPpbytex;
4711 pent = PerlSock_getprotobyname(name);
4713 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4716 else if (which == OP_GPBYNUMBER) {
4717 #ifdef HAS_GETPROTOBYNUMBER
4719 pent = PerlSock_getprotobynumber(number);
4721 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4725 #ifdef HAS_GETPROTOENT
4726 pent = PerlSock_getprotoent();
4728 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4732 if (GIMME != G_ARRAY) {
4733 PUSHs(sv = sv_newmortal());
4735 if (which == OP_GPBYNAME)
4736 sv_setiv(sv, (IV)pent->p_proto);
4738 sv_setpv(sv, pent->p_name);
4744 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4745 sv_setpv(sv, pent->p_name);
4746 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4747 for (elem = pent->p_aliases; elem && *elem; elem++) {
4748 sv_catpv(sv, *elem);
4750 sv_catpvn(sv, " ", 1);
4752 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4753 sv_setiv(sv, (IV)pent->p_proto);
4758 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4764 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4766 I32 which = PL_op->op_type;
4767 register char **elem;
4769 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4770 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4771 struct servent *getservbyport(int, Netdb_name_t);
4772 struct servent *getservent(void);
4774 struct servent *sent;
4776 if (which == OP_GSBYNAME) {
4777 #ifdef HAS_GETSERVBYNAME
4778 char *proto = POPpbytex;
4779 char *name = POPpbytex;
4781 if (proto && !*proto)
4784 sent = PerlSock_getservbyname(name, proto);
4786 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4789 else if (which == OP_GSBYPORT) {
4790 #ifdef HAS_GETSERVBYPORT
4791 char *proto = POPpbytex;
4792 unsigned short port = (unsigned short)POPu;
4794 if (proto && !*proto)
4798 port = PerlSock_htons(port);
4800 sent = PerlSock_getservbyport(port, proto);
4802 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4806 #ifdef HAS_GETSERVENT
4807 sent = PerlSock_getservent();
4809 DIE(aTHX_ PL_no_sock_func, "getservent");
4813 if (GIMME != G_ARRAY) {
4814 PUSHs(sv = sv_newmortal());
4816 if (which == OP_GSBYNAME) {
4818 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4820 sv_setiv(sv, (IV)(sent->s_port));
4824 sv_setpv(sv, sent->s_name);
4830 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831 sv_setpv(sv, sent->s_name);
4832 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4833 for (elem = sent->s_aliases; elem && *elem; elem++) {
4834 sv_catpv(sv, *elem);
4836 sv_catpvn(sv, " ", 1);
4838 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4840 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4842 sv_setiv(sv, (IV)(sent->s_port));
4844 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4845 sv_setpv(sv, sent->s_proto);
4850 DIE(aTHX_ PL_no_sock_func, "getservent");
4856 #ifdef HAS_SETHOSTENT
4858 PerlSock_sethostent(TOPi);
4861 DIE(aTHX_ PL_no_sock_func, "sethostent");
4867 #ifdef HAS_SETNETENT
4869 PerlSock_setnetent(TOPi);
4872 DIE(aTHX_ PL_no_sock_func, "setnetent");
4878 #ifdef HAS_SETPROTOENT
4880 PerlSock_setprotoent(TOPi);
4883 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4889 #ifdef HAS_SETSERVENT
4891 PerlSock_setservent(TOPi);
4894 DIE(aTHX_ PL_no_sock_func, "setservent");
4900 #ifdef HAS_ENDHOSTENT
4902 PerlSock_endhostent();
4906 DIE(aTHX_ PL_no_sock_func, "endhostent");
4912 #ifdef HAS_ENDNETENT
4914 PerlSock_endnetent();
4918 DIE(aTHX_ PL_no_sock_func, "endnetent");
4924 #ifdef HAS_ENDPROTOENT
4926 PerlSock_endprotoent();
4930 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4936 #ifdef HAS_ENDSERVENT
4938 PerlSock_endservent();
4942 DIE(aTHX_ PL_no_sock_func, "endservent");
4950 I32 which = PL_op->op_type;
4952 struct passwd *pwent = NULL;
4954 * We currently support only the SysV getsp* shadow password interface.
4955 * The interface is declared in <shadow.h> and often one needs to link
4956 * with -lsecurity or some such.
4957 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
4960 * AIX getpwnam() is clever enough to return the encrypted password
4961 * only if the caller (euid?) is root.
4963 * There are at least three other shadow password APIs. Many platforms
4964 * seem to contain more than one interface for accessing the shadow
4965 * password databases, possibly for compatibility reasons.
4966 * The getsp*() is by far he simplest one, the other two interfaces
4967 * are much more complicated, but also very similar to each other.
4972 * struct pr_passwd *getprpw*();
4973 * The password is in
4974 * char getprpw*(...).ufld.fd_encrypt[]
4975 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
4980 * struct es_passwd *getespw*();
4981 * The password is in
4982 * char *(getespw*(...).ufld.fd_encrypt)
4983 * Mention HAS_GETESPWNAM here so that Configure probes for it.
4986 * struct userpw *getuserpw();
4987 * The password is in
4988 * char *(getuserpw(...)).spw_upw_passwd
4989 * (but the de facto standard getpwnam() should work okay)
4991 * Mention I_PROT here so that Configure probes for it.
4993 * In HP-UX for getprpw*() the manual page claims that one should include
4994 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
4995 * if one includes <shadow.h> as that includes <hpsecurity.h>,
4996 * and pp_sys.c already includes <shadow.h> if there is such.
4998 * Note that <sys/security.h> is already probed for, but currently
4999 * it is only included in special cases.
5001 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5002 * be preferred interface, even though also the getprpw*() interface
5003 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5004 * One also needs to call set_auth_parameters() in main() before
5005 * doing anything else, whether one is using getespw*() or getprpw*().
5007 * Note that accessing the shadow databases can be magnitudes
5008 * slower than accessing the standard databases.
5013 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5014 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5015 * the pw_comment is left uninitialized. */
5016 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5022 char* name = POPpbytex;
5023 pwent = getpwnam(name);
5029 pwent = getpwuid(uid);
5033 # ifdef HAS_GETPWENT
5035 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5036 if (pwent) pwent = getpwnam(pwent->pw_name);
5039 DIE(aTHX_ PL_no_func, "getpwent");
5045 if (GIMME != G_ARRAY) {
5046 PUSHs(sv = sv_newmortal());
5048 if (which == OP_GPWNAM)
5049 # if Uid_t_sign <= 0
5050 sv_setiv(sv, (IV)pwent->pw_uid);
5052 sv_setuv(sv, (UV)pwent->pw_uid);
5055 sv_setpv(sv, pwent->pw_name);
5061 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5062 sv_setpv(sv, pwent->pw_name);
5064 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5066 /* If we have getspnam(), we try to dig up the shadow
5067 * password. If we are underprivileged, the shadow
5068 * interface will set the errno to EACCES or similar,
5069 * and return a null pointer. If this happens, we will
5070 * use the dummy password (usually "*" or "x") from the
5071 * standard password database.
5073 * In theory we could skip the shadow call completely
5074 * if euid != 0 but in practice we cannot know which
5075 * security measures are guarding the shadow databases
5076 * on a random platform.
5078 * Resist the urge to use additional shadow interfaces.
5079 * Divert the urge to writing an extension instead.
5082 /* Some AIX setups falsely(?) detect some getspnam(), which
5083 * has a different API than the Solaris/IRIX one. */
5084 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5086 struct spwd *spwent;
5087 int saverrno; /* Save and restore errno so that
5088 * underprivileged attempts seem
5089 * to have never made the unsccessful
5090 * attempt to retrieve the shadow password. */
5093 spwent = getspnam(pwent->pw_name);
5095 if (spwent && spwent->sp_pwdp)
5096 sv_setpv(sv, spwent->sp_pwdp);
5100 if (!SvPOK(sv)) /* Use the standard password, then. */
5101 sv_setpv(sv, pwent->pw_passwd);
5104 # ifndef INCOMPLETE_TAINTS
5105 /* passwd is tainted because user himself can diddle with it.
5106 * admittedly not much and in a very limited way, but nevertheless. */
5110 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5111 # if Uid_t_sign <= 0
5112 sv_setiv(sv, (IV)pwent->pw_uid);
5114 sv_setuv(sv, (UV)pwent->pw_uid);
5117 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5118 # if Uid_t_sign <= 0
5119 sv_setiv(sv, (IV)pwent->pw_gid);
5121 sv_setuv(sv, (UV)pwent->pw_gid);
5123 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5124 * because of the poor interface of the Perl getpw*(),
5125 * not because there's some standard/convention saying so.
5126 * A better interface would have been to return a hash,
5127 * but we are accursed by our history, alas. --jhi. */
5128 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5130 sv_setiv(sv, (IV)pwent->pw_change);
5133 sv_setiv(sv, (IV)pwent->pw_quota);
5136 sv_setpv(sv, pwent->pw_age);
5141 /* pw_class and pw_comment are mutually exclusive--.
5142 * see the above note for pw_change, pw_quota, and pw_age. */
5143 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5145 sv_setpv(sv, pwent->pw_class);
5148 sv_setpv(sv, pwent->pw_comment);
5152 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5154 sv_setpv(sv, pwent->pw_gecos);
5156 # ifndef INCOMPLETE_TAINTS
5157 /* pw_gecos is tainted because user himself can diddle with it. */
5161 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5162 sv_setpv(sv, pwent->pw_dir);
5164 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5165 sv_setpv(sv, pwent->pw_shell);
5166 # ifndef INCOMPLETE_TAINTS
5167 /* pw_shell is tainted because user himself can diddle with it. */
5172 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5173 sv_setiv(sv, (IV)pwent->pw_expire);
5178 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5184 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5189 DIE(aTHX_ PL_no_func, "setpwent");
5195 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5200 DIE(aTHX_ PL_no_func, "endpwent");
5208 I32 which = PL_op->op_type;
5209 register char **elem;
5211 struct group *grent;
5213 if (which == OP_GGRNAM) {
5214 char* name = POPpbytex;
5215 grent = (struct group *)getgrnam(name);
5217 else if (which == OP_GGRGID) {
5219 grent = (struct group *)getgrgid(gid);
5223 grent = (struct group *)getgrent();
5225 DIE(aTHX_ PL_no_func, "getgrent");
5229 if (GIMME != G_ARRAY) {
5230 PUSHs(sv = sv_newmortal());
5232 if (which == OP_GGRNAM)
5233 sv_setiv(sv, (IV)grent->gr_gid);
5235 sv_setpv(sv, grent->gr_name);
5241 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5242 sv_setpv(sv, grent->gr_name);
5244 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5246 sv_setpv(sv, grent->gr_passwd);
5249 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5250 sv_setiv(sv, (IV)grent->gr_gid);
5252 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5253 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5254 /* In UNICOS/mk (_CRAYMPP) the multithreading
5255 * versions (getgrnam_r, getgrgid_r)
5256 * seem to return an illegal pointer
5257 * as the group members list, gr_mem.
5258 * getgrent() doesn't even have a _r version
5259 * but the gr_mem is poisonous anyway.
5260 * So yes, you cannot get the list of group
5261 * members if building multithreaded in UNICOS/mk. */
5262 for (elem = grent->gr_mem; elem && *elem; elem++) {
5263 sv_catpv(sv, *elem);
5265 sv_catpvn(sv, " ", 1);
5272 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5278 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5283 DIE(aTHX_ PL_no_func, "setgrent");
5289 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5294 DIE(aTHX_ PL_no_func, "endgrent");
5304 if (!(tmps = PerlProc_getlogin()))
5306 PUSHp(tmps, strlen(tmps));
5309 DIE(aTHX_ PL_no_func, "getlogin");
5313 /* Miscellaneous. */
5318 dSP; dMARK; dORIGMARK; dTARGET;
5319 register I32 items = SP - MARK;
5320 unsigned long a[20];
5325 while (++MARK <= SP) {
5326 if (SvTAINTED(*MARK)) {
5332 TAINT_PROPER("syscall");
5335 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5336 * or where sizeof(long) != sizeof(char*). But such machines will
5337 * not likely have syscall implemented either, so who cares?
5339 while (++MARK <= SP) {
5340 if (SvNIOK(*MARK) || !i)
5341 a[i++] = SvIV(*MARK);
5342 else if (*MARK == &PL_sv_undef)
5345 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5351 DIE(aTHX_ "Too many args to syscall");
5353 DIE(aTHX_ "Too few args to syscall");
5355 retval = syscall(a[0]);
5358 retval = syscall(a[0],a[1]);
5361 retval = syscall(a[0],a[1],a[2]);
5364 retval = syscall(a[0],a[1],a[2],a[3]);
5367 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5370 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5373 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5376 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5380 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5383 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5386 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5390 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5394 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5398 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5399 a[10],a[11],a[12],a[13]);
5401 #endif /* atarist */
5407 DIE(aTHX_ PL_no_func, "syscall");
5411 #ifdef FCNTL_EMULATE_FLOCK
5413 /* XXX Emulate flock() with fcntl().
5414 What's really needed is a good file locking module.
5418 fcntl_emulate_flock(int fd, int operation)
5422 switch (operation & ~LOCK_NB) {
5424 flock.l_type = F_RDLCK;
5427 flock.l_type = F_WRLCK;
5430 flock.l_type = F_UNLCK;
5436 flock.l_whence = SEEK_SET;
5437 flock.l_start = flock.l_len = (Off_t)0;
5439 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5442 #endif /* FCNTL_EMULATE_FLOCK */
5444 #ifdef LOCKF_EMULATE_FLOCK
5446 /* XXX Emulate flock() with lockf(). This is just to increase
5447 portability of scripts. The calls are not completely
5448 interchangeable. What's really needed is a good file
5452 /* The lockf() constants might have been defined in <unistd.h>.
5453 Unfortunately, <unistd.h> causes troubles on some mixed
5454 (BSD/POSIX) systems, such as SunOS 4.1.3.
5456 Further, the lockf() constants aren't POSIX, so they might not be
5457 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5458 just stick in the SVID values and be done with it. Sigh.
5462 # define F_ULOCK 0 /* Unlock a previously locked region */
5465 # define F_LOCK 1 /* Lock a region for exclusive use */
5468 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5471 # define F_TEST 3 /* Test a region for other processes locks */
5475 lockf_emulate_flock(int fd, int operation)
5481 /* flock locks entire file so for lockf we need to do the same */
5483 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5484 if (pos > 0) /* is seekable and needs to be repositioned */
5485 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5486 pos = -1; /* seek failed, so don't seek back afterwards */
5489 switch (operation) {
5491 /* LOCK_SH - get a shared lock */
5493 /* LOCK_EX - get an exclusive lock */
5495 i = lockf (fd, F_LOCK, 0);
5498 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5499 case LOCK_SH|LOCK_NB:
5500 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5501 case LOCK_EX|LOCK_NB:
5502 i = lockf (fd, F_TLOCK, 0);
5504 if ((errno == EAGAIN) || (errno == EACCES))
5505 errno = EWOULDBLOCK;
5508 /* LOCK_UN - unlock (non-blocking is a no-op) */
5510 case LOCK_UN|LOCK_NB:
5511 i = lockf (fd, F_ULOCK, 0);
5514 /* Default - can't decipher operation */
5521 if (pos > 0) /* need to restore position of the handle */
5522 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5527 #endif /* LOCKF_EMULATE_FLOCK */
5531 * c-indentation-style: bsd
5533 * indent-tabs-mode: t
5536 * ex: set ts=8 sts=4 sw=4 noet: