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 (void);
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
126 #else /* no flock() */
128 /* fcntl.h might not have been included, even if it exists, because
129 the current Configure only sets I_FCNTL if it's needed to pick up
130 the *_OK constants. Make sure it has been included before testing
131 the fcntl() locking constants. */
132 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
136 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
137 # define FLOCK fcntl_emulate_flock
138 # define FCNTL_EMULATE_FLOCK
139 # else /* no flock() or fcntl(F_SETLK,...) */
141 # define FLOCK lockf_emulate_flock
142 # define LOCKF_EMULATE_FLOCK
144 # endif /* no flock() or fcntl(F_SETLK,...) */
147 static int FLOCK (int, int);
150 * These are the flock() constants. Since this sytems doesn't have
151 * flock(), the values of the constants are probably not available.
165 # endif /* emulating flock() */
167 #endif /* no flock() */
170 static char zero_but_true[ZBTLEN + 1] = "0 but true";
172 #if defined(I_SYS_ACCESS) && !defined(R_OK)
173 # include <sys/access.h>
176 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
177 # define FD_CLOEXEC 1 /* NeXT needs this */
183 /* Missing protos on LynxOS */
184 void sethostent(int);
185 void endhostent(void);
187 void endnetent(void);
188 void setprotoent(int);
189 void endprotoent(void);
190 void setservent(int);
191 void endservent(void);
194 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
195 #undef PERL_EFF_ACCESS_W_OK
196 #undef PERL_EFF_ACCESS_X_OK
198 /* AIX 5.2 and below use mktime for localtime, and defines the edge case
199 * for time 0x7fffffff to be valid only in UTC. AIX 5.3 provides localtime64
200 * available in the 32bit environment, which could warrant Configure
201 * checks in the future.
204 #define LOCALTIME_EDGECASE_BROKEN
207 /* F_OK unused: if stat() cannot find it... */
209 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
210 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
211 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
212 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
213 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
216 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
217 # ifdef I_SYS_SECURITY
218 # include <sys/security.h>
222 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
223 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
224 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
227 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
228 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
229 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
233 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
235 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
236 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
237 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
240 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
241 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
242 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
245 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
247 Uid_t ruid = getuid();
248 Uid_t euid = geteuid();
249 Gid_t rgid = getgid();
250 Gid_t egid = getegid();
254 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
255 Perl_croak(aTHX_ "switching effective uid is not implemented");
258 if (setreuid(euid, ruid))
261 if (setresuid(euid, ruid, (Uid_t)-1))
264 Perl_croak(aTHX_ "entering effective uid failed");
267 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
268 Perl_croak(aTHX_ "switching effective gid is not implemented");
271 if (setregid(egid, rgid))
274 if (setresgid(egid, rgid, (Gid_t)-1))
277 Perl_croak(aTHX_ "entering effective gid failed");
280 res = access(path, mode);
283 if (setreuid(ruid, euid))
286 if (setresuid(ruid, euid, (Uid_t)-1))
289 Perl_croak(aTHX_ "leaving effective uid failed");
292 if (setregid(rgid, egid))
295 if (setresgid(rgid, egid, (Gid_t)-1))
298 Perl_croak(aTHX_ "leaving effective gid failed");
303 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
304 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
305 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
308 #if !defined(PERL_EFF_ACCESS_R_OK)
309 /* With it or without it: anyway you get a warning: either that
310 it is unused, or it is declared static and never defined.
313 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
317 Perl_croak(aTHX_ "switching effective uid is not implemented");
327 const char * const tmps = POPpconstx;
328 const I32 gimme = GIMME_V;
329 const char *mode = "r";
332 if (PL_op->op_private & OPpOPEN_IN_RAW)
334 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
336 fp = PerlProc_popen((char*)tmps, (char *)mode);
338 const char *type = NULL;
339 if (PL_curcop->cop_io) {
340 type = SvPV_nolen_const(PL_curcop->cop_io);
343 PerlIO_apply_layers(aTHX_ fp,mode,type);
345 if (gimme == G_VOID) {
347 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
350 else if (gimme == G_SCALAR) {
353 PL_rs = &PL_sv_undef;
354 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
355 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
363 SV * const sv = NEWSV(56, 79);
364 if (sv_gets(sv, fp, 0) == Nullch) {
368 XPUSHs(sv_2mortal(sv));
369 if (SvLEN(sv) - SvCUR(sv) > 20) {
370 SvPV_shrink_to_cur(sv);
375 STATUS_NATIVE_SET(PerlProc_pclose(fp));
376 TAINT; /* "I believe that this is not gratuitous!" */
379 STATUS_NATIVE_SET(-1);
380 if (gimme == G_SCALAR)
390 tryAMAGICunTARGET(iter, -1);
392 /* Note that we only ever get here if File::Glob fails to load
393 * without at the same time croaking, for some reason, or if
394 * perl was built with PERL_EXTERNAL_GLOB */
401 * The external globbing program may use things we can't control,
402 * so for security reasons we must assume the worst.
405 taint_proper(PL_no_security, "glob");
409 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
410 PL_last_in_gv = (GV*)*PL_stack_sp--;
412 SAVESPTR(PL_rs); /* This is not permanent, either. */
413 PL_rs = sv_2mortal(newSVpvn("\000", 1));
416 *SvPVX(PL_rs) = '\n';
420 result = do_readline();
427 PL_last_in_gv = cGVOP_gv;
428 return do_readline();
437 if (SP - MARK != 1) {
439 do_join(TARG, &PL_sv_no, MARK, SP);
446 tmps = SvPV_const(tmpsv, len);
447 if ((!tmps || !len) && PL_errgv) {
448 SV * const error = ERRSV;
449 (void)SvUPGRADE(error, SVt_PV);
450 if (SvPOK(error) && SvCUR(error))
451 sv_catpv(error, "\t...caught");
453 tmps = SvPV_const(tmpsv, len);
456 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
458 Perl_warn(aTHX_ "%"SVf, tmpsv);
470 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
472 if (SP - MARK != 1) {
474 do_join(TARG, &PL_sv_no, MARK, SP);
476 tmps = SvPV_const(tmpsv, len);
482 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
486 (void)SvUPGRADE(error, SVt_PV);
487 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
489 SvSetSV(error,tmpsv);
490 else if (sv_isobject(error)) {
491 HV *stash = SvSTASH(SvRV(error));
492 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
494 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
495 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
502 call_sv((SV*)GvCV(gv),
503 G_SCALAR|G_EVAL|G_KEEPERR);
504 sv_setsv(error,*PL_stack_sp--);
510 if (SvPOK(error) && SvCUR(error))
511 sv_catpv(error, "\t...propagated");
514 tmps = SvPV_const(tmpsv, len);
520 tmpsv = sv_2mortal(newSVpvn("Died", 4));
522 DIE(aTHX_ "%"SVf, tmpsv);
542 DIE(aTHX_ PL_no_usym, "filehandle");
543 if ((io = GvIOp(gv)))
544 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
546 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
547 /* Method's args are same as ours ... */
548 /* ... except handle is replaced by the object */
549 *MARK-- = SvTIED_obj((SV*)io, mg);
553 call_method("OPEN", G_SCALAR);
566 tmps = SvPV_const(sv, len);
567 ok = do_openn(gv, (char *)tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
570 PUSHi( (I32)PL_forkprocess );
571 else if (PL_forkprocess == 0) /* we are a new child */
590 if (gv && (io = GvIO(gv))
591 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
594 XPUSHs(SvTIED_obj((SV*)io, mg));
597 call_method("CLOSE", G_SCALAR);
603 PUSHs(boolSV(do_close(gv, TRUE)));
623 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
624 DIE(aTHX_ PL_no_usym, "filehandle");
629 do_close(rgv, FALSE);
631 do_close(wgv, FALSE);
633 if (PerlProc_pipe(fd) < 0)
636 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
637 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
638 IoOFP(rstio) = IoIFP(rstio);
639 IoIFP(wstio) = IoOFP(wstio);
640 IoTYPE(rstio) = IoTYPE_RDONLY;
641 IoTYPE(wstio) = IoTYPE_WRONLY;
643 if (!IoIFP(rstio) || !IoOFP(wstio)) {
644 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
645 else PerlLIO_close(fd[0]);
646 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
647 else PerlLIO_close(fd[1]);
650 #if defined(HAS_FCNTL) && defined(F_SETFD)
651 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
652 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
659 DIE(aTHX_ PL_no_func, "pipe");
675 if (gv && (io = GvIO(gv))
676 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
679 XPUSHs(SvTIED_obj((SV*)io, mg));
682 call_method("FILENO", G_SCALAR);
688 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
689 /* Can't do this because people seem to do things like
690 defined(fileno($foo)) to check whether $foo is a valid fh.
691 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
692 report_evil_fh(gv, io, PL_op->op_type);
697 PUSHi(PerlIO_fileno(fp));
708 anum = PerlLIO_umask(0);
709 (void)PerlLIO_umask(anum);
712 anum = PerlLIO_umask(POPi);
713 TAINT_PROPER("umask");
716 /* Only DIE if trying to restrict permissions on "user" (self).
717 * Otherwise it's harmless and more useful to just return undef
718 * since 'group' and 'other' concepts probably don't exist here. */
719 if (MAXARG >= 1 && (POPi & 0700))
720 DIE(aTHX_ "umask not implemented");
721 XPUSHs(&PL_sv_undef);
743 if (gv && (io = GvIO(gv))
744 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
747 XPUSHs(SvTIED_obj((SV*)io, mg));
752 call_method("BINMODE", G_SCALAR);
759 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
760 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
761 report_evil_fh(gv, io, PL_op->op_type);
762 SETERRNO(EBADF,RMS_IFI);
767 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
768 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
769 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
770 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
771 mode_from_discipline(discp),
772 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
794 const I32 markoff = MARK - PL_stack_base;
795 const char *methname;
796 int how = PERL_MAGIC_tied;
800 switch(SvTYPE(varsv)) {
802 methname = "TIEHASH";
803 HvEITER_set((HV *)varsv, 0);
806 methname = "TIEARRAY";
809 #ifdef GV_UNIQUE_CHECK
810 if (GvUNIQUE((GV*)varsv)) {
811 Perl_croak(aTHX_ "Attempt to tie unique GV");
814 methname = "TIEHANDLE";
815 how = PERL_MAGIC_tiedscalar;
816 /* For tied filehandles, we apply tiedscalar magic to the IO
817 slot of the GP rather than the GV itself. AMS 20010812 */
819 GvIOp(varsv) = newIO();
820 varsv = (SV *)GvIOp(varsv);
823 methname = "TIESCALAR";
824 how = PERL_MAGIC_tiedscalar;
828 if (sv_isobject(*MARK)) {
830 PUSHSTACKi(PERLSI_MAGIC);
832 EXTEND(SP,(I32)items);
836 call_method(methname, G_SCALAR);
839 /* Not clear why we don't call call_method here too.
840 * perhaps to get different error message ?
842 stash = gv_stashsv(*MARK, FALSE);
843 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
844 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
848 PUSHSTACKi(PERLSI_MAGIC);
850 EXTEND(SP,(I32)items);
854 call_sv((SV*)GvCV(gv), G_SCALAR);
860 if (sv_isobject(sv)) {
861 sv_unmagic(varsv, how);
862 /* Croak if a self-tie on an aggregate is attempted. */
863 if (varsv == SvRV(sv) &&
864 (SvTYPE(varsv) == SVt_PVAV ||
865 SvTYPE(varsv) == SVt_PVHV))
867 "Self-ties of arrays and hashes are not supported");
868 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
871 SP = PL_stack_base + markoff;
881 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
882 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
884 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
887 if ((mg = SvTIED_mg(sv, how))) {
888 SV * const obj = SvRV(SvTIED_obj(sv, mg));
892 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
893 isGV(gv) && (cv = GvCV(gv))) {
895 XPUSHs(SvTIED_obj((SV*)gv, mg));
896 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
899 call_sv((SV *)cv, G_VOID);
903 else if (ckWARN(WARN_UNTIE)) {
904 if (mg && SvREFCNT(obj) > 1)
905 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
906 "untie attempted while %"UVuf" inner references still exist",
907 (UV)SvREFCNT(obj) - 1 ) ;
911 sv_unmagic(sv, how) ;
920 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
921 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
923 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
926 if ((mg = SvTIED_mg(sv, how))) {
927 SV *osv = SvTIED_obj(sv, mg);
928 if (osv == mg->mg_obj)
929 osv = sv_mortalcopy(osv);
944 HV * const hv = (HV*)POPs;
946 sv = sv_mortalcopy(&PL_sv_no);
947 sv_setpv(sv, "AnyDBM_File");
948 stash = gv_stashsv(sv, FALSE);
949 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
951 require_pv("AnyDBM_File.pm");
953 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
954 DIE(aTHX_ "No dbm on this machine");
964 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
966 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
969 call_sv((SV*)GvCV(gv), G_SCALAR);
972 if (!sv_isobject(TOPs)) {
977 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
980 call_sv((SV*)GvCV(gv), G_SCALAR);
984 if (sv_isobject(TOPs)) {
985 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
986 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
1008 struct timeval timebuf;
1009 struct timeval *tbuf = &timebuf;
1012 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1017 # if BYTEORDER & 0xf0000
1018 # define ORDERBYTE (0x88888888 - BYTEORDER)
1020 # define ORDERBYTE (0x4444 - BYTEORDER)
1026 for (i = 1; i <= 3; i++) {
1028 if (SvOK(sv) && SvREADONLY(sv)) {
1030 sv_force_normal_flags(sv, 0);
1032 DIE(aTHX_ PL_no_modify);
1041 /* little endians can use vecs directly */
1042 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1049 masksize = NFDBITS / NBBY;
1051 masksize = sizeof(long); /* documented int, everyone seems to use long */
1053 Zero(&fd_sets[0], 4, char*);
1056 # if SELECT_MIN_BITS == 1
1057 growsize = sizeof(fd_set);
1059 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1060 # undef SELECT_MIN_BITS
1061 # define SELECT_MIN_BITS __FD_SETSIZE
1063 /* If SELECT_MIN_BITS is greater than one we most probably will want
1064 * to align the sizes with SELECT_MIN_BITS/8 because for example
1065 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1066 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1067 * on (sets/tests/clears bits) is 32 bits. */
1068 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1076 timebuf.tv_sec = (long)value;
1077 value -= (NV)timebuf.tv_sec;
1078 timebuf.tv_usec = (long)(value * 1000000.0);
1081 tbuf = Null(struct timeval*);
1083 for (i = 1; i <= 3; i++) {
1089 else if (!SvPOK(sv))
1090 SvPV_force_nolen(sv); /* force string conversion */
1093 Sv_Grow(sv, growsize);
1097 while (++j <= growsize) {
1101 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1103 Newx(fd_sets[i], growsize, char);
1104 for (offset = 0; offset < growsize; offset += masksize) {
1105 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1106 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1109 fd_sets[i] = SvPVX(sv);
1113 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1114 /* Can't make just the (void*) conditional because that would be
1115 * cpp #if within cpp macro, and not all compilers like that. */
1116 nfound = PerlSock_select(
1118 (Select_fd_set_t) fd_sets[1],
1119 (Select_fd_set_t) fd_sets[2],
1120 (Select_fd_set_t) fd_sets[3],
1121 (void*) tbuf); /* Workaround for compiler bug. */
1123 nfound = PerlSock_select(
1125 (Select_fd_set_t) fd_sets[1],
1126 (Select_fd_set_t) fd_sets[2],
1127 (Select_fd_set_t) fd_sets[3],
1130 for (i = 1; i <= 3; i++) {
1133 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1135 for (offset = 0; offset < growsize; offset += masksize) {
1136 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1137 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1139 Safefree(fd_sets[i]);
1146 if (GIMME == G_ARRAY && tbuf) {
1147 value = (NV)(timebuf.tv_sec) +
1148 (NV)(timebuf.tv_usec) / 1000000.0;
1149 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1150 sv_setnv(sv, value);
1154 DIE(aTHX_ "select not implemented");
1159 Perl_setdefout(pTHX_ GV *gv)
1162 (void)SvREFCNT_inc(gv);
1164 SvREFCNT_dec(PL_defoutgv);
1174 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1176 egv = GvEGV(PL_defoutgv);
1181 XPUSHs(&PL_sv_undef);
1183 GV ** const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1184 if (gvp && *gvp == egv) {
1185 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1189 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1194 if (!GvIO(newdefout))
1195 gv_IOadd(newdefout);
1196 setdefout(newdefout);
1207 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1209 if (gv && (io = GvIO(gv))
1210 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1212 const I32 gimme = GIMME_V;
1214 XPUSHs(SvTIED_obj((SV*)io, mg));
1217 call_method("GETC", gimme);
1220 if (gimme == G_SCALAR)
1221 SvSetMagicSV_nosteal(TARG, TOPs);
1224 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1225 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1226 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1227 report_evil_fh(gv, io, PL_op->op_type);
1228 SETERRNO(EBADF,RMS_IFI);
1232 sv_setpvn(TARG, " ", 1);
1233 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1234 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1235 /* Find out how many bytes the char needs */
1236 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1239 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1240 SvCUR_set(TARG,1+len);
1250 return pp_sysread();
1254 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1256 register PERL_CONTEXT *cx;
1257 const I32 gimme = GIMME_V;
1263 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1266 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1268 setdefout(gv); /* locally select filehandle so $% et al work */
1300 SV * const tmpsv = sv_newmortal();
1302 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1303 name = SvPV_nolen_const(tmpsv);
1305 DIE(aTHX_ "Undefined format \"%s\" called", name);
1307 DIE(aTHX_ "Not a format reference");
1310 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1312 IoFLAGS(io) &= ~IOf_DIDTOP;
1313 return doform(cv,gv,PL_op->op_next);
1319 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1320 register IO * const io = GvIOp(gv);
1321 PerlIO * const ofp = IoOFP(io);
1325 register PERL_CONTEXT *cx;
1327 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1328 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1331 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1332 PL_formtarget != PL_toptarget)
1336 if (!IoTOP_GV(io)) {
1339 if (!IoTOP_NAME(io)) {
1341 if (!IoFMT_NAME(io))
1342 IoFMT_NAME(io) = savepv(GvNAME(gv));
1343 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1344 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1345 if ((topgv && GvFORM(topgv)) ||
1346 !gv_fetchpv("top",FALSE,SVt_PVFM))
1347 IoTOP_NAME(io) = savesvpv(topname);
1349 IoTOP_NAME(io) = savepv("top");
1351 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1352 if (!topgv || !GvFORM(topgv)) {
1353 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1356 IoTOP_GV(io) = topgv;
1358 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1359 I32 lines = IoLINES_LEFT(io);
1360 const char *s = SvPVX_const(PL_formtarget);
1361 if (lines <= 0) /* Yow, header didn't even fit!!! */
1363 while (lines-- > 0) {
1364 s = strchr(s, '\n');
1370 const STRLEN save = SvCUR(PL_formtarget);
1371 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1372 do_print(PL_formtarget, ofp);
1373 SvCUR_set(PL_formtarget, save);
1374 sv_chop(PL_formtarget, s);
1375 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1378 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1379 do_print(PL_formfeed, ofp);
1380 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1382 PL_formtarget = PL_toptarget;
1383 IoFLAGS(io) |= IOf_DIDTOP;
1386 DIE(aTHX_ "bad top format reference");
1389 SV * const sv = sv_newmortal();
1391 gv_efullname4(sv, fgv, Nullch, FALSE);
1392 name = SvPV_nolen_const(sv);
1394 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1398 DIE(aTHX_ "Undefined top format called");
1401 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1402 return doform(cv,gv,PL_op);
1406 POPBLOCK(cx,PL_curpm);
1412 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1414 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1415 else if (ckWARN(WARN_CLOSED))
1416 report_evil_fh(gv, io, PL_op->op_type);
1421 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1422 if (ckWARN(WARN_IO))
1423 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1425 if (!do_print(PL_formtarget, fp))
1428 FmLINES(PL_formtarget) = 0;
1429 SvCUR_set(PL_formtarget, 0);
1430 *SvEND(PL_formtarget) = '\0';
1431 if (IoFLAGS(io) & IOf_FLUSH)
1432 (void)PerlIO_flush(fp);
1437 PL_formtarget = PL_bodytarget;
1439 PERL_UNUSED_VAR(newsp);
1440 PERL_UNUSED_VAR(gimme);
1441 return pop_return();
1446 dSP; dMARK; dORIGMARK;
1453 if (PL_op->op_flags & OPf_STACKED)
1458 if (gv && (io = GvIO(gv))
1459 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1461 if (MARK == ORIGMARK) {
1464 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1468 *MARK = SvTIED_obj((SV*)io, mg);
1471 call_method("PRINTF", G_SCALAR);
1474 MARK = ORIGMARK + 1;
1481 if (!(io = GvIO(gv))) {
1482 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1483 report_evil_fh(gv, io, PL_op->op_type);
1484 SETERRNO(EBADF,RMS_IFI);
1487 else if (!(fp = IoOFP(io))) {
1488 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1490 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1491 else if (ckWARN(WARN_CLOSED))
1492 report_evil_fh(gv, io, PL_op->op_type);
1494 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1498 do_sprintf(sv, SP - MARK, MARK + 1);
1499 if (!do_print(sv, fp))
1502 if (IoFLAGS(io) & IOf_FLUSH)
1503 if (PerlIO_flush(fp) == EOF)
1514 PUSHs(&PL_sv_undef);
1521 const int perm = (MAXARG > 3) ? POPi : 0666;
1522 const int mode = POPi;
1523 SV * const sv = POPs;
1524 GV * const gv = (GV *)POPs;
1527 /* Need TIEHANDLE method ? */
1528 const char * const tmps = SvPV_const(sv, len);
1529 /* FIXME? do_open should do const */
1530 if (do_open(gv, (char*)tmps, len, TRUE, mode, perm, Nullfp)) {
1531 IoLINES(GvIOp(gv)) = 0;
1535 PUSHs(&PL_sv_undef);
1542 dSP; dMARK; dORIGMARK; dTARGET;
1548 Sock_size_t bufsize;
1556 bool charstart = FALSE;
1557 STRLEN charskip = 0;
1560 GV * const gv = (GV*)*++MARK;
1561 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1562 && gv && (io = GvIO(gv)) )
1564 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1568 *MARK = SvTIED_obj((SV*)io, mg);
1570 call_method("READ", G_SCALAR);
1584 sv_setpvn(bufsv, "", 0);
1585 length = SvIVx(*++MARK);
1588 offset = SvIVx(*++MARK);
1592 if (!io || !IoIFP(io)) {
1593 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1594 report_evil_fh(gv, io, PL_op->op_type);
1595 SETERRNO(EBADF,RMS_IFI);
1598 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1599 buffer = SvPVutf8_force(bufsv, blen);
1600 /* UTF-8 may not have been set if they are all low bytes */
1605 buffer = SvPV_force(bufsv, blen);
1606 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1609 DIE(aTHX_ "Negative length");
1617 if (PL_op->op_type == OP_RECV) {
1618 char namebuf[MAXPATHLEN];
1619 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1620 bufsize = sizeof (struct sockaddr_in);
1622 bufsize = sizeof namebuf;
1624 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1628 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1629 /* 'offset' means 'flags' here */
1630 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1631 (struct sockaddr *)namebuf, &bufsize);
1635 /* Bogus return without padding */
1636 bufsize = sizeof (struct sockaddr_in);
1638 SvCUR_set(bufsv, count);
1639 *SvEND(bufsv) = '\0';
1640 (void)SvPOK_only(bufsv);
1644 /* This should not be marked tainted if the fp is marked clean */
1645 if (!(IoFLAGS(io) & IOf_UNTAINT))
1646 SvTAINTED_on(bufsv);
1648 sv_setpvn(TARG, namebuf, bufsize);
1653 if (PL_op->op_type == OP_RECV)
1654 DIE(aTHX_ PL_no_sock_func, "recv");
1656 if (DO_UTF8(bufsv)) {
1657 /* offset adjust in characters not bytes */
1658 blen = sv_len_utf8(bufsv);
1661 if (-offset > (int)blen)
1662 DIE(aTHX_ "Offset outside string");
1665 if (DO_UTF8(bufsv)) {
1666 /* convert offset-as-chars to offset-as-bytes */
1667 if (offset >= (int)blen)
1668 offset += SvCUR(bufsv) - blen;
1670 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1673 bufsize = SvCUR(bufsv);
1674 /* Allocating length + offset + 1 isn't perfect in the case of reading
1675 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1677 (should be 2 * length + offset + 1, or possibly something longer if
1678 PL_encoding is true) */
1679 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1680 if (offset > bufsize) { /* Zero any newly allocated space */
1681 Zero(buffer+bufsize, offset-bufsize, char);
1683 buffer = buffer + offset;
1685 read_target = bufsv;
1687 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1688 concatenate it to the current buffer. */
1690 /* Truncate the existing buffer to the start of where we will be
1692 SvCUR_set(bufsv, offset);
1694 read_target = sv_newmortal();
1695 (void)SvUPGRADE(read_target, SVt_PV);
1696 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1699 if (PL_op->op_type == OP_SYSREAD) {
1700 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1701 if (IoTYPE(io) == IoTYPE_SOCKET) {
1702 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1708 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1713 #ifdef HAS_SOCKET__bad_code_maybe
1714 if (IoTYPE(io) == IoTYPE_SOCKET) {
1715 char namebuf[MAXPATHLEN];
1716 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1717 bufsize = sizeof (struct sockaddr_in);
1719 bufsize = sizeof namebuf;
1721 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1722 (struct sockaddr *)namebuf, &bufsize);
1727 count = PerlIO_read(IoIFP(io), buffer, length);
1728 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1729 if (count == 0 && PerlIO_error(IoIFP(io)))
1733 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1734 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1737 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1738 *SvEND(read_target) = '\0';
1739 (void)SvPOK_only(read_target);
1740 if (fp_utf8 && !IN_BYTES) {
1741 /* Look at utf8 we got back and count the characters */
1742 const char *bend = buffer + count;
1743 while (buffer < bend) {
1745 skip = UTF8SKIP(buffer);
1748 if (buffer - charskip + skip > bend) {
1749 /* partial character - try for rest of it */
1750 length = skip - (bend-buffer);
1751 offset = bend - SvPVX_const(bufsv);
1763 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1764 provided amount read (count) was what was requested (length)
1766 if (got < wanted && count == length) {
1767 length = wanted - got;
1768 offset = bend - SvPVX_const(bufsv);
1771 /* return value is character count */
1775 else if (buffer_utf8) {
1776 /* Let svcatsv upgrade the bytes we read in to utf8.
1777 The buffer is a mortal so will be freed soon. */
1778 sv_catsv_nomg(bufsv, read_target);
1781 /* This should not be marked tainted if the fp is marked clean */
1782 if (!(IoFLAGS(io) & IOf_UNTAINT))
1783 SvTAINTED_on(bufsv);
1796 const int items = (SP - PL_stack_base) - TOPMARK;
1800 sv = sv_2mortal(newSViv(sv_len(*SP)));
1809 dSP; dMARK; dORIGMARK; dTARGET;
1820 if (PL_op->op_type == OP_SYSWRITE
1821 && gv && (io = GvIO(gv))
1822 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1827 *MARK = SvTIED_obj((SV*)io, mg);
1829 call_method("WRITE", G_SCALAR);
1840 #if Size_t_size > IVSIZE
1841 length = (Size_t)SvNVx(*++MARK);
1843 length = (Size_t)SvIVx(*++MARK);
1845 if ((SSize_t)length < 0)
1846 DIE(aTHX_ "Negative length");
1849 if (!io || !IoIFP(io)) {
1851 if (ckWARN(WARN_CLOSED))
1852 report_evil_fh(gv, io, PL_op->op_type);
1853 SETERRNO(EBADF,RMS_IFI);
1857 if (PerlIO_isutf8(IoIFP(io))) {
1858 if (!SvUTF8(bufsv)) {
1859 bufsv = sv_2mortal(newSVsv(bufsv));
1860 buffer = sv_2pvutf8(bufsv, &blen);
1862 buffer = SvPV_const(bufsv, blen);
1865 if (DO_UTF8(bufsv)) {
1866 /* Not modifying source SV, so making a temporary copy. */
1867 bufsv = sv_2mortal(newSVsv(bufsv));
1868 sv_utf8_downgrade(bufsv, FALSE);
1870 buffer = SvPV_const(bufsv, blen);
1873 if (PL_op->op_type == OP_SYSWRITE) {
1875 if (DO_UTF8(bufsv)) {
1876 /* length and offset are in chars */
1877 blen = sv_len_utf8(bufsv);
1880 offset = SvIVx(*++MARK);
1882 if (-offset > (IV)blen)
1883 DIE(aTHX_ "Offset outside string");
1885 } else if (offset >= (IV)blen && blen > 0)
1886 DIE(aTHX_ "Offset outside string");
1889 if (length > blen - offset)
1890 length = blen - offset;
1891 if (DO_UTF8(bufsv)) {
1892 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1893 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1896 buffer = buffer+offset;
1898 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1899 if (IoTYPE(io) == IoTYPE_SOCKET) {
1900 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1906 /* See the note at doio.c:do_print about filesize limits. --jhi */
1907 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1912 else if (SP > MARK) {
1914 char * const sockbuf = SvPVx(*++MARK, mlen);
1915 /* length is really flags */
1916 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1917 length, (struct sockaddr *)sockbuf, mlen);
1920 /* length is really flags */
1921 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1924 DIE(aTHX_ PL_no_sock_func, "send");
1930 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1931 #if Size_t_size > IVSIZE
1945 return pp_sysread();
1956 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1958 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1960 if (io && !IoIFP(io)) {
1961 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1963 IoFLAGS(io) &= ~IOf_START;
1964 do_open(gv, (char *)"-", 1, FALSE, O_RDONLY, 0, Nullfp);
1965 sv_setpvn(GvSV(gv), "-", 1);
1966 SvSETMAGIC(GvSV(gv));
1968 else if (!nextargv(gv))
1973 gv = PL_last_in_gv; /* eof */
1976 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1978 if (gv && (io = GvIO(gv))
1979 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1982 XPUSHs(SvTIED_obj((SV*)io, mg));
1985 call_method("EOF", G_SCALAR);
1991 PUSHs(boolSV(!gv || do_eof(gv)));
2005 gv = PL_last_in_gv = (GV*)POPs;
2007 if (gv && (io = GvIO(gv))
2008 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2011 XPUSHs(SvTIED_obj((SV*)io, mg));
2014 call_method("TELL", G_SCALAR);
2020 #if LSEEKSIZE > IVSIZE
2021 PUSHn( do_tell(gv) );
2023 PUSHi( do_tell(gv) );
2030 return pp_sysseek();
2038 const int whence = POPi;
2039 #if LSEEKSIZE > IVSIZE
2040 Off_t offset = (Off_t)SvNVx(POPs);
2042 Off_t offset = (Off_t)SvIVx(POPs);
2046 gv = PL_last_in_gv = (GV*)POPs;
2048 if (gv && (io = GvIO(gv))
2049 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2052 XPUSHs(SvTIED_obj((SV*)io, mg));
2053 #if LSEEKSIZE > IVSIZE
2054 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2056 XPUSHs(sv_2mortal(newSViv(offset)));
2058 XPUSHs(sv_2mortal(newSViv(whence)));
2061 call_method("SEEK", G_SCALAR);
2067 if (PL_op->op_type == OP_SEEK)
2068 PUSHs(boolSV(do_seek(gv, offset, whence)));
2070 Off_t sought = do_sysseek(gv, offset, whence);
2072 PUSHs(&PL_sv_undef);
2075 #if LSEEKSIZE > IVSIZE
2080 : newSVpvn(zero_but_true, ZBTLEN);
2081 PUSHs(sv_2mortal(sv));
2090 /* There seems to be no consensus on the length type of truncate()
2091 * and ftruncate(), both off_t and size_t have supporters. In
2092 * general one would think that when using large files, off_t is
2093 * at least as wide as size_t, so using an off_t should be okay. */
2094 /* XXX Configure probe for the length type of *truncate() needed XXX */
2097 #if Off_t_size > IVSIZE
2102 /* Checking for length < 0 is problematic as the type might or
2103 * might not be signed: if it is not, clever compilers will moan. */
2104 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2111 if (PL_op->op_flags & OPf_SPECIAL) {
2112 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2121 TAINT_PROPER("truncate");
2122 if (!(fp = IoIFP(io))) {
2128 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2130 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2140 if (SvTYPE(sv) == SVt_PVGV) {
2141 tmpgv = (GV*)sv; /* *main::FRED for example */
2142 goto do_ftruncate_gv;
2144 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2145 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2146 goto do_ftruncate_gv;
2148 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2149 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2150 goto do_ftruncate_io;
2153 name = SvPV_nolen_const(sv);
2154 TAINT_PROPER("truncate");
2156 if (truncate(name, len) < 0)
2162 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2165 if (my_chsize(tmpfd, len) < 0)
2167 PerlLIO_close(tmpfd);
2176 SETERRNO(EBADF,RMS_IFI);
2190 const unsigned int func = POPu;
2191 const int optype = PL_op->op_type;
2195 IO *io = gv ? GvIOn(gv) : 0;
2197 if (!io || !argsv || !IoIFP(io)) {
2198 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2199 report_evil_fh(gv, io, PL_op->op_type);
2200 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2204 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2207 s = SvPV_force(argsv, len);
2208 need = IOCPARM_LEN(func);
2210 s = Sv_Grow(argsv, need + 1);
2211 SvCUR_set(argsv, need);
2214 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2217 retval = SvIV(argsv);
2218 s = INT2PTR(char*,retval); /* ouch */
2221 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2223 if (optype == OP_IOCTL)
2225 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2227 DIE(aTHX_ "ioctl is not implemented");
2231 DIE(aTHX_ "fcntl is not implemented");
2233 #if defined(OS2) && defined(__EMX__)
2234 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2236 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2240 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2242 if (s[SvCUR(argsv)] != 17)
2243 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2245 s[SvCUR(argsv)] = 0; /* put our null back */
2246 SvSETMAGIC(argsv); /* Assume it has changed */
2255 PUSHp(zero_but_true, ZBTLEN);
2276 if (gv && (io = GvIO(gv)))
2283 (void)PerlIO_flush(fp);
2284 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2287 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2288 report_evil_fh(gv, io, PL_op->op_type);
2290 SETERRNO(EBADF,RMS_IFI);
2295 DIE(aTHX_ PL_no_func, "flock()");
2307 int protocol = POPi;
2313 io = gv ? GvIOn(gv) : NULL;
2316 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2317 report_evil_fh(gv, io, PL_op->op_type);
2319 do_close(gv, FALSE);
2320 SETERRNO(EBADF,LIB_INVARG);
2325 do_close(gv, FALSE);
2327 TAINT_PROPER("socket");
2328 fd = PerlSock_socket(domain, type, protocol);
2331 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2332 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2333 IoTYPE(io) = IoTYPE_SOCKET;
2334 if (!IoIFP(io) || !IoOFP(io)) {
2335 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2336 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2337 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2340 #if defined(HAS_FCNTL) && defined(F_SETFD)
2341 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2345 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2350 DIE(aTHX_ PL_no_sock_func, "socket");
2356 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2362 int protocol = POPi;
2369 io1 = gv1 ? GvIOn(gv1) : NULL;
2370 io2 = gv2 ? GvIOn(gv2) : NULL;
2371 if (!gv1 || !gv2 || !io1 || !io2) {
2372 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2374 report_evil_fh(gv1, io1, PL_op->op_type);
2376 report_evil_fh(gv1, io2, PL_op->op_type);
2379 do_close(gv1, FALSE);
2381 do_close(gv2, FALSE);
2386 do_close(gv1, FALSE);
2388 do_close(gv2, FALSE);
2390 TAINT_PROPER("socketpair");
2391 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2393 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2394 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2395 IoTYPE(io1) = IoTYPE_SOCKET;
2396 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2397 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2398 IoTYPE(io2) = IoTYPE_SOCKET;
2399 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2400 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2401 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2402 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2403 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2404 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2405 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2408 #if defined(HAS_FCNTL) && defined(F_SETFD)
2409 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2410 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2415 DIE(aTHX_ PL_no_sock_func, "socketpair");
2423 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2424 extern void GETPRIVMODE();
2425 extern void GETUSERMODE();
2428 /* OK, so on what platform does bind modify addr? */
2431 register IO *io = GvIOn(gv);
2435 int mpeprivmode = 0;
2438 if (!io || !IoIFP(io))
2441 addr = SvPV_const(addrsv, len);
2442 TAINT_PROPER("bind");
2443 #ifdef MPE /* Deal with MPE bind() peculiarities */
2444 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2445 /* The address *MUST* stupidly be zero. */
2446 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2447 /* PRIV mode is required to bind() to ports < 1024. */
2448 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2449 ((struct sockaddr_in *)addr)->sin_port > 0) {
2450 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2455 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2456 (struct sockaddr *)addr, len) >= 0)
2459 #ifdef MPE /* Switch back to USER mode */
2470 if (ckWARN(WARN_CLOSED))
2471 report_evil_fh(gv, io, PL_op->op_type);
2472 SETERRNO(EBADF,SS_IVCHAN);
2475 DIE(aTHX_ PL_no_sock_func, "bind");
2486 register IO *io = GvIOn(gv);
2489 if (!io || !IoIFP(io))
2492 addr = SvPV_const(addrsv, len);
2493 TAINT_PROPER("connect");
2494 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2500 if (ckWARN(WARN_CLOSED))
2501 report_evil_fh(gv, io, PL_op->op_type);
2502 SETERRNO(EBADF,SS_IVCHAN);
2505 DIE(aTHX_ PL_no_sock_func, "connect");
2515 register IO *io = gv ? GvIOn(gv) : NULL;
2517 if (!gv || !io || !IoIFP(io))
2520 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2526 if (ckWARN(WARN_CLOSED))
2527 report_evil_fh(gv, io, PL_op->op_type);
2528 SETERRNO(EBADF,SS_IVCHAN);
2531 DIE(aTHX_ PL_no_sock_func, "listen");
2543 char namebuf[MAXPATHLEN];
2544 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2545 Sock_size_t len = sizeof (struct sockaddr_in);
2547 Sock_size_t len = sizeof namebuf;
2560 if (!gstio || !IoIFP(gstio))
2564 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2568 do_close(ngv, FALSE);
2569 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2570 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2571 IoTYPE(nstio) = IoTYPE_SOCKET;
2572 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2573 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2574 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2575 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2578 #if defined(HAS_FCNTL) && defined(F_SETFD)
2579 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2583 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2584 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2586 #ifdef __SCO_VERSION__
2587 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2590 PUSHp(namebuf, len);
2594 if (ckWARN(WARN_CLOSED))
2595 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2596 SETERRNO(EBADF,SS_IVCHAN);
2602 DIE(aTHX_ PL_no_sock_func, "accept");
2612 register IO *io = GvIOn(gv);
2614 if (!io || !IoIFP(io))
2617 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2621 if (ckWARN(WARN_CLOSED))
2622 report_evil_fh(gv, io, PL_op->op_type);
2623 SETERRNO(EBADF,SS_IVCHAN);
2626 DIE(aTHX_ PL_no_sock_func, "shutdown");
2633 return pp_ssockopt();
2635 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2643 int optype = PL_op->op_type;
2646 unsigned int optname;
2652 if (optype == OP_GSOCKOPT)
2653 sv = sv_2mortal(NEWSV(22, 257));
2656 optname = (unsigned int) POPi;
2657 lvl = (unsigned int) POPi;
2661 if (!io || !IoIFP(io))
2664 fd = PerlIO_fileno(IoIFP(io));
2668 (void)SvPOK_only(sv);
2672 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2683 buf = SvPV_const(sv, l);
2687 aint = (int)SvIV(sv);
2688 buf = (const char*)&aint;
2691 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2700 if (ckWARN(WARN_CLOSED))
2701 report_evil_fh(gv, io, optype);
2702 SETERRNO(EBADF,SS_IVCHAN);
2707 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2714 return pp_getpeername();
2716 DIE(aTHX_ PL_no_sock_func, "getsockname");
2724 int optype = PL_op->op_type;
2728 register IO *io = GvIOn(gv);
2731 if (!io || !IoIFP(io))
2734 sv = sv_2mortal(NEWSV(22, 257));
2735 (void)SvPOK_only(sv);
2739 fd = PerlIO_fileno(IoIFP(io));
2741 case OP_GETSOCKNAME:
2742 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2745 case OP_GETPEERNAME:
2746 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2748 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2750 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";
2751 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2752 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2753 !memcmp((char *)SvPVX_const(sv) + sizeof(u_short), nowhere,
2754 sizeof(u_short) + sizeof(struct in_addr))) {
2761 #ifdef BOGUS_GETNAME_RETURN
2762 /* Interactive Unix, getpeername() and getsockname()
2763 does not return valid namelen */
2764 if (len == BOGUS_GETNAME_RETURN)
2765 len = sizeof(struct sockaddr);
2773 if (ckWARN(WARN_CLOSED))
2774 report_evil_fh(gv, io, optype);
2775 SETERRNO(EBADF,SS_IVCHAN);
2780 DIE(aTHX_ PL_no_sock_func, "getpeername");
2798 if (PL_op->op_flags & OPf_REF) {
2800 if (PL_op->op_type == OP_LSTAT) {
2801 if (gv != PL_defgv) {
2802 if (ckWARN(WARN_IO))
2803 Perl_warner(aTHX_ packWARN(WARN_IO),
2804 "lstat() on filehandle %s", GvENAME(gv));
2805 } else if (PL_laststype != OP_LSTAT)
2806 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2810 if (gv != PL_defgv) {
2811 PL_laststype = OP_STAT;
2813 sv_setpvn(PL_statname, "", 0);
2814 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2815 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2817 if (PL_laststatval < 0) {
2818 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2819 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2825 if (SvTYPE(sv) == SVt_PVGV) {
2829 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2831 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2832 Perl_warner(aTHX_ packWARN(WARN_IO),
2833 "lstat() on filehandle %s", GvENAME(gv));
2836 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2838 PL_laststype = PL_op->op_type;
2839 if (PL_op->op_type == OP_LSTAT)
2840 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2842 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2843 if (PL_laststatval < 0) {
2844 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2845 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2851 if (gimme != G_ARRAY) {
2852 if (gimme != G_VOID)
2853 XPUSHs(boolSV(max));
2859 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2860 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2861 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2862 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2863 #if Uid_t_size > IVSIZE
2864 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2866 # if Uid_t_sign <= 0
2867 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2869 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2872 #if Gid_t_size > IVSIZE
2873 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2875 # if Gid_t_sign <= 0
2876 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2878 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2881 #ifdef USE_STAT_RDEV
2882 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2884 PUSHs(sv_2mortal(newSVpvn("", 0)));
2886 #if Off_t_size > IVSIZE
2887 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2889 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2892 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2893 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2894 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2896 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2897 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2898 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2900 #ifdef USE_STAT_BLOCKS
2901 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2902 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2904 PUSHs(sv_2mortal(newSVpvn("", 0)));
2905 PUSHs(sv_2mortal(newSVpvn("", 0)));
2915 #if defined(HAS_ACCESS) && defined(R_OK)
2916 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2917 result = access(POPpx, R_OK);
2932 if (cando(S_IRUSR, 0, &PL_statcache))
2941 #if defined(HAS_ACCESS) && defined(W_OK)
2942 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2943 result = access(POPpx, W_OK);
2958 if (cando(S_IWUSR, 0, &PL_statcache))
2967 #if defined(HAS_ACCESS) && defined(X_OK)
2968 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2969 result = access(POPpx, X_OK);
2984 if (cando(S_IXUSR, 0, &PL_statcache))
2993 #ifdef PERL_EFF_ACCESS_R_OK
2994 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2995 result = PERL_EFF_ACCESS_R_OK(POPpx);
3010 if (cando(S_IRUSR, 1, &PL_statcache))
3019 #ifdef PERL_EFF_ACCESS_W_OK
3020 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3021 result = PERL_EFF_ACCESS_W_OK(POPpx);
3036 if (cando(S_IWUSR, 1, &PL_statcache))
3045 #ifdef PERL_EFF_ACCESS_X_OK
3046 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3047 result = PERL_EFF_ACCESS_X_OK(POPpx);
3062 if (cando(S_IXUSR, 1, &PL_statcache))
3069 I32 result = my_stat();
3078 return pp_ftrowned();
3083 I32 result = my_stat();
3087 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3095 I32 result = my_stat();
3099 if (PL_statcache.st_size == 0)
3106 I32 result = my_stat();
3110 #if Off_t_size > IVSIZE
3111 PUSHn(PL_statcache.st_size);
3113 PUSHi(PL_statcache.st_size);
3120 I32 result = my_stat();
3124 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3130 I32 result = my_stat();
3134 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3140 I32 result = my_stat();
3144 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3150 I32 result = my_stat();
3154 if (S_ISSOCK(PL_statcache.st_mode))
3161 I32 result = my_stat();
3165 if (S_ISCHR(PL_statcache.st_mode))
3172 I32 result = my_stat();
3176 if (S_ISBLK(PL_statcache.st_mode))
3183 I32 result = my_stat();
3187 if (S_ISREG(PL_statcache.st_mode))
3194 I32 result = my_stat();
3198 if (S_ISDIR(PL_statcache.st_mode))
3205 I32 result = my_stat();
3209 if (S_ISFIFO(PL_statcache.st_mode))
3216 I32 result = my_lstat();
3220 if (S_ISLNK(PL_statcache.st_mode))
3229 I32 result = my_stat();
3233 if (PL_statcache.st_mode & S_ISUID)
3243 I32 result = my_stat();
3247 if (PL_statcache.st_mode & S_ISGID)
3257 I32 result = my_stat();
3261 if (PL_statcache.st_mode & S_ISVTX)
3272 char *tmps = Nullch;
3274 if (PL_op->op_flags & OPf_REF)
3276 else if (isGV(TOPs))
3278 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3279 gv = (GV*)SvRV(POPs);
3281 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3283 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3284 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3285 else if (tmps && isDIGIT(*tmps))
3289 if (PerlLIO_isatty(fd))
3294 #if defined(atarist) /* this will work with atariST. Configure will
3295 make guesses for other systems. */
3296 # define FILE_base(f) ((f)->_base)
3297 # define FILE_ptr(f) ((f)->_ptr)
3298 # define FILE_cnt(f) ((f)->_cnt)
3299 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3309 register STDCHAR *s;
3315 if (PL_op->op_flags & OPf_REF)
3317 else if (isGV(TOPs))
3319 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3320 gv = (GV*)SvRV(POPs);
3326 if (gv == PL_defgv) {
3328 io = GvIO(PL_statgv);
3331 goto really_filename;
3336 PL_laststatval = -1;
3337 sv_setpvn(PL_statname, "", 0);
3338 io = GvIO(PL_statgv);
3340 if (io && IoIFP(io)) {
3341 if (! PerlIO_has_base(IoIFP(io)))
3342 DIE(aTHX_ "-T and -B not implemented on filehandles");
3343 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3344 if (PL_laststatval < 0)
3346 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3347 if (PL_op->op_type == OP_FTTEXT)
3352 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3353 i = PerlIO_getc(IoIFP(io));
3355 (void)PerlIO_ungetc(IoIFP(io),i);
3357 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3359 len = PerlIO_get_bufsiz(IoIFP(io));
3360 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3361 /* sfio can have large buffers - limit to 512 */
3366 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3368 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3370 SETERRNO(EBADF,RMS_IFI);
3378 PL_laststype = OP_STAT;
3379 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3380 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3381 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3383 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3386 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3387 if (PL_laststatval < 0) {
3388 (void)PerlIO_close(fp);
3391 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3392 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3393 (void)PerlIO_close(fp);
3395 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3396 RETPUSHNO; /* special case NFS directories */
3397 RETPUSHYES; /* null file is anything */
3402 /* now scan s to look for textiness */
3403 /* XXX ASCII dependent code */
3405 #if defined(DOSISH) || defined(USEMYBINMODE)
3406 /* ignore trailing ^Z on short files */
3407 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3411 for (i = 0; i < len; i++, s++) {
3412 if (!*s) { /* null never allowed in text */
3417 else if (!(isPRINT(*s) || isSPACE(*s)))
3420 else if (*s & 128) {
3422 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3425 /* utf8 characters don't count as odd */
3426 if (UTF8_IS_START(*s)) {
3427 int ulen = UTF8SKIP(s);
3428 if (ulen < len - i) {
3430 for (j = 1; j < ulen; j++) {
3431 if (!UTF8_IS_CONTINUATION(s[j]))
3434 --ulen; /* loop does extra increment */
3444 *s != '\n' && *s != '\r' && *s != '\b' &&
3445 *s != '\t' && *s != '\f' && *s != 27)
3450 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3474 if( !tmps || !*tmps ) {
3475 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3476 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3478 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3483 deprecate("chdir('') or chdir(undef) as chdir()");
3484 tmps = SvPV_nolen_const(*svp);
3488 TAINT_PROPER("chdir");
3493 TAINT_PROPER("chdir");
3494 PUSHi( PerlDir_chdir(tmps) >= 0 );
3496 /* Clear the DEFAULT element of ENV so we'll get the new value
3498 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3506 dSP; dMARK; dTARGET;
3507 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3513 DIE(aTHX_ PL_no_func, "chown");
3522 TAINT_PROPER("chroot");
3523 PUSHi( chroot(tmps) >= 0 );
3526 DIE(aTHX_ PL_no_func, "chroot");
3532 dSP; dMARK; dTARGET;
3534 value = (I32)apply(PL_op->op_type, MARK, SP);
3542 dSP; dMARK; dTARGET;
3544 value = (I32)apply(PL_op->op_type, MARK, SP);
3552 dSP; dMARK; dTARGET;
3554 value = (I32)apply(PL_op->op_type, MARK, SP);
3564 const char *tmps2 = POPpconstx;
3565 const char *tmps = SvPV_nolen_const(TOPs);
3566 TAINT_PROPER("rename");
3568 anum = PerlLIO_rename(tmps, tmps2);
3570 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3571 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3574 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3575 (void)UNLINK(tmps2);
3576 if (!(anum = link(tmps, tmps2)))
3577 anum = UNLINK(tmps);
3589 const char *tmps2 = POPpconstx;
3590 const char *tmps = SvPV_nolen_const(TOPs);
3591 TAINT_PROPER("link");
3592 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3595 DIE(aTHX_ PL_no_func, "link");
3603 const char *tmps2 = POPpconstx;
3604 const char *tmps = SvPV_nolen_const(TOPs);
3605 TAINT_PROPER("symlink");
3606 SETi( symlink(tmps, tmps2) >= 0 );
3609 DIE(aTHX_ PL_no_func, "symlink");
3619 char buf[MAXPATHLEN];
3622 #ifndef INCOMPLETE_TAINTS
3626 len = readlink(tmps, buf, sizeof(buf) - 1);
3634 RETSETUNDEF; /* just pretend it's a normal file */
3638 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3640 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3642 char * const save_filename = filename;
3648 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3649 strcpy(cmdline, cmd);
3650 strcat(cmdline, " ");
3651 for (s = cmdline + strlen(cmdline); *filename; ) {
3656 myfp = PerlProc_popen(cmdline, "r");
3660 SV *tmpsv = sv_newmortal();
3661 /* Need to save/restore 'PL_rs' ?? */
3662 s = sv_gets(tmpsv, myfp, 0);
3663 (void)PerlProc_pclose(myfp);
3667 #ifdef HAS_SYS_ERRLIST
3672 /* you don't see this */
3674 #ifdef HAS_SYS_ERRLIST
3682 if (instr(s, errmsg)) {
3689 #define EACCES EPERM
3691 if (instr(s, "cannot make"))
3692 SETERRNO(EEXIST,RMS_FEX);
3693 else if (instr(s, "existing file"))
3694 SETERRNO(EEXIST,RMS_FEX);
3695 else if (instr(s, "ile exists"))
3696 SETERRNO(EEXIST,RMS_FEX);
3697 else if (instr(s, "non-exist"))
3698 SETERRNO(ENOENT,RMS_FNF);
3699 else if (instr(s, "does not exist"))
3700 SETERRNO(ENOENT,RMS_FNF);
3701 else if (instr(s, "not empty"))
3702 SETERRNO(EBUSY,SS_DEVOFFLINE);
3703 else if (instr(s, "cannot access"))
3704 SETERRNO(EACCES,RMS_PRV);
3706 SETERRNO(EPERM,RMS_PRV);
3709 else { /* some mkdirs return no failure indication */
3710 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3711 if (PL_op->op_type == OP_RMDIR)
3716 SETERRNO(EACCES,RMS_PRV); /* a guess */
3725 /* This macro removes trailing slashes from a directory name.
3726 * Different operating and file systems take differently to
3727 * trailing slashes. According to POSIX 1003.1 1996 Edition
3728 * any number of trailing slashes should be allowed.
3729 * Thusly we snip them away so that even non-conforming
3730 * systems are happy.
3731 * We should probably do this "filtering" for all
3732 * the functions that expect (potentially) directory names:
3733 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3734 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3736 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3737 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3740 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3741 (tmps) = savepvn((tmps), (len)); \
3761 TRIMSLASHES(tmps,len,copy);
3763 TAINT_PROPER("mkdir");
3765 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3767 SETi( dooneliner("mkdir", tmps) );
3768 oldumask = PerlLIO_umask(0);
3769 PerlLIO_umask(oldumask);
3770 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3784 TRIMSLASHES(tmps,len,copy);
3785 TAINT_PROPER("rmdir");
3787 SETi( PerlDir_rmdir(tmps) >= 0 );
3789 SETi( dooneliner("rmdir", tmps) );
3796 /* Directory calls. */
3800 #if defined(Direntry_t) && defined(HAS_READDIR)
3802 const char *dirname = POPpconstx;
3804 register IO *io = GvIOn(gv);
3810 PerlDir_close(IoDIRP(io));
3811 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3817 SETERRNO(EBADF,RMS_DIR);
3820 DIE(aTHX_ PL_no_dir_func, "opendir");
3826 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3827 DIE(aTHX_ PL_no_dir_func, "readdir");
3829 #if !defined(I_DIRENT) && !defined(VMS)
3830 Direntry_t *readdir (DIR *);
3835 const I32 gimme = GIMME;
3836 GV *gv = (GV *)POPs;
3837 register Direntry_t *dp;
3838 register IO *io = GvIOn(gv);
3840 if (!io || !IoDIRP(io))
3844 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3848 sv = newSVpvn(dp->d_name, dp->d_namlen);
3850 sv = newSVpv(dp->d_name, 0);
3852 #ifndef INCOMPLETE_TAINTS
3853 if (!(IoFLAGS(io) & IOf_UNTAINT))
3856 XPUSHs(sv_2mortal(sv));
3858 while (gimme == G_ARRAY);
3860 if (!dp && gimme != G_ARRAY)
3867 SETERRNO(EBADF,RMS_ISI);
3868 if (GIMME == G_ARRAY)
3877 #if defined(HAS_TELLDIR) || defined(telldir)
3879 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3880 /* XXX netbsd still seemed to.
3881 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3882 --JHI 1999-Feb-02 */
3883 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3884 long telldir (DIR *);
3887 register IO *io = GvIOn(gv);
3889 if (!io || !IoDIRP(io))
3892 PUSHi( PerlDir_tell(IoDIRP(io)) );
3896 SETERRNO(EBADF,RMS_ISI);
3899 DIE(aTHX_ PL_no_dir_func, "telldir");
3905 #if defined(HAS_SEEKDIR) || defined(seekdir)
3909 register IO *io = GvIOn(gv);
3911 if (!io || !IoDIRP(io))
3914 (void)PerlDir_seek(IoDIRP(io), along);
3919 SETERRNO(EBADF,RMS_ISI);
3922 DIE(aTHX_ PL_no_dir_func, "seekdir");
3928 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3931 register IO *io = GvIOn(gv);
3933 if (!io || !IoDIRP(io))
3936 (void)PerlDir_rewind(IoDIRP(io));
3940 SETERRNO(EBADF,RMS_ISI);
3943 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3949 #if defined(Direntry_t) && defined(HAS_READDIR)
3952 register IO *io = GvIOn(gv);
3954 if (!io || !IoDIRP(io))
3957 #ifdef VOID_CLOSEDIR
3958 PerlDir_close(IoDIRP(io));
3960 if (PerlDir_close(IoDIRP(io)) < 0) {
3961 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3970 SETERRNO(EBADF,RMS_IFI);
3973 DIE(aTHX_ PL_no_dir_func, "closedir");
3977 /* Process control. */
3987 PERL_FLUSHALL_FOR_CHILD;
3988 childpid = PerlProc_fork();
3992 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3993 SvREADONLY_off(GvSV(tmpgv));
3994 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3995 SvREADONLY_on(GvSV(tmpgv));
3997 #ifdef THREADS_HAVE_PIDS
3998 PL_ppid = (IV)getppid();
4000 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4005 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4010 PERL_FLUSHALL_FOR_CHILD;
4011 childpid = PerlProc_fork();
4017 DIE(aTHX_ PL_no_func, "fork");
4024 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4029 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4030 childpid = wait4pid(-1, &argflags, 0);
4032 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4037 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4038 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4039 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4041 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4046 DIE(aTHX_ PL_no_func, "wait");
4052 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4061 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4062 result = wait4pid(pid, &argflags, optype);
4064 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4069 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4070 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4071 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4073 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4078 DIE(aTHX_ PL_no_func, "waitpid");
4084 dSP; dMARK; dORIGMARK; dTARGET;
4091 while (++MARK <= SP) {
4092 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4097 TAINT_PROPER("system");
4099 PERL_FLUSHALL_FOR_CHILD;
4100 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4105 if (PerlProc_pipe(pp) >= 0)
4107 while ((childpid = PerlProc_fork()) == -1) {
4108 if (errno != EAGAIN) {
4113 PerlLIO_close(pp[0]);
4114 PerlLIO_close(pp[1]);
4121 Sigsave_t ihand,qhand; /* place to save signals during system() */
4125 PerlLIO_close(pp[1]);
4127 rsignal_save(SIGINT, SIG_IGN, &ihand);
4128 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4131 result = wait4pid(childpid, &status, 0);
4132 } while (result == -1 && errno == EINTR);
4134 (void)rsignal_restore(SIGINT, &ihand);
4135 (void)rsignal_restore(SIGQUIT, &qhand);
4137 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4138 do_execfree(); /* free any memory child malloced on fork */
4144 while (n < sizeof(int)) {
4145 n1 = PerlLIO_read(pp[0],
4146 (void*)(((char*)&errkid)+n),
4152 PerlLIO_close(pp[0]);
4153 if (n) { /* Error */
4154 if (n != sizeof(int))
4155 DIE(aTHX_ "panic: kid popen errno read");
4156 errno = errkid; /* Propagate errno from kid */
4157 STATUS_CURRENT = -1;
4160 PUSHi(STATUS_CURRENT);
4164 PerlLIO_close(pp[0]);
4165 #if defined(HAS_FCNTL) && defined(F_SETFD)
4166 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4169 if (PL_op->op_flags & OPf_STACKED) {
4170 SV *really = *++MARK;
4171 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4173 else if (SP - MARK != 1)
4174 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4176 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4180 #else /* ! FORK or VMS or OS/2 */
4183 if (PL_op->op_flags & OPf_STACKED) {
4184 SV *really = *++MARK;
4185 # if defined(WIN32) || defined(OS2)
4186 value = (I32)do_aspawn(really, MARK, SP);
4188 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4191 else if (SP - MARK != 1) {
4192 # if defined(WIN32) || defined(OS2)
4193 value = (I32)do_aspawn(Nullsv, MARK, SP);
4195 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4199 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4201 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4203 STATUS_NATIVE_SET(value);
4206 PUSHi(result ? value : STATUS_CURRENT);
4207 #endif /* !FORK or VMS */
4213 dSP; dMARK; dORIGMARK; dTARGET;
4218 while (++MARK <= SP) {
4219 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4224 TAINT_PROPER("exec");
4226 PERL_FLUSHALL_FOR_CHILD;
4227 if (PL_op->op_flags & OPf_STACKED) {
4228 SV *really = *++MARK;
4229 value = (I32)do_aexec(really, MARK, SP);
4231 else if (SP - MARK != 1)
4233 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4237 (void ) do_aspawn(Nullsv, MARK, SP);
4241 value = (I32)do_aexec(Nullsv, MARK, SP);
4246 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4249 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4252 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4265 dSP; dMARK; dTARGET;
4267 value = (I32)apply(PL_op->op_type, MARK, SP);
4272 DIE(aTHX_ PL_no_func, "kill");
4280 # ifdef THREADS_HAVE_PIDS
4281 if (PL_ppid != 1 && getppid() == 1)
4282 /* maybe the parent process has died. Refresh ppid cache */
4286 XPUSHi( getppid() );
4290 DIE(aTHX_ PL_no_func, "getppid");
4306 pgrp = (I32)BSD_GETPGRP(pid);
4308 if (pid != 0 && pid != PerlProc_getpid())
4309 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4315 DIE(aTHX_ PL_no_func, "getpgrp()");
4334 TAINT_PROPER("setpgrp");
4336 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4338 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4339 || (pid != 0 && pid != PerlProc_getpid()))
4341 DIE(aTHX_ "setpgrp can't take arguments");
4343 SETi( setpgrp() >= 0 );
4344 #endif /* USE_BSDPGRP */
4347 DIE(aTHX_ PL_no_func, "setpgrp()");
4353 #ifdef HAS_GETPRIORITY
4357 SETi( getpriority(which, who) );
4360 DIE(aTHX_ PL_no_func, "getpriority()");
4366 #ifdef HAS_SETPRIORITY
4371 TAINT_PROPER("setpriority");
4372 SETi( setpriority(which, who, niceval) >= 0 );
4375 DIE(aTHX_ PL_no_func, "setpriority()");
4385 XPUSHn( time(Null(Time_t*)) );
4387 XPUSHi( time(Null(Time_t*)) );
4398 (void)PerlProc_times(&PL_timesbuf);
4400 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4401 /* struct tms, though same data */
4405 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4406 if (GIMME == G_ARRAY) {
4407 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4408 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4409 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4415 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4417 if (GIMME == G_ARRAY) {
4418 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4419 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4420 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4424 DIE(aTHX_ "times not implemented");
4426 #endif /* HAS_TIMES */
4434 #ifdef LOCALTIME_EDGECASE_BROKEN
4435 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4440 /* No workarounds in the valid range */
4441 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4442 return (localtime (tp));
4444 /* This edge case is to workaround the undefined behaviour, where the
4445 * TIMEZONE makes the time go beyond the defined range.
4446 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4447 * If there is a negative offset in TZ, like MET-1METDST, some broken
4448 * implementations of localtime () (like AIX 5.2) barf with bogus
4450 * 0x7fffffff gmtime 2038-01-19 03:14:07
4451 * 0x7fffffff localtime 1901-12-13 21:45:51
4452 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4453 * 0x3c19137f gmtime 2001-12-13 20:45:51
4454 * 0x3c19137f localtime 2001-12-13 21:45:51
4455 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4456 * Given that legal timezones are typically between GMT-12 and GMT+12
4457 * we turn back the clock 23 hours before calling the localtime
4458 * function, and add those to the return value. This will never cause
4459 * day wrapping problems, since the edge case is Tue Jan *19*
4461 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4464 if (P->tm_hour >= 24) {
4466 P->tm_mday++; /* 18 -> 19 */
4467 P->tm_wday++; /* Mon -> Tue */
4468 P->tm_yday++; /* 18 -> 19 */
4471 } /* S_my_localtime */
4478 const struct tm *tmbuf;
4479 static const char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4480 static const char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4481 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4487 when = (Time_t)SvNVx(POPs);
4489 when = (Time_t)SvIVx(POPs);
4492 if (PL_op->op_type == OP_LOCALTIME)
4493 #ifdef LOCALTIME_EDGECASE_BROKEN
4494 tmbuf = S_my_localtime(aTHX_ &when);
4496 tmbuf = localtime(&when);
4499 tmbuf = gmtime(&when);
4501 if (GIMME != G_ARRAY) {
4507 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4508 dayname[tmbuf->tm_wday],
4509 monname[tmbuf->tm_mon],
4514 tmbuf->tm_year + 1900);
4515 PUSHs(sv_2mortal(tsv));
4520 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4521 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4522 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4523 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4524 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4525 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4526 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4527 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4528 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4539 anum = alarm((unsigned int)anum);
4546 DIE(aTHX_ PL_no_func, "alarm");
4557 (void)time(&lasttime);
4562 PerlProc_sleep((unsigned int)duration);
4565 XPUSHi(when - lasttime);
4569 /* Shared memory. */
4583 return pp_shmwrite();
4588 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4589 dSP; dMARK; dTARGET;
4590 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4599 /* Message passing. */
4613 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4614 dSP; dMARK; dTARGET;
4615 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4626 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4627 dSP; dMARK; dTARGET;
4628 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4641 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4642 dSP; dMARK; dTARGET;
4643 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4650 DIE(aTHX_ "System V IPC is not implemented on this machine");
4656 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4657 dSP; dMARK; dTARGET;
4658 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4666 PUSHp(zero_but_true, ZBTLEN);
4676 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4677 dSP; dMARK; dTARGET;
4678 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4687 /* Get system info. */
4691 #ifdef HAS_GETHOSTBYNAME
4692 return pp_ghostent();
4694 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4700 #ifdef HAS_GETHOSTBYADDR
4701 return pp_ghostent();
4703 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4709 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4711 I32 which = PL_op->op_type;
4712 register char **elem;
4714 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4715 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4716 struct hostent *gethostbyname(Netdb_name_t);
4717 struct hostent *gethostent(void);
4719 struct hostent *hent;
4723 if (which == OP_GHBYNAME) {
4724 #ifdef HAS_GETHOSTBYNAME
4725 char* name = POPpbytex;
4726 hent = PerlSock_gethostbyname(name);
4728 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4731 else if (which == OP_GHBYADDR) {
4732 #ifdef HAS_GETHOSTBYADDR
4733 int addrtype = POPi;
4736 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4738 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4740 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4744 #ifdef HAS_GETHOSTENT
4745 hent = PerlSock_gethostent();
4747 DIE(aTHX_ PL_no_sock_func, "gethostent");
4750 #ifdef HOST_NOT_FOUND
4752 #ifdef USE_REENTRANT_API
4753 # ifdef USE_GETHOSTENT_ERRNO
4754 h_errno = PL_reentrant_buffer->_gethostent_errno;
4757 STATUS_NATIVE_SET(h_errno);
4761 if (GIMME != G_ARRAY) {
4762 PUSHs(sv = sv_newmortal());
4764 if (which == OP_GHBYNAME) {
4766 sv_setpvn(sv, hent->h_addr, hent->h_length);
4769 sv_setpv(sv, (char*)hent->h_name);
4775 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4776 sv_setpv(sv, (char*)hent->h_name);
4777 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4778 for (elem = hent->h_aliases; elem && *elem; elem++) {
4779 sv_catpv(sv, *elem);
4781 sv_catpvn(sv, " ", 1);
4783 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4784 sv_setiv(sv, (IV)hent->h_addrtype);
4785 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4786 len = hent->h_length;
4787 sv_setiv(sv, (IV)len);
4789 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4790 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4791 sv_setpvn(sv, *elem, len);
4794 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4796 sv_setpvn(sv, hent->h_addr, len);
4801 DIE(aTHX_ PL_no_sock_func, "gethostent");
4807 #ifdef HAS_GETNETBYNAME
4808 return pp_gnetent();
4810 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4816 #ifdef HAS_GETNETBYADDR
4817 return pp_gnetent();
4819 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4825 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4827 I32 which = PL_op->op_type;
4828 register char **elem;
4830 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4831 struct netent *getnetbyaddr(Netdb_net_t, int);
4832 struct netent *getnetbyname(Netdb_name_t);
4833 struct netent *getnetent(void);
4835 struct netent *nent;
4837 if (which == OP_GNBYNAME){
4838 #ifdef HAS_GETNETBYNAME
4839 char *name = POPpbytex;
4840 nent = PerlSock_getnetbyname(name);
4842 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4845 else if (which == OP_GNBYADDR) {
4846 #ifdef HAS_GETNETBYADDR
4847 int addrtype = POPi;
4848 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4849 nent = PerlSock_getnetbyaddr(addr, addrtype);
4851 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4855 #ifdef HAS_GETNETENT
4856 nent = PerlSock_getnetent();
4858 DIE(aTHX_ PL_no_sock_func, "getnetent");
4861 #ifdef HOST_NOT_FOUND
4863 #ifdef USE_REENTRANT_API
4864 # ifdef USE_GETNETENT_ERRNO
4865 h_errno = PL_reentrant_buffer->_getnetent_errno;
4868 STATUS_NATIVE_SET(h_errno);
4873 if (GIMME != G_ARRAY) {
4874 PUSHs(sv = sv_newmortal());
4876 if (which == OP_GNBYNAME)
4877 sv_setiv(sv, (IV)nent->n_net);
4879 sv_setpv(sv, nent->n_name);
4885 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4886 sv_setpv(sv, nent->n_name);
4887 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4888 for (elem = nent->n_aliases; elem && *elem; elem++) {
4889 sv_catpv(sv, *elem);
4891 sv_catpvn(sv, " ", 1);
4893 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4894 sv_setiv(sv, (IV)nent->n_addrtype);
4895 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4896 sv_setiv(sv, (IV)nent->n_net);
4901 DIE(aTHX_ PL_no_sock_func, "getnetent");
4907 #ifdef HAS_GETPROTOBYNAME
4908 return pp_gprotoent();
4910 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4916 #ifdef HAS_GETPROTOBYNUMBER
4917 return pp_gprotoent();
4919 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4925 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4927 I32 which = PL_op->op_type;
4928 register char **elem;
4930 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4931 struct protoent *getprotobyname(Netdb_name_t);
4932 struct protoent *getprotobynumber(int);
4933 struct protoent *getprotoent(void);
4935 struct protoent *pent;
4937 if (which == OP_GPBYNAME) {
4938 #ifdef HAS_GETPROTOBYNAME
4939 char* name = POPpbytex;
4940 pent = PerlSock_getprotobyname(name);
4942 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4945 else if (which == OP_GPBYNUMBER) {
4946 #ifdef HAS_GETPROTOBYNUMBER
4948 pent = PerlSock_getprotobynumber(number);
4950 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4954 #ifdef HAS_GETPROTOENT
4955 pent = PerlSock_getprotoent();
4957 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4961 if (GIMME != G_ARRAY) {
4962 PUSHs(sv = sv_newmortal());
4964 if (which == OP_GPBYNAME)
4965 sv_setiv(sv, (IV)pent->p_proto);
4967 sv_setpv(sv, pent->p_name);
4973 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4974 sv_setpv(sv, pent->p_name);
4975 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4976 for (elem = pent->p_aliases; elem && *elem; elem++) {
4977 sv_catpv(sv, *elem);
4979 sv_catpvn(sv, " ", 1);
4981 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4982 sv_setiv(sv, (IV)pent->p_proto);
4987 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4993 #ifdef HAS_GETSERVBYNAME
4994 return pp_gservent();
4996 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5002 #ifdef HAS_GETSERVBYPORT
5003 return pp_gservent();
5005 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5011 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5013 I32 which = PL_op->op_type;
5014 register char **elem;
5016 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5017 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5018 struct servent *getservbyport(int, Netdb_name_t);
5019 struct servent *getservent(void);
5021 struct servent *sent;
5023 if (which == OP_GSBYNAME) {
5024 #ifdef HAS_GETSERVBYNAME
5025 char *proto = POPpbytex;
5026 char *name = POPpbytex;
5028 if (proto && !*proto)
5031 sent = PerlSock_getservbyname(name, proto);
5033 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5036 else if (which == OP_GSBYPORT) {
5037 #ifdef HAS_GETSERVBYPORT
5038 char *proto = POPpbytex;
5039 unsigned short port = (unsigned short)POPu;
5041 if (proto && !*proto)
5045 port = PerlSock_htons(port);
5047 sent = PerlSock_getservbyport(port, proto);
5049 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5053 #ifdef HAS_GETSERVENT
5054 sent = PerlSock_getservent();
5056 DIE(aTHX_ PL_no_sock_func, "getservent");
5060 if (GIMME != G_ARRAY) {
5061 PUSHs(sv = sv_newmortal());
5063 if (which == OP_GSBYNAME) {
5065 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5067 sv_setiv(sv, (IV)(sent->s_port));
5071 sv_setpv(sv, sent->s_name);
5077 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5078 sv_setpv(sv, sent->s_name);
5079 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5080 for (elem = sent->s_aliases; elem && *elem; elem++) {
5081 sv_catpv(sv, *elem);
5083 sv_catpvn(sv, " ", 1);
5085 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5087 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5089 sv_setiv(sv, (IV)(sent->s_port));
5091 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5092 sv_setpv(sv, sent->s_proto);
5097 DIE(aTHX_ PL_no_sock_func, "getservent");
5103 #ifdef HAS_SETHOSTENT
5105 PerlSock_sethostent(TOPi);
5108 DIE(aTHX_ PL_no_sock_func, "sethostent");
5114 #ifdef HAS_SETNETENT
5116 PerlSock_setnetent(TOPi);
5119 DIE(aTHX_ PL_no_sock_func, "setnetent");
5125 #ifdef HAS_SETPROTOENT
5127 PerlSock_setprotoent(TOPi);
5130 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5136 #ifdef HAS_SETSERVENT
5138 PerlSock_setservent(TOPi);
5141 DIE(aTHX_ PL_no_sock_func, "setservent");
5147 #ifdef HAS_ENDHOSTENT
5149 PerlSock_endhostent();
5153 DIE(aTHX_ PL_no_sock_func, "endhostent");
5159 #ifdef HAS_ENDNETENT
5161 PerlSock_endnetent();
5165 DIE(aTHX_ PL_no_sock_func, "endnetent");
5171 #ifdef HAS_ENDPROTOENT
5173 PerlSock_endprotoent();
5177 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5183 #ifdef HAS_ENDSERVENT
5185 PerlSock_endservent();
5189 DIE(aTHX_ PL_no_sock_func, "endservent");
5198 DIE(aTHX_ PL_no_func, "getpwnam");
5207 DIE(aTHX_ PL_no_func, "getpwuid");
5215 I32 which = PL_op->op_type;
5217 struct passwd *pwent = NULL;
5219 * We currently support only the SysV getsp* shadow password interface.
5220 * The interface is declared in <shadow.h> and often one needs to link
5221 * with -lsecurity or some such.
5222 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5225 * AIX getpwnam() is clever enough to return the encrypted password
5226 * only if the caller (euid?) is root.
5228 * There are at least three other shadow password APIs. Many platforms
5229 * seem to contain more than one interface for accessing the shadow
5230 * password databases, possibly for compatibility reasons.
5231 * The getsp*() is by far he simplest one, the other two interfaces
5232 * are much more complicated, but also very similar to each other.
5237 * struct pr_passwd *getprpw*();
5238 * The password is in
5239 * char getprpw*(...).ufld.fd_encrypt[]
5240 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5245 * struct es_passwd *getespw*();
5246 * The password is in
5247 * char *(getespw*(...).ufld.fd_encrypt)
5248 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5251 * struct userpw *getuserpw();
5252 * The password is in
5253 * char *(getuserpw(...)).spw_upw_passwd
5254 * (but the de facto standard getpwnam() should work okay)
5256 * Mention I_PROT here so that Configure probes for it.
5258 * In HP-UX for getprpw*() the manual page claims that one should include
5259 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5260 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5261 * and pp_sys.c already includes <shadow.h> if there is such.
5263 * Note that <sys/security.h> is already probed for, but currently
5264 * it is only included in special cases.
5266 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5267 * be preferred interface, even though also the getprpw*() interface
5268 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5269 * One also needs to call set_auth_parameters() in main() before
5270 * doing anything else, whether one is using getespw*() or getprpw*().
5272 * Note that accessing the shadow databases can be magnitudes
5273 * slower than accessing the standard databases.
5278 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5279 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5280 * the pw_comment is left uninitialized. */
5281 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5287 char* name = POPpbytex;
5288 pwent = getpwnam(name);
5294 pwent = getpwuid(uid);
5298 # ifdef HAS_GETPWENT
5300 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5301 if (pwent) pwent = getpwnam(pwent->pw_name);
5304 DIE(aTHX_ PL_no_func, "getpwent");
5310 if (GIMME != G_ARRAY) {
5311 PUSHs(sv = sv_newmortal());
5313 if (which == OP_GPWNAM)
5314 # if Uid_t_sign <= 0
5315 sv_setiv(sv, (IV)pwent->pw_uid);
5317 sv_setuv(sv, (UV)pwent->pw_uid);
5320 sv_setpv(sv, pwent->pw_name);
5326 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5327 sv_setpv(sv, pwent->pw_name);
5329 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5331 /* If we have getspnam(), we try to dig up the shadow
5332 * password. If we are underprivileged, the shadow
5333 * interface will set the errno to EACCES or similar,
5334 * and return a null pointer. If this happens, we will
5335 * use the dummy password (usually "*" or "x") from the
5336 * standard password database.
5338 * In theory we could skip the shadow call completely
5339 * if euid != 0 but in practice we cannot know which
5340 * security measures are guarding the shadow databases
5341 * on a random platform.
5343 * Resist the urge to use additional shadow interfaces.
5344 * Divert the urge to writing an extension instead.
5347 /* Some AIX setups falsely(?) detect some getspnam(), which
5348 * has a different API than the Solaris/IRIX one. */
5349 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5351 struct spwd *spwent;
5352 int saverrno; /* Save and restore errno so that
5353 * underprivileged attempts seem
5354 * to have never made the unsccessful
5355 * attempt to retrieve the shadow password. */
5358 spwent = getspnam(pwent->pw_name);
5360 if (spwent && spwent->sp_pwdp)
5361 sv_setpv(sv, spwent->sp_pwdp);
5365 if (!SvPOK(sv)) /* Use the standard password, then. */
5366 sv_setpv(sv, pwent->pw_passwd);
5369 # ifndef INCOMPLETE_TAINTS
5370 /* passwd is tainted because user himself can diddle with it.
5371 * admittedly not much and in a very limited way, but nevertheless. */
5375 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5376 # if Uid_t_sign <= 0
5377 sv_setiv(sv, (IV)pwent->pw_uid);
5379 sv_setuv(sv, (UV)pwent->pw_uid);
5382 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5383 # if Uid_t_sign <= 0
5384 sv_setiv(sv, (IV)pwent->pw_gid);
5386 sv_setuv(sv, (UV)pwent->pw_gid);
5388 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5389 * because of the poor interface of the Perl getpw*(),
5390 * not because there's some standard/convention saying so.
5391 * A better interface would have been to return a hash,
5392 * but we are accursed by our history, alas. --jhi. */
5393 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5395 sv_setiv(sv, (IV)pwent->pw_change);
5398 sv_setiv(sv, (IV)pwent->pw_quota);
5401 sv_setpv(sv, pwent->pw_age);
5406 /* pw_class and pw_comment are mutually exclusive--.
5407 * see the above note for pw_change, pw_quota, and pw_age. */
5408 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5410 sv_setpv(sv, pwent->pw_class);
5413 sv_setpv(sv, pwent->pw_comment);
5417 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5419 sv_setpv(sv, pwent->pw_gecos);
5421 # ifndef INCOMPLETE_TAINTS
5422 /* pw_gecos is tainted because user himself can diddle with it. */
5426 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5427 sv_setpv(sv, pwent->pw_dir);
5429 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5430 sv_setpv(sv, pwent->pw_shell);
5431 # ifndef INCOMPLETE_TAINTS
5432 /* pw_shell is tainted because user himself can diddle with it. */
5437 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5438 sv_setiv(sv, (IV)pwent->pw_expire);
5443 DIE(aTHX_ PL_no_func, "getpwent");
5449 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5454 DIE(aTHX_ PL_no_func, "setpwent");
5460 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5465 DIE(aTHX_ PL_no_func, "endpwent");
5474 DIE(aTHX_ PL_no_func, "getgrnam");
5483 DIE(aTHX_ PL_no_func, "getgrgid");
5491 I32 which = PL_op->op_type;
5492 register char **elem;
5494 struct group *grent;
5496 if (which == OP_GGRNAM) {
5497 char* name = POPpbytex;
5498 grent = (struct group *)getgrnam(name);
5500 else if (which == OP_GGRGID) {
5502 grent = (struct group *)getgrgid(gid);
5506 grent = (struct group *)getgrent();
5508 DIE(aTHX_ PL_no_func, "getgrent");
5512 if (GIMME != G_ARRAY) {
5513 PUSHs(sv = sv_newmortal());
5515 if (which == OP_GGRNAM)
5516 sv_setiv(sv, (IV)grent->gr_gid);
5518 sv_setpv(sv, grent->gr_name);
5524 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5525 sv_setpv(sv, grent->gr_name);
5527 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5529 sv_setpv(sv, grent->gr_passwd);
5532 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5533 sv_setiv(sv, (IV)grent->gr_gid);
5535 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5536 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5537 /* In UNICOS/mk (_CRAYMPP) the multithreading
5538 * versions (getgrnam_r, getgrgid_r)
5539 * seem to return an illegal pointer
5540 * as the group members list, gr_mem.
5541 * getgrent() doesn't even have a _r version
5542 * but the gr_mem is poisonous anyway.
5543 * So yes, you cannot get the list of group
5544 * members if building multithreaded in UNICOS/mk. */
5545 for (elem = grent->gr_mem; elem && *elem; elem++) {
5546 sv_catpv(sv, *elem);
5548 sv_catpvn(sv, " ", 1);
5555 DIE(aTHX_ PL_no_func, "getgrent");
5561 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5566 DIE(aTHX_ PL_no_func, "setgrent");
5572 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5577 DIE(aTHX_ PL_no_func, "endgrent");
5587 if (!(tmps = PerlProc_getlogin()))
5589 PUSHp(tmps, strlen(tmps));
5592 DIE(aTHX_ PL_no_func, "getlogin");
5596 /* Miscellaneous. */
5601 dSP; dMARK; dORIGMARK; dTARGET;
5602 register I32 items = SP - MARK;
5603 unsigned long a[20];
5608 while (++MARK <= SP) {
5609 if (SvTAINTED(*MARK)) {
5615 TAINT_PROPER("syscall");
5618 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5619 * or where sizeof(long) != sizeof(char*). But such machines will
5620 * not likely have syscall implemented either, so who cares?
5622 while (++MARK <= SP) {
5623 if (SvNIOK(*MARK) || !i)
5624 a[i++] = SvIV(*MARK);
5625 else if (*MARK == &PL_sv_undef)
5628 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5634 DIE(aTHX_ "Too many args to syscall");
5636 DIE(aTHX_ "Too few args to syscall");
5638 retval = syscall(a[0]);
5641 retval = syscall(a[0],a[1]);
5644 retval = syscall(a[0],a[1],a[2]);
5647 retval = syscall(a[0],a[1],a[2],a[3]);
5650 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5653 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5656 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5659 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5663 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5666 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5669 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5673 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5677 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5681 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5682 a[10],a[11],a[12],a[13]);
5684 #endif /* atarist */
5690 DIE(aTHX_ PL_no_func, "syscall");
5694 #ifdef FCNTL_EMULATE_FLOCK
5696 /* XXX Emulate flock() with fcntl().
5697 What's really needed is a good file locking module.
5701 fcntl_emulate_flock(int fd, int operation)
5705 switch (operation & ~LOCK_NB) {
5707 flock.l_type = F_RDLCK;
5710 flock.l_type = F_WRLCK;
5713 flock.l_type = F_UNLCK;
5719 flock.l_whence = SEEK_SET;
5720 flock.l_start = flock.l_len = (Off_t)0;
5722 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5725 #endif /* FCNTL_EMULATE_FLOCK */
5727 #ifdef LOCKF_EMULATE_FLOCK
5729 /* XXX Emulate flock() with lockf(). This is just to increase
5730 portability of scripts. The calls are not completely
5731 interchangeable. What's really needed is a good file
5735 /* The lockf() constants might have been defined in <unistd.h>.
5736 Unfortunately, <unistd.h> causes troubles on some mixed
5737 (BSD/POSIX) systems, such as SunOS 4.1.3.
5739 Further, the lockf() constants aren't POSIX, so they might not be
5740 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5741 just stick in the SVID values and be done with it. Sigh.
5745 # define F_ULOCK 0 /* Unlock a previously locked region */
5748 # define F_LOCK 1 /* Lock a region for exclusive use */
5751 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5754 # define F_TEST 3 /* Test a region for other processes locks */
5758 lockf_emulate_flock(int fd, int operation)
5764 /* flock locks entire file so for lockf we need to do the same */
5766 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5767 if (pos > 0) /* is seekable and needs to be repositioned */
5768 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5769 pos = -1; /* seek failed, so don't seek back afterwards */
5772 switch (operation) {
5774 /* LOCK_SH - get a shared lock */
5776 /* LOCK_EX - get an exclusive lock */
5778 i = lockf (fd, F_LOCK, 0);
5781 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5782 case LOCK_SH|LOCK_NB:
5783 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5784 case LOCK_EX|LOCK_NB:
5785 i = lockf (fd, F_TLOCK, 0);
5787 if ((errno == EAGAIN) || (errno == EACCES))
5788 errno = EWOULDBLOCK;
5791 /* LOCK_UN - unlock (non-blocking is a no-op) */
5793 case LOCK_UN|LOCK_NB:
5794 i = lockf (fd, F_ULOCK, 0);
5797 /* Default - can't decipher operation */
5804 if (pos > 0) /* need to restore position of the handle */
5805 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5810 #endif /* LOCKF_EMULATE_FLOCK */
5814 * c-indentation-style: bsd
5816 * indent-tabs-mode: t
5819 * ex: set ts=8 sts=4 sw=4 noet: