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_R_OK /* EFFective uid/gid ACCESS R_OK */
201 #undef PERL_EFF_ACCESS_W_OK
202 #undef PERL_EFF_ACCESS_X_OK
204 /* AIX 5.2 and below use mktime for localtime, and defines the edge case
205 * for time 0x7fffffff to be valid only in UTC. AIX 5.3 provides localtime64
206 * available in the 32bit environment, which could warrant Configure
207 * checks in the future.
210 #define LOCALTIME_EDGECASE_BROKEN
213 /* F_OK unused: if stat() cannot find it... */
215 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
216 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
217 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
218 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
219 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
222 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
223 # ifdef I_SYS_SECURITY
224 # include <sys/security.h>
228 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
229 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
230 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
233 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
234 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
235 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
239 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
241 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
242 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
243 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
246 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
247 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
248 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
251 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
253 Uid_t ruid = getuid();
254 Uid_t euid = geteuid();
255 Gid_t rgid = getgid();
256 Gid_t egid = getegid();
260 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
261 Perl_croak(aTHX_ "switching effective uid is not implemented");
264 if (setreuid(euid, ruid))
267 if (setresuid(euid, ruid, (Uid_t)-1))
270 Perl_croak(aTHX_ "entering effective uid failed");
273 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
274 Perl_croak(aTHX_ "switching effective gid is not implemented");
277 if (setregid(egid, rgid))
280 if (setresgid(egid, rgid, (Gid_t)-1))
283 Perl_croak(aTHX_ "entering effective gid failed");
286 res = access(path, mode);
289 if (setreuid(ruid, euid))
292 if (setresuid(ruid, euid, (Uid_t)-1))
295 Perl_croak(aTHX_ "leaving effective uid failed");
298 if (setregid(rgid, egid))
301 if (setresgid(rgid, egid, (Gid_t)-1))
304 Perl_croak(aTHX_ "leaving effective gid failed");
309 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
310 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
311 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
314 #if !defined(PERL_EFF_ACCESS_R_OK)
315 /* With it or without it: anyway you get a warning: either that
316 it is unused, or it is declared static and never defined.
319 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
323 Perl_croak(aTHX_ "switching effective uid is not implemented");
333 const char * const tmps = POPpconstx;
334 const I32 gimme = GIMME_V;
335 const char *mode = "r";
338 if (PL_op->op_private & OPpOPEN_IN_RAW)
340 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
342 fp = PerlProc_popen(tmps, mode);
344 const char *type = NULL;
345 if (PL_curcop->cop_io) {
346 type = SvPV_nolen_const(PL_curcop->cop_io);
349 PerlIO_apply_layers(aTHX_ fp,mode,type);
351 if (gimme == G_VOID) {
353 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
356 else if (gimme == G_SCALAR) {
359 PL_rs = &PL_sv_undef;
360 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
361 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
369 SV * const sv = NEWSV(56, 79);
370 if (sv_gets(sv, fp, 0) == Nullch) {
374 XPUSHs(sv_2mortal(sv));
375 if (SvLEN(sv) - SvCUR(sv) > 20) {
376 SvPV_shrink_to_cur(sv);
381 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
382 TAINT; /* "I believe that this is not gratuitous!" */
385 STATUS_NATIVE_CHILD_SET(-1);
386 if (gimme == G_SCALAR)
397 tryAMAGICunTARGET(iter, -1);
399 /* Note that we only ever get here if File::Glob fails to load
400 * without at the same time croaking, for some reason, or if
401 * perl was built with PERL_EXTERNAL_GLOB */
408 * The external globbing program may use things we can't control,
409 * so for security reasons we must assume the worst.
412 taint_proper(PL_no_security, "glob");
416 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
417 PL_last_in_gv = (GV*)*PL_stack_sp--;
419 SAVESPTR(PL_rs); /* This is not permanent, either. */
420 PL_rs = sv_2mortal(newSVpvn("\000", 1));
423 *SvPVX(PL_rs) = '\n';
427 result = do_readline();
434 PL_last_in_gv = cGVOP_gv;
435 return do_readline();
444 if (SP - MARK != 1) {
446 do_join(TARG, &PL_sv_no, MARK, SP);
453 tmps = SvPV_const(tmpsv, len);
454 if ((!tmps || !len) && PL_errgv) {
455 SV * const error = ERRSV;
456 SvUPGRADE(error, SVt_PV);
457 if (SvPOK(error) && SvCUR(error))
458 sv_catpv(error, "\t...caught");
460 tmps = SvPV_const(tmpsv, len);
463 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
465 Perl_warn(aTHX_ "%"SVf, tmpsv);
477 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
479 if (SP - MARK != 1) {
481 do_join(TARG, &PL_sv_no, MARK, SP);
483 tmps = SvPV_const(tmpsv, len);
489 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
493 SvUPGRADE(error, SVt_PV);
494 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
496 SvSetSV(error,tmpsv);
497 else if (sv_isobject(error)) {
498 HV *stash = SvSTASH(SvRV(error));
499 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
501 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
502 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
509 call_sv((SV*)GvCV(gv),
510 G_SCALAR|G_EVAL|G_KEEPERR);
511 sv_setsv(error,*PL_stack_sp--);
517 if (SvPOK(error) && SvCUR(error))
518 sv_catpv(error, "\t...propagated");
521 tmps = SvPV_const(tmpsv, len);
527 tmpsv = sv_2mortal(newSVpvn("Died", 4));
529 DIE(aTHX_ "%"SVf, tmpsv);
549 DIE(aTHX_ PL_no_usym, "filehandle");
550 if ((io = GvIOp(gv)))
551 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
553 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
554 /* Method's args are same as ours ... */
555 /* ... except handle is replaced by the object */
556 *MARK-- = SvTIED_obj((SV*)io, mg);
560 call_method("OPEN", G_SCALAR);
573 tmps = SvPV_const(sv, len);
574 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
577 PUSHi( (I32)PL_forkprocess );
578 else if (PL_forkprocess == 0) /* we are a new child */
597 if (gv && (io = GvIO(gv))
598 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
601 XPUSHs(SvTIED_obj((SV*)io, mg));
604 call_method("CLOSE", G_SCALAR);
610 PUSHs(boolSV(do_close(gv, TRUE)));
630 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
631 DIE(aTHX_ PL_no_usym, "filehandle");
636 do_close(rgv, FALSE);
638 do_close(wgv, FALSE);
640 if (PerlProc_pipe(fd) < 0)
643 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
644 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
645 IoOFP(rstio) = IoIFP(rstio);
646 IoIFP(wstio) = IoOFP(wstio);
647 IoTYPE(rstio) = IoTYPE_RDONLY;
648 IoTYPE(wstio) = IoTYPE_WRONLY;
650 if (!IoIFP(rstio) || !IoOFP(wstio)) {
651 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
652 else PerlLIO_close(fd[0]);
653 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
654 else PerlLIO_close(fd[1]);
657 #if defined(HAS_FCNTL) && defined(F_SETFD)
658 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
659 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
666 DIE(aTHX_ PL_no_func, "pipe");
682 if (gv && (io = GvIO(gv))
683 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
686 XPUSHs(SvTIED_obj((SV*)io, mg));
689 call_method("FILENO", G_SCALAR);
695 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
696 /* Can't do this because people seem to do things like
697 defined(fileno($foo)) to check whether $foo is a valid fh.
698 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
699 report_evil_fh(gv, io, PL_op->op_type);
704 PUSHi(PerlIO_fileno(fp));
716 anum = PerlLIO_umask(0);
717 (void)PerlLIO_umask(anum);
720 anum = PerlLIO_umask(POPi);
721 TAINT_PROPER("umask");
724 /* Only DIE if trying to restrict permissions on "user" (self).
725 * Otherwise it's harmless and more useful to just return undef
726 * since 'group' and 'other' concepts probably don't exist here. */
727 if (MAXARG >= 1 && (POPi & 0700))
728 DIE(aTHX_ "umask not implemented");
729 XPUSHs(&PL_sv_undef);
751 if (gv && (io = GvIO(gv))
752 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
755 XPUSHs(SvTIED_obj((SV*)io, mg));
760 call_method("BINMODE", G_SCALAR);
767 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
768 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
769 report_evil_fh(gv, io, PL_op->op_type);
770 SETERRNO(EBADF,RMS_IFI);
775 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
776 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
777 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
778 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
779 mode_from_discipline(discp),
780 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
801 const I32 markoff = MARK - PL_stack_base;
802 const char *methname;
803 int how = PERL_MAGIC_tied;
807 switch(SvTYPE(varsv)) {
809 methname = "TIEHASH";
810 HvEITER_set((HV *)varsv, 0);
813 methname = "TIEARRAY";
816 #ifdef GV_UNIQUE_CHECK
817 if (GvUNIQUE((GV*)varsv)) {
818 Perl_croak(aTHX_ "Attempt to tie unique GV");
821 methname = "TIEHANDLE";
822 how = PERL_MAGIC_tiedscalar;
823 /* For tied filehandles, we apply tiedscalar magic to the IO
824 slot of the GP rather than the GV itself. AMS 20010812 */
826 GvIOp(varsv) = newIO();
827 varsv = (SV *)GvIOp(varsv);
830 methname = "TIESCALAR";
831 how = PERL_MAGIC_tiedscalar;
835 if (sv_isobject(*MARK)) {
837 PUSHSTACKi(PERLSI_MAGIC);
839 EXTEND(SP,(I32)items);
843 call_method(methname, G_SCALAR);
846 /* Not clear why we don't call call_method here too.
847 * perhaps to get different error message ?
849 stash = gv_stashsv(*MARK, FALSE);
850 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
851 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
855 PUSHSTACKi(PERLSI_MAGIC);
857 EXTEND(SP,(I32)items);
861 call_sv((SV*)GvCV(gv), G_SCALAR);
867 if (sv_isobject(sv)) {
868 sv_unmagic(varsv, how);
869 /* Croak if a self-tie on an aggregate is attempted. */
870 if (varsv == SvRV(sv) &&
871 (SvTYPE(varsv) == SVt_PVAV ||
872 SvTYPE(varsv) == SVt_PVHV))
874 "Self-ties of arrays and hashes are not supported");
875 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
878 SP = PL_stack_base + markoff;
888 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
889 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
891 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
894 if ((mg = SvTIED_mg(sv, how))) {
895 SV * const obj = SvRV(SvTIED_obj(sv, mg));
899 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
900 isGV(gv) && (cv = GvCV(gv))) {
902 XPUSHs(SvTIED_obj((SV*)gv, mg));
903 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
906 call_sv((SV *)cv, G_VOID);
910 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
911 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
912 "untie attempted while %"UVuf" inner references still exist",
913 (UV)SvREFCNT(obj) - 1 ) ;
917 sv_unmagic(sv, how) ;
926 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
927 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
929 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
932 if ((mg = SvTIED_mg(sv, how))) {
933 SV *osv = SvTIED_obj(sv, mg);
934 if (osv == mg->mg_obj)
935 osv = sv_mortalcopy(osv);
950 HV * const hv = (HV*)POPs;
952 sv = sv_mortalcopy(&PL_sv_no);
953 sv_setpv(sv, "AnyDBM_File");
954 stash = gv_stashsv(sv, FALSE);
955 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
957 require_pv("AnyDBM_File.pm");
959 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
960 DIE(aTHX_ "No dbm on this machine");
970 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
972 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
975 call_sv((SV*)GvCV(gv), G_SCALAR);
978 if (!sv_isobject(TOPs)) {
983 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
986 call_sv((SV*)GvCV(gv), G_SCALAR);
990 if (sv_isobject(TOPs)) {
991 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
992 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
1014 struct timeval timebuf;
1015 struct timeval *tbuf = &timebuf;
1018 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1023 # if BYTEORDER & 0xf0000
1024 # define ORDERBYTE (0x88888888 - BYTEORDER)
1026 # define ORDERBYTE (0x4444 - BYTEORDER)
1032 for (i = 1; i <= 3; i++) {
1034 if (SvOK(sv) && SvREADONLY(sv)) {
1036 sv_force_normal_flags(sv, 0);
1038 DIE(aTHX_ PL_no_modify);
1047 /* little endians can use vecs directly */
1048 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1055 masksize = NFDBITS / NBBY;
1057 masksize = sizeof(long); /* documented int, everyone seems to use long */
1059 Zero(&fd_sets[0], 4, char*);
1062 # if SELECT_MIN_BITS == 1
1063 growsize = sizeof(fd_set);
1065 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1066 # undef SELECT_MIN_BITS
1067 # define SELECT_MIN_BITS __FD_SETSIZE
1069 /* If SELECT_MIN_BITS is greater than one we most probably will want
1070 * to align the sizes with SELECT_MIN_BITS/8 because for example
1071 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1072 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1073 * on (sets/tests/clears bits) is 32 bits. */
1074 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1082 timebuf.tv_sec = (long)value;
1083 value -= (NV)timebuf.tv_sec;
1084 timebuf.tv_usec = (long)(value * 1000000.0);
1087 tbuf = Null(struct timeval*);
1089 for (i = 1; i <= 3; i++) {
1095 else if (!SvPOK(sv))
1096 SvPV_force_nolen(sv); /* force string conversion */
1099 Sv_Grow(sv, growsize);
1103 while (++j <= growsize) {
1107 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1109 Newx(fd_sets[i], growsize, char);
1110 for (offset = 0; offset < growsize; offset += masksize) {
1111 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1112 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1115 fd_sets[i] = SvPVX(sv);
1119 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1120 /* Can't make just the (void*) conditional because that would be
1121 * cpp #if within cpp macro, and not all compilers like that. */
1122 nfound = PerlSock_select(
1124 (Select_fd_set_t) fd_sets[1],
1125 (Select_fd_set_t) fd_sets[2],
1126 (Select_fd_set_t) fd_sets[3],
1127 (void*) tbuf); /* Workaround for compiler bug. */
1129 nfound = PerlSock_select(
1131 (Select_fd_set_t) fd_sets[1],
1132 (Select_fd_set_t) fd_sets[2],
1133 (Select_fd_set_t) fd_sets[3],
1136 for (i = 1; i <= 3; i++) {
1139 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1141 for (offset = 0; offset < growsize; offset += masksize) {
1142 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1143 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1145 Safefree(fd_sets[i]);
1152 if (GIMME == G_ARRAY && tbuf) {
1153 value = (NV)(timebuf.tv_sec) +
1154 (NV)(timebuf.tv_usec) / 1000000.0;
1155 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1156 sv_setnv(sv, value);
1160 DIE(aTHX_ "select not implemented");
1165 Perl_setdefout(pTHX_ GV *gv)
1168 (void)SvREFCNT_inc(gv);
1170 SvREFCNT_dec(PL_defoutgv);
1180 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1182 egv = GvEGV(PL_defoutgv);
1187 XPUSHs(&PL_sv_undef);
1189 GV ** const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1190 if (gvp && *gvp == egv) {
1191 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1195 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1200 if (!GvIO(newdefout))
1201 gv_IOadd(newdefout);
1202 setdefout(newdefout);
1213 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1215 if (gv && (io = GvIO(gv))
1216 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1218 const I32 gimme = GIMME_V;
1220 XPUSHs(SvTIED_obj((SV*)io, mg));
1223 call_method("GETC", gimme);
1226 if (gimme == G_SCALAR)
1227 SvSetMagicSV_nosteal(TARG, TOPs);
1230 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1231 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1232 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1233 report_evil_fh(gv, io, PL_op->op_type);
1234 SETERRNO(EBADF,RMS_IFI);
1238 sv_setpvn(TARG, " ", 1);
1239 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1240 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1241 /* Find out how many bytes the char needs */
1242 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1245 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1246 SvCUR_set(TARG,1+len);
1256 return pp_sysread();
1260 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1263 register PERL_CONTEXT *cx;
1264 const I32 gimme = GIMME_V;
1269 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1271 cx->blk_sub.retop = retop;
1273 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1275 setdefout(gv); /* locally select filehandle so $% et al work */
1307 SV * const tmpsv = sv_newmortal();
1309 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1310 name = SvPV_nolen_const(tmpsv);
1312 DIE(aTHX_ "Undefined format \"%s\" called", name);
1314 DIE(aTHX_ "Not a format reference");
1317 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1319 IoFLAGS(io) &= ~IOf_DIDTOP;
1320 return doform(cv,gv,PL_op->op_next);
1326 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1327 register IO * const io = GvIOp(gv);
1328 PerlIO * const ofp = IoOFP(io);
1332 register PERL_CONTEXT *cx;
1334 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1335 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1338 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1339 PL_formtarget != PL_toptarget)
1343 if (!IoTOP_GV(io)) {
1346 if (!IoTOP_NAME(io)) {
1348 if (!IoFMT_NAME(io))
1349 IoFMT_NAME(io) = savepv(GvNAME(gv));
1350 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1351 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1352 if ((topgv && GvFORM(topgv)) ||
1353 !gv_fetchpv("top",FALSE,SVt_PVFM))
1354 IoTOP_NAME(io) = savesvpv(topname);
1356 IoTOP_NAME(io) = savepvn("top", 3);
1358 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1359 if (!topgv || !GvFORM(topgv)) {
1360 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1363 IoTOP_GV(io) = topgv;
1365 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1366 I32 lines = IoLINES_LEFT(io);
1367 const char *s = SvPVX_const(PL_formtarget);
1368 if (lines <= 0) /* Yow, header didn't even fit!!! */
1370 while (lines-- > 0) {
1371 s = strchr(s, '\n');
1377 const STRLEN save = SvCUR(PL_formtarget);
1378 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1379 do_print(PL_formtarget, ofp);
1380 SvCUR_set(PL_formtarget, save);
1381 sv_chop(PL_formtarget, s);
1382 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1385 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1386 do_print(PL_formfeed, ofp);
1387 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1389 PL_formtarget = PL_toptarget;
1390 IoFLAGS(io) |= IOf_DIDTOP;
1393 DIE(aTHX_ "bad top format reference");
1396 SV * const sv = sv_newmortal();
1398 gv_efullname4(sv, fgv, Nullch, FALSE);
1399 name = SvPV_nolen_const(sv);
1401 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1405 DIE(aTHX_ "Undefined top format called");
1408 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1409 return doform(cv,gv,PL_op);
1413 POPBLOCK(cx,PL_curpm);
1419 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1421 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1422 else if (ckWARN(WARN_CLOSED))
1423 report_evil_fh(gv, io, PL_op->op_type);
1428 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1429 if (ckWARN(WARN_IO))
1430 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1432 if (!do_print(PL_formtarget, fp))
1435 FmLINES(PL_formtarget) = 0;
1436 SvCUR_set(PL_formtarget, 0);
1437 *SvEND(PL_formtarget) = '\0';
1438 if (IoFLAGS(io) & IOf_FLUSH)
1439 (void)PerlIO_flush(fp);
1444 PL_formtarget = PL_bodytarget;
1446 PERL_UNUSED_VAR(newsp);
1447 PERL_UNUSED_VAR(gimme);
1448 return cx->blk_sub.retop;
1453 dVAR; dSP; dMARK; dORIGMARK;
1460 if (PL_op->op_flags & OPf_STACKED)
1465 if (gv && (io = GvIO(gv))
1466 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1468 if (MARK == ORIGMARK) {
1471 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1475 *MARK = SvTIED_obj((SV*)io, mg);
1478 call_method("PRINTF", G_SCALAR);
1481 MARK = ORIGMARK + 1;
1488 if (!(io = GvIO(gv))) {
1489 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1490 report_evil_fh(gv, io, PL_op->op_type);
1491 SETERRNO(EBADF,RMS_IFI);
1494 else if (!(fp = IoOFP(io))) {
1495 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1497 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1498 else if (ckWARN(WARN_CLOSED))
1499 report_evil_fh(gv, io, PL_op->op_type);
1501 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1505 do_sprintf(sv, SP - MARK, MARK + 1);
1506 if (!do_print(sv, fp))
1509 if (IoFLAGS(io) & IOf_FLUSH)
1510 if (PerlIO_flush(fp) == EOF)
1521 PUSHs(&PL_sv_undef);
1528 const int perm = (MAXARG > 3) ? POPi : 0666;
1529 const int mode = POPi;
1530 SV * const sv = POPs;
1531 GV * const gv = (GV *)POPs;
1534 /* Need TIEHANDLE method ? */
1535 const char * const tmps = SvPV_const(sv, len);
1536 /* FIXME? do_open should do const */
1537 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1538 IoLINES(GvIOp(gv)) = 0;
1542 PUSHs(&PL_sv_undef);
1549 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1555 Sock_size_t bufsize;
1563 bool charstart = FALSE;
1564 STRLEN charskip = 0;
1567 GV * const gv = (GV*)*++MARK;
1568 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1569 && gv && (io = GvIO(gv)) )
1571 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1575 *MARK = SvTIED_obj((SV*)io, mg);
1577 call_method("READ", G_SCALAR);
1591 sv_setpvn(bufsv, "", 0);
1592 length = SvIVx(*++MARK);
1595 offset = SvIVx(*++MARK);
1599 if (!io || !IoIFP(io)) {
1600 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1601 report_evil_fh(gv, io, PL_op->op_type);
1602 SETERRNO(EBADF,RMS_IFI);
1605 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1606 buffer = SvPVutf8_force(bufsv, blen);
1607 /* UTF-8 may not have been set if they are all low bytes */
1612 buffer = SvPV_force(bufsv, blen);
1613 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1616 DIE(aTHX_ "Negative length");
1624 if (PL_op->op_type == OP_RECV) {
1625 char namebuf[MAXPATHLEN];
1626 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1627 bufsize = sizeof (struct sockaddr_in);
1629 bufsize = sizeof namebuf;
1631 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1635 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1636 /* 'offset' means 'flags' here */
1637 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1638 (struct sockaddr *)namebuf, &bufsize);
1642 /* Bogus return without padding */
1643 bufsize = sizeof (struct sockaddr_in);
1645 SvCUR_set(bufsv, count);
1646 *SvEND(bufsv) = '\0';
1647 (void)SvPOK_only(bufsv);
1651 /* This should not be marked tainted if the fp is marked clean */
1652 if (!(IoFLAGS(io) & IOf_UNTAINT))
1653 SvTAINTED_on(bufsv);
1655 sv_setpvn(TARG, namebuf, bufsize);
1660 if (PL_op->op_type == OP_RECV)
1661 DIE(aTHX_ PL_no_sock_func, "recv");
1663 if (DO_UTF8(bufsv)) {
1664 /* offset adjust in characters not bytes */
1665 blen = sv_len_utf8(bufsv);
1668 if (-offset > (int)blen)
1669 DIE(aTHX_ "Offset outside string");
1672 if (DO_UTF8(bufsv)) {
1673 /* convert offset-as-chars to offset-as-bytes */
1674 if (offset >= (int)blen)
1675 offset += SvCUR(bufsv) - blen;
1677 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1680 bufsize = SvCUR(bufsv);
1681 /* Allocating length + offset + 1 isn't perfect in the case of reading
1682 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1684 (should be 2 * length + offset + 1, or possibly something longer if
1685 PL_encoding is true) */
1686 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1687 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1688 Zero(buffer+bufsize, offset-bufsize, char);
1690 buffer = buffer + offset;
1692 read_target = bufsv;
1694 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1695 concatenate it to the current buffer. */
1697 /* Truncate the existing buffer to the start of where we will be
1699 SvCUR_set(bufsv, offset);
1701 read_target = sv_newmortal();
1702 SvUPGRADE(read_target, SVt_PV);
1703 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1706 if (PL_op->op_type == OP_SYSREAD) {
1707 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1708 if (IoTYPE(io) == IoTYPE_SOCKET) {
1709 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1715 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1720 #ifdef HAS_SOCKET__bad_code_maybe
1721 if (IoTYPE(io) == IoTYPE_SOCKET) {
1722 char namebuf[MAXPATHLEN];
1723 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1724 bufsize = sizeof (struct sockaddr_in);
1726 bufsize = sizeof namebuf;
1728 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1729 (struct sockaddr *)namebuf, &bufsize);
1734 count = PerlIO_read(IoIFP(io), buffer, length);
1735 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1736 if (count == 0 && PerlIO_error(IoIFP(io)))
1740 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1741 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1744 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1745 *SvEND(read_target) = '\0';
1746 (void)SvPOK_only(read_target);
1747 if (fp_utf8 && !IN_BYTES) {
1748 /* Look at utf8 we got back and count the characters */
1749 const char *bend = buffer + count;
1750 while (buffer < bend) {
1752 skip = UTF8SKIP(buffer);
1755 if (buffer - charskip + skip > bend) {
1756 /* partial character - try for rest of it */
1757 length = skip - (bend-buffer);
1758 offset = bend - SvPVX_const(bufsv);
1770 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1771 provided amount read (count) was what was requested (length)
1773 if (got < wanted && count == length) {
1774 length = wanted - got;
1775 offset = bend - SvPVX_const(bufsv);
1778 /* return value is character count */
1782 else if (buffer_utf8) {
1783 /* Let svcatsv upgrade the bytes we read in to utf8.
1784 The buffer is a mortal so will be freed soon. */
1785 sv_catsv_nomg(bufsv, read_target);
1788 /* This should not be marked tainted if the fp is marked clean */
1789 if (!(IoFLAGS(io) & IOf_UNTAINT))
1790 SvTAINTED_on(bufsv);
1803 const int items = (SP - PL_stack_base) - TOPMARK;
1807 sv = sv_2mortal(newSViv(sv_len(*SP)));
1816 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1827 if (PL_op->op_type == OP_SYSWRITE
1828 && gv && (io = GvIO(gv))
1829 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1834 *MARK = SvTIED_obj((SV*)io, mg);
1836 call_method("WRITE", G_SCALAR);
1847 #if Size_t_size > IVSIZE
1848 length = (Size_t)SvNVx(*++MARK);
1850 length = (Size_t)SvIVx(*++MARK);
1852 if ((SSize_t)length < 0)
1853 DIE(aTHX_ "Negative length");
1856 if (!io || !IoIFP(io)) {
1858 if (ckWARN(WARN_CLOSED))
1859 report_evil_fh(gv, io, PL_op->op_type);
1860 SETERRNO(EBADF,RMS_IFI);
1864 if (PerlIO_isutf8(IoIFP(io))) {
1865 if (!SvUTF8(bufsv)) {
1866 bufsv = sv_2mortal(newSVsv(bufsv));
1867 buffer = sv_2pvutf8(bufsv, &blen);
1869 buffer = SvPV_const(bufsv, blen);
1872 if (DO_UTF8(bufsv)) {
1873 /* Not modifying source SV, so making a temporary copy. */
1874 bufsv = sv_2mortal(newSVsv(bufsv));
1875 sv_utf8_downgrade(bufsv, FALSE);
1877 buffer = SvPV_const(bufsv, blen);
1880 if (PL_op->op_type == OP_SYSWRITE) {
1882 if (DO_UTF8(bufsv)) {
1883 /* length and offset are in chars */
1884 blen = sv_len_utf8(bufsv);
1887 offset = SvIVx(*++MARK);
1889 if (-offset > (IV)blen)
1890 DIE(aTHX_ "Offset outside string");
1892 } else if (offset >= (IV)blen && blen > 0)
1893 DIE(aTHX_ "Offset outside string");
1896 if (length > blen - offset)
1897 length = blen - offset;
1898 if (DO_UTF8(bufsv)) {
1899 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1900 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1903 buffer = buffer+offset;
1905 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1906 if (IoTYPE(io) == IoTYPE_SOCKET) {
1907 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1913 /* See the note at doio.c:do_print about filesize limits. --jhi */
1914 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1919 else if (SP > MARK) {
1921 char * const sockbuf = SvPVx(*++MARK, mlen);
1922 /* length is really flags */
1923 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1924 length, (struct sockaddr *)sockbuf, mlen);
1927 /* length is really flags */
1928 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1931 DIE(aTHX_ PL_no_sock_func, "send");
1937 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1938 #if Size_t_size > IVSIZE
1952 return pp_sysread();
1963 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1965 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1967 if (io && !IoIFP(io)) {
1968 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1970 IoFLAGS(io) &= ~IOf_START;
1971 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1972 sv_setpvn(GvSV(gv), "-", 1);
1973 SvSETMAGIC(GvSV(gv));
1975 else if (!nextargv(gv))
1980 gv = PL_last_in_gv; /* eof */
1983 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1985 if (gv && (io = GvIO(gv))
1986 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1989 XPUSHs(SvTIED_obj((SV*)io, mg));
1992 call_method("EOF", G_SCALAR);
1998 PUSHs(boolSV(!gv || do_eof(gv)));
2012 gv = PL_last_in_gv = (GV*)POPs;
2014 if (gv && (io = GvIO(gv))
2015 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2018 XPUSHs(SvTIED_obj((SV*)io, mg));
2021 call_method("TELL", G_SCALAR);
2027 #if LSEEKSIZE > IVSIZE
2028 PUSHn( do_tell(gv) );
2030 PUSHi( do_tell(gv) );
2037 return pp_sysseek();
2045 const int whence = POPi;
2046 #if LSEEKSIZE > IVSIZE
2047 Off_t offset = (Off_t)SvNVx(POPs);
2049 Off_t offset = (Off_t)SvIVx(POPs);
2053 gv = PL_last_in_gv = (GV*)POPs;
2055 if (gv && (io = GvIO(gv))
2056 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2059 XPUSHs(SvTIED_obj((SV*)io, mg));
2060 #if LSEEKSIZE > IVSIZE
2061 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2063 XPUSHs(sv_2mortal(newSViv(offset)));
2065 XPUSHs(sv_2mortal(newSViv(whence)));
2068 call_method("SEEK", G_SCALAR);
2074 if (PL_op->op_type == OP_SEEK)
2075 PUSHs(boolSV(do_seek(gv, offset, whence)));
2077 Off_t sought = do_sysseek(gv, offset, whence);
2079 PUSHs(&PL_sv_undef);
2082 #if LSEEKSIZE > IVSIZE
2087 : newSVpvn(zero_but_true, ZBTLEN);
2088 PUSHs(sv_2mortal(sv));
2097 /* There seems to be no consensus on the length type of truncate()
2098 * and ftruncate(), both off_t and size_t have supporters. In
2099 * general one would think that when using large files, off_t is
2100 * at least as wide as size_t, so using an off_t should be okay. */
2101 /* XXX Configure probe for the length type of *truncate() needed XXX */
2104 #if Off_t_size > IVSIZE
2109 /* Checking for length < 0 is problematic as the type might or
2110 * might not be signed: if it is not, clever compilers will moan. */
2111 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2118 if (PL_op->op_flags & OPf_SPECIAL) {
2119 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2128 TAINT_PROPER("truncate");
2129 if (!(fp = IoIFP(io))) {
2135 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2137 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2147 if (SvTYPE(sv) == SVt_PVGV) {
2148 tmpgv = (GV*)sv; /* *main::FRED for example */
2149 goto do_ftruncate_gv;
2151 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2152 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2153 goto do_ftruncate_gv;
2155 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2156 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2157 goto do_ftruncate_io;
2160 name = SvPV_nolen_const(sv);
2161 TAINT_PROPER("truncate");
2163 if (truncate(name, len) < 0)
2169 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2172 if (my_chsize(tmpfd, len) < 0)
2174 PerlLIO_close(tmpfd);
2183 SETERRNO(EBADF,RMS_IFI);
2197 const unsigned int func = POPu;
2198 const int optype = PL_op->op_type;
2202 IO *io = gv ? GvIOn(gv) : 0;
2204 if (!io || !argsv || !IoIFP(io)) {
2205 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2206 report_evil_fh(gv, io, PL_op->op_type);
2207 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2211 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2214 s = SvPV_force(argsv, len);
2215 need = IOCPARM_LEN(func);
2217 s = Sv_Grow(argsv, need + 1);
2218 SvCUR_set(argsv, need);
2221 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2224 retval = SvIV(argsv);
2225 s = INT2PTR(char*,retval); /* ouch */
2228 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2230 if (optype == OP_IOCTL)
2232 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2234 DIE(aTHX_ "ioctl is not implemented");
2238 DIE(aTHX_ "fcntl is not implemented");
2240 #if defined(OS2) && defined(__EMX__)
2241 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2243 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2247 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2249 if (s[SvCUR(argsv)] != 17)
2250 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2252 s[SvCUR(argsv)] = 0; /* put our null back */
2253 SvSETMAGIC(argsv); /* Assume it has changed */
2262 PUSHp(zero_but_true, ZBTLEN);
2283 if (gv && (io = GvIO(gv)))
2290 (void)PerlIO_flush(fp);
2291 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2294 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2295 report_evil_fh(gv, io, PL_op->op_type);
2297 SETERRNO(EBADF,RMS_IFI);
2302 DIE(aTHX_ PL_no_func, "flock()");
2314 int protocol = POPi;
2320 io = gv ? GvIOn(gv) : NULL;
2323 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2324 report_evil_fh(gv, io, PL_op->op_type);
2326 do_close(gv, FALSE);
2327 SETERRNO(EBADF,LIB_INVARG);
2332 do_close(gv, FALSE);
2334 TAINT_PROPER("socket");
2335 fd = PerlSock_socket(domain, type, protocol);
2338 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2339 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2340 IoTYPE(io) = IoTYPE_SOCKET;
2341 if (!IoIFP(io) || !IoOFP(io)) {
2342 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2343 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2344 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2347 #if defined(HAS_FCNTL) && defined(F_SETFD)
2348 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2352 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2357 DIE(aTHX_ PL_no_sock_func, "socket");
2363 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2369 int protocol = POPi;
2376 io1 = gv1 ? GvIOn(gv1) : NULL;
2377 io2 = gv2 ? GvIOn(gv2) : NULL;
2378 if (!gv1 || !gv2 || !io1 || !io2) {
2379 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2381 report_evil_fh(gv1, io1, PL_op->op_type);
2383 report_evil_fh(gv1, io2, PL_op->op_type);
2386 do_close(gv1, FALSE);
2388 do_close(gv2, FALSE);
2393 do_close(gv1, FALSE);
2395 do_close(gv2, FALSE);
2397 TAINT_PROPER("socketpair");
2398 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2400 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2401 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2402 IoTYPE(io1) = IoTYPE_SOCKET;
2403 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2404 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2405 IoTYPE(io2) = IoTYPE_SOCKET;
2406 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2407 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2408 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2409 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2410 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2411 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2412 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2415 #if defined(HAS_FCNTL) && defined(F_SETFD)
2416 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2417 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2422 DIE(aTHX_ PL_no_sock_func, "socketpair");
2430 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2431 extern void GETPRIVMODE();
2432 extern void GETUSERMODE();
2435 /* OK, so on what platform does bind modify addr? */
2438 register IO *io = GvIOn(gv);
2442 int mpeprivmode = 0;
2445 if (!io || !IoIFP(io))
2448 addr = SvPV_const(addrsv, len);
2449 TAINT_PROPER("bind");
2450 #ifdef MPE /* Deal with MPE bind() peculiarities */
2451 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2452 /* The address *MUST* stupidly be zero. */
2453 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2454 /* PRIV mode is required to bind() to ports < 1024. */
2455 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2456 ((struct sockaddr_in *)addr)->sin_port > 0) {
2457 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2462 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2463 (struct sockaddr *)addr, len) >= 0)
2466 #ifdef MPE /* Switch back to USER mode */
2477 if (ckWARN(WARN_CLOSED))
2478 report_evil_fh(gv, io, PL_op->op_type);
2479 SETERRNO(EBADF,SS_IVCHAN);
2482 DIE(aTHX_ PL_no_sock_func, "bind");
2493 register IO *io = GvIOn(gv);
2496 if (!io || !IoIFP(io))
2499 addr = SvPV_const(addrsv, len);
2500 TAINT_PROPER("connect");
2501 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2507 if (ckWARN(WARN_CLOSED))
2508 report_evil_fh(gv, io, PL_op->op_type);
2509 SETERRNO(EBADF,SS_IVCHAN);
2512 DIE(aTHX_ PL_no_sock_func, "connect");
2522 register IO *io = gv ? GvIOn(gv) : NULL;
2524 if (!gv || !io || !IoIFP(io))
2527 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2533 if (ckWARN(WARN_CLOSED))
2534 report_evil_fh(gv, io, PL_op->op_type);
2535 SETERRNO(EBADF,SS_IVCHAN);
2538 DIE(aTHX_ PL_no_sock_func, "listen");
2550 char namebuf[MAXPATHLEN];
2551 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2552 Sock_size_t len = sizeof (struct sockaddr_in);
2554 Sock_size_t len = sizeof namebuf;
2567 if (!gstio || !IoIFP(gstio))
2571 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2575 do_close(ngv, FALSE);
2576 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2577 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2578 IoTYPE(nstio) = IoTYPE_SOCKET;
2579 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2580 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2581 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2582 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2585 #if defined(HAS_FCNTL) && defined(F_SETFD)
2586 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2590 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2591 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2593 #ifdef __SCO_VERSION__
2594 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2597 PUSHp(namebuf, len);
2601 if (ckWARN(WARN_CLOSED))
2602 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2603 SETERRNO(EBADF,SS_IVCHAN);
2609 DIE(aTHX_ PL_no_sock_func, "accept");
2619 register IO *io = GvIOn(gv);
2621 if (!io || !IoIFP(io))
2624 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2628 if (ckWARN(WARN_CLOSED))
2629 report_evil_fh(gv, io, PL_op->op_type);
2630 SETERRNO(EBADF,SS_IVCHAN);
2633 DIE(aTHX_ PL_no_sock_func, "shutdown");
2640 return pp_ssockopt();
2642 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2650 int optype = PL_op->op_type;
2653 unsigned int optname;
2659 if (optype == OP_GSOCKOPT)
2660 sv = sv_2mortal(NEWSV(22, 257));
2663 optname = (unsigned int) POPi;
2664 lvl = (unsigned int) POPi;
2668 if (!io || !IoIFP(io))
2671 fd = PerlIO_fileno(IoIFP(io));
2675 (void)SvPOK_only(sv);
2679 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2686 #if defined(__SYMBIAN32__)
2687 # define SETSOCKOPT_OPTION_VALUE_T void *
2689 # define SETSOCKOPT_OPTION_VALUE_T const char *
2691 /* XXX TODO: We need to have a proper type (a Configure probe,
2692 * etc.) for what the C headers think of the third argument of
2693 * setsockopt(), the option_value read-only buffer: is it
2694 * a "char *", or a "void *", const or not. Some compilers
2695 * don't take kindly to e.g. assuming that "char *" implicitly
2696 * promotes to a "void *", or to explicitly promoting/demoting
2697 * consts to non/vice versa. The "const void *" is the SUS
2698 * definition, but that does not fly everywhere for the above
2700 SETSOCKOPT_OPTION_VALUE_T buf;
2704 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2708 aint = (int)SvIV(sv);
2709 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2712 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2721 if (ckWARN(WARN_CLOSED))
2722 report_evil_fh(gv, io, optype);
2723 SETERRNO(EBADF,SS_IVCHAN);
2728 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2735 return pp_getpeername();
2737 DIE(aTHX_ PL_no_sock_func, "getsockname");
2745 int optype = PL_op->op_type;
2749 register IO *io = GvIOn(gv);
2752 if (!io || !IoIFP(io))
2755 sv = sv_2mortal(NEWSV(22, 257));
2756 (void)SvPOK_only(sv);
2760 fd = PerlIO_fileno(IoIFP(io));
2762 case OP_GETSOCKNAME:
2763 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2766 case OP_GETPEERNAME:
2767 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2769 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2771 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";
2772 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2773 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2774 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2775 sizeof(u_short) + sizeof(struct in_addr))) {
2782 #ifdef BOGUS_GETNAME_RETURN
2783 /* Interactive Unix, getpeername() and getsockname()
2784 does not return valid namelen */
2785 if (len == BOGUS_GETNAME_RETURN)
2786 len = sizeof(struct sockaddr);
2794 if (ckWARN(WARN_CLOSED))
2795 report_evil_fh(gv, io, optype);
2796 SETERRNO(EBADF,SS_IVCHAN);
2801 DIE(aTHX_ PL_no_sock_func, "getpeername");
2819 if (PL_op->op_flags & OPf_REF) {
2821 if (PL_op->op_type == OP_LSTAT) {
2822 if (gv != PL_defgv) {
2823 if (ckWARN(WARN_IO))
2824 Perl_warner(aTHX_ packWARN(WARN_IO),
2825 "lstat() on filehandle %s", GvENAME(gv));
2826 } else if (PL_laststype != OP_LSTAT)
2827 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2831 if (gv != PL_defgv) {
2832 PL_laststype = OP_STAT;
2834 sv_setpvn(PL_statname, "", 0);
2835 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2836 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2838 if (PL_laststatval < 0) {
2839 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2840 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2846 if (SvTYPE(sv) == SVt_PVGV) {
2850 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2852 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2853 Perl_warner(aTHX_ packWARN(WARN_IO),
2854 "lstat() on filehandle %s", GvENAME(gv));
2857 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2859 PL_laststype = PL_op->op_type;
2860 if (PL_op->op_type == OP_LSTAT)
2861 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2863 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2864 if (PL_laststatval < 0) {
2865 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2866 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2872 if (gimme != G_ARRAY) {
2873 if (gimme != G_VOID)
2874 XPUSHs(boolSV(max));
2880 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2881 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2882 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2883 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2884 #if Uid_t_size > IVSIZE
2885 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2887 # if Uid_t_sign <= 0
2888 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2890 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2893 #if Gid_t_size > IVSIZE
2894 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2896 # if Gid_t_sign <= 0
2897 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2899 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2902 #ifdef USE_STAT_RDEV
2903 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2905 PUSHs(sv_2mortal(newSVpvn("", 0)));
2907 #if Off_t_size > IVSIZE
2908 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2910 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2913 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2914 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2915 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2917 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2918 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2919 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2921 #ifdef USE_STAT_BLOCKS
2922 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2923 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2925 PUSHs(sv_2mortal(newSVpvn("", 0)));
2926 PUSHs(sv_2mortal(newSVpvn("", 0)));
2932 /* This macro is used by the stacked filetest operators :
2933 * if the previous filetest failed, short-circuit and pass its value.
2934 * Else, discard it from the stack and continue. --rgs
2936 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2937 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2938 else { (void)POPs; PUTBACK; } \
2945 STACKED_FTEST_CHECK;
2946 #if defined(HAS_ACCESS) && defined(R_OK)
2947 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2948 result = access(POPpx, R_OK);
2963 if (cando(S_IRUSR, 0, &PL_statcache))
2972 STACKED_FTEST_CHECK;
2973 #if defined(HAS_ACCESS) && defined(W_OK)
2974 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2975 result = access(POPpx, W_OK);
2990 if (cando(S_IWUSR, 0, &PL_statcache))
2999 STACKED_FTEST_CHECK;
3000 #if defined(HAS_ACCESS) && defined(X_OK)
3001 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3002 result = access(POPpx, X_OK);
3017 if (cando(S_IXUSR, 0, &PL_statcache))
3026 STACKED_FTEST_CHECK;
3027 #ifdef PERL_EFF_ACCESS_R_OK
3028 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3029 result = PERL_EFF_ACCESS_R_OK(POPpx);
3044 if (cando(S_IRUSR, 1, &PL_statcache))
3053 STACKED_FTEST_CHECK;
3054 #ifdef PERL_EFF_ACCESS_W_OK
3055 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3056 result = PERL_EFF_ACCESS_W_OK(POPpx);
3071 if (cando(S_IWUSR, 1, &PL_statcache))
3080 STACKED_FTEST_CHECK;
3081 #ifdef PERL_EFF_ACCESS_X_OK
3082 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3083 result = PERL_EFF_ACCESS_X_OK(POPpx);
3098 if (cando(S_IXUSR, 1, &PL_statcache))
3107 STACKED_FTEST_CHECK;
3117 return pp_ftrowned();
3124 STACKED_FTEST_CHECK;
3129 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3139 STACKED_FTEST_CHECK;
3144 if (PL_statcache.st_size == 0)
3153 STACKED_FTEST_CHECK;
3158 #if Off_t_size > IVSIZE
3159 PUSHn(PL_statcache.st_size);
3161 PUSHi(PL_statcache.st_size);
3170 STACKED_FTEST_CHECK;
3175 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3183 STACKED_FTEST_CHECK;
3188 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3196 STACKED_FTEST_CHECK;
3201 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3209 STACKED_FTEST_CHECK;
3214 if (S_ISSOCK(PL_statcache.st_mode))
3223 STACKED_FTEST_CHECK;
3228 if (S_ISCHR(PL_statcache.st_mode))
3237 STACKED_FTEST_CHECK;
3242 if (S_ISBLK(PL_statcache.st_mode))
3251 STACKED_FTEST_CHECK;
3256 if (S_ISREG(PL_statcache.st_mode))
3265 STACKED_FTEST_CHECK;
3270 if (S_ISDIR(PL_statcache.st_mode))
3279 STACKED_FTEST_CHECK;
3284 if (S_ISFIFO(PL_statcache.st_mode))
3291 I32 result = my_lstat();
3295 if (S_ISLNK(PL_statcache.st_mode))
3305 STACKED_FTEST_CHECK;
3310 if (PL_statcache.st_mode & S_ISUID)
3321 STACKED_FTEST_CHECK;
3326 if (PL_statcache.st_mode & S_ISGID)
3337 STACKED_FTEST_CHECK;
3342 if (PL_statcache.st_mode & S_ISVTX)
3355 STACKED_FTEST_CHECK;
3357 if (PL_op->op_flags & OPf_REF)
3359 else if (isGV(TOPs))
3361 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3362 gv = (GV*)SvRV(POPs);
3364 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3366 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3367 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3368 else if (tmpsv && SvOK(tmpsv)) {
3369 const char *tmps = SvPV_nolen_const(tmpsv);
3377 if (PerlLIO_isatty(fd))
3382 #if defined(atarist) /* this will work with atariST. Configure will
3383 make guesses for other systems. */
3384 # define FILE_base(f) ((f)->_base)
3385 # define FILE_ptr(f) ((f)->_ptr)
3386 # define FILE_cnt(f) ((f)->_cnt)
3387 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3397 register STDCHAR *s;
3403 STACKED_FTEST_CHECK;
3405 if (PL_op->op_flags & OPf_REF)
3407 else if (isGV(TOPs))
3409 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3410 gv = (GV*)SvRV(POPs);
3416 if (gv == PL_defgv) {
3418 io = GvIO(PL_statgv);
3421 goto really_filename;
3426 PL_laststatval = -1;
3427 sv_setpvn(PL_statname, "", 0);
3428 io = GvIO(PL_statgv);
3430 if (io && IoIFP(io)) {
3431 if (! PerlIO_has_base(IoIFP(io)))
3432 DIE(aTHX_ "-T and -B not implemented on filehandles");
3433 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3434 if (PL_laststatval < 0)
3436 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3437 if (PL_op->op_type == OP_FTTEXT)
3442 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3443 i = PerlIO_getc(IoIFP(io));
3445 (void)PerlIO_ungetc(IoIFP(io),i);
3447 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3449 len = PerlIO_get_bufsiz(IoIFP(io));
3450 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3451 /* sfio can have large buffers - limit to 512 */
3456 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3458 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3460 SETERRNO(EBADF,RMS_IFI);
3468 PL_laststype = OP_STAT;
3469 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3470 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3471 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3473 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3476 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3477 if (PL_laststatval < 0) {
3478 (void)PerlIO_close(fp);
3481 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3482 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3483 (void)PerlIO_close(fp);
3485 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3486 RETPUSHNO; /* special case NFS directories */
3487 RETPUSHYES; /* null file is anything */
3492 /* now scan s to look for textiness */
3493 /* XXX ASCII dependent code */
3495 #if defined(DOSISH) || defined(USEMYBINMODE)
3496 /* ignore trailing ^Z on short files */
3497 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3501 for (i = 0; i < len; i++, s++) {
3502 if (!*s) { /* null never allowed in text */
3507 else if (!(isPRINT(*s) || isSPACE(*s)))
3510 else if (*s & 128) {
3512 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3515 /* utf8 characters don't count as odd */
3516 if (UTF8_IS_START(*s)) {
3517 int ulen = UTF8SKIP(s);
3518 if (ulen < len - i) {
3520 for (j = 1; j < ulen; j++) {
3521 if (!UTF8_IS_CONTINUATION(s[j]))
3524 --ulen; /* loop does extra increment */
3534 *s != '\n' && *s != '\r' && *s != '\b' &&
3535 *s != '\t' && *s != '\f' && *s != 27)
3540 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3556 const char *tmps = 0;
3560 SV * const sv = POPs;
3561 if (SvTYPE(sv) == SVt_PVGV) {
3564 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3568 tmps = SvPVx_nolen_const(sv);
3572 if( !gv && (!tmps || !*tmps) ) {
3573 HV * const table = GvHVn(PL_envgv);
3576 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3577 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
3579 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
3584 deprecate("chdir('') or chdir(undef) as chdir()");
3585 tmps = SvPV_nolen_const(*svp);
3589 TAINT_PROPER("chdir");
3594 TAINT_PROPER("chdir");
3597 IO* const io = GvIO(gv);
3600 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3602 else if (IoDIRP(io)) {
3604 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3606 DIE(aTHX_ PL_no_func, "dirfd");
3617 DIE(aTHX_ PL_no_func, "fchdir");
3621 PUSHi( PerlDir_chdir(tmps) >= 0 );
3623 /* Clear the DEFAULT element of ENV so we'll get the new value
3625 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3633 dSP; dMARK; dTARGET;
3634 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3640 DIE(aTHX_ PL_no_func, "chown");
3649 TAINT_PROPER("chroot");
3650 PUSHi( chroot(tmps) >= 0 );
3653 DIE(aTHX_ PL_no_func, "chroot");
3659 dSP; dMARK; dTARGET;
3661 value = (I32)apply(PL_op->op_type, MARK, SP);
3669 dSP; dMARK; dTARGET;
3671 value = (I32)apply(PL_op->op_type, MARK, SP);
3679 dSP; dMARK; dTARGET;
3681 value = (I32)apply(PL_op->op_type, MARK, SP);
3691 const char *tmps2 = POPpconstx;
3692 const char *tmps = SvPV_nolen_const(TOPs);
3693 TAINT_PROPER("rename");
3695 anum = PerlLIO_rename(tmps, tmps2);
3697 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3698 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3701 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3702 (void)UNLINK(tmps2);
3703 if (!(anum = link(tmps, tmps2)))
3704 anum = UNLINK(tmps);
3716 const char *tmps2 = POPpconstx;
3717 const char *tmps = SvPV_nolen_const(TOPs);
3718 TAINT_PROPER("link");
3719 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3722 DIE(aTHX_ PL_no_func, "link");
3730 const char *tmps2 = POPpconstx;
3731 const char *tmps = SvPV_nolen_const(TOPs);
3732 TAINT_PROPER("symlink");
3733 SETi( symlink(tmps, tmps2) >= 0 );
3736 DIE(aTHX_ PL_no_func, "symlink");
3746 char buf[MAXPATHLEN];
3749 #ifndef INCOMPLETE_TAINTS
3753 len = readlink(tmps, buf, sizeof(buf) - 1);
3761 RETSETUNDEF; /* just pretend it's a normal file */
3765 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3767 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3769 char * const save_filename = filename;
3775 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3776 strcpy(cmdline, cmd);
3777 strcat(cmdline, " ");
3778 for (s = cmdline + strlen(cmdline); *filename; ) {
3783 myfp = PerlProc_popen(cmdline, "r");
3787 SV *tmpsv = sv_newmortal();
3788 /* Need to save/restore 'PL_rs' ?? */
3789 s = sv_gets(tmpsv, myfp, 0);
3790 (void)PerlProc_pclose(myfp);
3794 #ifdef HAS_SYS_ERRLIST
3799 /* you don't see this */
3801 #ifdef HAS_SYS_ERRLIST
3809 if (instr(s, errmsg)) {
3816 #define EACCES EPERM
3818 if (instr(s, "cannot make"))
3819 SETERRNO(EEXIST,RMS_FEX);
3820 else if (instr(s, "existing file"))
3821 SETERRNO(EEXIST,RMS_FEX);
3822 else if (instr(s, "ile exists"))
3823 SETERRNO(EEXIST,RMS_FEX);
3824 else if (instr(s, "non-exist"))
3825 SETERRNO(ENOENT,RMS_FNF);
3826 else if (instr(s, "does not exist"))
3827 SETERRNO(ENOENT,RMS_FNF);
3828 else if (instr(s, "not empty"))
3829 SETERRNO(EBUSY,SS_DEVOFFLINE);
3830 else if (instr(s, "cannot access"))
3831 SETERRNO(EACCES,RMS_PRV);
3833 SETERRNO(EPERM,RMS_PRV);
3836 else { /* some mkdirs return no failure indication */
3837 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3838 if (PL_op->op_type == OP_RMDIR)
3843 SETERRNO(EACCES,RMS_PRV); /* a guess */
3852 /* This macro removes trailing slashes from a directory name.
3853 * Different operating and file systems take differently to
3854 * trailing slashes. According to POSIX 1003.1 1996 Edition
3855 * any number of trailing slashes should be allowed.
3856 * Thusly we snip them away so that even non-conforming
3857 * systems are happy.
3858 * We should probably do this "filtering" for all
3859 * the functions that expect (potentially) directory names:
3860 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3861 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3863 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3864 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3867 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3868 (tmps) = savepvn((tmps), (len)); \
3888 TRIMSLASHES(tmps,len,copy);
3890 TAINT_PROPER("mkdir");
3892 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3894 SETi( dooneliner("mkdir", tmps) );
3895 oldumask = PerlLIO_umask(0);
3896 PerlLIO_umask(oldumask);
3897 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3911 TRIMSLASHES(tmps,len,copy);
3912 TAINT_PROPER("rmdir");
3914 SETi( PerlDir_rmdir(tmps) >= 0 );
3916 SETi( dooneliner("rmdir", tmps) );
3923 /* Directory calls. */
3927 #if defined(Direntry_t) && defined(HAS_READDIR)
3929 const char *dirname = POPpconstx;
3931 register IO *io = GvIOn(gv);
3937 PerlDir_close(IoDIRP(io));
3938 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3944 SETERRNO(EBADF,RMS_DIR);
3947 DIE(aTHX_ PL_no_dir_func, "opendir");
3953 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3954 DIE(aTHX_ PL_no_dir_func, "readdir");
3956 #if !defined(I_DIRENT) && !defined(VMS)
3957 Direntry_t *readdir (DIR *);
3962 const I32 gimme = GIMME;
3963 GV *gv = (GV *)POPs;
3964 register Direntry_t *dp;
3965 register IO *io = GvIOn(gv);
3967 if (!io || !IoDIRP(io))
3971 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3975 sv = newSVpvn(dp->d_name, dp->d_namlen);
3977 sv = newSVpv(dp->d_name, 0);
3979 #ifndef INCOMPLETE_TAINTS
3980 if (!(IoFLAGS(io) & IOf_UNTAINT))
3983 XPUSHs(sv_2mortal(sv));
3985 while (gimme == G_ARRAY);
3987 if (!dp && gimme != G_ARRAY)
3994 SETERRNO(EBADF,RMS_ISI);
3995 if (GIMME == G_ARRAY)
4004 #if defined(HAS_TELLDIR) || defined(telldir)
4006 /* XXX does _anyone_ need this? --AD 2/20/1998 */
4007 /* XXX netbsd still seemed to.
4008 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
4009 --JHI 1999-Feb-02 */
4010 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
4011 long telldir (DIR *);
4014 register IO *io = GvIOn(gv);
4016 if (!io || !IoDIRP(io))
4019 PUSHi( PerlDir_tell(IoDIRP(io)) );
4023 SETERRNO(EBADF,RMS_ISI);
4026 DIE(aTHX_ PL_no_dir_func, "telldir");
4032 #if defined(HAS_SEEKDIR) || defined(seekdir)
4036 register IO *io = GvIOn(gv);
4038 if (!io || !IoDIRP(io))
4041 (void)PerlDir_seek(IoDIRP(io), along);
4046 SETERRNO(EBADF,RMS_ISI);
4049 DIE(aTHX_ PL_no_dir_func, "seekdir");
4055 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4058 register IO *io = GvIOn(gv);
4060 if (!io || !IoDIRP(io))
4063 (void)PerlDir_rewind(IoDIRP(io));
4067 SETERRNO(EBADF,RMS_ISI);
4070 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4076 #if defined(Direntry_t) && defined(HAS_READDIR)
4079 register IO *io = GvIOn(gv);
4081 if (!io || !IoDIRP(io))
4084 #ifdef VOID_CLOSEDIR
4085 PerlDir_close(IoDIRP(io));
4087 if (PerlDir_close(IoDIRP(io)) < 0) {
4088 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4097 SETERRNO(EBADF,RMS_IFI);
4100 DIE(aTHX_ PL_no_dir_func, "closedir");
4104 /* Process control. */
4114 PERL_FLUSHALL_FOR_CHILD;
4115 childpid = PerlProc_fork();
4119 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4120 SvREADONLY_off(GvSV(tmpgv));
4121 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4122 SvREADONLY_on(GvSV(tmpgv));
4124 #ifdef THREADS_HAVE_PIDS
4125 PL_ppid = (IV)getppid();
4127 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4132 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4137 PERL_FLUSHALL_FOR_CHILD;
4138 childpid = PerlProc_fork();
4144 DIE(aTHX_ PL_no_func, "fork");
4151 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4156 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4157 childpid = wait4pid(-1, &argflags, 0);
4159 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4164 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4165 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4166 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4168 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4173 DIE(aTHX_ PL_no_func, "wait");
4179 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4188 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4189 result = wait4pid(pid, &argflags, optype);
4191 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4196 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4197 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4198 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4200 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4205 DIE(aTHX_ PL_no_func, "waitpid");
4211 dSP; dMARK; dORIGMARK; dTARGET;
4217 while (++MARK <= SP) {
4218 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4223 TAINT_PROPER("system");
4225 PERL_FLUSHALL_FOR_CHILD;
4226 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4232 if (PerlProc_pipe(pp) >= 0)
4234 while ((childpid = PerlProc_fork()) == -1) {
4235 if (errno != EAGAIN) {
4240 PerlLIO_close(pp[0]);
4241 PerlLIO_close(pp[1]);
4248 Sigsave_t ihand,qhand; /* place to save signals during system() */
4252 PerlLIO_close(pp[1]);
4254 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4255 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4258 result = wait4pid(childpid, &status, 0);
4259 } while (result == -1 && errno == EINTR);
4261 (void)rsignal_restore(SIGINT, &ihand);
4262 (void)rsignal_restore(SIGQUIT, &qhand);
4264 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4265 do_execfree(); /* free any memory child malloced on fork */
4271 while (n < sizeof(int)) {
4272 n1 = PerlLIO_read(pp[0],
4273 (void*)(((char*)&errkid)+n),
4279 PerlLIO_close(pp[0]);
4280 if (n) { /* Error */
4281 if (n != sizeof(int))
4282 DIE(aTHX_ "panic: kid popen errno read");
4283 errno = errkid; /* Propagate errno from kid */
4284 STATUS_NATIVE_CHILD_SET(-1);
4287 PUSHi(STATUS_CURRENT);
4291 PerlLIO_close(pp[0]);
4292 #if defined(HAS_FCNTL) && defined(F_SETFD)
4293 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4296 if (PL_op->op_flags & OPf_STACKED) {
4297 SV *really = *++MARK;
4298 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4300 else if (SP - MARK != 1)
4301 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4303 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4307 #else /* ! FORK or VMS or OS/2 */
4310 if (PL_op->op_flags & OPf_STACKED) {
4311 SV *really = *++MARK;
4312 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4313 value = (I32)do_aspawn(really, MARK, SP);
4315 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4318 else if (SP - MARK != 1) {
4319 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4320 value = (I32)do_aspawn(Nullsv, MARK, SP);
4322 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4326 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4328 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4330 STATUS_NATIVE_CHILD_SET(value);
4333 PUSHi(result ? value : STATUS_CURRENT);
4334 #endif /* !FORK or VMS */
4340 dSP; dMARK; dORIGMARK; dTARGET;
4345 while (++MARK <= SP) {
4346 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4351 TAINT_PROPER("exec");
4353 PERL_FLUSHALL_FOR_CHILD;
4354 if (PL_op->op_flags & OPf_STACKED) {
4355 SV *really = *++MARK;
4356 value = (I32)do_aexec(really, MARK, SP);
4358 else if (SP - MARK != 1)
4360 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4364 (void ) do_aspawn(Nullsv, MARK, SP);
4368 value = (I32)do_aexec(Nullsv, MARK, SP);
4373 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4376 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4379 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4392 dSP; dMARK; dTARGET;
4394 value = (I32)apply(PL_op->op_type, MARK, SP);
4399 DIE(aTHX_ PL_no_func, "kill");
4407 # ifdef THREADS_HAVE_PIDS
4408 if (PL_ppid != 1 && getppid() == 1)
4409 /* maybe the parent process has died. Refresh ppid cache */
4413 XPUSHi( getppid() );
4417 DIE(aTHX_ PL_no_func, "getppid");
4433 pgrp = (I32)BSD_GETPGRP(pid);
4435 if (pid != 0 && pid != PerlProc_getpid())
4436 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4442 DIE(aTHX_ PL_no_func, "getpgrp()");
4461 TAINT_PROPER("setpgrp");
4463 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4465 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4466 || (pid != 0 && pid != PerlProc_getpid()))
4468 DIE(aTHX_ "setpgrp can't take arguments");
4470 SETi( setpgrp() >= 0 );
4471 #endif /* USE_BSDPGRP */
4474 DIE(aTHX_ PL_no_func, "setpgrp()");
4480 #ifdef HAS_GETPRIORITY
4484 SETi( getpriority(which, who) );
4487 DIE(aTHX_ PL_no_func, "getpriority()");
4493 #ifdef HAS_SETPRIORITY
4498 TAINT_PROPER("setpriority");
4499 SETi( setpriority(which, who, niceval) >= 0 );
4502 DIE(aTHX_ PL_no_func, "setpriority()");
4512 XPUSHn( time(Null(Time_t*)) );
4514 XPUSHi( time(Null(Time_t*)) );
4525 (void)PerlProc_times(&PL_timesbuf);
4527 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4528 /* struct tms, though same data */
4532 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4533 if (GIMME == G_ARRAY) {
4534 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4535 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4536 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4542 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4544 if (GIMME == G_ARRAY) {
4545 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4546 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4547 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4551 DIE(aTHX_ "times not implemented");
4553 #endif /* HAS_TIMES */
4561 #ifdef LOCALTIME_EDGECASE_BROKEN
4562 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4567 /* No workarounds in the valid range */
4568 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4569 return (localtime (tp));
4571 /* This edge case is to workaround the undefined behaviour, where the
4572 * TIMEZONE makes the time go beyond the defined range.
4573 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4574 * If there is a negative offset in TZ, like MET-1METDST, some broken
4575 * implementations of localtime () (like AIX 5.2) barf with bogus
4577 * 0x7fffffff gmtime 2038-01-19 03:14:07
4578 * 0x7fffffff localtime 1901-12-13 21:45:51
4579 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4580 * 0x3c19137f gmtime 2001-12-13 20:45:51
4581 * 0x3c19137f localtime 2001-12-13 21:45:51
4582 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4583 * Given that legal timezones are typically between GMT-12 and GMT+12
4584 * we turn back the clock 23 hours before calling the localtime
4585 * function, and add those to the return value. This will never cause
4586 * day wrapping problems, since the edge case is Tue Jan *19*
4588 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4591 if (P->tm_hour >= 24) {
4593 P->tm_mday++; /* 18 -> 19 */
4594 P->tm_wday++; /* Mon -> Tue */
4595 P->tm_yday++; /* 18 -> 19 */
4598 } /* S_my_localtime */
4605 const struct tm *tmbuf;
4606 static const char * const dayname[] =
4607 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4608 static const char * const monname[] =
4609 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4610 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4616 when = (Time_t)SvNVx(POPs);
4618 when = (Time_t)SvIVx(POPs);
4621 if (PL_op->op_type == OP_LOCALTIME)
4622 #ifdef LOCALTIME_EDGECASE_BROKEN
4623 tmbuf = S_my_localtime(aTHX_ &when);
4625 tmbuf = localtime(&when);
4628 tmbuf = gmtime(&when);
4630 if (GIMME != G_ARRAY) {
4636 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4637 dayname[tmbuf->tm_wday],
4638 monname[tmbuf->tm_mon],
4643 tmbuf->tm_year + 1900);
4644 PUSHs(sv_2mortal(tsv));
4649 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4650 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4651 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4652 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4653 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4654 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4655 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4656 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4657 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4668 anum = alarm((unsigned int)anum);
4675 DIE(aTHX_ PL_no_func, "alarm");
4686 (void)time(&lasttime);
4691 PerlProc_sleep((unsigned int)duration);
4694 XPUSHi(when - lasttime);
4698 /* Shared memory. */
4712 return pp_shmwrite();
4717 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4718 dSP; dMARK; dTARGET;
4719 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4728 /* Message passing. */
4742 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4743 dSP; dMARK; dTARGET;
4744 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4755 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4756 dSP; dMARK; dTARGET;
4757 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4770 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4771 dSP; dMARK; dTARGET;
4772 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4779 DIE(aTHX_ "System V IPC is not implemented on this machine");
4785 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4786 dSP; dMARK; dTARGET;
4787 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4795 PUSHp(zero_but_true, ZBTLEN);
4805 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4806 dSP; dMARK; dTARGET;
4807 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4816 /* Get system info. */
4820 #ifdef HAS_GETHOSTBYNAME
4821 return pp_ghostent();
4823 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4829 #ifdef HAS_GETHOSTBYADDR
4830 return pp_ghostent();
4832 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4838 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4840 I32 which = PL_op->op_type;
4841 register char **elem;
4843 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4844 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4845 struct hostent *gethostbyname(Netdb_name_t);
4846 struct hostent *gethostent(void);
4848 struct hostent *hent;
4852 if (which == OP_GHBYNAME) {
4853 #ifdef HAS_GETHOSTBYNAME
4854 char* name = POPpbytex;
4855 hent = PerlSock_gethostbyname(name);
4857 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4860 else if (which == OP_GHBYADDR) {
4861 #ifdef HAS_GETHOSTBYADDR
4862 int addrtype = POPi;
4865 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4867 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4869 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4873 #ifdef HAS_GETHOSTENT
4874 hent = PerlSock_gethostent();
4876 DIE(aTHX_ PL_no_sock_func, "gethostent");
4879 #ifdef HOST_NOT_FOUND
4881 #ifdef USE_REENTRANT_API
4882 # ifdef USE_GETHOSTENT_ERRNO
4883 h_errno = PL_reentrant_buffer->_gethostent_errno;
4886 STATUS_UNIX_SET(h_errno);
4890 if (GIMME != G_ARRAY) {
4891 PUSHs(sv = sv_newmortal());
4893 if (which == OP_GHBYNAME) {
4895 sv_setpvn(sv, hent->h_addr, hent->h_length);
4898 sv_setpv(sv, (char*)hent->h_name);
4904 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4905 sv_setpv(sv, (char*)hent->h_name);
4906 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4907 for (elem = hent->h_aliases; elem && *elem; elem++) {
4908 sv_catpv(sv, *elem);
4910 sv_catpvn(sv, " ", 1);
4912 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4913 sv_setiv(sv, (IV)hent->h_addrtype);
4914 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4915 len = hent->h_length;
4916 sv_setiv(sv, (IV)len);
4918 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4919 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4920 sv_setpvn(sv, *elem, len);
4923 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4925 sv_setpvn(sv, hent->h_addr, len);
4930 DIE(aTHX_ PL_no_sock_func, "gethostent");
4936 #ifdef HAS_GETNETBYNAME
4937 return pp_gnetent();
4939 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4945 #ifdef HAS_GETNETBYADDR
4946 return pp_gnetent();
4948 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4954 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4956 I32 which = PL_op->op_type;
4957 register char **elem;
4959 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4960 struct netent *getnetbyaddr(Netdb_net_t, int);
4961 struct netent *getnetbyname(Netdb_name_t);
4962 struct netent *getnetent(void);
4964 struct netent *nent;
4966 if (which == OP_GNBYNAME){
4967 #ifdef HAS_GETNETBYNAME
4968 char *name = POPpbytex;
4969 nent = PerlSock_getnetbyname(name);
4971 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4974 else if (which == OP_GNBYADDR) {
4975 #ifdef HAS_GETNETBYADDR
4976 int addrtype = POPi;
4977 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4978 nent = PerlSock_getnetbyaddr(addr, addrtype);
4980 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4984 #ifdef HAS_GETNETENT
4985 nent = PerlSock_getnetent();
4987 DIE(aTHX_ PL_no_sock_func, "getnetent");
4990 #ifdef HOST_NOT_FOUND
4992 #ifdef USE_REENTRANT_API
4993 # ifdef USE_GETNETENT_ERRNO
4994 h_errno = PL_reentrant_buffer->_getnetent_errno;
4997 STATUS_UNIX_SET(h_errno);
5002 if (GIMME != G_ARRAY) {
5003 PUSHs(sv = sv_newmortal());
5005 if (which == OP_GNBYNAME)
5006 sv_setiv(sv, (IV)nent->n_net);
5008 sv_setpv(sv, nent->n_name);
5014 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5015 sv_setpv(sv, nent->n_name);
5016 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5017 for (elem = nent->n_aliases; elem && *elem; elem++) {
5018 sv_catpv(sv, *elem);
5020 sv_catpvn(sv, " ", 1);
5022 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5023 sv_setiv(sv, (IV)nent->n_addrtype);
5024 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5025 sv_setiv(sv, (IV)nent->n_net);
5030 DIE(aTHX_ PL_no_sock_func, "getnetent");
5036 #ifdef HAS_GETPROTOBYNAME
5037 return pp_gprotoent();
5039 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
5045 #ifdef HAS_GETPROTOBYNUMBER
5046 return pp_gprotoent();
5048 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
5054 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
5056 I32 which = PL_op->op_type;
5057 register char **elem;
5059 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
5060 struct protoent *getprotobyname(Netdb_name_t);
5061 struct protoent *getprotobynumber(int);
5062 struct protoent *getprotoent(void);
5064 struct protoent *pent;
5066 if (which == OP_GPBYNAME) {
5067 #ifdef HAS_GETPROTOBYNAME
5068 char* name = POPpbytex;
5069 pent = PerlSock_getprotobyname(name);
5071 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
5074 else if (which == OP_GPBYNUMBER) {
5075 #ifdef HAS_GETPROTOBYNUMBER
5077 pent = PerlSock_getprotobynumber(number);
5079 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
5083 #ifdef HAS_GETPROTOENT
5084 pent = PerlSock_getprotoent();
5086 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5090 if (GIMME != G_ARRAY) {
5091 PUSHs(sv = sv_newmortal());
5093 if (which == OP_GPBYNAME)
5094 sv_setiv(sv, (IV)pent->p_proto);
5096 sv_setpv(sv, pent->p_name);
5102 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5103 sv_setpv(sv, pent->p_name);
5104 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5105 for (elem = pent->p_aliases; elem && *elem; elem++) {
5106 sv_catpv(sv, *elem);
5108 sv_catpvn(sv, " ", 1);
5110 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5111 sv_setiv(sv, (IV)pent->p_proto);
5116 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5122 #ifdef HAS_GETSERVBYNAME
5123 return pp_gservent();
5125 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5131 #ifdef HAS_GETSERVBYPORT
5132 return pp_gservent();
5134 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5140 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5142 I32 which = PL_op->op_type;
5143 register char **elem;
5145 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5146 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5147 struct servent *getservbyport(int, Netdb_name_t);
5148 struct servent *getservent(void);
5150 struct servent *sent;
5152 if (which == OP_GSBYNAME) {
5153 #ifdef HAS_GETSERVBYNAME
5154 char *proto = POPpbytex;
5155 char *name = POPpbytex;
5157 if (proto && !*proto)
5160 sent = PerlSock_getservbyname(name, proto);
5162 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5165 else if (which == OP_GSBYPORT) {
5166 #ifdef HAS_GETSERVBYPORT
5167 char *proto = POPpbytex;
5168 unsigned short port = (unsigned short)POPu;
5170 if (proto && !*proto)
5174 port = PerlSock_htons(port);
5176 sent = PerlSock_getservbyport(port, proto);
5178 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5182 #ifdef HAS_GETSERVENT
5183 sent = PerlSock_getservent();
5185 DIE(aTHX_ PL_no_sock_func, "getservent");
5189 if (GIMME != G_ARRAY) {
5190 PUSHs(sv = sv_newmortal());
5192 if (which == OP_GSBYNAME) {
5194 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5196 sv_setiv(sv, (IV)(sent->s_port));
5200 sv_setpv(sv, sent->s_name);
5206 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5207 sv_setpv(sv, sent->s_name);
5208 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5209 for (elem = sent->s_aliases; elem && *elem; elem++) {
5210 sv_catpv(sv, *elem);
5212 sv_catpvn(sv, " ", 1);
5214 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5216 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5218 sv_setiv(sv, (IV)(sent->s_port));
5220 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5221 sv_setpv(sv, sent->s_proto);
5226 DIE(aTHX_ PL_no_sock_func, "getservent");
5232 #ifdef HAS_SETHOSTENT
5234 PerlSock_sethostent(TOPi);
5237 DIE(aTHX_ PL_no_sock_func, "sethostent");
5243 #ifdef HAS_SETNETENT
5245 PerlSock_setnetent(TOPi);
5248 DIE(aTHX_ PL_no_sock_func, "setnetent");
5254 #ifdef HAS_SETPROTOENT
5256 PerlSock_setprotoent(TOPi);
5259 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5265 #ifdef HAS_SETSERVENT
5267 PerlSock_setservent(TOPi);
5270 DIE(aTHX_ PL_no_sock_func, "setservent");
5276 #ifdef HAS_ENDHOSTENT
5278 PerlSock_endhostent();
5282 DIE(aTHX_ PL_no_sock_func, "endhostent");
5288 #ifdef HAS_ENDNETENT
5290 PerlSock_endnetent();
5294 DIE(aTHX_ PL_no_sock_func, "endnetent");
5300 #ifdef HAS_ENDPROTOENT
5302 PerlSock_endprotoent();
5306 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5312 #ifdef HAS_ENDSERVENT
5314 PerlSock_endservent();
5318 DIE(aTHX_ PL_no_sock_func, "endservent");
5327 DIE(aTHX_ PL_no_func, "getpwnam");
5336 DIE(aTHX_ PL_no_func, "getpwuid");
5344 I32 which = PL_op->op_type;
5346 struct passwd *pwent = NULL;
5348 * We currently support only the SysV getsp* shadow password interface.
5349 * The interface is declared in <shadow.h> and often one needs to link
5350 * with -lsecurity or some such.
5351 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5354 * AIX getpwnam() is clever enough to return the encrypted password
5355 * only if the caller (euid?) is root.
5357 * There are at least three other shadow password APIs. Many platforms
5358 * seem to contain more than one interface for accessing the shadow
5359 * password databases, possibly for compatibility reasons.
5360 * The getsp*() is by far he simplest one, the other two interfaces
5361 * are much more complicated, but also very similar to each other.
5366 * struct pr_passwd *getprpw*();
5367 * The password is in
5368 * char getprpw*(...).ufld.fd_encrypt[]
5369 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5374 * struct es_passwd *getespw*();
5375 * The password is in
5376 * char *(getespw*(...).ufld.fd_encrypt)
5377 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5380 * struct userpw *getuserpw();
5381 * The password is in
5382 * char *(getuserpw(...)).spw_upw_passwd
5383 * (but the de facto standard getpwnam() should work okay)
5385 * Mention I_PROT here so that Configure probes for it.
5387 * In HP-UX for getprpw*() the manual page claims that one should include
5388 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5389 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5390 * and pp_sys.c already includes <shadow.h> if there is such.
5392 * Note that <sys/security.h> is already probed for, but currently
5393 * it is only included in special cases.
5395 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5396 * be preferred interface, even though also the getprpw*() interface
5397 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5398 * One also needs to call set_auth_parameters() in main() before
5399 * doing anything else, whether one is using getespw*() or getprpw*().
5401 * Note that accessing the shadow databases can be magnitudes
5402 * slower than accessing the standard databases.
5407 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5408 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5409 * the pw_comment is left uninitialized. */
5410 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5416 char* name = POPpbytex;
5417 pwent = getpwnam(name);
5423 pwent = getpwuid(uid);
5427 # ifdef HAS_GETPWENT
5429 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5430 if (pwent) pwent = getpwnam(pwent->pw_name);
5433 DIE(aTHX_ PL_no_func, "getpwent");
5439 if (GIMME != G_ARRAY) {
5440 PUSHs(sv = sv_newmortal());
5442 if (which == OP_GPWNAM)
5443 # if Uid_t_sign <= 0
5444 sv_setiv(sv, (IV)pwent->pw_uid);
5446 sv_setuv(sv, (UV)pwent->pw_uid);
5449 sv_setpv(sv, pwent->pw_name);
5455 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5456 sv_setpv(sv, pwent->pw_name);
5458 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5460 /* If we have getspnam(), we try to dig up the shadow
5461 * password. If we are underprivileged, the shadow
5462 * interface will set the errno to EACCES or similar,
5463 * and return a null pointer. If this happens, we will
5464 * use the dummy password (usually "*" or "x") from the
5465 * standard password database.
5467 * In theory we could skip the shadow call completely
5468 * if euid != 0 but in practice we cannot know which
5469 * security measures are guarding the shadow databases
5470 * on a random platform.
5472 * Resist the urge to use additional shadow interfaces.
5473 * Divert the urge to writing an extension instead.
5476 /* Some AIX setups falsely(?) detect some getspnam(), which
5477 * has a different API than the Solaris/IRIX one. */
5478 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5480 struct spwd *spwent;
5481 int saverrno; /* Save and restore errno so that
5482 * underprivileged attempts seem
5483 * to have never made the unsccessful
5484 * attempt to retrieve the shadow password. */
5487 spwent = getspnam(pwent->pw_name);
5489 if (spwent && spwent->sp_pwdp)
5490 sv_setpv(sv, spwent->sp_pwdp);
5494 if (!SvPOK(sv)) /* Use the standard password, then. */
5495 sv_setpv(sv, pwent->pw_passwd);
5498 # ifndef INCOMPLETE_TAINTS
5499 /* passwd is tainted because user himself can diddle with it.
5500 * admittedly not much and in a very limited way, but nevertheless. */
5504 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5505 # if Uid_t_sign <= 0
5506 sv_setiv(sv, (IV)pwent->pw_uid);
5508 sv_setuv(sv, (UV)pwent->pw_uid);
5511 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5512 # if Uid_t_sign <= 0
5513 sv_setiv(sv, (IV)pwent->pw_gid);
5515 sv_setuv(sv, (UV)pwent->pw_gid);
5517 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5518 * because of the poor interface of the Perl getpw*(),
5519 * not because there's some standard/convention saying so.
5520 * A better interface would have been to return a hash,
5521 * but we are accursed by our history, alas. --jhi. */
5522 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5524 sv_setiv(sv, (IV)pwent->pw_change);
5527 sv_setiv(sv, (IV)pwent->pw_quota);
5530 sv_setpv(sv, pwent->pw_age);
5535 /* pw_class and pw_comment are mutually exclusive--.
5536 * see the above note for pw_change, pw_quota, and pw_age. */
5537 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5539 sv_setpv(sv, pwent->pw_class);
5542 sv_setpv(sv, pwent->pw_comment);
5546 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5548 sv_setpv(sv, pwent->pw_gecos);
5550 # ifndef INCOMPLETE_TAINTS
5551 /* pw_gecos is tainted because user himself can diddle with it. */
5555 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5556 sv_setpv(sv, pwent->pw_dir);
5558 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5559 sv_setpv(sv, pwent->pw_shell);
5560 # ifndef INCOMPLETE_TAINTS
5561 /* pw_shell is tainted because user himself can diddle with it. */
5566 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5567 sv_setiv(sv, (IV)pwent->pw_expire);
5572 DIE(aTHX_ PL_no_func, "getpwent");
5578 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5583 DIE(aTHX_ PL_no_func, "setpwent");
5589 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5594 DIE(aTHX_ PL_no_func, "endpwent");
5603 DIE(aTHX_ PL_no_func, "getgrnam");
5612 DIE(aTHX_ PL_no_func, "getgrgid");
5620 I32 which = PL_op->op_type;
5621 register char **elem;
5623 struct group *grent;
5625 if (which == OP_GGRNAM) {
5626 char* name = POPpbytex;
5627 grent = (struct group *)getgrnam(name);
5629 else if (which == OP_GGRGID) {
5631 grent = (struct group *)getgrgid(gid);
5635 grent = (struct group *)getgrent();
5637 DIE(aTHX_ PL_no_func, "getgrent");
5641 if (GIMME != G_ARRAY) {
5642 PUSHs(sv = sv_newmortal());
5644 if (which == OP_GGRNAM)
5645 sv_setiv(sv, (IV)grent->gr_gid);
5647 sv_setpv(sv, grent->gr_name);
5653 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5654 sv_setpv(sv, grent->gr_name);
5656 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5658 sv_setpv(sv, grent->gr_passwd);
5661 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5662 sv_setiv(sv, (IV)grent->gr_gid);
5664 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5665 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5666 /* In UNICOS/mk (_CRAYMPP) the multithreading
5667 * versions (getgrnam_r, getgrgid_r)
5668 * seem to return an illegal pointer
5669 * as the group members list, gr_mem.
5670 * getgrent() doesn't even have a _r version
5671 * but the gr_mem is poisonous anyway.
5672 * So yes, you cannot get the list of group
5673 * members if building multithreaded in UNICOS/mk. */
5674 for (elem = grent->gr_mem; elem && *elem; elem++) {
5675 sv_catpv(sv, *elem);
5677 sv_catpvn(sv, " ", 1);
5684 DIE(aTHX_ PL_no_func, "getgrent");
5690 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5695 DIE(aTHX_ PL_no_func, "setgrent");
5701 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5706 DIE(aTHX_ PL_no_func, "endgrent");
5716 if (!(tmps = PerlProc_getlogin()))
5718 PUSHp(tmps, strlen(tmps));
5721 DIE(aTHX_ PL_no_func, "getlogin");
5725 /* Miscellaneous. */
5730 dSP; dMARK; dORIGMARK; dTARGET;
5731 register I32 items = SP - MARK;
5732 unsigned long a[20];
5737 while (++MARK <= SP) {
5738 if (SvTAINTED(*MARK)) {
5744 TAINT_PROPER("syscall");
5747 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5748 * or where sizeof(long) != sizeof(char*). But such machines will
5749 * not likely have syscall implemented either, so who cares?
5751 while (++MARK <= SP) {
5752 if (SvNIOK(*MARK) || !i)
5753 a[i++] = SvIV(*MARK);
5754 else if (*MARK == &PL_sv_undef)
5757 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5763 DIE(aTHX_ "Too many args to syscall");
5765 DIE(aTHX_ "Too few args to syscall");
5767 retval = syscall(a[0]);
5770 retval = syscall(a[0],a[1]);
5773 retval = syscall(a[0],a[1],a[2]);
5776 retval = syscall(a[0],a[1],a[2],a[3]);
5779 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5782 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5785 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5788 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5792 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5795 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5798 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5802 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5806 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5810 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5811 a[10],a[11],a[12],a[13]);
5813 #endif /* atarist */
5819 DIE(aTHX_ PL_no_func, "syscall");
5823 #ifdef FCNTL_EMULATE_FLOCK
5825 /* XXX Emulate flock() with fcntl().
5826 What's really needed is a good file locking module.
5830 fcntl_emulate_flock(int fd, int operation)
5834 switch (operation & ~LOCK_NB) {
5836 flock.l_type = F_RDLCK;
5839 flock.l_type = F_WRLCK;
5842 flock.l_type = F_UNLCK;
5848 flock.l_whence = SEEK_SET;
5849 flock.l_start = flock.l_len = (Off_t)0;
5851 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5854 #endif /* FCNTL_EMULATE_FLOCK */
5856 #ifdef LOCKF_EMULATE_FLOCK
5858 /* XXX Emulate flock() with lockf(). This is just to increase
5859 portability of scripts. The calls are not completely
5860 interchangeable. What's really needed is a good file
5864 /* The lockf() constants might have been defined in <unistd.h>.
5865 Unfortunately, <unistd.h> causes troubles on some mixed
5866 (BSD/POSIX) systems, such as SunOS 4.1.3.
5868 Further, the lockf() constants aren't POSIX, so they might not be
5869 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5870 just stick in the SVID values and be done with it. Sigh.
5874 # define F_ULOCK 0 /* Unlock a previously locked region */
5877 # define F_LOCK 1 /* Lock a region for exclusive use */
5880 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5883 # define F_TEST 3 /* Test a region for other processes locks */
5887 lockf_emulate_flock(int fd, int operation)
5893 /* flock locks entire file so for lockf we need to do the same */
5895 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5896 if (pos > 0) /* is seekable and needs to be repositioned */
5897 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5898 pos = -1; /* seek failed, so don't seek back afterwards */
5901 switch (operation) {
5903 /* LOCK_SH - get a shared lock */
5905 /* LOCK_EX - get an exclusive lock */
5907 i = lockf (fd, F_LOCK, 0);
5910 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5911 case LOCK_SH|LOCK_NB:
5912 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5913 case LOCK_EX|LOCK_NB:
5914 i = lockf (fd, F_TLOCK, 0);
5916 if ((errno == EAGAIN) || (errno == EACCES))
5917 errno = EWOULDBLOCK;
5920 /* LOCK_UN - unlock (non-blocking is a no-op) */
5922 case LOCK_UN|LOCK_NB:
5923 i = lockf (fd, F_ULOCK, 0);
5926 /* Default - can't decipher operation */
5933 if (pos > 0) /* need to restore position of the handle */
5934 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5939 #endif /* LOCKF_EMULATE_FLOCK */
5943 * c-indentation-style: bsd
5945 * indent-tabs-mode: t
5948 * ex: set ts=8 sts=4 sw=4 noet: