3 * Copyright (c) 1991-1999, 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)
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>
119 #include <sys/file.h>
122 /* Put this after #includes because fork and vfork prototypes may conflict. */
127 /* Put this after #includes because <unistd.h> defines _XOPEN_*. */
129 # if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__)
130 # define Sock_size_t Size_t
132 # define Sock_size_t int
137 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
140 # define my_chsize PerlLIO_chsize
145 #else /* no flock() */
147 /* fcntl.h might not have been included, even if it exists, because
148 the current Configure only sets I_FCNTL if it's needed to pick up
149 the *_OK constants. Make sure it has been included before testing
150 the fcntl() locking constants. */
151 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
155 # if defined(HAS_FCNTL) && defined(F_SETLK) && defined (F_SETLKW)
156 # define FLOCK fcntl_emulate_flock
157 # define FCNTL_EMULATE_FLOCK
158 # else /* no flock() or fcntl(F_SETLK,...) */
160 # define FLOCK lockf_emulate_flock
161 # define LOCKF_EMULATE_FLOCK
163 # endif /* no flock() or fcntl(F_SETLK,...) */
166 static int FLOCK (int, int);
169 * These are the flock() constants. Since this sytems doesn't have
170 * flock(), the values of the constants are probably not available.
184 # endif /* emulating flock() */
186 #endif /* no flock() */
189 static char zero_but_true[ZBTLEN + 1] = "0 but true";
191 #if defined(I_SYS_ACCESS) && !defined(R_OK)
192 # include <sys/access.h>
195 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
196 # define FD_CLOEXEC 1 /* NeXT needs this */
199 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
200 #undef PERL_EFF_ACCESS_W_OK
201 #undef PERL_EFF_ACCESS_X_OK
203 /* F_OK unused: if stat() cannot find it... */
205 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
206 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
207 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
208 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
209 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
212 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
213 # if defined(I_SYS_SECURITY)
214 # include <sys/security.h>
216 /* XXX Configure test needed for eaccess */
219 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
220 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
221 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
224 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
225 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
226 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
230 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
232 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
233 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
234 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
237 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
238 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
239 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
242 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
244 Uid_t ruid = getuid();
245 Uid_t euid = geteuid();
246 Gid_t rgid = getgid();
247 Gid_t egid = getegid();
251 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
252 Perl_croak(aTHX_ "switching effective uid is not implemented");
255 if (setreuid(euid, ruid))
258 if (setresuid(euid, ruid, (Uid_t)-1))
261 Perl_croak(aTHX_ "entering effective uid failed");
264 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
265 Perl_croak(aTHX_ "switching effective gid is not implemented");
268 if (setregid(egid, rgid))
271 if (setresgid(egid, rgid, (Gid_t)-1))
274 Perl_croak(aTHX_ "entering effective gid failed");
277 res = access(path, mode);
280 if (setreuid(ruid, euid))
283 if (setresuid(ruid, euid, (Uid_t)-1))
286 Perl_croak(aTHX_ "leaving effective uid failed");
289 if (setregid(rgid, egid))
292 if (setresgid(rgid, egid, (Gid_t)-1))
295 Perl_croak(aTHX_ "leaving effective gid failed");
300 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
301 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
302 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
305 #if !defined(PERL_EFF_ACCESS_R_OK)
307 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
309 Perl_croak(aTHX_ "switching effective uid is not implemented");
324 fp = PerlProc_popen(tmps, "r");
326 if (gimme == G_VOID) {
328 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
332 else if (gimme == G_SCALAR) {
333 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
334 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
345 if (sv_gets(sv, fp, 0) == Nullch) {
349 XPUSHs(sv_2mortal(sv));
350 if (SvLEN(sv) - SvCUR(sv) > 20) {
351 SvLEN_set(sv, SvCUR(sv)+1);
352 Renew(SvPVX(sv), SvLEN(sv), char);
357 STATUS_NATIVE_SET(PerlProc_pclose(fp));
358 TAINT; /* "I believe that this is not gratuitous!" */
361 STATUS_NATIVE_SET(-1);
362 if (gimme == G_SCALAR)
372 tryAMAGICunTARGET(iter, -1);
379 * The external globbing program may use things we can't control,
380 * so for security reasons we must assume the worst.
383 taint_proper(PL_no_security, "glob");
387 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
388 PL_last_in_gv = (GV*)*PL_stack_sp--;
390 SAVESPTR(PL_rs); /* This is not permanent, either. */
391 PL_rs = sv_2mortal(newSVpvn("\000", 1));
394 *SvPVX(PL_rs) = '\n';
398 result = do_readline();
403 #if 0 /* XXX never used! */
407 PL_last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*PL_stack_sp--)), n_a), TRUE,SVt_PVIO);
408 return do_readline();
414 PL_last_in_gv = cGVOP_gv;
415 return do_readline();
424 if (SP - MARK != 1) {
426 do_join(TARG, &PL_sv_no, MARK, SP);
433 tmps = SvPV(tmpsv, len);
436 (void)SvUPGRADE(error, SVt_PV);
437 if (SvPOK(error) && SvCUR(error))
438 sv_catpv(error, "\t...caught");
440 tmps = SvPV(tmpsv, len);
443 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
445 Perl_warn(aTHX_ "%_", tmpsv);
456 if (SP - MARK != 1) {
458 do_join(TARG, &PL_sv_no, MARK, SP);
460 tmps = SvPV(tmpsv, len);
466 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
470 (void)SvUPGRADE(error, SVt_PV);
471 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
473 SvSetSV(error,tmpsv);
474 else if (sv_isobject(error)) {
475 HV *stash = SvSTASH(SvRV(error));
476 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
478 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
479 SV *line = sv_2mortal(newSViv(CopLINE(PL_curcop)));
486 call_sv((SV*)GvCV(gv),
487 G_SCALAR|G_EVAL|G_KEEPERR);
488 sv_setsv(error,*PL_stack_sp--);
494 if (SvPOK(error) && SvCUR(error))
495 sv_catpv(error, "\t...propagated");
497 tmps = SvPV(tmpsv, len);
501 tmpsv = sv_2mortal(newSVpvn("Died", 4));
503 DIE(aTHX_ "%_", tmpsv);
526 DIE(aTHX_ PL_no_usym, "filehandle");
531 DIE(aTHX_ PL_no_usym, "filehandle");
533 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
535 if (mg = SvTIED_mg((SV*)gv, 'q')) {
537 XPUSHs(SvTIED_obj((SV*)gv, mg));
543 call_method("OPEN", G_SCALAR);
549 tmps = SvPV(sv, len);
550 if (do_open9(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, name, have_name))
551 PUSHi( (I32)PL_forkprocess );
552 else if (PL_forkprocess == 0) /* we are a new child */
570 if (mg = SvTIED_mg((SV*)gv, 'q')) {
572 XPUSHs(SvTIED_obj((SV*)gv, mg));
575 call_method("CLOSE", G_SCALAR);
581 PUSHs(boolSV(do_close(gv, TRUE)));
601 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
602 DIE(aTHX_ PL_no_usym, "filehandle");
607 do_close(rgv, FALSE);
609 do_close(wgv, FALSE);
611 if (PerlProc_pipe(fd) < 0)
614 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
615 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
616 IoIFP(wstio) = IoOFP(wstio);
620 if (!IoIFP(rstio) || !IoOFP(wstio)) {
621 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
622 else PerlLIO_close(fd[0]);
623 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
624 else PerlLIO_close(fd[1]);
627 #if defined(HAS_FCNTL) && defined(F_SETFD)
628 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
629 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
636 DIE(aTHX_ PL_no_func, "pipe");
652 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
654 XPUSHs(SvTIED_obj((SV*)gv, mg));
657 call_method("FILENO", G_SCALAR);
663 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io)))
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);
707 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
709 XPUSHs(SvTIED_obj((SV*)gv, mg));
712 call_method("BINMODE", G_SCALAR);
719 if (!(io = GvIO(gv)) || !(fp = IoIFP(io)))
722 if (do_binmode(fp,IoTYPE(io),TRUE))
737 I32 markoff = MARK - PL_stack_base;
744 switch(SvTYPE(varsv)) {
746 methname = "TIEHASH";
749 methname = "TIEARRAY";
752 methname = "TIEHANDLE";
756 methname = "TIESCALAR";
761 if (sv_isobject(*MARK)) {
763 PUSHSTACKi(PERLSI_MAGIC);
769 call_method(methname, G_SCALAR);
772 /* Not clear why we don't call call_method here too.
773 * perhaps to get different error message ?
775 stash = gv_stashsv(*MARK, FALSE);
776 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
777 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"",
778 methname, SvPV(*MARK,n_a));
781 PUSHSTACKi(PERLSI_MAGIC);
787 call_sv((SV*)GvCV(gv), G_SCALAR);
793 if (sv_isobject(sv)) {
794 sv_unmagic(varsv, how);
795 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
798 SP = PL_stack_base + markoff;
807 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
809 if (ckWARN(WARN_UNTIE)) {
811 if (mg = SvTIED_mg(sv, how)) {
812 if (mg && SvREFCNT(SvRV(mg->mg_obj)) > 1)
813 Perl_warner(aTHX_ WARN_UNTIE,
814 "untie attempted while %"UVuf" inner references still exist",
815 (UV)SvREFCNT(SvRV(mg->mg_obj)) - 1 ) ;
827 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
830 if (mg = SvTIED_mg(sv, how)) {
831 SV *osv = SvTIED_obj(sv, mg);
832 if (osv == mg->mg_obj)
833 osv = sv_mortalcopy(osv);
851 sv = sv_mortalcopy(&PL_sv_no);
852 sv_setpv(sv, "AnyDBM_File");
853 stash = gv_stashsv(sv, FALSE);
854 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
856 require_pv("AnyDBM_File.pm");
858 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
859 DIE(aTHX_ "No dbm on this machine");
869 PUSHs(sv_2mortal(newSViv(O_RDWR|O_CREAT)));
871 PUSHs(sv_2mortal(newSViv(O_RDWR)));
874 call_sv((SV*)GvCV(gv), G_SCALAR);
877 if (!sv_isobject(TOPs)) {
882 PUSHs(sv_2mortal(newSViv(O_RDONLY)));
885 call_sv((SV*)GvCV(gv), G_SCALAR);
889 if (sv_isobject(TOPs)) {
890 sv_unmagic((SV *) hv, 'P');
891 sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
913 struct timeval timebuf;
914 struct timeval *tbuf = &timebuf;
918 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
923 # if BYTEORDER & 0xf0000
924 # define ORDERBYTE (0x88888888 - BYTEORDER)
926 # define ORDERBYTE (0x4444 - BYTEORDER)
932 for (i = 1; i <= 3; i++) {
940 /* little endians can use vecs directly */
941 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
942 # if SELECT_MIN_BITS > 1
943 /* If SELECT_MIN_BITS is greater than one we most probably will want
944 * to align the sizes with SELECT_MIN_BITS/8 because for example
945 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
946 * UNIX, Solaris, NeXT, Rhapsody) the smallest quantum select() operates
947 * on (sets/tests/clears bits) is 32 bits. */
948 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
950 growsize = sizeof(fd_set);
959 masksize = NFDBITS / NBBY;
961 masksize = sizeof(long); /* documented int, everyone seems to use long */
963 growsize = maxlen + (masksize - (maxlen % masksize));
964 Zero(&fd_sets[0], 4, char*);
972 timebuf.tv_sec = (long)value;
973 value -= (NV)timebuf.tv_sec;
974 timebuf.tv_usec = (long)(value * 1000000.0);
977 tbuf = Null(struct timeval*);
979 for (i = 1; i <= 3; i++) {
986 SvPV_force(sv,n_a); /* force string conversion */
989 Sv_Grow(sv, growsize);
993 while (++j <= growsize) {
997 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
999 New(403, fd_sets[i], growsize, char);
1000 for (offset = 0; offset < growsize; offset += masksize) {
1001 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1002 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1005 fd_sets[i] = SvPVX(sv);
1009 nfound = PerlSock_select(
1011 (Select_fd_set_t) fd_sets[1],
1012 (Select_fd_set_t) fd_sets[2],
1013 (Select_fd_set_t) fd_sets[3],
1015 for (i = 1; i <= 3; i++) {
1018 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1020 for (offset = 0; offset < growsize; offset += masksize) {
1021 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1022 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1024 Safefree(fd_sets[i]);
1031 if (GIMME == G_ARRAY && tbuf) {
1032 value = (NV)(timebuf.tv_sec) +
1033 (NV)(timebuf.tv_usec) / 1000000.0;
1034 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1035 sv_setnv(sv, value);
1039 DIE(aTHX_ "select not implemented");
1044 Perl_setdefout(pTHX_ GV *gv)
1048 (void)SvREFCNT_inc(gv);
1050 SvREFCNT_dec(PL_defoutgv);
1057 GV *newdefout, *egv;
1060 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1062 egv = GvEGV(PL_defoutgv);
1067 XPUSHs(&PL_sv_undef);
1069 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1070 if (gvp && *gvp == egv) {
1071 gv_efullname3(TARG, PL_defoutgv, Nullch);
1075 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1080 if (!GvIO(newdefout))
1081 gv_IOadd(newdefout);
1082 setdefout(newdefout);
1101 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1102 I32 gimme = GIMME_V;
1104 XPUSHs(SvTIED_obj((SV*)gv, mg));
1107 call_method("GETC", gimme);
1110 if (gimme == G_SCALAR)
1111 SvSetMagicSV_nosteal(TARG, TOPs);
1114 if (!gv || do_eof(gv)) /* make sure we have fp with something */
1117 sv_setpv(TARG, " ");
1118 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1125 return pp_sysread();
1129 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1132 register PERL_CONTEXT *cx;
1133 I32 gimme = GIMME_V;
1134 AV* padlist = CvPADLIST(cv);
1135 SV** svp = AvARRAY(padlist);
1141 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1143 SAVEVPTR(PL_curpad);
1144 PL_curpad = AvARRAY((AV*)svp[1]);
1146 setdefout(gv); /* locally select filehandle so $% et al work */
1178 SV *tmpsv = sv_newmortal();
1179 gv_efullname3(tmpsv, fgv, Nullch);
1180 DIE(aTHX_ "Undefined format \"%s\" called",SvPVX(tmpsv));
1182 DIE(aTHX_ "Not a format reference");
1185 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1187 IoFLAGS(io) &= ~IOf_DIDTOP;
1188 return doform(cv,gv,PL_op->op_next);
1194 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1195 register IO *io = GvIOp(gv);
1196 PerlIO *ofp = IoOFP(io);
1200 register PERL_CONTEXT *cx;
1202 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1203 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1204 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1205 PL_formtarget != PL_toptarget)
1209 if (!IoTOP_GV(io)) {
1213 if (!IoTOP_NAME(io)) {
1214 if (!IoFMT_NAME(io))
1215 IoFMT_NAME(io) = savepv(GvNAME(gv));
1216 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1217 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1218 if ((topgv && GvFORM(topgv)) ||
1219 !gv_fetchpv("top",FALSE,SVt_PVFM))
1220 IoTOP_NAME(io) = savepv(SvPVX(topname));
1222 IoTOP_NAME(io) = savepv("top");
1224 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1225 if (!topgv || !GvFORM(topgv)) {
1226 IoLINES_LEFT(io) = 100000000;
1229 IoTOP_GV(io) = topgv;
1231 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1232 I32 lines = IoLINES_LEFT(io);
1233 char *s = SvPVX(PL_formtarget);
1234 if (lines <= 0) /* Yow, header didn't even fit!!! */
1236 while (lines-- > 0) {
1237 s = strchr(s, '\n');
1243 PerlIO_write(ofp, SvPVX(PL_formtarget), s - SvPVX(PL_formtarget));
1244 sv_chop(PL_formtarget, s);
1245 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1248 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1249 PerlIO_write(ofp, SvPVX(PL_formfeed), SvCUR(PL_formfeed));
1250 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1252 PL_formtarget = PL_toptarget;
1253 IoFLAGS(io) |= IOf_DIDTOP;
1256 DIE(aTHX_ "bad top format reference");
1259 SV *tmpsv = sv_newmortal();
1260 gv_efullname3(tmpsv, fgv, Nullch);
1261 DIE(aTHX_ "Undefined top format \"%s\" called",SvPVX(tmpsv));
1264 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1265 return doform(cv,gv,PL_op);
1269 POPBLOCK(cx,PL_curpm);
1275 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1276 SV* sv = sv_newmortal();
1277 gv_efullname3(sv, gv, Nullch);
1279 Perl_warner(aTHX_ WARN_IO,
1280 "Filehandle %s opened only for input",
1282 else if (ckWARN(WARN_CLOSED))
1283 Perl_warner(aTHX_ WARN_CLOSED,
1284 "Write on closed filehandle %s", SvPV_nolen(sv));
1289 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1290 if (ckWARN(WARN_IO))
1291 Perl_warner(aTHX_ WARN_IO, "page overflow");
1293 if (!PerlIO_write(ofp, SvPVX(PL_formtarget), SvCUR(PL_formtarget)) ||
1297 FmLINES(PL_formtarget) = 0;
1298 SvCUR_set(PL_formtarget, 0);
1299 *SvEND(PL_formtarget) = '\0';
1300 if (IoFLAGS(io) & IOf_FLUSH)
1301 (void)PerlIO_flush(fp);
1305 PL_formtarget = PL_bodytarget;
1307 return pop_return();
1312 djSP; dMARK; dORIGMARK;
1320 if (PL_op->op_flags & OPf_STACKED)
1325 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1326 if (MARK == ORIGMARK) {
1329 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1333 *MARK = SvTIED_obj((SV*)gv, mg);
1336 call_method("PRINTF", G_SCALAR);
1339 MARK = ORIGMARK + 1;
1346 if (!(io = GvIO(gv))) {
1347 if (ckWARN(WARN_UNOPENED)) {
1348 gv_efullname3(sv, gv, Nullch);
1349 Perl_warner(aTHX_ WARN_UNOPENED,
1350 "Filehandle %s never opened", SvPV(sv,n_a));
1352 SETERRNO(EBADF,RMS$_IFI);
1355 else if (!(fp = IoOFP(io))) {
1356 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1357 gv_efullname3(sv, gv, Nullch);
1359 Perl_warner(aTHX_ WARN_IO,
1360 "Filehandle %s opened only for input",
1362 else if (ckWARN(WARN_CLOSED))
1363 Perl_warner(aTHX_ WARN_CLOSED,
1364 "printf on closed filehandle %s", SvPV(sv,n_a));
1366 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
1370 do_sprintf(sv, SP - MARK, MARK + 1);
1371 if (!do_print(sv, fp))
1374 if (IoFLAGS(io) & IOf_FLUSH)
1375 if (PerlIO_flush(fp) == EOF)
1386 PUSHs(&PL_sv_undef);
1407 /* Need TIEHANDLE method ? */
1409 tmps = SvPV(sv, len);
1410 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1411 IoLINES(GvIOp(gv)) = 0;
1415 PUSHs(&PL_sv_undef);
1422 djSP; dMARK; dORIGMARK; dTARGET;
1428 Sock_size_t bufsize;
1434 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) &&
1435 (mg = SvTIED_mg((SV*)gv, 'q')))
1440 *MARK = SvTIED_obj((SV*)gv, mg);
1442 call_method("READ", G_SCALAR);
1455 sv_setpvn(bufsv, "", 0);
1456 buffer = SvPV_force(bufsv, blen);
1457 length = SvIVx(*++MARK);
1459 DIE(aTHX_ "Negative length");
1462 offset = SvIVx(*++MARK);
1466 if (!io || !IoIFP(io))
1469 if (PL_op->op_type == OP_RECV) {
1470 char namebuf[MAXPATHLEN];
1471 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE)
1472 bufsize = sizeof (struct sockaddr_in);
1474 bufsize = sizeof namebuf;
1476 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1480 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1484 buffer = SvGROW(bufsv, length+1);
1485 /* 'offset' means 'flags' here */
1486 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1487 (struct sockaddr *)namebuf, &bufsize);
1490 SvCUR_set(bufsv, length);
1491 *SvEND(bufsv) = '\0';
1492 (void)SvPOK_only(bufsv);
1494 /* This should not be marked tainted if the fp is marked clean */
1495 if (!(IoFLAGS(io) & IOf_UNTAINT))
1496 SvTAINTED_on(bufsv);
1498 sv_setpvn(TARG, namebuf, bufsize);
1503 if (PL_op->op_type == OP_RECV)
1504 DIE(aTHX_ PL_no_sock_func, "recv");
1508 DIE(aTHX_ "Offset outside string");
1511 bufsize = SvCUR(bufsv);
1512 buffer = SvGROW(bufsv, length+offset+1);
1513 if (offset > bufsize) { /* Zero any newly allocated space */
1514 Zero(buffer+bufsize, offset-bufsize, char);
1516 if (PL_op->op_type == OP_SYSREAD) {
1517 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1518 if (IoTYPE(io) == 's') {
1519 length = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1520 buffer+offset, length, 0);
1525 length = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1526 buffer+offset, length);
1530 #ifdef HAS_SOCKET__bad_code_maybe
1531 if (IoTYPE(io) == 's') {
1532 char namebuf[MAXPATHLEN];
1533 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1534 bufsize = sizeof (struct sockaddr_in);
1536 bufsize = sizeof namebuf;
1538 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer+offset, length, 0,
1539 (struct sockaddr *)namebuf, &bufsize);
1544 length = PerlIO_read(IoIFP(io), buffer+offset, length);
1545 /* fread() returns 0 on both error and EOF */
1546 if (length == 0 && PerlIO_error(IoIFP(io)))
1550 if ((IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
1551 || IoIFP(io) == PerlIO_stderr()) && ckWARN(WARN_IO))
1553 SV* sv = sv_newmortal();
1554 gv_efullname3(sv, gv, Nullch);
1555 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
1560 SvCUR_set(bufsv, length+offset);
1561 *SvEND(bufsv) = '\0';
1562 (void)SvPOK_only(bufsv);
1564 /* This should not be marked tainted if the fp is marked clean */
1565 if (!(IoFLAGS(io) & IOf_UNTAINT))
1566 SvTAINTED_on(bufsv);
1579 int items = (SP - PL_stack_base) - TOPMARK;
1583 sv = sv_2mortal(newSViv(sv_len(*SP)));
1592 djSP; dMARK; dORIGMARK; dTARGET;
1603 if (PL_op->op_type == OP_SYSWRITE && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1607 *MARK = SvTIED_obj((SV*)gv, mg);
1609 call_method("WRITE", G_SCALAR);
1620 buffer = SvPV(bufsv, blen);
1621 #if Off_t_SIZE > IVSIZE
1622 length = SvNVx(*++MARK);
1624 length = SvIVx(*++MARK);
1627 DIE(aTHX_ "Negative length");
1630 if (!io || !IoIFP(io)) {
1632 if (ckWARN(WARN_CLOSED)) {
1633 if (PL_op->op_type == OP_SYSWRITE)
1634 Perl_warner(aTHX_ WARN_CLOSED, "Syswrite on closed filehandle");
1636 Perl_warner(aTHX_ WARN_CLOSED, "Send on closed socket");
1639 else if (PL_op->op_type == OP_SYSWRITE) {
1641 #if Off_t_SIZE > IVSIZE
1642 offset = SvNVx(*++MARK);
1644 offset = SvIVx(*++MARK);
1648 DIE(aTHX_ "Offset outside string");
1650 } else if (offset >= blen && blen > 0)
1651 DIE(aTHX_ "Offset outside string");
1654 if (length > blen - offset)
1655 length = blen - offset;
1656 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1657 if (IoTYPE(io) == 's') {
1658 length = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1659 buffer+offset, length, 0);
1664 /* See the note at doio.c:do_print about filesize limits. --jhi */
1665 length = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1666 buffer+offset, length);
1670 else if (SP > MARK) {
1673 sockbuf = SvPVx(*++MARK, mlen);
1674 length = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen, length,
1675 (struct sockaddr *)sockbuf, mlen);
1678 length = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1682 DIE(aTHX_ PL_no_sock_func, "send");
1697 return pp_sysread();
1707 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1709 gv = PL_last_in_gv = PL_argvgv;
1711 if (io && !IoIFP(io)) {
1712 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1714 IoFLAGS(io) &= ~IOf_START;
1715 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1716 sv_setpvn(GvSV(gv), "-", 1);
1717 SvSETMAGIC(GvSV(gv));
1719 else if (!nextargv(gv))
1724 gv = PL_last_in_gv; /* eof */
1727 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1729 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1731 XPUSHs(SvTIED_obj((SV*)gv, mg));
1734 call_method("EOF", G_SCALAR);
1740 PUSHs(boolSV(!gv || do_eof(gv)));
1753 gv = PL_last_in_gv = (GV*)POPs;
1755 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1757 XPUSHs(SvTIED_obj((SV*)gv, mg));
1760 call_method("TELL", G_SCALAR);
1766 #if LSEEKSIZE > IVSIZE
1767 PUSHn( do_tell(gv) );
1769 PUSHi( do_tell(gv) );
1776 return pp_sysseek();
1784 #if LSEEKSIZE > IVSIZE
1785 Off_t offset = (Off_t)SvNVx(POPs);
1787 Off_t offset = (Off_t)SvIVx(POPs);
1791 gv = PL_last_in_gv = (GV*)POPs;
1793 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1795 XPUSHs(SvTIED_obj((SV*)gv, mg));
1796 XPUSHs(sv_2mortal(newSViv((IV) offset)));
1797 XPUSHs(sv_2mortal(newSViv((IV) whence)));
1800 call_method("SEEK", G_SCALAR);
1806 if (PL_op->op_type == OP_SEEK)
1807 PUSHs(boolSV(do_seek(gv, offset, whence)));
1809 Off_t n = do_sysseek(gv, offset, whence);
1811 PUSHs(&PL_sv_undef);
1814 #if LSEEKSIZE > IVSIZE
1819 : newSVpvn(zero_but_true, ZBTLEN);
1820 PUSHs(sv_2mortal(sv));
1829 Off_t len = (Off_t)POPn;
1835 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
1836 if (PL_op->op_flags & OPf_SPECIAL) {
1837 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
1839 TAINT_PROPER("truncate");
1840 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1842 ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1844 my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1853 if (SvTYPE(sv) == SVt_PVGV) {
1854 tmpgv = (GV*)sv; /* *main::FRED for example */
1857 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1858 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
1862 name = SvPV(sv, n_a);
1863 TAINT_PROPER("truncate");
1865 if (truncate(name, len) < 0)
1870 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
1873 if (my_chsize(tmpfd, len) < 0)
1875 PerlLIO_close(tmpfd);
1884 SETERRNO(EBADF,RMS$_IFI);
1887 DIE(aTHX_ "truncate not implemented");
1900 unsigned int func = U_I(POPn);
1901 int optype = PL_op->op_type;
1907 if (!io || !argsv || !IoIFP(io)) {
1908 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
1912 if (SvPOK(argsv) || !SvNIOK(argsv)) {
1915 s = SvPV_force(argsv, len);
1916 need = IOCPARM_LEN(func);
1918 s = Sv_Grow(argsv, need + 1);
1919 SvCUR_set(argsv, need);
1922 s[SvCUR(argsv)] = 17; /* a little sanity check here */
1925 retval = SvIV(argsv);
1926 s = INT2PTR(char*,retval); /* ouch */
1929 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1931 if (optype == OP_IOCTL)
1933 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
1935 DIE(aTHX_ "ioctl is not implemented");
1939 #if defined(OS2) && defined(__EMX__)
1940 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
1942 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
1945 DIE(aTHX_ "fcntl is not implemented");
1949 if (s[SvCUR(argsv)] != 17)
1950 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
1951 PL_op_name[optype]);
1952 s[SvCUR(argsv)] = 0; /* put our null back */
1953 SvSETMAGIC(argsv); /* Assume it has changed */
1962 PUSHp(zero_but_true, ZBTLEN);
1982 fp = IoIFP(GvIOp(gv));
1986 (void)PerlIO_flush(fp);
1987 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
1994 DIE(aTHX_ PL_no_func, "flock()");
2006 int protocol = POPi;
2014 SETERRNO(EBADF,LIB$_INVARG);
2020 do_close(gv, FALSE);
2022 TAINT_PROPER("socket");
2023 fd = PerlSock_socket(domain, type, protocol);
2026 IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */
2027 IoOFP(io) = PerlIO_fdopen(fd, "w");
2029 if (!IoIFP(io) || !IoOFP(io)) {
2030 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2031 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2032 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2038 DIE(aTHX_ PL_no_sock_func, "socket");
2045 #ifdef HAS_SOCKETPAIR
2050 int protocol = POPi;
2063 do_close(gv1, FALSE);
2065 do_close(gv2, FALSE);
2067 TAINT_PROPER("socketpair");
2068 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2070 IoIFP(io1) = PerlIO_fdopen(fd[0], "r");
2071 IoOFP(io1) = PerlIO_fdopen(fd[0], "w");
2073 IoIFP(io2) = PerlIO_fdopen(fd[1], "r");
2074 IoOFP(io2) = PerlIO_fdopen(fd[1], "w");
2076 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2077 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2078 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2079 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2080 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2081 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2082 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2088 DIE(aTHX_ PL_no_sock_func, "socketpair");
2096 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2097 extern GETPRIVMODE();
2098 extern GETUSERMODE();
2103 register IO *io = GvIOn(gv);
2107 int mpeprivmode = 0;
2110 if (!io || !IoIFP(io))
2113 addr = SvPV(addrsv, len);
2114 TAINT_PROPER("bind");
2115 #ifdef MPE /* Deal with MPE bind() peculiarities */
2116 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2117 /* The address *MUST* stupidly be zero. */
2118 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2119 /* PRIV mode is required to bind() to ports < 1024. */
2120 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2121 ((struct sockaddr_in *)addr)->sin_port > 0) {
2122 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2127 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2128 (struct sockaddr *)addr, len) >= 0)
2131 #ifdef MPE /* Switch back to USER mode */
2142 if (ckWARN(WARN_CLOSED))
2143 Perl_warner(aTHX_ WARN_CLOSED, "bind() on closed fd");
2144 SETERRNO(EBADF,SS$_IVCHAN);
2147 DIE(aTHX_ PL_no_sock_func, "bind");
2158 register IO *io = GvIOn(gv);
2161 if (!io || !IoIFP(io))
2164 addr = SvPV(addrsv, len);
2165 TAINT_PROPER("connect");
2166 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2172 if (ckWARN(WARN_CLOSED))
2173 Perl_warner(aTHX_ WARN_CLOSED, "connect() on closed fd");
2174 SETERRNO(EBADF,SS$_IVCHAN);
2177 DIE(aTHX_ PL_no_sock_func, "connect");
2187 register IO *io = GvIOn(gv);
2189 if (!io || !IoIFP(io))
2192 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2198 if (ckWARN(WARN_CLOSED))
2199 Perl_warner(aTHX_ WARN_CLOSED, "listen() on closed fd");
2200 SETERRNO(EBADF,SS$_IVCHAN);
2203 DIE(aTHX_ PL_no_sock_func, "listen");
2215 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2216 Sock_size_t len = sizeof saddr;
2228 if (!gstio || !IoIFP(gstio))
2233 do_close(ngv, FALSE);
2235 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2238 IoIFP(nstio) = PerlIO_fdopen(fd, "r");
2239 IoOFP(nstio) = PerlIO_fdopen(fd, "w");
2240 IoTYPE(nstio) = 's';
2241 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2242 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2243 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2244 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2248 PUSHp((char *)&saddr, len);
2252 if (ckWARN(WARN_CLOSED))
2253 Perl_warner(aTHX_ WARN_CLOSED, "accept() on closed fd");
2254 SETERRNO(EBADF,SS$_IVCHAN);
2260 DIE(aTHX_ PL_no_sock_func, "accept");
2270 register IO *io = GvIOn(gv);
2272 if (!io || !IoIFP(io))
2275 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2279 if (ckWARN(WARN_CLOSED))
2280 Perl_warner(aTHX_ WARN_CLOSED, "shutdown() on closed fd");
2281 SETERRNO(EBADF,SS$_IVCHAN);
2284 DIE(aTHX_ PL_no_sock_func, "shutdown");
2291 return pp_ssockopt();
2293 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2301 int optype = PL_op->op_type;
2304 unsigned int optname;
2310 if (optype == OP_GSOCKOPT)
2311 sv = sv_2mortal(NEWSV(22, 257));
2314 optname = (unsigned int) POPi;
2315 lvl = (unsigned int) POPi;
2319 if (!io || !IoIFP(io))
2322 fd = PerlIO_fileno(IoIFP(io));
2326 (void)SvPOK_only(sv);
2330 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2345 aint = (int)SvIV(sv);
2349 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2358 if (ckWARN(WARN_CLOSED))
2359 Perl_warner(aTHX_ WARN_CLOSED, "[gs]etsockopt() on closed fd");
2360 SETERRNO(EBADF,SS$_IVCHAN);
2365 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2372 return pp_getpeername();
2374 DIE(aTHX_ PL_no_sock_func, "getsockname");
2382 int optype = PL_op->op_type;
2386 register IO *io = GvIOn(gv);
2389 if (!io || !IoIFP(io))
2392 sv = sv_2mortal(NEWSV(22, 257));
2393 (void)SvPOK_only(sv);
2397 fd = PerlIO_fileno(IoIFP(io));
2399 case OP_GETSOCKNAME:
2400 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2403 case OP_GETPEERNAME:
2404 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2406 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2408 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";
2409 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2410 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2411 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2412 sizeof(u_short) + sizeof(struct in_addr))) {
2419 #ifdef BOGUS_GETNAME_RETURN
2420 /* Interactive Unix, getpeername() and getsockname()
2421 does not return valid namelen */
2422 if (len == BOGUS_GETNAME_RETURN)
2423 len = sizeof(struct sockaddr);
2431 if (ckWARN(WARN_CLOSED))
2432 Perl_warner(aTHX_ WARN_CLOSED, "get{sock, peer}name() on closed fd");
2433 SETERRNO(EBADF,SS$_IVCHAN);
2438 DIE(aTHX_ PL_no_sock_func, "getpeername");
2457 if (PL_op->op_flags & OPf_REF) {
2460 if (tmpgv != PL_defgv) {
2461 PL_laststype = OP_STAT;
2463 sv_setpv(PL_statname, "");
2464 PL_laststatval = (GvIO(tmpgv) && IoIFP(GvIOp(tmpgv))
2465 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &PL_statcache) : -1);
2467 if (PL_laststatval < 0)
2472 if (SvTYPE(sv) == SVt_PVGV) {
2476 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2477 tmpgv = (GV*)SvRV(sv);
2480 sv_setpv(PL_statname, SvPV(sv,n_a));
2483 PL_laststype = PL_op->op_type;
2484 if (PL_op->op_type == OP_LSTAT)
2485 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2488 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2489 if (PL_laststatval < 0) {
2490 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2491 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
2497 if (gimme != G_ARRAY) {
2498 if (gimme != G_VOID)
2499 XPUSHs(boolSV(max));
2505 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2506 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2507 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mode)));
2508 PUSHs(sv_2mortal(newSViv(PL_statcache.st_nlink)));
2509 #if Uid_t_size > IVSIZE
2510 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2512 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2514 #if Gid_t_size > IVSIZE
2515 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2517 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2519 #ifdef USE_STAT_RDEV
2520 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2522 PUSHs(sv_2mortal(newSVpvn("", 0)));
2524 #if Off_t_size > IVSIZE
2525 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size)));
2527 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2530 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2531 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2532 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2534 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2535 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2536 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2538 #ifdef USE_STAT_BLOCKS
2539 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blksize)));
2540 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blocks)));
2542 PUSHs(sv_2mortal(newSVpvn("", 0)));
2543 PUSHs(sv_2mortal(newSVpvn("", 0)));
2553 #if defined(HAS_ACCESS) && defined(R_OK)
2555 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2556 result = access(TOPpx, R_OK);
2571 if (cando(S_IRUSR, 0, &PL_statcache))
2580 #if defined(HAS_ACCESS) && defined(W_OK)
2582 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2583 result = access(TOPpx, W_OK);
2598 if (cando(S_IWUSR, 0, &PL_statcache))
2607 #if defined(HAS_ACCESS) && defined(X_OK)
2609 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2610 result = access(TOPpx, X_OK);
2625 if (cando(S_IXUSR, 0, &PL_statcache))
2634 #ifdef PERL_EFF_ACCESS_R_OK
2636 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2637 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2652 if (cando(S_IRUSR, 1, &PL_statcache))
2661 #ifdef PERL_EFF_ACCESS_W_OK
2663 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2664 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2679 if (cando(S_IWUSR, 1, &PL_statcache))
2688 #ifdef PERL_EFF_ACCESS_X_OK
2690 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2691 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2706 if (cando(S_IXUSR, 1, &PL_statcache))
2713 I32 result = my_stat();
2722 return pp_ftrowned();
2727 I32 result = my_stat();
2731 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
2739 I32 result = my_stat();
2743 if (PL_statcache.st_size == 0)
2750 I32 result = my_stat();
2754 #if Off_t_size > IVSIZE
2755 PUSHn(PL_statcache.st_size);
2757 PUSHi(PL_statcache.st_size);
2764 I32 result = my_stat();
2768 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
2774 I32 result = my_stat();
2778 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
2784 I32 result = my_stat();
2788 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
2794 I32 result = my_stat();
2798 if (S_ISSOCK(PL_statcache.st_mode))
2805 I32 result = my_stat();
2809 if (S_ISCHR(PL_statcache.st_mode))
2816 I32 result = my_stat();
2820 if (S_ISBLK(PL_statcache.st_mode))
2827 I32 result = my_stat();
2831 if (S_ISREG(PL_statcache.st_mode))
2838 I32 result = my_stat();
2842 if (S_ISDIR(PL_statcache.st_mode))
2849 I32 result = my_stat();
2853 if (S_ISFIFO(PL_statcache.st_mode))
2860 I32 result = my_lstat();
2864 if (S_ISLNK(PL_statcache.st_mode))
2873 I32 result = my_stat();
2877 if (PL_statcache.st_mode & S_ISUID)
2887 I32 result = my_stat();
2891 if (PL_statcache.st_mode & S_ISGID)
2901 I32 result = my_stat();
2905 if (PL_statcache.st_mode & S_ISVTX)
2916 char *tmps = Nullch;
2919 if (PL_op->op_flags & OPf_REF)
2921 else if (isGV(TOPs))
2923 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2924 gv = (GV*)SvRV(POPs);
2926 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
2928 if (GvIO(gv) && IoIFP(GvIOp(gv)))
2929 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
2930 else if (tmps && isDIGIT(*tmps))
2934 if (PerlLIO_isatty(fd))
2939 #if defined(atarist) /* this will work with atariST. Configure will
2940 make guesses for other systems. */
2941 # define FILE_base(f) ((f)->_base)
2942 # define FILE_ptr(f) ((f)->_ptr)
2943 # define FILE_cnt(f) ((f)->_cnt)
2944 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
2954 register STDCHAR *s;
2961 if (PL_op->op_flags & OPf_REF)
2963 else if (isGV(TOPs))
2965 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2966 gv = (GV*)SvRV(POPs);
2972 if (gv == PL_defgv) {
2974 io = GvIO(PL_statgv);
2977 goto really_filename;
2982 PL_laststatval = -1;
2983 sv_setpv(PL_statname, "");
2984 io = GvIO(PL_statgv);
2986 if (io && IoIFP(io)) {
2987 if (! PerlIO_has_base(IoIFP(io)))
2988 DIE(aTHX_ "-T and -B not implemented on filehandles");
2989 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2990 if (PL_laststatval < 0)
2992 if (S_ISDIR(PL_statcache.st_mode)) /* handle NFS glitch */
2993 if (PL_op->op_type == OP_FTTEXT)
2997 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
2998 i = PerlIO_getc(IoIFP(io));
3000 (void)PerlIO_ungetc(IoIFP(io),i);
3002 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3004 len = PerlIO_get_bufsiz(IoIFP(io));
3005 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3006 /* sfio can have large buffers - limit to 512 */
3011 if (ckWARN(WARN_UNOPENED)) {
3013 Perl_warner(aTHX_ WARN_UNOPENED, "Test on unopened file <%s>",
3016 SETERRNO(EBADF,RMS$_IFI);
3024 PL_laststatval = -1;
3025 sv_setpv(PL_statname, SvPV(sv, n_a));
3026 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3027 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3028 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
3031 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3032 if (PL_laststatval < 0) {
3033 (void)PerlIO_close(fp);
3036 do_binmode(fp, '<', TRUE);
3037 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3038 (void)PerlIO_close(fp);
3040 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3041 RETPUSHNO; /* special case NFS directories */
3042 RETPUSHYES; /* null file is anything */
3047 /* now scan s to look for textiness */
3048 /* XXX ASCII dependent code */
3050 #if defined(DOSISH) || defined(USEMYBINMODE)
3051 /* ignore trailing ^Z on short files */
3052 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3056 for (i = 0; i < len; i++, s++) {
3057 if (!*s) { /* null never allowed in text */
3062 else if (!(isPRINT(*s) || isSPACE(*s)))
3065 else if (*s & 128) {
3067 if (!(PL_op->op_private & OPpLOCALE) || !isALPHA_LC(*s))
3072 *s != '\n' && *s != '\r' && *s != '\b' &&
3073 *s != '\t' && *s != '\f' && *s != 27)
3078 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3102 if (!tmps || !*tmps) {
3103 svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE);
3105 tmps = SvPV(*svp, n_a);
3107 if (!tmps || !*tmps) {
3108 svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE);
3110 tmps = SvPV(*svp, n_a);
3113 if (!tmps || !*tmps) {
3114 svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE);
3116 tmps = SvPV(*svp, n_a);
3119 TAINT_PROPER("chdir");
3120 PUSHi( PerlDir_chdir(tmps) >= 0 );
3122 /* Clear the DEFAULT element of ENV so we'll get the new value
3124 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3131 djSP; dMARK; dTARGET;
3134 value = (I32)apply(PL_op->op_type, MARK, SP);
3139 DIE(aTHX_ PL_no_func, "Unsupported function chown");
3150 TAINT_PROPER("chroot");
3151 PUSHi( chroot(tmps) >= 0 );
3154 DIE(aTHX_ PL_no_func, "chroot");
3160 djSP; dMARK; dTARGET;
3162 value = (I32)apply(PL_op->op_type, MARK, SP);
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);
3194 char *tmps2 = POPpx;
3195 char *tmps = SvPV(TOPs, n_a);
3196 TAINT_PROPER("rename");
3198 anum = PerlLIO_rename(tmps, tmps2);
3200 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3201 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3204 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3205 (void)UNLINK(tmps2);
3206 if (!(anum = link(tmps, tmps2)))
3207 anum = UNLINK(tmps);
3220 char *tmps2 = POPpx;
3221 char *tmps = SvPV(TOPs, n_a);
3222 TAINT_PROPER("link");
3223 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3225 DIE(aTHX_ PL_no_func, "Unsupported function link");
3235 char *tmps2 = POPpx;
3236 char *tmps = SvPV(TOPs, n_a);
3237 TAINT_PROPER("symlink");
3238 SETi( symlink(tmps, tmps2) >= 0 );
3241 DIE(aTHX_ PL_no_func, "symlink");
3250 char buf[MAXPATHLEN];
3254 #ifndef INCOMPLETE_TAINTS
3258 len = readlink(tmps, buf, sizeof buf);
3266 RETSETUNDEF; /* just pretend it's a normal file */
3270 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3272 S_dooneliner(pTHX_ char *cmd, char *filename)
3274 char *save_filename = filename;
3280 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3281 strcpy(cmdline, cmd);
3282 strcat(cmdline, " ");
3283 for (s = cmdline + strlen(cmdline); *filename; ) {
3288 myfp = PerlProc_popen(cmdline, "r");
3292 SV *tmpsv = sv_newmortal();
3293 /* Need to save/restore 'PL_rs' ?? */
3294 s = sv_gets(tmpsv, myfp, 0);
3295 (void)PerlProc_pclose(myfp);
3299 #ifdef HAS_SYS_ERRLIST
3304 /* you don't see this */
3306 #ifdef HAS_SYS_ERRLIST
3314 if (instr(s, errmsg)) {
3321 #define EACCES EPERM
3323 if (instr(s, "cannot make"))
3324 SETERRNO(EEXIST,RMS$_FEX);
3325 else if (instr(s, "existing file"))
3326 SETERRNO(EEXIST,RMS$_FEX);
3327 else if (instr(s, "ile exists"))
3328 SETERRNO(EEXIST,RMS$_FEX);
3329 else if (instr(s, "non-exist"))
3330 SETERRNO(ENOENT,RMS$_FNF);
3331 else if (instr(s, "does not exist"))
3332 SETERRNO(ENOENT,RMS$_FNF);
3333 else if (instr(s, "not empty"))
3334 SETERRNO(EBUSY,SS$_DEVOFFLINE);
3335 else if (instr(s, "cannot access"))
3336 SETERRNO(EACCES,RMS$_PRV);
3338 SETERRNO(EPERM,RMS$_PRV);
3341 else { /* some mkdirs return no failure indication */
3342 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3343 if (PL_op->op_type == OP_RMDIR)
3348 SETERRNO(EACCES,RMS$_PRV); /* a guess */
3365 char *tmps = SvPV(TOPs, n_a);
3367 TAINT_PROPER("mkdir");
3369 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3371 SETi( dooneliner("mkdir", tmps) );
3372 oldumask = PerlLIO_umask(0);
3373 PerlLIO_umask(oldumask);
3374 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3386 TAINT_PROPER("rmdir");
3388 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3390 XPUSHi( dooneliner("rmdir", tmps) );
3395 /* Directory calls. */
3400 #if defined(Direntry_t) && defined(HAS_READDIR)
3402 char *dirname = POPpx;
3404 register IO *io = GvIOn(gv);
3410 PerlDir_close(IoDIRP(io));
3411 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3417 SETERRNO(EBADF,RMS$_DIR);
3420 DIE(aTHX_ PL_no_dir_func, "opendir");
3427 #if defined(Direntry_t) && defined(HAS_READDIR)
3429 Direntry_t *readdir (DIR *);
3431 register Direntry_t *dp;
3433 register IO *io = GvIOn(gv);
3436 if (!io || !IoDIRP(io))
3439 if (GIMME == G_ARRAY) {
3441 while (dp = (Direntry_t *)PerlDir_read(IoDIRP(io))) {
3443 sv = newSVpvn(dp->d_name, dp->d_namlen);
3445 sv = newSVpv(dp->d_name, 0);
3447 #ifndef INCOMPLETE_TAINTS
3450 XPUSHs(sv_2mortal(sv));
3454 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3457 sv = newSVpvn(dp->d_name, dp->d_namlen);
3459 sv = newSVpv(dp->d_name, 0);
3461 #ifndef INCOMPLETE_TAINTS
3464 XPUSHs(sv_2mortal(sv));
3470 SETERRNO(EBADF,RMS$_ISI);
3471 if (GIMME == G_ARRAY)
3476 DIE(aTHX_ PL_no_dir_func, "readdir");
3483 #if defined(HAS_TELLDIR) || defined(telldir)
3484 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3485 /* XXX netbsd still seemed to.
3486 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3487 --JHI 1999-Feb-02 */
3488 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3489 long telldir (DIR *);
3492 register IO *io = GvIOn(gv);
3494 if (!io || !IoDIRP(io))
3497 PUSHi( PerlDir_tell(IoDIRP(io)) );
3501 SETERRNO(EBADF,RMS$_ISI);
3504 DIE(aTHX_ PL_no_dir_func, "telldir");
3511 #if defined(HAS_SEEKDIR) || defined(seekdir)
3514 register IO *io = GvIOn(gv);
3516 if (!io || !IoDIRP(io))
3519 (void)PerlDir_seek(IoDIRP(io), along);
3524 SETERRNO(EBADF,RMS$_ISI);
3527 DIE(aTHX_ PL_no_dir_func, "seekdir");
3534 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3536 register IO *io = GvIOn(gv);
3538 if (!io || !IoDIRP(io))
3541 (void)PerlDir_rewind(IoDIRP(io));
3545 SETERRNO(EBADF,RMS$_ISI);
3548 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3555 #if defined(Direntry_t) && defined(HAS_READDIR)
3557 register IO *io = GvIOn(gv);
3559 if (!io || !IoDIRP(io))
3562 #ifdef VOID_CLOSEDIR
3563 PerlDir_close(IoDIRP(io));
3565 if (PerlDir_close(IoDIRP(io)) < 0) {
3566 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3575 SETERRNO(EBADF,RMS$_IFI);
3578 DIE(aTHX_ PL_no_dir_func, "closedir");
3582 /* Process control. */
3592 PERL_FLUSHALL_FOR_CHILD;
3598 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
3599 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3600 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3605 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3610 PERL_FLUSHALL_FOR_CHILD;
3611 childpid = PerlProc_fork();
3615 DIE(aTHX_ PL_no_func, "Unsupported function fork");
3622 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3627 childpid = wait4pid(-1, &argflags, 0);
3628 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3632 DIE(aTHX_ PL_no_func, "Unsupported function wait");
3638 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3646 childpid = wait4pid(childpid, &argflags, optype);
3647 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3651 DIE(aTHX_ PL_no_func, "Unsupported function waitpid");
3657 djSP; dMARK; dORIGMARK; dTARGET;
3662 Sigsave_t ihand,qhand; /* place to save signals during system() */
3667 if (SP - MARK == 1) {
3669 char *junk = SvPV(TOPs, n_a);
3671 TAINT_PROPER("system");
3674 PERL_FLUSHALL_FOR_CHILD;
3675 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2)
3676 if (PerlProc_pipe(pp) >= 0)
3678 while ((childpid = vfork()) == -1) {
3679 if (errno != EAGAIN) {
3684 PerlLIO_close(pp[0]);
3685 PerlLIO_close(pp[1]);
3693 PerlLIO_close(pp[1]);
3694 rsignal_save(SIGINT, SIG_IGN, &ihand);
3695 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
3697 result = wait4pid(childpid, &status, 0);
3698 } while (result == -1 && errno == EINTR);
3699 (void)rsignal_restore(SIGINT, &ihand);
3700 (void)rsignal_restore(SIGQUIT, &qhand);
3701 STATUS_NATIVE_SET(result == -1 ? -1 : status);
3702 do_execfree(); /* free any memory child malloced on vfork */
3708 while (n < sizeof(int)) {
3709 n1 = PerlLIO_read(pp[0],
3710 (void*)(((char*)&errkid)+n),
3716 PerlLIO_close(pp[0]);
3717 if (n) { /* Error */
3718 if (n != sizeof(int))
3719 DIE(aTHX_ "panic: kid popen errno read");
3720 errno = errkid; /* Propagate errno from kid */
3721 STATUS_CURRENT = -1;
3724 PUSHi(STATUS_CURRENT);
3728 PerlLIO_close(pp[0]);
3729 #if defined(HAS_FCNTL) && defined(F_SETFD)
3730 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
3733 if (PL_op->op_flags & OPf_STACKED) {
3734 SV *really = *++MARK;
3735 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
3737 else if (SP - MARK != 1)
3738 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
3740 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
3743 #else /* ! FORK or VMS or OS/2 */
3744 if (PL_op->op_flags & OPf_STACKED) {
3745 SV *really = *++MARK;
3746 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
3748 else if (SP - MARK != 1)
3749 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
3751 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3753 STATUS_NATIVE_SET(value);
3756 PUSHi(STATUS_CURRENT);
3757 #endif /* !FORK or VMS */
3763 djSP; dMARK; dORIGMARK; dTARGET;
3767 PERL_FLUSHALL_FOR_CHILD;
3768 if (PL_op->op_flags & OPf_STACKED) {
3769 SV *really = *++MARK;
3770 value = (I32)do_aexec(really, MARK, SP);
3772 else if (SP - MARK != 1)
3774 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
3778 (void ) do_aspawn(Nullsv, MARK, SP);
3782 value = (I32)do_aexec(Nullsv, MARK, SP);
3787 char *junk = SvPV(*SP, n_a);
3789 TAINT_PROPER("exec");
3792 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3795 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3798 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3803 #if !defined(HAS_FORK) && defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3815 djSP; dMARK; dTARGET;
3818 value = (I32)apply(PL_op->op_type, MARK, SP);
3823 DIE(aTHX_ PL_no_func, "Unsupported function kill");
3831 XPUSHi( getppid() );
3834 DIE(aTHX_ PL_no_func, "getppid");
3850 pgrp = (I32)BSD_GETPGRP(pid);
3852 if (pid != 0 && pid != PerlProc_getpid())
3853 DIE(aTHX_ "POSIX getpgrp can't take an argument");
3859 DIE(aTHX_ PL_no_func, "getpgrp()");
3878 TAINT_PROPER("setpgrp");
3880 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
3882 if ((pgrp != 0 && pgrp != PerlProc_getpid())
3883 || (pid != 0 && pid != PerlProc_getpid()))
3885 DIE(aTHX_ "setpgrp can't take arguments");
3887 SETi( setpgrp() >= 0 );
3888 #endif /* USE_BSDPGRP */
3891 DIE(aTHX_ PL_no_func, "setpgrp()");
3900 #ifdef HAS_GETPRIORITY
3903 SETi( getpriority(which, who) );
3906 DIE(aTHX_ PL_no_func, "getpriority()");
3916 #ifdef HAS_SETPRIORITY
3920 TAINT_PROPER("setpriority");
3921 SETi( setpriority(which, who, niceval) >= 0 );
3924 DIE(aTHX_ PL_no_func, "setpriority()");
3934 XPUSHn( time(Null(Time_t*)) );
3936 XPUSHi( time(Null(Time_t*)) );
3941 /* XXX The POSIX name is CLK_TCK; it is to be preferred
3942 to HZ. Probably. For now, assume that if the system
3943 defines HZ, it does so correctly. (Will this break
3945 Probably we ought to use _sysconf(_SC_CLK_TCK), if
3946 it's supported. --AD 9/96.
3962 DIE(aTHX_ "times not implemented");
3967 (void)PerlProc_times(&PL_timesbuf);
3969 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
3970 /* struct tms, though same data */
3974 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
3975 if (GIMME == G_ARRAY) {
3976 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
3977 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
3978 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
3981 #endif /* HAS_TIMES */
3994 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
3995 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3996 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4002 when = (Time_t)SvNVx(POPs);
4004 when = (Time_t)SvIVx(POPs);
4007 if (PL_op->op_type == OP_LOCALTIME)
4008 tmbuf = localtime(&when);
4010 tmbuf = gmtime(&when);
4014 if (GIMME != G_ARRAY) {
4019 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4020 dayname[tmbuf->tm_wday],
4021 monname[tmbuf->tm_mon],
4026 tmbuf->tm_year + 1900);
4027 PUSHs(sv_2mortal(tsv));
4030 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4031 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4032 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4033 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4034 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4035 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4036 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4037 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4038 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4049 anum = alarm((unsigned int)anum);
4056 DIE(aTHX_ PL_no_func, "Unsupported function alarm");
4067 (void)time(&lasttime);
4072 PerlProc_sleep((unsigned int)duration);
4075 XPUSHi(when - lasttime);
4079 /* Shared memory. */
4093 return pp_shmwrite();
4098 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4099 djSP; dMARK; dTARGET;
4100 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4109 /* Message passing. */
4123 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4124 djSP; dMARK; dTARGET;
4125 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4136 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4137 djSP; dMARK; dTARGET;
4138 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4151 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4152 djSP; dMARK; dTARGET;
4153 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4160 DIE(aTHX_ "System V IPC is not implemented on this machine");
4166 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4167 djSP; dMARK; dTARGET;
4168 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4176 PUSHp(zero_but_true, ZBTLEN);
4186 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4187 djSP; dMARK; dTARGET;
4188 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4197 /* Get system info. */
4201 #ifdef HAS_GETHOSTBYNAME
4202 return pp_ghostent();
4204 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4210 #ifdef HAS_GETHOSTBYADDR
4211 return pp_ghostent();
4213 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4220 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4221 I32 which = PL_op->op_type;
4222 register char **elem;
4224 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4225 struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4226 struct hostent *PerlSock_gethostbyname(Netdb_name_t);
4227 struct hostent *PerlSock_gethostent(void);
4229 struct hostent *hent;
4234 if (which == OP_GHBYNAME)
4235 #ifdef HAS_GETHOSTBYNAME
4236 hent = PerlSock_gethostbyname(POPpx);
4238 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4240 else if (which == OP_GHBYADDR) {
4241 #ifdef HAS_GETHOSTBYADDR
4242 int addrtype = POPi;
4245 Netdb_host_t addr = (Netdb_host_t) SvPV(addrsv, addrlen);
4247 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4249 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4253 #ifdef HAS_GETHOSTENT
4254 hent = PerlSock_gethostent();
4256 DIE(aTHX_ PL_no_sock_func, "gethostent");
4259 #ifdef HOST_NOT_FOUND
4261 STATUS_NATIVE_SET(h_errno);
4264 if (GIMME != G_ARRAY) {
4265 PUSHs(sv = sv_newmortal());
4267 if (which == OP_GHBYNAME) {
4269 sv_setpvn(sv, hent->h_addr, hent->h_length);
4272 sv_setpv(sv, (char*)hent->h_name);
4278 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4279 sv_setpv(sv, (char*)hent->h_name);
4280 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4281 for (elem = hent->h_aliases; elem && *elem; elem++) {
4282 sv_catpv(sv, *elem);
4284 sv_catpvn(sv, " ", 1);
4286 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4287 sv_setiv(sv, (IV)hent->h_addrtype);
4288 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4289 len = hent->h_length;
4290 sv_setiv(sv, (IV)len);
4292 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4293 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4294 sv_setpvn(sv, *elem, len);
4297 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4299 sv_setpvn(sv, hent->h_addr, len);
4304 DIE(aTHX_ PL_no_sock_func, "gethostent");
4310 #ifdef HAS_GETNETBYNAME
4311 return pp_gnetent();
4313 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4319 #ifdef HAS_GETNETBYADDR
4320 return pp_gnetent();
4322 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4329 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4330 I32 which = PL_op->op_type;
4331 register char **elem;
4333 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4334 struct netent *PerlSock_getnetbyaddr(Netdb_net_t, int);
4335 struct netent *PerlSock_getnetbyname(Netdb_name_t);
4336 struct netent *PerlSock_getnetent(void);
4338 struct netent *nent;
4341 if (which == OP_GNBYNAME)
4342 #ifdef HAS_GETNETBYNAME
4343 nent = PerlSock_getnetbyname(POPpx);
4345 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4347 else if (which == OP_GNBYADDR) {
4348 #ifdef HAS_GETNETBYADDR
4349 int addrtype = POPi;
4350 Netdb_net_t addr = (Netdb_net_t) U_L(POPn);
4351 nent = PerlSock_getnetbyaddr(addr, addrtype);
4353 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4357 #ifdef HAS_GETNETENT
4358 nent = PerlSock_getnetent();
4360 DIE(aTHX_ PL_no_sock_func, "getnetent");
4364 if (GIMME != G_ARRAY) {
4365 PUSHs(sv = sv_newmortal());
4367 if (which == OP_GNBYNAME)
4368 sv_setiv(sv, (IV)nent->n_net);
4370 sv_setpv(sv, nent->n_name);
4376 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4377 sv_setpv(sv, nent->n_name);
4378 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4379 for (elem = nent->n_aliases; elem && *elem; elem++) {
4380 sv_catpv(sv, *elem);
4382 sv_catpvn(sv, " ", 1);
4384 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4385 sv_setiv(sv, (IV)nent->n_addrtype);
4386 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4387 sv_setiv(sv, (IV)nent->n_net);
4392 DIE(aTHX_ PL_no_sock_func, "getnetent");
4398 #ifdef HAS_GETPROTOBYNAME
4399 return pp_gprotoent();
4401 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4407 #ifdef HAS_GETPROTOBYNUMBER
4408 return pp_gprotoent();
4410 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4417 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4418 I32 which = PL_op->op_type;
4419 register char **elem;
4421 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4422 struct protoent *PerlSock_getprotobyname(Netdb_name_t);
4423 struct protoent *PerlSock_getprotobynumber(int);
4424 struct protoent *PerlSock_getprotoent(void);
4426 struct protoent *pent;
4429 if (which == OP_GPBYNAME)
4430 #ifdef HAS_GETPROTOBYNAME
4431 pent = PerlSock_getprotobyname(POPpx);
4433 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4435 else if (which == OP_GPBYNUMBER)
4436 #ifdef HAS_GETPROTOBYNUMBER
4437 pent = PerlSock_getprotobynumber(POPi);
4439 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4442 #ifdef HAS_GETPROTOENT
4443 pent = PerlSock_getprotoent();
4445 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4449 if (GIMME != G_ARRAY) {
4450 PUSHs(sv = sv_newmortal());
4452 if (which == OP_GPBYNAME)
4453 sv_setiv(sv, (IV)pent->p_proto);
4455 sv_setpv(sv, pent->p_name);
4461 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4462 sv_setpv(sv, pent->p_name);
4463 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4464 for (elem = pent->p_aliases; elem && *elem; elem++) {
4465 sv_catpv(sv, *elem);
4467 sv_catpvn(sv, " ", 1);
4469 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4470 sv_setiv(sv, (IV)pent->p_proto);
4475 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4481 #ifdef HAS_GETSERVBYNAME
4482 return pp_gservent();
4484 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4490 #ifdef HAS_GETSERVBYPORT
4491 return pp_gservent();
4493 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4500 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4501 I32 which = PL_op->op_type;
4502 register char **elem;
4504 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4505 struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t);
4506 struct servent *PerlSock_getservbyport(int, Netdb_name_t);
4507 struct servent *PerlSock_getservent(void);
4509 struct servent *sent;
4512 if (which == OP_GSBYNAME) {
4513 #ifdef HAS_GETSERVBYNAME
4514 char *proto = POPpx;
4517 if (proto && !*proto)
4520 sent = PerlSock_getservbyname(name, proto);
4522 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4525 else if (which == OP_GSBYPORT) {
4526 #ifdef HAS_GETSERVBYPORT
4527 char *proto = POPpx;
4528 unsigned short port = POPu;
4531 port = PerlSock_htons(port);
4533 sent = PerlSock_getservbyport(port, proto);
4535 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4539 #ifdef HAS_GETSERVENT
4540 sent = PerlSock_getservent();
4542 DIE(aTHX_ PL_no_sock_func, "getservent");
4546 if (GIMME != G_ARRAY) {
4547 PUSHs(sv = sv_newmortal());
4549 if (which == OP_GSBYNAME) {
4551 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4553 sv_setiv(sv, (IV)(sent->s_port));
4557 sv_setpv(sv, sent->s_name);
4563 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4564 sv_setpv(sv, sent->s_name);
4565 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4566 for (elem = sent->s_aliases; elem && *elem; elem++) {
4567 sv_catpv(sv, *elem);
4569 sv_catpvn(sv, " ", 1);
4571 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4573 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4575 sv_setiv(sv, (IV)(sent->s_port));
4577 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4578 sv_setpv(sv, sent->s_proto);
4583 DIE(aTHX_ PL_no_sock_func, "getservent");
4590 #ifdef HAS_SETHOSTENT
4591 PerlSock_sethostent(TOPi);
4594 DIE(aTHX_ PL_no_sock_func, "sethostent");
4601 #ifdef HAS_SETNETENT
4602 PerlSock_setnetent(TOPi);
4605 DIE(aTHX_ PL_no_sock_func, "setnetent");
4612 #ifdef HAS_SETPROTOENT
4613 PerlSock_setprotoent(TOPi);
4616 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4623 #ifdef HAS_SETSERVENT
4624 PerlSock_setservent(TOPi);
4627 DIE(aTHX_ PL_no_sock_func, "setservent");
4634 #ifdef HAS_ENDHOSTENT
4635 PerlSock_endhostent();
4639 DIE(aTHX_ PL_no_sock_func, "endhostent");
4646 #ifdef HAS_ENDNETENT
4647 PerlSock_endnetent();
4651 DIE(aTHX_ PL_no_sock_func, "endnetent");
4658 #ifdef HAS_ENDPROTOENT
4659 PerlSock_endprotoent();
4663 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4670 #ifdef HAS_ENDSERVENT
4671 PerlSock_endservent();
4675 DIE(aTHX_ PL_no_sock_func, "endservent");
4684 DIE(aTHX_ PL_no_func, "getpwnam");
4693 DIE(aTHX_ PL_no_func, "getpwuid");
4700 #if defined(HAS_PASSWD) && defined(HAS_GETPWENT)
4701 I32 which = PL_op->op_type;
4703 struct passwd *pwent;
4705 #if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4706 struct spwd *spwent = NULL;
4709 if (which == OP_GPWNAM)
4710 pwent = getpwnam(POPpx);
4711 else if (which == OP_GPWUID)
4712 pwent = getpwuid(POPi);
4714 pwent = (struct passwd *)getpwent();
4717 if (which == OP_GPWNAM) {
4719 spwent = getspnam(pwent->pw_name);
4721 # ifdef HAS_GETSPUID /* AFAIK there isn't any anywhere. --jhi */
4722 else if (which == OP_GPWUID) {
4724 spwent = getspnam(pwent->pw_name);
4727 # ifdef HAS_GETSPENT
4729 spwent = (struct spwd *)getspent();
4734 if (GIMME != G_ARRAY) {
4735 PUSHs(sv = sv_newmortal());
4737 if (which == OP_GPWNAM)
4738 sv_setiv(sv, (IV)pwent->pw_uid);
4740 sv_setpv(sv, pwent->pw_name);
4746 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4747 sv_setpv(sv, pwent->pw_name);
4749 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4751 # if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4753 sv_setpv(sv, spwent->sp_pwdp);
4755 sv_setpv(sv, pwent->pw_passwd);
4757 sv_setpv(sv, pwent->pw_passwd);
4761 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4762 sv_setiv(sv, (IV)pwent->pw_uid);
4764 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4765 sv_setiv(sv, (IV)pwent->pw_gid);
4767 /* pw_change, pw_quota, and pw_age are mutually exclusive. */
4768 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4770 sv_setiv(sv, (IV)pwent->pw_change);
4773 sv_setiv(sv, (IV)pwent->pw_quota);
4776 sv_setpv(sv, pwent->pw_age);
4781 /* pw_class and pw_comment are mutually exclusive. */
4782 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4784 sv_setpv(sv, pwent->pw_class);
4787 sv_setpv(sv, pwent->pw_comment);
4791 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4793 sv_setpv(sv, pwent->pw_gecos);
4795 #ifndef INCOMPLETE_TAINTS
4796 /* pw_gecos is tainted because user himself can diddle with it. */
4800 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4801 sv_setpv(sv, pwent->pw_dir);
4803 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4804 sv_setpv(sv, pwent->pw_shell);
4807 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4808 sv_setiv(sv, (IV)pwent->pw_expire);
4813 DIE(aTHX_ PL_no_func, "getpwent");
4820 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
4822 # ifdef HAS_SETSPENT
4827 DIE(aTHX_ PL_no_func, "setpwent");
4834 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
4836 # ifdef HAS_ENDSPENT
4841 DIE(aTHX_ PL_no_func, "endpwent");
4850 DIE(aTHX_ PL_no_func, "getgrnam");
4859 DIE(aTHX_ PL_no_func, "getgrgid");
4866 #if defined(HAS_GROUP) && defined(HAS_GETGRENT)
4867 I32 which = PL_op->op_type;
4868 register char **elem;
4870 struct group *grent;
4873 if (which == OP_GGRNAM)
4874 grent = (struct group *)getgrnam(POPpx);
4875 else if (which == OP_GGRGID)
4876 grent = (struct group *)getgrgid(POPi);
4878 grent = (struct group *)getgrent();
4881 if (GIMME != G_ARRAY) {
4882 PUSHs(sv = sv_newmortal());
4884 if (which == OP_GGRNAM)
4885 sv_setiv(sv, (IV)grent->gr_gid);
4887 sv_setpv(sv, grent->gr_name);
4893 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4894 sv_setpv(sv, grent->gr_name);
4896 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4898 sv_setpv(sv, grent->gr_passwd);
4901 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4902 sv_setiv(sv, (IV)grent->gr_gid);
4904 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4905 for (elem = grent->gr_mem; elem && *elem; elem++) {
4906 sv_catpv(sv, *elem);
4908 sv_catpvn(sv, " ", 1);
4914 DIE(aTHX_ PL_no_func, "getgrent");
4921 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
4925 DIE(aTHX_ PL_no_func, "setgrent");
4932 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
4936 DIE(aTHX_ PL_no_func, "endgrent");
4946 if (!(tmps = PerlProc_getlogin()))
4948 PUSHp(tmps, strlen(tmps));
4951 DIE(aTHX_ PL_no_func, "getlogin");
4955 /* Miscellaneous. */
4960 djSP; dMARK; dORIGMARK; dTARGET;
4961 register I32 items = SP - MARK;
4962 unsigned long a[20];
4969 while (++MARK <= SP) {
4970 if (SvTAINTED(*MARK)) {
4976 TAINT_PROPER("syscall");
4979 /* This probably won't work on machines where sizeof(long) != sizeof(int)
4980 * or where sizeof(long) != sizeof(char*). But such machines will
4981 * not likely have syscall implemented either, so who cares?
4983 while (++MARK <= SP) {
4984 if (SvNIOK(*MARK) || !i)
4985 a[i++] = SvIV(*MARK);
4986 else if (*MARK == &PL_sv_undef)
4989 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
4995 DIE(aTHX_ "Too many args to syscall");
4997 DIE(aTHX_ "Too few args to syscall");
4999 retval = syscall(a[0]);
5002 retval = syscall(a[0],a[1]);
5005 retval = syscall(a[0],a[1],a[2]);
5008 retval = syscall(a[0],a[1],a[2],a[3]);
5011 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5014 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5017 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5020 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5024 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5027 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5030 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5034 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5038 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5042 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5043 a[10],a[11],a[12],a[13]);
5045 #endif /* atarist */
5051 DIE(aTHX_ PL_no_func, "syscall");
5055 #ifdef FCNTL_EMULATE_FLOCK
5057 /* XXX Emulate flock() with fcntl().
5058 What's really needed is a good file locking module.
5062 fcntl_emulate_flock(int fd, int operation)
5066 switch (operation & ~LOCK_NB) {
5068 flock.l_type = F_RDLCK;
5071 flock.l_type = F_WRLCK;
5074 flock.l_type = F_UNLCK;
5080 flock.l_whence = SEEK_SET;
5081 flock.l_start = flock.l_len = (Off_t)0;
5083 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5086 #endif /* FCNTL_EMULATE_FLOCK */
5088 #ifdef LOCKF_EMULATE_FLOCK
5090 /* XXX Emulate flock() with lockf(). This is just to increase
5091 portability of scripts. The calls are not completely
5092 interchangeable. What's really needed is a good file
5096 /* The lockf() constants might have been defined in <unistd.h>.
5097 Unfortunately, <unistd.h> causes troubles on some mixed
5098 (BSD/POSIX) systems, such as SunOS 4.1.3.
5100 Further, the lockf() constants aren't POSIX, so they might not be
5101 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5102 just stick in the SVID values and be done with it. Sigh.
5106 # define F_ULOCK 0 /* Unlock a previously locked region */
5109 # define F_LOCK 1 /* Lock a region for exclusive use */
5112 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5115 # define F_TEST 3 /* Test a region for other processes locks */
5119 lockf_emulate_flock(int fd, int operation)
5125 /* flock locks entire file so for lockf we need to do the same */
5127 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5128 if (pos > 0) /* is seekable and needs to be repositioned */
5129 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5130 pos = -1; /* seek failed, so don't seek back afterwards */
5133 switch (operation) {
5135 /* LOCK_SH - get a shared lock */
5137 /* LOCK_EX - get an exclusive lock */
5139 i = lockf (fd, F_LOCK, 0);
5142 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5143 case LOCK_SH|LOCK_NB:
5144 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5145 case LOCK_EX|LOCK_NB:
5146 i = lockf (fd, F_TLOCK, 0);
5148 if ((errno == EAGAIN) || (errno == EACCES))
5149 errno = EWOULDBLOCK;
5152 /* LOCK_UN - unlock (non-blocking is a no-op) */
5154 case LOCK_UN|LOCK_NB:
5155 i = lockf (fd, F_ULOCK, 0);
5158 /* Default - can't decipher operation */
5165 if (pos > 0) /* need to restore position of the handle */
5166 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5171 #endif /* LOCKF_EMULATE_FLOCK */