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);
1099 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1100 I32 gimme = GIMME_V;
1102 XPUSHs(SvTIED_obj((SV*)gv, mg));
1105 call_method("GETC", gimme);
1108 if (gimme == G_SCALAR)
1109 SvSetMagicSV_nosteal(TARG, TOPs);
1112 if (!gv || do_eof(gv)) /* make sure we have fp with something */
1115 sv_setpv(TARG, " ");
1116 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1123 return pp_sysread();
1127 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1130 register PERL_CONTEXT *cx;
1131 I32 gimme = GIMME_V;
1132 AV* padlist = CvPADLIST(cv);
1133 SV** svp = AvARRAY(padlist);
1139 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1141 SAVEVPTR(PL_curpad);
1142 PL_curpad = AvARRAY((AV*)svp[1]);
1144 setdefout(gv); /* locally select filehandle so $% et al work */
1176 SV *tmpsv = sv_newmortal();
1177 gv_efullname3(tmpsv, fgv, Nullch);
1178 DIE(aTHX_ "Undefined format \"%s\" called",SvPVX(tmpsv));
1180 DIE(aTHX_ "Not a format reference");
1183 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1185 IoFLAGS(io) &= ~IOf_DIDTOP;
1186 return doform(cv,gv,PL_op->op_next);
1192 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1193 register IO *io = GvIOp(gv);
1194 PerlIO *ofp = IoOFP(io);
1198 register PERL_CONTEXT *cx;
1200 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1201 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1202 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1203 PL_formtarget != PL_toptarget)
1207 if (!IoTOP_GV(io)) {
1211 if (!IoTOP_NAME(io)) {
1212 if (!IoFMT_NAME(io))
1213 IoFMT_NAME(io) = savepv(GvNAME(gv));
1214 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1215 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1216 if ((topgv && GvFORM(topgv)) ||
1217 !gv_fetchpv("top",FALSE,SVt_PVFM))
1218 IoTOP_NAME(io) = savepv(SvPVX(topname));
1220 IoTOP_NAME(io) = savepv("top");
1222 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1223 if (!topgv || !GvFORM(topgv)) {
1224 IoLINES_LEFT(io) = 100000000;
1227 IoTOP_GV(io) = topgv;
1229 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1230 I32 lines = IoLINES_LEFT(io);
1231 char *s = SvPVX(PL_formtarget);
1232 if (lines <= 0) /* Yow, header didn't even fit!!! */
1234 while (lines-- > 0) {
1235 s = strchr(s, '\n');
1241 PerlIO_write(ofp, SvPVX(PL_formtarget), s - SvPVX(PL_formtarget));
1242 sv_chop(PL_formtarget, s);
1243 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1246 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1247 PerlIO_write(ofp, SvPVX(PL_formfeed), SvCUR(PL_formfeed));
1248 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1250 PL_formtarget = PL_toptarget;
1251 IoFLAGS(io) |= IOf_DIDTOP;
1254 DIE(aTHX_ "bad top format reference");
1257 SV *tmpsv = sv_newmortal();
1258 gv_efullname3(tmpsv, fgv, Nullch);
1259 DIE(aTHX_ "Undefined top format \"%s\" called",SvPVX(tmpsv));
1262 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1263 return doform(cv,gv,PL_op);
1267 POPBLOCK(cx,PL_curpm);
1273 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1275 SV* sv = sv_newmortal();
1276 gv_efullname3(sv, gv, Nullch);
1277 Perl_warner(aTHX_ WARN_IO,
1278 "Filehandle %s opened only for input",
1281 else if (ckWARN(WARN_CLOSED))
1282 report_closed_fh(gv, io, "write", "filehandle");
1287 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1288 if (ckWARN(WARN_IO))
1289 Perl_warner(aTHX_ WARN_IO, "page overflow");
1291 if (!PerlIO_write(ofp, SvPVX(PL_formtarget), SvCUR(PL_formtarget)) ||
1295 FmLINES(PL_formtarget) = 0;
1296 SvCUR_set(PL_formtarget, 0);
1297 *SvEND(PL_formtarget) = '\0';
1298 if (IoFLAGS(io) & IOf_FLUSH)
1299 (void)PerlIO_flush(fp);
1303 PL_formtarget = PL_bodytarget;
1305 return pop_return();
1310 djSP; dMARK; dORIGMARK;
1318 if (PL_op->op_flags & OPf_STACKED)
1323 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1324 if (MARK == ORIGMARK) {
1327 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1331 *MARK = SvTIED_obj((SV*)gv, mg);
1334 call_method("PRINTF", G_SCALAR);
1337 MARK = ORIGMARK + 1;
1344 if (!(io = GvIO(gv))) {
1345 if (ckWARN(WARN_UNOPENED)) {
1346 gv_efullname3(sv, gv, Nullch);
1347 Perl_warner(aTHX_ WARN_UNOPENED,
1348 "Filehandle %s never opened", SvPV(sv,n_a));
1350 SETERRNO(EBADF,RMS$_IFI);
1353 else if (!(fp = IoOFP(io))) {
1354 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1356 gv_efullname3(sv, gv, Nullch);
1357 Perl_warner(aTHX_ WARN_IO,
1358 "Filehandle %s opened only for input",
1361 else if (ckWARN(WARN_CLOSED))
1362 report_closed_fh(gv, io, "printf", "filehandle");
1364 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
1368 do_sprintf(sv, SP - MARK, MARK + 1);
1369 if (!do_print(sv, fp))
1372 if (IoFLAGS(io) & IOf_FLUSH)
1373 if (PerlIO_flush(fp) == EOF)
1384 PUSHs(&PL_sv_undef);
1405 /* Need TIEHANDLE method ? */
1407 tmps = SvPV(sv, len);
1408 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1409 IoLINES(GvIOp(gv)) = 0;
1413 PUSHs(&PL_sv_undef);
1420 djSP; dMARK; dORIGMARK; dTARGET;
1426 Sock_size_t bufsize;
1432 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) &&
1433 (mg = SvTIED_mg((SV*)gv, 'q')))
1438 *MARK = SvTIED_obj((SV*)gv, mg);
1440 call_method("READ", G_SCALAR);
1453 sv_setpvn(bufsv, "", 0);
1454 buffer = SvPV_force(bufsv, blen);
1455 length = SvIVx(*++MARK);
1457 DIE(aTHX_ "Negative length");
1460 offset = SvIVx(*++MARK);
1464 if (!io || !IoIFP(io))
1467 if (PL_op->op_type == OP_RECV) {
1468 char namebuf[MAXPATHLEN];
1469 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE)
1470 bufsize = sizeof (struct sockaddr_in);
1472 bufsize = sizeof namebuf;
1474 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1478 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1482 buffer = SvGROW(bufsv, length+1);
1483 /* 'offset' means 'flags' here */
1484 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1485 (struct sockaddr *)namebuf, &bufsize);
1488 SvCUR_set(bufsv, length);
1489 *SvEND(bufsv) = '\0';
1490 (void)SvPOK_only(bufsv);
1492 /* This should not be marked tainted if the fp is marked clean */
1493 if (!(IoFLAGS(io) & IOf_UNTAINT))
1494 SvTAINTED_on(bufsv);
1496 sv_setpvn(TARG, namebuf, bufsize);
1501 if (PL_op->op_type == OP_RECV)
1502 DIE(aTHX_ PL_no_sock_func, "recv");
1506 DIE(aTHX_ "Offset outside string");
1509 bufsize = SvCUR(bufsv);
1510 buffer = SvGROW(bufsv, length+offset+1);
1511 if (offset > bufsize) { /* Zero any newly allocated space */
1512 Zero(buffer+bufsize, offset-bufsize, char);
1514 if (PL_op->op_type == OP_SYSREAD) {
1515 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1516 if (IoTYPE(io) == 's') {
1517 length = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1518 buffer+offset, length, 0);
1523 length = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1524 buffer+offset, length);
1528 #ifdef HAS_SOCKET__bad_code_maybe
1529 if (IoTYPE(io) == 's') {
1530 char namebuf[MAXPATHLEN];
1531 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1532 bufsize = sizeof (struct sockaddr_in);
1534 bufsize = sizeof namebuf;
1536 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer+offset, length, 0,
1537 (struct sockaddr *)namebuf, &bufsize);
1542 length = PerlIO_read(IoIFP(io), buffer+offset, length);
1543 /* fread() returns 0 on both error and EOF */
1544 if (length == 0 && PerlIO_error(IoIFP(io)))
1548 if ((IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
1549 || IoIFP(io) == PerlIO_stderr()) && ckWARN(WARN_IO))
1551 SV* sv = sv_newmortal();
1552 gv_efullname3(sv, gv, Nullch);
1553 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
1558 SvCUR_set(bufsv, length+offset);
1559 *SvEND(bufsv) = '\0';
1560 (void)SvPOK_only(bufsv);
1562 /* This should not be marked tainted if the fp is marked clean */
1563 if (!(IoFLAGS(io) & IOf_UNTAINT))
1564 SvTAINTED_on(bufsv);
1577 int items = (SP - PL_stack_base) - TOPMARK;
1581 sv = sv_2mortal(newSViv(sv_len(*SP)));
1590 djSP; dMARK; dORIGMARK; dTARGET;
1601 if (PL_op->op_type == OP_SYSWRITE && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1605 *MARK = SvTIED_obj((SV*)gv, mg);
1607 call_method("WRITE", G_SCALAR);
1618 buffer = SvPV(bufsv, blen);
1619 #if Off_t_SIZE > IVSIZE
1620 length = SvNVx(*++MARK);
1622 length = SvIVx(*++MARK);
1625 DIE(aTHX_ "Negative length");
1628 if (!io || !IoIFP(io)) {
1630 if (ckWARN(WARN_CLOSED)) {
1631 if (PL_op->op_type == OP_SYSWRITE)
1632 report_closed_fh(gv, io, "syswrite", "filehandle");
1634 report_closed_fh(gv, io, "send", "socket");
1637 else if (PL_op->op_type == OP_SYSWRITE) {
1639 #if Off_t_SIZE > IVSIZE
1640 offset = SvNVx(*++MARK);
1642 offset = SvIVx(*++MARK);
1646 DIE(aTHX_ "Offset outside string");
1648 } else if (offset >= blen && blen > 0)
1649 DIE(aTHX_ "Offset outside string");
1652 if (length > blen - offset)
1653 length = blen - offset;
1654 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1655 if (IoTYPE(io) == 's') {
1656 length = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1657 buffer+offset, length, 0);
1662 /* See the note at doio.c:do_print about filesize limits. --jhi */
1663 length = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1664 buffer+offset, length);
1668 else if (SP > MARK) {
1671 sockbuf = SvPVx(*++MARK, mlen);
1672 length = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen, length,
1673 (struct sockaddr *)sockbuf, mlen);
1676 length = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1680 DIE(aTHX_ PL_no_sock_func, "send");
1695 return pp_sysread();
1705 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1707 gv = PL_last_in_gv = PL_argvgv;
1709 if (io && !IoIFP(io)) {
1710 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1712 IoFLAGS(io) &= ~IOf_START;
1713 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1714 sv_setpvn(GvSV(gv), "-", 1);
1715 SvSETMAGIC(GvSV(gv));
1717 else if (!nextargv(gv))
1722 gv = PL_last_in_gv; /* eof */
1725 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1727 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1729 XPUSHs(SvTIED_obj((SV*)gv, mg));
1732 call_method("EOF", G_SCALAR);
1738 PUSHs(boolSV(!gv || do_eof(gv)));
1751 gv = PL_last_in_gv = (GV*)POPs;
1753 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1755 XPUSHs(SvTIED_obj((SV*)gv, mg));
1758 call_method("TELL", G_SCALAR);
1764 #if LSEEKSIZE > IVSIZE
1765 PUSHn( do_tell(gv) );
1767 PUSHi( do_tell(gv) );
1774 return pp_sysseek();
1782 #if LSEEKSIZE > IVSIZE
1783 Off_t offset = (Off_t)SvNVx(POPs);
1785 Off_t offset = (Off_t)SvIVx(POPs);
1789 gv = PL_last_in_gv = (GV*)POPs;
1791 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1793 XPUSHs(SvTIED_obj((SV*)gv, mg));
1794 XPUSHs(sv_2mortal(newSViv((IV) offset)));
1795 XPUSHs(sv_2mortal(newSViv((IV) whence)));
1798 call_method("SEEK", G_SCALAR);
1804 if (PL_op->op_type == OP_SEEK)
1805 PUSHs(boolSV(do_seek(gv, offset, whence)));
1807 Off_t n = do_sysseek(gv, offset, whence);
1809 PUSHs(&PL_sv_undef);
1812 #if LSEEKSIZE > IVSIZE
1817 : newSVpvn(zero_but_true, ZBTLEN);
1818 PUSHs(sv_2mortal(sv));
1827 Off_t len = (Off_t)POPn;
1833 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
1834 if (PL_op->op_flags & OPf_SPECIAL) {
1835 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
1837 TAINT_PROPER("truncate");
1838 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1840 ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1842 my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1851 if (SvTYPE(sv) == SVt_PVGV) {
1852 tmpgv = (GV*)sv; /* *main::FRED for example */
1855 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1856 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
1860 name = SvPV(sv, n_a);
1861 TAINT_PROPER("truncate");
1863 if (truncate(name, len) < 0)
1868 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
1871 if (my_chsize(tmpfd, len) < 0)
1873 PerlLIO_close(tmpfd);
1882 SETERRNO(EBADF,RMS$_IFI);
1885 DIE(aTHX_ "truncate not implemented");
1898 unsigned int func = U_I(POPn);
1899 int optype = PL_op->op_type;
1905 if (!io || !argsv || !IoIFP(io)) {
1906 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
1910 if (SvPOK(argsv) || !SvNIOK(argsv)) {
1913 s = SvPV_force(argsv, len);
1914 need = IOCPARM_LEN(func);
1916 s = Sv_Grow(argsv, need + 1);
1917 SvCUR_set(argsv, need);
1920 s[SvCUR(argsv)] = 17; /* a little sanity check here */
1923 retval = SvIV(argsv);
1924 s = INT2PTR(char*,retval); /* ouch */
1927 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1929 if (optype == OP_IOCTL)
1931 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
1933 DIE(aTHX_ "ioctl is not implemented");
1937 #if defined(OS2) && defined(__EMX__)
1938 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
1940 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
1943 DIE(aTHX_ "fcntl is not implemented");
1947 if (s[SvCUR(argsv)] != 17)
1948 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
1949 PL_op_name[optype]);
1950 s[SvCUR(argsv)] = 0; /* put our null back */
1951 SvSETMAGIC(argsv); /* Assume it has changed */
1960 PUSHp(zero_but_true, ZBTLEN);
1980 fp = IoIFP(GvIOp(gv));
1984 (void)PerlIO_flush(fp);
1985 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
1989 SETERRNO(EBADF,RMS$_IFI);
1990 if (ckWARN(WARN_CLOSED))
1991 report_closed_fh(gv, GvIO(gv), "flock", "filehandle");
1996 DIE(aTHX_ PL_no_func, "flock()");
2008 int protocol = POPi;
2016 SETERRNO(EBADF,LIB$_INVARG);
2022 do_close(gv, FALSE);
2024 TAINT_PROPER("socket");
2025 fd = PerlSock_socket(domain, type, protocol);
2028 IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */
2029 IoOFP(io) = PerlIO_fdopen(fd, "w");
2031 if (!IoIFP(io) || !IoOFP(io)) {
2032 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2033 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2034 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2040 DIE(aTHX_ PL_no_sock_func, "socket");
2047 #ifdef HAS_SOCKETPAIR
2052 int protocol = POPi;
2065 do_close(gv1, FALSE);
2067 do_close(gv2, FALSE);
2069 TAINT_PROPER("socketpair");
2070 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2072 IoIFP(io1) = PerlIO_fdopen(fd[0], "r");
2073 IoOFP(io1) = PerlIO_fdopen(fd[0], "w");
2075 IoIFP(io2) = PerlIO_fdopen(fd[1], "r");
2076 IoOFP(io2) = PerlIO_fdopen(fd[1], "w");
2078 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2079 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2080 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2081 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2082 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2083 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2084 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
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);
2250 PUSHp((char *)&saddr, len);
2254 if (ckWARN(WARN_CLOSED))
2255 report_closed_fh(ggv, ggv ? GvIO(ggv) : 0, "accept", "socket");
2256 SETERRNO(EBADF,SS$_IVCHAN);
2262 DIE(aTHX_ PL_no_sock_func, "accept");
2272 register IO *io = GvIOn(gv);
2274 if (!io || !IoIFP(io))
2277 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2281 if (ckWARN(WARN_CLOSED))
2282 report_closed_fh(gv, io, "shutdown", "socket");
2283 SETERRNO(EBADF,SS$_IVCHAN);
2286 DIE(aTHX_ PL_no_sock_func, "shutdown");
2293 return pp_ssockopt();
2295 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2303 int optype = PL_op->op_type;
2306 unsigned int optname;
2312 if (optype == OP_GSOCKOPT)
2313 sv = sv_2mortal(NEWSV(22, 257));
2316 optname = (unsigned int) POPi;
2317 lvl = (unsigned int) POPi;
2321 if (!io || !IoIFP(io))
2324 fd = PerlIO_fileno(IoIFP(io));
2328 (void)SvPOK_only(sv);
2332 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2347 aint = (int)SvIV(sv);
2351 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2360 if (ckWARN(WARN_CLOSED))
2361 report_closed_fh(gv, io,
2362 optype == OP_GSOCKOPT ? "getsockopt" : "setsockopt",
2364 SETERRNO(EBADF,SS$_IVCHAN);
2369 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2376 return pp_getpeername();
2378 DIE(aTHX_ PL_no_sock_func, "getsockname");
2386 int optype = PL_op->op_type;
2390 register IO *io = GvIOn(gv);
2393 if (!io || !IoIFP(io))
2396 sv = sv_2mortal(NEWSV(22, 257));
2397 (void)SvPOK_only(sv);
2401 fd = PerlIO_fileno(IoIFP(io));
2403 case OP_GETSOCKNAME:
2404 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2407 case OP_GETPEERNAME:
2408 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2410 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2412 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";
2413 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2414 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2415 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2416 sizeof(u_short) + sizeof(struct in_addr))) {
2423 #ifdef BOGUS_GETNAME_RETURN
2424 /* Interactive Unix, getpeername() and getsockname()
2425 does not return valid namelen */
2426 if (len == BOGUS_GETNAME_RETURN)
2427 len = sizeof(struct sockaddr);
2435 if (ckWARN(WARN_CLOSED))
2436 report_closed_fh(gv, io,
2437 optype == OP_GETSOCKNAME ? "getsockname"
2440 SETERRNO(EBADF,SS$_IVCHAN);
2445 DIE(aTHX_ PL_no_sock_func, "getpeername");
2464 if (PL_op->op_flags & OPf_REF) {
2467 if (tmpgv != PL_defgv) {
2468 PL_laststype = OP_STAT;
2470 sv_setpv(PL_statname, "");
2471 PL_laststatval = (GvIO(tmpgv) && IoIFP(GvIOp(tmpgv))
2472 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &PL_statcache) : -1);
2474 if (PL_laststatval < 0)
2479 if (SvTYPE(sv) == SVt_PVGV) {
2483 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2484 tmpgv = (GV*)SvRV(sv);
2487 sv_setpv(PL_statname, SvPV(sv,n_a));
2490 PL_laststype = PL_op->op_type;
2491 if (PL_op->op_type == OP_LSTAT)
2492 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2495 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2496 if (PL_laststatval < 0) {
2497 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2498 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
2504 if (gimme != G_ARRAY) {
2505 if (gimme != G_VOID)
2506 XPUSHs(boolSV(max));
2512 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2513 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2514 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mode)));
2515 PUSHs(sv_2mortal(newSViv(PL_statcache.st_nlink)));
2516 #if Uid_t_size > IVSIZE
2517 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2519 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2521 #if Gid_t_size > IVSIZE
2522 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2524 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2526 #ifdef USE_STAT_RDEV
2527 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2529 PUSHs(sv_2mortal(newSVpvn("", 0)));
2531 #if Off_t_size > IVSIZE
2532 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size)));
2534 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2537 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2538 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2539 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2541 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2542 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2543 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2545 #ifdef USE_STAT_BLOCKS
2546 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blksize)));
2547 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blocks)));
2549 PUSHs(sv_2mortal(newSVpvn("", 0)));
2550 PUSHs(sv_2mortal(newSVpvn("", 0)));
2560 #if defined(HAS_ACCESS) && defined(R_OK)
2562 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2563 result = access(TOPpx, R_OK);
2578 if (cando(S_IRUSR, 0, &PL_statcache))
2587 #if defined(HAS_ACCESS) && defined(W_OK)
2589 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2590 result = access(TOPpx, W_OK);
2605 if (cando(S_IWUSR, 0, &PL_statcache))
2614 #if defined(HAS_ACCESS) && defined(X_OK)
2616 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2617 result = access(TOPpx, X_OK);
2632 if (cando(S_IXUSR, 0, &PL_statcache))
2641 #ifdef PERL_EFF_ACCESS_R_OK
2643 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2644 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2659 if (cando(S_IRUSR, 1, &PL_statcache))
2668 #ifdef PERL_EFF_ACCESS_W_OK
2670 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2671 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2686 if (cando(S_IWUSR, 1, &PL_statcache))
2695 #ifdef PERL_EFF_ACCESS_X_OK
2697 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2698 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2713 if (cando(S_IXUSR, 1, &PL_statcache))
2720 I32 result = my_stat();
2729 return pp_ftrowned();
2734 I32 result = my_stat();
2738 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
2746 I32 result = my_stat();
2750 if (PL_statcache.st_size == 0)
2757 I32 result = my_stat();
2761 #if Off_t_size > IVSIZE
2762 PUSHn(PL_statcache.st_size);
2764 PUSHi(PL_statcache.st_size);
2771 I32 result = my_stat();
2775 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
2781 I32 result = my_stat();
2785 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
2791 I32 result = my_stat();
2795 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
2801 I32 result = my_stat();
2805 if (S_ISSOCK(PL_statcache.st_mode))
2812 I32 result = my_stat();
2816 if (S_ISCHR(PL_statcache.st_mode))
2823 I32 result = my_stat();
2827 if (S_ISBLK(PL_statcache.st_mode))
2834 I32 result = my_stat();
2838 if (S_ISREG(PL_statcache.st_mode))
2845 I32 result = my_stat();
2849 if (S_ISDIR(PL_statcache.st_mode))
2856 I32 result = my_stat();
2860 if (S_ISFIFO(PL_statcache.st_mode))
2867 I32 result = my_lstat();
2871 if (S_ISLNK(PL_statcache.st_mode))
2880 I32 result = my_stat();
2884 if (PL_statcache.st_mode & S_ISUID)
2894 I32 result = my_stat();
2898 if (PL_statcache.st_mode & S_ISGID)
2908 I32 result = my_stat();
2912 if (PL_statcache.st_mode & S_ISVTX)
2923 char *tmps = Nullch;
2926 if (PL_op->op_flags & OPf_REF)
2928 else if (isGV(TOPs))
2930 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2931 gv = (GV*)SvRV(POPs);
2933 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
2935 if (GvIO(gv) && IoIFP(GvIOp(gv)))
2936 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
2937 else if (tmps && isDIGIT(*tmps))
2941 if (PerlLIO_isatty(fd))
2946 #if defined(atarist) /* this will work with atariST. Configure will
2947 make guesses for other systems. */
2948 # define FILE_base(f) ((f)->_base)
2949 # define FILE_ptr(f) ((f)->_ptr)
2950 # define FILE_cnt(f) ((f)->_cnt)
2951 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
2961 register STDCHAR *s;
2968 if (PL_op->op_flags & OPf_REF)
2970 else if (isGV(TOPs))
2972 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2973 gv = (GV*)SvRV(POPs);
2979 if (gv == PL_defgv) {
2981 io = GvIO(PL_statgv);
2984 goto really_filename;
2989 PL_laststatval = -1;
2990 sv_setpv(PL_statname, "");
2991 io = GvIO(PL_statgv);
2993 if (io && IoIFP(io)) {
2994 if (! PerlIO_has_base(IoIFP(io)))
2995 DIE(aTHX_ "-T and -B not implemented on filehandles");
2996 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2997 if (PL_laststatval < 0)
2999 if (S_ISDIR(PL_statcache.st_mode)) /* handle NFS glitch */
3000 if (PL_op->op_type == OP_FTTEXT)
3004 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3005 i = PerlIO_getc(IoIFP(io));
3007 (void)PerlIO_ungetc(IoIFP(io),i);
3009 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3011 len = PerlIO_get_bufsiz(IoIFP(io));
3012 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3013 /* sfio can have large buffers - limit to 512 */
3018 if (ckWARN(WARN_UNOPENED)) {
3020 Perl_warner(aTHX_ WARN_UNOPENED, "Test on unopened file <%s>",
3023 SETERRNO(EBADF,RMS$_IFI);
3031 PL_laststatval = -1;
3032 sv_setpv(PL_statname, SvPV(sv, n_a));
3033 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3034 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3035 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
3038 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3039 if (PL_laststatval < 0) {
3040 (void)PerlIO_close(fp);
3043 do_binmode(fp, '<', TRUE);
3044 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3045 (void)PerlIO_close(fp);
3047 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3048 RETPUSHNO; /* special case NFS directories */
3049 RETPUSHYES; /* null file is anything */
3054 /* now scan s to look for textiness */
3055 /* XXX ASCII dependent code */
3057 #if defined(DOSISH) || defined(USEMYBINMODE)
3058 /* ignore trailing ^Z on short files */
3059 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3063 for (i = 0; i < len; i++, s++) {
3064 if (!*s) { /* null never allowed in text */
3069 else if (!(isPRINT(*s) || isSPACE(*s)))
3072 else if (*s & 128) {
3074 if (!(PL_op->op_private & OPpLOCALE) || !isALPHA_LC(*s))
3079 *s != '\n' && *s != '\r' && *s != '\b' &&
3080 *s != '\t' && *s != '\f' && *s != 27)
3085 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3109 if (!tmps || !*tmps) {
3110 svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE);
3112 tmps = SvPV(*svp, n_a);
3114 if (!tmps || !*tmps) {
3115 svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE);
3117 tmps = SvPV(*svp, n_a);
3120 if (!tmps || !*tmps) {
3121 svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE);
3123 tmps = SvPV(*svp, n_a);
3126 TAINT_PROPER("chdir");
3127 PUSHi( PerlDir_chdir(tmps) >= 0 );
3129 /* Clear the DEFAULT element of ENV so we'll get the new value
3131 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3138 djSP; dMARK; dTARGET;
3141 value = (I32)apply(PL_op->op_type, MARK, SP);
3146 DIE(aTHX_ PL_no_func, "Unsupported function chown");
3157 TAINT_PROPER("chroot");
3158 PUSHi( chroot(tmps) >= 0 );
3161 DIE(aTHX_ PL_no_func, "chroot");
3167 djSP; dMARK; dTARGET;
3169 value = (I32)apply(PL_op->op_type, MARK, SP);
3177 djSP; dMARK; dTARGET;
3179 value = (I32)apply(PL_op->op_type, MARK, SP);
3187 djSP; dMARK; dTARGET;
3189 value = (I32)apply(PL_op->op_type, MARK, SP);
3201 char *tmps2 = POPpx;
3202 char *tmps = SvPV(TOPs, n_a);
3203 TAINT_PROPER("rename");
3205 anum = PerlLIO_rename(tmps, tmps2);
3207 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3208 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3211 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3212 (void)UNLINK(tmps2);
3213 if (!(anum = link(tmps, tmps2)))
3214 anum = UNLINK(tmps);
3227 char *tmps2 = POPpx;
3228 char *tmps = SvPV(TOPs, n_a);
3229 TAINT_PROPER("link");
3230 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3232 DIE(aTHX_ PL_no_func, "Unsupported function link");
3242 char *tmps2 = POPpx;
3243 char *tmps = SvPV(TOPs, n_a);
3244 TAINT_PROPER("symlink");
3245 SETi( symlink(tmps, tmps2) >= 0 );
3248 DIE(aTHX_ PL_no_func, "symlink");
3257 char buf[MAXPATHLEN];
3261 #ifndef INCOMPLETE_TAINTS
3265 len = readlink(tmps, buf, sizeof buf);
3273 RETSETUNDEF; /* just pretend it's a normal file */
3277 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3279 S_dooneliner(pTHX_ char *cmd, char *filename)
3281 char *save_filename = filename;
3287 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3288 strcpy(cmdline, cmd);
3289 strcat(cmdline, " ");
3290 for (s = cmdline + strlen(cmdline); *filename; ) {
3295 myfp = PerlProc_popen(cmdline, "r");
3299 SV *tmpsv = sv_newmortal();
3300 /* Need to save/restore 'PL_rs' ?? */
3301 s = sv_gets(tmpsv, myfp, 0);
3302 (void)PerlProc_pclose(myfp);
3306 #ifdef HAS_SYS_ERRLIST
3311 /* you don't see this */
3313 #ifdef HAS_SYS_ERRLIST
3321 if (instr(s, errmsg)) {
3328 #define EACCES EPERM
3330 if (instr(s, "cannot make"))
3331 SETERRNO(EEXIST,RMS$_FEX);
3332 else if (instr(s, "existing file"))
3333 SETERRNO(EEXIST,RMS$_FEX);
3334 else if (instr(s, "ile exists"))
3335 SETERRNO(EEXIST,RMS$_FEX);
3336 else if (instr(s, "non-exist"))
3337 SETERRNO(ENOENT,RMS$_FNF);
3338 else if (instr(s, "does not exist"))
3339 SETERRNO(ENOENT,RMS$_FNF);
3340 else if (instr(s, "not empty"))
3341 SETERRNO(EBUSY,SS$_DEVOFFLINE);
3342 else if (instr(s, "cannot access"))
3343 SETERRNO(EACCES,RMS$_PRV);
3345 SETERRNO(EPERM,RMS$_PRV);
3348 else { /* some mkdirs return no failure indication */
3349 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3350 if (PL_op->op_type == OP_RMDIR)
3355 SETERRNO(EACCES,RMS$_PRV); /* a guess */
3372 char *tmps = SvPV(TOPs, n_a);
3374 TAINT_PROPER("mkdir");
3376 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3378 SETi( dooneliner("mkdir", tmps) );
3379 oldumask = PerlLIO_umask(0);
3380 PerlLIO_umask(oldumask);
3381 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3393 TAINT_PROPER("rmdir");
3395 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3397 XPUSHi( dooneliner("rmdir", tmps) );
3402 /* Directory calls. */
3407 #if defined(Direntry_t) && defined(HAS_READDIR)
3409 char *dirname = POPpx;
3411 register IO *io = GvIOn(gv);
3417 PerlDir_close(IoDIRP(io));
3418 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3424 SETERRNO(EBADF,RMS$_DIR);
3427 DIE(aTHX_ PL_no_dir_func, "opendir");
3434 #if defined(Direntry_t) && defined(HAS_READDIR)
3436 Direntry_t *readdir (DIR *);
3438 register Direntry_t *dp;
3440 register IO *io = GvIOn(gv);
3443 if (!io || !IoDIRP(io))
3446 if (GIMME == G_ARRAY) {
3448 while (dp = (Direntry_t *)PerlDir_read(IoDIRP(io))) {
3450 sv = newSVpvn(dp->d_name, dp->d_namlen);
3452 sv = newSVpv(dp->d_name, 0);
3454 #ifndef INCOMPLETE_TAINTS
3457 XPUSHs(sv_2mortal(sv));
3461 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3464 sv = newSVpvn(dp->d_name, dp->d_namlen);
3466 sv = newSVpv(dp->d_name, 0);
3468 #ifndef INCOMPLETE_TAINTS
3471 XPUSHs(sv_2mortal(sv));
3477 SETERRNO(EBADF,RMS$_ISI);
3478 if (GIMME == G_ARRAY)
3483 DIE(aTHX_ PL_no_dir_func, "readdir");
3490 #if defined(HAS_TELLDIR) || defined(telldir)
3491 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3492 /* XXX netbsd still seemed to.
3493 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3494 --JHI 1999-Feb-02 */
3495 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3496 long telldir (DIR *);
3499 register IO *io = GvIOn(gv);
3501 if (!io || !IoDIRP(io))
3504 PUSHi( PerlDir_tell(IoDIRP(io)) );
3508 SETERRNO(EBADF,RMS$_ISI);
3511 DIE(aTHX_ PL_no_dir_func, "telldir");
3518 #if defined(HAS_SEEKDIR) || defined(seekdir)
3521 register IO *io = GvIOn(gv);
3523 if (!io || !IoDIRP(io))
3526 (void)PerlDir_seek(IoDIRP(io), along);
3531 SETERRNO(EBADF,RMS$_ISI);
3534 DIE(aTHX_ PL_no_dir_func, "seekdir");
3541 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3543 register IO *io = GvIOn(gv);
3545 if (!io || !IoDIRP(io))
3548 (void)PerlDir_rewind(IoDIRP(io));
3552 SETERRNO(EBADF,RMS$_ISI);
3555 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3562 #if defined(Direntry_t) && defined(HAS_READDIR)
3564 register IO *io = GvIOn(gv);
3566 if (!io || !IoDIRP(io))
3569 #ifdef VOID_CLOSEDIR
3570 PerlDir_close(IoDIRP(io));
3572 if (PerlDir_close(IoDIRP(io)) < 0) {
3573 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3582 SETERRNO(EBADF,RMS$_IFI);
3585 DIE(aTHX_ PL_no_dir_func, "closedir");
3589 /* Process control. */
3599 PERL_FLUSHALL_FOR_CHILD;
3605 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
3606 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3607 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3612 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3617 PERL_FLUSHALL_FOR_CHILD;
3618 childpid = PerlProc_fork();
3622 DIE(aTHX_ PL_no_func, "Unsupported function fork");
3629 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3634 childpid = wait4pid(-1, &argflags, 0);
3635 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3639 DIE(aTHX_ PL_no_func, "Unsupported function wait");
3645 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3653 childpid = wait4pid(childpid, &argflags, optype);
3654 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3658 DIE(aTHX_ PL_no_func, "Unsupported function waitpid");
3664 djSP; dMARK; dORIGMARK; dTARGET;
3669 Sigsave_t ihand,qhand; /* place to save signals during system() */
3674 if (SP - MARK == 1) {
3676 char *junk = SvPV(TOPs, n_a);
3678 TAINT_PROPER("system");
3681 PERL_FLUSHALL_FOR_CHILD;
3682 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2)
3683 if (PerlProc_pipe(pp) >= 0)
3685 while ((childpid = vfork()) == -1) {
3686 if (errno != EAGAIN) {
3691 PerlLIO_close(pp[0]);
3692 PerlLIO_close(pp[1]);
3700 PerlLIO_close(pp[1]);
3701 rsignal_save(SIGINT, SIG_IGN, &ihand);
3702 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
3704 result = wait4pid(childpid, &status, 0);
3705 } while (result == -1 && errno == EINTR);
3706 (void)rsignal_restore(SIGINT, &ihand);
3707 (void)rsignal_restore(SIGQUIT, &qhand);
3708 STATUS_NATIVE_SET(result == -1 ? -1 : status);
3709 do_execfree(); /* free any memory child malloced on vfork */
3715 while (n < sizeof(int)) {
3716 n1 = PerlLIO_read(pp[0],
3717 (void*)(((char*)&errkid)+n),
3723 PerlLIO_close(pp[0]);
3724 if (n) { /* Error */
3725 if (n != sizeof(int))
3726 DIE(aTHX_ "panic: kid popen errno read");
3727 errno = errkid; /* Propagate errno from kid */
3728 STATUS_CURRENT = -1;
3731 PUSHi(STATUS_CURRENT);
3735 PerlLIO_close(pp[0]);
3736 #if defined(HAS_FCNTL) && defined(F_SETFD)
3737 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
3740 if (PL_op->op_flags & OPf_STACKED) {
3741 SV *really = *++MARK;
3742 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
3744 else if (SP - MARK != 1)
3745 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
3747 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
3750 #else /* ! FORK or VMS or OS/2 */
3751 if (PL_op->op_flags & OPf_STACKED) {
3752 SV *really = *++MARK;
3753 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
3755 else if (SP - MARK != 1)
3756 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
3758 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3760 STATUS_NATIVE_SET(value);
3763 PUSHi(STATUS_CURRENT);
3764 #endif /* !FORK or VMS */
3770 djSP; dMARK; dORIGMARK; dTARGET;
3774 PERL_FLUSHALL_FOR_CHILD;
3775 if (PL_op->op_flags & OPf_STACKED) {
3776 SV *really = *++MARK;
3777 value = (I32)do_aexec(really, MARK, SP);
3779 else if (SP - MARK != 1)
3781 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
3785 (void ) do_aspawn(Nullsv, MARK, SP);
3789 value = (I32)do_aexec(Nullsv, MARK, SP);
3794 char *junk = SvPV(*SP, n_a);
3796 TAINT_PROPER("exec");
3799 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3802 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3805 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3810 #if !defined(HAS_FORK) && defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3822 djSP; dMARK; dTARGET;
3825 value = (I32)apply(PL_op->op_type, MARK, SP);
3830 DIE(aTHX_ PL_no_func, "Unsupported function kill");
3838 XPUSHi( getppid() );
3841 DIE(aTHX_ PL_no_func, "getppid");
3857 pgrp = (I32)BSD_GETPGRP(pid);
3859 if (pid != 0 && pid != PerlProc_getpid())
3860 DIE(aTHX_ "POSIX getpgrp can't take an argument");
3866 DIE(aTHX_ PL_no_func, "getpgrp()");
3885 TAINT_PROPER("setpgrp");
3887 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
3889 if ((pgrp != 0 && pgrp != PerlProc_getpid())
3890 || (pid != 0 && pid != PerlProc_getpid()))
3892 DIE(aTHX_ "setpgrp can't take arguments");
3894 SETi( setpgrp() >= 0 );
3895 #endif /* USE_BSDPGRP */
3898 DIE(aTHX_ PL_no_func, "setpgrp()");
3907 #ifdef HAS_GETPRIORITY
3910 SETi( getpriority(which, who) );
3913 DIE(aTHX_ PL_no_func, "getpriority()");
3923 #ifdef HAS_SETPRIORITY
3927 TAINT_PROPER("setpriority");
3928 SETi( setpriority(which, who, niceval) >= 0 );
3931 DIE(aTHX_ PL_no_func, "setpriority()");
3941 XPUSHn( time(Null(Time_t*)) );
3943 XPUSHi( time(Null(Time_t*)) );
3948 /* XXX The POSIX name is CLK_TCK; it is to be preferred
3949 to HZ. Probably. For now, assume that if the system
3950 defines HZ, it does so correctly. (Will this break
3952 Probably we ought to use _sysconf(_SC_CLK_TCK), if
3953 it's supported. --AD 9/96.
3969 DIE(aTHX_ "times not implemented");
3974 (void)PerlProc_times(&PL_timesbuf);
3976 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
3977 /* struct tms, though same data */
3981 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
3982 if (GIMME == G_ARRAY) {
3983 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
3984 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
3985 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
3988 #endif /* HAS_TIMES */
4001 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4002 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4003 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4009 when = (Time_t)SvNVx(POPs);
4011 when = (Time_t)SvIVx(POPs);
4014 if (PL_op->op_type == OP_LOCALTIME)
4015 tmbuf = localtime(&when);
4017 tmbuf = gmtime(&when);
4021 if (GIMME != G_ARRAY) {
4026 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4027 dayname[tmbuf->tm_wday],
4028 monname[tmbuf->tm_mon],
4033 tmbuf->tm_year + 1900);
4034 PUSHs(sv_2mortal(tsv));
4037 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4038 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4039 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4040 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4041 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4042 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4043 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4044 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4045 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4056 anum = alarm((unsigned int)anum);
4063 DIE(aTHX_ PL_no_func, "Unsupported function alarm");
4074 (void)time(&lasttime);
4079 PerlProc_sleep((unsigned int)duration);
4082 XPUSHi(when - lasttime);
4086 /* Shared memory. */
4100 return pp_shmwrite();
4105 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4106 djSP; dMARK; dTARGET;
4107 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4116 /* Message passing. */
4130 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4131 djSP; dMARK; dTARGET;
4132 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4143 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4144 djSP; dMARK; dTARGET;
4145 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4158 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4159 djSP; dMARK; dTARGET;
4160 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4167 DIE(aTHX_ "System V IPC is not implemented on this machine");
4173 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4174 djSP; dMARK; dTARGET;
4175 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4183 PUSHp(zero_but_true, ZBTLEN);
4193 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4194 djSP; dMARK; dTARGET;
4195 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4204 /* Get system info. */
4208 #ifdef HAS_GETHOSTBYNAME
4209 return pp_ghostent();
4211 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4217 #ifdef HAS_GETHOSTBYADDR
4218 return pp_ghostent();
4220 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4227 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4228 I32 which = PL_op->op_type;
4229 register char **elem;
4231 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4232 struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4233 struct hostent *PerlSock_gethostbyname(Netdb_name_t);
4234 struct hostent *PerlSock_gethostent(void);
4236 struct hostent *hent;
4241 if (which == OP_GHBYNAME)
4242 #ifdef HAS_GETHOSTBYNAME
4243 hent = PerlSock_gethostbyname(POPpx);
4245 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4247 else if (which == OP_GHBYADDR) {
4248 #ifdef HAS_GETHOSTBYADDR
4249 int addrtype = POPi;
4252 Netdb_host_t addr = (Netdb_host_t) SvPV(addrsv, addrlen);
4254 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4256 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4260 #ifdef HAS_GETHOSTENT
4261 hent = PerlSock_gethostent();
4263 DIE(aTHX_ PL_no_sock_func, "gethostent");
4266 #ifdef HOST_NOT_FOUND
4268 STATUS_NATIVE_SET(h_errno);
4271 if (GIMME != G_ARRAY) {
4272 PUSHs(sv = sv_newmortal());
4274 if (which == OP_GHBYNAME) {
4276 sv_setpvn(sv, hent->h_addr, hent->h_length);
4279 sv_setpv(sv, (char*)hent->h_name);
4285 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4286 sv_setpv(sv, (char*)hent->h_name);
4287 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4288 for (elem = hent->h_aliases; elem && *elem; elem++) {
4289 sv_catpv(sv, *elem);
4291 sv_catpvn(sv, " ", 1);
4293 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4294 sv_setiv(sv, (IV)hent->h_addrtype);
4295 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4296 len = hent->h_length;
4297 sv_setiv(sv, (IV)len);
4299 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4300 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4301 sv_setpvn(sv, *elem, len);
4304 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4306 sv_setpvn(sv, hent->h_addr, len);
4311 DIE(aTHX_ PL_no_sock_func, "gethostent");
4317 #ifdef HAS_GETNETBYNAME
4318 return pp_gnetent();
4320 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4326 #ifdef HAS_GETNETBYADDR
4327 return pp_gnetent();
4329 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4336 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4337 I32 which = PL_op->op_type;
4338 register char **elem;
4340 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4341 struct netent *PerlSock_getnetbyaddr(Netdb_net_t, int);
4342 struct netent *PerlSock_getnetbyname(Netdb_name_t);
4343 struct netent *PerlSock_getnetent(void);
4345 struct netent *nent;
4348 if (which == OP_GNBYNAME)
4349 #ifdef HAS_GETNETBYNAME
4350 nent = PerlSock_getnetbyname(POPpx);
4352 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4354 else if (which == OP_GNBYADDR) {
4355 #ifdef HAS_GETNETBYADDR
4356 int addrtype = POPi;
4357 Netdb_net_t addr = (Netdb_net_t) U_L(POPn);
4358 nent = PerlSock_getnetbyaddr(addr, addrtype);
4360 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4364 #ifdef HAS_GETNETENT
4365 nent = PerlSock_getnetent();
4367 DIE(aTHX_ PL_no_sock_func, "getnetent");
4371 if (GIMME != G_ARRAY) {
4372 PUSHs(sv = sv_newmortal());
4374 if (which == OP_GNBYNAME)
4375 sv_setiv(sv, (IV)nent->n_net);
4377 sv_setpv(sv, nent->n_name);
4383 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4384 sv_setpv(sv, nent->n_name);
4385 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4386 for (elem = nent->n_aliases; elem && *elem; elem++) {
4387 sv_catpv(sv, *elem);
4389 sv_catpvn(sv, " ", 1);
4391 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4392 sv_setiv(sv, (IV)nent->n_addrtype);
4393 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4394 sv_setiv(sv, (IV)nent->n_net);
4399 DIE(aTHX_ PL_no_sock_func, "getnetent");
4405 #ifdef HAS_GETPROTOBYNAME
4406 return pp_gprotoent();
4408 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4414 #ifdef HAS_GETPROTOBYNUMBER
4415 return pp_gprotoent();
4417 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4424 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4425 I32 which = PL_op->op_type;
4426 register char **elem;
4428 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4429 struct protoent *PerlSock_getprotobyname(Netdb_name_t);
4430 struct protoent *PerlSock_getprotobynumber(int);
4431 struct protoent *PerlSock_getprotoent(void);
4433 struct protoent *pent;
4436 if (which == OP_GPBYNAME)
4437 #ifdef HAS_GETPROTOBYNAME
4438 pent = PerlSock_getprotobyname(POPpx);
4440 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4442 else if (which == OP_GPBYNUMBER)
4443 #ifdef HAS_GETPROTOBYNUMBER
4444 pent = PerlSock_getprotobynumber(POPi);
4446 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4449 #ifdef HAS_GETPROTOENT
4450 pent = PerlSock_getprotoent();
4452 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4456 if (GIMME != G_ARRAY) {
4457 PUSHs(sv = sv_newmortal());
4459 if (which == OP_GPBYNAME)
4460 sv_setiv(sv, (IV)pent->p_proto);
4462 sv_setpv(sv, pent->p_name);
4468 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4469 sv_setpv(sv, pent->p_name);
4470 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4471 for (elem = pent->p_aliases; elem && *elem; elem++) {
4472 sv_catpv(sv, *elem);
4474 sv_catpvn(sv, " ", 1);
4476 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4477 sv_setiv(sv, (IV)pent->p_proto);
4482 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4488 #ifdef HAS_GETSERVBYNAME
4489 return pp_gservent();
4491 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4497 #ifdef HAS_GETSERVBYPORT
4498 return pp_gservent();
4500 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4507 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4508 I32 which = PL_op->op_type;
4509 register char **elem;
4511 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4512 struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t);
4513 struct servent *PerlSock_getservbyport(int, Netdb_name_t);
4514 struct servent *PerlSock_getservent(void);
4516 struct servent *sent;
4519 if (which == OP_GSBYNAME) {
4520 #ifdef HAS_GETSERVBYNAME
4521 char *proto = POPpx;
4524 if (proto && !*proto)
4527 sent = PerlSock_getservbyname(name, proto);
4529 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4532 else if (which == OP_GSBYPORT) {
4533 #ifdef HAS_GETSERVBYPORT
4534 char *proto = POPpx;
4535 unsigned short port = POPu;
4538 port = PerlSock_htons(port);
4540 sent = PerlSock_getservbyport(port, proto);
4542 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4546 #ifdef HAS_GETSERVENT
4547 sent = PerlSock_getservent();
4549 DIE(aTHX_ PL_no_sock_func, "getservent");
4553 if (GIMME != G_ARRAY) {
4554 PUSHs(sv = sv_newmortal());
4556 if (which == OP_GSBYNAME) {
4558 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4560 sv_setiv(sv, (IV)(sent->s_port));
4564 sv_setpv(sv, sent->s_name);
4570 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4571 sv_setpv(sv, sent->s_name);
4572 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4573 for (elem = sent->s_aliases; elem && *elem; elem++) {
4574 sv_catpv(sv, *elem);
4576 sv_catpvn(sv, " ", 1);
4578 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4580 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4582 sv_setiv(sv, (IV)(sent->s_port));
4584 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4585 sv_setpv(sv, sent->s_proto);
4590 DIE(aTHX_ PL_no_sock_func, "getservent");
4597 #ifdef HAS_SETHOSTENT
4598 PerlSock_sethostent(TOPi);
4601 DIE(aTHX_ PL_no_sock_func, "sethostent");
4608 #ifdef HAS_SETNETENT
4609 PerlSock_setnetent(TOPi);
4612 DIE(aTHX_ PL_no_sock_func, "setnetent");
4619 #ifdef HAS_SETPROTOENT
4620 PerlSock_setprotoent(TOPi);
4623 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4630 #ifdef HAS_SETSERVENT
4631 PerlSock_setservent(TOPi);
4634 DIE(aTHX_ PL_no_sock_func, "setservent");
4641 #ifdef HAS_ENDHOSTENT
4642 PerlSock_endhostent();
4646 DIE(aTHX_ PL_no_sock_func, "endhostent");
4653 #ifdef HAS_ENDNETENT
4654 PerlSock_endnetent();
4658 DIE(aTHX_ PL_no_sock_func, "endnetent");
4665 #ifdef HAS_ENDPROTOENT
4666 PerlSock_endprotoent();
4670 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4677 #ifdef HAS_ENDSERVENT
4678 PerlSock_endservent();
4682 DIE(aTHX_ PL_no_sock_func, "endservent");
4691 DIE(aTHX_ PL_no_func, "getpwnam");
4700 DIE(aTHX_ PL_no_func, "getpwuid");
4707 #if defined(HAS_PASSWD) && defined(HAS_GETPWENT)
4708 I32 which = PL_op->op_type;
4710 struct passwd *pwent;
4712 #if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4713 struct spwd *spwent = NULL;
4716 if (which == OP_GPWNAM)
4717 pwent = getpwnam(POPpx);
4718 else if (which == OP_GPWUID)
4719 pwent = getpwuid(POPi);
4721 pwent = (struct passwd *)getpwent();
4724 if (which == OP_GPWNAM) {
4726 spwent = getspnam(pwent->pw_name);
4728 # ifdef HAS_GETSPUID /* AFAIK there isn't any anywhere. --jhi */
4729 else if (which == OP_GPWUID) {
4731 spwent = getspnam(pwent->pw_name);
4734 # ifdef HAS_GETSPENT
4736 spwent = (struct spwd *)getspent();
4741 if (GIMME != G_ARRAY) {
4742 PUSHs(sv = sv_newmortal());
4744 if (which == OP_GPWNAM)
4745 sv_setiv(sv, (IV)pwent->pw_uid);
4747 sv_setpv(sv, pwent->pw_name);
4753 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4754 sv_setpv(sv, pwent->pw_name);
4756 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4758 # if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4760 sv_setpv(sv, spwent->sp_pwdp);
4762 sv_setpv(sv, pwent->pw_passwd);
4764 sv_setpv(sv, pwent->pw_passwd);
4768 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4769 sv_setiv(sv, (IV)pwent->pw_uid);
4771 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4772 sv_setiv(sv, (IV)pwent->pw_gid);
4774 /* pw_change, pw_quota, and pw_age are mutually exclusive. */
4775 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4777 sv_setiv(sv, (IV)pwent->pw_change);
4780 sv_setiv(sv, (IV)pwent->pw_quota);
4783 sv_setpv(sv, pwent->pw_age);
4788 /* pw_class and pw_comment are mutually exclusive. */
4789 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4791 sv_setpv(sv, pwent->pw_class);
4794 sv_setpv(sv, pwent->pw_comment);
4798 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4800 sv_setpv(sv, pwent->pw_gecos);
4802 #ifndef INCOMPLETE_TAINTS
4803 /* pw_gecos is tainted because user himself can diddle with it. */
4807 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4808 sv_setpv(sv, pwent->pw_dir);
4810 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4811 sv_setpv(sv, pwent->pw_shell);
4814 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4815 sv_setiv(sv, (IV)pwent->pw_expire);
4820 DIE(aTHX_ PL_no_func, "getpwent");
4827 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
4829 # ifdef HAS_SETSPENT
4834 DIE(aTHX_ PL_no_func, "setpwent");
4841 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
4843 # ifdef HAS_ENDSPENT
4848 DIE(aTHX_ PL_no_func, "endpwent");
4857 DIE(aTHX_ PL_no_func, "getgrnam");
4866 DIE(aTHX_ PL_no_func, "getgrgid");
4873 #if defined(HAS_GROUP) && defined(HAS_GETGRENT)
4874 I32 which = PL_op->op_type;
4875 register char **elem;
4877 struct group *grent;
4880 if (which == OP_GGRNAM)
4881 grent = (struct group *)getgrnam(POPpx);
4882 else if (which == OP_GGRGID)
4883 grent = (struct group *)getgrgid(POPi);
4885 grent = (struct group *)getgrent();
4888 if (GIMME != G_ARRAY) {
4889 PUSHs(sv = sv_newmortal());
4891 if (which == OP_GGRNAM)
4892 sv_setiv(sv, (IV)grent->gr_gid);
4894 sv_setpv(sv, grent->gr_name);
4900 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4901 sv_setpv(sv, grent->gr_name);
4903 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4905 sv_setpv(sv, grent->gr_passwd);
4908 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4909 sv_setiv(sv, (IV)grent->gr_gid);
4911 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4912 for (elem = grent->gr_mem; elem && *elem; elem++) {
4913 sv_catpv(sv, *elem);
4915 sv_catpvn(sv, " ", 1);
4921 DIE(aTHX_ PL_no_func, "getgrent");
4928 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
4932 DIE(aTHX_ PL_no_func, "setgrent");
4939 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
4943 DIE(aTHX_ PL_no_func, "endgrent");
4953 if (!(tmps = PerlProc_getlogin()))
4955 PUSHp(tmps, strlen(tmps));
4958 DIE(aTHX_ PL_no_func, "getlogin");
4962 /* Miscellaneous. */
4967 djSP; dMARK; dORIGMARK; dTARGET;
4968 register I32 items = SP - MARK;
4969 unsigned long a[20];
4976 while (++MARK <= SP) {
4977 if (SvTAINTED(*MARK)) {
4983 TAINT_PROPER("syscall");
4986 /* This probably won't work on machines where sizeof(long) != sizeof(int)
4987 * or where sizeof(long) != sizeof(char*). But such machines will
4988 * not likely have syscall implemented either, so who cares?
4990 while (++MARK <= SP) {
4991 if (SvNIOK(*MARK) || !i)
4992 a[i++] = SvIV(*MARK);
4993 else if (*MARK == &PL_sv_undef)
4996 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5002 DIE(aTHX_ "Too many args to syscall");
5004 DIE(aTHX_ "Too few args to syscall");
5006 retval = syscall(a[0]);
5009 retval = syscall(a[0],a[1]);
5012 retval = syscall(a[0],a[1],a[2]);
5015 retval = syscall(a[0],a[1],a[2],a[3]);
5018 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5021 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5024 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5027 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5031 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5034 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5037 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5041 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5045 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5049 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5050 a[10],a[11],a[12],a[13]);
5052 #endif /* atarist */
5058 DIE(aTHX_ PL_no_func, "syscall");
5062 #ifdef FCNTL_EMULATE_FLOCK
5064 /* XXX Emulate flock() with fcntl().
5065 What's really needed is a good file locking module.
5069 fcntl_emulate_flock(int fd, int operation)
5073 switch (operation & ~LOCK_NB) {
5075 flock.l_type = F_RDLCK;
5078 flock.l_type = F_WRLCK;
5081 flock.l_type = F_UNLCK;
5087 flock.l_whence = SEEK_SET;
5088 flock.l_start = flock.l_len = (Off_t)0;
5090 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5093 #endif /* FCNTL_EMULATE_FLOCK */
5095 #ifdef LOCKF_EMULATE_FLOCK
5097 /* XXX Emulate flock() with lockf(). This is just to increase
5098 portability of scripts. The calls are not completely
5099 interchangeable. What's really needed is a good file
5103 /* The lockf() constants might have been defined in <unistd.h>.
5104 Unfortunately, <unistd.h> causes troubles on some mixed
5105 (BSD/POSIX) systems, such as SunOS 4.1.3.
5107 Further, the lockf() constants aren't POSIX, so they might not be
5108 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5109 just stick in the SVID values and be done with it. Sigh.
5113 # define F_ULOCK 0 /* Unlock a previously locked region */
5116 # define F_LOCK 1 /* Lock a region for exclusive use */
5119 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5122 # define F_TEST 3 /* Test a region for other processes locks */
5126 lockf_emulate_flock(int fd, int operation)
5132 /* flock locks entire file so for lockf we need to do the same */
5134 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5135 if (pos > 0) /* is seekable and needs to be repositioned */
5136 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5137 pos = -1; /* seek failed, so don't seek back afterwards */
5140 switch (operation) {
5142 /* LOCK_SH - get a shared lock */
5144 /* LOCK_EX - get an exclusive lock */
5146 i = lockf (fd, F_LOCK, 0);
5149 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5150 case LOCK_SH|LOCK_NB:
5151 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5152 case LOCK_EX|LOCK_NB:
5153 i = lockf (fd, F_TLOCK, 0);
5155 if ((errno == EAGAIN) || (errno == EACCES))
5156 errno = EWOULDBLOCK;
5159 /* LOCK_UN - unlock (non-blocking is a no-op) */
5161 case LOCK_UN|LOCK_NB:
5162 i = lockf (fd, F_ULOCK, 0);
5165 /* Default - can't decipher operation */
5172 if (pos > 0) /* need to restore position of the handle */
5173 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5178 #endif /* LOCKF_EMULATE_FLOCK */