3 * Copyright (c) 1991-2000, 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. --jhi */
26 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
27 * and another MAXINT from "perl.h" <- <sys/param.h>. */
33 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
40 extern "C" int syscall(unsigned long,...);
45 # include <sys/wait.h>
49 # include <sys/resource.h>
52 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
53 # include <sys/socket.h>
54 # if defined(USE_SOCKS) && defined(I_SOCKS)
62 # include <net/errno.h>
69 #include <sys/select.h>
73 /* XXX Configure test needed.
74 h_errno might not be a simple 'int', especially for multi-threaded
75 applications, see "extern int errno in perl.h". Creating such
76 a test requires taking into account the differences between
77 compiling multithreaded and singlethreaded ($ccflags et al).
78 HOST_NOT_FOUND is typically defined in <netdb.h>.
80 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
88 struct passwd *getpwnam (char *);
89 struct passwd *getpwuid (Uid_t);
92 struct passwd *getpwent (void);
100 struct group *getgrnam (char *);
101 struct group *getgrgid (Gid_t);
104 struct group *getgrent (void);
109 # if defined(_MSC_VER) || defined(__MINGW32__)
110 # include <sys/utime.h>
116 /* Put this after #includes because fork and vfork prototypes may conflict. */
122 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
125 # define my_chsize PerlLIO_chsize
130 #else /* no flock() */
132 /* fcntl.h might not have been included, even if it exists, because
133 the current Configure only sets I_FCNTL if it's needed to pick up
134 the *_OK constants. Make sure it has been included before testing
135 the fcntl() locking constants. */
136 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
140 # if defined(HAS_FCNTL) && defined(F_SETLK) && defined (F_SETLKW)
141 # define FLOCK fcntl_emulate_flock
142 # define FCNTL_EMULATE_FLOCK
143 # else /* no flock() or fcntl(F_SETLK,...) */
145 # define FLOCK lockf_emulate_flock
146 # define LOCKF_EMULATE_FLOCK
148 # endif /* no flock() or fcntl(F_SETLK,...) */
151 static int FLOCK (int, int);
154 * These are the flock() constants. Since this sytems doesn't have
155 * flock(), the values of the constants are probably not available.
169 # endif /* emulating flock() */
171 #endif /* no flock() */
174 static char zero_but_true[ZBTLEN + 1] = "0 but true";
176 #if defined(I_SYS_ACCESS) && !defined(R_OK)
177 # include <sys/access.h>
180 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
181 # define FD_CLOEXEC 1 /* NeXT needs this */
184 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
185 #undef PERL_EFF_ACCESS_W_OK
186 #undef PERL_EFF_ACCESS_X_OK
188 /* F_OK unused: if stat() cannot find it... */
190 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
191 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
192 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
193 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
194 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
197 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
198 # if defined(I_SYS_SECURITY)
199 # include <sys/security.h>
201 /* XXX Configure test needed for eaccess */
204 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
205 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
206 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
209 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
210 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
211 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
215 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
217 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
218 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
219 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
222 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
223 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
224 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
227 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
229 Uid_t ruid = getuid();
230 Uid_t euid = geteuid();
231 Gid_t rgid = getgid();
232 Gid_t egid = getegid();
236 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
237 Perl_croak(aTHX_ "switching effective uid is not implemented");
240 if (setreuid(euid, ruid))
243 if (setresuid(euid, ruid, (Uid_t)-1))
246 Perl_croak(aTHX_ "entering effective uid failed");
249 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
250 Perl_croak(aTHX_ "switching effective gid is not implemented");
253 if (setregid(egid, rgid))
256 if (setresgid(egid, rgid, (Gid_t)-1))
259 Perl_croak(aTHX_ "entering effective gid failed");
262 res = access(path, mode);
265 if (setreuid(ruid, euid))
268 if (setresuid(ruid, euid, (Uid_t)-1))
271 Perl_croak(aTHX_ "leaving effective uid failed");
274 if (setregid(rgid, egid))
277 if (setresgid(rgid, egid, (Gid_t)-1))
280 Perl_croak(aTHX_ "leaving effective gid failed");
285 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
286 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
287 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
290 #if !defined(PERL_EFF_ACCESS_R_OK)
292 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
294 Perl_croak(aTHX_ "switching effective uid is not implemented");
309 fp = PerlProc_popen(tmps, "r");
311 if (gimme == G_VOID) {
313 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
317 else if (gimme == G_SCALAR) {
318 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
319 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
330 if (sv_gets(sv, fp, 0) == Nullch) {
334 XPUSHs(sv_2mortal(sv));
335 if (SvLEN(sv) - SvCUR(sv) > 20) {
336 SvLEN_set(sv, SvCUR(sv)+1);
337 Renew(SvPVX(sv), SvLEN(sv), char);
342 STATUS_NATIVE_SET(PerlProc_pclose(fp));
343 TAINT; /* "I believe that this is not gratuitous!" */
346 STATUS_NATIVE_SET(-1);
347 if (gimme == G_SCALAR)
357 tryAMAGICunTARGET(iter, -1);
364 * The external globbing program may use things we can't control,
365 * so for security reasons we must assume the worst.
368 taint_proper(PL_no_security, "glob");
372 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
373 PL_last_in_gv = (GV*)*PL_stack_sp--;
375 SAVESPTR(PL_rs); /* This is not permanent, either. */
376 PL_rs = sv_2mortal(newSVpvn("\000", 1));
379 *SvPVX(PL_rs) = '\n';
383 result = do_readline();
388 #if 0 /* XXX never used! */
392 PL_last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*PL_stack_sp--)), n_a), TRUE,SVt_PVIO);
393 return do_readline();
399 PL_last_in_gv = cGVOP_gv;
400 return do_readline();
409 if (SP - MARK != 1) {
411 do_join(TARG, &PL_sv_no, MARK, SP);
418 tmps = SvPV(tmpsv, len);
421 (void)SvUPGRADE(error, SVt_PV);
422 if (SvPOK(error) && SvCUR(error))
423 sv_catpv(error, "\t...caught");
425 tmps = SvPV(tmpsv, len);
428 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
430 Perl_warn(aTHX_ "%"SVf, tmpsv);
441 if (SP - MARK != 1) {
443 do_join(TARG, &PL_sv_no, MARK, SP);
445 tmps = SvPV(tmpsv, len);
451 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
455 (void)SvUPGRADE(error, SVt_PV);
456 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
458 SvSetSV(error,tmpsv);
459 else if (sv_isobject(error)) {
460 HV *stash = SvSTASH(SvRV(error));
461 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
463 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
464 SV *line = sv_2mortal(newSViv(CopLINE(PL_curcop)));
471 call_sv((SV*)GvCV(gv),
472 G_SCALAR|G_EVAL|G_KEEPERR);
473 sv_setsv(error,*PL_stack_sp--);
479 if (SvPOK(error) && SvCUR(error))
480 sv_catpv(error, "\t...propagated");
482 tmps = SvPV(tmpsv, len);
486 tmpsv = sv_2mortal(newSVpvn("Died", 4));
488 DIE(aTHX_ "%"SVf, tmpsv);
511 DIE(aTHX_ PL_no_usym, "filehandle");
516 DIE(aTHX_ PL_no_usym, "filehandle");
518 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
520 if (mg = SvTIED_mg((SV*)gv, 'q')) {
522 XPUSHs(SvTIED_obj((SV*)gv, mg));
528 call_method("OPEN", G_SCALAR);
534 tmps = SvPV(sv, len);
535 if (do_open9(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, name, have_name))
536 PUSHi( (I32)PL_forkprocess );
537 else if (PL_forkprocess == 0) /* we are a new child */
555 if (mg = SvTIED_mg((SV*)gv, 'q')) {
557 XPUSHs(SvTIED_obj((SV*)gv, mg));
560 call_method("CLOSE", G_SCALAR);
566 PUSHs(boolSV(do_close(gv, TRUE)));
586 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
587 DIE(aTHX_ PL_no_usym, "filehandle");
592 do_close(rgv, FALSE);
594 do_close(wgv, FALSE);
596 if (PerlProc_pipe(fd) < 0)
599 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
600 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
601 IoIFP(wstio) = IoOFP(wstio);
605 if (!IoIFP(rstio) || !IoOFP(wstio)) {
606 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
607 else PerlLIO_close(fd[0]);
608 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
609 else PerlLIO_close(fd[1]);
612 #if defined(HAS_FCNTL) && defined(F_SETFD)
613 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
614 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
621 DIE(aTHX_ PL_no_func, "pipe");
637 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
639 XPUSHs(SvTIED_obj((SV*)gv, mg));
642 call_method("FILENO", G_SCALAR);
648 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io)))
650 PUSHi(PerlIO_fileno(fp));
661 anum = PerlLIO_umask(0);
662 (void)PerlLIO_umask(anum);
665 anum = PerlLIO_umask(POPi);
666 TAINT_PROPER("umask");
669 /* Only DIE if trying to restrict permissions on `user' (self).
670 * Otherwise it's harmless and more useful to just return undef
671 * since 'group' and 'other' concepts probably don't exist here. */
672 if (MAXARG >= 1 && (POPi & 0700))
673 DIE(aTHX_ "umask not implemented");
674 XPUSHs(&PL_sv_undef);
692 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
694 XPUSHs(SvTIED_obj((SV*)gv, mg));
697 call_method("BINMODE", G_SCALAR);
704 if (!(io = GvIO(gv)) || !(fp = IoIFP(io)))
707 if (do_binmode(fp,IoTYPE(io),TRUE))
722 I32 markoff = MARK - PL_stack_base;
729 switch(SvTYPE(varsv)) {
731 methname = "TIEHASH";
734 methname = "TIEARRAY";
737 methname = "TIEHANDLE";
741 methname = "TIESCALAR";
746 if (sv_isobject(*MARK)) {
748 PUSHSTACKi(PERLSI_MAGIC);
754 call_method(methname, G_SCALAR);
757 /* Not clear why we don't call call_method here too.
758 * perhaps to get different error message ?
760 stash = gv_stashsv(*MARK, FALSE);
761 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
762 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"",
763 methname, SvPV(*MARK,n_a));
766 PUSHSTACKi(PERLSI_MAGIC);
772 call_sv((SV*)GvCV(gv), G_SCALAR);
778 if (sv_isobject(sv)) {
779 sv_unmagic(varsv, how);
780 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
783 SP = PL_stack_base + markoff;
792 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
794 if (ckWARN(WARN_UNTIE)) {
796 if (mg = SvTIED_mg(sv, how)) {
797 if (mg && SvREFCNT(SvRV(mg->mg_obj)) > 1)
798 Perl_warner(aTHX_ WARN_UNTIE,
799 "untie attempted while %"UVuf" inner references still exist",
800 (UV)SvREFCNT(SvRV(mg->mg_obj)) - 1 ) ;
812 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
815 if (mg = SvTIED_mg(sv, how)) {
816 SV *osv = SvTIED_obj(sv, mg);
817 if (osv == mg->mg_obj)
818 osv = sv_mortalcopy(osv);
836 sv = sv_mortalcopy(&PL_sv_no);
837 sv_setpv(sv, "AnyDBM_File");
838 stash = gv_stashsv(sv, FALSE);
839 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
841 require_pv("AnyDBM_File.pm");
843 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
844 DIE(aTHX_ "No dbm on this machine");
854 PUSHs(sv_2mortal(newSViv(O_RDWR|O_CREAT)));
856 PUSHs(sv_2mortal(newSViv(O_RDWR)));
859 call_sv((SV*)GvCV(gv), G_SCALAR);
862 if (!sv_isobject(TOPs)) {
867 PUSHs(sv_2mortal(newSViv(O_RDONLY)));
870 call_sv((SV*)GvCV(gv), G_SCALAR);
874 if (sv_isobject(TOPs)) {
875 sv_unmagic((SV *) hv, 'P');
876 sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
898 struct timeval timebuf;
899 struct timeval *tbuf = &timebuf;
903 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
908 # if BYTEORDER & 0xf0000
909 # define ORDERBYTE (0x88888888 - BYTEORDER)
911 # define ORDERBYTE (0x4444 - BYTEORDER)
917 for (i = 1; i <= 3; i++) {
925 /* little endians can use vecs directly */
926 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
927 # if SELECT_MIN_BITS > 1
928 /* If SELECT_MIN_BITS is greater than one we most probably will want
929 * to align the sizes with SELECT_MIN_BITS/8 because for example
930 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
931 * UNIX, Solaris, NeXT, Rhapsody) the smallest quantum select() operates
932 * on (sets/tests/clears bits) is 32 bits. */
933 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
935 growsize = sizeof(fd_set);
944 masksize = NFDBITS / NBBY;
946 masksize = sizeof(long); /* documented int, everyone seems to use long */
948 growsize = maxlen + (masksize - (maxlen % masksize));
949 Zero(&fd_sets[0], 4, char*);
957 timebuf.tv_sec = (long)value;
958 value -= (NV)timebuf.tv_sec;
959 timebuf.tv_usec = (long)(value * 1000000.0);
962 tbuf = Null(struct timeval*);
964 for (i = 1; i <= 3; i++) {
971 SvPV_force(sv,n_a); /* force string conversion */
974 Sv_Grow(sv, growsize);
978 while (++j <= growsize) {
982 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
984 New(403, fd_sets[i], growsize, char);
985 for (offset = 0; offset < growsize; offset += masksize) {
986 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
987 fd_sets[i][j+offset] = s[(k % masksize) + offset];
990 fd_sets[i] = SvPVX(sv);
994 nfound = PerlSock_select(
996 (Select_fd_set_t) fd_sets[1],
997 (Select_fd_set_t) fd_sets[2],
998 (Select_fd_set_t) fd_sets[3],
1000 for (i = 1; i <= 3; i++) {
1003 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1005 for (offset = 0; offset < growsize; offset += masksize) {
1006 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1007 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1009 Safefree(fd_sets[i]);
1016 if (GIMME == G_ARRAY && tbuf) {
1017 value = (NV)(timebuf.tv_sec) +
1018 (NV)(timebuf.tv_usec) / 1000000.0;
1019 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1020 sv_setnv(sv, value);
1024 DIE(aTHX_ "select not implemented");
1029 Perl_setdefout(pTHX_ GV *gv)
1033 (void)SvREFCNT_inc(gv);
1035 SvREFCNT_dec(PL_defoutgv);
1042 GV *newdefout, *egv;
1045 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1047 egv = GvEGV(PL_defoutgv);
1052 XPUSHs(&PL_sv_undef);
1054 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1055 if (gvp && *gvp == egv) {
1056 gv_efullname3(TARG, PL_defoutgv, Nullch);
1060 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1065 if (!GvIO(newdefout))
1066 gv_IOadd(newdefout);
1067 setdefout(newdefout);
1084 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1085 I32 gimme = GIMME_V;
1087 XPUSHs(SvTIED_obj((SV*)gv, mg));
1090 call_method("GETC", gimme);
1093 if (gimme == G_SCALAR)
1094 SvSetMagicSV_nosteal(TARG, TOPs);
1097 if (!gv || do_eof(gv)) /* make sure we have fp with something */
1100 sv_setpv(TARG, " ");
1101 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1108 return pp_sysread();
1112 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1115 register PERL_CONTEXT *cx;
1116 I32 gimme = GIMME_V;
1117 AV* padlist = CvPADLIST(cv);
1118 SV** svp = AvARRAY(padlist);
1124 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1126 SAVEVPTR(PL_curpad);
1127 PL_curpad = AvARRAY((AV*)svp[1]);
1129 setdefout(gv); /* locally select filehandle so $% et al work */
1161 SV *tmpsv = sv_newmortal();
1162 gv_efullname3(tmpsv, fgv, Nullch);
1163 DIE(aTHX_ "Undefined format \"%s\" called",SvPVX(tmpsv));
1165 DIE(aTHX_ "Not a format reference");
1168 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1170 IoFLAGS(io) &= ~IOf_DIDTOP;
1171 return doform(cv,gv,PL_op->op_next);
1177 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1178 register IO *io = GvIOp(gv);
1179 PerlIO *ofp = IoOFP(io);
1183 register PERL_CONTEXT *cx;
1185 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1186 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1187 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1188 PL_formtarget != PL_toptarget)
1192 if (!IoTOP_GV(io)) {
1196 if (!IoTOP_NAME(io)) {
1197 if (!IoFMT_NAME(io))
1198 IoFMT_NAME(io) = savepv(GvNAME(gv));
1199 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1200 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1201 if ((topgv && GvFORM(topgv)) ||
1202 !gv_fetchpv("top",FALSE,SVt_PVFM))
1203 IoTOP_NAME(io) = savepv(SvPVX(topname));
1205 IoTOP_NAME(io) = savepv("top");
1207 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1208 if (!topgv || !GvFORM(topgv)) {
1209 IoLINES_LEFT(io) = 100000000;
1212 IoTOP_GV(io) = topgv;
1214 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1215 I32 lines = IoLINES_LEFT(io);
1216 char *s = SvPVX(PL_formtarget);
1217 if (lines <= 0) /* Yow, header didn't even fit!!! */
1219 while (lines-- > 0) {
1220 s = strchr(s, '\n');
1226 PerlIO_write(ofp, SvPVX(PL_formtarget), s - SvPVX(PL_formtarget));
1227 sv_chop(PL_formtarget, s);
1228 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1231 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1232 PerlIO_write(ofp, SvPVX(PL_formfeed), SvCUR(PL_formfeed));
1233 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1235 PL_formtarget = PL_toptarget;
1236 IoFLAGS(io) |= IOf_DIDTOP;
1239 DIE(aTHX_ "bad top format reference");
1242 SV *tmpsv = sv_newmortal();
1243 gv_efullname3(tmpsv, fgv, Nullch);
1244 DIE(aTHX_ "Undefined top format \"%s\" called",SvPVX(tmpsv));
1247 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1248 return doform(cv,gv,PL_op);
1252 POPBLOCK(cx,PL_curpm);
1258 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1260 SV* sv = sv_newmortal();
1261 gv_efullname3(sv, gv, Nullch);
1262 Perl_warner(aTHX_ WARN_IO,
1263 "Filehandle %s opened only for input",
1266 else if (ckWARN(WARN_CLOSED))
1267 report_closed_fh(gv, io, "write", "filehandle");
1272 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1273 if (ckWARN(WARN_IO))
1274 Perl_warner(aTHX_ WARN_IO, "page overflow");
1276 if (!PerlIO_write(ofp, SvPVX(PL_formtarget), SvCUR(PL_formtarget)) ||
1280 FmLINES(PL_formtarget) = 0;
1281 SvCUR_set(PL_formtarget, 0);
1282 *SvEND(PL_formtarget) = '\0';
1283 if (IoFLAGS(io) & IOf_FLUSH)
1284 (void)PerlIO_flush(fp);
1288 PL_formtarget = PL_bodytarget;
1290 return pop_return();
1295 djSP; dMARK; dORIGMARK;
1303 if (PL_op->op_flags & OPf_STACKED)
1308 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1309 if (MARK == ORIGMARK) {
1312 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1316 *MARK = SvTIED_obj((SV*)gv, mg);
1319 call_method("PRINTF", G_SCALAR);
1322 MARK = ORIGMARK + 1;
1329 if (!(io = GvIO(gv))) {
1330 if (ckWARN(WARN_UNOPENED)) {
1331 gv_efullname3(sv, gv, Nullch);
1332 Perl_warner(aTHX_ WARN_UNOPENED,
1333 "Filehandle %s never opened", SvPV(sv,n_a));
1335 SETERRNO(EBADF,RMS$_IFI);
1338 else if (!(fp = IoOFP(io))) {
1339 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1341 gv_efullname3(sv, gv, Nullch);
1342 Perl_warner(aTHX_ WARN_IO,
1343 "Filehandle %s opened only for input",
1346 else if (ckWARN(WARN_CLOSED))
1347 report_closed_fh(gv, io, "printf", "filehandle");
1349 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
1353 do_sprintf(sv, SP - MARK, MARK + 1);
1354 if (!do_print(sv, fp))
1357 if (IoFLAGS(io) & IOf_FLUSH)
1358 if (PerlIO_flush(fp) == EOF)
1369 PUSHs(&PL_sv_undef);
1390 /* Need TIEHANDLE method ? */
1392 tmps = SvPV(sv, len);
1393 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1394 IoLINES(GvIOp(gv)) = 0;
1398 PUSHs(&PL_sv_undef);
1405 djSP; dMARK; dORIGMARK; dTARGET;
1411 Sock_size_t bufsize;
1417 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) &&
1418 (mg = SvTIED_mg((SV*)gv, 'q')))
1423 *MARK = SvTIED_obj((SV*)gv, mg);
1425 call_method("READ", G_SCALAR);
1438 sv_setpvn(bufsv, "", 0);
1439 buffer = SvPV_force(bufsv, blen);
1440 length = SvIVx(*++MARK);
1442 DIE(aTHX_ "Negative length");
1445 offset = SvIVx(*++MARK);
1449 if (!io || !IoIFP(io))
1452 if (PL_op->op_type == OP_RECV) {
1453 char namebuf[MAXPATHLEN];
1454 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE)
1455 bufsize = sizeof (struct sockaddr_in);
1457 bufsize = sizeof namebuf;
1459 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1463 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1467 buffer = SvGROW(bufsv, length+1);
1468 /* 'offset' means 'flags' here */
1469 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1470 (struct sockaddr *)namebuf, &bufsize);
1473 SvCUR_set(bufsv, length);
1474 *SvEND(bufsv) = '\0';
1475 (void)SvPOK_only(bufsv);
1477 /* This should not be marked tainted if the fp is marked clean */
1478 if (!(IoFLAGS(io) & IOf_UNTAINT))
1479 SvTAINTED_on(bufsv);
1481 sv_setpvn(TARG, namebuf, bufsize);
1486 if (PL_op->op_type == OP_RECV)
1487 DIE(aTHX_ PL_no_sock_func, "recv");
1491 DIE(aTHX_ "Offset outside string");
1494 bufsize = SvCUR(bufsv);
1495 buffer = SvGROW(bufsv, length+offset+1);
1496 if (offset > bufsize) { /* Zero any newly allocated space */
1497 Zero(buffer+bufsize, offset-bufsize, char);
1499 if (PL_op->op_type == OP_SYSREAD) {
1500 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1501 if (IoTYPE(io) == 's') {
1502 length = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1503 buffer+offset, length, 0);
1508 length = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1509 buffer+offset, length);
1513 #ifdef HAS_SOCKET__bad_code_maybe
1514 if (IoTYPE(io) == 's') {
1515 char namebuf[MAXPATHLEN];
1516 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1517 bufsize = sizeof (struct sockaddr_in);
1519 bufsize = sizeof namebuf;
1521 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer+offset, length, 0,
1522 (struct sockaddr *)namebuf, &bufsize);
1527 length = PerlIO_read(IoIFP(io), buffer+offset, length);
1528 /* fread() returns 0 on both error and EOF */
1529 if (length == 0 && PerlIO_error(IoIFP(io)))
1533 if ((IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
1534 || IoIFP(io) == PerlIO_stderr()) && ckWARN(WARN_IO))
1536 SV* sv = sv_newmortal();
1537 gv_efullname3(sv, gv, Nullch);
1538 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
1543 SvCUR_set(bufsv, length+offset);
1544 *SvEND(bufsv) = '\0';
1545 (void)SvPOK_only(bufsv);
1547 /* This should not be marked tainted if the fp is marked clean */
1548 if (!(IoFLAGS(io) & IOf_UNTAINT))
1549 SvTAINTED_on(bufsv);
1562 int items = (SP - PL_stack_base) - TOPMARK;
1566 sv = sv_2mortal(newSViv(sv_len(*SP)));
1575 djSP; dMARK; dORIGMARK; dTARGET;
1586 if (PL_op->op_type == OP_SYSWRITE && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1590 *MARK = SvTIED_obj((SV*)gv, mg);
1592 call_method("WRITE", G_SCALAR);
1603 buffer = SvPV(bufsv, blen);
1604 #if Off_t_SIZE > IVSIZE
1605 length = SvNVx(*++MARK);
1607 length = SvIVx(*++MARK);
1610 DIE(aTHX_ "Negative length");
1613 if (!io || !IoIFP(io)) {
1615 if (ckWARN(WARN_CLOSED)) {
1616 if (PL_op->op_type == OP_SYSWRITE)
1617 report_closed_fh(gv, io, "syswrite", "filehandle");
1619 report_closed_fh(gv, io, "send", "socket");
1622 else if (PL_op->op_type == OP_SYSWRITE) {
1624 #if Off_t_SIZE > IVSIZE
1625 offset = SvNVx(*++MARK);
1627 offset = SvIVx(*++MARK);
1631 DIE(aTHX_ "Offset outside string");
1633 } else if (offset >= blen && blen > 0)
1634 DIE(aTHX_ "Offset outside string");
1637 if (length > blen - offset)
1638 length = blen - offset;
1639 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1640 if (IoTYPE(io) == 's') {
1641 length = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1642 buffer+offset, length, 0);
1647 /* See the note at doio.c:do_print about filesize limits. --jhi */
1648 length = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1649 buffer+offset, length);
1653 else if (SP > MARK) {
1656 sockbuf = SvPVx(*++MARK, mlen);
1657 length = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen, length,
1658 (struct sockaddr *)sockbuf, mlen);
1661 length = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1665 DIE(aTHX_ PL_no_sock_func, "send");
1680 return pp_sysread();
1690 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1692 gv = PL_last_in_gv = PL_argvgv;
1694 if (io && !IoIFP(io)) {
1695 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1697 IoFLAGS(io) &= ~IOf_START;
1698 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1699 sv_setpvn(GvSV(gv), "-", 1);
1700 SvSETMAGIC(GvSV(gv));
1702 else if (!nextargv(gv))
1707 gv = PL_last_in_gv; /* eof */
1710 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1712 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1714 XPUSHs(SvTIED_obj((SV*)gv, mg));
1717 call_method("EOF", G_SCALAR);
1723 PUSHs(boolSV(!gv || do_eof(gv)));
1736 gv = PL_last_in_gv = (GV*)POPs;
1738 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1740 XPUSHs(SvTIED_obj((SV*)gv, mg));
1743 call_method("TELL", G_SCALAR);
1749 #if LSEEKSIZE > IVSIZE
1750 PUSHn( do_tell(gv) );
1752 PUSHi( do_tell(gv) );
1759 return pp_sysseek();
1767 #if LSEEKSIZE > IVSIZE
1768 Off_t offset = (Off_t)SvNVx(POPs);
1770 Off_t offset = (Off_t)SvIVx(POPs);
1774 gv = PL_last_in_gv = (GV*)POPs;
1776 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1778 XPUSHs(SvTIED_obj((SV*)gv, mg));
1779 #if LSEEKSIZE > IVSIZE
1780 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1782 XPUSHs(sv_2mortal(newSViv((IV) offset)));
1784 XPUSHs(sv_2mortal(newSViv((IV) whence)));
1787 call_method("SEEK", G_SCALAR);
1793 if (PL_op->op_type == OP_SEEK)
1794 PUSHs(boolSV(do_seek(gv, offset, whence)));
1796 Off_t n = do_sysseek(gv, offset, whence);
1798 PUSHs(&PL_sv_undef);
1801 #if LSEEKSIZE > IVSIZE
1806 : newSVpvn(zero_but_true, ZBTLEN);
1807 PUSHs(sv_2mortal(sv));
1816 Off_t len = (Off_t)POPn;
1822 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
1823 if (PL_op->op_flags & OPf_SPECIAL) {
1824 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
1826 TAINT_PROPER("truncate");
1827 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)))
1830 PerlIO_flush(IoIFP(GvIOp(tmpgv)));
1832 if (ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1834 if (my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1844 if (SvTYPE(sv) == SVt_PVGV) {
1845 tmpgv = (GV*)sv; /* *main::FRED for example */
1848 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1849 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
1853 name = SvPV(sv, n_a);
1854 TAINT_PROPER("truncate");
1856 if (truncate(name, len) < 0)
1861 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
1864 if (my_chsize(tmpfd, len) < 0)
1866 PerlLIO_close(tmpfd);
1875 SETERRNO(EBADF,RMS$_IFI);
1878 DIE(aTHX_ "truncate not implemented");
1891 unsigned int func = U_I(POPn);
1892 int optype = PL_op->op_type;
1898 if (!io || !argsv || !IoIFP(io)) {
1899 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
1903 if (SvPOK(argsv) || !SvNIOK(argsv)) {
1906 s = SvPV_force(argsv, len);
1907 need = IOCPARM_LEN(func);
1909 s = Sv_Grow(argsv, need + 1);
1910 SvCUR_set(argsv, need);
1913 s[SvCUR(argsv)] = 17; /* a little sanity check here */
1916 retval = SvIV(argsv);
1917 s = INT2PTR(char*,retval); /* ouch */
1920 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1922 if (optype == OP_IOCTL)
1924 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
1926 DIE(aTHX_ "ioctl is not implemented");
1930 #if defined(OS2) && defined(__EMX__)
1931 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
1933 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
1936 DIE(aTHX_ "fcntl is not implemented");
1940 if (s[SvCUR(argsv)] != 17)
1941 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
1942 PL_op_name[optype]);
1943 s[SvCUR(argsv)] = 0; /* put our null back */
1944 SvSETMAGIC(argsv); /* Assume it has changed */
1953 PUSHp(zero_but_true, ZBTLEN);
1973 fp = IoIFP(GvIOp(gv));
1977 (void)PerlIO_flush(fp);
1978 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
1982 SETERRNO(EBADF,RMS$_IFI);
1983 if (ckWARN(WARN_CLOSED))
1984 report_closed_fh(gv, GvIO(gv), "flock", "filehandle");
1989 DIE(aTHX_ PL_no_func, "flock()");
2001 int protocol = POPi;
2009 SETERRNO(EBADF,LIB$_INVARG);
2015 do_close(gv, FALSE);
2017 TAINT_PROPER("socket");
2018 fd = PerlSock_socket(domain, type, protocol);
2021 IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */
2022 IoOFP(io) = PerlIO_fdopen(fd, "w");
2024 if (!IoIFP(io) || !IoOFP(io)) {
2025 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2026 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2027 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2030 #if defined(HAS_FCNTL) && defined(F_SETFD)
2031 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2036 DIE(aTHX_ PL_no_sock_func, "socket");
2043 #ifdef HAS_SOCKETPAIR
2048 int protocol = POPi;
2061 do_close(gv1, FALSE);
2063 do_close(gv2, FALSE);
2065 TAINT_PROPER("socketpair");
2066 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2068 IoIFP(io1) = PerlIO_fdopen(fd[0], "r");
2069 IoOFP(io1) = PerlIO_fdopen(fd[0], "w");
2071 IoIFP(io2) = PerlIO_fdopen(fd[1], "r");
2072 IoOFP(io2) = PerlIO_fdopen(fd[1], "w");
2074 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2075 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2076 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2077 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2078 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2079 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2080 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2083 #if defined(HAS_FCNTL) && defined(F_SETFD)
2084 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2085 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2090 DIE(aTHX_ PL_no_sock_func, "socketpair");
2098 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2099 extern GETPRIVMODE();
2100 extern GETUSERMODE();
2105 register IO *io = GvIOn(gv);
2109 int mpeprivmode = 0;
2112 if (!io || !IoIFP(io))
2115 addr = SvPV(addrsv, len);
2116 TAINT_PROPER("bind");
2117 #ifdef MPE /* Deal with MPE bind() peculiarities */
2118 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2119 /* The address *MUST* stupidly be zero. */
2120 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2121 /* PRIV mode is required to bind() to ports < 1024. */
2122 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2123 ((struct sockaddr_in *)addr)->sin_port > 0) {
2124 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2129 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2130 (struct sockaddr *)addr, len) >= 0)
2133 #ifdef MPE /* Switch back to USER mode */
2144 if (ckWARN(WARN_CLOSED))
2145 report_closed_fh(gv, io, "bind", "socket");
2146 SETERRNO(EBADF,SS$_IVCHAN);
2149 DIE(aTHX_ PL_no_sock_func, "bind");
2160 register IO *io = GvIOn(gv);
2163 if (!io || !IoIFP(io))
2166 addr = SvPV(addrsv, len);
2167 TAINT_PROPER("connect");
2168 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2174 if (ckWARN(WARN_CLOSED))
2175 report_closed_fh(gv, io, "connect", "socket");
2176 SETERRNO(EBADF,SS$_IVCHAN);
2179 DIE(aTHX_ PL_no_sock_func, "connect");
2189 register IO *io = GvIOn(gv);
2191 if (!io || !IoIFP(io))
2194 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2200 if (ckWARN(WARN_CLOSED))
2201 report_closed_fh(gv, io, "listen", "socket");
2202 SETERRNO(EBADF,SS$_IVCHAN);
2205 DIE(aTHX_ PL_no_sock_func, "listen");
2217 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2218 Sock_size_t len = sizeof saddr;
2230 if (!gstio || !IoIFP(gstio))
2235 do_close(ngv, FALSE);
2237 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2240 IoIFP(nstio) = PerlIO_fdopen(fd, "r");
2241 IoOFP(nstio) = PerlIO_fdopen(fd, "w");
2242 IoTYPE(nstio) = 's';
2243 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2244 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2245 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2246 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2249 #if defined(HAS_FCNTL) && defined(F_SETFD)
2250 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2253 PUSHp((char *)&saddr, len);
2257 if (ckWARN(WARN_CLOSED))
2258 report_closed_fh(ggv, ggv ? GvIO(ggv) : 0, "accept", "socket");
2259 SETERRNO(EBADF,SS$_IVCHAN);
2265 DIE(aTHX_ PL_no_sock_func, "accept");
2275 register IO *io = GvIOn(gv);
2277 if (!io || !IoIFP(io))
2280 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2284 if (ckWARN(WARN_CLOSED))
2285 report_closed_fh(gv, io, "shutdown", "socket");
2286 SETERRNO(EBADF,SS$_IVCHAN);
2289 DIE(aTHX_ PL_no_sock_func, "shutdown");
2296 return pp_ssockopt();
2298 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2306 int optype = PL_op->op_type;
2309 unsigned int optname;
2315 if (optype == OP_GSOCKOPT)
2316 sv = sv_2mortal(NEWSV(22, 257));
2319 optname = (unsigned int) POPi;
2320 lvl = (unsigned int) POPi;
2324 if (!io || !IoIFP(io))
2327 fd = PerlIO_fileno(IoIFP(io));
2331 (void)SvPOK_only(sv);
2335 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2350 aint = (int)SvIV(sv);
2354 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2363 if (ckWARN(WARN_CLOSED))
2364 report_closed_fh(gv, io,
2365 optype == OP_GSOCKOPT ? "getsockopt" : "setsockopt",
2367 SETERRNO(EBADF,SS$_IVCHAN);
2372 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2379 return pp_getpeername();
2381 DIE(aTHX_ PL_no_sock_func, "getsockname");
2389 int optype = PL_op->op_type;
2393 register IO *io = GvIOn(gv);
2396 if (!io || !IoIFP(io))
2399 sv = sv_2mortal(NEWSV(22, 257));
2400 (void)SvPOK_only(sv);
2404 fd = PerlIO_fileno(IoIFP(io));
2406 case OP_GETSOCKNAME:
2407 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2410 case OP_GETPEERNAME:
2411 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2413 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2415 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";
2416 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2417 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2418 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2419 sizeof(u_short) + sizeof(struct in_addr))) {
2426 #ifdef BOGUS_GETNAME_RETURN
2427 /* Interactive Unix, getpeername() and getsockname()
2428 does not return valid namelen */
2429 if (len == BOGUS_GETNAME_RETURN)
2430 len = sizeof(struct sockaddr);
2438 if (ckWARN(WARN_CLOSED))
2439 report_closed_fh(gv, io,
2440 optype == OP_GETSOCKNAME ? "getsockname"
2443 SETERRNO(EBADF,SS$_IVCHAN);
2448 DIE(aTHX_ PL_no_sock_func, "getpeername");
2467 if (PL_op->op_flags & OPf_REF) {
2470 if (tmpgv != PL_defgv) {
2471 PL_laststype = OP_STAT;
2473 sv_setpv(PL_statname, "");
2474 PL_laststatval = (GvIO(tmpgv) && IoIFP(GvIOp(tmpgv))
2475 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &PL_statcache) : -1);
2477 if (PL_laststatval < 0)
2482 if (SvTYPE(sv) == SVt_PVGV) {
2486 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2487 tmpgv = (GV*)SvRV(sv);
2490 sv_setpv(PL_statname, SvPV(sv,n_a));
2493 PL_laststype = PL_op->op_type;
2494 if (PL_op->op_type == OP_LSTAT)
2495 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2498 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2499 if (PL_laststatval < 0) {
2500 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2501 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
2507 if (gimme != G_ARRAY) {
2508 if (gimme != G_VOID)
2509 XPUSHs(boolSV(max));
2515 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2516 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2517 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mode)));
2518 PUSHs(sv_2mortal(newSViv(PL_statcache.st_nlink)));
2519 #if Uid_t_size > IVSIZE
2520 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2522 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2524 #if Gid_t_size > IVSIZE
2525 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2527 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2529 #ifdef USE_STAT_RDEV
2530 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2532 PUSHs(sv_2mortal(newSVpvn("", 0)));
2534 #if Off_t_size > IVSIZE
2535 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size)));
2537 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2540 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2541 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2542 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2544 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2545 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2546 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2548 #ifdef USE_STAT_BLOCKS
2549 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blksize)));
2550 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blocks)));
2552 PUSHs(sv_2mortal(newSVpvn("", 0)));
2553 PUSHs(sv_2mortal(newSVpvn("", 0)));
2563 #if defined(HAS_ACCESS) && defined(R_OK)
2565 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2566 result = access(TOPpx, R_OK);
2581 if (cando(S_IRUSR, 0, &PL_statcache))
2590 #if defined(HAS_ACCESS) && defined(W_OK)
2592 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2593 result = access(TOPpx, W_OK);
2608 if (cando(S_IWUSR, 0, &PL_statcache))
2617 #if defined(HAS_ACCESS) && defined(X_OK)
2619 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2620 result = access(TOPpx, X_OK);
2635 if (cando(S_IXUSR, 0, &PL_statcache))
2644 #ifdef PERL_EFF_ACCESS_R_OK
2646 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2647 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2662 if (cando(S_IRUSR, 1, &PL_statcache))
2671 #ifdef PERL_EFF_ACCESS_W_OK
2673 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2674 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2689 if (cando(S_IWUSR, 1, &PL_statcache))
2698 #ifdef PERL_EFF_ACCESS_X_OK
2700 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2701 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2716 if (cando(S_IXUSR, 1, &PL_statcache))
2723 I32 result = my_stat();
2732 return pp_ftrowned();
2737 I32 result = my_stat();
2741 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
2749 I32 result = my_stat();
2753 if (PL_statcache.st_size == 0)
2760 I32 result = my_stat();
2764 #if Off_t_size > IVSIZE
2765 PUSHn(PL_statcache.st_size);
2767 PUSHi(PL_statcache.st_size);
2774 I32 result = my_stat();
2778 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
2784 I32 result = my_stat();
2788 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
2794 I32 result = my_stat();
2798 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
2804 I32 result = my_stat();
2808 if (S_ISSOCK(PL_statcache.st_mode))
2815 I32 result = my_stat();
2819 if (S_ISCHR(PL_statcache.st_mode))
2826 I32 result = my_stat();
2830 if (S_ISBLK(PL_statcache.st_mode))
2837 I32 result = my_stat();
2841 if (S_ISREG(PL_statcache.st_mode))
2848 I32 result = my_stat();
2852 if (S_ISDIR(PL_statcache.st_mode))
2859 I32 result = my_stat();
2863 if (S_ISFIFO(PL_statcache.st_mode))
2870 I32 result = my_lstat();
2874 if (S_ISLNK(PL_statcache.st_mode))
2883 I32 result = my_stat();
2887 if (PL_statcache.st_mode & S_ISUID)
2897 I32 result = my_stat();
2901 if (PL_statcache.st_mode & S_ISGID)
2911 I32 result = my_stat();
2915 if (PL_statcache.st_mode & S_ISVTX)
2926 char *tmps = Nullch;
2929 if (PL_op->op_flags & OPf_REF)
2931 else if (isGV(TOPs))
2933 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2934 gv = (GV*)SvRV(POPs);
2936 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
2938 if (GvIO(gv) && IoIFP(GvIOp(gv)))
2939 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
2940 else if (tmps && isDIGIT(*tmps))
2944 if (PerlLIO_isatty(fd))
2949 #if defined(atarist) /* this will work with atariST. Configure will
2950 make guesses for other systems. */
2951 # define FILE_base(f) ((f)->_base)
2952 # define FILE_ptr(f) ((f)->_ptr)
2953 # define FILE_cnt(f) ((f)->_cnt)
2954 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
2964 register STDCHAR *s;
2971 if (PL_op->op_flags & OPf_REF)
2973 else if (isGV(TOPs))
2975 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2976 gv = (GV*)SvRV(POPs);
2982 if (gv == PL_defgv) {
2984 io = GvIO(PL_statgv);
2987 goto really_filename;
2992 PL_laststatval = -1;
2993 sv_setpv(PL_statname, "");
2994 io = GvIO(PL_statgv);
2996 if (io && IoIFP(io)) {
2997 if (! PerlIO_has_base(IoIFP(io)))
2998 DIE(aTHX_ "-T and -B not implemented on filehandles");
2999 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3000 if (PL_laststatval < 0)
3002 if (S_ISDIR(PL_statcache.st_mode)) /* handle NFS glitch */
3003 if (PL_op->op_type == OP_FTTEXT)
3007 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3008 i = PerlIO_getc(IoIFP(io));
3010 (void)PerlIO_ungetc(IoIFP(io),i);
3012 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3014 len = PerlIO_get_bufsiz(IoIFP(io));
3015 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3016 /* sfio can have large buffers - limit to 512 */
3021 if (ckWARN(WARN_UNOPENED)) {
3023 Perl_warner(aTHX_ WARN_UNOPENED, "Test on unopened file <%s>",
3026 SETERRNO(EBADF,RMS$_IFI);
3034 PL_laststatval = -1;
3035 sv_setpv(PL_statname, SvPV(sv, n_a));
3036 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3037 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3038 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
3041 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3042 if (PL_laststatval < 0) {
3043 (void)PerlIO_close(fp);
3046 do_binmode(fp, '<', TRUE);
3047 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3048 (void)PerlIO_close(fp);
3050 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3051 RETPUSHNO; /* special case NFS directories */
3052 RETPUSHYES; /* null file is anything */
3057 /* now scan s to look for textiness */
3058 /* XXX ASCII dependent code */
3060 #if defined(DOSISH) || defined(USEMYBINMODE)
3061 /* ignore trailing ^Z on short files */
3062 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3066 for (i = 0; i < len; i++, s++) {
3067 if (!*s) { /* null never allowed in text */
3072 else if (!(isPRINT(*s) || isSPACE(*s)))
3075 else if (*s & 128) {
3077 if (!(PL_op->op_private & OPpLOCALE) || !isALPHA_LC(*s))
3082 *s != '\n' && *s != '\r' && *s != '\b' &&
3083 *s != '\t' && *s != '\f' && *s != 27)
3088 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3112 if (!tmps || !*tmps) {
3113 svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE);
3115 tmps = SvPV(*svp, n_a);
3117 if (!tmps || !*tmps) {
3118 svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE);
3120 tmps = SvPV(*svp, n_a);
3123 if (!tmps || !*tmps) {
3124 svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE);
3126 tmps = SvPV(*svp, n_a);
3129 TAINT_PROPER("chdir");
3130 PUSHi( PerlDir_chdir(tmps) >= 0 );
3132 /* Clear the DEFAULT element of ENV so we'll get the new value
3134 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3141 djSP; dMARK; dTARGET;
3144 value = (I32)apply(PL_op->op_type, MARK, SP);
3149 DIE(aTHX_ PL_no_func, "Unsupported function chown");
3160 TAINT_PROPER("chroot");
3161 PUSHi( chroot(tmps) >= 0 );
3164 DIE(aTHX_ PL_no_func, "chroot");
3170 djSP; dMARK; dTARGET;
3172 value = (I32)apply(PL_op->op_type, MARK, SP);
3180 djSP; dMARK; dTARGET;
3182 value = (I32)apply(PL_op->op_type, MARK, SP);
3190 djSP; dMARK; dTARGET;
3192 value = (I32)apply(PL_op->op_type, MARK, SP);
3204 char *tmps2 = POPpx;
3205 char *tmps = SvPV(TOPs, n_a);
3206 TAINT_PROPER("rename");
3208 anum = PerlLIO_rename(tmps, tmps2);
3210 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3211 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3214 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3215 (void)UNLINK(tmps2);
3216 if (!(anum = link(tmps, tmps2)))
3217 anum = UNLINK(tmps);
3230 char *tmps2 = POPpx;
3231 char *tmps = SvPV(TOPs, n_a);
3232 TAINT_PROPER("link");
3233 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3235 DIE(aTHX_ PL_no_func, "Unsupported function link");
3245 char *tmps2 = POPpx;
3246 char *tmps = SvPV(TOPs, n_a);
3247 TAINT_PROPER("symlink");
3248 SETi( symlink(tmps, tmps2) >= 0 );
3251 DIE(aTHX_ PL_no_func, "symlink");
3260 char buf[MAXPATHLEN];
3264 #ifndef INCOMPLETE_TAINTS
3268 len = readlink(tmps, buf, sizeof buf);
3276 RETSETUNDEF; /* just pretend it's a normal file */
3280 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3282 S_dooneliner(pTHX_ char *cmd, char *filename)
3284 char *save_filename = filename;
3290 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3291 strcpy(cmdline, cmd);
3292 strcat(cmdline, " ");
3293 for (s = cmdline + strlen(cmdline); *filename; ) {
3298 myfp = PerlProc_popen(cmdline, "r");
3302 SV *tmpsv = sv_newmortal();
3303 /* Need to save/restore 'PL_rs' ?? */
3304 s = sv_gets(tmpsv, myfp, 0);
3305 (void)PerlProc_pclose(myfp);
3309 #ifdef HAS_SYS_ERRLIST
3314 /* you don't see this */
3316 #ifdef HAS_SYS_ERRLIST
3324 if (instr(s, errmsg)) {
3331 #define EACCES EPERM
3333 if (instr(s, "cannot make"))
3334 SETERRNO(EEXIST,RMS$_FEX);
3335 else if (instr(s, "existing file"))
3336 SETERRNO(EEXIST,RMS$_FEX);
3337 else if (instr(s, "ile exists"))
3338 SETERRNO(EEXIST,RMS$_FEX);
3339 else if (instr(s, "non-exist"))
3340 SETERRNO(ENOENT,RMS$_FNF);
3341 else if (instr(s, "does not exist"))
3342 SETERRNO(ENOENT,RMS$_FNF);
3343 else if (instr(s, "not empty"))
3344 SETERRNO(EBUSY,SS$_DEVOFFLINE);
3345 else if (instr(s, "cannot access"))
3346 SETERRNO(EACCES,RMS$_PRV);
3348 SETERRNO(EPERM,RMS$_PRV);
3351 else { /* some mkdirs return no failure indication */
3352 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3353 if (PL_op->op_type == OP_RMDIR)
3358 SETERRNO(EACCES,RMS$_PRV); /* a guess */
3382 tmps = SvPV(TOPs, n_a);
3384 TAINT_PROPER("mkdir");
3386 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3388 SETi( dooneliner("mkdir", tmps) );
3389 oldumask = PerlLIO_umask(0);
3390 PerlLIO_umask(oldumask);
3391 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3403 TAINT_PROPER("rmdir");
3405 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3407 XPUSHi( dooneliner("rmdir", tmps) );
3412 /* Directory calls. */
3417 #if defined(Direntry_t) && defined(HAS_READDIR)
3419 char *dirname = POPpx;
3421 register IO *io = GvIOn(gv);
3427 PerlDir_close(IoDIRP(io));
3428 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3434 SETERRNO(EBADF,RMS$_DIR);
3437 DIE(aTHX_ PL_no_dir_func, "opendir");
3444 #if defined(Direntry_t) && defined(HAS_READDIR)
3446 Direntry_t *readdir (DIR *);
3448 register Direntry_t *dp;
3450 register IO *io = GvIOn(gv);
3453 if (!io || !IoDIRP(io))
3456 if (GIMME == G_ARRAY) {
3458 while (dp = (Direntry_t *)PerlDir_read(IoDIRP(io))) {
3460 sv = newSVpvn(dp->d_name, dp->d_namlen);
3462 sv = newSVpv(dp->d_name, 0);
3464 #ifndef INCOMPLETE_TAINTS
3467 XPUSHs(sv_2mortal(sv));
3471 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3474 sv = newSVpvn(dp->d_name, dp->d_namlen);
3476 sv = newSVpv(dp->d_name, 0);
3478 #ifndef INCOMPLETE_TAINTS
3481 XPUSHs(sv_2mortal(sv));
3487 SETERRNO(EBADF,RMS$_ISI);
3488 if (GIMME == G_ARRAY)
3493 DIE(aTHX_ PL_no_dir_func, "readdir");
3500 #if defined(HAS_TELLDIR) || defined(telldir)
3501 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3502 /* XXX netbsd still seemed to.
3503 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3504 --JHI 1999-Feb-02 */
3505 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3506 long telldir (DIR *);
3509 register IO *io = GvIOn(gv);
3511 if (!io || !IoDIRP(io))
3514 PUSHi( PerlDir_tell(IoDIRP(io)) );
3518 SETERRNO(EBADF,RMS$_ISI);
3521 DIE(aTHX_ PL_no_dir_func, "telldir");
3528 #if defined(HAS_SEEKDIR) || defined(seekdir)
3531 register IO *io = GvIOn(gv);
3533 if (!io || !IoDIRP(io))
3536 (void)PerlDir_seek(IoDIRP(io), along);
3541 SETERRNO(EBADF,RMS$_ISI);
3544 DIE(aTHX_ PL_no_dir_func, "seekdir");
3551 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3553 register IO *io = GvIOn(gv);
3555 if (!io || !IoDIRP(io))
3558 (void)PerlDir_rewind(IoDIRP(io));
3562 SETERRNO(EBADF,RMS$_ISI);
3565 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3572 #if defined(Direntry_t) && defined(HAS_READDIR)
3574 register IO *io = GvIOn(gv);
3576 if (!io || !IoDIRP(io))
3579 #ifdef VOID_CLOSEDIR
3580 PerlDir_close(IoDIRP(io));
3582 if (PerlDir_close(IoDIRP(io)) < 0) {
3583 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3592 SETERRNO(EBADF,RMS$_IFI);
3595 DIE(aTHX_ PL_no_dir_func, "closedir");
3599 /* Process control. */
3609 PERL_FLUSHALL_FOR_CHILD;
3615 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
3616 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3617 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3622 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3627 PERL_FLUSHALL_FOR_CHILD;
3628 childpid = PerlProc_fork();
3632 DIE(aTHX_ PL_no_func, "Unsupported function fork");
3639 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3644 childpid = wait4pid(-1, &argflags, 0);
3645 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3649 DIE(aTHX_ PL_no_func, "Unsupported function wait");
3655 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3663 childpid = wait4pid(childpid, &argflags, optype);
3664 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3668 DIE(aTHX_ PL_no_func, "Unsupported function waitpid");
3674 djSP; dMARK; dORIGMARK; dTARGET;
3679 Sigsave_t ihand,qhand; /* place to save signals during system() */
3684 if (SP - MARK == 1) {
3686 char *junk = SvPV(TOPs, n_a);
3688 TAINT_PROPER("system");
3691 PERL_FLUSHALL_FOR_CHILD;
3692 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2)
3693 if (PerlProc_pipe(pp) >= 0)
3695 while ((childpid = vfork()) == -1) {
3696 if (errno != EAGAIN) {
3701 PerlLIO_close(pp[0]);
3702 PerlLIO_close(pp[1]);
3710 PerlLIO_close(pp[1]);
3711 rsignal_save(SIGINT, SIG_IGN, &ihand);
3712 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
3714 result = wait4pid(childpid, &status, 0);
3715 } while (result == -1 && errno == EINTR);
3716 (void)rsignal_restore(SIGINT, &ihand);
3717 (void)rsignal_restore(SIGQUIT, &qhand);
3718 STATUS_NATIVE_SET(result == -1 ? -1 : status);
3719 do_execfree(); /* free any memory child malloced on vfork */
3725 while (n < sizeof(int)) {
3726 n1 = PerlLIO_read(pp[0],
3727 (void*)(((char*)&errkid)+n),
3733 PerlLIO_close(pp[0]);
3734 if (n) { /* Error */
3735 if (n != sizeof(int))
3736 DIE(aTHX_ "panic: kid popen errno read");
3737 errno = errkid; /* Propagate errno from kid */
3738 STATUS_CURRENT = -1;
3741 PUSHi(STATUS_CURRENT);
3745 PerlLIO_close(pp[0]);
3746 #if defined(HAS_FCNTL) && defined(F_SETFD)
3747 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
3750 if (PL_op->op_flags & OPf_STACKED) {
3751 SV *really = *++MARK;
3752 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
3754 else if (SP - MARK != 1)
3755 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
3757 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
3760 #else /* ! FORK or VMS or OS/2 */
3761 if (PL_op->op_flags & OPf_STACKED) {
3762 SV *really = *++MARK;
3763 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
3765 else if (SP - MARK != 1)
3766 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
3768 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3770 STATUS_NATIVE_SET(value);
3773 PUSHi(STATUS_CURRENT);
3774 #endif /* !FORK or VMS */
3780 djSP; dMARK; dORIGMARK; dTARGET;
3784 PERL_FLUSHALL_FOR_CHILD;
3785 if (PL_op->op_flags & OPf_STACKED) {
3786 SV *really = *++MARK;
3787 value = (I32)do_aexec(really, MARK, SP);
3789 else if (SP - MARK != 1)
3791 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
3795 (void ) do_aspawn(Nullsv, MARK, SP);
3799 value = (I32)do_aexec(Nullsv, MARK, SP);
3804 char *junk = SvPV(*SP, n_a);
3806 TAINT_PROPER("exec");
3809 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3812 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3815 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3820 #if !defined(HAS_FORK) && defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3832 djSP; dMARK; dTARGET;
3835 value = (I32)apply(PL_op->op_type, MARK, SP);
3840 DIE(aTHX_ PL_no_func, "Unsupported function kill");
3848 XPUSHi( getppid() );
3851 DIE(aTHX_ PL_no_func, "getppid");
3867 pgrp = (I32)BSD_GETPGRP(pid);
3869 if (pid != 0 && pid != PerlProc_getpid())
3870 DIE(aTHX_ "POSIX getpgrp can't take an argument");
3876 DIE(aTHX_ PL_no_func, "getpgrp()");
3895 TAINT_PROPER("setpgrp");
3897 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
3899 if ((pgrp != 0 && pgrp != PerlProc_getpid())
3900 || (pid != 0 && pid != PerlProc_getpid()))
3902 DIE(aTHX_ "setpgrp can't take arguments");
3904 SETi( setpgrp() >= 0 );
3905 #endif /* USE_BSDPGRP */
3908 DIE(aTHX_ PL_no_func, "setpgrp()");
3917 #ifdef HAS_GETPRIORITY
3920 SETi( getpriority(which, who) );
3923 DIE(aTHX_ PL_no_func, "getpriority()");
3933 #ifdef HAS_SETPRIORITY
3937 TAINT_PROPER("setpriority");
3938 SETi( setpriority(which, who, niceval) >= 0 );
3941 DIE(aTHX_ PL_no_func, "setpriority()");
3951 XPUSHn( time(Null(Time_t*)) );
3953 XPUSHi( time(Null(Time_t*)) );
3958 /* XXX The POSIX name is CLK_TCK; it is to be preferred
3959 to HZ. Probably. For now, assume that if the system
3960 defines HZ, it does so correctly. (Will this break
3962 Probably we ought to use _sysconf(_SC_CLK_TCK), if
3963 it's supported. --AD 9/96.
3979 DIE(aTHX_ "times not implemented");
3984 (void)PerlProc_times(&PL_timesbuf);
3986 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
3987 /* struct tms, though same data */
3991 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
3992 if (GIMME == G_ARRAY) {
3993 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
3994 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
3995 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
3998 #endif /* HAS_TIMES */
4011 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4012 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4013 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4019 when = (Time_t)SvNVx(POPs);
4021 when = (Time_t)SvIVx(POPs);
4024 if (PL_op->op_type == OP_LOCALTIME)
4025 tmbuf = localtime(&when);
4027 tmbuf = gmtime(&when);
4031 if (GIMME != G_ARRAY) {
4036 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4037 dayname[tmbuf->tm_wday],
4038 monname[tmbuf->tm_mon],
4043 tmbuf->tm_year + 1900);
4044 PUSHs(sv_2mortal(tsv));
4047 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4048 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4049 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4050 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4051 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4052 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4053 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4054 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4055 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4066 anum = alarm((unsigned int)anum);
4073 DIE(aTHX_ PL_no_func, "Unsupported function alarm");
4084 (void)time(&lasttime);
4089 PerlProc_sleep((unsigned int)duration);
4092 XPUSHi(when - lasttime);
4096 /* Shared memory. */
4110 return pp_shmwrite();
4115 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4116 djSP; dMARK; dTARGET;
4117 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4126 /* Message passing. */
4140 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4141 djSP; dMARK; dTARGET;
4142 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4153 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4154 djSP; dMARK; dTARGET;
4155 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4168 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4169 djSP; dMARK; dTARGET;
4170 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4177 DIE(aTHX_ "System V IPC is not implemented on this machine");
4183 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4184 djSP; dMARK; dTARGET;
4185 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4193 PUSHp(zero_but_true, ZBTLEN);
4203 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4204 djSP; dMARK; dTARGET;
4205 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4214 /* Get system info. */
4218 #ifdef HAS_GETHOSTBYNAME
4219 return pp_ghostent();
4221 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4227 #ifdef HAS_GETHOSTBYADDR
4228 return pp_ghostent();
4230 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4237 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4238 I32 which = PL_op->op_type;
4239 register char **elem;
4241 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4242 struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4243 struct hostent *PerlSock_gethostbyname(Netdb_name_t);
4244 struct hostent *PerlSock_gethostent(void);
4246 struct hostent *hent;
4251 if (which == OP_GHBYNAME)
4252 #ifdef HAS_GETHOSTBYNAME
4253 hent = PerlSock_gethostbyname(POPpx);
4255 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4257 else if (which == OP_GHBYADDR) {
4258 #ifdef HAS_GETHOSTBYADDR
4259 int addrtype = POPi;
4262 Netdb_host_t addr = (Netdb_host_t) SvPV(addrsv, addrlen);
4264 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4266 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4270 #ifdef HAS_GETHOSTENT
4271 hent = PerlSock_gethostent();
4273 DIE(aTHX_ PL_no_sock_func, "gethostent");
4276 #ifdef HOST_NOT_FOUND
4278 STATUS_NATIVE_SET(h_errno);
4281 if (GIMME != G_ARRAY) {
4282 PUSHs(sv = sv_newmortal());
4284 if (which == OP_GHBYNAME) {
4286 sv_setpvn(sv, hent->h_addr, hent->h_length);
4289 sv_setpv(sv, (char*)hent->h_name);
4295 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4296 sv_setpv(sv, (char*)hent->h_name);
4297 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4298 for (elem = hent->h_aliases; elem && *elem; elem++) {
4299 sv_catpv(sv, *elem);
4301 sv_catpvn(sv, " ", 1);
4303 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4304 sv_setiv(sv, (IV)hent->h_addrtype);
4305 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4306 len = hent->h_length;
4307 sv_setiv(sv, (IV)len);
4309 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4310 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4311 sv_setpvn(sv, *elem, len);
4314 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4316 sv_setpvn(sv, hent->h_addr, len);
4321 DIE(aTHX_ PL_no_sock_func, "gethostent");
4327 #ifdef HAS_GETNETBYNAME
4328 return pp_gnetent();
4330 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4336 #ifdef HAS_GETNETBYADDR
4337 return pp_gnetent();
4339 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4346 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4347 I32 which = PL_op->op_type;
4348 register char **elem;
4350 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4351 struct netent *PerlSock_getnetbyaddr(Netdb_net_t, int);
4352 struct netent *PerlSock_getnetbyname(Netdb_name_t);
4353 struct netent *PerlSock_getnetent(void);
4355 struct netent *nent;
4358 if (which == OP_GNBYNAME)
4359 #ifdef HAS_GETNETBYNAME
4360 nent = PerlSock_getnetbyname(POPpx);
4362 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4364 else if (which == OP_GNBYADDR) {
4365 #ifdef HAS_GETNETBYADDR
4366 int addrtype = POPi;
4367 Netdb_net_t addr = (Netdb_net_t) U_L(POPn);
4368 nent = PerlSock_getnetbyaddr(addr, addrtype);
4370 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4374 #ifdef HAS_GETNETENT
4375 nent = PerlSock_getnetent();
4377 DIE(aTHX_ PL_no_sock_func, "getnetent");
4381 if (GIMME != G_ARRAY) {
4382 PUSHs(sv = sv_newmortal());
4384 if (which == OP_GNBYNAME)
4385 sv_setiv(sv, (IV)nent->n_net);
4387 sv_setpv(sv, nent->n_name);
4393 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4394 sv_setpv(sv, nent->n_name);
4395 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4396 for (elem = nent->n_aliases; elem && *elem; elem++) {
4397 sv_catpv(sv, *elem);
4399 sv_catpvn(sv, " ", 1);
4401 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4402 sv_setiv(sv, (IV)nent->n_addrtype);
4403 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4404 sv_setiv(sv, (IV)nent->n_net);
4409 DIE(aTHX_ PL_no_sock_func, "getnetent");
4415 #ifdef HAS_GETPROTOBYNAME
4416 return pp_gprotoent();
4418 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4424 #ifdef HAS_GETPROTOBYNUMBER
4425 return pp_gprotoent();
4427 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4434 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4435 I32 which = PL_op->op_type;
4436 register char **elem;
4438 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4439 struct protoent *PerlSock_getprotobyname(Netdb_name_t);
4440 struct protoent *PerlSock_getprotobynumber(int);
4441 struct protoent *PerlSock_getprotoent(void);
4443 struct protoent *pent;
4446 if (which == OP_GPBYNAME)
4447 #ifdef HAS_GETPROTOBYNAME
4448 pent = PerlSock_getprotobyname(POPpx);
4450 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4452 else if (which == OP_GPBYNUMBER)
4453 #ifdef HAS_GETPROTOBYNUMBER
4454 pent = PerlSock_getprotobynumber(POPi);
4456 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4459 #ifdef HAS_GETPROTOENT
4460 pent = PerlSock_getprotoent();
4462 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4466 if (GIMME != G_ARRAY) {
4467 PUSHs(sv = sv_newmortal());
4469 if (which == OP_GPBYNAME)
4470 sv_setiv(sv, (IV)pent->p_proto);
4472 sv_setpv(sv, pent->p_name);
4478 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4479 sv_setpv(sv, pent->p_name);
4480 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4481 for (elem = pent->p_aliases; elem && *elem; elem++) {
4482 sv_catpv(sv, *elem);
4484 sv_catpvn(sv, " ", 1);
4486 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4487 sv_setiv(sv, (IV)pent->p_proto);
4492 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4498 #ifdef HAS_GETSERVBYNAME
4499 return pp_gservent();
4501 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4507 #ifdef HAS_GETSERVBYPORT
4508 return pp_gservent();
4510 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4517 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4518 I32 which = PL_op->op_type;
4519 register char **elem;
4521 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4522 struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t);
4523 struct servent *PerlSock_getservbyport(int, Netdb_name_t);
4524 struct servent *PerlSock_getservent(void);
4526 struct servent *sent;
4529 if (which == OP_GSBYNAME) {
4530 #ifdef HAS_GETSERVBYNAME
4531 char *proto = POPpx;
4534 if (proto && !*proto)
4537 sent = PerlSock_getservbyname(name, proto);
4539 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4542 else if (which == OP_GSBYPORT) {
4543 #ifdef HAS_GETSERVBYPORT
4544 char *proto = POPpx;
4545 unsigned short port = POPu;
4548 port = PerlSock_htons(port);
4550 sent = PerlSock_getservbyport(port, proto);
4552 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4556 #ifdef HAS_GETSERVENT
4557 sent = PerlSock_getservent();
4559 DIE(aTHX_ PL_no_sock_func, "getservent");
4563 if (GIMME != G_ARRAY) {
4564 PUSHs(sv = sv_newmortal());
4566 if (which == OP_GSBYNAME) {
4568 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4570 sv_setiv(sv, (IV)(sent->s_port));
4574 sv_setpv(sv, sent->s_name);
4580 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4581 sv_setpv(sv, sent->s_name);
4582 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4583 for (elem = sent->s_aliases; elem && *elem; elem++) {
4584 sv_catpv(sv, *elem);
4586 sv_catpvn(sv, " ", 1);
4588 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4590 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4592 sv_setiv(sv, (IV)(sent->s_port));
4594 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4595 sv_setpv(sv, sent->s_proto);
4600 DIE(aTHX_ PL_no_sock_func, "getservent");
4607 #ifdef HAS_SETHOSTENT
4608 PerlSock_sethostent(TOPi);
4611 DIE(aTHX_ PL_no_sock_func, "sethostent");
4618 #ifdef HAS_SETNETENT
4619 PerlSock_setnetent(TOPi);
4622 DIE(aTHX_ PL_no_sock_func, "setnetent");
4629 #ifdef HAS_SETPROTOENT
4630 PerlSock_setprotoent(TOPi);
4633 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4640 #ifdef HAS_SETSERVENT
4641 PerlSock_setservent(TOPi);
4644 DIE(aTHX_ PL_no_sock_func, "setservent");
4651 #ifdef HAS_ENDHOSTENT
4652 PerlSock_endhostent();
4656 DIE(aTHX_ PL_no_sock_func, "endhostent");
4663 #ifdef HAS_ENDNETENT
4664 PerlSock_endnetent();
4668 DIE(aTHX_ PL_no_sock_func, "endnetent");
4675 #ifdef HAS_ENDPROTOENT
4676 PerlSock_endprotoent();
4680 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4687 #ifdef HAS_ENDSERVENT
4688 PerlSock_endservent();
4692 DIE(aTHX_ PL_no_sock_func, "endservent");
4701 DIE(aTHX_ PL_no_func, "getpwnam");
4710 DIE(aTHX_ PL_no_func, "getpwuid");
4718 I32 which = PL_op->op_type;
4720 struct passwd *pwent;
4722 #if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4723 struct spwd *spwent = NULL;
4726 if (which == OP_GPWNAM)
4727 pwent = getpwnam(POPpx);
4728 else if (which == OP_GPWUID)
4729 pwent = getpwuid(POPi);
4732 pwent = (struct passwd *)getpwent();
4734 DIE(aTHX_ PL_no_func, "getpwent");
4738 if (which == OP_GPWNAM) {
4740 spwent = getspnam(pwent->pw_name);
4742 # ifdef HAS_GETSPUID /* AFAIK there isn't any anywhere. --jhi */
4743 else if (which == OP_GPWUID) {
4745 spwent = getspnam(pwent->pw_name);
4748 # ifdef HAS_GETSPENT
4750 spwent = (struct spwd *)getspent();
4755 if (GIMME != G_ARRAY) {
4756 PUSHs(sv = sv_newmortal());
4758 if (which == OP_GPWNAM)
4759 sv_setiv(sv, (IV)pwent->pw_uid);
4761 sv_setpv(sv, pwent->pw_name);
4767 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4768 sv_setpv(sv, pwent->pw_name);
4770 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4772 # if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4774 sv_setpv(sv, spwent->sp_pwdp);
4776 sv_setpv(sv, pwent->pw_passwd);
4778 sv_setpv(sv, pwent->pw_passwd);
4782 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4783 sv_setiv(sv, (IV)pwent->pw_uid);
4785 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4786 sv_setiv(sv, (IV)pwent->pw_gid);
4788 /* pw_change, pw_quota, and pw_age are mutually exclusive. */
4789 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4791 sv_setiv(sv, (IV)pwent->pw_change);
4794 sv_setiv(sv, (IV)pwent->pw_quota);
4797 sv_setpv(sv, pwent->pw_age);
4802 /* pw_class and pw_comment are mutually exclusive. */
4803 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4805 sv_setpv(sv, pwent->pw_class);
4808 sv_setpv(sv, pwent->pw_comment);
4812 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4814 sv_setpv(sv, pwent->pw_gecos);
4816 #ifndef INCOMPLETE_TAINTS
4817 /* pw_gecos is tainted because user himself can diddle with it. */
4821 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4822 sv_setpv(sv, pwent->pw_dir);
4824 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4825 sv_setpv(sv, pwent->pw_shell);
4828 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4829 sv_setiv(sv, (IV)pwent->pw_expire);
4834 DIE(aTHX_ PL_no_func, "getpwent");
4841 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
4843 # ifdef HAS_SETSPENT
4848 DIE(aTHX_ PL_no_func, "setpwent");
4855 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
4857 # ifdef HAS_ENDSPENT
4862 DIE(aTHX_ PL_no_func, "endpwent");
4871 DIE(aTHX_ PL_no_func, "getgrnam");
4880 DIE(aTHX_ PL_no_func, "getgrgid");
4888 I32 which = PL_op->op_type;
4889 register char **elem;
4891 struct group *grent;
4894 if (which == OP_GGRNAM)
4895 grent = (struct group *)getgrnam(POPpx);
4896 else if (which == OP_GGRGID)
4897 grent = (struct group *)getgrgid(POPi);
4900 grent = (struct group *)getgrent();
4902 DIE(aTHX_ PL_no_func, "getgrent");
4906 if (GIMME != G_ARRAY) {
4907 PUSHs(sv = sv_newmortal());
4909 if (which == OP_GGRNAM)
4910 sv_setiv(sv, (IV)grent->gr_gid);
4912 sv_setpv(sv, grent->gr_name);
4918 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4919 sv_setpv(sv, grent->gr_name);
4921 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4923 sv_setpv(sv, grent->gr_passwd);
4926 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4927 sv_setiv(sv, (IV)grent->gr_gid);
4929 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4930 for (elem = grent->gr_mem; elem && *elem; elem++) {
4931 sv_catpv(sv, *elem);
4933 sv_catpvn(sv, " ", 1);
4939 DIE(aTHX_ PL_no_func, "getgrent");
4946 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
4950 DIE(aTHX_ PL_no_func, "setgrent");
4957 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
4961 DIE(aTHX_ PL_no_func, "endgrent");
4971 if (!(tmps = PerlProc_getlogin()))
4973 PUSHp(tmps, strlen(tmps));
4976 DIE(aTHX_ PL_no_func, "getlogin");
4980 /* Miscellaneous. */
4985 djSP; dMARK; dORIGMARK; dTARGET;
4986 register I32 items = SP - MARK;
4987 unsigned long a[20];
4994 while (++MARK <= SP) {
4995 if (SvTAINTED(*MARK)) {
5001 TAINT_PROPER("syscall");
5004 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5005 * or where sizeof(long) != sizeof(char*). But such machines will
5006 * not likely have syscall implemented either, so who cares?
5008 while (++MARK <= SP) {
5009 if (SvNIOK(*MARK) || !i)
5010 a[i++] = SvIV(*MARK);
5011 else if (*MARK == &PL_sv_undef)
5014 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5020 DIE(aTHX_ "Too many args to syscall");
5022 DIE(aTHX_ "Too few args to syscall");
5024 retval = syscall(a[0]);
5027 retval = syscall(a[0],a[1]);
5030 retval = syscall(a[0],a[1],a[2]);
5033 retval = syscall(a[0],a[1],a[2],a[3]);
5036 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5039 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5042 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5045 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5049 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5052 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5055 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5059 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5063 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5067 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5068 a[10],a[11],a[12],a[13]);
5070 #endif /* atarist */
5076 DIE(aTHX_ PL_no_func, "syscall");
5080 #ifdef FCNTL_EMULATE_FLOCK
5082 /* XXX Emulate flock() with fcntl().
5083 What's really needed is a good file locking module.
5087 fcntl_emulate_flock(int fd, int operation)
5091 switch (operation & ~LOCK_NB) {
5093 flock.l_type = F_RDLCK;
5096 flock.l_type = F_WRLCK;
5099 flock.l_type = F_UNLCK;
5105 flock.l_whence = SEEK_SET;
5106 flock.l_start = flock.l_len = (Off_t)0;
5108 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5111 #endif /* FCNTL_EMULATE_FLOCK */
5113 #ifdef LOCKF_EMULATE_FLOCK
5115 /* XXX Emulate flock() with lockf(). This is just to increase
5116 portability of scripts. The calls are not completely
5117 interchangeable. What's really needed is a good file
5121 /* The lockf() constants might have been defined in <unistd.h>.
5122 Unfortunately, <unistd.h> causes troubles on some mixed
5123 (BSD/POSIX) systems, such as SunOS 4.1.3.
5125 Further, the lockf() constants aren't POSIX, so they might not be
5126 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5127 just stick in the SVID values and be done with it. Sigh.
5131 # define F_ULOCK 0 /* Unlock a previously locked region */
5134 # define F_LOCK 1 /* Lock a region for exclusive use */
5137 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5140 # define F_TEST 3 /* Test a region for other processes locks */
5144 lockf_emulate_flock(int fd, int operation)
5150 /* flock locks entire file so for lockf we need to do the same */
5152 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5153 if (pos > 0) /* is seekable and needs to be repositioned */
5154 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5155 pos = -1; /* seek failed, so don't seek back afterwards */
5158 switch (operation) {
5160 /* LOCK_SH - get a shared lock */
5162 /* LOCK_EX - get an exclusive lock */
5164 i = lockf (fd, F_LOCK, 0);
5167 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5168 case LOCK_SH|LOCK_NB:
5169 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5170 case LOCK_EX|LOCK_NB:
5171 i = lockf (fd, F_TLOCK, 0);
5173 if ((errno == EAGAIN) || (errno == EACCES))
5174 errno = EWOULDBLOCK;
5177 /* LOCK_UN - unlock (non-blocking is a no-op) */
5179 case LOCK_UN|LOCK_NB:
5180 i = lockf (fd, F_ULOCK, 0);
5183 /* Default - can't decipher operation */
5190 if (pos > 0) /* need to restore position of the handle */
5191 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5196 #endif /* LOCKF_EMULATE_FLOCK */