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;
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 New(403, 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 PUSHs(&PL_sv_undef);
1149 if (GIMME == G_ARRAY && tbuf) {
1150 value = (NV)(timebuf.tv_sec) +
1151 (NV)(timebuf.tv_usec) / 1000000.0;
1152 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1153 sv_setnv(sv, value);
1157 DIE(aTHX_ "select not implemented");
1162 Perl_setdefout(pTHX_ GV *gv)
1165 (void)SvREFCNT_inc(gv);
1167 SvREFCNT_dec(PL_defoutgv);
1177 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1179 egv = GvEGV(PL_defoutgv);
1184 XPUSHs(&PL_sv_undef);
1186 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1187 if (gvp && *gvp == egv) {
1188 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1192 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1197 if (!GvIO(newdefout))
1198 gv_IOadd(newdefout);
1199 setdefout(newdefout);
1217 if (gv && (io = GvIO(gv))
1218 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1220 const I32 gimme = GIMME_V;
1222 XPUSHs(SvTIED_obj((SV*)io, mg));
1225 call_method("GETC", gimme);
1228 if (gimme == G_SCALAR)
1229 SvSetMagicSV_nosteal(TARG, TOPs);
1232 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1233 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1234 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1235 report_evil_fh(gv, io, PL_op->op_type);
1236 SETERRNO(EBADF,RMS_IFI);
1240 sv_setpvn(TARG, " ", 1);
1241 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1242 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1243 /* Find out how many bytes the char needs */
1244 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1247 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1248 SvCUR_set(TARG,1+len);
1258 return pp_sysread();
1262 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1265 register PERL_CONTEXT *cx;
1266 const I32 gimme = GIMME_V;
1271 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1273 cx->blk_sub.retop = retop;
1274 PAD_SET_CUR(CvPADLIST(cv), 1);
1276 setdefout(gv); /* locally select filehandle so $% et al work */
1308 SV * const tmpsv = sv_newmortal();
1310 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1311 name = SvPV_nolen_const(tmpsv);
1313 DIE(aTHX_ "Undefined format \"%s\" called", name);
1315 DIE(aTHX_ "Not a format reference");
1318 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1320 IoFLAGS(io) &= ~IOf_DIDTOP;
1321 return doform(cv,gv,PL_op->op_next);
1327 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1328 register IO *io = GvIOp(gv);
1329 PerlIO *ofp = IoOFP(io);
1333 register PERL_CONTEXT *cx;
1335 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1336 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1339 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1340 PL_formtarget != PL_toptarget)
1344 if (!IoTOP_GV(io)) {
1348 if (!IoTOP_NAME(io)) {
1349 if (!IoFMT_NAME(io))
1350 IoFMT_NAME(io) = savepv(GvNAME(gv));
1351 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1352 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1353 if ((topgv && GvFORM(topgv)) ||
1354 !gv_fetchpv("top",FALSE,SVt_PVFM))
1355 IoTOP_NAME(io) = savesvpv(topname);
1357 IoTOP_NAME(io) = savepvn("top", 3);
1359 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1360 if (!topgv || !GvFORM(topgv)) {
1361 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1364 IoTOP_GV(io) = topgv;
1366 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1367 I32 lines = IoLINES_LEFT(io);
1368 const char *s = SvPVX_const(PL_formtarget);
1369 if (lines <= 0) /* Yow, header didn't even fit!!! */
1371 while (lines-- > 0) {
1372 s = strchr(s, '\n');
1378 const STRLEN save = SvCUR(PL_formtarget);
1379 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1380 do_print(PL_formtarget, ofp);
1381 SvCUR_set(PL_formtarget, save);
1382 sv_chop(PL_formtarget, s);
1383 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1386 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1387 do_print(PL_formfeed, ofp);
1388 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1390 PL_formtarget = PL_toptarget;
1391 IoFLAGS(io) |= IOf_DIDTOP;
1394 DIE(aTHX_ "bad top format reference");
1397 SV * const sv = sv_newmortal();
1399 gv_efullname4(sv, fgv, Nullch, FALSE);
1400 name = SvPV_nolen_const(sv);
1402 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1406 DIE(aTHX_ "Undefined top format called");
1409 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1410 return doform(cv,gv,PL_op);
1414 POPBLOCK(cx,PL_curpm);
1420 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1422 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1423 else if (ckWARN(WARN_CLOSED))
1424 report_evil_fh(gv, io, PL_op->op_type);
1429 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1430 if (ckWARN(WARN_IO))
1431 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1433 if (!do_print(PL_formtarget, fp))
1436 FmLINES(PL_formtarget) = 0;
1437 SvCUR_set(PL_formtarget, 0);
1438 *SvEND(PL_formtarget) = '\0';
1439 if (IoFLAGS(io) & IOf_FLUSH)
1440 (void)PerlIO_flush(fp);
1445 PL_formtarget = PL_bodytarget;
1447 return cx->blk_sub.retop;
1452 dVAR; dSP; dMARK; dORIGMARK;
1459 if (PL_op->op_flags & OPf_STACKED)
1464 if (gv && (io = GvIO(gv))
1465 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1467 if (MARK == ORIGMARK) {
1470 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1474 *MARK = SvTIED_obj((SV*)io, mg);
1477 call_method("PRINTF", G_SCALAR);
1480 MARK = ORIGMARK + 1;
1487 if (!(io = GvIO(gv))) {
1488 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1489 report_evil_fh(gv, io, PL_op->op_type);
1490 SETERRNO(EBADF,RMS_IFI);
1493 else if (!(fp = IoOFP(io))) {
1494 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1496 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1497 else if (ckWARN(WARN_CLOSED))
1498 report_evil_fh(gv, io, PL_op->op_type);
1500 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1504 do_sprintf(sv, SP - MARK, MARK + 1);
1505 if (!do_print(sv, fp))
1508 if (IoFLAGS(io) & IOf_FLUSH)
1509 if (PerlIO_flush(fp) == EOF)
1520 PUSHs(&PL_sv_undef);
1531 const int perm = (MAXARG > 3) ? POPi : 0666;
1532 const int mode = POPi;
1537 /* Need TIEHANDLE method ? */
1539 tmps = SvPV_const(sv, len);
1540 /* FIXME? do_open should do const */
1541 if (do_open(gv, (char*)tmps, len, TRUE, mode, perm, Nullfp)) {
1542 IoLINES(GvIOp(gv)) = 0;
1546 PUSHs(&PL_sv_undef);
1553 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1560 Sock_size_t bufsize;
1569 bool charstart = FALSE;
1570 STRLEN charskip = 0;
1574 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1575 && gv && (io = GvIO(gv))
1576 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1581 *MARK = SvTIED_obj((SV*)io, mg);
1583 call_method("READ", G_SCALAR);
1596 sv_setpvn(bufsv, "", 0);
1597 length = SvIVx(*++MARK);
1600 offset = SvIVx(*++MARK);
1604 if (!io || !IoIFP(io)) {
1605 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1606 report_evil_fh(gv, io, PL_op->op_type);
1607 SETERRNO(EBADF,RMS_IFI);
1610 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1611 buffer = SvPVutf8_force(bufsv, blen);
1612 /* UTF-8 may not have been set if they are all low bytes */
1617 buffer = SvPV_force(bufsv, blen);
1618 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1621 DIE(aTHX_ "Negative length");
1629 if (PL_op->op_type == OP_RECV) {
1630 char namebuf[MAXPATHLEN];
1631 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1632 bufsize = sizeof (struct sockaddr_in);
1634 bufsize = sizeof namebuf;
1636 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1640 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1641 /* 'offset' means 'flags' here */
1642 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1643 (struct sockaddr *)namebuf, &bufsize);
1647 /* Bogus return without padding */
1648 bufsize = sizeof (struct sockaddr_in);
1650 SvCUR_set(bufsv, count);
1651 *SvEND(bufsv) = '\0';
1652 (void)SvPOK_only(bufsv);
1656 /* This should not be marked tainted if the fp is marked clean */
1657 if (!(IoFLAGS(io) & IOf_UNTAINT))
1658 SvTAINTED_on(bufsv);
1660 sv_setpvn(TARG, namebuf, bufsize);
1665 if (PL_op->op_type == OP_RECV)
1666 DIE(aTHX_ PL_no_sock_func, "recv");
1668 if (DO_UTF8(bufsv)) {
1669 /* offset adjust in characters not bytes */
1670 blen = sv_len_utf8(bufsv);
1673 if (-offset > (int)blen)
1674 DIE(aTHX_ "Offset outside string");
1677 if (DO_UTF8(bufsv)) {
1678 /* convert offset-as-chars to offset-as-bytes */
1679 if (offset >= (int)blen)
1680 offset += SvCUR(bufsv) - blen;
1682 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1685 bufsize = SvCUR(bufsv);
1686 /* Allocating length + offset + 1 isn't perfect in the case of reading
1687 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1689 (should be 2 * length + offset + 1, or possibly something longer if
1690 PL_encoding is true) */
1691 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1692 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1693 Zero(buffer+bufsize, offset-bufsize, char);
1695 buffer = buffer + offset;
1697 read_target = bufsv;
1699 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1700 concatenate it to the current buffer. */
1702 /* Truncate the existing buffer to the start of where we will be
1704 SvCUR_set(bufsv, offset);
1706 read_target = sv_newmortal();
1707 SvUPGRADE(read_target, SVt_PV);
1708 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1711 if (PL_op->op_type == OP_SYSREAD) {
1712 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1713 if (IoTYPE(io) == IoTYPE_SOCKET) {
1714 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1720 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1725 #ifdef HAS_SOCKET__bad_code_maybe
1726 if (IoTYPE(io) == IoTYPE_SOCKET) {
1727 char namebuf[MAXPATHLEN];
1728 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1729 bufsize = sizeof (struct sockaddr_in);
1731 bufsize = sizeof namebuf;
1733 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1734 (struct sockaddr *)namebuf, &bufsize);
1739 count = PerlIO_read(IoIFP(io), buffer, length);
1740 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1741 if (count == 0 && PerlIO_error(IoIFP(io)))
1745 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1746 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1749 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1750 *SvEND(read_target) = '\0';
1751 (void)SvPOK_only(read_target);
1752 if (fp_utf8 && !IN_BYTES) {
1753 /* Look at utf8 we got back and count the characters */
1754 const char *bend = buffer + count;
1755 while (buffer < bend) {
1757 skip = UTF8SKIP(buffer);
1760 if (buffer - charskip + skip > bend) {
1761 /* partial character - try for rest of it */
1762 length = skip - (bend-buffer);
1763 offset = bend - SvPVX_const(bufsv);
1775 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1776 provided amount read (count) was what was requested (length)
1778 if (got < wanted && count == length) {
1779 length = wanted - got;
1780 offset = bend - SvPVX_const(bufsv);
1783 /* return value is character count */
1787 else if (buffer_utf8) {
1788 /* Let svcatsv upgrade the bytes we read in to utf8.
1789 The buffer is a mortal so will be freed soon. */
1790 sv_catsv_nomg(bufsv, read_target);
1793 /* This should not be marked tainted if the fp is marked clean */
1794 if (!(IoFLAGS(io) & IOf_UNTAINT))
1795 SvTAINTED_on(bufsv);
1808 const int items = (SP - PL_stack_base) - TOPMARK;
1812 sv = sv_2mortal(newSViv(sv_len(*SP)));
1821 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1832 if (PL_op->op_type == OP_SYSWRITE
1833 && gv && (io = GvIO(gv))
1834 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1839 *MARK = SvTIED_obj((SV*)io, mg);
1841 call_method("WRITE", G_SCALAR);
1852 #if Size_t_size > IVSIZE
1853 length = (Size_t)SvNVx(*++MARK);
1855 length = (Size_t)SvIVx(*++MARK);
1857 if ((SSize_t)length < 0)
1858 DIE(aTHX_ "Negative length");
1861 if (!io || !IoIFP(io)) {
1863 if (ckWARN(WARN_CLOSED))
1864 report_evil_fh(gv, io, PL_op->op_type);
1865 SETERRNO(EBADF,RMS_IFI);
1869 if (PerlIO_isutf8(IoIFP(io))) {
1870 if (!SvUTF8(bufsv)) {
1871 bufsv = sv_2mortal(newSVsv(bufsv));
1872 buffer = sv_2pvutf8(bufsv, &blen);
1874 buffer = SvPV_const(bufsv, blen);
1877 if (DO_UTF8(bufsv)) {
1878 /* Not modifying source SV, so making a temporary copy. */
1879 bufsv = sv_2mortal(newSVsv(bufsv));
1880 sv_utf8_downgrade(bufsv, FALSE);
1882 buffer = SvPV_const(bufsv, blen);
1885 if (PL_op->op_type == OP_SYSWRITE) {
1887 if (DO_UTF8(bufsv)) {
1888 /* length and offset are in chars */
1889 blen = sv_len_utf8(bufsv);
1892 offset = SvIVx(*++MARK);
1894 if (-offset > (IV)blen)
1895 DIE(aTHX_ "Offset outside string");
1897 } else if (offset >= (IV)blen && blen > 0)
1898 DIE(aTHX_ "Offset outside string");
1901 if (length > blen - offset)
1902 length = blen - offset;
1903 if (DO_UTF8(bufsv)) {
1904 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1905 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1908 buffer = buffer+offset;
1910 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1911 if (IoTYPE(io) == IoTYPE_SOCKET) {
1912 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1918 /* See the note at doio.c:do_print about filesize limits. --jhi */
1919 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1924 else if (SP > MARK) {
1926 char * const sockbuf = SvPVx(*++MARK, mlen);
1927 /* length is really flags */
1928 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1929 length, (struct sockaddr *)sockbuf, mlen);
1932 /* length is really flags */
1933 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1936 DIE(aTHX_ PL_no_sock_func, "send");
1942 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1943 #if Size_t_size > IVSIZE
1957 return pp_sysread();
1968 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1970 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1972 if (io && !IoIFP(io)) {
1973 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1975 IoFLAGS(io) &= ~IOf_START;
1976 do_open(gv, (char *)"-", 1, FALSE, O_RDONLY, 0, Nullfp);
1977 sv_setpvn(GvSV(gv), "-", 1);
1978 SvSETMAGIC(GvSV(gv));
1980 else if (!nextargv(gv))
1985 gv = PL_last_in_gv; /* eof */
1988 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1990 if (gv && (io = GvIO(gv))
1991 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1994 XPUSHs(SvTIED_obj((SV*)io, mg));
1997 call_method("EOF", G_SCALAR);
2003 PUSHs(boolSV(!gv || do_eof(gv)));
2017 gv = PL_last_in_gv = (GV*)POPs;
2019 if (gv && (io = GvIO(gv))
2020 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2023 XPUSHs(SvTIED_obj((SV*)io, mg));
2026 call_method("TELL", G_SCALAR);
2032 #if LSEEKSIZE > IVSIZE
2033 PUSHn( do_tell(gv) );
2035 PUSHi( do_tell(gv) );
2042 return pp_sysseek();
2050 const int whence = POPi;
2051 #if LSEEKSIZE > IVSIZE
2052 Off_t offset = (Off_t)SvNVx(POPs);
2054 Off_t offset = (Off_t)SvIVx(POPs);
2058 gv = PL_last_in_gv = (GV*)POPs;
2060 if (gv && (io = GvIO(gv))
2061 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2064 XPUSHs(SvTIED_obj((SV*)io, mg));
2065 #if LSEEKSIZE > IVSIZE
2066 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2068 XPUSHs(sv_2mortal(newSViv(offset)));
2070 XPUSHs(sv_2mortal(newSViv(whence)));
2073 call_method("SEEK", G_SCALAR);
2079 if (PL_op->op_type == OP_SEEK)
2080 PUSHs(boolSV(do_seek(gv, offset, whence)));
2082 Off_t sought = do_sysseek(gv, offset, whence);
2084 PUSHs(&PL_sv_undef);
2087 #if LSEEKSIZE > IVSIZE
2092 : newSVpvn(zero_but_true, ZBTLEN);
2093 PUSHs(sv_2mortal(sv));
2102 /* There seems to be no consensus on the length type of truncate()
2103 * and ftruncate(), both off_t and size_t have supporters. In
2104 * general one would think that when using large files, off_t is
2105 * at least as wide as size_t, so using an off_t should be okay. */
2106 /* XXX Configure probe for the length type of *truncate() needed XXX */
2109 #if Off_t_size > IVSIZE
2114 /* Checking for length < 0 is problematic as the type might or
2115 * might not be signed: if it is not, clever compilers will moan. */
2116 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2123 if (PL_op->op_flags & OPf_SPECIAL) {
2124 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2133 TAINT_PROPER("truncate");
2134 if (!(fp = IoIFP(io))) {
2140 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2142 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2152 if (SvTYPE(sv) == SVt_PVGV) {
2153 tmpgv = (GV*)sv; /* *main::FRED for example */
2154 goto do_ftruncate_gv;
2156 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2157 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2158 goto do_ftruncate_gv;
2160 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2161 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2162 goto do_ftruncate_io;
2165 name = SvPV_nolen_const(sv);
2166 TAINT_PROPER("truncate");
2168 if (truncate(name, len) < 0)
2174 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2177 if (my_chsize(tmpfd, len) < 0)
2179 PerlLIO_close(tmpfd);
2188 SETERRNO(EBADF,RMS_IFI);
2202 const unsigned int func = POPu;
2203 const int optype = PL_op->op_type;
2207 IO *io = gv ? GvIOn(gv) : 0;
2209 if (!io || !argsv || !IoIFP(io)) {
2210 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2211 report_evil_fh(gv, io, PL_op->op_type);
2212 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2216 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2219 s = SvPV_force(argsv, len);
2220 need = IOCPARM_LEN(func);
2222 s = Sv_Grow(argsv, need + 1);
2223 SvCUR_set(argsv, need);
2226 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2229 retval = SvIV(argsv);
2230 s = INT2PTR(char*,retval); /* ouch */
2233 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2235 if (optype == OP_IOCTL)
2237 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2239 DIE(aTHX_ "ioctl is not implemented");
2243 DIE(aTHX_ "fcntl is not implemented");
2245 #if defined(OS2) && defined(__EMX__)
2246 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2248 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2252 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2254 if (s[SvCUR(argsv)] != 17)
2255 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2257 s[SvCUR(argsv)] = 0; /* put our null back */
2258 SvSETMAGIC(argsv); /* Assume it has changed */
2267 PUSHp(zero_but_true, ZBTLEN);
2288 if (gv && (io = GvIO(gv)))
2295 (void)PerlIO_flush(fp);
2296 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2299 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2300 report_evil_fh(gv, io, PL_op->op_type);
2302 SETERRNO(EBADF,RMS_IFI);
2307 DIE(aTHX_ PL_no_func, "flock()");
2319 int protocol = POPi;
2325 io = gv ? GvIOn(gv) : NULL;
2328 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2329 report_evil_fh(gv, io, PL_op->op_type);
2331 do_close(gv, FALSE);
2332 SETERRNO(EBADF,LIB_INVARG);
2337 do_close(gv, FALSE);
2339 TAINT_PROPER("socket");
2340 fd = PerlSock_socket(domain, type, protocol);
2343 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2344 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2345 IoTYPE(io) = IoTYPE_SOCKET;
2346 if (!IoIFP(io) || !IoOFP(io)) {
2347 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2348 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2349 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2352 #if defined(HAS_FCNTL) && defined(F_SETFD)
2353 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2357 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2362 DIE(aTHX_ PL_no_sock_func, "socket");
2368 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2374 int protocol = POPi;
2381 io1 = gv1 ? GvIOn(gv1) : NULL;
2382 io2 = gv2 ? GvIOn(gv2) : NULL;
2383 if (!gv1 || !gv2 || !io1 || !io2) {
2384 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2386 report_evil_fh(gv1, io1, PL_op->op_type);
2388 report_evil_fh(gv1, io2, PL_op->op_type);
2391 do_close(gv1, FALSE);
2393 do_close(gv2, FALSE);
2398 do_close(gv1, FALSE);
2400 do_close(gv2, FALSE);
2402 TAINT_PROPER("socketpair");
2403 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2405 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2406 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2407 IoTYPE(io1) = IoTYPE_SOCKET;
2408 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2409 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2410 IoTYPE(io2) = IoTYPE_SOCKET;
2411 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2412 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2413 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2414 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2415 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2416 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2417 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2420 #if defined(HAS_FCNTL) && defined(F_SETFD)
2421 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2422 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2427 DIE(aTHX_ PL_no_sock_func, "socketpair");
2435 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2436 extern void GETPRIVMODE();
2437 extern void GETUSERMODE();
2440 /* OK, so on what platform does bind modify addr? */
2443 register IO *io = GvIOn(gv);
2447 int mpeprivmode = 0;
2450 if (!io || !IoIFP(io))
2453 addr = SvPV_const(addrsv, len);
2454 TAINT_PROPER("bind");
2455 #ifdef MPE /* Deal with MPE bind() peculiarities */
2456 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2457 /* The address *MUST* stupidly be zero. */
2458 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2459 /* PRIV mode is required to bind() to ports < 1024. */
2460 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2461 ((struct sockaddr_in *)addr)->sin_port > 0) {
2462 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2467 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2468 (struct sockaddr *)addr, len) >= 0)
2471 #ifdef MPE /* Switch back to USER mode */
2482 if (ckWARN(WARN_CLOSED))
2483 report_evil_fh(gv, io, PL_op->op_type);
2484 SETERRNO(EBADF,SS_IVCHAN);
2487 DIE(aTHX_ PL_no_sock_func, "bind");
2498 register IO *io = GvIOn(gv);
2501 if (!io || !IoIFP(io))
2504 addr = SvPV_const(addrsv, len);
2505 TAINT_PROPER("connect");
2506 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2512 if (ckWARN(WARN_CLOSED))
2513 report_evil_fh(gv, io, PL_op->op_type);
2514 SETERRNO(EBADF,SS_IVCHAN);
2517 DIE(aTHX_ PL_no_sock_func, "connect");
2527 register IO *io = gv ? GvIOn(gv) : NULL;
2529 if (!gv || !io || !IoIFP(io))
2532 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2538 if (ckWARN(WARN_CLOSED))
2539 report_evil_fh(gv, io, PL_op->op_type);
2540 SETERRNO(EBADF,SS_IVCHAN);
2543 DIE(aTHX_ PL_no_sock_func, "listen");
2555 char namebuf[MAXPATHLEN];
2556 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2557 Sock_size_t len = sizeof (struct sockaddr_in);
2559 Sock_size_t len = sizeof namebuf;
2572 if (!gstio || !IoIFP(gstio))
2576 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2580 do_close(ngv, FALSE);
2581 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2582 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2583 IoTYPE(nstio) = IoTYPE_SOCKET;
2584 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2585 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2586 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2587 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2590 #if defined(HAS_FCNTL) && defined(F_SETFD)
2591 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2595 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2596 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2598 #ifdef __SCO_VERSION__
2599 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2602 PUSHp(namebuf, len);
2606 if (ckWARN(WARN_CLOSED))
2607 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2608 SETERRNO(EBADF,SS_IVCHAN);
2614 DIE(aTHX_ PL_no_sock_func, "accept");
2624 register IO *io = GvIOn(gv);
2626 if (!io || !IoIFP(io))
2629 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2633 if (ckWARN(WARN_CLOSED))
2634 report_evil_fh(gv, io, PL_op->op_type);
2635 SETERRNO(EBADF,SS_IVCHAN);
2638 DIE(aTHX_ PL_no_sock_func, "shutdown");
2645 return pp_ssockopt();
2647 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2655 int optype = PL_op->op_type;
2658 unsigned int optname;
2664 if (optype == OP_GSOCKOPT)
2665 sv = sv_2mortal(NEWSV(22, 257));
2668 optname = (unsigned int) POPi;
2669 lvl = (unsigned int) POPi;
2673 if (!io || !IoIFP(io))
2676 fd = PerlIO_fileno(IoIFP(io));
2680 (void)SvPOK_only(sv);
2684 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2695 buf = SvPV_const(sv, l);
2699 aint = (int)SvIV(sv);
2700 buf = (const char*)&aint;
2703 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2712 if (ckWARN(WARN_CLOSED))
2713 report_evil_fh(gv, io, optype);
2714 SETERRNO(EBADF,SS_IVCHAN);
2719 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2726 return pp_getpeername();
2728 DIE(aTHX_ PL_no_sock_func, "getsockname");
2736 int optype = PL_op->op_type;
2740 register IO *io = GvIOn(gv);
2743 if (!io || !IoIFP(io))
2746 sv = sv_2mortal(NEWSV(22, 257));
2747 (void)SvPOK_only(sv);
2751 fd = PerlIO_fileno(IoIFP(io));
2753 case OP_GETSOCKNAME:
2754 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2757 case OP_GETPEERNAME:
2758 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2760 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2762 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";
2763 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2764 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2765 !memcmp((char *)SvPVX_const(sv) + sizeof(u_short), nowhere,
2766 sizeof(u_short) + sizeof(struct in_addr))) {
2773 #ifdef BOGUS_GETNAME_RETURN
2774 /* Interactive Unix, getpeername() and getsockname()
2775 does not return valid namelen */
2776 if (len == BOGUS_GETNAME_RETURN)
2777 len = sizeof(struct sockaddr);
2785 if (ckWARN(WARN_CLOSED))
2786 report_evil_fh(gv, io, optype);
2787 SETERRNO(EBADF,SS_IVCHAN);
2792 DIE(aTHX_ PL_no_sock_func, "getpeername");
2810 if (PL_op->op_flags & OPf_REF) {
2812 if (PL_op->op_type == OP_LSTAT) {
2813 if (gv != PL_defgv) {
2814 if (ckWARN(WARN_IO))
2815 Perl_warner(aTHX_ packWARN(WARN_IO),
2816 "lstat() on filehandle %s", GvENAME(gv));
2817 } else if (PL_laststype != OP_LSTAT)
2818 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2822 if (gv != PL_defgv) {
2823 PL_laststype = OP_STAT;
2825 sv_setpvn(PL_statname, "", 0);
2826 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2827 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2829 if (PL_laststatval < 0) {
2830 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2831 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2837 if (SvTYPE(sv) == SVt_PVGV) {
2841 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2843 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2844 Perl_warner(aTHX_ packWARN(WARN_IO),
2845 "lstat() on filehandle %s", GvENAME(gv));
2848 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2850 PL_laststype = PL_op->op_type;
2851 if (PL_op->op_type == OP_LSTAT)
2852 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2854 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2855 if (PL_laststatval < 0) {
2856 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2857 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2863 if (gimme != G_ARRAY) {
2864 if (gimme != G_VOID)
2865 XPUSHs(boolSV(max));
2871 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2872 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2873 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2874 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2875 #if Uid_t_size > IVSIZE
2876 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2878 # if Uid_t_sign <= 0
2879 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2881 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2884 #if Gid_t_size > IVSIZE
2885 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2887 # if Gid_t_sign <= 0
2888 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2890 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2893 #ifdef USE_STAT_RDEV
2894 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2896 PUSHs(sv_2mortal(newSVpvn("", 0)));
2898 #if Off_t_size > IVSIZE
2899 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2901 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2904 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2905 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2906 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2908 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2909 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2910 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2912 #ifdef USE_STAT_BLOCKS
2913 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2914 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2916 PUSHs(sv_2mortal(newSVpvn("", 0)));
2917 PUSHs(sv_2mortal(newSVpvn("", 0)));
2923 /* This macro is used by the stacked filetest operators :
2924 * if the previous filetest failed, short-circuit and pass its value.
2925 * Else, discard it from the stack and continue. --rgs
2927 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2928 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2929 else { (void)POPs; PUTBACK; } \
2936 STACKED_FTEST_CHECK;
2937 #if defined(HAS_ACCESS) && defined(R_OK)
2938 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2939 result = access(POPpx, R_OK);
2954 if (cando(S_IRUSR, 0, &PL_statcache))
2963 STACKED_FTEST_CHECK;
2964 #if defined(HAS_ACCESS) && defined(W_OK)
2965 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2966 result = access(POPpx, W_OK);
2981 if (cando(S_IWUSR, 0, &PL_statcache))
2990 STACKED_FTEST_CHECK;
2991 #if defined(HAS_ACCESS) && defined(X_OK)
2992 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2993 result = access(POPpx, X_OK);
3008 if (cando(S_IXUSR, 0, &PL_statcache))
3017 STACKED_FTEST_CHECK;
3018 #ifdef PERL_EFF_ACCESS_R_OK
3019 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3020 result = PERL_EFF_ACCESS_R_OK(POPpx);
3035 if (cando(S_IRUSR, 1, &PL_statcache))
3044 STACKED_FTEST_CHECK;
3045 #ifdef PERL_EFF_ACCESS_W_OK
3046 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3047 result = PERL_EFF_ACCESS_W_OK(POPpx);
3062 if (cando(S_IWUSR, 1, &PL_statcache))
3071 STACKED_FTEST_CHECK;
3072 #ifdef PERL_EFF_ACCESS_X_OK
3073 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3074 result = PERL_EFF_ACCESS_X_OK(POPpx);
3089 if (cando(S_IXUSR, 1, &PL_statcache))
3098 STACKED_FTEST_CHECK;
3108 return pp_ftrowned();
3115 STACKED_FTEST_CHECK;
3120 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3130 STACKED_FTEST_CHECK;
3135 if (PL_statcache.st_size == 0)
3144 STACKED_FTEST_CHECK;
3149 #if Off_t_size > IVSIZE
3150 PUSHn(PL_statcache.st_size);
3152 PUSHi(PL_statcache.st_size);
3161 STACKED_FTEST_CHECK;
3166 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3174 STACKED_FTEST_CHECK;
3179 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3187 STACKED_FTEST_CHECK;
3192 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3200 STACKED_FTEST_CHECK;
3205 if (S_ISSOCK(PL_statcache.st_mode))
3214 STACKED_FTEST_CHECK;
3219 if (S_ISCHR(PL_statcache.st_mode))
3228 STACKED_FTEST_CHECK;
3233 if (S_ISBLK(PL_statcache.st_mode))
3242 STACKED_FTEST_CHECK;
3247 if (S_ISREG(PL_statcache.st_mode))
3256 STACKED_FTEST_CHECK;
3261 if (S_ISDIR(PL_statcache.st_mode))
3270 STACKED_FTEST_CHECK;
3275 if (S_ISFIFO(PL_statcache.st_mode))
3282 I32 result = my_lstat();
3286 if (S_ISLNK(PL_statcache.st_mode))
3296 STACKED_FTEST_CHECK;
3301 if (PL_statcache.st_mode & S_ISUID)
3312 STACKED_FTEST_CHECK;
3317 if (PL_statcache.st_mode & S_ISGID)
3328 STACKED_FTEST_CHECK;
3333 if (PL_statcache.st_mode & S_ISVTX)
3346 STACKED_FTEST_CHECK;
3348 if (PL_op->op_flags & OPf_REF)
3350 else if (isGV(TOPs))
3352 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3353 gv = (GV*)SvRV(POPs);
3355 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3357 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3358 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3359 else if (tmpsv && SvOK(tmpsv)) {
3360 const char *tmps = SvPV_nolen_const(tmpsv);
3368 if (PerlLIO_isatty(fd))
3373 #if defined(atarist) /* this will work with atariST. Configure will
3374 make guesses for other systems. */
3375 # define FILE_base(f) ((f)->_base)
3376 # define FILE_ptr(f) ((f)->_ptr)
3377 # define FILE_cnt(f) ((f)->_cnt)
3378 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3388 register STDCHAR *s;
3394 STACKED_FTEST_CHECK;
3396 if (PL_op->op_flags & OPf_REF)
3398 else if (isGV(TOPs))
3400 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3401 gv = (GV*)SvRV(POPs);
3407 if (gv == PL_defgv) {
3409 io = GvIO(PL_statgv);
3412 goto really_filename;
3417 PL_laststatval = -1;
3418 sv_setpvn(PL_statname, "", 0);
3419 io = GvIO(PL_statgv);
3421 if (io && IoIFP(io)) {
3422 if (! PerlIO_has_base(IoIFP(io)))
3423 DIE(aTHX_ "-T and -B not implemented on filehandles");
3424 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3425 if (PL_laststatval < 0)
3427 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3428 if (PL_op->op_type == OP_FTTEXT)
3433 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3434 i = PerlIO_getc(IoIFP(io));
3436 (void)PerlIO_ungetc(IoIFP(io),i);
3438 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3440 len = PerlIO_get_bufsiz(IoIFP(io));
3441 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3442 /* sfio can have large buffers - limit to 512 */
3447 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3449 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3451 SETERRNO(EBADF,RMS_IFI);
3459 PL_laststype = OP_STAT;
3460 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3461 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3462 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3464 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3467 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3468 if (PL_laststatval < 0) {
3469 (void)PerlIO_close(fp);
3472 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3473 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3474 (void)PerlIO_close(fp);
3476 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3477 RETPUSHNO; /* special case NFS directories */
3478 RETPUSHYES; /* null file is anything */
3483 /* now scan s to look for textiness */
3484 /* XXX ASCII dependent code */
3486 #if defined(DOSISH) || defined(USEMYBINMODE)
3487 /* ignore trailing ^Z on short files */
3488 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3492 for (i = 0; i < len; i++, s++) {
3493 if (!*s) { /* null never allowed in text */
3498 else if (!(isPRINT(*s) || isSPACE(*s)))
3501 else if (*s & 128) {
3503 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3506 /* utf8 characters don't count as odd */
3507 if (UTF8_IS_START(*s)) {
3508 int ulen = UTF8SKIP(s);
3509 if (ulen < len - i) {
3511 for (j = 1; j < ulen; j++) {
3512 if (!UTF8_IS_CONTINUATION(s[j]))
3515 --ulen; /* loop does extra increment */
3525 *s != '\n' && *s != '\r' && *s != '\b' &&
3526 *s != '\t' && *s != '\f' && *s != 27)
3531 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3555 if( !tmps || !*tmps ) {
3556 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3557 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3559 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3564 deprecate("chdir('') or chdir(undef) as chdir()");
3565 tmps = SvPV_nolen_const(*svp);
3569 TAINT_PROPER("chdir");
3574 TAINT_PROPER("chdir");
3575 PUSHi( PerlDir_chdir(tmps) >= 0 );
3577 /* Clear the DEFAULT element of ENV so we'll get the new value
3579 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3587 dSP; dMARK; dTARGET;
3588 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3594 DIE(aTHX_ PL_no_func, "chown");
3603 TAINT_PROPER("chroot");
3604 PUSHi( chroot(tmps) >= 0 );
3607 DIE(aTHX_ PL_no_func, "chroot");
3613 dSP; dMARK; dTARGET;
3615 value = (I32)apply(PL_op->op_type, MARK, SP);
3623 dSP; dMARK; dTARGET;
3625 value = (I32)apply(PL_op->op_type, MARK, SP);
3633 dSP; dMARK; dTARGET;
3635 value = (I32)apply(PL_op->op_type, MARK, SP);
3645 const char *tmps2 = POPpconstx;
3646 const char *tmps = SvPV_nolen_const(TOPs);
3647 TAINT_PROPER("rename");
3649 anum = PerlLIO_rename(tmps, tmps2);
3651 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3652 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3655 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3656 (void)UNLINK(tmps2);
3657 if (!(anum = link(tmps, tmps2)))
3658 anum = UNLINK(tmps);
3670 const char *tmps2 = POPpconstx;
3671 const char *tmps = SvPV_nolen_const(TOPs);
3672 TAINT_PROPER("link");
3673 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3676 DIE(aTHX_ PL_no_func, "link");
3684 const char *tmps2 = POPpconstx;
3685 const char *tmps = SvPV_nolen_const(TOPs);
3686 TAINT_PROPER("symlink");
3687 SETi( symlink(tmps, tmps2) >= 0 );
3690 DIE(aTHX_ PL_no_func, "symlink");
3700 char buf[MAXPATHLEN];
3703 #ifndef INCOMPLETE_TAINTS
3707 len = readlink(tmps, buf, sizeof(buf) - 1);
3715 RETSETUNDEF; /* just pretend it's a normal file */
3719 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3721 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3723 char * const save_filename = filename;
3729 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3730 strcpy(cmdline, cmd);
3731 strcat(cmdline, " ");
3732 for (s = cmdline + strlen(cmdline); *filename; ) {
3737 myfp = PerlProc_popen(cmdline, "r");
3741 SV *tmpsv = sv_newmortal();
3742 /* Need to save/restore 'PL_rs' ?? */
3743 s = sv_gets(tmpsv, myfp, 0);
3744 (void)PerlProc_pclose(myfp);
3748 #ifdef HAS_SYS_ERRLIST
3753 /* you don't see this */
3755 #ifdef HAS_SYS_ERRLIST
3763 if (instr(s, errmsg)) {
3770 #define EACCES EPERM
3772 if (instr(s, "cannot make"))
3773 SETERRNO(EEXIST,RMS_FEX);
3774 else if (instr(s, "existing file"))
3775 SETERRNO(EEXIST,RMS_FEX);
3776 else if (instr(s, "ile exists"))
3777 SETERRNO(EEXIST,RMS_FEX);
3778 else if (instr(s, "non-exist"))
3779 SETERRNO(ENOENT,RMS_FNF);
3780 else if (instr(s, "does not exist"))
3781 SETERRNO(ENOENT,RMS_FNF);
3782 else if (instr(s, "not empty"))
3783 SETERRNO(EBUSY,SS_DEVOFFLINE);
3784 else if (instr(s, "cannot access"))
3785 SETERRNO(EACCES,RMS_PRV);
3787 SETERRNO(EPERM,RMS_PRV);
3790 else { /* some mkdirs return no failure indication */
3791 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3792 if (PL_op->op_type == OP_RMDIR)
3797 SETERRNO(EACCES,RMS_PRV); /* a guess */
3806 /* This macro removes trailing slashes from a directory name.
3807 * Different operating and file systems take differently to
3808 * trailing slashes. According to POSIX 1003.1 1996 Edition
3809 * any number of trailing slashes should be allowed.
3810 * Thusly we snip them away so that even non-conforming
3811 * systems are happy.
3812 * We should probably do this "filtering" for all
3813 * the functions that expect (potentially) directory names:
3814 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3815 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3817 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3818 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3821 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3822 (tmps) = savepvn((tmps), (len)); \
3842 TRIMSLASHES(tmps,len,copy);
3844 TAINT_PROPER("mkdir");
3846 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3848 SETi( dooneliner("mkdir", tmps) );
3849 oldumask = PerlLIO_umask(0);
3850 PerlLIO_umask(oldumask);
3851 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3865 TRIMSLASHES(tmps,len,copy);
3866 TAINT_PROPER("rmdir");
3868 SETi( PerlDir_rmdir(tmps) >= 0 );
3870 SETi( dooneliner("rmdir", tmps) );
3877 /* Directory calls. */
3881 #if defined(Direntry_t) && defined(HAS_READDIR)
3883 const char *dirname = POPpconstx;
3885 register IO *io = GvIOn(gv);
3891 PerlDir_close(IoDIRP(io));
3892 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3898 SETERRNO(EBADF,RMS_DIR);
3901 DIE(aTHX_ PL_no_dir_func, "opendir");
3907 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3908 DIE(aTHX_ PL_no_dir_func, "readdir");
3910 #if !defined(I_DIRENT) && !defined(VMS)
3911 Direntry_t *readdir (DIR *);
3916 const I32 gimme = GIMME;
3917 GV *gv = (GV *)POPs;
3918 register Direntry_t *dp;
3919 register IO *io = GvIOn(gv);
3921 if (!io || !IoDIRP(io))
3925 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3929 sv = newSVpvn(dp->d_name, dp->d_namlen);
3931 sv = newSVpv(dp->d_name, 0);
3933 #ifndef INCOMPLETE_TAINTS
3934 if (!(IoFLAGS(io) & IOf_UNTAINT))
3937 XPUSHs(sv_2mortal(sv));
3939 while (gimme == G_ARRAY);
3941 if (!dp && gimme != G_ARRAY)
3948 SETERRNO(EBADF,RMS_ISI);
3949 if (GIMME == G_ARRAY)
3958 #if defined(HAS_TELLDIR) || defined(telldir)
3960 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3961 /* XXX netbsd still seemed to.
3962 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3963 --JHI 1999-Feb-02 */
3964 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3965 long telldir (DIR *);
3968 register IO *io = GvIOn(gv);
3970 if (!io || !IoDIRP(io))
3973 PUSHi( PerlDir_tell(IoDIRP(io)) );
3977 SETERRNO(EBADF,RMS_ISI);
3980 DIE(aTHX_ PL_no_dir_func, "telldir");
3986 #if defined(HAS_SEEKDIR) || defined(seekdir)
3990 register IO *io = GvIOn(gv);
3992 if (!io || !IoDIRP(io))
3995 (void)PerlDir_seek(IoDIRP(io), along);
4000 SETERRNO(EBADF,RMS_ISI);
4003 DIE(aTHX_ PL_no_dir_func, "seekdir");
4009 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4012 register IO *io = GvIOn(gv);
4014 if (!io || !IoDIRP(io))
4017 (void)PerlDir_rewind(IoDIRP(io));
4021 SETERRNO(EBADF,RMS_ISI);
4024 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4030 #if defined(Direntry_t) && defined(HAS_READDIR)
4033 register IO *io = GvIOn(gv);
4035 if (!io || !IoDIRP(io))
4038 #ifdef VOID_CLOSEDIR
4039 PerlDir_close(IoDIRP(io));
4041 if (PerlDir_close(IoDIRP(io)) < 0) {
4042 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4051 SETERRNO(EBADF,RMS_IFI);
4054 DIE(aTHX_ PL_no_dir_func, "closedir");
4058 /* Process control. */
4068 PERL_FLUSHALL_FOR_CHILD;
4069 childpid = PerlProc_fork();
4074 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4075 SvREADONLY_off(GvSV(tmpgv));
4076 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4077 SvREADONLY_on(GvSV(tmpgv));
4079 #ifdef THREADS_HAVE_PIDS
4080 PL_ppid = (IV)getppid();
4082 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4087 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4092 PERL_FLUSHALL_FOR_CHILD;
4093 childpid = PerlProc_fork();
4099 DIE(aTHX_ PL_no_func, "fork");
4106 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4111 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4112 childpid = wait4pid(-1, &argflags, 0);
4114 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4119 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4120 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4121 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4123 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4128 DIE(aTHX_ PL_no_func, "wait");
4134 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4143 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4144 result = wait4pid(pid, &argflags, optype);
4146 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4151 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4152 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4153 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4155 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4160 DIE(aTHX_ PL_no_func, "waitpid");
4166 dSP; dMARK; dORIGMARK; dTARGET;
4172 while (++MARK <= SP) {
4173 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4178 TAINT_PROPER("system");
4180 PERL_FLUSHALL_FOR_CHILD;
4181 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4187 if (PerlProc_pipe(pp) >= 0)
4189 while ((childpid = PerlProc_fork()) == -1) {
4190 if (errno != EAGAIN) {
4195 PerlLIO_close(pp[0]);
4196 PerlLIO_close(pp[1]);
4203 Sigsave_t ihand,qhand; /* place to save signals during system() */
4207 PerlLIO_close(pp[1]);
4209 rsignal_save(SIGINT, SIG_IGN, &ihand);
4210 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4213 result = wait4pid(childpid, &status, 0);
4214 } while (result == -1 && errno == EINTR);
4216 (void)rsignal_restore(SIGINT, &ihand);
4217 (void)rsignal_restore(SIGQUIT, &qhand);
4219 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4220 do_execfree(); /* free any memory child malloced on fork */
4226 while (n < sizeof(int)) {
4227 n1 = PerlLIO_read(pp[0],
4228 (void*)(((char*)&errkid)+n),
4234 PerlLIO_close(pp[0]);
4235 if (n) { /* Error */
4236 if (n != sizeof(int))
4237 DIE(aTHX_ "panic: kid popen errno read");
4238 errno = errkid; /* Propagate errno from kid */
4239 STATUS_CURRENT = -1;
4242 PUSHi(STATUS_CURRENT);
4246 PerlLIO_close(pp[0]);
4247 #if defined(HAS_FCNTL) && defined(F_SETFD)
4248 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4251 if (PL_op->op_flags & OPf_STACKED) {
4252 SV *really = *++MARK;
4253 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4255 else if (SP - MARK != 1)
4256 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4258 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4262 #else /* ! FORK or VMS or OS/2 */
4265 if (PL_op->op_flags & OPf_STACKED) {
4266 SV *really = *++MARK;
4267 # if defined(WIN32) || defined(OS2) || defined(SYMBIAN)
4268 value = (I32)do_aspawn(really, MARK, SP);
4270 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4273 else if (SP - MARK != 1) {
4274 # if defined(WIN32) || defined(OS2) || defined(SYMBIAN)
4275 value = (I32)do_aspawn(Nullsv, MARK, SP);
4277 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4281 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4283 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4285 STATUS_NATIVE_SET(value);
4288 PUSHi(result ? value : STATUS_CURRENT);
4289 #endif /* !FORK or VMS */
4295 dSP; dMARK; dORIGMARK; dTARGET;
4300 while (++MARK <= SP) {
4301 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4306 TAINT_PROPER("exec");
4308 PERL_FLUSHALL_FOR_CHILD;
4309 if (PL_op->op_flags & OPf_STACKED) {
4310 SV *really = *++MARK;
4311 value = (I32)do_aexec(really, MARK, SP);
4313 else if (SP - MARK != 1)
4315 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4319 (void ) do_aspawn(Nullsv, MARK, SP);
4323 value = (I32)do_aexec(Nullsv, MARK, SP);
4328 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4331 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4334 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4347 dSP; dMARK; dTARGET;
4349 value = (I32)apply(PL_op->op_type, MARK, SP);
4354 DIE(aTHX_ PL_no_func, "kill");
4362 # ifdef THREADS_HAVE_PIDS
4363 if (PL_ppid != 1 && getppid() == 1)
4364 /* maybe the parent process has died. Refresh ppid cache */
4368 XPUSHi( getppid() );
4372 DIE(aTHX_ PL_no_func, "getppid");
4388 pgrp = (I32)BSD_GETPGRP(pid);
4390 if (pid != 0 && pid != PerlProc_getpid())
4391 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4397 DIE(aTHX_ PL_no_func, "getpgrp()");
4416 TAINT_PROPER("setpgrp");
4418 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4420 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4421 || (pid != 0 && pid != PerlProc_getpid()))
4423 DIE(aTHX_ "setpgrp can't take arguments");
4425 SETi( setpgrp() >= 0 );
4426 #endif /* USE_BSDPGRP */
4429 DIE(aTHX_ PL_no_func, "setpgrp()");
4435 #ifdef HAS_GETPRIORITY
4439 SETi( getpriority(which, who) );
4442 DIE(aTHX_ PL_no_func, "getpriority()");
4448 #ifdef HAS_SETPRIORITY
4453 TAINT_PROPER("setpriority");
4454 SETi( setpriority(which, who, niceval) >= 0 );
4457 DIE(aTHX_ PL_no_func, "setpriority()");
4467 XPUSHn( time(Null(Time_t*)) );
4469 XPUSHi( time(Null(Time_t*)) );
4480 (void)PerlProc_times(&PL_timesbuf);
4482 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4483 /* struct tms, though same data */
4487 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4488 if (GIMME == G_ARRAY) {
4489 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4490 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4491 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4497 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4499 if (GIMME == G_ARRAY) {
4500 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4501 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4502 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4506 DIE(aTHX_ "times not implemented");
4508 #endif /* HAS_TIMES */
4520 const struct tm *tmbuf;
4521 static const char * const dayname[] =
4522 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4523 static const char * const monname[] =
4524 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4525 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4531 when = (Time_t)SvNVx(POPs);
4533 when = (Time_t)SvIVx(POPs);
4536 if (PL_op->op_type == OP_LOCALTIME)
4537 tmbuf = localtime(&when);
4539 tmbuf = gmtime(&when);
4541 if (GIMME != G_ARRAY) {
4547 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4548 dayname[tmbuf->tm_wday],
4549 monname[tmbuf->tm_mon],
4554 tmbuf->tm_year + 1900);
4555 PUSHs(sv_2mortal(tsv));
4560 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4561 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4562 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4563 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4564 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4565 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4566 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4567 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4568 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4579 anum = alarm((unsigned int)anum);
4586 DIE(aTHX_ PL_no_func, "alarm");
4597 (void)time(&lasttime);
4602 PerlProc_sleep((unsigned int)duration);
4605 XPUSHi(when - lasttime);
4609 /* Shared memory. */
4623 return pp_shmwrite();
4628 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4629 dSP; dMARK; dTARGET;
4630 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4639 /* Message passing. */
4653 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4654 dSP; dMARK; dTARGET;
4655 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4666 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4667 dSP; dMARK; dTARGET;
4668 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4681 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4682 dSP; dMARK; dTARGET;
4683 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4690 DIE(aTHX_ "System V IPC is not implemented on this machine");
4696 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4697 dSP; dMARK; dTARGET;
4698 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4706 PUSHp(zero_but_true, ZBTLEN);
4716 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4717 dSP; dMARK; dTARGET;
4718 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4727 /* Get system info. */
4731 #ifdef HAS_GETHOSTBYNAME
4732 return pp_ghostent();
4734 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4740 #ifdef HAS_GETHOSTBYADDR
4741 return pp_ghostent();
4743 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4749 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4751 I32 which = PL_op->op_type;
4752 register char **elem;
4754 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4755 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4756 struct hostent *gethostbyname(Netdb_name_t);
4757 struct hostent *gethostent(void);
4759 struct hostent *hent;
4763 if (which == OP_GHBYNAME) {
4764 #ifdef HAS_GETHOSTBYNAME
4765 char* name = POPpbytex;
4766 hent = PerlSock_gethostbyname(name);
4768 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4771 else if (which == OP_GHBYADDR) {
4772 #ifdef HAS_GETHOSTBYADDR
4773 int addrtype = POPi;
4776 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4778 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4780 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4784 #ifdef HAS_GETHOSTENT
4785 hent = PerlSock_gethostent();
4787 DIE(aTHX_ PL_no_sock_func, "gethostent");
4790 #ifdef HOST_NOT_FOUND
4792 #ifdef USE_REENTRANT_API
4793 # ifdef USE_GETHOSTENT_ERRNO
4794 h_errno = PL_reentrant_buffer->_gethostent_errno;
4797 STATUS_NATIVE_SET(h_errno);
4801 if (GIMME != G_ARRAY) {
4802 PUSHs(sv = sv_newmortal());
4804 if (which == OP_GHBYNAME) {
4806 sv_setpvn(sv, hent->h_addr, hent->h_length);
4809 sv_setpv(sv, (char*)hent->h_name);
4815 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4816 sv_setpv(sv, (char*)hent->h_name);
4817 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4818 for (elem = hent->h_aliases; elem && *elem; elem++) {
4819 sv_catpv(sv, *elem);
4821 sv_catpvn(sv, " ", 1);
4823 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4824 sv_setiv(sv, (IV)hent->h_addrtype);
4825 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4826 len = hent->h_length;
4827 sv_setiv(sv, (IV)len);
4829 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4830 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831 sv_setpvn(sv, *elem, len);
4834 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4836 sv_setpvn(sv, hent->h_addr, len);
4841 DIE(aTHX_ PL_no_sock_func, "gethostent");
4847 #ifdef HAS_GETNETBYNAME
4848 return pp_gnetent();
4850 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4856 #ifdef HAS_GETNETBYADDR
4857 return pp_gnetent();
4859 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4865 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4867 I32 which = PL_op->op_type;
4868 register char **elem;
4870 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4871 struct netent *getnetbyaddr(Netdb_net_t, int);
4872 struct netent *getnetbyname(Netdb_name_t);
4873 struct netent *getnetent(void);
4875 struct netent *nent;
4877 if (which == OP_GNBYNAME){
4878 #ifdef HAS_GETNETBYNAME
4879 char *name = POPpbytex;
4880 nent = PerlSock_getnetbyname(name);
4882 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4885 else if (which == OP_GNBYADDR) {
4886 #ifdef HAS_GETNETBYADDR
4887 int addrtype = POPi;
4888 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4889 nent = PerlSock_getnetbyaddr(addr, addrtype);
4891 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4895 #ifdef HAS_GETNETENT
4896 nent = PerlSock_getnetent();
4898 DIE(aTHX_ PL_no_sock_func, "getnetent");
4901 #ifdef HOST_NOT_FOUND
4903 #ifdef USE_REENTRANT_API
4904 # ifdef USE_GETNETENT_ERRNO
4905 h_errno = PL_reentrant_buffer->_getnetent_errno;
4908 STATUS_NATIVE_SET(h_errno);
4913 if (GIMME != G_ARRAY) {
4914 PUSHs(sv = sv_newmortal());
4916 if (which == OP_GNBYNAME)
4917 sv_setiv(sv, (IV)nent->n_net);
4919 sv_setpv(sv, nent->n_name);
4925 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4926 sv_setpv(sv, nent->n_name);
4927 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4928 for (elem = nent->n_aliases; elem && *elem; elem++) {
4929 sv_catpv(sv, *elem);
4931 sv_catpvn(sv, " ", 1);
4933 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4934 sv_setiv(sv, (IV)nent->n_addrtype);
4935 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4936 sv_setiv(sv, (IV)nent->n_net);
4941 DIE(aTHX_ PL_no_sock_func, "getnetent");
4947 #ifdef HAS_GETPROTOBYNAME
4948 return pp_gprotoent();
4950 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4956 #ifdef HAS_GETPROTOBYNUMBER
4957 return pp_gprotoent();
4959 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4965 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4967 I32 which = PL_op->op_type;
4968 register char **elem;
4970 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4971 struct protoent *getprotobyname(Netdb_name_t);
4972 struct protoent *getprotobynumber(int);
4973 struct protoent *getprotoent(void);
4975 struct protoent *pent;
4977 if (which == OP_GPBYNAME) {
4978 #ifdef HAS_GETPROTOBYNAME
4979 char* name = POPpbytex;
4980 pent = PerlSock_getprotobyname(name);
4982 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4985 else if (which == OP_GPBYNUMBER) {
4986 #ifdef HAS_GETPROTOBYNUMBER
4988 pent = PerlSock_getprotobynumber(number);
4990 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4994 #ifdef HAS_GETPROTOENT
4995 pent = PerlSock_getprotoent();
4997 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5001 if (GIMME != G_ARRAY) {
5002 PUSHs(sv = sv_newmortal());
5004 if (which == OP_GPBYNAME)
5005 sv_setiv(sv, (IV)pent->p_proto);
5007 sv_setpv(sv, pent->p_name);
5013 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5014 sv_setpv(sv, pent->p_name);
5015 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5016 for (elem = pent->p_aliases; elem && *elem; elem++) {
5017 sv_catpv(sv, *elem);
5019 sv_catpvn(sv, " ", 1);
5021 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5022 sv_setiv(sv, (IV)pent->p_proto);
5027 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5033 #ifdef HAS_GETSERVBYNAME
5034 return pp_gservent();
5036 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5042 #ifdef HAS_GETSERVBYPORT
5043 return pp_gservent();
5045 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5051 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5053 I32 which = PL_op->op_type;
5054 register char **elem;
5056 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5057 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5058 struct servent *getservbyport(int, Netdb_name_t);
5059 struct servent *getservent(void);
5061 struct servent *sent;
5063 if (which == OP_GSBYNAME) {
5064 #ifdef HAS_GETSERVBYNAME
5065 char *proto = POPpbytex;
5066 char *name = POPpbytex;
5068 if (proto && !*proto)
5071 sent = PerlSock_getservbyname(name, proto);
5073 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5076 else if (which == OP_GSBYPORT) {
5077 #ifdef HAS_GETSERVBYPORT
5078 char *proto = POPpbytex;
5079 unsigned short port = (unsigned short)POPu;
5081 if (proto && !*proto)
5085 port = PerlSock_htons(port);
5087 sent = PerlSock_getservbyport(port, proto);
5089 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5093 #ifdef HAS_GETSERVENT
5094 sent = PerlSock_getservent();
5096 DIE(aTHX_ PL_no_sock_func, "getservent");
5100 if (GIMME != G_ARRAY) {
5101 PUSHs(sv = sv_newmortal());
5103 if (which == OP_GSBYNAME) {
5105 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5107 sv_setiv(sv, (IV)(sent->s_port));
5111 sv_setpv(sv, sent->s_name);
5117 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5118 sv_setpv(sv, sent->s_name);
5119 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5120 for (elem = sent->s_aliases; elem && *elem; elem++) {
5121 sv_catpv(sv, *elem);
5123 sv_catpvn(sv, " ", 1);
5125 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5127 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5129 sv_setiv(sv, (IV)(sent->s_port));
5131 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5132 sv_setpv(sv, sent->s_proto);
5137 DIE(aTHX_ PL_no_sock_func, "getservent");
5143 #ifdef HAS_SETHOSTENT
5145 PerlSock_sethostent(TOPi);
5148 DIE(aTHX_ PL_no_sock_func, "sethostent");
5154 #ifdef HAS_SETNETENT
5156 PerlSock_setnetent(TOPi);
5159 DIE(aTHX_ PL_no_sock_func, "setnetent");
5165 #ifdef HAS_SETPROTOENT
5167 PerlSock_setprotoent(TOPi);
5170 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5176 #ifdef HAS_SETSERVENT
5178 PerlSock_setservent(TOPi);
5181 DIE(aTHX_ PL_no_sock_func, "setservent");
5187 #ifdef HAS_ENDHOSTENT
5189 PerlSock_endhostent();
5193 DIE(aTHX_ PL_no_sock_func, "endhostent");
5199 #ifdef HAS_ENDNETENT
5201 PerlSock_endnetent();
5205 DIE(aTHX_ PL_no_sock_func, "endnetent");
5211 #ifdef HAS_ENDPROTOENT
5213 PerlSock_endprotoent();
5217 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5223 #ifdef HAS_ENDSERVENT
5225 PerlSock_endservent();
5229 DIE(aTHX_ PL_no_sock_func, "endservent");
5238 DIE(aTHX_ PL_no_func, "getpwnam");
5247 DIE(aTHX_ PL_no_func, "getpwuid");
5255 I32 which = PL_op->op_type;
5257 struct passwd *pwent = NULL;
5259 * We currently support only the SysV getsp* shadow password interface.
5260 * The interface is declared in <shadow.h> and often one needs to link
5261 * with -lsecurity or some such.
5262 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5265 * AIX getpwnam() is clever enough to return the encrypted password
5266 * only if the caller (euid?) is root.
5268 * There are at least three other shadow password APIs. Many platforms
5269 * seem to contain more than one interface for accessing the shadow
5270 * password databases, possibly for compatibility reasons.
5271 * The getsp*() is by far he simplest one, the other two interfaces
5272 * are much more complicated, but also very similar to each other.
5277 * struct pr_passwd *getprpw*();
5278 * The password is in
5279 * char getprpw*(...).ufld.fd_encrypt[]
5280 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5285 * struct es_passwd *getespw*();
5286 * The password is in
5287 * char *(getespw*(...).ufld.fd_encrypt)
5288 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5291 * struct userpw *getuserpw();
5292 * The password is in
5293 * char *(getuserpw(...)).spw_upw_passwd
5294 * (but the de facto standard getpwnam() should work okay)
5296 * Mention I_PROT here so that Configure probes for it.
5298 * In HP-UX for getprpw*() the manual page claims that one should include
5299 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5300 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5301 * and pp_sys.c already includes <shadow.h> if there is such.
5303 * Note that <sys/security.h> is already probed for, but currently
5304 * it is only included in special cases.
5306 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5307 * be preferred interface, even though also the getprpw*() interface
5308 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5309 * One also needs to call set_auth_parameters() in main() before
5310 * doing anything else, whether one is using getespw*() or getprpw*().
5312 * Note that accessing the shadow databases can be magnitudes
5313 * slower than accessing the standard databases.
5318 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5319 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5320 * the pw_comment is left uninitialized. */
5321 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5327 char* name = POPpbytex;
5328 pwent = getpwnam(name);
5334 pwent = getpwuid(uid);
5338 # ifdef HAS_GETPWENT
5340 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5341 if (pwent) pwent = getpwnam(pwent->pw_name);
5344 DIE(aTHX_ PL_no_func, "getpwent");
5350 if (GIMME != G_ARRAY) {
5351 PUSHs(sv = sv_newmortal());
5353 if (which == OP_GPWNAM)
5354 # if Uid_t_sign <= 0
5355 sv_setiv(sv, (IV)pwent->pw_uid);
5357 sv_setuv(sv, (UV)pwent->pw_uid);
5360 sv_setpv(sv, pwent->pw_name);
5366 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5367 sv_setpv(sv, pwent->pw_name);
5369 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5371 /* If we have getspnam(), we try to dig up the shadow
5372 * password. If we are underprivileged, the shadow
5373 * interface will set the errno to EACCES or similar,
5374 * and return a null pointer. If this happens, we will
5375 * use the dummy password (usually "*" or "x") from the
5376 * standard password database.
5378 * In theory we could skip the shadow call completely
5379 * if euid != 0 but in practice we cannot know which
5380 * security measures are guarding the shadow databases
5381 * on a random platform.
5383 * Resist the urge to use additional shadow interfaces.
5384 * Divert the urge to writing an extension instead.
5387 /* Some AIX setups falsely(?) detect some getspnam(), which
5388 * has a different API than the Solaris/IRIX one. */
5389 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5391 struct spwd *spwent;
5392 int saverrno; /* Save and restore errno so that
5393 * underprivileged attempts seem
5394 * to have never made the unsccessful
5395 * attempt to retrieve the shadow password. */
5398 spwent = getspnam(pwent->pw_name);
5400 if (spwent && spwent->sp_pwdp)
5401 sv_setpv(sv, spwent->sp_pwdp);
5405 if (!SvPOK(sv)) /* Use the standard password, then. */
5406 sv_setpv(sv, pwent->pw_passwd);
5409 # ifndef INCOMPLETE_TAINTS
5410 /* passwd is tainted because user himself can diddle with it.
5411 * admittedly not much and in a very limited way, but nevertheless. */
5415 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5416 # if Uid_t_sign <= 0
5417 sv_setiv(sv, (IV)pwent->pw_uid);
5419 sv_setuv(sv, (UV)pwent->pw_uid);
5422 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5423 # if Uid_t_sign <= 0
5424 sv_setiv(sv, (IV)pwent->pw_gid);
5426 sv_setuv(sv, (UV)pwent->pw_gid);
5428 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5429 * because of the poor interface of the Perl getpw*(),
5430 * not because there's some standard/convention saying so.
5431 * A better interface would have been to return a hash,
5432 * but we are accursed by our history, alas. --jhi. */
5433 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5435 sv_setiv(sv, (IV)pwent->pw_change);
5438 sv_setiv(sv, (IV)pwent->pw_quota);
5441 sv_setpv(sv, pwent->pw_age);
5446 /* pw_class and pw_comment are mutually exclusive--.
5447 * see the above note for pw_change, pw_quota, and pw_age. */
5448 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5450 sv_setpv(sv, pwent->pw_class);
5453 sv_setpv(sv, pwent->pw_comment);
5457 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5459 sv_setpv(sv, pwent->pw_gecos);
5461 # ifndef INCOMPLETE_TAINTS
5462 /* pw_gecos is tainted because user himself can diddle with it. */
5466 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5467 sv_setpv(sv, pwent->pw_dir);
5469 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5470 sv_setpv(sv, pwent->pw_shell);
5471 # ifndef INCOMPLETE_TAINTS
5472 /* pw_shell is tainted because user himself can diddle with it. */
5477 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5478 sv_setiv(sv, (IV)pwent->pw_expire);
5483 DIE(aTHX_ PL_no_func, "getpwent");
5489 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5494 DIE(aTHX_ PL_no_func, "setpwent");
5500 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5505 DIE(aTHX_ PL_no_func, "endpwent");
5514 DIE(aTHX_ PL_no_func, "getgrnam");
5523 DIE(aTHX_ PL_no_func, "getgrgid");
5531 I32 which = PL_op->op_type;
5532 register char **elem;
5534 struct group *grent;
5536 if (which == OP_GGRNAM) {
5537 char* name = POPpbytex;
5538 grent = (struct group *)getgrnam(name);
5540 else if (which == OP_GGRGID) {
5542 grent = (struct group *)getgrgid(gid);
5546 grent = (struct group *)getgrent();
5548 DIE(aTHX_ PL_no_func, "getgrent");
5552 if (GIMME != G_ARRAY) {
5553 PUSHs(sv = sv_newmortal());
5555 if (which == OP_GGRNAM)
5556 sv_setiv(sv, (IV)grent->gr_gid);
5558 sv_setpv(sv, grent->gr_name);
5564 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5565 sv_setpv(sv, grent->gr_name);
5567 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5569 sv_setpv(sv, grent->gr_passwd);
5572 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5573 sv_setiv(sv, (IV)grent->gr_gid);
5575 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5576 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5577 /* In UNICOS/mk (_CRAYMPP) the multithreading
5578 * versions (getgrnam_r, getgrgid_r)
5579 * seem to return an illegal pointer
5580 * as the group members list, gr_mem.
5581 * getgrent() doesn't even have a _r version
5582 * but the gr_mem is poisonous anyway.
5583 * So yes, you cannot get the list of group
5584 * members if building multithreaded in UNICOS/mk. */
5585 for (elem = grent->gr_mem; elem && *elem; elem++) {
5586 sv_catpv(sv, *elem);
5588 sv_catpvn(sv, " ", 1);
5595 DIE(aTHX_ PL_no_func, "getgrent");
5601 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5606 DIE(aTHX_ PL_no_func, "setgrent");
5612 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5617 DIE(aTHX_ PL_no_func, "endgrent");
5627 if (!(tmps = PerlProc_getlogin()))
5629 PUSHp(tmps, strlen(tmps));
5632 DIE(aTHX_ PL_no_func, "getlogin");
5636 /* Miscellaneous. */
5641 dSP; dMARK; dORIGMARK; dTARGET;
5642 register I32 items = SP - MARK;
5643 unsigned long a[20];
5648 while (++MARK <= SP) {
5649 if (SvTAINTED(*MARK)) {
5655 TAINT_PROPER("syscall");
5658 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5659 * or where sizeof(long) != sizeof(char*). But such machines will
5660 * not likely have syscall implemented either, so who cares?
5662 while (++MARK <= SP) {
5663 if (SvNIOK(*MARK) || !i)
5664 a[i++] = SvIV(*MARK);
5665 else if (*MARK == &PL_sv_undef)
5668 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5674 DIE(aTHX_ "Too many args to syscall");
5676 DIE(aTHX_ "Too few args to syscall");
5678 retval = syscall(a[0]);
5681 retval = syscall(a[0],a[1]);
5684 retval = syscall(a[0],a[1],a[2]);
5687 retval = syscall(a[0],a[1],a[2],a[3]);
5690 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5693 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5696 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5699 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5703 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5706 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],
5721 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5722 a[10],a[11],a[12],a[13]);
5724 #endif /* atarist */
5730 DIE(aTHX_ PL_no_func, "syscall");
5734 #ifdef FCNTL_EMULATE_FLOCK
5736 /* XXX Emulate flock() with fcntl().
5737 What's really needed is a good file locking module.
5741 fcntl_emulate_flock(int fd, int operation)
5745 switch (operation & ~LOCK_NB) {
5747 flock.l_type = F_RDLCK;
5750 flock.l_type = F_WRLCK;
5753 flock.l_type = F_UNLCK;
5759 flock.l_whence = SEEK_SET;
5760 flock.l_start = flock.l_len = (Off_t)0;
5762 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5765 #endif /* FCNTL_EMULATE_FLOCK */
5767 #ifdef LOCKF_EMULATE_FLOCK
5769 /* XXX Emulate flock() with lockf(). This is just to increase
5770 portability of scripts. The calls are not completely
5771 interchangeable. What's really needed is a good file
5775 /* The lockf() constants might have been defined in <unistd.h>.
5776 Unfortunately, <unistd.h> causes troubles on some mixed
5777 (BSD/POSIX) systems, such as SunOS 4.1.3.
5779 Further, the lockf() constants aren't POSIX, so they might not be
5780 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5781 just stick in the SVID values and be done with it. Sigh.
5785 # define F_ULOCK 0 /* Unlock a previously locked region */
5788 # define F_LOCK 1 /* Lock a region for exclusive use */
5791 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5794 # define F_TEST 3 /* Test a region for other processes locks */
5798 lockf_emulate_flock(int fd, int operation)
5804 /* flock locks entire file so for lockf we need to do the same */
5806 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5807 if (pos > 0) /* is seekable and needs to be repositioned */
5808 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5809 pos = -1; /* seek failed, so don't seek back afterwards */
5812 switch (operation) {
5814 /* LOCK_SH - get a shared lock */
5816 /* LOCK_EX - get an exclusive lock */
5818 i = lockf (fd, F_LOCK, 0);
5821 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5822 case LOCK_SH|LOCK_NB:
5823 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5824 case LOCK_EX|LOCK_NB:
5825 i = lockf (fd, F_TLOCK, 0);
5827 if ((errno == EAGAIN) || (errno == EACCES))
5828 errno = EWOULDBLOCK;
5831 /* LOCK_UN - unlock (non-blocking is a no-op) */
5833 case LOCK_UN|LOCK_NB:
5834 i = lockf (fd, F_ULOCK, 0);
5837 /* Default - can't decipher operation */
5844 if (pos > 0) /* need to restore position of the handle */
5845 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5850 #endif /* LOCKF_EMULATE_FLOCK */
5854 * c-indentation-style: bsd
5856 * indent-tabs-mode: t
5859 * ex: set ts=8 sts=4 sw=4 noet: