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
124 # define my_chsize PerlLIO_chsize
126 I32 my_chsize(int fd, Off_t length);
132 #else /* no flock() */
134 /* fcntl.h might not have been included, even if it exists, because
135 the current Configure only sets I_FCNTL if it's needed to pick up
136 the *_OK constants. Make sure it has been included before testing
137 the fcntl() locking constants. */
138 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
142 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
143 # define FLOCK fcntl_emulate_flock
144 # define FCNTL_EMULATE_FLOCK
145 # else /* no flock() or fcntl(F_SETLK,...) */
147 # define FLOCK lockf_emulate_flock
148 # define LOCKF_EMULATE_FLOCK
150 # endif /* no flock() or fcntl(F_SETLK,...) */
153 static int FLOCK (int, int);
156 * These are the flock() constants. Since this sytems doesn't have
157 * flock(), the values of the constants are probably not available.
171 # endif /* emulating flock() */
173 #endif /* no flock() */
176 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
178 #if defined(I_SYS_ACCESS) && !defined(R_OK)
179 # include <sys/access.h>
182 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
183 # define FD_CLOEXEC 1 /* NeXT needs this */
189 /* Missing protos on LynxOS */
190 void sethostent(int);
191 void endhostent(void);
193 void endnetent(void);
194 void setprotoent(int);
195 void endprotoent(void);
196 void setservent(int);
197 void endservent(void);
200 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
201 #undef PERL_EFF_ACCESS_W_OK
202 #undef PERL_EFF_ACCESS_X_OK
204 /* F_OK unused: if stat() cannot find it... */
206 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
207 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
208 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
209 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
210 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
213 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
214 # ifdef I_SYS_SECURITY
215 # include <sys/security.h>
219 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
220 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
221 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
224 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
225 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
226 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
230 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
232 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
233 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
234 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
237 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
238 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
239 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
242 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
244 Uid_t ruid = getuid();
245 Uid_t euid = geteuid();
246 Gid_t rgid = getgid();
247 Gid_t egid = getegid();
251 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
252 Perl_croak(aTHX_ "switching effective uid is not implemented");
255 if (setreuid(euid, ruid))
258 if (setresuid(euid, ruid, (Uid_t)-1))
261 Perl_croak(aTHX_ "entering effective uid failed");
264 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
265 Perl_croak(aTHX_ "switching effective gid is not implemented");
268 if (setregid(egid, rgid))
271 if (setresgid(egid, rgid, (Gid_t)-1))
274 Perl_croak(aTHX_ "entering effective gid failed");
277 res = access(path, mode);
280 if (setreuid(ruid, euid))
283 if (setresuid(ruid, euid, (Uid_t)-1))
286 Perl_croak(aTHX_ "leaving effective uid failed");
289 if (setregid(rgid, egid))
292 if (setresgid(rgid, egid, (Gid_t)-1))
295 Perl_croak(aTHX_ "leaving effective gid failed");
300 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
301 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
302 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
305 #if !defined(PERL_EFF_ACCESS_R_OK)
306 /* With it or without it: anyway you get a warning: either that
307 it is unused, or it is declared static and never defined.
310 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
314 Perl_croak(aTHX_ "switching effective uid is not implemented");
324 const char *tmps = POPpconstx;
325 const I32 gimme = GIMME_V;
326 const char *mode = "r";
329 if (PL_op->op_private & OPpOPEN_IN_RAW)
331 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
333 fp = PerlProc_popen((char*)tmps, (char *)mode);
335 const char *type = NULL;
336 if (PL_curcop->cop_io) {
337 type = SvPV_nolen_const(PL_curcop->cop_io);
340 PerlIO_apply_layers(aTHX_ fp,mode,type);
342 if (gimme == G_VOID) {
344 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
348 else if (gimme == G_SCALAR) {
351 PL_rs = &PL_sv_undef;
352 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
353 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
365 if (sv_gets(sv, fp, 0) == Nullch) {
369 XPUSHs(sv_2mortal(sv));
370 if (SvLEN(sv) - SvCUR(sv) > 20) {
371 SvPV_shrink_to_cur(sv);
376 STATUS_NATIVE_SET(PerlProc_pclose(fp));
377 TAINT; /* "I believe that this is not gratuitous!" */
380 STATUS_NATIVE_SET(-1);
381 if (gimme == G_SCALAR)
392 tryAMAGICunTARGET(iter, -1);
394 /* Note that we only ever get here if File::Glob fails to load
395 * without at the same time croaking, for some reason, or if
396 * perl was built with PERL_EXTERNAL_GLOB */
403 * The external globbing program may use things we can't control,
404 * so for security reasons we must assume the worst.
407 taint_proper(PL_no_security, "glob");
411 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
412 PL_last_in_gv = (GV*)*PL_stack_sp--;
414 SAVESPTR(PL_rs); /* This is not permanent, either. */
415 PL_rs = sv_2mortal(newSVpvn("\000", 1));
418 *SvPVX(PL_rs) = '\n';
422 result = do_readline();
429 PL_last_in_gv = cGVOP_gv;
430 return do_readline();
439 if (SP - MARK != 1) {
441 do_join(TARG, &PL_sv_no, MARK, SP);
448 tmps = SvPV_const(tmpsv, len);
449 if ((!tmps || !len) && PL_errgv) {
451 SvUPGRADE(error, SVt_PV);
452 if (SvPOK(error) && SvCUR(error))
453 sv_catpv(error, "\t...caught");
455 tmps = SvPV_const(tmpsv, len);
458 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
460 Perl_warn(aTHX_ "%"SVf, tmpsv);
472 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
474 if (SP - MARK != 1) {
476 do_join(TARG, &PL_sv_no, MARK, SP);
478 tmps = SvPV_const(tmpsv, len);
484 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
488 SvUPGRADE(error, SVt_PV);
489 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
491 SvSetSV(error,tmpsv);
492 else if (sv_isobject(error)) {
493 HV *stash = SvSTASH(SvRV(error));
494 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
496 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
497 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
504 call_sv((SV*)GvCV(gv),
505 G_SCALAR|G_EVAL|G_KEEPERR);
506 sv_setsv(error,*PL_stack_sp--);
512 if (SvPOK(error) && SvCUR(error))
513 sv_catpv(error, "\t...propagated");
515 tmps = SvPV_const(tmpsv, len);
519 tmpsv = sv_2mortal(newSVpvn("Died", 4));
521 DIE(aTHX_ "%"SVf, tmpsv);
541 DIE(aTHX_ PL_no_usym, "filehandle");
542 if ((io = GvIOp(gv)))
543 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
545 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
546 /* Method's args are same as ours ... */
547 /* ... except handle is replaced by the object */
548 *MARK-- = SvTIED_obj((SV*)io, mg);
552 call_method("OPEN", G_SCALAR);
565 tmps = SvPV_const(sv, len);
566 ok = do_openn(gv, (char *)tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
569 PUSHi( (I32)PL_forkprocess );
570 else if (PL_forkprocess == 0) /* we are a new child */
589 if (gv && (io = GvIO(gv))
590 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
593 XPUSHs(SvTIED_obj((SV*)io, mg));
596 call_method("CLOSE", G_SCALAR);
602 PUSHs(boolSV(do_close(gv, TRUE)));
622 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
623 DIE(aTHX_ PL_no_usym, "filehandle");
628 do_close(rgv, FALSE);
630 do_close(wgv, FALSE);
632 if (PerlProc_pipe(fd) < 0)
635 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
636 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
637 IoOFP(rstio) = IoIFP(rstio);
638 IoIFP(wstio) = IoOFP(wstio);
639 IoTYPE(rstio) = IoTYPE_RDONLY;
640 IoTYPE(wstio) = IoTYPE_WRONLY;
642 if (!IoIFP(rstio) || !IoOFP(wstio)) {
643 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
644 else PerlLIO_close(fd[0]);
645 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
646 else PerlLIO_close(fd[1]);
649 #if defined(HAS_FCNTL) && defined(F_SETFD)
650 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
651 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
658 DIE(aTHX_ PL_no_func, "pipe");
674 if (gv && (io = GvIO(gv))
675 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
678 XPUSHs(SvTIED_obj((SV*)io, mg));
681 call_method("FILENO", G_SCALAR);
687 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
688 /* Can't do this because people seem to do things like
689 defined(fileno($foo)) to check whether $foo is a valid fh.
690 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
691 report_evil_fh(gv, io, PL_op->op_type);
696 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)) {
793 const I32 markoff = MARK - PL_stack_base;
794 const char *methname;
795 int how = PERL_MAGIC_tied;
799 switch(SvTYPE(varsv)) {
801 methname = "TIEHASH";
802 HvEITER_set((HV *)varsv, 0);
805 methname = "TIEARRAY";
808 #ifdef GV_UNIQUE_CHECK
809 if (GvUNIQUE((GV*)varsv)) {
810 Perl_croak(aTHX_ "Attempt to tie unique GV");
813 methname = "TIEHANDLE";
814 how = PERL_MAGIC_tiedscalar;
815 /* For tied filehandles, we apply tiedscalar magic to the IO
816 slot of the GP rather than the GV itself. AMS 20010812 */
818 GvIOp(varsv) = newIO();
819 varsv = (SV *)GvIOp(varsv);
822 methname = "TIESCALAR";
823 how = PERL_MAGIC_tiedscalar;
827 if (sv_isobject(*MARK)) {
829 PUSHSTACKi(PERLSI_MAGIC);
831 EXTEND(SP,(I32)items);
835 call_method(methname, G_SCALAR);
838 /* Not clear why we don't call call_method here too.
839 * perhaps to get different error message ?
841 stash = gv_stashsv(*MARK, FALSE);
842 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
843 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
847 PUSHSTACKi(PERLSI_MAGIC);
849 EXTEND(SP,(I32)items);
853 call_sv((SV*)GvCV(gv), G_SCALAR);
859 if (sv_isobject(sv)) {
860 sv_unmagic(varsv, how);
861 /* Croak if a self-tie on an aggregate is attempted. */
862 if (varsv == SvRV(sv) &&
863 (SvTYPE(varsv) == SVt_PVAV ||
864 SvTYPE(varsv) == SVt_PVHV))
866 "Self-ties of arrays and hashes are not supported");
867 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
870 SP = PL_stack_base + markoff;
880 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
881 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
883 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
886 if ((mg = SvTIED_mg(sv, how))) {
887 SV *obj = SvRV(SvTIED_obj(sv, mg));
891 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
892 isGV(gv) && (cv = GvCV(gv))) {
894 XPUSHs(SvTIED_obj((SV*)gv, mg));
895 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
898 call_sv((SV *)cv, G_VOID);
902 else if (ckWARN(WARN_UNTIE)) {
903 if (mg && SvREFCNT(obj) > 1)
904 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
905 "untie attempted while %"UVuf" inner references still exist",
906 (UV)SvREFCNT(obj) - 1 ) ;
910 sv_unmagic(sv, how) ;
919 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
920 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
922 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
925 if ((mg = SvTIED_mg(sv, how))) {
926 SV *osv = SvTIED_obj(sv, mg);
927 if (osv == mg->mg_obj)
928 osv = sv_mortalcopy(osv);
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;
1013 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1018 # if BYTEORDER & 0xf0000
1019 # define ORDERBYTE (0x88888888 - BYTEORDER)
1021 # define ORDERBYTE (0x4444 - BYTEORDER)
1027 for (i = 1; i <= 3; i++) {
1035 /* little endians can use vecs directly */
1036 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1043 masksize = NFDBITS / NBBY;
1045 masksize = sizeof(long); /* documented int, everyone seems to use long */
1047 Zero(&fd_sets[0], 4, char*);
1050 # if SELECT_MIN_BITS == 1
1051 growsize = sizeof(fd_set);
1053 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1054 # undef SELECT_MIN_BITS
1055 # define SELECT_MIN_BITS __FD_SETSIZE
1057 /* If SELECT_MIN_BITS is greater than one we most probably will want
1058 * to align the sizes with SELECT_MIN_BITS/8 because for example
1059 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1060 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1061 * on (sets/tests/clears bits) is 32 bits. */
1062 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1070 timebuf.tv_sec = (long)value;
1071 value -= (NV)timebuf.tv_sec;
1072 timebuf.tv_usec = (long)(value * 1000000.0);
1075 tbuf = Null(struct timeval*);
1077 for (i = 1; i <= 3; i++) {
1083 else if (!SvPOK(sv))
1084 SvPV_force(sv,n_a); /* force string conversion */
1087 Sv_Grow(sv, growsize);
1091 while (++j <= growsize) {
1095 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1097 New(403, fd_sets[i], growsize, char);
1098 for (offset = 0; offset < growsize; offset += masksize) {
1099 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1100 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1103 fd_sets[i] = SvPVX(sv);
1107 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1108 /* Can't make just the (void*) conditional because that would be
1109 * cpp #if within cpp macro, and not all compilers like that. */
1110 nfound = PerlSock_select(
1112 (Select_fd_set_t) fd_sets[1],
1113 (Select_fd_set_t) fd_sets[2],
1114 (Select_fd_set_t) fd_sets[3],
1115 (void*) tbuf); /* Workaround for compiler bug. */
1117 nfound = PerlSock_select(
1119 (Select_fd_set_t) fd_sets[1],
1120 (Select_fd_set_t) fd_sets[2],
1121 (Select_fd_set_t) fd_sets[3],
1124 for (i = 1; i <= 3; i++) {
1127 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1129 for (offset = 0; offset < growsize; offset += masksize) {
1130 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1131 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1133 Safefree(fd_sets[i]);
1140 PUSHs(&PL_sv_undef);
1143 if (GIMME == G_ARRAY && tbuf) {
1144 value = (NV)(timebuf.tv_sec) +
1145 (NV)(timebuf.tv_usec) / 1000000.0;
1146 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1147 sv_setnv(sv, value);
1151 DIE(aTHX_ "select not implemented");
1156 Perl_setdefout(pTHX_ GV *gv)
1159 (void)SvREFCNT_inc(gv);
1161 SvREFCNT_dec(PL_defoutgv);
1171 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1173 egv = GvEGV(PL_defoutgv);
1178 XPUSHs(&PL_sv_undef);
1180 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1181 if (gvp && *gvp == egv) {
1182 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1186 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1191 if (!GvIO(newdefout))
1192 gv_IOadd(newdefout);
1193 setdefout(newdefout);
1211 if (gv && (io = GvIO(gv))
1212 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1214 const I32 gimme = GIMME_V;
1216 XPUSHs(SvTIED_obj((SV*)io, mg));
1219 call_method("GETC", gimme);
1222 if (gimme == G_SCALAR)
1223 SvSetMagicSV_nosteal(TARG, TOPs);
1226 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1227 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1228 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1229 report_evil_fh(gv, io, PL_op->op_type);
1230 SETERRNO(EBADF,RMS_IFI);
1234 sv_setpvn(TARG, " ", 1);
1235 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1236 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1237 /* Find out how many bytes the char needs */
1238 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1241 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1242 SvCUR_set(TARG,1+len);
1252 return pp_sysread();
1256 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1259 register PERL_CONTEXT *cx;
1260 const I32 gimme = GIMME_V;
1265 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1267 cx->blk_sub.retop = retop;
1268 PAD_SET_CUR(CvPADLIST(cv), 1);
1270 setdefout(gv); /* locally select filehandle so $% et al work */
1302 SV * const tmpsv = sv_newmortal();
1304 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1305 name = SvPV_nolen_const(tmpsv);
1307 DIE(aTHX_ "Undefined format \"%s\" called", name);
1309 DIE(aTHX_ "Not a format reference");
1312 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1314 IoFLAGS(io) &= ~IOf_DIDTOP;
1315 return doform(cv,gv,PL_op->op_next);
1321 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1322 register IO *io = GvIOp(gv);
1323 PerlIO *ofp = IoOFP(io);
1327 register PERL_CONTEXT *cx;
1329 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1330 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1333 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1334 PL_formtarget != PL_toptarget)
1338 if (!IoTOP_GV(io)) {
1342 if (!IoTOP_NAME(io)) {
1343 if (!IoFMT_NAME(io))
1344 IoFMT_NAME(io) = savepv(GvNAME(gv));
1345 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1346 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1347 if ((topgv && GvFORM(topgv)) ||
1348 !gv_fetchpv("top",FALSE,SVt_PVFM))
1349 IoTOP_NAME(io) = savesvpv(topname);
1351 IoTOP_NAME(io) = savepvn("top", 3);
1353 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1354 if (!topgv || !GvFORM(topgv)) {
1355 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1358 IoTOP_GV(io) = topgv;
1360 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1361 I32 lines = IoLINES_LEFT(io);
1362 const char *s = SvPVX_const(PL_formtarget);
1363 if (lines <= 0) /* Yow, header didn't even fit!!! */
1365 while (lines-- > 0) {
1366 s = strchr(s, '\n');
1372 const STRLEN save = SvCUR(PL_formtarget);
1373 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1374 do_print(PL_formtarget, ofp);
1375 SvCUR_set(PL_formtarget, save);
1376 sv_chop(PL_formtarget, s);
1377 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1380 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1381 do_print(PL_formfeed, ofp);
1382 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1384 PL_formtarget = PL_toptarget;
1385 IoFLAGS(io) |= IOf_DIDTOP;
1388 DIE(aTHX_ "bad top format reference");
1391 SV * const sv = sv_newmortal();
1393 gv_efullname4(sv, fgv, Nullch, FALSE);
1394 name = SvPV_nolen_const(sv);
1396 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1400 DIE(aTHX_ "Undefined top format called");
1403 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1404 return doform(cv,gv,PL_op);
1408 POPBLOCK(cx,PL_curpm);
1414 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1416 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1417 else if (ckWARN(WARN_CLOSED))
1418 report_evil_fh(gv, io, PL_op->op_type);
1423 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1424 if (ckWARN(WARN_IO))
1425 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1427 if (!do_print(PL_formtarget, fp))
1430 FmLINES(PL_formtarget) = 0;
1431 SvCUR_set(PL_formtarget, 0);
1432 *SvEND(PL_formtarget) = '\0';
1433 if (IoFLAGS(io) & IOf_FLUSH)
1434 (void)PerlIO_flush(fp);
1439 PL_formtarget = PL_bodytarget;
1441 return cx->blk_sub.retop;
1446 dVAR; 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);
1525 const int perm = (MAXARG > 3) ? POPi : 0666;
1526 const int mode = POPi;
1531 /* Need TIEHANDLE method ? */
1533 tmps = SvPV_const(sv, len);
1534 /* FIXME? do_open should do const */
1535 if (do_open(gv, (char*)tmps, len, TRUE, mode, perm, Nullfp)) {
1536 IoLINES(GvIOp(gv)) = 0;
1540 PUSHs(&PL_sv_undef);
1547 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1554 Sock_size_t bufsize;
1563 bool charstart = FALSE;
1564 STRLEN charskip = 0;
1568 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1569 && gv && (io = GvIO(gv))
1570 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1575 *MARK = SvTIED_obj((SV*)io, mg);
1577 call_method("READ", G_SCALAR);
1590 sv_setpvn(bufsv, "", 0);
1591 length = SvIVx(*++MARK);
1594 offset = SvIVx(*++MARK);
1598 if (!io || !IoIFP(io)) {
1599 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1600 report_evil_fh(gv, io, PL_op->op_type);
1601 SETERRNO(EBADF,RMS_IFI);
1604 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1605 buffer = SvPVutf8_force(bufsv, blen);
1606 /* UTF-8 may not have been set if they are all low bytes */
1611 buffer = SvPV_force(bufsv, blen);
1612 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1615 DIE(aTHX_ "Negative length");
1623 if (PL_op->op_type == OP_RECV) {
1624 char namebuf[MAXPATHLEN];
1625 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1626 bufsize = sizeof (struct sockaddr_in);
1628 bufsize = sizeof namebuf;
1630 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1634 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1635 /* 'offset' means 'flags' here */
1636 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1637 (struct sockaddr *)namebuf, &bufsize);
1641 /* Bogus return without padding */
1642 bufsize = sizeof (struct sockaddr_in);
1644 SvCUR_set(bufsv, count);
1645 *SvEND(bufsv) = '\0';
1646 (void)SvPOK_only(bufsv);
1650 /* This should not be marked tainted if the fp is marked clean */
1651 if (!(IoFLAGS(io) & IOf_UNTAINT))
1652 SvTAINTED_on(bufsv);
1654 sv_setpvn(TARG, namebuf, bufsize);
1659 if (PL_op->op_type == OP_RECV)
1660 DIE(aTHX_ PL_no_sock_func, "recv");
1662 if (DO_UTF8(bufsv)) {
1663 /* offset adjust in characters not bytes */
1664 blen = sv_len_utf8(bufsv);
1667 if (-offset > (int)blen)
1668 DIE(aTHX_ "Offset outside string");
1671 if (DO_UTF8(bufsv)) {
1672 /* convert offset-as-chars to offset-as-bytes */
1673 if (offset >= (int)blen)
1674 offset += SvCUR(bufsv) - blen;
1676 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1679 bufsize = SvCUR(bufsv);
1680 /* Allocating length + offset + 1 isn't perfect in the case of reading
1681 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1683 (should be 2 * length + offset + 1, or possibly something longer if
1684 PL_encoding is true) */
1685 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1686 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1687 Zero(buffer+bufsize, offset-bufsize, char);
1689 buffer = buffer + offset;
1691 read_target = bufsv;
1693 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1694 concatenate it to the current buffer. */
1696 /* Truncate the existing buffer to the start of where we will be
1698 SvCUR_set(bufsv, offset);
1700 read_target = sv_newmortal();
1701 SvUPGRADE(read_target, SVt_PV);
1702 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1705 if (PL_op->op_type == OP_SYSREAD) {
1706 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1707 if (IoTYPE(io) == IoTYPE_SOCKET) {
1708 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1714 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1719 #ifdef HAS_SOCKET__bad_code_maybe
1720 if (IoTYPE(io) == IoTYPE_SOCKET) {
1721 char namebuf[MAXPATHLEN];
1722 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1723 bufsize = sizeof (struct sockaddr_in);
1725 bufsize = sizeof namebuf;
1727 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1728 (struct sockaddr *)namebuf, &bufsize);
1733 count = PerlIO_read(IoIFP(io), buffer, length);
1734 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1735 if (count == 0 && PerlIO_error(IoIFP(io)))
1739 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1740 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1743 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1744 *SvEND(read_target) = '\0';
1745 (void)SvPOK_only(read_target);
1746 if (fp_utf8 && !IN_BYTES) {
1747 /* Look at utf8 we got back and count the characters */
1748 const char *bend = buffer + count;
1749 while (buffer < bend) {
1751 skip = UTF8SKIP(buffer);
1754 if (buffer - charskip + skip > bend) {
1755 /* partial character - try for rest of it */
1756 length = skip - (bend-buffer);
1757 offset = bend - SvPVX_const(bufsv);
1769 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1770 provided amount read (count) was what was requested (length)
1772 if (got < wanted && count == length) {
1773 length = wanted - got;
1774 offset = bend - SvPVX_const(bufsv);
1777 /* return value is character count */
1781 else if (buffer_utf8) {
1782 /* Let svcatsv upgrade the bytes we read in to utf8.
1783 The buffer is a mortal so will be freed soon. */
1784 sv_catsv_nomg(bufsv, read_target);
1787 /* This should not be marked tainted if the fp is marked clean */
1788 if (!(IoFLAGS(io) & IOf_UNTAINT))
1789 SvTAINTED_on(bufsv);
1802 const int items = (SP - PL_stack_base) - TOPMARK;
1806 sv = sv_2mortal(newSViv(sv_len(*SP)));
1815 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1826 if (PL_op->op_type == OP_SYSWRITE
1827 && gv && (io = GvIO(gv))
1828 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1833 *MARK = SvTIED_obj((SV*)io, mg);
1835 call_method("WRITE", G_SCALAR);
1846 #if Size_t_size > IVSIZE
1847 length = (Size_t)SvNVx(*++MARK);
1849 length = (Size_t)SvIVx(*++MARK);
1851 if ((SSize_t)length < 0)
1852 DIE(aTHX_ "Negative length");
1855 if (!io || !IoIFP(io)) {
1857 if (ckWARN(WARN_CLOSED))
1858 report_evil_fh(gv, io, PL_op->op_type);
1859 SETERRNO(EBADF,RMS_IFI);
1863 if (PerlIO_isutf8(IoIFP(io))) {
1864 if (!SvUTF8(bufsv)) {
1865 bufsv = sv_2mortal(newSVsv(bufsv));
1866 buffer = sv_2pvutf8(bufsv, &blen);
1868 buffer = SvPV_const(bufsv, blen);
1871 if (DO_UTF8(bufsv)) {
1872 /* Not modifying source SV, so making a temporary copy. */
1873 bufsv = sv_2mortal(newSVsv(bufsv));
1874 sv_utf8_downgrade(bufsv, FALSE);
1876 buffer = SvPV_const(bufsv, blen);
1879 if (PL_op->op_type == OP_SYSWRITE) {
1881 if (DO_UTF8(bufsv)) {
1882 /* length and offset are in chars */
1883 blen = sv_len_utf8(bufsv);
1886 offset = SvIVx(*++MARK);
1888 if (-offset > (IV)blen)
1889 DIE(aTHX_ "Offset outside string");
1891 } else if (offset >= (IV)blen && blen > 0)
1892 DIE(aTHX_ "Offset outside string");
1895 if (length > blen - offset)
1896 length = blen - offset;
1897 if (DO_UTF8(bufsv)) {
1898 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1899 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1902 buffer = buffer+offset;
1904 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1905 if (IoTYPE(io) == IoTYPE_SOCKET) {
1906 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1912 /* See the note at doio.c:do_print about filesize limits. --jhi */
1913 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1918 else if (SP > MARK) {
1920 char * const sockbuf = SvPVx(*++MARK, mlen);
1921 /* length is really flags */
1922 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1923 length, (struct sockaddr *)sockbuf, mlen);
1926 /* length is really flags */
1927 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1930 DIE(aTHX_ PL_no_sock_func, "send");
1936 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1937 #if Size_t_size > IVSIZE
1951 return pp_sysread();
1962 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1964 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1966 if (io && !IoIFP(io)) {
1967 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1969 IoFLAGS(io) &= ~IOf_START;
1970 do_open(gv, (char *)"-", 1, FALSE, O_RDONLY, 0, Nullfp);
1971 sv_setpvn(GvSV(gv), "-", 1);
1972 SvSETMAGIC(GvSV(gv));
1974 else if (!nextargv(gv))
1979 gv = PL_last_in_gv; /* eof */
1982 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1984 if (gv && (io = GvIO(gv))
1985 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1988 XPUSHs(SvTIED_obj((SV*)io, mg));
1991 call_method("EOF", G_SCALAR);
1997 PUSHs(boolSV(!gv || do_eof(gv)));
2011 gv = PL_last_in_gv = (GV*)POPs;
2013 if (gv && (io = GvIO(gv))
2014 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2017 XPUSHs(SvTIED_obj((SV*)io, mg));
2020 call_method("TELL", G_SCALAR);
2026 #if LSEEKSIZE > IVSIZE
2027 PUSHn( do_tell(gv) );
2029 PUSHi( do_tell(gv) );
2036 return pp_sysseek();
2044 const int whence = POPi;
2045 #if LSEEKSIZE > IVSIZE
2046 Off_t offset = (Off_t)SvNVx(POPs);
2048 Off_t offset = (Off_t)SvIVx(POPs);
2052 gv = PL_last_in_gv = (GV*)POPs;
2054 if (gv && (io = GvIO(gv))
2055 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2058 XPUSHs(SvTIED_obj((SV*)io, mg));
2059 #if LSEEKSIZE > IVSIZE
2060 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2062 XPUSHs(sv_2mortal(newSViv(offset)));
2064 XPUSHs(sv_2mortal(newSViv(whence)));
2067 call_method("SEEK", G_SCALAR);
2073 if (PL_op->op_type == OP_SEEK)
2074 PUSHs(boolSV(do_seek(gv, offset, whence)));
2076 Off_t sought = do_sysseek(gv, offset, whence);
2078 PUSHs(&PL_sv_undef);
2081 #if LSEEKSIZE > IVSIZE
2086 : newSVpvn(zero_but_true, ZBTLEN);
2087 PUSHs(sv_2mortal(sv));
2096 /* There seems to be no consensus on the length type of truncate()
2097 * and ftruncate(), both off_t and size_t have supporters. In
2098 * general one would think that when using large files, off_t is
2099 * at least as wide as size_t, so using an off_t should be okay. */
2100 /* XXX Configure probe for the length type of *truncate() needed XXX */
2103 #if Off_t_size > IVSIZE
2108 /* Checking for length < 0 is problematic as the type might or
2109 * might not be signed: if it is not, clever compilers will moan. */
2110 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2117 if (PL_op->op_flags & OPf_SPECIAL) {
2118 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2127 TAINT_PROPER("truncate");
2128 if (!(fp = IoIFP(io))) {
2134 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2136 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2146 if (SvTYPE(sv) == SVt_PVGV) {
2147 tmpgv = (GV*)sv; /* *main::FRED for example */
2148 goto do_ftruncate_gv;
2150 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2151 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2152 goto do_ftruncate_gv;
2154 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2155 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2156 goto do_ftruncate_io;
2159 name = SvPV_nolen_const(sv);
2160 TAINT_PROPER("truncate");
2162 if (truncate(name, len) < 0)
2168 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2171 if (my_chsize(tmpfd, len) < 0)
2173 PerlLIO_close(tmpfd);
2182 SETERRNO(EBADF,RMS_IFI);
2196 const unsigned int func = POPu;
2197 const int optype = PL_op->op_type;
2201 IO *io = gv ? GvIOn(gv) : 0;
2203 if (!io || !argsv || !IoIFP(io)) {
2204 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2205 report_evil_fh(gv, io, PL_op->op_type);
2206 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2210 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2213 s = SvPV_force(argsv, len);
2214 need = IOCPARM_LEN(func);
2216 s = Sv_Grow(argsv, need + 1);
2217 SvCUR_set(argsv, need);
2220 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2223 retval = SvIV(argsv);
2224 s = INT2PTR(char*,retval); /* ouch */
2227 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2229 if (optype == OP_IOCTL)
2231 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2233 DIE(aTHX_ "ioctl is not implemented");
2237 DIE(aTHX_ "fcntl is not implemented");
2239 #if defined(OS2) && defined(__EMX__)
2240 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2242 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2246 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2248 if (s[SvCUR(argsv)] != 17)
2249 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2251 s[SvCUR(argsv)] = 0; /* put our null back */
2252 SvSETMAGIC(argsv); /* Assume it has changed */
2261 PUSHp(zero_but_true, ZBTLEN);
2282 if (gv && (io = GvIO(gv)))
2289 (void)PerlIO_flush(fp);
2290 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2293 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2294 report_evil_fh(gv, io, PL_op->op_type);
2296 SETERRNO(EBADF,RMS_IFI);
2301 DIE(aTHX_ PL_no_func, "flock()");
2313 int protocol = POPi;
2319 io = gv ? GvIOn(gv) : NULL;
2322 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2323 report_evil_fh(gv, io, PL_op->op_type);
2325 do_close(gv, FALSE);
2326 SETERRNO(EBADF,LIB_INVARG);
2331 do_close(gv, FALSE);
2333 TAINT_PROPER("socket");
2334 fd = PerlSock_socket(domain, type, protocol);
2337 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2338 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2339 IoTYPE(io) = IoTYPE_SOCKET;
2340 if (!IoIFP(io) || !IoOFP(io)) {
2341 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2342 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2343 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2346 #if defined(HAS_FCNTL) && defined(F_SETFD)
2347 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2351 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2356 DIE(aTHX_ PL_no_sock_func, "socket");
2362 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2368 int protocol = POPi;
2375 io1 = gv1 ? GvIOn(gv1) : NULL;
2376 io2 = gv2 ? GvIOn(gv2) : NULL;
2377 if (!gv1 || !gv2 || !io1 || !io2) {
2378 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2380 report_evil_fh(gv1, io1, PL_op->op_type);
2382 report_evil_fh(gv1, io2, PL_op->op_type);
2385 do_close(gv1, FALSE);
2387 do_close(gv2, FALSE);
2392 do_close(gv1, FALSE);
2394 do_close(gv2, FALSE);
2396 TAINT_PROPER("socketpair");
2397 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2399 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2400 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2401 IoTYPE(io1) = IoTYPE_SOCKET;
2402 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2403 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2404 IoTYPE(io2) = IoTYPE_SOCKET;
2405 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2406 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2407 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2408 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2409 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2410 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2411 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2414 #if defined(HAS_FCNTL) && defined(F_SETFD)
2415 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2416 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2421 DIE(aTHX_ PL_no_sock_func, "socketpair");
2429 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2430 extern void GETPRIVMODE();
2431 extern void GETUSERMODE();
2436 register IO *io = GvIOn(gv);
2440 int mpeprivmode = 0;
2443 if (!io || !IoIFP(io))
2446 addr = SvPV(addrsv, len);
2447 TAINT_PROPER("bind");
2448 #ifdef MPE /* Deal with MPE bind() peculiarities */
2449 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2450 /* The address *MUST* stupidly be zero. */
2451 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2452 /* PRIV mode is required to bind() to ports < 1024. */
2453 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2454 ((struct sockaddr_in *)addr)->sin_port > 0) {
2455 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2460 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2461 (struct sockaddr *)addr, len) >= 0)
2464 #ifdef MPE /* Switch back to USER mode */
2475 if (ckWARN(WARN_CLOSED))
2476 report_evil_fh(gv, io, PL_op->op_type);
2477 SETERRNO(EBADF,SS_IVCHAN);
2480 DIE(aTHX_ PL_no_sock_func, "bind");
2491 register IO *io = GvIOn(gv);
2494 if (!io || !IoIFP(io))
2497 addr = SvPV(addrsv, len);
2498 TAINT_PROPER("connect");
2499 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2505 if (ckWARN(WARN_CLOSED))
2506 report_evil_fh(gv, io, PL_op->op_type);
2507 SETERRNO(EBADF,SS_IVCHAN);
2510 DIE(aTHX_ PL_no_sock_func, "connect");
2520 register IO *io = gv ? GvIOn(gv) : NULL;
2522 if (!gv || !io || !IoIFP(io))
2525 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2531 if (ckWARN(WARN_CLOSED))
2532 report_evil_fh(gv, io, PL_op->op_type);
2533 SETERRNO(EBADF,SS_IVCHAN);
2536 DIE(aTHX_ PL_no_sock_func, "listen");
2548 char namebuf[MAXPATHLEN];
2549 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2550 Sock_size_t len = sizeof (struct sockaddr_in);
2552 Sock_size_t len = sizeof namebuf;
2565 if (!gstio || !IoIFP(gstio))
2569 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2573 do_close(ngv, FALSE);
2574 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2575 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2576 IoTYPE(nstio) = IoTYPE_SOCKET;
2577 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2578 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2579 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2580 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2583 #if defined(HAS_FCNTL) && defined(F_SETFD)
2584 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2588 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2589 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2591 #ifdef __SCO_VERSION__
2592 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2595 PUSHp(namebuf, len);
2599 if (ckWARN(WARN_CLOSED))
2600 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2601 SETERRNO(EBADF,SS_IVCHAN);
2607 DIE(aTHX_ PL_no_sock_func, "accept");
2617 register IO *io = GvIOn(gv);
2619 if (!io || !IoIFP(io))
2622 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2626 if (ckWARN(WARN_CLOSED))
2627 report_evil_fh(gv, io, PL_op->op_type);
2628 SETERRNO(EBADF,SS_IVCHAN);
2631 DIE(aTHX_ PL_no_sock_func, "shutdown");
2638 return pp_ssockopt();
2640 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2648 int optype = PL_op->op_type;
2651 unsigned int optname;
2657 if (optype == OP_GSOCKOPT)
2658 sv = sv_2mortal(NEWSV(22, 257));
2661 optname = (unsigned int) POPi;
2662 lvl = (unsigned int) POPi;
2666 if (!io || !IoIFP(io))
2669 fd = PerlIO_fileno(IoIFP(io));
2673 (void)SvPOK_only(sv);
2677 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2692 aint = (int)SvIV(sv);
2696 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2705 if (ckWARN(WARN_CLOSED))
2706 report_evil_fh(gv, io, optype);
2707 SETERRNO(EBADF,SS_IVCHAN);
2712 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2719 return pp_getpeername();
2721 DIE(aTHX_ PL_no_sock_func, "getsockname");
2729 int optype = PL_op->op_type;
2733 register IO *io = GvIOn(gv);
2736 if (!io || !IoIFP(io))
2739 sv = sv_2mortal(NEWSV(22, 257));
2740 (void)SvPOK_only(sv);
2744 fd = PerlIO_fileno(IoIFP(io));
2746 case OP_GETSOCKNAME:
2747 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2750 case OP_GETPEERNAME:
2751 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2753 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2755 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";
2756 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2757 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2758 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2759 sizeof(u_short) + sizeof(struct in_addr))) {
2766 #ifdef BOGUS_GETNAME_RETURN
2767 /* Interactive Unix, getpeername() and getsockname()
2768 does not return valid namelen */
2769 if (len == BOGUS_GETNAME_RETURN)
2770 len = sizeof(struct sockaddr);
2778 if (ckWARN(WARN_CLOSED))
2779 report_evil_fh(gv, io, optype);
2780 SETERRNO(EBADF,SS_IVCHAN);
2785 DIE(aTHX_ PL_no_sock_func, "getpeername");
2804 if (PL_op->op_flags & OPf_REF) {
2806 if (PL_op->op_type == OP_LSTAT) {
2807 if (gv != PL_defgv) {
2808 if (ckWARN(WARN_IO))
2809 Perl_warner(aTHX_ packWARN(WARN_IO),
2810 "lstat() on filehandle %s", GvENAME(gv));
2811 } else if (PL_laststype != OP_LSTAT)
2812 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2816 if (gv != PL_defgv) {
2817 PL_laststype = OP_STAT;
2819 sv_setpvn(PL_statname, "", 0);
2820 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2821 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2823 if (PL_laststatval < 0) {
2824 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2825 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2831 if (SvTYPE(sv) == SVt_PVGV) {
2835 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2837 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2838 Perl_warner(aTHX_ packWARN(WARN_IO),
2839 "lstat() on filehandle %s", GvENAME(gv));
2842 sv_setpv(PL_statname, SvPV_const(sv,n_a));
2844 PL_laststype = PL_op->op_type;
2845 if (PL_op->op_type == OP_LSTAT)
2846 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2848 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2849 if (PL_laststatval < 0) {
2850 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2851 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2857 if (gimme != G_ARRAY) {
2858 if (gimme != G_VOID)
2859 XPUSHs(boolSV(max));
2865 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2866 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2867 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2868 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2869 #if Uid_t_size > IVSIZE
2870 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2872 # if Uid_t_sign <= 0
2873 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2875 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2878 #if Gid_t_size > IVSIZE
2879 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2881 # if Gid_t_sign <= 0
2882 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2884 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2887 #ifdef USE_STAT_RDEV
2888 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2890 PUSHs(sv_2mortal(newSVpvn("", 0)));
2892 #if Off_t_size > IVSIZE
2893 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2895 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2898 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2899 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2900 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2902 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2903 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2904 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2906 #ifdef USE_STAT_BLOCKS
2907 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2908 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2910 PUSHs(sv_2mortal(newSVpvn("", 0)));
2911 PUSHs(sv_2mortal(newSVpvn("", 0)));
2917 /* This macro is used by the stacked filetest operators :
2918 * if the previous filetest failed, short-circuit and pass its value.
2919 * Else, discard it from the stack and continue. --rgs
2921 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2922 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2923 else { (void)POPs; PUTBACK; } \
2930 STACKED_FTEST_CHECK;
2931 #if defined(HAS_ACCESS) && defined(R_OK)
2932 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2933 result = access(POPpx, R_OK);
2948 if (cando(S_IRUSR, 0, &PL_statcache))
2957 STACKED_FTEST_CHECK;
2958 #if defined(HAS_ACCESS) && defined(W_OK)
2959 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2960 result = access(POPpx, W_OK);
2975 if (cando(S_IWUSR, 0, &PL_statcache))
2984 STACKED_FTEST_CHECK;
2985 #if defined(HAS_ACCESS) && defined(X_OK)
2986 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2987 result = access(POPpx, X_OK);
3002 if (cando(S_IXUSR, 0, &PL_statcache))
3011 STACKED_FTEST_CHECK;
3012 #ifdef PERL_EFF_ACCESS_R_OK
3013 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3015 result = PERL_EFF_ACCESS_R_OK(POPpx);
3030 if (cando(S_IRUSR, 1, &PL_statcache))
3039 STACKED_FTEST_CHECK;
3040 #ifdef PERL_EFF_ACCESS_W_OK
3041 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3043 result = PERL_EFF_ACCESS_W_OK(POPpx);
3058 if (cando(S_IWUSR, 1, &PL_statcache))
3067 STACKED_FTEST_CHECK;
3068 #ifdef PERL_EFF_ACCESS_X_OK
3069 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3070 result = PERL_EFF_ACCESS_X_OK(POPpx);
3085 if (cando(S_IXUSR, 1, &PL_statcache))
3094 STACKED_FTEST_CHECK;
3104 return pp_ftrowned();
3111 STACKED_FTEST_CHECK;
3116 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3126 STACKED_FTEST_CHECK;
3131 if (PL_statcache.st_size == 0)
3140 STACKED_FTEST_CHECK;
3145 #if Off_t_size > IVSIZE
3146 PUSHn(PL_statcache.st_size);
3148 PUSHi(PL_statcache.st_size);
3157 STACKED_FTEST_CHECK;
3162 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3170 STACKED_FTEST_CHECK;
3175 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3183 STACKED_FTEST_CHECK;
3188 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3196 STACKED_FTEST_CHECK;
3201 if (S_ISSOCK(PL_statcache.st_mode))
3210 STACKED_FTEST_CHECK;
3215 if (S_ISCHR(PL_statcache.st_mode))
3224 STACKED_FTEST_CHECK;
3229 if (S_ISBLK(PL_statcache.st_mode))
3238 STACKED_FTEST_CHECK;
3243 if (S_ISREG(PL_statcache.st_mode))
3252 STACKED_FTEST_CHECK;
3257 if (S_ISDIR(PL_statcache.st_mode))
3266 STACKED_FTEST_CHECK;
3271 if (S_ISFIFO(PL_statcache.st_mode))
3278 I32 result = my_lstat();
3282 if (S_ISLNK(PL_statcache.st_mode))
3292 STACKED_FTEST_CHECK;
3297 if (PL_statcache.st_mode & S_ISUID)
3308 STACKED_FTEST_CHECK;
3313 if (PL_statcache.st_mode & S_ISGID)
3324 STACKED_FTEST_CHECK;
3329 if (PL_statcache.st_mode & S_ISVTX)
3342 STACKED_FTEST_CHECK;
3344 if (PL_op->op_flags & OPf_REF)
3346 else if (isGV(TOPs))
3348 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3349 gv = (GV*)SvRV(POPs);
3351 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3353 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3354 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3355 else if (tmpsv && SvOK(tmpsv)) {
3356 char *tmps = SvPV_nolen(tmpsv);
3364 if (PerlLIO_isatty(fd))
3369 #if defined(atarist) /* this will work with atariST. Configure will
3370 make guesses for other systems. */
3371 # define FILE_base(f) ((f)->_base)
3372 # define FILE_ptr(f) ((f)->_ptr)
3373 # define FILE_cnt(f) ((f)->_cnt)
3374 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3384 register STDCHAR *s;
3390 STACKED_FTEST_CHECK;
3392 if (PL_op->op_flags & OPf_REF)
3394 else if (isGV(TOPs))
3396 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3397 gv = (GV*)SvRV(POPs);
3403 if (gv == PL_defgv) {
3405 io = GvIO(PL_statgv);
3408 goto really_filename;
3413 PL_laststatval = -1;
3414 sv_setpvn(PL_statname, "", 0);
3415 io = GvIO(PL_statgv);
3417 if (io && IoIFP(io)) {
3418 if (! PerlIO_has_base(IoIFP(io)))
3419 DIE(aTHX_ "-T and -B not implemented on filehandles");
3420 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3421 if (PL_laststatval < 0)
3423 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3424 if (PL_op->op_type == OP_FTTEXT)
3429 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3430 i = PerlIO_getc(IoIFP(io));
3432 (void)PerlIO_ungetc(IoIFP(io),i);
3434 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3436 len = PerlIO_get_bufsiz(IoIFP(io));
3437 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3438 /* sfio can have large buffers - limit to 512 */
3443 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3445 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3447 SETERRNO(EBADF,RMS_IFI);
3455 PL_laststype = OP_STAT;
3456 sv_setpv(PL_statname, SvPV_nolen(sv));
3457 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3458 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen(PL_statname), '\n'))
3459 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3462 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3463 if (PL_laststatval < 0) {
3464 (void)PerlIO_close(fp);
3467 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3468 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3469 (void)PerlIO_close(fp);
3471 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3472 RETPUSHNO; /* special case NFS directories */
3473 RETPUSHYES; /* null file is anything */
3478 /* now scan s to look for textiness */
3479 /* XXX ASCII dependent code */
3481 #if defined(DOSISH) || defined(USEMYBINMODE)
3482 /* ignore trailing ^Z on short files */
3483 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3487 for (i = 0; i < len; i++, s++) {
3488 if (!*s) { /* null never allowed in text */
3493 else if (!(isPRINT(*s) || isSPACE(*s)))
3496 else if (*s & 128) {
3498 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3501 /* utf8 characters don't count as odd */
3502 if (UTF8_IS_START(*s)) {
3503 int ulen = UTF8SKIP(s);
3504 if (ulen < len - i) {
3506 for (j = 1; j < ulen; j++) {
3507 if (!UTF8_IS_CONTINUATION(s[j]))
3510 --ulen; /* loop does extra increment */
3520 *s != '\n' && *s != '\r' && *s != '\b' &&
3521 *s != '\t' && *s != '\f' && *s != 27)
3526 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3550 if( !tmps || !*tmps ) {
3551 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3552 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3554 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3559 deprecate("chdir('') or chdir(undef) as chdir()");
3560 tmps = SvPV_nolen_const(*svp);
3564 TAINT_PROPER("chdir");
3569 TAINT_PROPER("chdir");
3570 PUSHi( PerlDir_chdir(tmps) >= 0 );
3572 /* Clear the DEFAULT element of ENV so we'll get the new value
3574 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3582 dSP; dMARK; dTARGET;
3583 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3589 DIE(aTHX_ PL_no_func, "chown");
3598 TAINT_PROPER("chroot");
3599 PUSHi( chroot(tmps) >= 0 );
3602 DIE(aTHX_ PL_no_func, "chroot");
3608 dSP; dMARK; dTARGET;
3610 value = (I32)apply(PL_op->op_type, MARK, SP);
3618 dSP; dMARK; dTARGET;
3620 value = (I32)apply(PL_op->op_type, MARK, SP);
3628 dSP; dMARK; dTARGET;
3630 value = (I32)apply(PL_op->op_type, MARK, SP);
3640 const char *tmps2 = POPpconstx;
3641 const char *tmps = SvPV_nolen_const(TOPs);
3642 TAINT_PROPER("rename");
3644 anum = PerlLIO_rename(tmps, tmps2);
3646 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3647 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3650 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3651 (void)UNLINK(tmps2);
3652 if (!(anum = link(tmps, tmps2)))
3653 anum = UNLINK(tmps);
3665 const char *tmps2 = POPpconstx;
3666 const char *tmps = SvPV_nolen_const(TOPs);
3667 TAINT_PROPER("link");
3668 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3671 DIE(aTHX_ PL_no_func, "link");
3679 const char *tmps2 = POPpconstx;
3680 const char *tmps = SvPV_nolen_const(TOPs);
3681 TAINT_PROPER("symlink");
3682 SETi( symlink(tmps, tmps2) >= 0 );
3685 DIE(aTHX_ PL_no_func, "symlink");
3695 char buf[MAXPATHLEN];
3698 #ifndef INCOMPLETE_TAINTS
3702 len = readlink(tmps, buf, sizeof(buf) - 1);
3710 RETSETUNDEF; /* just pretend it's a normal file */
3714 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3716 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3718 char * const save_filename = filename;
3724 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3725 strcpy(cmdline, cmd);
3726 strcat(cmdline, " ");
3727 for (s = cmdline + strlen(cmdline); *filename; ) {
3732 myfp = PerlProc_popen(cmdline, "r");
3736 SV *tmpsv = sv_newmortal();
3737 /* Need to save/restore 'PL_rs' ?? */
3738 s = sv_gets(tmpsv, myfp, 0);
3739 (void)PerlProc_pclose(myfp);
3743 #ifdef HAS_SYS_ERRLIST
3748 /* you don't see this */
3750 #ifdef HAS_SYS_ERRLIST
3758 if (instr(s, errmsg)) {
3765 #define EACCES EPERM
3767 if (instr(s, "cannot make"))
3768 SETERRNO(EEXIST,RMS_FEX);
3769 else if (instr(s, "existing file"))
3770 SETERRNO(EEXIST,RMS_FEX);
3771 else if (instr(s, "ile exists"))
3772 SETERRNO(EEXIST,RMS_FEX);
3773 else if (instr(s, "non-exist"))
3774 SETERRNO(ENOENT,RMS_FNF);
3775 else if (instr(s, "does not exist"))
3776 SETERRNO(ENOENT,RMS_FNF);
3777 else if (instr(s, "not empty"))
3778 SETERRNO(EBUSY,SS_DEVOFFLINE);
3779 else if (instr(s, "cannot access"))
3780 SETERRNO(EACCES,RMS_PRV);
3782 SETERRNO(EPERM,RMS_PRV);
3785 else { /* some mkdirs return no failure indication */
3786 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3787 if (PL_op->op_type == OP_RMDIR)
3792 SETERRNO(EACCES,RMS_PRV); /* a guess */
3801 /* This macro removes trailing slashes from a directory name.
3802 * Different operating and file systems take differently to
3803 * trailing slashes. According to POSIX 1003.1 1996 Edition
3804 * any number of trailing slashes should be allowed.
3805 * Thusly we snip them away so that even non-conforming
3806 * systems are happy.
3807 * We should probably do this "filtering" for all
3808 * the functions that expect (potentially) directory names:
3809 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3810 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3812 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3813 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3816 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3817 (tmps) = savepvn((tmps), (len)); \
3837 TRIMSLASHES(tmps,len,copy);
3839 TAINT_PROPER("mkdir");
3841 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3843 SETi( dooneliner("mkdir", tmps) );
3844 oldumask = PerlLIO_umask(0);
3845 PerlLIO_umask(oldumask);
3846 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3860 TRIMSLASHES(tmps,len,copy);
3861 TAINT_PROPER("rmdir");
3863 SETi( PerlDir_rmdir(tmps) >= 0 );
3865 SETi( dooneliner("rmdir", tmps) );
3872 /* Directory calls. */
3876 #if defined(Direntry_t) && defined(HAS_READDIR)
3878 const char *dirname = POPpconstx;
3880 register IO *io = GvIOn(gv);
3886 PerlDir_close(IoDIRP(io));
3887 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3893 SETERRNO(EBADF,RMS_DIR);
3896 DIE(aTHX_ PL_no_dir_func, "opendir");
3902 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3903 DIE(aTHX_ PL_no_dir_func, "readdir");
3905 #if !defined(I_DIRENT) && !defined(VMS)
3906 Direntry_t *readdir (DIR *);
3911 const I32 gimme = GIMME;
3912 GV *gv = (GV *)POPs;
3913 register Direntry_t *dp;
3914 register IO *io = GvIOn(gv);
3916 if (!io || !IoDIRP(io))
3920 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3924 sv = newSVpvn(dp->d_name, dp->d_namlen);
3926 sv = newSVpv(dp->d_name, 0);
3928 #ifndef INCOMPLETE_TAINTS
3929 if (!(IoFLAGS(io) & IOf_UNTAINT))
3932 XPUSHs(sv_2mortal(sv));
3934 while (gimme == G_ARRAY);
3936 if (!dp && gimme != G_ARRAY)
3943 SETERRNO(EBADF,RMS_ISI);
3944 if (GIMME == G_ARRAY)
3953 #if defined(HAS_TELLDIR) || defined(telldir)
3955 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3956 /* XXX netbsd still seemed to.
3957 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3958 --JHI 1999-Feb-02 */
3959 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3960 long telldir (DIR *);
3963 register IO *io = GvIOn(gv);
3965 if (!io || !IoDIRP(io))
3968 PUSHi( PerlDir_tell(IoDIRP(io)) );
3972 SETERRNO(EBADF,RMS_ISI);
3975 DIE(aTHX_ PL_no_dir_func, "telldir");
3981 #if defined(HAS_SEEKDIR) || defined(seekdir)
3985 register IO *io = GvIOn(gv);
3987 if (!io || !IoDIRP(io))
3990 (void)PerlDir_seek(IoDIRP(io), along);
3995 SETERRNO(EBADF,RMS_ISI);
3998 DIE(aTHX_ PL_no_dir_func, "seekdir");
4004 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4007 register IO *io = GvIOn(gv);
4009 if (!io || !IoDIRP(io))
4012 (void)PerlDir_rewind(IoDIRP(io));
4016 SETERRNO(EBADF,RMS_ISI);
4019 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4025 #if defined(Direntry_t) && defined(HAS_READDIR)
4028 register IO *io = GvIOn(gv);
4030 if (!io || !IoDIRP(io))
4033 #ifdef VOID_CLOSEDIR
4034 PerlDir_close(IoDIRP(io));
4036 if (PerlDir_close(IoDIRP(io)) < 0) {
4037 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4046 SETERRNO(EBADF,RMS_IFI);
4049 DIE(aTHX_ PL_no_dir_func, "closedir");
4053 /* Process control. */
4063 PERL_FLUSHALL_FOR_CHILD;
4064 childpid = PerlProc_fork();
4069 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4070 SvREADONLY_off(GvSV(tmpgv));
4071 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4072 SvREADONLY_on(GvSV(tmpgv));
4074 #ifdef THREADS_HAVE_PIDS
4075 PL_ppid = (IV)getppid();
4077 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4082 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4087 PERL_FLUSHALL_FOR_CHILD;
4088 childpid = PerlProc_fork();
4094 DIE(aTHX_ PL_no_func, "fork");
4101 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4106 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4107 childpid = wait4pid(-1, &argflags, 0);
4109 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4114 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4115 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4116 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4118 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4123 DIE(aTHX_ PL_no_func, "wait");
4129 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4138 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4139 result = wait4pid(pid, &argflags, optype);
4141 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4146 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4147 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4148 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4150 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4155 DIE(aTHX_ PL_no_func, "waitpid");
4161 dSP; dMARK; dORIGMARK; dTARGET;
4167 while (++MARK <= SP) {
4168 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4173 TAINT_PROPER("system");
4175 PERL_FLUSHALL_FOR_CHILD;
4176 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4182 if (PerlProc_pipe(pp) >= 0)
4184 while ((childpid = PerlProc_fork()) == -1) {
4185 if (errno != EAGAIN) {
4190 PerlLIO_close(pp[0]);
4191 PerlLIO_close(pp[1]);
4198 Sigsave_t ihand,qhand; /* place to save signals during system() */
4202 PerlLIO_close(pp[1]);
4204 rsignal_save(SIGINT, SIG_IGN, &ihand);
4205 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4208 result = wait4pid(childpid, &status, 0);
4209 } while (result == -1 && errno == EINTR);
4211 (void)rsignal_restore(SIGINT, &ihand);
4212 (void)rsignal_restore(SIGQUIT, &qhand);
4214 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4215 do_execfree(); /* free any memory child malloced on fork */
4221 while (n < sizeof(int)) {
4222 n1 = PerlLIO_read(pp[0],
4223 (void*)(((char*)&errkid)+n),
4229 PerlLIO_close(pp[0]);
4230 if (n) { /* Error */
4231 if (n != sizeof(int))
4232 DIE(aTHX_ "panic: kid popen errno read");
4233 errno = errkid; /* Propagate errno from kid */
4234 STATUS_CURRENT = -1;
4237 PUSHi(STATUS_CURRENT);
4241 PerlLIO_close(pp[0]);
4242 #if defined(HAS_FCNTL) && defined(F_SETFD)
4243 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4246 if (PL_op->op_flags & OPf_STACKED) {
4247 SV *really = *++MARK;
4248 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4250 else if (SP - MARK != 1)
4251 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4253 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4257 #else /* ! FORK or VMS or OS/2 */
4260 if (PL_op->op_flags & OPf_STACKED) {
4261 SV *really = *++MARK;
4262 # if defined(WIN32) || defined(OS2) || defined(SYMBIAN)
4263 value = (I32)do_aspawn(really, MARK, SP);
4265 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4268 else if (SP - MARK != 1) {
4269 # if defined(WIN32) || defined(OS2) || defined(SYMBIAN)
4270 value = (I32)do_aspawn(Nullsv, MARK, SP);
4272 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4276 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4278 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4280 STATUS_NATIVE_SET(value);
4283 PUSHi(result ? value : STATUS_CURRENT);
4284 #endif /* !FORK or VMS */
4290 dSP; dMARK; dORIGMARK; dTARGET;
4295 while (++MARK <= SP) {
4296 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4301 TAINT_PROPER("exec");
4303 PERL_FLUSHALL_FOR_CHILD;
4304 if (PL_op->op_flags & OPf_STACKED) {
4305 SV *really = *++MARK;
4306 value = (I32)do_aexec(really, MARK, SP);
4308 else if (SP - MARK != 1)
4310 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4314 (void ) do_aspawn(Nullsv, MARK, SP);
4318 value = (I32)do_aexec(Nullsv, MARK, SP);
4323 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4326 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4329 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4342 dSP; dMARK; dTARGET;
4344 value = (I32)apply(PL_op->op_type, MARK, SP);
4349 DIE(aTHX_ PL_no_func, "kill");
4357 # ifdef THREADS_HAVE_PIDS
4358 if (PL_ppid != 1 && getppid() == 1)
4359 /* maybe the parent process has died. Refresh ppid cache */
4363 XPUSHi( getppid() );
4367 DIE(aTHX_ PL_no_func, "getppid");
4383 pgrp = (I32)BSD_GETPGRP(pid);
4385 if (pid != 0 && pid != PerlProc_getpid())
4386 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4392 DIE(aTHX_ PL_no_func, "getpgrp()");
4411 TAINT_PROPER("setpgrp");
4413 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4415 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4416 || (pid != 0 && pid != PerlProc_getpid()))
4418 DIE(aTHX_ "setpgrp can't take arguments");
4420 SETi( setpgrp() >= 0 );
4421 #endif /* USE_BSDPGRP */
4424 DIE(aTHX_ PL_no_func, "setpgrp()");
4430 #ifdef HAS_GETPRIORITY
4434 SETi( getpriority(which, who) );
4437 DIE(aTHX_ PL_no_func, "getpriority()");
4443 #ifdef HAS_SETPRIORITY
4448 TAINT_PROPER("setpriority");
4449 SETi( setpriority(which, who, niceval) >= 0 );
4452 DIE(aTHX_ PL_no_func, "setpriority()");
4462 XPUSHn( time(Null(Time_t*)) );
4464 XPUSHi( time(Null(Time_t*)) );
4475 (void)PerlProc_times(&PL_timesbuf);
4477 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4478 /* struct tms, though same data */
4482 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4483 if (GIMME == G_ARRAY) {
4484 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4485 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4486 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4492 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4494 if (GIMME == G_ARRAY) {
4495 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4496 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4497 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4501 DIE(aTHX_ "times not implemented");
4503 #endif /* HAS_TIMES */
4515 const struct tm *tmbuf;
4516 static const char * const dayname[] =
4517 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4518 static const char * const monname[] =
4519 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4520 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4526 when = (Time_t)SvNVx(POPs);
4528 when = (Time_t)SvIVx(POPs);
4531 if (PL_op->op_type == OP_LOCALTIME)
4532 tmbuf = localtime(&when);
4534 tmbuf = gmtime(&when);
4536 if (GIMME != G_ARRAY) {
4542 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4543 dayname[tmbuf->tm_wday],
4544 monname[tmbuf->tm_mon],
4549 tmbuf->tm_year + 1900);
4550 PUSHs(sv_2mortal(tsv));
4555 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4556 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4557 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4558 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4559 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4560 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4561 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4562 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4563 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4574 anum = alarm((unsigned int)anum);
4581 DIE(aTHX_ PL_no_func, "alarm");
4592 (void)time(&lasttime);
4597 PerlProc_sleep((unsigned int)duration);
4600 XPUSHi(when - lasttime);
4604 /* Shared memory. */
4618 return pp_shmwrite();
4623 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4624 dSP; dMARK; dTARGET;
4625 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4634 /* Message passing. */
4648 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4649 dSP; dMARK; dTARGET;
4650 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4661 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4662 dSP; dMARK; dTARGET;
4663 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4676 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4677 dSP; dMARK; dTARGET;
4678 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4685 DIE(aTHX_ "System V IPC is not implemented on this machine");
4691 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4692 dSP; dMARK; dTARGET;
4693 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4701 PUSHp(zero_but_true, ZBTLEN);
4711 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4712 dSP; dMARK; dTARGET;
4713 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4722 /* Get system info. */
4726 #ifdef HAS_GETHOSTBYNAME
4727 return pp_ghostent();
4729 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4735 #ifdef HAS_GETHOSTBYADDR
4736 return pp_ghostent();
4738 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4744 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4746 I32 which = PL_op->op_type;
4747 register char **elem;
4749 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4750 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4751 struct hostent *gethostbyname(Netdb_name_t);
4752 struct hostent *gethostent(void);
4754 struct hostent *hent;
4758 if (which == OP_GHBYNAME) {
4759 #ifdef HAS_GETHOSTBYNAME
4760 char* name = POPpbytex;
4761 hent = PerlSock_gethostbyname(name);
4763 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4766 else if (which == OP_GHBYADDR) {
4767 #ifdef HAS_GETHOSTBYADDR
4768 int addrtype = POPi;
4771 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4773 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4775 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4779 #ifdef HAS_GETHOSTENT
4780 hent = PerlSock_gethostent();
4782 DIE(aTHX_ PL_no_sock_func, "gethostent");
4785 #ifdef HOST_NOT_FOUND
4787 #ifdef USE_REENTRANT_API
4788 # ifdef USE_GETHOSTENT_ERRNO
4789 h_errno = PL_reentrant_buffer->_gethostent_errno;
4792 STATUS_NATIVE_SET(h_errno);
4796 if (GIMME != G_ARRAY) {
4797 PUSHs(sv = sv_newmortal());
4799 if (which == OP_GHBYNAME) {
4801 sv_setpvn(sv, hent->h_addr, hent->h_length);
4804 sv_setpv(sv, (char*)hent->h_name);
4810 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4811 sv_setpv(sv, (char*)hent->h_name);
4812 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4813 for (elem = hent->h_aliases; elem && *elem; elem++) {
4814 sv_catpv(sv, *elem);
4816 sv_catpvn(sv, " ", 1);
4818 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4819 sv_setiv(sv, (IV)hent->h_addrtype);
4820 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4821 len = hent->h_length;
4822 sv_setiv(sv, (IV)len);
4824 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4825 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4826 sv_setpvn(sv, *elem, len);
4829 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831 sv_setpvn(sv, hent->h_addr, len);
4836 DIE(aTHX_ PL_no_sock_func, "gethostent");
4842 #ifdef HAS_GETNETBYNAME
4843 return pp_gnetent();
4845 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4851 #ifdef HAS_GETNETBYADDR
4852 return pp_gnetent();
4854 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4860 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4862 I32 which = PL_op->op_type;
4863 register char **elem;
4865 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4866 struct netent *getnetbyaddr(Netdb_net_t, int);
4867 struct netent *getnetbyname(Netdb_name_t);
4868 struct netent *getnetent(void);
4870 struct netent *nent;
4872 if (which == OP_GNBYNAME){
4873 #ifdef HAS_GETNETBYNAME
4874 char *name = POPpbytex;
4875 nent = PerlSock_getnetbyname(name);
4877 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4880 else if (which == OP_GNBYADDR) {
4881 #ifdef HAS_GETNETBYADDR
4882 int addrtype = POPi;
4883 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4884 nent = PerlSock_getnetbyaddr(addr, addrtype);
4886 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4890 #ifdef HAS_GETNETENT
4891 nent = PerlSock_getnetent();
4893 DIE(aTHX_ PL_no_sock_func, "getnetent");
4896 #ifdef HOST_NOT_FOUND
4898 #ifdef USE_REENTRANT_API
4899 # ifdef USE_GETNETENT_ERRNO
4900 h_errno = PL_reentrant_buffer->_getnetent_errno;
4903 STATUS_NATIVE_SET(h_errno);
4908 if (GIMME != G_ARRAY) {
4909 PUSHs(sv = sv_newmortal());
4911 if (which == OP_GNBYNAME)
4912 sv_setiv(sv, (IV)nent->n_net);
4914 sv_setpv(sv, nent->n_name);
4920 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4921 sv_setpv(sv, nent->n_name);
4922 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4923 for (elem = nent->n_aliases; elem && *elem; elem++) {
4924 sv_catpv(sv, *elem);
4926 sv_catpvn(sv, " ", 1);
4928 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4929 sv_setiv(sv, (IV)nent->n_addrtype);
4930 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4931 sv_setiv(sv, (IV)nent->n_net);
4936 DIE(aTHX_ PL_no_sock_func, "getnetent");
4942 #ifdef HAS_GETPROTOBYNAME
4943 return pp_gprotoent();
4945 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4951 #ifdef HAS_GETPROTOBYNUMBER
4952 return pp_gprotoent();
4954 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4960 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4962 I32 which = PL_op->op_type;
4963 register char **elem;
4965 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4966 struct protoent *getprotobyname(Netdb_name_t);
4967 struct protoent *getprotobynumber(int);
4968 struct protoent *getprotoent(void);
4970 struct protoent *pent;
4972 if (which == OP_GPBYNAME) {
4973 #ifdef HAS_GETPROTOBYNAME
4974 char* name = POPpbytex;
4975 pent = PerlSock_getprotobyname(name);
4977 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4980 else if (which == OP_GPBYNUMBER) {
4981 #ifdef HAS_GETPROTOBYNUMBER
4983 pent = PerlSock_getprotobynumber(number);
4985 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4989 #ifdef HAS_GETPROTOENT
4990 pent = PerlSock_getprotoent();
4992 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4996 if (GIMME != G_ARRAY) {
4997 PUSHs(sv = sv_newmortal());
4999 if (which == OP_GPBYNAME)
5000 sv_setiv(sv, (IV)pent->p_proto);
5002 sv_setpv(sv, pent->p_name);
5008 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5009 sv_setpv(sv, pent->p_name);
5010 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5011 for (elem = pent->p_aliases; elem && *elem; elem++) {
5012 sv_catpv(sv, *elem);
5014 sv_catpvn(sv, " ", 1);
5016 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5017 sv_setiv(sv, (IV)pent->p_proto);
5022 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5028 #ifdef HAS_GETSERVBYNAME
5029 return pp_gservent();
5031 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5037 #ifdef HAS_GETSERVBYPORT
5038 return pp_gservent();
5040 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5046 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5048 I32 which = PL_op->op_type;
5049 register char **elem;
5051 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5052 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5053 struct servent *getservbyport(int, Netdb_name_t);
5054 struct servent *getservent(void);
5056 struct servent *sent;
5058 if (which == OP_GSBYNAME) {
5059 #ifdef HAS_GETSERVBYNAME
5060 char *proto = POPpbytex;
5061 char *name = POPpbytex;
5063 if (proto && !*proto)
5066 sent = PerlSock_getservbyname(name, proto);
5068 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5071 else if (which == OP_GSBYPORT) {
5072 #ifdef HAS_GETSERVBYPORT
5073 char *proto = POPpbytex;
5074 unsigned short port = (unsigned short)POPu;
5076 if (proto && !*proto)
5080 port = PerlSock_htons(port);
5082 sent = PerlSock_getservbyport(port, proto);
5084 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5088 #ifdef HAS_GETSERVENT
5089 sent = PerlSock_getservent();
5091 DIE(aTHX_ PL_no_sock_func, "getservent");
5095 if (GIMME != G_ARRAY) {
5096 PUSHs(sv = sv_newmortal());
5098 if (which == OP_GSBYNAME) {
5100 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5102 sv_setiv(sv, (IV)(sent->s_port));
5106 sv_setpv(sv, sent->s_name);
5112 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5113 sv_setpv(sv, sent->s_name);
5114 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5115 for (elem = sent->s_aliases; elem && *elem; elem++) {
5116 sv_catpv(sv, *elem);
5118 sv_catpvn(sv, " ", 1);
5120 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5122 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5124 sv_setiv(sv, (IV)(sent->s_port));
5126 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5127 sv_setpv(sv, sent->s_proto);
5132 DIE(aTHX_ PL_no_sock_func, "getservent");
5138 #ifdef HAS_SETHOSTENT
5140 PerlSock_sethostent(TOPi);
5143 DIE(aTHX_ PL_no_sock_func, "sethostent");
5149 #ifdef HAS_SETNETENT
5151 PerlSock_setnetent(TOPi);
5154 DIE(aTHX_ PL_no_sock_func, "setnetent");
5160 #ifdef HAS_SETPROTOENT
5162 PerlSock_setprotoent(TOPi);
5165 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5171 #ifdef HAS_SETSERVENT
5173 PerlSock_setservent(TOPi);
5176 DIE(aTHX_ PL_no_sock_func, "setservent");
5182 #ifdef HAS_ENDHOSTENT
5184 PerlSock_endhostent();
5188 DIE(aTHX_ PL_no_sock_func, "endhostent");
5194 #ifdef HAS_ENDNETENT
5196 PerlSock_endnetent();
5200 DIE(aTHX_ PL_no_sock_func, "endnetent");
5206 #ifdef HAS_ENDPROTOENT
5208 PerlSock_endprotoent();
5212 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5218 #ifdef HAS_ENDSERVENT
5220 PerlSock_endservent();
5224 DIE(aTHX_ PL_no_sock_func, "endservent");
5233 DIE(aTHX_ PL_no_func, "getpwnam");
5242 DIE(aTHX_ PL_no_func, "getpwuid");
5250 I32 which = PL_op->op_type;
5252 struct passwd *pwent = NULL;
5254 * We currently support only the SysV getsp* shadow password interface.
5255 * The interface is declared in <shadow.h> and often one needs to link
5256 * with -lsecurity or some such.
5257 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5260 * AIX getpwnam() is clever enough to return the encrypted password
5261 * only if the caller (euid?) is root.
5263 * There are at least three other shadow password APIs. Many platforms
5264 * seem to contain more than one interface for accessing the shadow
5265 * password databases, possibly for compatibility reasons.
5266 * The getsp*() is by far he simplest one, the other two interfaces
5267 * are much more complicated, but also very similar to each other.
5272 * struct pr_passwd *getprpw*();
5273 * The password is in
5274 * char getprpw*(...).ufld.fd_encrypt[]
5275 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5280 * struct es_passwd *getespw*();
5281 * The password is in
5282 * char *(getespw*(...).ufld.fd_encrypt)
5283 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5286 * struct userpw *getuserpw();
5287 * The password is in
5288 * char *(getuserpw(...)).spw_upw_passwd
5289 * (but the de facto standard getpwnam() should work okay)
5291 * Mention I_PROT here so that Configure probes for it.
5293 * In HP-UX for getprpw*() the manual page claims that one should include
5294 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5295 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5296 * and pp_sys.c already includes <shadow.h> if there is such.
5298 * Note that <sys/security.h> is already probed for, but currently
5299 * it is only included in special cases.
5301 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5302 * be preferred interface, even though also the getprpw*() interface
5303 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5304 * One also needs to call set_auth_parameters() in main() before
5305 * doing anything else, whether one is using getespw*() or getprpw*().
5307 * Note that accessing the shadow databases can be magnitudes
5308 * slower than accessing the standard databases.
5313 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5314 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5315 * the pw_comment is left uninitialized. */
5316 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5322 char* name = POPpbytex;
5323 pwent = getpwnam(name);
5329 pwent = getpwuid(uid);
5333 # ifdef HAS_GETPWENT
5335 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5336 if (pwent) pwent = getpwnam(pwent->pw_name);
5339 DIE(aTHX_ PL_no_func, "getpwent");
5345 if (GIMME != G_ARRAY) {
5346 PUSHs(sv = sv_newmortal());
5348 if (which == OP_GPWNAM)
5349 # if Uid_t_sign <= 0
5350 sv_setiv(sv, (IV)pwent->pw_uid);
5352 sv_setuv(sv, (UV)pwent->pw_uid);
5355 sv_setpv(sv, pwent->pw_name);
5361 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5362 sv_setpv(sv, pwent->pw_name);
5364 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5366 /* If we have getspnam(), we try to dig up the shadow
5367 * password. If we are underprivileged, the shadow
5368 * interface will set the errno to EACCES or similar,
5369 * and return a null pointer. If this happens, we will
5370 * use the dummy password (usually "*" or "x") from the
5371 * standard password database.
5373 * In theory we could skip the shadow call completely
5374 * if euid != 0 but in practice we cannot know which
5375 * security measures are guarding the shadow databases
5376 * on a random platform.
5378 * Resist the urge to use additional shadow interfaces.
5379 * Divert the urge to writing an extension instead.
5382 /* Some AIX setups falsely(?) detect some getspnam(), which
5383 * has a different API than the Solaris/IRIX one. */
5384 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5386 struct spwd *spwent;
5387 int saverrno; /* Save and restore errno so that
5388 * underprivileged attempts seem
5389 * to have never made the unsccessful
5390 * attempt to retrieve the shadow password. */
5393 spwent = getspnam(pwent->pw_name);
5395 if (spwent && spwent->sp_pwdp)
5396 sv_setpv(sv, spwent->sp_pwdp);
5400 if (!SvPOK(sv)) /* Use the standard password, then. */
5401 sv_setpv(sv, pwent->pw_passwd);
5404 # ifndef INCOMPLETE_TAINTS
5405 /* passwd is tainted because user himself can diddle with it.
5406 * admittedly not much and in a very limited way, but nevertheless. */
5410 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5411 # if Uid_t_sign <= 0
5412 sv_setiv(sv, (IV)pwent->pw_uid);
5414 sv_setuv(sv, (UV)pwent->pw_uid);
5417 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5418 # if Uid_t_sign <= 0
5419 sv_setiv(sv, (IV)pwent->pw_gid);
5421 sv_setuv(sv, (UV)pwent->pw_gid);
5423 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5424 * because of the poor interface of the Perl getpw*(),
5425 * not because there's some standard/convention saying so.
5426 * A better interface would have been to return a hash,
5427 * but we are accursed by our history, alas. --jhi. */
5428 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5430 sv_setiv(sv, (IV)pwent->pw_change);
5433 sv_setiv(sv, (IV)pwent->pw_quota);
5436 sv_setpv(sv, pwent->pw_age);
5441 /* pw_class and pw_comment are mutually exclusive--.
5442 * see the above note for pw_change, pw_quota, and pw_age. */
5443 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5445 sv_setpv(sv, pwent->pw_class);
5448 sv_setpv(sv, pwent->pw_comment);
5452 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5454 sv_setpv(sv, pwent->pw_gecos);
5456 # ifndef INCOMPLETE_TAINTS
5457 /* pw_gecos is tainted because user himself can diddle with it. */
5461 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5462 sv_setpv(sv, pwent->pw_dir);
5464 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5465 sv_setpv(sv, pwent->pw_shell);
5466 # ifndef INCOMPLETE_TAINTS
5467 /* pw_shell is tainted because user himself can diddle with it. */
5472 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5473 sv_setiv(sv, (IV)pwent->pw_expire);
5478 DIE(aTHX_ PL_no_func, "getpwent");
5484 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5489 DIE(aTHX_ PL_no_func, "setpwent");
5495 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5500 DIE(aTHX_ PL_no_func, "endpwent");
5509 DIE(aTHX_ PL_no_func, "getgrnam");
5518 DIE(aTHX_ PL_no_func, "getgrgid");
5526 I32 which = PL_op->op_type;
5527 register char **elem;
5529 struct group *grent;
5531 if (which == OP_GGRNAM) {
5532 char* name = POPpbytex;
5533 grent = (struct group *)getgrnam(name);
5535 else if (which == OP_GGRGID) {
5537 grent = (struct group *)getgrgid(gid);
5541 grent = (struct group *)getgrent();
5543 DIE(aTHX_ PL_no_func, "getgrent");
5547 if (GIMME != G_ARRAY) {
5548 PUSHs(sv = sv_newmortal());
5550 if (which == OP_GGRNAM)
5551 sv_setiv(sv, (IV)grent->gr_gid);
5553 sv_setpv(sv, grent->gr_name);
5559 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5560 sv_setpv(sv, grent->gr_name);
5562 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5564 sv_setpv(sv, grent->gr_passwd);
5567 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5568 sv_setiv(sv, (IV)grent->gr_gid);
5570 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5571 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5572 /* In UNICOS/mk (_CRAYMPP) the multithreading
5573 * versions (getgrnam_r, getgrgid_r)
5574 * seem to return an illegal pointer
5575 * as the group members list, gr_mem.
5576 * getgrent() doesn't even have a _r version
5577 * but the gr_mem is poisonous anyway.
5578 * So yes, you cannot get the list of group
5579 * members if building multithreaded in UNICOS/mk. */
5580 for (elem = grent->gr_mem; elem && *elem; elem++) {
5581 sv_catpv(sv, *elem);
5583 sv_catpvn(sv, " ", 1);
5590 DIE(aTHX_ PL_no_func, "getgrent");
5596 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5601 DIE(aTHX_ PL_no_func, "setgrent");
5607 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5612 DIE(aTHX_ PL_no_func, "endgrent");
5622 if (!(tmps = PerlProc_getlogin()))
5624 PUSHp(tmps, strlen(tmps));
5627 DIE(aTHX_ PL_no_func, "getlogin");
5631 /* Miscellaneous. */
5636 dSP; dMARK; dORIGMARK; dTARGET;
5637 register I32 items = SP - MARK;
5638 unsigned long a[20];
5644 while (++MARK <= SP) {
5645 if (SvTAINTED(*MARK)) {
5651 TAINT_PROPER("syscall");
5654 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5655 * or where sizeof(long) != sizeof(char*). But such machines will
5656 * not likely have syscall implemented either, so who cares?
5658 while (++MARK <= SP) {
5659 if (SvNIOK(*MARK) || !i)
5660 a[i++] = SvIV(*MARK);
5661 else if (*MARK == &PL_sv_undef)
5664 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5670 DIE(aTHX_ "Too many args to syscall");
5672 DIE(aTHX_ "Too few args to syscall");
5674 retval = syscall(a[0]);
5677 retval = syscall(a[0],a[1]);
5680 retval = syscall(a[0],a[1],a[2]);
5683 retval = syscall(a[0],a[1],a[2],a[3]);
5686 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5689 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5692 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5695 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5699 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5702 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5705 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5709 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5713 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5717 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5718 a[10],a[11],a[12],a[13]);
5720 #endif /* atarist */
5726 DIE(aTHX_ PL_no_func, "syscall");
5730 #ifdef FCNTL_EMULATE_FLOCK
5732 /* XXX Emulate flock() with fcntl().
5733 What's really needed is a good file locking module.
5737 fcntl_emulate_flock(int fd, int operation)
5741 switch (operation & ~LOCK_NB) {
5743 flock.l_type = F_RDLCK;
5746 flock.l_type = F_WRLCK;
5749 flock.l_type = F_UNLCK;
5755 flock.l_whence = SEEK_SET;
5756 flock.l_start = flock.l_len = (Off_t)0;
5758 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5761 #endif /* FCNTL_EMULATE_FLOCK */
5763 #ifdef LOCKF_EMULATE_FLOCK
5765 /* XXX Emulate flock() with lockf(). This is just to increase
5766 portability of scripts. The calls are not completely
5767 interchangeable. What's really needed is a good file
5771 /* The lockf() constants might have been defined in <unistd.h>.
5772 Unfortunately, <unistd.h> causes troubles on some mixed
5773 (BSD/POSIX) systems, such as SunOS 4.1.3.
5775 Further, the lockf() constants aren't POSIX, so they might not be
5776 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5777 just stick in the SVID values and be done with it. Sigh.
5781 # define F_ULOCK 0 /* Unlock a previously locked region */
5784 # define F_LOCK 1 /* Lock a region for exclusive use */
5787 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5790 # define F_TEST 3 /* Test a region for other processes locks */
5794 lockf_emulate_flock(int fd, int operation)
5800 /* flock locks entire file so for lockf we need to do the same */
5802 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5803 if (pos > 0) /* is seekable and needs to be repositioned */
5804 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5805 pos = -1; /* seek failed, so don't seek back afterwards */
5808 switch (operation) {
5810 /* LOCK_SH - get a shared lock */
5812 /* LOCK_EX - get an exclusive lock */
5814 i = lockf (fd, F_LOCK, 0);
5817 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5818 case LOCK_SH|LOCK_NB:
5819 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5820 case LOCK_EX|LOCK_NB:
5821 i = lockf (fd, F_TLOCK, 0);
5823 if ((errno == EAGAIN) || (errno == EACCES))
5824 errno = EWOULDBLOCK;
5827 /* LOCK_UN - unlock (non-blocking is a no-op) */
5829 case LOCK_UN|LOCK_NB:
5830 i = lockf (fd, F_ULOCK, 0);
5833 /* Default - can't decipher operation */
5840 if (pos > 0) /* need to restore position of the handle */
5841 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5846 #endif /* LOCKF_EMULATE_FLOCK */
5850 * c-indentation-style: bsd
5852 * indent-tabs-mode: t
5855 * ex: set ts=8 sts=4 sw=4 noet: