3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * But only a short way ahead its floor and the walls on either side were
12 * cloven by a great fissure, out of which the red glare came, now leaping
13 * up, now dying down into darkness; and all the while far below there was
14 * a rumour and a trouble as of great engines throbbing and labouring.
18 #define PERL_IN_PP_SYS_C
22 /* Shadow password support for solaris - pdo@cs.umd.edu
23 * Not just Solaris: at least HP-UX, IRIX, Linux.
24 * The API is from SysV.
26 * There are at least two more shadow interfaces,
27 * see the comments in pp_gpwent().
31 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
32 * and another MAXINT from "perl.h" <- <sys/param.h>. */
40 extern "C" int syscall(unsigned long,...);
45 # include <sys/wait.h>
49 # include <sys/resource.h>
58 # include <sys/select.h>
62 /* XXX Configure test needed.
63 h_errno might not be a simple 'int', especially for multi-threaded
64 applications, see "extern int errno in perl.h". Creating such
65 a test requires taking into account the differences between
66 compiling multithreaded and singlethreaded ($ccflags et al).
67 HOST_NOT_FOUND is typically defined in <netdb.h>.
69 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
78 struct passwd *getpwnam (char *);
79 struct passwd *getpwuid (Uid_t);
83 struct passwd *getpwent (void);
91 struct group *getgrnam (char *);
92 struct group *getgrgid (Gid_t);
95 struct group *getgrent (void);
100 # if defined(_MSC_VER) || defined(__MINGW32__)
101 # include <sys/utime.h>
107 /* Put this after #includes because fork and vfork prototypes may conflict. */
113 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
116 # define my_chsize PerlLIO_chsize
121 #else /* no flock() */
123 /* fcntl.h might not have been included, even if it exists, because
124 the current Configure only sets I_FCNTL if it's needed to pick up
125 the *_OK constants. Make sure it has been included before testing
126 the fcntl() locking constants. */
127 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
131 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
132 # define FLOCK fcntl_emulate_flock
133 # define FCNTL_EMULATE_FLOCK
134 # else /* no flock() or fcntl(F_SETLK,...) */
136 # define FLOCK lockf_emulate_flock
137 # define LOCKF_EMULATE_FLOCK
139 # endif /* no flock() or fcntl(F_SETLK,...) */
142 static int FLOCK (int, int);
145 * These are the flock() constants. Since this sytems doesn't have
146 * flock(), the values of the constants are probably not available.
160 # endif /* emulating flock() */
162 #endif /* no flock() */
165 static char zero_but_true[ZBTLEN + 1] = "0 but true";
167 #if defined(I_SYS_ACCESS) && !defined(R_OK)
168 # include <sys/access.h>
171 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
172 # define FD_CLOEXEC 1 /* NeXT needs this */
175 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
176 #undef PERL_EFF_ACCESS_W_OK
177 #undef PERL_EFF_ACCESS_X_OK
179 /* F_OK unused: if stat() cannot find it... */
181 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
182 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
183 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
184 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
185 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
188 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
189 # ifdef I_SYS_SECURITY
190 # include <sys/security.h>
194 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
195 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
196 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
199 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
200 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
201 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
205 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
207 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
208 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
209 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
212 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
213 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
214 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
217 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
219 Uid_t ruid = getuid();
220 Uid_t euid = geteuid();
221 Gid_t rgid = getgid();
222 Gid_t egid = getegid();
226 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
227 Perl_croak(aTHX_ "switching effective uid is not implemented");
230 if (setreuid(euid, ruid))
233 if (setresuid(euid, ruid, (Uid_t)-1))
236 Perl_croak(aTHX_ "entering effective uid failed");
239 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
240 Perl_croak(aTHX_ "switching effective gid is not implemented");
243 if (setregid(egid, rgid))
246 if (setresgid(egid, rgid, (Gid_t)-1))
249 Perl_croak(aTHX_ "entering effective gid failed");
252 res = access(path, mode);
255 if (setreuid(ruid, euid))
258 if (setresuid(ruid, euid, (Uid_t)-1))
261 Perl_croak(aTHX_ "leaving effective uid failed");
264 if (setregid(rgid, egid))
267 if (setresgid(rgid, egid, (Gid_t)-1))
270 Perl_croak(aTHX_ "leaving effective gid failed");
275 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
276 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
277 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
280 #if !defined(PERL_EFF_ACCESS_R_OK)
281 /* With it or without it: anyway you get a warning: either that
282 it is unused, or it is declared static and never defined.
285 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
287 Perl_croak(aTHX_ "switching effective uid is not implemented");
303 if (PL_op->op_private & OPpOPEN_IN_RAW)
305 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
307 fp = PerlProc_popen(tmps, mode);
310 if (PL_curcop->cop_io) {
311 type = SvPV_nolen(PL_curcop->cop_io);
314 PerlIO_apply_layers(aTHX_ fp,mode,type);
316 if (gimme == G_VOID) {
318 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
322 else if (gimme == G_SCALAR) {
323 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
324 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
335 if (sv_gets(sv, fp, 0) == Nullch) {
339 XPUSHs(sv_2mortal(sv));
340 if (SvLEN(sv) - SvCUR(sv) > 20) {
341 SvLEN_set(sv, SvCUR(sv)+1);
342 Renew(SvPVX(sv), SvLEN(sv), char);
347 STATUS_NATIVE_SET(PerlProc_pclose(fp));
348 TAINT; /* "I believe that this is not gratuitous!" */
351 STATUS_NATIVE_SET(-1);
352 if (gimme == G_SCALAR)
362 tryAMAGICunTARGET(iter, -1);
364 /* Note that we only ever get here if File::Glob fails to load
365 * without at the same time croaking, for some reason, or if
366 * perl was built with PERL_EXTERNAL_GLOB */
373 * The external globbing program may use things we can't control,
374 * so for security reasons we must assume the worst.
377 taint_proper(PL_no_security, "glob");
381 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
382 PL_last_in_gv = (GV*)*PL_stack_sp--;
384 SAVESPTR(PL_rs); /* This is not permanent, either. */
385 PL_rs = sv_2mortal(newSVpvn("\000", 1));
388 *SvPVX(PL_rs) = '\n';
392 result = do_readline();
397 #if 0 /* XXX never used! */
401 PL_last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*PL_stack_sp--)), n_a), TRUE,SVt_PVIO);
402 return do_readline();
408 PL_last_in_gv = cGVOP_gv;
409 return do_readline();
418 if (SP - MARK != 1) {
420 do_join(TARG, &PL_sv_no, MARK, SP);
427 tmps = SvPV(tmpsv, len);
430 (void)SvUPGRADE(error, SVt_PV);
431 if (SvPOK(error) && SvCUR(error))
432 sv_catpv(error, "\t...caught");
434 tmps = SvPV(tmpsv, len);
437 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
439 Perl_warn(aTHX_ "%"SVf, tmpsv);
450 if (SP - MARK != 1) {
452 do_join(TARG, &PL_sv_no, MARK, SP);
454 tmps = SvPV(tmpsv, len);
460 tmps = (SvROK(tmpsv) && PL_in_eval) ? Nullch : SvPV(tmpsv, len);
464 (void)SvUPGRADE(error, SVt_PV);
465 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
467 SvSetSV(error,tmpsv);
468 else if (sv_isobject(error)) {
469 HV *stash = SvSTASH(SvRV(error));
470 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
472 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
473 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
480 call_sv((SV*)GvCV(gv),
481 G_SCALAR|G_EVAL|G_KEEPERR);
482 sv_setsv(error,*PL_stack_sp--);
488 if (SvPOK(error) && SvCUR(error))
489 sv_catpv(error, "\t...propagated");
491 tmps = SvPV(tmpsv, len);
495 tmpsv = sv_2mortal(newSVpvn("Died", 4));
497 DIE(aTHX_ "%"SVf, tmpsv);
516 DIE(aTHX_ PL_no_usym, "filehandle");
518 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
520 if ((mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
521 /* Method's args are same as ours ... */
522 /* ... except handle is replaced by the object */
523 *MARK-- = SvTIED_obj((SV*)gv, mg);
527 call_method("OPEN", G_SCALAR);
540 tmps = SvPV(sv, len);
541 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
544 PUSHi( (I32)PL_forkprocess );
545 else if (PL_forkprocess == 0) /* we are a new child */
563 if ((mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
565 XPUSHs(SvTIED_obj((SV*)gv, mg));
568 call_method("CLOSE", G_SCALAR);
574 PUSHs(boolSV(do_close(gv, TRUE)));
594 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
595 DIE(aTHX_ PL_no_usym, "filehandle");
600 do_close(rgv, FALSE);
602 do_close(wgv, FALSE);
604 if (PerlProc_pipe(fd) < 0)
607 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
608 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
609 IoIFP(wstio) = IoOFP(wstio);
610 IoTYPE(rstio) = IoTYPE_RDONLY;
611 IoTYPE(wstio) = IoTYPE_WRONLY;
613 if (!IoIFP(rstio) || !IoOFP(wstio)) {
614 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
615 else PerlLIO_close(fd[0]);
616 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
617 else PerlLIO_close(fd[1]);
620 #if defined(HAS_FCNTL) && defined(F_SETFD)
621 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
622 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
629 DIE(aTHX_ PL_no_func, "pipe");
645 if (gv && (mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
647 XPUSHs(SvTIED_obj((SV*)gv, mg));
650 call_method("FILENO", G_SCALAR);
656 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
657 /* Can't do this because people seem to do things like
658 defined(fileno($foo)) to check whether $foo is a valid fh.
659 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
660 report_evil_fh(gv, io, PL_op->op_type);
665 PUSHi(PerlIO_fileno(fp));
676 anum = PerlLIO_umask(0);
677 (void)PerlLIO_umask(anum);
680 anum = PerlLIO_umask(POPi);
681 TAINT_PROPER("umask");
684 /* Only DIE if trying to restrict permissions on `user' (self).
685 * Otherwise it's harmless and more useful to just return undef
686 * since 'group' and 'other' concepts probably don't exist here. */
687 if (MAXARG >= 1 && (POPi & 0700))
688 DIE(aTHX_ "umask not implemented");
689 XPUSHs(&PL_sv_undef);
713 if (gv && (mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
715 XPUSHs(SvTIED_obj((SV*)gv, mg));
720 call_method("BINMODE", G_SCALAR);
727 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
728 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
729 report_evil_fh(gv, io, PL_op->op_type);
734 names = SvPV(discp,len);
737 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
738 (discp) ? SvPV_nolen(discp) : Nullch))
752 I32 markoff = MARK - PL_stack_base;
754 int how = PERL_MAGIC_tied;
759 switch(SvTYPE(varsv)) {
761 methname = "TIEHASH";
764 methname = "TIEARRAY";
767 #ifdef GV_SHARED_CHECK
768 if (GvSHARED((GV*)varsv)) {
769 Perl_croak(aTHX_ "Attempt to tie shared GV");
772 methname = "TIEHANDLE";
773 how = PERL_MAGIC_tiedscalar;
776 methname = "TIESCALAR";
777 how = PERL_MAGIC_tiedscalar;
781 if (sv_isobject(*MARK)) {
783 PUSHSTACKi(PERLSI_MAGIC);
789 call_method(methname, G_SCALAR);
792 /* Not clear why we don't call call_method here too.
793 * perhaps to get different error message ?
795 stash = gv_stashsv(*MARK, FALSE);
796 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
797 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"",
798 methname, SvPV(*MARK,n_a));
801 PUSHSTACKi(PERLSI_MAGIC);
807 call_sv((SV*)GvCV(gv), G_SCALAR);
813 if (sv_isobject(sv)) {
814 sv_unmagic(varsv, how);
815 /* Croak if a self-tie on an aggregate is attempted. */
816 if (varsv == SvRV(sv) &&
817 (SvTYPE(sv) == SVt_PVAV ||
818 SvTYPE(sv) == SVt_PVHV))
820 "Self-ties of arrays and hashes are not supported");
821 sv_magic(varsv, sv, how, Nullch, 0);
824 SP = PL_stack_base + markoff;
833 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
834 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
837 if ((mg = SvTIED_mg(sv, how))) {
838 SV *obj = SvRV(mg->mg_obj);
841 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
842 isGV(gv) && (cv = GvCV(gv))) {
844 XPUSHs(SvTIED_obj((SV*)gv, mg));
845 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
848 call_sv((SV *)cv, G_VOID);
852 else if (ckWARN(WARN_UNTIE)) {
853 if (mg && SvREFCNT(obj) > 1)
854 Perl_warner(aTHX_ WARN_UNTIE,
855 "untie attempted while %"UVuf" inner references still exist",
856 (UV)SvREFCNT(obj) - 1 ) ;
867 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
868 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
871 if ((mg = SvTIED_mg(sv, how))) {
872 SV *osv = SvTIED_obj(sv, mg);
873 if (osv == mg->mg_obj)
874 osv = sv_mortalcopy(osv);
892 sv = sv_mortalcopy(&PL_sv_no);
893 sv_setpv(sv, "AnyDBM_File");
894 stash = gv_stashsv(sv, FALSE);
895 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
897 require_pv("AnyDBM_File.pm");
899 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
900 DIE(aTHX_ "No dbm on this machine");
910 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
912 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
915 call_sv((SV*)GvCV(gv), G_SCALAR);
918 if (!sv_isobject(TOPs)) {
923 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
926 call_sv((SV*)GvCV(gv), G_SCALAR);
930 if (sv_isobject(TOPs)) {
931 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
932 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
954 struct timeval timebuf;
955 struct timeval *tbuf = &timebuf;
959 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
964 # if BYTEORDER & 0xf0000
965 # define ORDERBYTE (0x88888888 - BYTEORDER)
967 # define ORDERBYTE (0x4444 - BYTEORDER)
973 for (i = 1; i <= 3; i++) {
981 /* little endians can use vecs directly */
982 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
983 # if SELECT_MIN_BITS > 1
984 /* If SELECT_MIN_BITS is greater than one we most probably will want
985 * to align the sizes with SELECT_MIN_BITS/8 because for example
986 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
987 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
988 * on (sets/tests/clears bits) is 32 bits. */
989 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
991 growsize = sizeof(fd_set);
1000 masksize = NFDBITS / NBBY;
1002 masksize = sizeof(long); /* documented int, everyone seems to use long */
1004 growsize = maxlen + (masksize - (maxlen % masksize));
1005 Zero(&fd_sets[0], 4, char*);
1013 timebuf.tv_sec = (long)value;
1014 value -= (NV)timebuf.tv_sec;
1015 timebuf.tv_usec = (long)(value * 1000000.0);
1018 tbuf = Null(struct timeval*);
1020 for (i = 1; i <= 3; i++) {
1026 else if (!SvPOK(sv))
1027 SvPV_force(sv,n_a); /* force string conversion */
1030 Sv_Grow(sv, growsize);
1034 while (++j <= growsize) {
1038 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1040 New(403, fd_sets[i], growsize, char);
1041 for (offset = 0; offset < growsize; offset += masksize) {
1042 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1043 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1046 fd_sets[i] = SvPVX(sv);
1050 nfound = PerlSock_select(
1052 (Select_fd_set_t) fd_sets[1],
1053 (Select_fd_set_t) fd_sets[2],
1054 (Select_fd_set_t) fd_sets[3],
1056 for (i = 1; i <= 3; i++) {
1059 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1061 for (offset = 0; offset < growsize; offset += masksize) {
1062 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1063 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1065 Safefree(fd_sets[i]);
1072 if (GIMME == G_ARRAY && tbuf) {
1073 value = (NV)(timebuf.tv_sec) +
1074 (NV)(timebuf.tv_usec) / 1000000.0;
1075 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1076 sv_setnv(sv, value);
1080 DIE(aTHX_ "select not implemented");
1085 Perl_setdefout(pTHX_ GV *gv)
1088 (void)SvREFCNT_inc(gv);
1090 SvREFCNT_dec(PL_defoutgv);
1097 GV *newdefout, *egv;
1100 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1102 egv = GvEGV(PL_defoutgv);
1107 XPUSHs(&PL_sv_undef);
1109 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1110 if (gvp && *gvp == egv) {
1111 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1115 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1120 if (!GvIO(newdefout))
1121 gv_IOadd(newdefout);
1122 setdefout(newdefout);
1139 if ((mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
1140 I32 gimme = GIMME_V;
1142 XPUSHs(SvTIED_obj((SV*)gv, mg));
1145 call_method("GETC", gimme);
1148 if (gimme == G_SCALAR)
1149 SvSetMagicSV_nosteal(TARG, TOPs);
1152 if (!gv || do_eof(gv)) /* make sure we have fp with something */
1155 sv_setpv(TARG, " ");
1156 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1157 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1158 /* Find out how many bytes the char needs */
1159 Size_t len = UTF8SKIP(SvPVX(TARG));
1162 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1163 SvCUR_set(TARG,1+len);
1173 return pp_sysread();
1177 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1179 register PERL_CONTEXT *cx;
1180 I32 gimme = GIMME_V;
1181 AV* padlist = CvPADLIST(cv);
1182 SV** svp = AvARRAY(padlist);
1188 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1190 SAVEVPTR(PL_curpad);
1191 PL_curpad = AvARRAY((AV*)svp[1]);
1193 setdefout(gv); /* locally select filehandle so $% et al work */
1226 SV *tmpsv = sv_newmortal();
1227 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1228 name = SvPV_nolen(tmpsv);
1231 DIE(aTHX_ "Undefined format \"%s\" called", name);
1232 DIE(aTHX_ "Not a format reference");
1235 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1237 IoFLAGS(io) &= ~IOf_DIDTOP;
1238 return doform(cv,gv,PL_op->op_next);
1244 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1245 register IO *io = GvIOp(gv);
1246 PerlIO *ofp = IoOFP(io);
1250 register PERL_CONTEXT *cx;
1252 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1253 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1256 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1257 PL_formtarget != PL_toptarget)
1261 if (!IoTOP_GV(io)) {
1265 if (!IoTOP_NAME(io)) {
1266 if (!IoFMT_NAME(io))
1267 IoFMT_NAME(io) = savepv(GvNAME(gv));
1268 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1269 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1270 if ((topgv && GvFORM(topgv)) ||
1271 !gv_fetchpv("top",FALSE,SVt_PVFM))
1272 IoTOP_NAME(io) = savepv(SvPVX(topname));
1274 IoTOP_NAME(io) = savepv("top");
1276 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1277 if (!topgv || !GvFORM(topgv)) {
1278 IoLINES_LEFT(io) = 100000000;
1281 IoTOP_GV(io) = topgv;
1283 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1284 I32 lines = IoLINES_LEFT(io);
1285 char *s = SvPVX(PL_formtarget);
1286 if (lines <= 0) /* Yow, header didn't even fit!!! */
1288 while (lines-- > 0) {
1289 s = strchr(s, '\n');
1295 STRLEN save = SvCUR(PL_formtarget);
1296 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1297 do_print(PL_formtarget, ofp);
1298 SvCUR_set(PL_formtarget, save);
1299 sv_chop(PL_formtarget, s);
1300 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1303 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1304 do_print(PL_formfeed, ofp);
1305 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1307 PL_formtarget = PL_toptarget;
1308 IoFLAGS(io) |= IOf_DIDTOP;
1311 DIE(aTHX_ "bad top format reference");
1316 SV *sv = sv_newmortal();
1317 gv_efullname4(sv, fgv, Nullch, FALSE);
1318 name = SvPV_nolen(sv);
1321 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1324 DIE(aTHX_ "Undefined top format called");
1328 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1329 return doform(cv,gv,PL_op);
1333 POPBLOCK(cx,PL_curpm);
1339 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1341 /* integrate with report_evil_fh()? */
1344 SV* sv = sv_newmortal();
1345 gv_efullname4(sv, gv, Nullch, FALSE);
1346 name = SvPV_nolen(sv);
1349 Perl_warner(aTHX_ WARN_IO,
1350 "Filehandle %s opened only for input", name);
1352 Perl_warner(aTHX_ WARN_IO,
1353 "Filehandle opened only for input");
1355 else if (ckWARN(WARN_CLOSED))
1356 report_evil_fh(gv, io, PL_op->op_type);
1361 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1362 if (ckWARN(WARN_IO))
1363 Perl_warner(aTHX_ WARN_IO, "page overflow");
1365 if (!do_print(PL_formtarget, fp))
1368 FmLINES(PL_formtarget) = 0;
1369 SvCUR_set(PL_formtarget, 0);
1370 *SvEND(PL_formtarget) = '\0';
1371 if (IoFLAGS(io) & IOf_FLUSH)
1372 (void)PerlIO_flush(fp);
1377 PL_formtarget = PL_bodytarget;
1379 return pop_return();
1384 dSP; dMARK; dORIGMARK;
1391 if (PL_op->op_flags & OPf_STACKED)
1396 if ((mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
1397 if (MARK == ORIGMARK) {
1400 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1404 *MARK = SvTIED_obj((SV*)gv, mg);
1407 call_method("PRINTF", G_SCALAR);
1410 MARK = ORIGMARK + 1;
1417 if (!(io = GvIO(gv))) {
1418 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1419 report_evil_fh(gv, io, PL_op->op_type);
1420 SETERRNO(EBADF,RMS$_IFI);
1423 else if (!(fp = IoOFP(io))) {
1424 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1425 /* integrate with report_evil_fh()? */
1429 gv_efullname4(sv, gv, Nullch, FALSE);
1430 name = SvPV_nolen(sv);
1433 Perl_warner(aTHX_ WARN_IO,
1434 "Filehandle %s opened only for input", name);
1436 Perl_warner(aTHX_ WARN_IO,
1437 "Filehandle opened only for input");
1439 else if (ckWARN(WARN_CLOSED))
1440 report_evil_fh(gv, io, PL_op->op_type);
1442 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
1446 do_sprintf(sv, SP - MARK, MARK + 1);
1447 if (!do_print(sv, fp))
1450 if (IoFLAGS(io) & IOf_FLUSH)
1451 if (PerlIO_flush(fp) == EOF)
1462 PUSHs(&PL_sv_undef);
1483 /* Need TIEHANDLE method ? */
1485 tmps = SvPV(sv, len);
1486 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1487 IoLINES(GvIOp(gv)) = 0;
1491 PUSHs(&PL_sv_undef);
1498 dSP; dMARK; dORIGMARK; dTARGET;
1505 Sock_size_t bufsize;
1514 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) &&
1515 (mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar)))
1520 *MARK = SvTIED_obj((SV*)gv, mg);
1522 call_method("READ", G_SCALAR);
1535 sv_setpvn(bufsv, "", 0);
1536 length = SvIVx(*++MARK);
1539 offset = SvIVx(*++MARK);
1543 if (!io || !IoIFP(io))
1545 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1546 buffer = SvPVutf8_force(bufsv, blen);
1547 /* UTF8 may not have been set if they are all low bytes */
1551 buffer = SvPV_force(bufsv, blen);
1554 DIE(aTHX_ "Negative length");
1558 if (PL_op->op_type == OP_RECV) {
1559 char namebuf[MAXPATHLEN];
1560 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE)
1561 bufsize = sizeof (struct sockaddr_in);
1563 bufsize = sizeof namebuf;
1565 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1569 buffer = SvGROW(bufsv, length+1);
1570 /* 'offset' means 'flags' here */
1571 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1572 (struct sockaddr *)namebuf, &bufsize);
1576 /* Bogus return without padding */
1577 bufsize = sizeof (struct sockaddr_in);
1579 SvCUR_set(bufsv, count);
1580 *SvEND(bufsv) = '\0';
1581 (void)SvPOK_only(bufsv);
1585 /* This should not be marked tainted if the fp is marked clean */
1586 if (!(IoFLAGS(io) & IOf_UNTAINT))
1587 SvTAINTED_on(bufsv);
1589 sv_setpvn(TARG, namebuf, bufsize);
1594 if (PL_op->op_type == OP_RECV)
1595 DIE(aTHX_ PL_no_sock_func, "recv");
1597 if (DO_UTF8(bufsv)) {
1598 /* offset adjust in characters not bytes */
1599 blen = sv_len_utf8(bufsv);
1603 DIE(aTHX_ "Offset outside string");
1606 if (DO_UTF8(bufsv)) {
1607 /* convert offset-as-chars to offset-as-bytes */
1608 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1611 bufsize = SvCUR(bufsv);
1612 buffer = SvGROW(bufsv, length+offset+1);
1613 if (offset > bufsize) { /* Zero any newly allocated space */
1614 Zero(buffer+bufsize, offset-bufsize, char);
1616 buffer = buffer + offset;
1618 if (PL_op->op_type == OP_SYSREAD) {
1619 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1620 if (IoTYPE(io) == IoTYPE_SOCKET) {
1621 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1627 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1632 #ifdef HAS_SOCKET__bad_code_maybe
1633 if (IoTYPE(io) == IoTYPE_SOCKET) {
1634 char namebuf[MAXPATHLEN];
1635 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1636 bufsize = sizeof (struct sockaddr_in);
1638 bufsize = sizeof namebuf;
1640 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1641 (struct sockaddr *)namebuf, &bufsize);
1646 count = PerlIO_read(IoIFP(io), buffer, length);
1647 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1648 if (count == 0 && PerlIO_error(IoIFP(io)))
1652 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1654 /* integrate with report_evil_fh()? */
1657 SV* sv = sv_newmortal();
1658 gv_efullname4(sv, gv, Nullch, FALSE);
1659 name = SvPV_nolen(sv);
1662 Perl_warner(aTHX_ WARN_IO,
1663 "Filehandle %s opened only for output", name);
1665 Perl_warner(aTHX_ WARN_IO,
1666 "Filehandle opened only for output");
1670 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1671 *SvEND(bufsv) = '\0';
1672 (void)SvPOK_only(bufsv);
1673 if (fp_utf8 && !IN_BYTES) {
1674 /* Look at utf8 we got back and count the characters */
1675 char *bend = buffer + count;
1676 while (buffer < bend) {
1677 STRLEN skip = UTF8SKIP(buffer);
1678 if (buffer+skip > bend) {
1679 /* partial character - try for rest of it */
1680 length = skip - (bend-buffer);
1681 offset = bend - SvPVX(bufsv);
1689 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1690 provided amount read (count) was what was requested (length)
1692 if (got < wanted && count == length) {
1693 length = (wanted-got);
1694 offset = bend - SvPVX(bufsv);
1697 /* return value is character count */
1702 /* This should not be marked tainted if the fp is marked clean */
1703 if (!(IoFLAGS(io) & IOf_UNTAINT))
1704 SvTAINTED_on(bufsv);
1717 int items = (SP - PL_stack_base) - TOPMARK;
1721 sv = sv_2mortal(newSViv(sv_len(*SP)));
1730 dSP; dMARK; dORIGMARK; dTARGET;
1741 if (PL_op->op_type == OP_SYSWRITE
1742 && (mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar)))
1747 *MARK = SvTIED_obj((SV*)gv, mg);
1749 call_method("WRITE", G_SCALAR);
1760 #if Size_t_size > IVSIZE
1761 length = (Size_t)SvNVx(*++MARK);
1763 length = (Size_t)SvIVx(*++MARK);
1765 if ((SSize_t)length < 0)
1766 DIE(aTHX_ "Negative length");
1769 if (!io || !IoIFP(io)) {
1771 if (ckWARN(WARN_CLOSED))
1772 report_evil_fh(gv, io, PL_op->op_type);
1776 if (PerlIO_isutf8(IoIFP(io))) {
1777 buffer = SvPVutf8(bufsv, blen);
1781 sv_utf8_downgrade(bufsv, FALSE);
1782 buffer = SvPV(bufsv, blen);
1785 if (PL_op->op_type == OP_SYSWRITE) {
1787 if (DO_UTF8(bufsv)) {
1788 /* length and offset are in chars */
1789 blen = sv_len_utf8(bufsv);
1792 offset = SvIVx(*++MARK);
1795 DIE(aTHX_ "Offset outside string");
1797 } else if (offset >= blen && blen > 0)
1798 DIE(aTHX_ "Offset outside string");
1801 if (length > blen - offset)
1802 length = blen - offset;
1803 if (DO_UTF8(bufsv)) {
1804 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1805 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1808 buffer = buffer+offset;
1810 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1811 if (IoTYPE(io) == IoTYPE_SOCKET) {
1812 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1818 /* See the note at doio.c:do_print about filesize limits. --jhi */
1819 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1824 else if (SP > MARK) {
1827 sockbuf = SvPVx(*++MARK, mlen);
1828 /* length is really flags */
1829 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1830 length, (struct sockaddr *)sockbuf, mlen);
1833 /* length is really flags */
1834 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1837 DIE(aTHX_ PL_no_sock_func, "send");
1842 #if Size_t_size > IVSIZE
1856 return pp_sysread();
1866 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1868 gv = PL_last_in_gv = PL_argvgv;
1870 if (io && !IoIFP(io)) {
1871 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1873 IoFLAGS(io) &= ~IOf_START;
1874 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1875 sv_setpvn(GvSV(gv), "-", 1);
1876 SvSETMAGIC(GvSV(gv));
1878 else if (!nextargv(gv))
1883 gv = PL_last_in_gv; /* eof */
1886 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1888 if (gv && (mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
1890 XPUSHs(SvTIED_obj((SV*)gv, mg));
1893 call_method("EOF", G_SCALAR);
1899 PUSHs(boolSV(!gv || do_eof(gv)));
1912 gv = PL_last_in_gv = (GV*)POPs;
1914 if (gv && (mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
1916 XPUSHs(SvTIED_obj((SV*)gv, mg));
1919 call_method("TELL", G_SCALAR);
1925 #if LSEEKSIZE > IVSIZE
1926 PUSHn( do_tell(gv) );
1928 PUSHi( do_tell(gv) );
1935 return pp_sysseek();
1943 #if LSEEKSIZE > IVSIZE
1944 Off_t offset = (Off_t)SvNVx(POPs);
1946 Off_t offset = (Off_t)SvIVx(POPs);
1950 gv = PL_last_in_gv = (GV*)POPs;
1952 if (gv && (mg = SvTIED_mg((SV*)gv, PERL_MAGIC_tiedscalar))) {
1954 XPUSHs(SvTIED_obj((SV*)gv, mg));
1955 #if LSEEKSIZE > IVSIZE
1956 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1958 XPUSHs(sv_2mortal(newSViv(offset)));
1960 XPUSHs(sv_2mortal(newSViv(whence)));
1963 call_method("SEEK", G_SCALAR);
1969 if (PL_op->op_type == OP_SEEK)
1970 PUSHs(boolSV(do_seek(gv, offset, whence)));
1972 Off_t sought = do_sysseek(gv, offset, whence);
1974 PUSHs(&PL_sv_undef);
1977 #if LSEEKSIZE > IVSIZE
1982 : newSVpvn(zero_but_true, ZBTLEN);
1983 PUSHs(sv_2mortal(sv));
1992 /* There seems to be no consensus on the length type of truncate()
1993 * and ftruncate(), both off_t and size_t have supporters. In
1994 * general one would think that when using large files, off_t is
1995 * at least as wide as size_t, so using an off_t should be okay. */
1996 /* XXX Configure probe for the length type of *truncate() needed XXX */
1999 #if Size_t_size > IVSIZE
2004 /* Checking for length < 0 is problematic as the type might or
2005 * might not be signed: if it is not, clever compilers will moan. */
2006 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2008 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2014 if (PL_op->op_flags & OPf_SPECIAL) {
2015 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2018 TAINT_PROPER("truncate");
2019 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)))
2022 PerlIO_flush(IoIFP(GvIOp(tmpgv)));
2024 if (ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
2026 if (my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
2035 if (SvTYPE(sv) == SVt_PVGV) {
2036 tmpgv = (GV*)sv; /* *main::FRED for example */
2039 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2040 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2044 name = SvPV(sv, n_a);
2045 TAINT_PROPER("truncate");
2047 if (truncate(name, len) < 0)
2053 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2056 if (my_chsize(tmpfd, len) < 0)
2058 PerlLIO_close(tmpfd);
2067 SETERRNO(EBADF,RMS$_IFI);
2071 DIE(aTHX_ "truncate not implemented");
2084 unsigned int func = POPu;
2085 int optype = PL_op->op_type;
2089 IO *io = gv ? GvIOn(gv) : 0;
2091 if (!io || !argsv || !IoIFP(io)) {
2092 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2093 report_evil_fh(gv, io, PL_op->op_type);
2094 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
2098 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2101 s = SvPV_force(argsv, len);
2102 need = IOCPARM_LEN(func);
2104 s = Sv_Grow(argsv, need + 1);
2105 SvCUR_set(argsv, need);
2108 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2111 retval = SvIV(argsv);
2112 s = INT2PTR(char*,retval); /* ouch */
2115 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2117 if (optype == OP_IOCTL)
2119 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2121 DIE(aTHX_ "ioctl is not implemented");
2125 #if defined(OS2) && defined(__EMX__)
2126 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2128 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2131 DIE(aTHX_ "fcntl is not implemented");
2135 if (s[SvCUR(argsv)] != 17)
2136 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2137 PL_op_name[optype]);
2138 s[SvCUR(argsv)] = 0; /* put our null back */
2139 SvSETMAGIC(argsv); /* Assume it has changed */
2148 PUSHp(zero_but_true, ZBTLEN);
2168 if (gv && (io = GvIO(gv)))
2175 (void)PerlIO_flush(fp);
2176 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2179 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2180 report_evil_fh(gv, io, PL_op->op_type);
2182 SETERRNO(EBADF,RMS$_IFI);
2187 DIE(aTHX_ PL_no_func, "flock()");
2199 int protocol = POPi;
2205 io = gv ? GvIOn(gv) : NULL;
2208 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2209 report_evil_fh(gv, io, PL_op->op_type);
2211 do_close(gv, FALSE);
2212 SETERRNO(EBADF,LIB$_INVARG);
2217 do_close(gv, FALSE);
2219 TAINT_PROPER("socket");
2220 fd = PerlSock_socket(domain, type, protocol);
2223 IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */
2224 IoOFP(io) = PerlIO_fdopen(fd, "w");
2225 IoTYPE(io) = IoTYPE_SOCKET;
2226 if (!IoIFP(io) || !IoOFP(io)) {
2227 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2228 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2229 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2232 #if defined(HAS_FCNTL) && defined(F_SETFD)
2233 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2237 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2242 DIE(aTHX_ PL_no_sock_func, "socket");
2248 #ifdef HAS_SOCKETPAIR
2254 int protocol = POPi;
2261 io1 = gv1 ? GvIOn(gv1) : NULL;
2262 io2 = gv2 ? GvIOn(gv2) : NULL;
2263 if (!gv1 || !gv2 || !io1 || !io2) {
2264 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2266 report_evil_fh(gv1, io1, PL_op->op_type);
2268 report_evil_fh(gv1, io2, PL_op->op_type);
2271 do_close(gv1, FALSE);
2273 do_close(gv2, FALSE);
2278 do_close(gv1, FALSE);
2280 do_close(gv2, FALSE);
2282 TAINT_PROPER("socketpair");
2283 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2285 IoIFP(io1) = PerlIO_fdopen(fd[0], "r");
2286 IoOFP(io1) = PerlIO_fdopen(fd[0], "w");
2287 IoTYPE(io1) = IoTYPE_SOCKET;
2288 IoIFP(io2) = PerlIO_fdopen(fd[1], "r");
2289 IoOFP(io2) = PerlIO_fdopen(fd[1], "w");
2290 IoTYPE(io2) = IoTYPE_SOCKET;
2291 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2292 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2293 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2294 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2295 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2296 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2297 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2300 #if defined(HAS_FCNTL) && defined(F_SETFD)
2301 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2302 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2307 DIE(aTHX_ PL_no_sock_func, "socketpair");
2315 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2316 extern void GETPRIVMODE();
2317 extern void GETUSERMODE();
2322 register IO *io = GvIOn(gv);
2326 int mpeprivmode = 0;
2329 if (!io || !IoIFP(io))
2332 addr = SvPV(addrsv, len);
2333 TAINT_PROPER("bind");
2334 #ifdef MPE /* Deal with MPE bind() peculiarities */
2335 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2336 /* The address *MUST* stupidly be zero. */
2337 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2338 /* PRIV mode is required to bind() to ports < 1024. */
2339 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2340 ((struct sockaddr_in *)addr)->sin_port > 0) {
2341 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2346 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2347 (struct sockaddr *)addr, len) >= 0)
2350 #ifdef MPE /* Switch back to USER mode */
2361 if (ckWARN(WARN_CLOSED))
2362 report_evil_fh(gv, io, PL_op->op_type);
2363 SETERRNO(EBADF,SS$_IVCHAN);
2366 DIE(aTHX_ PL_no_sock_func, "bind");
2377 register IO *io = GvIOn(gv);
2380 if (!io || !IoIFP(io))
2383 addr = SvPV(addrsv, len);
2384 TAINT_PROPER("connect");
2385 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2391 if (ckWARN(WARN_CLOSED))
2392 report_evil_fh(gv, io, PL_op->op_type);
2393 SETERRNO(EBADF,SS$_IVCHAN);
2396 DIE(aTHX_ PL_no_sock_func, "connect");
2406 register IO *io = gv ? GvIOn(gv) : NULL;
2408 if (!gv || !io || !IoIFP(io))
2411 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2417 if (ckWARN(WARN_CLOSED))
2418 report_evil_fh(gv, io, PL_op->op_type);
2419 SETERRNO(EBADF,SS$_IVCHAN);
2422 DIE(aTHX_ PL_no_sock_func, "listen");
2434 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2435 Sock_size_t len = sizeof saddr;
2447 if (!gstio || !IoIFP(gstio))
2452 do_close(ngv, FALSE);
2454 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2457 IoIFP(nstio) = PerlIO_fdopen(fd, "r");
2458 IoOFP(nstio) = PerlIO_fdopen(fd, "w");
2459 IoTYPE(nstio) = IoTYPE_SOCKET;
2460 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2461 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2462 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2463 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2466 #if defined(HAS_FCNTL) && defined(F_SETFD)
2467 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2471 len = sizeof saddr; /* EPOC somehow truncates info */
2472 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2475 PUSHp((char *)&saddr, len);
2479 if (ckWARN(WARN_CLOSED))
2480 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2481 SETERRNO(EBADF,SS$_IVCHAN);
2487 DIE(aTHX_ PL_no_sock_func, "accept");
2497 register IO *io = GvIOn(gv);
2499 if (!io || !IoIFP(io))
2502 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2506 if (ckWARN(WARN_CLOSED))
2507 report_evil_fh(gv, io, PL_op->op_type);
2508 SETERRNO(EBADF,SS$_IVCHAN);
2511 DIE(aTHX_ PL_no_sock_func, "shutdown");
2518 return pp_ssockopt();
2520 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2528 int optype = PL_op->op_type;
2531 unsigned int optname;
2537 if (optype == OP_GSOCKOPT)
2538 sv = sv_2mortal(NEWSV(22, 257));
2541 optname = (unsigned int) POPi;
2542 lvl = (unsigned int) POPi;
2546 if (!io || !IoIFP(io))
2549 fd = PerlIO_fileno(IoIFP(io));
2553 (void)SvPOK_only(sv);
2557 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2572 aint = (int)SvIV(sv);
2576 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2585 if (ckWARN(WARN_CLOSED))
2586 report_evil_fh(gv, io, optype);
2587 SETERRNO(EBADF,SS$_IVCHAN);
2592 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2599 return pp_getpeername();
2601 DIE(aTHX_ PL_no_sock_func, "getsockname");
2609 int optype = PL_op->op_type;
2613 register IO *io = GvIOn(gv);
2616 if (!io || !IoIFP(io))
2619 sv = sv_2mortal(NEWSV(22, 257));
2620 (void)SvPOK_only(sv);
2624 fd = PerlIO_fileno(IoIFP(io));
2626 case OP_GETSOCKNAME:
2627 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2630 case OP_GETPEERNAME:
2631 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2633 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2635 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";
2636 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2637 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2638 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2639 sizeof(u_short) + sizeof(struct in_addr))) {
2646 #ifdef BOGUS_GETNAME_RETURN
2647 /* Interactive Unix, getpeername() and getsockname()
2648 does not return valid namelen */
2649 if (len == BOGUS_GETNAME_RETURN)
2650 len = sizeof(struct sockaddr);
2658 if (ckWARN(WARN_CLOSED))
2659 report_evil_fh(gv, io, optype);
2660 SETERRNO(EBADF,SS$_IVCHAN);
2665 DIE(aTHX_ PL_no_sock_func, "getpeername");
2684 if (PL_op->op_flags & OPf_REF) {
2686 if (PL_op->op_type == OP_LSTAT) {
2687 if (PL_laststype != OP_LSTAT)
2688 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2689 if (ckWARN(WARN_IO) && gv != PL_defgv)
2690 Perl_warner(aTHX_ WARN_IO,
2691 "lstat() on filehandle %s", GvENAME(gv));
2692 /* Perl_my_lstat (-l) croak's on filehandle, why warn here? */
2696 if (gv != PL_defgv) {
2697 PL_laststype = OP_STAT;
2699 sv_setpv(PL_statname, "");
2700 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2701 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2703 if (PL_laststatval < 0) {
2704 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2705 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2711 if (SvTYPE(sv) == SVt_PVGV) {
2715 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2719 sv_setpv(PL_statname, SvPV(sv,n_a));
2722 PL_laststype = PL_op->op_type;
2723 if (PL_op->op_type == OP_LSTAT)
2724 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2727 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2728 if (PL_laststatval < 0) {
2729 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2730 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
2736 if (gimme != G_ARRAY) {
2737 if (gimme != G_VOID)
2738 XPUSHs(boolSV(max));
2744 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2745 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2746 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2747 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2748 #if Uid_t_size > IVSIZE
2749 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2751 # if Uid_t_sign <= 0
2752 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2754 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2757 #if Gid_t_size > IVSIZE
2758 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2760 # if Gid_t_sign <= 0
2761 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2763 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2766 #ifdef USE_STAT_RDEV
2767 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2769 PUSHs(sv_2mortal(newSVpvn("", 0)));
2771 #if Off_t_size > IVSIZE
2772 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size)));
2774 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2777 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2778 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2779 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2781 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2782 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2783 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2785 #ifdef USE_STAT_BLOCKS
2786 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2787 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2789 PUSHs(sv_2mortal(newSVpvn("", 0)));
2790 PUSHs(sv_2mortal(newSVpvn("", 0)));
2800 #if defined(HAS_ACCESS) && defined(R_OK)
2802 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2803 result = access(TOPpx, R_OK);
2818 if (cando(S_IRUSR, 0, &PL_statcache))
2827 #if defined(HAS_ACCESS) && defined(W_OK)
2829 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2830 result = access(TOPpx, W_OK);
2845 if (cando(S_IWUSR, 0, &PL_statcache))
2854 #if defined(HAS_ACCESS) && defined(X_OK)
2856 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2857 result = access(TOPpx, X_OK);
2872 if (cando(S_IXUSR, 0, &PL_statcache))
2881 #ifdef PERL_EFF_ACCESS_R_OK
2883 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2884 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2899 if (cando(S_IRUSR, 1, &PL_statcache))
2908 #ifdef PERL_EFF_ACCESS_W_OK
2910 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2911 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2926 if (cando(S_IWUSR, 1, &PL_statcache))
2935 #ifdef PERL_EFF_ACCESS_X_OK
2937 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2938 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2953 if (cando(S_IXUSR, 1, &PL_statcache))
2960 I32 result = my_stat();
2969 return pp_ftrowned();
2974 I32 result = my_stat();
2978 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
2986 I32 result = my_stat();
2990 if (PL_statcache.st_size == 0)
2997 I32 result = my_stat();
3001 #if Off_t_size > IVSIZE
3002 PUSHn(PL_statcache.st_size);
3004 PUSHi(PL_statcache.st_size);
3011 I32 result = my_stat();
3015 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3021 I32 result = my_stat();
3025 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3031 I32 result = my_stat();
3035 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3041 I32 result = my_stat();
3045 if (S_ISSOCK(PL_statcache.st_mode))
3052 I32 result = my_stat();
3056 if (S_ISCHR(PL_statcache.st_mode))
3063 I32 result = my_stat();
3067 if (S_ISBLK(PL_statcache.st_mode))
3074 I32 result = my_stat();
3078 if (S_ISREG(PL_statcache.st_mode))
3085 I32 result = my_stat();
3089 if (S_ISDIR(PL_statcache.st_mode))
3096 I32 result = my_stat();
3100 if (S_ISFIFO(PL_statcache.st_mode))
3107 I32 result = my_lstat();
3111 if (S_ISLNK(PL_statcache.st_mode))
3120 I32 result = my_stat();
3124 if (PL_statcache.st_mode & S_ISUID)
3134 I32 result = my_stat();
3138 if (PL_statcache.st_mode & S_ISGID)
3148 I32 result = my_stat();
3152 if (PL_statcache.st_mode & S_ISVTX)
3163 char *tmps = Nullch;
3166 if (PL_op->op_flags & OPf_REF)
3168 else if (isGV(TOPs))
3170 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3171 gv = (GV*)SvRV(POPs);
3173 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3175 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3176 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3177 else if (tmps && isDIGIT(*tmps))
3181 if (PerlLIO_isatty(fd))
3186 #if defined(atarist) /* this will work with atariST. Configure will
3187 make guesses for other systems. */
3188 # define FILE_base(f) ((f)->_base)
3189 # define FILE_ptr(f) ((f)->_ptr)
3190 # define FILE_cnt(f) ((f)->_cnt)
3191 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3201 register STDCHAR *s;
3208 if (PL_op->op_flags & OPf_REF)
3210 else if (isGV(TOPs))
3212 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3213 gv = (GV*)SvRV(POPs);
3219 if (gv == PL_defgv) {
3221 io = GvIO(PL_statgv);
3224 goto really_filename;
3229 PL_laststatval = -1;
3230 sv_setpv(PL_statname, "");
3231 io = GvIO(PL_statgv);
3233 if (io && IoIFP(io)) {
3234 if (! PerlIO_has_base(IoIFP(io)))
3235 DIE(aTHX_ "-T and -B not implemented on filehandles");
3236 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3237 if (PL_laststatval < 0)
3239 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3240 if (PL_op->op_type == OP_FTTEXT)
3245 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3246 i = PerlIO_getc(IoIFP(io));
3248 (void)PerlIO_ungetc(IoIFP(io),i);
3250 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3252 len = PerlIO_get_bufsiz(IoIFP(io));
3253 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3254 /* sfio can have large buffers - limit to 512 */
3259 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3261 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3263 SETERRNO(EBADF,RMS$_IFI);
3271 PL_laststatval = -1;
3272 sv_setpv(PL_statname, SvPV(sv, n_a));
3273 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3274 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3275 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
3278 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3279 if (PL_laststatval < 0) {
3280 (void)PerlIO_close(fp);
3283 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3284 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3285 (void)PerlIO_close(fp);
3287 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3288 RETPUSHNO; /* special case NFS directories */
3289 RETPUSHYES; /* null file is anything */
3294 /* now scan s to look for textiness */
3295 /* XXX ASCII dependent code */
3297 #if defined(DOSISH) || defined(USEMYBINMODE)
3298 /* ignore trailing ^Z on short files */
3299 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3303 for (i = 0; i < len; i++, s++) {
3304 if (!*s) { /* null never allowed in text */
3309 else if (!(isPRINT(*s) || isSPACE(*s)))
3312 else if (*s & 128) {
3314 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3317 /* utf8 characters don't count as odd */
3318 if (UTF8_IS_START(*s)) {
3319 int ulen = UTF8SKIP(s);
3320 if (ulen < len - i) {
3322 for (j = 1; j < ulen; j++) {
3323 if (!UTF8_IS_CONTINUATION(s[j]))
3326 --ulen; /* loop does extra increment */
3336 *s != '\n' && *s != '\r' && *s != '\b' &&
3337 *s != '\t' && *s != '\f' && *s != 27)
3342 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3366 if (!tmps || !*tmps) {
3367 svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE);
3369 tmps = SvPV(*svp, n_a);
3371 if (!tmps || !*tmps) {
3372 svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE);
3374 tmps = SvPV(*svp, n_a);
3377 if (!tmps || !*tmps) {
3378 svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE);
3380 tmps = SvPV(*svp, n_a);
3383 TAINT_PROPER("chdir");
3384 PUSHi( PerlDir_chdir(tmps) >= 0 );
3386 /* Clear the DEFAULT element of ENV so we'll get the new value
3388 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3396 dSP; dMARK; dTARGET;
3397 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3403 DIE(aTHX_ PL_no_func, "chown");
3413 TAINT_PROPER("chroot");
3414 PUSHi( chroot(tmps) >= 0 );
3417 DIE(aTHX_ PL_no_func, "chroot");
3423 dSP; dMARK; dTARGET;
3425 value = (I32)apply(PL_op->op_type, MARK, SP);
3433 dSP; dMARK; dTARGET;
3435 value = (I32)apply(PL_op->op_type, MARK, SP);
3443 dSP; dMARK; dTARGET;
3445 value = (I32)apply(PL_op->op_type, MARK, SP);
3457 char *tmps2 = POPpx;
3458 char *tmps = SvPV(TOPs, n_a);
3459 TAINT_PROPER("rename");
3461 anum = PerlLIO_rename(tmps, tmps2);
3463 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3464 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3467 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3468 (void)UNLINK(tmps2);
3469 if (!(anum = link(tmps, tmps2)))
3470 anum = UNLINK(tmps);
3483 char *tmps2 = POPpx;
3484 char *tmps = SvPV(TOPs, n_a);
3485 TAINT_PROPER("link");
3486 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3488 DIE(aTHX_ PL_no_func, "link");
3498 char *tmps2 = POPpx;
3499 char *tmps = SvPV(TOPs, n_a);
3500 TAINT_PROPER("symlink");
3501 SETi( symlink(tmps, tmps2) >= 0 );
3504 DIE(aTHX_ PL_no_func, "symlink");
3514 char buf[MAXPATHLEN];
3518 #ifndef INCOMPLETE_TAINTS
3522 len = readlink(tmps, buf, sizeof(buf) - 1);
3530 RETSETUNDEF; /* just pretend it's a normal file */
3534 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3536 S_dooneliner(pTHX_ char *cmd, char *filename)
3538 char *save_filename = filename;
3544 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3545 strcpy(cmdline, cmd);
3546 strcat(cmdline, " ");
3547 for (s = cmdline + strlen(cmdline); *filename; ) {
3552 myfp = PerlProc_popen(cmdline, "r");
3556 SV *tmpsv = sv_newmortal();
3557 /* Need to save/restore 'PL_rs' ?? */
3558 s = sv_gets(tmpsv, myfp, 0);
3559 (void)PerlProc_pclose(myfp);
3563 #ifdef HAS_SYS_ERRLIST
3568 /* you don't see this */
3570 #ifdef HAS_SYS_ERRLIST
3578 if (instr(s, errmsg)) {
3585 #define EACCES EPERM
3587 if (instr(s, "cannot make"))
3588 SETERRNO(EEXIST,RMS$_FEX);
3589 else if (instr(s, "existing file"))
3590 SETERRNO(EEXIST,RMS$_FEX);
3591 else if (instr(s, "ile exists"))
3592 SETERRNO(EEXIST,RMS$_FEX);
3593 else if (instr(s, "non-exist"))
3594 SETERRNO(ENOENT,RMS$_FNF);
3595 else if (instr(s, "does not exist"))
3596 SETERRNO(ENOENT,RMS$_FNF);
3597 else if (instr(s, "not empty"))
3598 SETERRNO(EBUSY,SS$_DEVOFFLINE);
3599 else if (instr(s, "cannot access"))
3600 SETERRNO(EACCES,RMS$_PRV);
3602 SETERRNO(EPERM,RMS$_PRV);
3605 else { /* some mkdirs return no failure indication */
3606 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3607 if (PL_op->op_type == OP_RMDIR)
3612 SETERRNO(EACCES,RMS$_PRV); /* a guess */
3637 tmps = SvPV(TOPs, len);
3638 /* Different operating and file systems take differently to
3639 * trailing slashes. According to POSIX 1003.1 1996 Edition
3640 * any number of trailing slashes should be allowed.
3641 * Thusly we snip them away so that even non-conforming
3642 * systems are happy. */
3643 /* We should probably do this "filtering" for all
3644 * the functions that expect (potentially) directory names:
3645 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3646 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3647 if (len > 1 && tmps[len-1] == '/') {
3648 while (tmps[len] == '/' && len > 1)
3650 tmps = savepvn(tmps, len);
3654 TAINT_PROPER("mkdir");
3656 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3658 SETi( dooneliner("mkdir", tmps) );
3659 oldumask = PerlLIO_umask(0);
3660 PerlLIO_umask(oldumask);
3661 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3675 TAINT_PROPER("rmdir");
3677 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3679 XPUSHi( dooneliner("rmdir", tmps) );
3684 /* Directory calls. */
3688 #if defined(Direntry_t) && defined(HAS_READDIR)
3691 char *dirname = POPpx;
3693 register IO *io = GvIOn(gv);
3699 PerlDir_close(IoDIRP(io));
3700 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3706 SETERRNO(EBADF,RMS$_DIR);
3709 DIE(aTHX_ PL_no_dir_func, "opendir");
3715 #if defined(Direntry_t) && defined(HAS_READDIR)
3717 #if !defined(I_DIRENT) && !defined(VMS)
3718 Direntry_t *readdir (DIR *);
3720 register Direntry_t *dp;
3722 register IO *io = GvIOn(gv);
3725 if (!io || !IoDIRP(io))
3728 if (GIMME == G_ARRAY) {
3730 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3732 sv = newSVpvn(dp->d_name, dp->d_namlen);
3734 sv = newSVpv(dp->d_name, 0);
3736 #ifndef INCOMPLETE_TAINTS
3737 if (!(IoFLAGS(io) & IOf_UNTAINT))
3740 XPUSHs(sv_2mortal(sv));
3744 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3747 sv = newSVpvn(dp->d_name, dp->d_namlen);
3749 sv = newSVpv(dp->d_name, 0);
3751 #ifndef INCOMPLETE_TAINTS
3752 if (!(IoFLAGS(io) & IOf_UNTAINT))
3755 XPUSHs(sv_2mortal(sv));
3761 SETERRNO(EBADF,RMS$_ISI);
3762 if (GIMME == G_ARRAY)
3767 DIE(aTHX_ PL_no_dir_func, "readdir");
3773 #if defined(HAS_TELLDIR) || defined(telldir)
3775 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3776 /* XXX netbsd still seemed to.
3777 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3778 --JHI 1999-Feb-02 */
3779 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3780 long telldir (DIR *);
3783 register IO *io = GvIOn(gv);
3785 if (!io || !IoDIRP(io))
3788 PUSHi( PerlDir_tell(IoDIRP(io)) );
3792 SETERRNO(EBADF,RMS$_ISI);
3795 DIE(aTHX_ PL_no_dir_func, "telldir");
3801 #if defined(HAS_SEEKDIR) || defined(seekdir)
3805 register IO *io = GvIOn(gv);
3807 if (!io || !IoDIRP(io))
3810 (void)PerlDir_seek(IoDIRP(io), along);
3815 SETERRNO(EBADF,RMS$_ISI);
3818 DIE(aTHX_ PL_no_dir_func, "seekdir");
3824 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3827 register IO *io = GvIOn(gv);
3829 if (!io || !IoDIRP(io))
3832 (void)PerlDir_rewind(IoDIRP(io));
3836 SETERRNO(EBADF,RMS$_ISI);
3839 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3845 #if defined(Direntry_t) && defined(HAS_READDIR)
3848 register IO *io = GvIOn(gv);
3850 if (!io || !IoDIRP(io))
3853 #ifdef VOID_CLOSEDIR
3854 PerlDir_close(IoDIRP(io));
3856 if (PerlDir_close(IoDIRP(io)) < 0) {
3857 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3866 SETERRNO(EBADF,RMS$_IFI);
3869 DIE(aTHX_ PL_no_dir_func, "closedir");
3873 /* Process control. */
3883 PERL_FLUSHALL_FOR_CHILD;
3889 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV)))
3890 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3891 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3896 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3901 PERL_FLUSHALL_FOR_CHILD;
3902 childpid = PerlProc_fork();
3908 DIE(aTHX_ PL_no_func, "fork");
3915 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3920 #ifdef PERL_OLD_SIGNALS
3921 childpid = wait4pid(-1, &argflags, 0);
3923 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 && errno == EINTR) {
3927 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3928 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3929 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3931 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3936 DIE(aTHX_ PL_no_func, "wait");
3942 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3950 #ifdef PERL_OLD_SIGNALS
3951 childpid = wait4pid(childpid, &argflags, optype);
3953 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 && errno == EINTR) {
3957 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3958 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3959 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3961 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3966 DIE(aTHX_ PL_no_func, "waitpid");
3972 dSP; dMARK; dORIGMARK; dTARGET;
3979 if (SP - MARK == 1) {
3981 (void)SvPV_nolen(TOPs); /* stringify for taint check */
3983 TAINT_PROPER("system");
3986 PERL_FLUSHALL_FOR_CHILD;
3987 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
3991 Sigsave_t ihand,qhand; /* place to save signals during system() */
3993 if (PerlProc_pipe(pp) >= 0)
3995 while ((childpid = vfork()) == -1) {
3996 if (errno != EAGAIN) {
4001 PerlLIO_close(pp[0]);
4002 PerlLIO_close(pp[1]);
4010 PerlLIO_close(pp[1]);
4012 rsignal_save(SIGINT, SIG_IGN, &ihand);
4013 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4016 result = wait4pid(childpid, &status, 0);
4017 } while (result == -1 && errno == EINTR);
4019 (void)rsignal_restore(SIGINT, &ihand);
4020 (void)rsignal_restore(SIGQUIT, &qhand);
4022 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4023 do_execfree(); /* free any memory child malloced on vfork */
4029 while (n < sizeof(int)) {
4030 n1 = PerlLIO_read(pp[0],
4031 (void*)(((char*)&errkid)+n),
4037 PerlLIO_close(pp[0]);
4038 if (n) { /* Error */
4039 if (n != sizeof(int))
4040 DIE(aTHX_ "panic: kid popen errno read");
4041 errno = errkid; /* Propagate errno from kid */
4042 STATUS_CURRENT = -1;
4045 PUSHi(STATUS_CURRENT);
4049 PerlLIO_close(pp[0]);
4050 #if defined(HAS_FCNTL) && defined(F_SETFD)
4051 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4055 if (PL_op->op_flags & OPf_STACKED) {
4056 SV *really = *++MARK;
4057 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4059 else if (SP - MARK != 1)
4060 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4062 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4065 #else /* ! FORK or VMS or OS/2 */
4068 if (PL_op->op_flags & OPf_STACKED) {
4069 SV *really = *++MARK;
4070 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4072 else if (SP - MARK != 1)
4073 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4075 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4077 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4079 STATUS_NATIVE_SET(value);
4082 PUSHi(result ? value : STATUS_CURRENT);
4083 #endif /* !FORK or VMS */
4089 dSP; dMARK; dORIGMARK; dTARGET;
4093 PERL_FLUSHALL_FOR_CHILD;
4094 if (PL_op->op_flags & OPf_STACKED) {
4095 SV *really = *++MARK;
4096 value = (I32)do_aexec(really, MARK, SP);
4098 else if (SP - MARK != 1)
4100 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4104 (void ) do_aspawn(Nullsv, MARK, SP);
4108 value = (I32)do_aexec(Nullsv, MARK, SP);
4113 (void)SvPV_nolen(*SP); /* stringify for taint check */
4115 TAINT_PROPER("exec");
4118 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4121 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4124 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4129 #if !defined(HAS_FORK) && defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4142 dSP; dMARK; dTARGET;
4144 value = (I32)apply(PL_op->op_type, MARK, SP);
4149 DIE(aTHX_ PL_no_func, "kill");
4157 XPUSHi( getppid() );
4160 DIE(aTHX_ PL_no_func, "getppid");
4176 pgrp = (I32)BSD_GETPGRP(pid);
4178 if (pid != 0 && pid != PerlProc_getpid())
4179 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4185 DIE(aTHX_ PL_no_func, "getpgrp()");
4204 TAINT_PROPER("setpgrp");
4206 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4208 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4209 || (pid != 0 && pid != PerlProc_getpid()))
4211 DIE(aTHX_ "setpgrp can't take arguments");
4213 SETi( setpgrp() >= 0 );
4214 #endif /* USE_BSDPGRP */
4217 DIE(aTHX_ PL_no_func, "setpgrp()");
4223 #ifdef HAS_GETPRIORITY
4227 SETi( getpriority(which, who) );
4230 DIE(aTHX_ PL_no_func, "getpriority()");
4236 #ifdef HAS_SETPRIORITY
4241 TAINT_PROPER("setpriority");
4242 SETi( setpriority(which, who, niceval) >= 0 );
4245 DIE(aTHX_ PL_no_func, "setpriority()");
4255 XPUSHn( time(Null(Time_t*)) );
4257 XPUSHi( time(Null(Time_t*)) );
4262 /* XXX The POSIX name is CLK_TCK; it is to be preferred
4263 to HZ. Probably. For now, assume that if the system
4264 defines HZ, it does so correctly. (Will this break
4266 Probably we ought to use _sysconf(_SC_CLK_TCK), if
4267 it's supported. --AD 9/96.
4284 (void)PerlProc_times(&PL_timesbuf);
4286 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4287 /* struct tms, though same data */
4291 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
4292 if (GIMME == G_ARRAY) {
4293 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
4294 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
4295 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
4299 DIE(aTHX_ "times not implemented");
4300 #endif /* HAS_TIMES */
4313 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4314 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4315 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4321 when = (Time_t)SvNVx(POPs);
4323 when = (Time_t)SvIVx(POPs);
4326 if (PL_op->op_type == OP_LOCALTIME)
4327 tmbuf = localtime(&when);
4329 tmbuf = gmtime(&when);
4333 if (GIMME != G_ARRAY) {
4337 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4338 dayname[tmbuf->tm_wday],
4339 monname[tmbuf->tm_mon],
4344 tmbuf->tm_year + 1900);
4345 PUSHs(sv_2mortal(tsv));
4348 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4349 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4350 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4351 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4352 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4353 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4354 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4355 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4356 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4367 anum = alarm((unsigned int)anum);
4374 DIE(aTHX_ PL_no_func, "alarm");
4385 (void)time(&lasttime);
4390 PerlProc_sleep((unsigned int)duration);
4393 XPUSHi(when - lasttime);
4397 /* Shared memory. */
4411 return pp_shmwrite();
4416 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4417 dSP; dMARK; dTARGET;
4418 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4427 /* Message passing. */
4441 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4442 dSP; dMARK; dTARGET;
4443 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4454 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4455 dSP; dMARK; dTARGET;
4456 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4469 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4470 dSP; dMARK; dTARGET;
4471 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4478 DIE(aTHX_ "System V IPC is not implemented on this machine");
4484 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4485 dSP; dMARK; dTARGET;
4486 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4494 PUSHp(zero_but_true, ZBTLEN);
4504 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4505 dSP; dMARK; dTARGET;
4506 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4515 /* Get system info. */
4519 #ifdef HAS_GETHOSTBYNAME
4520 return pp_ghostent();
4522 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4528 #ifdef HAS_GETHOSTBYADDR
4529 return pp_ghostent();
4531 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4537 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4539 I32 which = PL_op->op_type;
4540 register char **elem;
4542 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4543 struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4544 struct hostent *PerlSock_gethostbyname(Netdb_name_t);
4545 struct hostent *PerlSock_gethostent(void);
4547 struct hostent *hent;
4552 if (which == OP_GHBYNAME)
4553 #ifdef HAS_GETHOSTBYNAME
4554 hent = PerlSock_gethostbyname(POPpbytex);
4556 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4558 else if (which == OP_GHBYADDR) {
4559 #ifdef HAS_GETHOSTBYADDR
4560 int addrtype = POPi;
4563 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4565 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4567 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4571 #ifdef HAS_GETHOSTENT
4572 hent = PerlSock_gethostent();
4574 DIE(aTHX_ PL_no_sock_func, "gethostent");
4577 #ifdef HOST_NOT_FOUND
4579 STATUS_NATIVE_SET(h_errno);
4582 if (GIMME != G_ARRAY) {
4583 PUSHs(sv = sv_newmortal());
4585 if (which == OP_GHBYNAME) {
4587 sv_setpvn(sv, hent->h_addr, hent->h_length);
4590 sv_setpv(sv, (char*)hent->h_name);
4596 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4597 sv_setpv(sv, (char*)hent->h_name);
4598 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4599 for (elem = hent->h_aliases; elem && *elem; elem++) {
4600 sv_catpv(sv, *elem);
4602 sv_catpvn(sv, " ", 1);
4604 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4605 sv_setiv(sv, (IV)hent->h_addrtype);
4606 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4607 len = hent->h_length;
4608 sv_setiv(sv, (IV)len);
4610 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4611 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4612 sv_setpvn(sv, *elem, len);
4615 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4617 sv_setpvn(sv, hent->h_addr, len);
4622 DIE(aTHX_ PL_no_sock_func, "gethostent");
4628 #ifdef HAS_GETNETBYNAME
4629 return pp_gnetent();
4631 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4637 #ifdef HAS_GETNETBYADDR
4638 return pp_gnetent();
4640 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4646 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4648 I32 which = PL_op->op_type;
4649 register char **elem;
4651 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4652 struct netent *PerlSock_getnetbyaddr(Netdb_net_t, int);
4653 struct netent *PerlSock_getnetbyname(Netdb_name_t);
4654 struct netent *PerlSock_getnetent(void);
4656 struct netent *nent;
4659 if (which == OP_GNBYNAME)
4660 #ifdef HAS_GETNETBYNAME
4661 nent = PerlSock_getnetbyname(POPpbytex);
4663 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4665 else if (which == OP_GNBYADDR) {
4666 #ifdef HAS_GETNETBYADDR
4667 int addrtype = POPi;
4668 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4669 nent = PerlSock_getnetbyaddr(addr, addrtype);
4671 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4675 #ifdef HAS_GETNETENT
4676 nent = PerlSock_getnetent();
4678 DIE(aTHX_ PL_no_sock_func, "getnetent");
4682 if (GIMME != G_ARRAY) {
4683 PUSHs(sv = sv_newmortal());
4685 if (which == OP_GNBYNAME)
4686 sv_setiv(sv, (IV)nent->n_net);
4688 sv_setpv(sv, nent->n_name);
4694 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4695 sv_setpv(sv, nent->n_name);
4696 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4697 for (elem = nent->n_aliases; elem && *elem; elem++) {
4698 sv_catpv(sv, *elem);
4700 sv_catpvn(sv, " ", 1);
4702 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4703 sv_setiv(sv, (IV)nent->n_addrtype);
4704 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4705 sv_setiv(sv, (IV)nent->n_net);
4710 DIE(aTHX_ PL_no_sock_func, "getnetent");
4716 #ifdef HAS_GETPROTOBYNAME
4717 return pp_gprotoent();
4719 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4725 #ifdef HAS_GETPROTOBYNUMBER
4726 return pp_gprotoent();
4728 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4734 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4736 I32 which = PL_op->op_type;
4737 register char **elem;
4739 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4740 struct protoent *PerlSock_getprotobyname(Netdb_name_t);
4741 struct protoent *PerlSock_getprotobynumber(int);
4742 struct protoent *PerlSock_getprotoent(void);
4744 struct protoent *pent;
4747 if (which == OP_GPBYNAME)
4748 #ifdef HAS_GETPROTOBYNAME
4749 pent = PerlSock_getprotobyname(POPpbytex);
4751 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4753 else if (which == OP_GPBYNUMBER)
4754 #ifdef HAS_GETPROTOBYNUMBER
4755 pent = PerlSock_getprotobynumber(POPi);
4757 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4760 #ifdef HAS_GETPROTOENT
4761 pent = PerlSock_getprotoent();
4763 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4767 if (GIMME != G_ARRAY) {
4768 PUSHs(sv = sv_newmortal());
4770 if (which == OP_GPBYNAME)
4771 sv_setiv(sv, (IV)pent->p_proto);
4773 sv_setpv(sv, pent->p_name);
4779 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4780 sv_setpv(sv, pent->p_name);
4781 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4782 for (elem = pent->p_aliases; elem && *elem; elem++) {
4783 sv_catpv(sv, *elem);
4785 sv_catpvn(sv, " ", 1);
4787 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4788 sv_setiv(sv, (IV)pent->p_proto);
4793 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4799 #ifdef HAS_GETSERVBYNAME
4800 return pp_gservent();
4802 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4808 #ifdef HAS_GETSERVBYPORT
4809 return pp_gservent();
4811 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4817 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4819 I32 which = PL_op->op_type;
4820 register char **elem;
4822 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4823 struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t);
4824 struct servent *PerlSock_getservbyport(int, Netdb_name_t);
4825 struct servent *PerlSock_getservent(void);
4827 struct servent *sent;
4830 if (which == OP_GSBYNAME) {
4831 #ifdef HAS_GETSERVBYNAME
4832 char *proto = POPpbytex;
4833 char *name = POPpbytex;
4835 if (proto && !*proto)
4838 sent = PerlSock_getservbyname(name, proto);
4840 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4843 else if (which == OP_GSBYPORT) {
4844 #ifdef HAS_GETSERVBYPORT
4845 char *proto = POPpbytex;
4846 unsigned short port = POPu;
4849 port = PerlSock_htons(port);
4851 sent = PerlSock_getservbyport(port, proto);
4853 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4857 #ifdef HAS_GETSERVENT
4858 sent = PerlSock_getservent();
4860 DIE(aTHX_ PL_no_sock_func, "getservent");
4864 if (GIMME != G_ARRAY) {
4865 PUSHs(sv = sv_newmortal());
4867 if (which == OP_GSBYNAME) {
4869 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4871 sv_setiv(sv, (IV)(sent->s_port));
4875 sv_setpv(sv, sent->s_name);
4881 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4882 sv_setpv(sv, sent->s_name);
4883 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4884 for (elem = sent->s_aliases; elem && *elem; elem++) {
4885 sv_catpv(sv, *elem);
4887 sv_catpvn(sv, " ", 1);
4889 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4891 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4893 sv_setiv(sv, (IV)(sent->s_port));
4895 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4896 sv_setpv(sv, sent->s_proto);
4901 DIE(aTHX_ PL_no_sock_func, "getservent");
4907 #ifdef HAS_SETHOSTENT
4909 PerlSock_sethostent(TOPi);
4912 DIE(aTHX_ PL_no_sock_func, "sethostent");
4918 #ifdef HAS_SETNETENT
4920 PerlSock_setnetent(TOPi);
4923 DIE(aTHX_ PL_no_sock_func, "setnetent");
4929 #ifdef HAS_SETPROTOENT
4931 PerlSock_setprotoent(TOPi);
4934 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4940 #ifdef HAS_SETSERVENT
4942 PerlSock_setservent(TOPi);
4945 DIE(aTHX_ PL_no_sock_func, "setservent");
4951 #ifdef HAS_ENDHOSTENT
4953 PerlSock_endhostent();
4957 DIE(aTHX_ PL_no_sock_func, "endhostent");
4963 #ifdef HAS_ENDNETENT
4965 PerlSock_endnetent();
4969 DIE(aTHX_ PL_no_sock_func, "endnetent");
4975 #ifdef HAS_ENDPROTOENT
4977 PerlSock_endprotoent();
4981 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4987 #ifdef HAS_ENDSERVENT
4989 PerlSock_endservent();
4993 DIE(aTHX_ PL_no_sock_func, "endservent");
5002 DIE(aTHX_ PL_no_func, "getpwnam");
5011 DIE(aTHX_ PL_no_func, "getpwuid");
5019 I32 which = PL_op->op_type;
5022 struct passwd *pwent = NULL;
5024 * We currently support only the SysV getsp* shadow password interface.
5025 * The interface is declared in <shadow.h> and often one needs to link
5026 * with -lsecurity or some such.
5027 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5030 * AIX getpwnam() is clever enough to return the encrypted password
5031 * only if the caller (euid?) is root.
5033 * There are at least two other shadow password APIs. Many platforms
5034 * seem to contain more than one interface for accessing the shadow
5035 * password databases, possibly for compatibility reasons.
5036 * The getsp*() is by far he simplest one, the other two interfaces
5037 * are much more complicated, but also very similar to each other.
5042 * struct pr_passwd *getprpw*();
5043 * The password is in
5044 * char getprpw*(...).ufld.fd_encrypt[]
5045 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5050 * struct es_passwd *getespw*();
5051 * The password is in
5052 * char *(getespw*(...).ufld.fd_encrypt)
5053 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5055 * Mention I_PROT here so that Configure probes for it.
5057 * In HP-UX for getprpw*() the manual page claims that one should include
5058 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5059 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5060 * and pp_sys.c already includes <shadow.h> if there is such.
5062 * Note that <sys/security.h> is already probed for, but currently
5063 * it is only included in special cases.
5065 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5066 * be preferred interface, even though also the getprpw*() interface
5067 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5068 * One also needs to call set_auth_parameters() in main() before
5069 * doing anything else, whether one is using getespw*() or getprpw*().
5071 * Note that accessing the shadow databases can be magnitudes
5072 * slower than accessing the standard databases.
5079 pwent = getpwnam(POPpbytex);
5082 pwent = getpwuid((Uid_t)POPi);
5085 # ifdef HAS_GETPWENT
5088 DIE(aTHX_ PL_no_func, "getpwent");
5094 if (GIMME != G_ARRAY) {
5095 PUSHs(sv = sv_newmortal());
5097 if (which == OP_GPWNAM)
5098 # if Uid_t_sign <= 0
5099 sv_setiv(sv, (IV)pwent->pw_uid);
5101 sv_setuv(sv, (UV)pwent->pw_uid);
5104 sv_setpv(sv, pwent->pw_name);
5110 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5111 sv_setpv(sv, pwent->pw_name);
5113 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5115 /* If we have getspnam(), we try to dig up the shadow
5116 * password. If we are underprivileged, the shadow
5117 * interface will set the errno to EACCES or similar,
5118 * and return a null pointer. If this happens, we will
5119 * use the dummy password (usually "*" or "x") from the
5120 * standard password database.
5122 * In theory we could skip the shadow call completely
5123 * if euid != 0 but in practice we cannot know which
5124 * security measures are guarding the shadow databases
5125 * on a random platform.
5127 * Resist the urge to use additional shadow interfaces.
5128 * Divert the urge to writing an extension instead.
5131 # ifdef HAS_GETSPNAM
5133 struct spwd *spwent;
5134 int saverrno; /* Save and restore errno so that
5135 * underprivileged attempts seem
5136 * to have never made the unsccessful
5137 * attempt to retrieve the shadow password. */
5140 spwent = getspnam(pwent->pw_name);
5142 if (spwent && spwent->sp_pwdp)
5143 sv_setpv(sv, spwent->sp_pwdp);
5147 if (!SvPOK(sv)) /* Use the standard password, then. */
5148 sv_setpv(sv, pwent->pw_passwd);
5151 # ifndef INCOMPLETE_TAINTS
5152 /* passwd is tainted because user himself can diddle with it.
5153 * admittedly not much and in a very limited way, but nevertheless. */
5157 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5158 # if Uid_t_sign <= 0
5159 sv_setiv(sv, (IV)pwent->pw_uid);
5161 sv_setuv(sv, (UV)pwent->pw_uid);
5164 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5165 # if Uid_t_sign <= 0
5166 sv_setiv(sv, (IV)pwent->pw_gid);
5168 sv_setuv(sv, (UV)pwent->pw_gid);
5170 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5171 * because of the poor interface of the Perl getpw*(),
5172 * not because there's some standard/convention saying so.
5173 * A better interface would have been to return a hash,
5174 * but we are accursed by our history, alas. --jhi. */
5175 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5177 sv_setiv(sv, (IV)pwent->pw_change);
5180 sv_setiv(sv, (IV)pwent->pw_quota);
5183 sv_setpv(sv, pwent->pw_age);
5188 /* pw_class and pw_comment are mutually exclusive--.
5189 * see the above note for pw_change, pw_quota, and pw_age. */
5190 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5192 sv_setpv(sv, pwent->pw_class);
5195 sv_setpv(sv, pwent->pw_comment);
5199 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5201 sv_setpv(sv, pwent->pw_gecos);
5203 # ifndef INCOMPLETE_TAINTS
5204 /* pw_gecos is tainted because user himself can diddle with it. */
5208 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5209 sv_setpv(sv, pwent->pw_dir);
5211 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5212 sv_setpv(sv, pwent->pw_shell);
5213 # ifndef INCOMPLETE_TAINTS
5214 /* pw_shell is tainted because user himself can diddle with it. */
5219 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5220 sv_setiv(sv, (IV)pwent->pw_expire);
5225 DIE(aTHX_ PL_no_func, "getpwent");
5231 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5236 DIE(aTHX_ PL_no_func, "setpwent");
5242 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5247 DIE(aTHX_ PL_no_func, "endpwent");
5256 DIE(aTHX_ PL_no_func, "getgrnam");
5265 DIE(aTHX_ PL_no_func, "getgrgid");
5273 I32 which = PL_op->op_type;
5274 register char **elem;
5276 struct group *grent;
5279 if (which == OP_GGRNAM)
5280 grent = (struct group *)getgrnam(POPpbytex);
5281 else if (which == OP_GGRGID)
5282 grent = (struct group *)getgrgid(POPi);
5285 grent = (struct group *)getgrent();
5287 DIE(aTHX_ PL_no_func, "getgrent");
5291 if (GIMME != G_ARRAY) {
5292 PUSHs(sv = sv_newmortal());
5294 if (which == OP_GGRNAM)
5295 sv_setiv(sv, (IV)grent->gr_gid);
5297 sv_setpv(sv, grent->gr_name);
5303 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5304 sv_setpv(sv, grent->gr_name);
5306 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5308 sv_setpv(sv, grent->gr_passwd);
5311 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5312 sv_setiv(sv, (IV)grent->gr_gid);
5314 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5315 for (elem = grent->gr_mem; elem && *elem; elem++) {
5316 sv_catpv(sv, *elem);
5318 sv_catpvn(sv, " ", 1);
5324 DIE(aTHX_ PL_no_func, "getgrent");
5330 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5335 DIE(aTHX_ PL_no_func, "setgrent");
5341 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5346 DIE(aTHX_ PL_no_func, "endgrent");
5356 if (!(tmps = PerlProc_getlogin()))
5358 PUSHp(tmps, strlen(tmps));
5361 DIE(aTHX_ PL_no_func, "getlogin");
5365 /* Miscellaneous. */
5370 dSP; dMARK; dORIGMARK; dTARGET;
5371 register I32 items = SP - MARK;
5372 unsigned long a[20];
5378 while (++MARK <= SP) {
5379 if (SvTAINTED(*MARK)) {
5385 TAINT_PROPER("syscall");
5388 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5389 * or where sizeof(long) != sizeof(char*). But such machines will
5390 * not likely have syscall implemented either, so who cares?
5392 while (++MARK <= SP) {
5393 if (SvNIOK(*MARK) || !i)
5394 a[i++] = SvIV(*MARK);
5395 else if (*MARK == &PL_sv_undef)
5398 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5404 DIE(aTHX_ "Too many args to syscall");
5406 DIE(aTHX_ "Too few args to syscall");
5408 retval = syscall(a[0]);
5411 retval = syscall(a[0],a[1]);
5414 retval = syscall(a[0],a[1],a[2]);
5417 retval = syscall(a[0],a[1],a[2],a[3]);
5420 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5423 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5426 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5429 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5433 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5436 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5439 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5443 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5447 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5451 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5452 a[10],a[11],a[12],a[13]);
5454 #endif /* atarist */
5460 DIE(aTHX_ PL_no_func, "syscall");
5464 #ifdef FCNTL_EMULATE_FLOCK
5466 /* XXX Emulate flock() with fcntl().
5467 What's really needed is a good file locking module.
5471 fcntl_emulate_flock(int fd, int operation)
5475 switch (operation & ~LOCK_NB) {
5477 flock.l_type = F_RDLCK;
5480 flock.l_type = F_WRLCK;
5483 flock.l_type = F_UNLCK;
5489 flock.l_whence = SEEK_SET;
5490 flock.l_start = flock.l_len = (Off_t)0;
5492 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5495 #endif /* FCNTL_EMULATE_FLOCK */
5497 #ifdef LOCKF_EMULATE_FLOCK
5499 /* XXX Emulate flock() with lockf(). This is just to increase
5500 portability of scripts. The calls are not completely
5501 interchangeable. What's really needed is a good file
5505 /* The lockf() constants might have been defined in <unistd.h>.
5506 Unfortunately, <unistd.h> causes troubles on some mixed
5507 (BSD/POSIX) systems, such as SunOS 4.1.3.
5509 Further, the lockf() constants aren't POSIX, so they might not be
5510 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5511 just stick in the SVID values and be done with it. Sigh.
5515 # define F_ULOCK 0 /* Unlock a previously locked region */
5518 # define F_LOCK 1 /* Lock a region for exclusive use */
5521 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5524 # define F_TEST 3 /* Test a region for other processes locks */
5528 lockf_emulate_flock(int fd, int operation)
5534 /* flock locks entire file so for lockf we need to do the same */
5536 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5537 if (pos > 0) /* is seekable and needs to be repositioned */
5538 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5539 pos = -1; /* seek failed, so don't seek back afterwards */
5542 switch (operation) {
5544 /* LOCK_SH - get a shared lock */
5546 /* LOCK_EX - get an exclusive lock */
5548 i = lockf (fd, F_LOCK, 0);
5551 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5552 case LOCK_SH|LOCK_NB:
5553 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5554 case LOCK_EX|LOCK_NB:
5555 i = lockf (fd, F_TLOCK, 0);
5557 if ((errno == EAGAIN) || (errno == EACCES))
5558 errno = EWOULDBLOCK;
5561 /* LOCK_UN - unlock (non-blocking is a no-op) */
5563 case LOCK_UN|LOCK_NB:
5564 i = lockf (fd, F_ULOCK, 0);
5567 /* Default - can't decipher operation */
5574 if (pos > 0) /* need to restore position of the handle */
5575 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5580 #endif /* LOCKF_EMULATE_FLOCK */