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; } \
2877 STACKED_FTEST_CHECK;
2878 #if defined(HAS_ACCESS) && defined(R_OK)
2879 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2880 result = access(POPpx, R_OK);
2895 if (cando(S_IRUSR, 0, &PL_statcache))
2904 STACKED_FTEST_CHECK;
2905 #if defined(HAS_ACCESS) && defined(W_OK)
2906 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2907 result = access(POPpx, W_OK);
2922 if (cando(S_IWUSR, 0, &PL_statcache))
2931 STACKED_FTEST_CHECK;
2932 #if defined(HAS_ACCESS) && defined(X_OK)
2933 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2934 result = access(POPpx, X_OK);
2949 if (cando(S_IXUSR, 0, &PL_statcache))
2958 STACKED_FTEST_CHECK;
2959 #ifdef PERL_EFF_ACCESS
2960 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2961 result = PERL_EFF_ACCESS(POPpx, R_OK);
2976 if (cando(S_IRUSR, 1, &PL_statcache))
2985 STACKED_FTEST_CHECK;
2986 #ifdef PERL_EFF_ACCESS
2987 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2988 result = PERL_EFF_ACCESS(POPpx, W_OK);
3003 if (cando(S_IWUSR, 1, &PL_statcache))
3012 STACKED_FTEST_CHECK;
3013 #ifdef PERL_EFF_ACCESS
3014 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3015 result = PERL_EFF_ACCESS(POPpx, X_OK);
3030 if (cando(S_IXUSR, 1, &PL_statcache))
3038 const int op_type = PL_op->op_type;
3040 STACKED_FTEST_CHECK;
3045 if (op_type == OP_FTIS)
3048 /* You can't dTARGET inside OP_FTIS, because you'll get
3049 "panic: pad_sv po" - the op is not flagged to have a target. */
3053 #if Off_t_size > IVSIZE
3054 PUSHn(PL_statcache.st_size);
3056 PUSHi(PL_statcache.st_size);
3060 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3063 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3066 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3078 /* I believe that all these three are likely to be defined on most every
3079 system these days. */
3081 if(PL_op->op_type == OP_FTSUID)
3085 if(PL_op->op_type == OP_FTSGID)
3089 if(PL_op->op_type == OP_FTSVTX)
3093 STACKED_FTEST_CHECK;
3098 switch (PL_op->op_type) {
3100 if (PL_statcache.st_uid == PL_uid)
3104 if (PL_statcache.st_uid == PL_euid)
3108 if (PL_statcache.st_size == 0)
3112 if (S_ISSOCK(PL_statcache.st_mode))
3116 if (S_ISCHR(PL_statcache.st_mode))
3120 if (S_ISBLK(PL_statcache.st_mode))
3124 if (S_ISREG(PL_statcache.st_mode))
3128 if (S_ISDIR(PL_statcache.st_mode))
3132 if (S_ISFIFO(PL_statcache.st_mode))
3137 if (PL_statcache.st_mode & S_ISUID)
3143 if (PL_statcache.st_mode & S_ISGID)
3149 if (PL_statcache.st_mode & S_ISVTX)
3159 I32 result = my_lstat();
3163 if (S_ISLNK(PL_statcache.st_mode))
3175 STACKED_FTEST_CHECK;
3177 if (PL_op->op_flags & OPf_REF)
3179 else if (isGV(TOPs))
3181 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3182 gv = (GV*)SvRV(POPs);
3184 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3186 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3187 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3188 else if (tmpsv && SvOK(tmpsv)) {
3189 const char *tmps = SvPV_nolen_const(tmpsv);
3197 if (PerlLIO_isatty(fd))
3202 #if defined(atarist) /* this will work with atariST. Configure will
3203 make guesses for other systems. */
3204 # define FILE_base(f) ((f)->_base)
3205 # define FILE_ptr(f) ((f)->_ptr)
3206 # define FILE_cnt(f) ((f)->_cnt)
3207 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3217 register STDCHAR *s;
3223 STACKED_FTEST_CHECK;
3225 if (PL_op->op_flags & OPf_REF)
3227 else if (isGV(TOPs))
3229 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3230 gv = (GV*)SvRV(POPs);
3236 if (gv == PL_defgv) {
3238 io = GvIO(PL_statgv);
3241 goto really_filename;
3246 PL_laststatval = -1;
3247 sv_setpvn(PL_statname, "", 0);
3248 io = GvIO(PL_statgv);
3250 if (io && IoIFP(io)) {
3251 if (! PerlIO_has_base(IoIFP(io)))
3252 DIE(aTHX_ "-T and -B not implemented on filehandles");
3253 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3254 if (PL_laststatval < 0)
3256 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3257 if (PL_op->op_type == OP_FTTEXT)
3262 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3263 i = PerlIO_getc(IoIFP(io));
3265 (void)PerlIO_ungetc(IoIFP(io),i);
3267 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3269 len = PerlIO_get_bufsiz(IoIFP(io));
3270 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3271 /* sfio can have large buffers - limit to 512 */
3276 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3278 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3280 SETERRNO(EBADF,RMS_IFI);
3288 PL_laststype = OP_STAT;
3289 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3290 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3291 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3293 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3296 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3297 if (PL_laststatval < 0) {
3298 (void)PerlIO_close(fp);
3301 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3302 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3303 (void)PerlIO_close(fp);
3305 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3306 RETPUSHNO; /* special case NFS directories */
3307 RETPUSHYES; /* null file is anything */
3312 /* now scan s to look for textiness */
3313 /* XXX ASCII dependent code */
3315 #if defined(DOSISH) || defined(USEMYBINMODE)
3316 /* ignore trailing ^Z on short files */
3317 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3321 for (i = 0; i < len; i++, s++) {
3322 if (!*s) { /* null never allowed in text */
3327 else if (!(isPRINT(*s) || isSPACE(*s)))
3330 else if (*s & 128) {
3332 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3335 /* utf8 characters don't count as odd */
3336 if (UTF8_IS_START(*s)) {
3337 int ulen = UTF8SKIP(s);
3338 if (ulen < len - i) {
3340 for (j = 1; j < ulen; j++) {
3341 if (!UTF8_IS_CONTINUATION(s[j]))
3344 --ulen; /* loop does extra increment */
3354 *s != '\n' && *s != '\r' && *s != '\b' &&
3355 *s != '\t' && *s != '\f' && *s != 27)
3360 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3371 const char *tmps = 0;
3375 SV * const sv = POPs;
3376 if (SvTYPE(sv) == SVt_PVGV) {
3379 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3383 tmps = SvPVx_nolen_const(sv);
3387 if( !gv && (!tmps || !*tmps) ) {
3388 HV * const table = GvHVn(PL_envgv);
3391 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3392 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
3394 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
3399 deprecate("chdir('') or chdir(undef) as chdir()");
3400 tmps = SvPV_nolen_const(*svp);
3404 TAINT_PROPER("chdir");
3409 TAINT_PROPER("chdir");
3412 IO* const io = GvIO(gv);
3415 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3417 else if (IoDIRP(io)) {
3419 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3421 DIE(aTHX_ PL_no_func, "dirfd");
3432 DIE(aTHX_ PL_no_func, "fchdir");
3436 PUSHi( PerlDir_chdir(tmps) >= 0 );
3438 /* Clear the DEFAULT element of ENV so we'll get the new value
3440 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3447 dSP; dMARK; dTARGET;
3448 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3460 TAINT_PROPER("chroot");
3461 PUSHi( chroot(tmps) >= 0 );
3464 DIE(aTHX_ PL_no_func, "chroot");
3472 const char *tmps2 = POPpconstx;
3473 const char *tmps = SvPV_nolen_const(TOPs);
3474 TAINT_PROPER("rename");
3476 anum = PerlLIO_rename(tmps, tmps2);
3478 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3479 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3482 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3483 (void)UNLINK(tmps2);
3484 if (!(anum = link(tmps, tmps2)))
3485 anum = UNLINK(tmps);
3493 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3497 const int op_type = PL_op->op_type;
3501 if (op_type == OP_LINK)
3502 DIE(aTHX_ PL_no_func, "link");
3504 # ifndef HAS_SYMLINK
3505 if (op_type == OP_SYMLINK)
3506 DIE(aTHX_ PL_no_func, "symlink");
3510 const char *tmps2 = POPpconstx;
3511 const char *tmps = SvPV_nolen_const(TOPs);
3512 TAINT_PROPER(PL_op_desc[op_type]);
3514 # if defined(HAS_LINK)
3515 # if defined(HAS_SYMLINK)
3516 /* Both present - need to choose which. */
3517 (op_type == OP_LINK) ?
3518 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3520 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3521 symlink(tmps, tmps2);
3524 # if defined(HAS_SYMLINK)
3525 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3526 PerlLIO_link(tmps, tmps2);
3531 SETi( result >= 0 );
3538 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3548 char buf[MAXPATHLEN];
3551 #ifndef INCOMPLETE_TAINTS
3555 len = readlink(tmps, buf, sizeof(buf) - 1);
3563 RETSETUNDEF; /* just pretend it's a normal file */
3567 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3569 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3571 char * const save_filename = filename;
3577 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3578 strcpy(cmdline, cmd);
3579 strcat(cmdline, " ");
3580 for (s = cmdline + strlen(cmdline); *filename; ) {
3585 myfp = PerlProc_popen(cmdline, "r");
3589 SV *tmpsv = sv_newmortal();
3590 /* Need to save/restore 'PL_rs' ?? */
3591 s = sv_gets(tmpsv, myfp, 0);
3592 (void)PerlProc_pclose(myfp);
3596 #ifdef HAS_SYS_ERRLIST
3601 /* you don't see this */
3603 #ifdef HAS_SYS_ERRLIST
3611 if (instr(s, errmsg)) {
3618 #define EACCES EPERM
3620 if (instr(s, "cannot make"))
3621 SETERRNO(EEXIST,RMS_FEX);
3622 else if (instr(s, "existing file"))
3623 SETERRNO(EEXIST,RMS_FEX);
3624 else if (instr(s, "ile exists"))
3625 SETERRNO(EEXIST,RMS_FEX);
3626 else if (instr(s, "non-exist"))
3627 SETERRNO(ENOENT,RMS_FNF);
3628 else if (instr(s, "does not exist"))
3629 SETERRNO(ENOENT,RMS_FNF);
3630 else if (instr(s, "not empty"))
3631 SETERRNO(EBUSY,SS_DEVOFFLINE);
3632 else if (instr(s, "cannot access"))
3633 SETERRNO(EACCES,RMS_PRV);
3635 SETERRNO(EPERM,RMS_PRV);
3638 else { /* some mkdirs return no failure indication */
3639 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3640 if (PL_op->op_type == OP_RMDIR)
3645 SETERRNO(EACCES,RMS_PRV); /* a guess */
3654 /* This macro removes trailing slashes from a directory name.
3655 * Different operating and file systems take differently to
3656 * trailing slashes. According to POSIX 1003.1 1996 Edition
3657 * any number of trailing slashes should be allowed.
3658 * Thusly we snip them away so that even non-conforming
3659 * systems are happy.
3660 * We should probably do this "filtering" for all
3661 * the functions that expect (potentially) directory names:
3662 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3663 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3665 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3666 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3669 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3670 (tmps) = savepvn((tmps), (len)); \
3690 TRIMSLASHES(tmps,len,copy);
3692 TAINT_PROPER("mkdir");
3694 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3696 SETi( dooneliner("mkdir", tmps) );
3697 oldumask = PerlLIO_umask(0);
3698 PerlLIO_umask(oldumask);
3699 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3713 TRIMSLASHES(tmps,len,copy);
3714 TAINT_PROPER("rmdir");
3716 SETi( PerlDir_rmdir(tmps) >= 0 );
3718 SETi( dooneliner("rmdir", tmps) );
3725 /* Directory calls. */
3729 #if defined(Direntry_t) && defined(HAS_READDIR)
3731 const char *dirname = POPpconstx;
3733 register IO *io = GvIOn(gv);
3739 PerlDir_close(IoDIRP(io));
3740 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3746 SETERRNO(EBADF,RMS_DIR);
3749 DIE(aTHX_ PL_no_dir_func, "opendir");
3755 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3756 DIE(aTHX_ PL_no_dir_func, "readdir");
3758 #if !defined(I_DIRENT) && !defined(VMS)
3759 Direntry_t *readdir (DIR *);
3764 const I32 gimme = GIMME;
3765 GV *gv = (GV *)POPs;
3766 register Direntry_t *dp;
3767 register IO *io = GvIOn(gv);
3769 if (!io || !IoDIRP(io))
3773 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3777 sv = newSVpvn(dp->d_name, dp->d_namlen);
3779 sv = newSVpv(dp->d_name, 0);
3781 #ifndef INCOMPLETE_TAINTS
3782 if (!(IoFLAGS(io) & IOf_UNTAINT))
3785 XPUSHs(sv_2mortal(sv));
3787 while (gimme == G_ARRAY);
3789 if (!dp && gimme != G_ARRAY)
3796 SETERRNO(EBADF,RMS_ISI);
3797 if (GIMME == G_ARRAY)
3806 #if defined(HAS_TELLDIR) || defined(telldir)
3808 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3809 /* XXX netbsd still seemed to.
3810 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3811 --JHI 1999-Feb-02 */
3812 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3813 long telldir (DIR *);
3816 register IO *io = GvIOn(gv);
3818 if (!io || !IoDIRP(io))
3821 PUSHi( PerlDir_tell(IoDIRP(io)) );
3825 SETERRNO(EBADF,RMS_ISI);
3828 DIE(aTHX_ PL_no_dir_func, "telldir");
3834 #if defined(HAS_SEEKDIR) || defined(seekdir)
3838 register IO *io = GvIOn(gv);
3840 if (!io || !IoDIRP(io))
3843 (void)PerlDir_seek(IoDIRP(io), along);
3848 SETERRNO(EBADF,RMS_ISI);
3851 DIE(aTHX_ PL_no_dir_func, "seekdir");
3857 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3860 register IO *io = GvIOn(gv);
3862 if (!io || !IoDIRP(io))
3865 (void)PerlDir_rewind(IoDIRP(io));
3869 SETERRNO(EBADF,RMS_ISI);
3872 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3878 #if defined(Direntry_t) && defined(HAS_READDIR)
3881 register IO *io = GvIOn(gv);
3883 if (!io || !IoDIRP(io))
3886 #ifdef VOID_CLOSEDIR
3887 PerlDir_close(IoDIRP(io));
3889 if (PerlDir_close(IoDIRP(io)) < 0) {
3890 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3899 SETERRNO(EBADF,RMS_IFI);
3902 DIE(aTHX_ PL_no_dir_func, "closedir");
3906 /* Process control. */
3916 PERL_FLUSHALL_FOR_CHILD;
3917 childpid = PerlProc_fork();
3921 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3922 SvREADONLY_off(GvSV(tmpgv));
3923 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3924 SvREADONLY_on(GvSV(tmpgv));
3926 #ifdef THREADS_HAVE_PIDS
3927 PL_ppid = (IV)getppid();
3929 #ifdef PERL_USES_PL_PIDSTATUS
3930 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3936 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3941 PERL_FLUSHALL_FOR_CHILD;
3942 childpid = PerlProc_fork();
3948 DIE(aTHX_ PL_no_func, "fork");
3955 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3960 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3961 childpid = wait4pid(-1, &argflags, 0);
3963 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3968 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3969 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3970 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
3972 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
3977 DIE(aTHX_ PL_no_func, "wait");
3983 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3992 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3993 result = wait4pid(pid, &argflags, optype);
3995 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4000 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4001 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4002 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4004 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4009 DIE(aTHX_ PL_no_func, "waitpid");
4015 dSP; dMARK; dORIGMARK; dTARGET;
4021 while (++MARK <= SP) {
4022 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4027 TAINT_PROPER("system");
4029 PERL_FLUSHALL_FOR_CHILD;
4030 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4036 if (PerlProc_pipe(pp) >= 0)
4038 while ((childpid = PerlProc_fork()) == -1) {
4039 if (errno != EAGAIN) {
4044 PerlLIO_close(pp[0]);
4045 PerlLIO_close(pp[1]);
4052 Sigsave_t ihand,qhand; /* place to save signals during system() */
4056 PerlLIO_close(pp[1]);
4058 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4059 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4062 result = wait4pid(childpid, &status, 0);
4063 } while (result == -1 && errno == EINTR);
4065 (void)rsignal_restore(SIGINT, &ihand);
4066 (void)rsignal_restore(SIGQUIT, &qhand);
4068 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4069 do_execfree(); /* free any memory child malloced on fork */
4075 while (n < sizeof(int)) {
4076 n1 = PerlLIO_read(pp[0],
4077 (void*)(((char*)&errkid)+n),
4083 PerlLIO_close(pp[0]);
4084 if (n) { /* Error */
4085 if (n != sizeof(int))
4086 DIE(aTHX_ "panic: kid popen errno read");
4087 errno = errkid; /* Propagate errno from kid */
4088 STATUS_NATIVE_CHILD_SET(-1);
4091 PUSHi(STATUS_CURRENT);
4095 PerlLIO_close(pp[0]);
4096 #if defined(HAS_FCNTL) && defined(F_SETFD)
4097 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4100 if (PL_op->op_flags & OPf_STACKED) {
4101 SV *really = *++MARK;
4102 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4104 else if (SP - MARK != 1)
4105 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4107 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4111 #else /* ! FORK or VMS or OS/2 */
4114 if (PL_op->op_flags & OPf_STACKED) {
4115 SV *really = *++MARK;
4116 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4117 value = (I32)do_aspawn(really, MARK, SP);
4119 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4122 else if (SP - MARK != 1) {
4123 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4124 value = (I32)do_aspawn(Nullsv, MARK, SP);
4126 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4130 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4132 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4134 STATUS_NATIVE_CHILD_SET(value);
4137 PUSHi(result ? value : STATUS_CURRENT);
4138 #endif /* !FORK or VMS */
4144 dSP; dMARK; dORIGMARK; dTARGET;
4149 while (++MARK <= SP) {
4150 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4155 TAINT_PROPER("exec");
4157 PERL_FLUSHALL_FOR_CHILD;
4158 if (PL_op->op_flags & OPf_STACKED) {
4159 SV *really = *++MARK;
4160 value = (I32)do_aexec(really, MARK, SP);
4162 else if (SP - MARK != 1)
4164 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4168 (void ) do_aspawn(Nullsv, MARK, SP);
4172 value = (I32)do_aexec(Nullsv, MARK, SP);
4177 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4180 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4183 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4197 # ifdef THREADS_HAVE_PIDS
4198 if (PL_ppid != 1 && getppid() == 1)
4199 /* maybe the parent process has died. Refresh ppid cache */
4203 XPUSHi( getppid() );
4207 DIE(aTHX_ PL_no_func, "getppid");
4223 pgrp = (I32)BSD_GETPGRP(pid);
4225 if (pid != 0 && pid != PerlProc_getpid())
4226 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4232 DIE(aTHX_ PL_no_func, "getpgrp()");
4251 TAINT_PROPER("setpgrp");
4253 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4255 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4256 || (pid != 0 && pid != PerlProc_getpid()))
4258 DIE(aTHX_ "setpgrp can't take arguments");
4260 SETi( setpgrp() >= 0 );
4261 #endif /* USE_BSDPGRP */
4264 DIE(aTHX_ PL_no_func, "setpgrp()");
4270 #ifdef HAS_GETPRIORITY
4274 SETi( getpriority(which, who) );
4277 DIE(aTHX_ PL_no_func, "getpriority()");
4283 #ifdef HAS_SETPRIORITY
4288 TAINT_PROPER("setpriority");
4289 SETi( setpriority(which, who, niceval) >= 0 );
4292 DIE(aTHX_ PL_no_func, "setpriority()");
4302 XPUSHn( time(Null(Time_t*)) );
4304 XPUSHi( time(Null(Time_t*)) );
4315 (void)PerlProc_times(&PL_timesbuf);
4317 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4318 /* struct tms, though same data */
4322 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4323 if (GIMME == G_ARRAY) {
4324 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4325 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4326 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4332 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4334 if (GIMME == G_ARRAY) {
4335 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4336 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4337 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4341 DIE(aTHX_ "times not implemented");
4343 #endif /* HAS_TIMES */
4346 #ifdef LOCALTIME_EDGECASE_BROKEN
4347 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4352 /* No workarounds in the valid range */
4353 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4354 return (localtime (tp));
4356 /* This edge case is to workaround the undefined behaviour, where the
4357 * TIMEZONE makes the time go beyond the defined range.
4358 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4359 * If there is a negative offset in TZ, like MET-1METDST, some broken
4360 * implementations of localtime () (like AIX 5.2) barf with bogus
4362 * 0x7fffffff gmtime 2038-01-19 03:14:07
4363 * 0x7fffffff localtime 1901-12-13 21:45:51
4364 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4365 * 0x3c19137f gmtime 2001-12-13 20:45:51
4366 * 0x3c19137f localtime 2001-12-13 21:45:51
4367 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4368 * Given that legal timezones are typically between GMT-12 and GMT+12
4369 * we turn back the clock 23 hours before calling the localtime
4370 * function, and add those to the return value. This will never cause
4371 * day wrapping problems, since the edge case is Tue Jan *19*
4373 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4376 if (P->tm_hour >= 24) {
4378 P->tm_mday++; /* 18 -> 19 */
4379 P->tm_wday++; /* Mon -> Tue */
4380 P->tm_yday++; /* 18 -> 19 */
4383 } /* S_my_localtime */
4390 const struct tm *tmbuf;
4391 static const char * const dayname[] =
4392 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4393 static const char * const monname[] =
4394 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4395 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4401 when = (Time_t)SvNVx(POPs);
4403 when = (Time_t)SvIVx(POPs);
4406 if (PL_op->op_type == OP_LOCALTIME)
4407 #ifdef LOCALTIME_EDGECASE_BROKEN
4408 tmbuf = S_my_localtime(aTHX_ &when);
4410 tmbuf = localtime(&when);
4413 tmbuf = gmtime(&when);
4415 if (GIMME != G_ARRAY) {
4421 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4422 dayname[tmbuf->tm_wday],
4423 monname[tmbuf->tm_mon],
4428 tmbuf->tm_year + 1900);
4429 PUSHs(sv_2mortal(tsv));
4434 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4435 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4436 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4437 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4438 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4439 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4440 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4441 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4442 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4453 anum = alarm((unsigned int)anum);
4460 DIE(aTHX_ PL_no_func, "alarm");
4471 (void)time(&lasttime);
4476 PerlProc_sleep((unsigned int)duration);
4479 XPUSHi(when - lasttime);
4483 /* Shared memory. */
4487 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4488 dSP; dMARK; dTARGET;
4489 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4498 /* Message passing. */
4502 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4503 dSP; dMARK; dTARGET;
4504 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4515 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4516 dSP; dMARK; dTARGET;
4517 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4530 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4531 dSP; dMARK; dTARGET;
4532 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4539 DIE(aTHX_ "System V IPC is not implemented on this machine");
4545 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4546 dSP; dMARK; dTARGET;
4547 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4555 PUSHp(zero_but_true, ZBTLEN);
4565 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4566 dSP; dMARK; dTARGET;
4567 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4576 /* Get system info. */
4580 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4582 I32 which = PL_op->op_type;
4583 register char **elem;
4585 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4586 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4587 struct hostent *gethostbyname(Netdb_name_t);
4588 struct hostent *gethostent(void);
4590 struct hostent *hent;
4594 if (which == OP_GHBYNAME) {
4595 #ifdef HAS_GETHOSTBYNAME
4596 char* name = POPpbytex;
4597 hent = PerlSock_gethostbyname(name);
4599 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4602 else if (which == OP_GHBYADDR) {
4603 #ifdef HAS_GETHOSTBYADDR
4604 int addrtype = POPi;
4607 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4609 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4611 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4615 #ifdef HAS_GETHOSTENT
4616 hent = PerlSock_gethostent();
4618 DIE(aTHX_ PL_no_sock_func, "gethostent");
4621 #ifdef HOST_NOT_FOUND
4623 #ifdef USE_REENTRANT_API
4624 # ifdef USE_GETHOSTENT_ERRNO
4625 h_errno = PL_reentrant_buffer->_gethostent_errno;
4628 STATUS_UNIX_SET(h_errno);
4632 if (GIMME != G_ARRAY) {
4633 PUSHs(sv = sv_newmortal());
4635 if (which == OP_GHBYNAME) {
4637 sv_setpvn(sv, hent->h_addr, hent->h_length);
4640 sv_setpv(sv, (char*)hent->h_name);
4646 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4647 sv_setpv(sv, (char*)hent->h_name);
4648 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4649 for (elem = hent->h_aliases; elem && *elem; elem++) {
4650 sv_catpv(sv, *elem);
4652 sv_catpvn(sv, " ", 1);
4654 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4655 sv_setiv(sv, (IV)hent->h_addrtype);
4656 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4657 len = hent->h_length;
4658 sv_setiv(sv, (IV)len);
4660 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4661 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4662 sv_setpvn(sv, *elem, len);
4665 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4667 sv_setpvn(sv, hent->h_addr, len);
4672 DIE(aTHX_ PL_no_sock_func, "gethostent");
4678 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4680 I32 which = PL_op->op_type;
4681 register char **elem;
4683 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4684 struct netent *getnetbyaddr(Netdb_net_t, int);
4685 struct netent *getnetbyname(Netdb_name_t);
4686 struct netent *getnetent(void);
4688 struct netent *nent;
4690 if (which == OP_GNBYNAME){
4691 #ifdef HAS_GETNETBYNAME
4692 char *name = POPpbytex;
4693 nent = PerlSock_getnetbyname(name);
4695 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4698 else if (which == OP_GNBYADDR) {
4699 #ifdef HAS_GETNETBYADDR
4700 int addrtype = POPi;
4701 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4702 nent = PerlSock_getnetbyaddr(addr, addrtype);
4704 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4708 #ifdef HAS_GETNETENT
4709 nent = PerlSock_getnetent();
4711 DIE(aTHX_ PL_no_sock_func, "getnetent");
4714 #ifdef HOST_NOT_FOUND
4716 #ifdef USE_REENTRANT_API
4717 # ifdef USE_GETNETENT_ERRNO
4718 h_errno = PL_reentrant_buffer->_getnetent_errno;
4721 STATUS_UNIX_SET(h_errno);
4726 if (GIMME != G_ARRAY) {
4727 PUSHs(sv = sv_newmortal());
4729 if (which == OP_GNBYNAME)
4730 sv_setiv(sv, (IV)nent->n_net);
4732 sv_setpv(sv, nent->n_name);
4738 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4739 sv_setpv(sv, nent->n_name);
4740 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4741 for (elem = nent->n_aliases; elem && *elem; elem++) {
4742 sv_catpv(sv, *elem);
4744 sv_catpvn(sv, " ", 1);
4746 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4747 sv_setiv(sv, (IV)nent->n_addrtype);
4748 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4749 sv_setiv(sv, (IV)nent->n_net);
4754 DIE(aTHX_ PL_no_sock_func, "getnetent");
4760 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4762 I32 which = PL_op->op_type;
4763 register char **elem;
4765 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4766 struct protoent *getprotobyname(Netdb_name_t);
4767 struct protoent *getprotobynumber(int);
4768 struct protoent *getprotoent(void);
4770 struct protoent *pent;
4772 if (which == OP_GPBYNAME) {
4773 #ifdef HAS_GETPROTOBYNAME
4774 char* name = POPpbytex;
4775 pent = PerlSock_getprotobyname(name);
4777 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4780 else if (which == OP_GPBYNUMBER) {
4781 #ifdef HAS_GETPROTOBYNUMBER
4783 pent = PerlSock_getprotobynumber(number);
4785 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4789 #ifdef HAS_GETPROTOENT
4790 pent = PerlSock_getprotoent();
4792 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4796 if (GIMME != G_ARRAY) {
4797 PUSHs(sv = sv_newmortal());
4799 if (which == OP_GPBYNAME)
4800 sv_setiv(sv, (IV)pent->p_proto);
4802 sv_setpv(sv, pent->p_name);
4808 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4809 sv_setpv(sv, pent->p_name);
4810 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4811 for (elem = pent->p_aliases; elem && *elem; elem++) {
4812 sv_catpv(sv, *elem);
4814 sv_catpvn(sv, " ", 1);
4816 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4817 sv_setiv(sv, (IV)pent->p_proto);
4822 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4828 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4830 I32 which = PL_op->op_type;
4831 register char **elem;
4833 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4834 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4835 struct servent *getservbyport(int, Netdb_name_t);
4836 struct servent *getservent(void);
4838 struct servent *sent;
4840 if (which == OP_GSBYNAME) {
4841 #ifdef HAS_GETSERVBYNAME
4842 char *proto = POPpbytex;
4843 char *name = POPpbytex;
4845 if (proto && !*proto)
4848 sent = PerlSock_getservbyname(name, proto);
4850 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4853 else if (which == OP_GSBYPORT) {
4854 #ifdef HAS_GETSERVBYPORT
4855 char *proto = POPpbytex;
4856 unsigned short port = (unsigned short)POPu;
4858 if (proto && !*proto)
4862 port = PerlSock_htons(port);
4864 sent = PerlSock_getservbyport(port, proto);
4866 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4870 #ifdef HAS_GETSERVENT
4871 sent = PerlSock_getservent();
4873 DIE(aTHX_ PL_no_sock_func, "getservent");
4877 if (GIMME != G_ARRAY) {
4878 PUSHs(sv = sv_newmortal());
4880 if (which == OP_GSBYNAME) {
4882 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4884 sv_setiv(sv, (IV)(sent->s_port));
4888 sv_setpv(sv, sent->s_name);
4894 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4895 sv_setpv(sv, sent->s_name);
4896 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4897 for (elem = sent->s_aliases; elem && *elem; elem++) {
4898 sv_catpv(sv, *elem);
4900 sv_catpvn(sv, " ", 1);
4902 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4904 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4906 sv_setiv(sv, (IV)(sent->s_port));
4908 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4909 sv_setpv(sv, sent->s_proto);
4914 DIE(aTHX_ PL_no_sock_func, "getservent");
4920 #ifdef HAS_SETHOSTENT
4922 PerlSock_sethostent(TOPi);
4925 DIE(aTHX_ PL_no_sock_func, "sethostent");
4931 #ifdef HAS_SETNETENT
4933 PerlSock_setnetent(TOPi);
4936 DIE(aTHX_ PL_no_sock_func, "setnetent");
4942 #ifdef HAS_SETPROTOENT
4944 PerlSock_setprotoent(TOPi);
4947 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4953 #ifdef HAS_SETSERVENT
4955 PerlSock_setservent(TOPi);
4958 DIE(aTHX_ PL_no_sock_func, "setservent");
4964 #ifdef HAS_ENDHOSTENT
4966 PerlSock_endhostent();
4970 DIE(aTHX_ PL_no_sock_func, "endhostent");
4976 #ifdef HAS_ENDNETENT
4978 PerlSock_endnetent();
4982 DIE(aTHX_ PL_no_sock_func, "endnetent");
4988 #ifdef HAS_ENDPROTOENT
4990 PerlSock_endprotoent();
4994 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5000 #ifdef HAS_ENDSERVENT
5002 PerlSock_endservent();
5006 DIE(aTHX_ PL_no_sock_func, "endservent");
5014 I32 which = PL_op->op_type;
5016 struct passwd *pwent = NULL;
5018 * We currently support only the SysV getsp* shadow password interface.
5019 * The interface is declared in <shadow.h> and often one needs to link
5020 * with -lsecurity or some such.
5021 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5024 * AIX getpwnam() is clever enough to return the encrypted password
5025 * only if the caller (euid?) is root.
5027 * There are at least three other shadow password APIs. Many platforms
5028 * seem to contain more than one interface for accessing the shadow
5029 * password databases, possibly for compatibility reasons.
5030 * The getsp*() is by far he simplest one, the other two interfaces
5031 * are much more complicated, but also very similar to each other.
5036 * struct pr_passwd *getprpw*();
5037 * The password is in
5038 * char getprpw*(...).ufld.fd_encrypt[]
5039 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5044 * struct es_passwd *getespw*();
5045 * The password is in
5046 * char *(getespw*(...).ufld.fd_encrypt)
5047 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5050 * struct userpw *getuserpw();
5051 * The password is in
5052 * char *(getuserpw(...)).spw_upw_passwd
5053 * (but the de facto standard getpwnam() should work okay)
5055 * Mention I_PROT here so that Configure probes for it.
5057 * In HP-UX for getprpw*() the manual page claims that one should include
5058 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5059 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5060 * and pp_sys.c already includes <shadow.h> if there is such.
5062 * Note that <sys/security.h> is already probed for, but currently
5063 * it is only included in special cases.
5065 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5066 * be preferred interface, even though also the getprpw*() interface
5067 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5068 * One also needs to call set_auth_parameters() in main() before
5069 * doing anything else, whether one is using getespw*() or getprpw*().
5071 * Note that accessing the shadow databases can be magnitudes
5072 * slower than accessing the standard databases.
5077 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5078 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5079 * the pw_comment is left uninitialized. */
5080 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5086 char* name = POPpbytex;
5087 pwent = getpwnam(name);
5093 pwent = getpwuid(uid);
5097 # ifdef HAS_GETPWENT
5099 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5100 if (pwent) pwent = getpwnam(pwent->pw_name);
5103 DIE(aTHX_ PL_no_func, "getpwent");
5109 if (GIMME != G_ARRAY) {
5110 PUSHs(sv = sv_newmortal());
5112 if (which == OP_GPWNAM)
5113 # if Uid_t_sign <= 0
5114 sv_setiv(sv, (IV)pwent->pw_uid);
5116 sv_setuv(sv, (UV)pwent->pw_uid);
5119 sv_setpv(sv, pwent->pw_name);
5125 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5126 sv_setpv(sv, pwent->pw_name);
5128 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5130 /* If we have getspnam(), we try to dig up the shadow
5131 * password. If we are underprivileged, the shadow
5132 * interface will set the errno to EACCES or similar,
5133 * and return a null pointer. If this happens, we will
5134 * use the dummy password (usually "*" or "x") from the
5135 * standard password database.
5137 * In theory we could skip the shadow call completely
5138 * if euid != 0 but in practice we cannot know which
5139 * security measures are guarding the shadow databases
5140 * on a random platform.
5142 * Resist the urge to use additional shadow interfaces.
5143 * Divert the urge to writing an extension instead.
5146 /* Some AIX setups falsely(?) detect some getspnam(), which
5147 * has a different API than the Solaris/IRIX one. */
5148 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5150 struct spwd *spwent;
5151 int saverrno; /* Save and restore errno so that
5152 * underprivileged attempts seem
5153 * to have never made the unsccessful
5154 * attempt to retrieve the shadow password. */
5157 spwent = getspnam(pwent->pw_name);
5159 if (spwent && spwent->sp_pwdp)
5160 sv_setpv(sv, spwent->sp_pwdp);
5164 if (!SvPOK(sv)) /* Use the standard password, then. */
5165 sv_setpv(sv, pwent->pw_passwd);
5168 # ifndef INCOMPLETE_TAINTS
5169 /* passwd is tainted because user himself can diddle with it.
5170 * admittedly not much and in a very limited way, but nevertheless. */
5174 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5175 # if Uid_t_sign <= 0
5176 sv_setiv(sv, (IV)pwent->pw_uid);
5178 sv_setuv(sv, (UV)pwent->pw_uid);
5181 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5182 # if Uid_t_sign <= 0
5183 sv_setiv(sv, (IV)pwent->pw_gid);
5185 sv_setuv(sv, (UV)pwent->pw_gid);
5187 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5188 * because of the poor interface of the Perl getpw*(),
5189 * not because there's some standard/convention saying so.
5190 * A better interface would have been to return a hash,
5191 * but we are accursed by our history, alas. --jhi. */
5192 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5194 sv_setiv(sv, (IV)pwent->pw_change);
5197 sv_setiv(sv, (IV)pwent->pw_quota);
5200 sv_setpv(sv, pwent->pw_age);
5205 /* pw_class and pw_comment are mutually exclusive--.
5206 * see the above note for pw_change, pw_quota, and pw_age. */
5207 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5209 sv_setpv(sv, pwent->pw_class);
5212 sv_setpv(sv, pwent->pw_comment);
5216 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5218 sv_setpv(sv, pwent->pw_gecos);
5220 # ifndef INCOMPLETE_TAINTS
5221 /* pw_gecos is tainted because user himself can diddle with it. */
5225 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5226 sv_setpv(sv, pwent->pw_dir);
5228 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5229 sv_setpv(sv, pwent->pw_shell);
5230 # ifndef INCOMPLETE_TAINTS
5231 /* pw_shell is tainted because user himself can diddle with it. */
5236 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5237 sv_setiv(sv, (IV)pwent->pw_expire);
5242 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5248 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5253 DIE(aTHX_ PL_no_func, "setpwent");
5259 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5264 DIE(aTHX_ PL_no_func, "endpwent");
5272 I32 which = PL_op->op_type;
5273 register char **elem;
5275 struct group *grent;
5277 if (which == OP_GGRNAM) {
5278 char* name = POPpbytex;
5279 grent = (struct group *)getgrnam(name);
5281 else if (which == OP_GGRGID) {
5283 grent = (struct group *)getgrgid(gid);
5287 grent = (struct group *)getgrent();
5289 DIE(aTHX_ PL_no_func, "getgrent");
5293 if (GIMME != G_ARRAY) {
5294 PUSHs(sv = sv_newmortal());
5296 if (which == OP_GGRNAM)
5297 sv_setiv(sv, (IV)grent->gr_gid);
5299 sv_setpv(sv, grent->gr_name);
5305 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5306 sv_setpv(sv, grent->gr_name);
5308 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5310 sv_setpv(sv, grent->gr_passwd);
5313 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5314 sv_setiv(sv, (IV)grent->gr_gid);
5316 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5317 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5318 /* In UNICOS/mk (_CRAYMPP) the multithreading
5319 * versions (getgrnam_r, getgrgid_r)
5320 * seem to return an illegal pointer
5321 * as the group members list, gr_mem.
5322 * getgrent() doesn't even have a _r version
5323 * but the gr_mem is poisonous anyway.
5324 * So yes, you cannot get the list of group
5325 * members if building multithreaded in UNICOS/mk. */
5326 for (elem = grent->gr_mem; elem && *elem; elem++) {
5327 sv_catpv(sv, *elem);
5329 sv_catpvn(sv, " ", 1);
5336 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5342 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5347 DIE(aTHX_ PL_no_func, "setgrent");
5353 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5358 DIE(aTHX_ PL_no_func, "endgrent");
5368 if (!(tmps = PerlProc_getlogin()))
5370 PUSHp(tmps, strlen(tmps));
5373 DIE(aTHX_ PL_no_func, "getlogin");
5377 /* Miscellaneous. */
5382 dSP; dMARK; dORIGMARK; dTARGET;
5383 register I32 items = SP - MARK;
5384 unsigned long a[20];
5389 while (++MARK <= SP) {
5390 if (SvTAINTED(*MARK)) {
5396 TAINT_PROPER("syscall");
5399 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5400 * or where sizeof(long) != sizeof(char*). But such machines will
5401 * not likely have syscall implemented either, so who cares?
5403 while (++MARK <= SP) {
5404 if (SvNIOK(*MARK) || !i)
5405 a[i++] = SvIV(*MARK);
5406 else if (*MARK == &PL_sv_undef)
5409 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5415 DIE(aTHX_ "Too many args to syscall");
5417 DIE(aTHX_ "Too few args to syscall");
5419 retval = syscall(a[0]);
5422 retval = syscall(a[0],a[1]);
5425 retval = syscall(a[0],a[1],a[2]);
5428 retval = syscall(a[0],a[1],a[2],a[3]);
5431 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5434 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5437 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5440 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5444 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5447 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5450 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5454 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5458 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5462 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5463 a[10],a[11],a[12],a[13]);
5465 #endif /* atarist */
5471 DIE(aTHX_ PL_no_func, "syscall");
5475 #ifdef FCNTL_EMULATE_FLOCK
5477 /* XXX Emulate flock() with fcntl().
5478 What's really needed is a good file locking module.
5482 fcntl_emulate_flock(int fd, int operation)
5486 switch (operation & ~LOCK_NB) {
5488 flock.l_type = F_RDLCK;
5491 flock.l_type = F_WRLCK;
5494 flock.l_type = F_UNLCK;
5500 flock.l_whence = SEEK_SET;
5501 flock.l_start = flock.l_len = (Off_t)0;
5503 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5506 #endif /* FCNTL_EMULATE_FLOCK */
5508 #ifdef LOCKF_EMULATE_FLOCK
5510 /* XXX Emulate flock() with lockf(). This is just to increase
5511 portability of scripts. The calls are not completely
5512 interchangeable. What's really needed is a good file
5516 /* The lockf() constants might have been defined in <unistd.h>.
5517 Unfortunately, <unistd.h> causes troubles on some mixed
5518 (BSD/POSIX) systems, such as SunOS 4.1.3.
5520 Further, the lockf() constants aren't POSIX, so they might not be
5521 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5522 just stick in the SVID values and be done with it. Sigh.
5526 # define F_ULOCK 0 /* Unlock a previously locked region */
5529 # define F_LOCK 1 /* Lock a region for exclusive use */
5532 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5535 # define F_TEST 3 /* Test a region for other processes locks */
5539 lockf_emulate_flock(int fd, int operation)
5545 /* flock locks entire file so for lockf we need to do the same */
5547 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5548 if (pos > 0) /* is seekable and needs to be repositioned */
5549 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5550 pos = -1; /* seek failed, so don't seek back afterwards */
5553 switch (operation) {
5555 /* LOCK_SH - get a shared lock */
5557 /* LOCK_EX - get an exclusive lock */
5559 i = lockf (fd, F_LOCK, 0);
5562 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5563 case LOCK_SH|LOCK_NB:
5564 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5565 case LOCK_EX|LOCK_NB:
5566 i = lockf (fd, F_TLOCK, 0);
5568 if ((errno == EAGAIN) || (errno == EACCES))
5569 errno = EWOULDBLOCK;
5572 /* LOCK_UN - unlock (non-blocking is a no-op) */
5574 case LOCK_UN|LOCK_NB:
5575 i = lockf (fd, F_ULOCK, 0);
5578 /* Default - can't decipher operation */
5585 if (pos > 0) /* need to restore position of the handle */
5586 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5591 #endif /* LOCKF_EMULATE_FLOCK */
5595 * c-indentation-style: bsd
5597 * indent-tabs-mode: t
5600 * ex: set ts=8 sts=4 sw=4 noet: