3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * But only a short way ahead its floor and the walls on either side were
12 * cloven by a great fissure, out of which the red glare came, now leaping
13 * up, now dying down into darkness; and all the while far below there was
14 * a rumour and a trouble as of great engines throbbing and labouring.
18 #define PERL_IN_PP_SYS_C
22 /* Shadow password support for solaris - pdo@cs.umd.edu
23 * Not just Solaris: at least HP-UX, IRIX, Linux.
24 * the API is from SysV. --jhi */
26 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
27 * and another MAXINT from "perl.h" <- <sys/param.h>. */
33 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
40 extern "C" int syscall(unsigned long,...);
45 # include <sys/wait.h>
49 # include <sys/resource.h>
52 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
53 # include <sys/socket.h>
54 # if defined(USE_SOCKS) && defined(I_SOCKS)
62 # include <net/errno.h>
69 #include <sys/select.h>
73 /* XXX Configure test needed.
74 h_errno might not be a simple 'int', especially for multi-threaded
75 applications, see "extern int errno in perl.h". Creating such
76 a test requires taking into account the differences between
77 compiling multithreaded and singlethreaded ($ccflags et al).
78 HOST_NOT_FOUND is typically defined in <netdb.h>.
80 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
88 struct passwd *getpwnam (char *);
89 struct passwd *getpwuid (Uid_t);
92 struct passwd *getpwent (void);
100 struct group *getgrnam (char *);
101 struct group *getgrgid (Gid_t);
104 struct group *getgrent (void);
109 # if defined(_MSC_VER) || defined(__MINGW32__)
110 # include <sys/utime.h>
116 /* Put this after #includes because fork and vfork prototypes may conflict. */
122 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
125 # define my_chsize PerlLIO_chsize
130 #else /* no flock() */
132 /* fcntl.h might not have been included, even if it exists, because
133 the current Configure only sets I_FCNTL if it's needed to pick up
134 the *_OK constants. Make sure it has been included before testing
135 the fcntl() locking constants. */
136 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
140 # if defined(HAS_FCNTL) && defined(F_SETLK) && defined (F_SETLKW)
141 # define FLOCK fcntl_emulate_flock
142 # define FCNTL_EMULATE_FLOCK
143 # else /* no flock() or fcntl(F_SETLK,...) */
145 # define FLOCK lockf_emulate_flock
146 # define LOCKF_EMULATE_FLOCK
148 # endif /* no flock() or fcntl(F_SETLK,...) */
151 static int FLOCK (int, int);
154 * These are the flock() constants. Since this sytems doesn't have
155 * flock(), the values of the constants are probably not available.
169 # endif /* emulating flock() */
171 #endif /* no flock() */
174 static char zero_but_true[ZBTLEN + 1] = "0 but true";
176 #if defined(I_SYS_ACCESS) && !defined(R_OK)
177 # include <sys/access.h>
180 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
181 # define FD_CLOEXEC 1 /* NeXT needs this */
184 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
185 #undef PERL_EFF_ACCESS_W_OK
186 #undef PERL_EFF_ACCESS_X_OK
188 /* F_OK unused: if stat() cannot find it... */
190 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
191 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
192 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
193 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
194 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
197 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
198 # if defined(I_SYS_SECURITY)
199 # include <sys/security.h>
201 /* XXX Configure test needed for eaccess */
204 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
205 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
206 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
209 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
210 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
211 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
215 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
217 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
218 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
219 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
222 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
223 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
224 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
227 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
229 Uid_t ruid = getuid();
230 Uid_t euid = geteuid();
231 Gid_t rgid = getgid();
232 Gid_t egid = getegid();
236 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
237 Perl_croak(aTHX_ "switching effective uid is not implemented");
240 if (setreuid(euid, ruid))
243 if (setresuid(euid, ruid, (Uid_t)-1))
246 Perl_croak(aTHX_ "entering effective uid failed");
249 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
250 Perl_croak(aTHX_ "switching effective gid is not implemented");
253 if (setregid(egid, rgid))
256 if (setresgid(egid, rgid, (Gid_t)-1))
259 Perl_croak(aTHX_ "entering effective gid failed");
262 res = access(path, mode);
265 if (setreuid(ruid, euid))
268 if (setresuid(ruid, euid, (Uid_t)-1))
271 Perl_croak(aTHX_ "leaving effective uid failed");
274 if (setregid(rgid, egid))
277 if (setresgid(rgid, egid, (Gid_t)-1))
280 Perl_croak(aTHX_ "leaving effective gid failed");
285 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
286 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
287 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
290 #if !defined(PERL_EFF_ACCESS_R_OK)
292 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
294 Perl_croak(aTHX_ "switching effective uid is not implemented");
309 fp = PerlProc_popen(tmps, "r");
311 if (gimme == G_VOID) {
313 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
317 else if (gimme == G_SCALAR) {
318 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
319 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
330 if (sv_gets(sv, fp, 0) == Nullch) {
334 XPUSHs(sv_2mortal(sv));
335 if (SvLEN(sv) - SvCUR(sv) > 20) {
336 SvLEN_set(sv, SvCUR(sv)+1);
337 Renew(SvPVX(sv), SvLEN(sv), char);
342 STATUS_NATIVE_SET(PerlProc_pclose(fp));
343 TAINT; /* "I believe that this is not gratuitous!" */
346 STATUS_NATIVE_SET(-1);
347 if (gimme == G_SCALAR)
357 tryAMAGICunTARGET(iter, -1);
362 /* If we're not using an external glob, just let readdir() tainting
363 * do its thing. Otherwise, engage paranoia mode. */
364 #if defined(PERL_EXTERNAL_GLOB)
367 * The external globbing program may use things we can't control,
368 * so for security reasons we must assume the worst.
371 taint_proper(PL_no_security, "glob");
373 #endif /* PERL_EXTERNAL_GLOB */
376 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
377 PL_last_in_gv = (GV*)*PL_stack_sp--;
379 SAVESPTR(PL_rs); /* This is not permanent, either. */
380 PL_rs = sv_2mortal(newSVpvn("\000", 1));
383 *SvPVX(PL_rs) = '\n';
387 result = do_readline();
392 #if 0 /* XXX never used! */
396 PL_last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*PL_stack_sp--)), n_a), TRUE,SVt_PVIO);
397 return do_readline();
403 PL_last_in_gv = cGVOP_gv;
404 return do_readline();
413 if (SP - MARK != 1) {
415 do_join(TARG, &PL_sv_no, MARK, SP);
422 tmps = SvPV(tmpsv, len);
425 (void)SvUPGRADE(error, SVt_PV);
426 if (SvPOK(error) && SvCUR(error))
427 sv_catpv(error, "\t...caught");
429 tmps = SvPV(tmpsv, len);
432 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
434 Perl_warn(aTHX_ "%"SVf, tmpsv);
445 if (SP - MARK != 1) {
447 do_join(TARG, &PL_sv_no, MARK, SP);
449 tmps = SvPV(tmpsv, len);
455 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
459 (void)SvUPGRADE(error, SVt_PV);
460 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
462 SvSetSV(error,tmpsv);
463 else if (sv_isobject(error)) {
464 HV *stash = SvSTASH(SvRV(error));
465 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
467 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
468 SV *line = sv_2mortal(newSViv(CopLINE(PL_curcop)));
475 call_sv((SV*)GvCV(gv),
476 G_SCALAR|G_EVAL|G_KEEPERR);
477 sv_setsv(error,*PL_stack_sp--);
483 if (SvPOK(error) && SvCUR(error))
484 sv_catpv(error, "\t...propagated");
486 tmps = SvPV(tmpsv, len);
490 tmpsv = sv_2mortal(newSVpvn("Died", 4));
492 DIE(aTHX_ "%"SVf, tmpsv);
515 DIE(aTHX_ PL_no_usym, "filehandle");
520 DIE(aTHX_ PL_no_usym, "filehandle");
522 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
524 if (mg = SvTIED_mg((SV*)gv, 'q')) {
526 XPUSHs(SvTIED_obj((SV*)gv, mg));
532 call_method("OPEN", G_SCALAR);
538 tmps = SvPV(sv, len);
539 if (do_open9(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, name, have_name))
540 PUSHi( (I32)PL_forkprocess );
541 else if (PL_forkprocess == 0) /* we are a new child */
559 if (mg = SvTIED_mg((SV*)gv, 'q')) {
561 XPUSHs(SvTIED_obj((SV*)gv, mg));
564 call_method("CLOSE", G_SCALAR);
570 PUSHs(boolSV(do_close(gv, TRUE)));
590 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
591 DIE(aTHX_ PL_no_usym, "filehandle");
596 do_close(rgv, FALSE);
598 do_close(wgv, FALSE);
600 if (PerlProc_pipe(fd) < 0)
603 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
604 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
605 IoIFP(wstio) = IoOFP(wstio);
609 if (!IoIFP(rstio) || !IoOFP(wstio)) {
610 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
611 else PerlLIO_close(fd[0]);
612 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
613 else PerlLIO_close(fd[1]);
616 #if defined(HAS_FCNTL) && defined(F_SETFD)
617 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
618 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
625 DIE(aTHX_ PL_no_func, "pipe");
641 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
643 XPUSHs(SvTIED_obj((SV*)gv, mg));
646 call_method("FILENO", G_SCALAR);
652 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io)))
654 PUSHi(PerlIO_fileno(fp));
665 anum = PerlLIO_umask(0);
666 (void)PerlLIO_umask(anum);
669 anum = PerlLIO_umask(POPi);
670 TAINT_PROPER("umask");
673 /* Only DIE if trying to restrict permissions on `user' (self).
674 * Otherwise it's harmless and more useful to just return undef
675 * since 'group' and 'other' concepts probably don't exist here. */
676 if (MAXARG >= 1 && (POPi & 0700))
677 DIE(aTHX_ "umask not implemented");
678 XPUSHs(&PL_sv_undef);
696 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
698 XPUSHs(SvTIED_obj((SV*)gv, mg));
701 call_method("BINMODE", G_SCALAR);
708 if (!(io = GvIO(gv)) || !(fp = IoIFP(io)))
711 if (do_binmode(fp,IoTYPE(io),TRUE))
726 I32 markoff = MARK - PL_stack_base;
733 switch(SvTYPE(varsv)) {
735 methname = "TIEHASH";
738 methname = "TIEARRAY";
741 methname = "TIEHANDLE";
745 methname = "TIESCALAR";
750 if (sv_isobject(*MARK)) {
752 PUSHSTACKi(PERLSI_MAGIC);
758 call_method(methname, G_SCALAR);
761 /* Not clear why we don't call call_method here too.
762 * perhaps to get different error message ?
764 stash = gv_stashsv(*MARK, FALSE);
765 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
766 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"",
767 methname, SvPV(*MARK,n_a));
770 PUSHSTACKi(PERLSI_MAGIC);
776 call_sv((SV*)GvCV(gv), G_SCALAR);
782 if (sv_isobject(sv)) {
783 sv_unmagic(varsv, how);
784 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
787 SP = PL_stack_base + markoff;
796 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
798 if (ckWARN(WARN_UNTIE)) {
800 if (mg = SvTIED_mg(sv, how)) {
801 if (mg && SvREFCNT(SvRV(mg->mg_obj)) > 1)
802 Perl_warner(aTHX_ WARN_UNTIE,
803 "untie attempted while %"UVuf" inner references still exist",
804 (UV)SvREFCNT(SvRV(mg->mg_obj)) - 1 ) ;
816 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
819 if (mg = SvTIED_mg(sv, how)) {
820 SV *osv = SvTIED_obj(sv, mg);
821 if (osv == mg->mg_obj)
822 osv = sv_mortalcopy(osv);
840 sv = sv_mortalcopy(&PL_sv_no);
841 sv_setpv(sv, "AnyDBM_File");
842 stash = gv_stashsv(sv, FALSE);
843 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
845 require_pv("AnyDBM_File.pm");
847 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
848 DIE(aTHX_ "No dbm on this machine");
858 PUSHs(sv_2mortal(newSViv(O_RDWR|O_CREAT)));
860 PUSHs(sv_2mortal(newSViv(O_RDWR)));
863 call_sv((SV*)GvCV(gv), G_SCALAR);
866 if (!sv_isobject(TOPs)) {
871 PUSHs(sv_2mortal(newSViv(O_RDONLY)));
874 call_sv((SV*)GvCV(gv), G_SCALAR);
878 if (sv_isobject(TOPs)) {
879 sv_unmagic((SV *) hv, 'P');
880 sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
902 struct timeval timebuf;
903 struct timeval *tbuf = &timebuf;
907 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
912 # if BYTEORDER & 0xf0000
913 # define ORDERBYTE (0x88888888 - BYTEORDER)
915 # define ORDERBYTE (0x4444 - BYTEORDER)
921 for (i = 1; i <= 3; i++) {
929 /* little endians can use vecs directly */
930 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
931 # if SELECT_MIN_BITS > 1
932 /* If SELECT_MIN_BITS is greater than one we most probably will want
933 * to align the sizes with SELECT_MIN_BITS/8 because for example
934 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
935 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
936 * on (sets/tests/clears bits) is 32 bits. */
937 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
939 growsize = sizeof(fd_set);
948 masksize = NFDBITS / NBBY;
950 masksize = sizeof(long); /* documented int, everyone seems to use long */
952 growsize = maxlen + (masksize - (maxlen % masksize));
953 Zero(&fd_sets[0], 4, char*);
961 timebuf.tv_sec = (long)value;
962 value -= (NV)timebuf.tv_sec;
963 timebuf.tv_usec = (long)(value * 1000000.0);
966 tbuf = Null(struct timeval*);
968 for (i = 1; i <= 3; i++) {
975 SvPV_force(sv,n_a); /* force string conversion */
978 Sv_Grow(sv, growsize);
982 while (++j <= growsize) {
986 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
988 New(403, fd_sets[i], growsize, char);
989 for (offset = 0; offset < growsize; offset += masksize) {
990 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
991 fd_sets[i][j+offset] = s[(k % masksize) + offset];
994 fd_sets[i] = SvPVX(sv);
998 nfound = PerlSock_select(
1000 (Select_fd_set_t) fd_sets[1],
1001 (Select_fd_set_t) fd_sets[2],
1002 (Select_fd_set_t) fd_sets[3],
1004 for (i = 1; i <= 3; i++) {
1007 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1009 for (offset = 0; offset < growsize; offset += masksize) {
1010 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1011 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1013 Safefree(fd_sets[i]);
1020 if (GIMME == G_ARRAY && tbuf) {
1021 value = (NV)(timebuf.tv_sec) +
1022 (NV)(timebuf.tv_usec) / 1000000.0;
1023 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1024 sv_setnv(sv, value);
1028 DIE(aTHX_ "select not implemented");
1033 Perl_setdefout(pTHX_ GV *gv)
1037 (void)SvREFCNT_inc(gv);
1039 SvREFCNT_dec(PL_defoutgv);
1046 GV *newdefout, *egv;
1049 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1051 egv = GvEGV(PL_defoutgv);
1056 XPUSHs(&PL_sv_undef);
1058 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1059 if (gvp && *gvp == egv) {
1060 gv_efullname3(TARG, PL_defoutgv, Nullch);
1064 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1069 if (!GvIO(newdefout))
1070 gv_IOadd(newdefout);
1071 setdefout(newdefout);
1088 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1089 I32 gimme = GIMME_V;
1091 XPUSHs(SvTIED_obj((SV*)gv, mg));
1094 call_method("GETC", gimme);
1097 if (gimme == G_SCALAR)
1098 SvSetMagicSV_nosteal(TARG, TOPs);
1101 if (!gv || do_eof(gv)) /* make sure we have fp with something */
1104 sv_setpv(TARG, " ");
1105 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1112 return pp_sysread();
1116 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1119 register PERL_CONTEXT *cx;
1120 I32 gimme = GIMME_V;
1121 AV* padlist = CvPADLIST(cv);
1122 SV** svp = AvARRAY(padlist);
1128 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1130 SAVEVPTR(PL_curpad);
1131 PL_curpad = AvARRAY((AV*)svp[1]);
1133 setdefout(gv); /* locally select filehandle so $% et al work */
1165 SV *tmpsv = sv_newmortal();
1166 gv_efullname3(tmpsv, fgv, Nullch);
1167 DIE(aTHX_ "Undefined format \"%s\" called",SvPVX(tmpsv));
1169 DIE(aTHX_ "Not a format reference");
1172 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1174 IoFLAGS(io) &= ~IOf_DIDTOP;
1175 return doform(cv,gv,PL_op->op_next);
1181 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1182 register IO *io = GvIOp(gv);
1183 PerlIO *ofp = IoOFP(io);
1187 register PERL_CONTEXT *cx;
1189 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1190 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1191 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1192 PL_formtarget != PL_toptarget)
1196 if (!IoTOP_GV(io)) {
1200 if (!IoTOP_NAME(io)) {
1201 if (!IoFMT_NAME(io))
1202 IoFMT_NAME(io) = savepv(GvNAME(gv));
1203 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1204 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1205 if ((topgv && GvFORM(topgv)) ||
1206 !gv_fetchpv("top",FALSE,SVt_PVFM))
1207 IoTOP_NAME(io) = savepv(SvPVX(topname));
1209 IoTOP_NAME(io) = savepv("top");
1211 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1212 if (!topgv || !GvFORM(topgv)) {
1213 IoLINES_LEFT(io) = 100000000;
1216 IoTOP_GV(io) = topgv;
1218 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1219 I32 lines = IoLINES_LEFT(io);
1220 char *s = SvPVX(PL_formtarget);
1221 if (lines <= 0) /* Yow, header didn't even fit!!! */
1223 while (lines-- > 0) {
1224 s = strchr(s, '\n');
1230 PerlIO_write(ofp, SvPVX(PL_formtarget), s - SvPVX(PL_formtarget));
1231 sv_chop(PL_formtarget, s);
1232 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1235 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1236 PerlIO_write(ofp, SvPVX(PL_formfeed), SvCUR(PL_formfeed));
1237 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1239 PL_formtarget = PL_toptarget;
1240 IoFLAGS(io) |= IOf_DIDTOP;
1243 DIE(aTHX_ "bad top format reference");
1246 SV *tmpsv = sv_newmortal();
1247 gv_efullname3(tmpsv, fgv, Nullch);
1248 DIE(aTHX_ "Undefined top format \"%s\" called",SvPVX(tmpsv));
1251 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1252 return doform(cv,gv,PL_op);
1256 POPBLOCK(cx,PL_curpm);
1262 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1264 SV* sv = sv_newmortal();
1265 gv_efullname3(sv, gv, Nullch);
1266 Perl_warner(aTHX_ WARN_IO,
1267 "Filehandle %s opened only for input",
1270 else if (ckWARN(WARN_CLOSED))
1271 report_closed_fh(gv, io, "write", "filehandle");
1276 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1277 if (ckWARN(WARN_IO))
1278 Perl_warner(aTHX_ WARN_IO, "page overflow");
1280 if (!PerlIO_write(ofp, SvPVX(PL_formtarget), SvCUR(PL_formtarget)) ||
1284 FmLINES(PL_formtarget) = 0;
1285 SvCUR_set(PL_formtarget, 0);
1286 *SvEND(PL_formtarget) = '\0';
1287 if (IoFLAGS(io) & IOf_FLUSH)
1288 (void)PerlIO_flush(fp);
1292 PL_formtarget = PL_bodytarget;
1294 return pop_return();
1299 djSP; dMARK; dORIGMARK;
1307 if (PL_op->op_flags & OPf_STACKED)
1312 if (mg = SvTIED_mg((SV*)gv, 'q')) {
1313 if (MARK == ORIGMARK) {
1316 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1320 *MARK = SvTIED_obj((SV*)gv, mg);
1323 call_method("PRINTF", G_SCALAR);
1326 MARK = ORIGMARK + 1;
1333 if (!(io = GvIO(gv))) {
1334 if (ckWARN(WARN_UNOPENED)) {
1335 gv_efullname3(sv, gv, Nullch);
1336 Perl_warner(aTHX_ WARN_UNOPENED,
1337 "Filehandle %s never opened", SvPV(sv,n_a));
1339 SETERRNO(EBADF,RMS$_IFI);
1342 else if (!(fp = IoOFP(io))) {
1343 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1345 gv_efullname3(sv, gv, Nullch);
1346 Perl_warner(aTHX_ WARN_IO,
1347 "Filehandle %s opened only for input",
1350 else if (ckWARN(WARN_CLOSED))
1351 report_closed_fh(gv, io, "printf", "filehandle");
1353 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
1357 do_sprintf(sv, SP - MARK, MARK + 1);
1358 if (!do_print(sv, fp))
1361 if (IoFLAGS(io) & IOf_FLUSH)
1362 if (PerlIO_flush(fp) == EOF)
1373 PUSHs(&PL_sv_undef);
1394 /* Need TIEHANDLE method ? */
1396 tmps = SvPV(sv, len);
1397 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1398 IoLINES(GvIOp(gv)) = 0;
1402 PUSHs(&PL_sv_undef);
1409 djSP; dMARK; dORIGMARK; dTARGET;
1415 Sock_size_t bufsize;
1421 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) &&
1422 (mg = SvTIED_mg((SV*)gv, 'q')))
1427 *MARK = SvTIED_obj((SV*)gv, mg);
1429 call_method("READ", G_SCALAR);
1442 sv_setpvn(bufsv, "", 0);
1443 buffer = SvPV_force(bufsv, blen);
1444 length = SvIVx(*++MARK);
1446 DIE(aTHX_ "Negative length");
1449 offset = SvIVx(*++MARK);
1453 if (!io || !IoIFP(io))
1456 if (PL_op->op_type == OP_RECV) {
1457 char namebuf[MAXPATHLEN];
1458 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE)
1459 bufsize = sizeof (struct sockaddr_in);
1461 bufsize = sizeof namebuf;
1463 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1467 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1471 buffer = SvGROW(bufsv, length+1);
1472 /* 'offset' means 'flags' here */
1473 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1474 (struct sockaddr *)namebuf, &bufsize);
1477 SvCUR_set(bufsv, length);
1478 *SvEND(bufsv) = '\0';
1479 (void)SvPOK_only(bufsv);
1481 /* This should not be marked tainted if the fp is marked clean */
1482 if (!(IoFLAGS(io) & IOf_UNTAINT))
1483 SvTAINTED_on(bufsv);
1485 sv_setpvn(TARG, namebuf, bufsize);
1490 if (PL_op->op_type == OP_RECV)
1491 DIE(aTHX_ PL_no_sock_func, "recv");
1495 DIE(aTHX_ "Offset outside string");
1498 bufsize = SvCUR(bufsv);
1499 buffer = SvGROW(bufsv, length+offset+1);
1500 if (offset > bufsize) { /* Zero any newly allocated space */
1501 Zero(buffer+bufsize, offset-bufsize, char);
1503 if (PL_op->op_type == OP_SYSREAD) {
1504 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1505 if (IoTYPE(io) == 's') {
1506 length = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1507 buffer+offset, length, 0);
1512 length = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1513 buffer+offset, length);
1517 #ifdef HAS_SOCKET__bad_code_maybe
1518 if (IoTYPE(io) == 's') {
1519 char namebuf[MAXPATHLEN];
1520 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1521 bufsize = sizeof (struct sockaddr_in);
1523 bufsize = sizeof namebuf;
1525 length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer+offset, length, 0,
1526 (struct sockaddr *)namebuf, &bufsize);
1531 length = PerlIO_read(IoIFP(io), buffer+offset, length);
1532 /* fread() returns 0 on both error and EOF */
1533 if (length == 0 && PerlIO_error(IoIFP(io)))
1537 if ((IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
1538 || IoIFP(io) == PerlIO_stderr()) && ckWARN(WARN_IO))
1540 SV* sv = sv_newmortal();
1541 gv_efullname3(sv, gv, Nullch);
1542 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
1547 SvCUR_set(bufsv, length+offset);
1548 *SvEND(bufsv) = '\0';
1549 (void)SvPOK_only(bufsv);
1551 /* This should not be marked tainted if the fp is marked clean */
1552 if (!(IoFLAGS(io) & IOf_UNTAINT))
1553 SvTAINTED_on(bufsv);
1566 int items = (SP - PL_stack_base) - TOPMARK;
1570 sv = sv_2mortal(newSViv(sv_len(*SP)));
1579 djSP; dMARK; dORIGMARK; dTARGET;
1590 if (PL_op->op_type == OP_SYSWRITE && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1594 *MARK = SvTIED_obj((SV*)gv, mg);
1596 call_method("WRITE", G_SCALAR);
1607 buffer = SvPV(bufsv, blen);
1608 #if Off_t_SIZE > IVSIZE
1609 length = SvNVx(*++MARK);
1611 length = SvIVx(*++MARK);
1614 DIE(aTHX_ "Negative length");
1617 if (!io || !IoIFP(io)) {
1619 if (ckWARN(WARN_CLOSED)) {
1620 if (PL_op->op_type == OP_SYSWRITE)
1621 report_closed_fh(gv, io, "syswrite", "filehandle");
1623 report_closed_fh(gv, io, "send", "socket");
1626 else if (PL_op->op_type == OP_SYSWRITE) {
1628 #if Off_t_SIZE > IVSIZE
1629 offset = SvNVx(*++MARK);
1631 offset = SvIVx(*++MARK);
1635 DIE(aTHX_ "Offset outside string");
1637 } else if (offset >= blen && blen > 0)
1638 DIE(aTHX_ "Offset outside string");
1641 if (length > blen - offset)
1642 length = blen - offset;
1643 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1644 if (IoTYPE(io) == 's') {
1645 length = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1646 buffer+offset, length, 0);
1651 /* See the note at doio.c:do_print about filesize limits. --jhi */
1652 length = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1653 buffer+offset, length);
1657 else if (SP > MARK) {
1660 sockbuf = SvPVx(*++MARK, mlen);
1661 length = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen, length,
1662 (struct sockaddr *)sockbuf, mlen);
1665 length = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1669 DIE(aTHX_ PL_no_sock_func, "send");
1684 return pp_sysread();
1694 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1696 gv = PL_last_in_gv = PL_argvgv;
1698 if (io && !IoIFP(io)) {
1699 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1701 IoFLAGS(io) &= ~IOf_START;
1702 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1703 sv_setpvn(GvSV(gv), "-", 1);
1704 SvSETMAGIC(GvSV(gv));
1706 else if (!nextargv(gv))
1711 gv = PL_last_in_gv; /* eof */
1714 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1716 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1718 XPUSHs(SvTIED_obj((SV*)gv, mg));
1721 call_method("EOF", G_SCALAR);
1727 PUSHs(boolSV(!gv || do_eof(gv)));
1740 gv = PL_last_in_gv = (GV*)POPs;
1742 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1744 XPUSHs(SvTIED_obj((SV*)gv, mg));
1747 call_method("TELL", G_SCALAR);
1753 #if LSEEKSIZE > IVSIZE
1754 PUSHn( do_tell(gv) );
1756 PUSHi( do_tell(gv) );
1763 return pp_sysseek();
1771 #if LSEEKSIZE > IVSIZE
1772 Off_t offset = (Off_t)SvNVx(POPs);
1774 Off_t offset = (Off_t)SvIVx(POPs);
1778 gv = PL_last_in_gv = (GV*)POPs;
1780 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1782 XPUSHs(SvTIED_obj((SV*)gv, mg));
1783 #if LSEEKSIZE > IVSIZE
1784 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1786 XPUSHs(sv_2mortal(newSViv((IV) offset)));
1788 XPUSHs(sv_2mortal(newSViv((IV) whence)));
1791 call_method("SEEK", G_SCALAR);
1797 if (PL_op->op_type == OP_SEEK)
1798 PUSHs(boolSV(do_seek(gv, offset, whence)));
1800 Off_t n = do_sysseek(gv, offset, whence);
1802 PUSHs(&PL_sv_undef);
1805 #if LSEEKSIZE > IVSIZE
1810 : newSVpvn(zero_but_true, ZBTLEN);
1811 PUSHs(sv_2mortal(sv));
1820 Off_t len = (Off_t)POPn;
1826 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
1827 if (PL_op->op_flags & OPf_SPECIAL) {
1828 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
1830 TAINT_PROPER("truncate");
1831 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)))
1834 PerlIO_flush(IoIFP(GvIOp(tmpgv)));
1836 if (ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1838 if (my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1848 if (SvTYPE(sv) == SVt_PVGV) {
1849 tmpgv = (GV*)sv; /* *main::FRED for example */
1852 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1853 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
1857 name = SvPV(sv, n_a);
1858 TAINT_PROPER("truncate");
1860 if (truncate(name, len) < 0)
1865 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
1868 if (my_chsize(tmpfd, len) < 0)
1870 PerlLIO_close(tmpfd);
1879 SETERRNO(EBADF,RMS$_IFI);
1882 DIE(aTHX_ "truncate not implemented");
1895 unsigned int func = U_I(POPn);
1896 int optype = PL_op->op_type;
1902 if (!io || !argsv || !IoIFP(io)) {
1903 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
1907 if (SvPOK(argsv) || !SvNIOK(argsv)) {
1910 s = SvPV_force(argsv, len);
1911 need = IOCPARM_LEN(func);
1913 s = Sv_Grow(argsv, need + 1);
1914 SvCUR_set(argsv, need);
1917 s[SvCUR(argsv)] = 17; /* a little sanity check here */
1920 retval = SvIV(argsv);
1921 s = INT2PTR(char*,retval); /* ouch */
1924 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1926 if (optype == OP_IOCTL)
1928 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
1930 DIE(aTHX_ "ioctl is not implemented");
1934 #if defined(OS2) && defined(__EMX__)
1935 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
1937 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
1940 DIE(aTHX_ "fcntl is not implemented");
1944 if (s[SvCUR(argsv)] != 17)
1945 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
1946 PL_op_name[optype]);
1947 s[SvCUR(argsv)] = 0; /* put our null back */
1948 SvSETMAGIC(argsv); /* Assume it has changed */
1957 PUSHp(zero_but_true, ZBTLEN);
1977 fp = IoIFP(GvIOp(gv));
1981 (void)PerlIO_flush(fp);
1982 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
1986 SETERRNO(EBADF,RMS$_IFI);
1987 if (ckWARN(WARN_CLOSED))
1988 report_closed_fh(gv, GvIO(gv), "flock", "filehandle");
1993 DIE(aTHX_ PL_no_func, "flock()");
2005 int protocol = POPi;
2013 SETERRNO(EBADF,LIB$_INVARG);
2019 do_close(gv, FALSE);
2021 TAINT_PROPER("socket");
2022 fd = PerlSock_socket(domain, type, protocol);
2025 IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */
2026 IoOFP(io) = PerlIO_fdopen(fd, "w");
2028 if (!IoIFP(io) || !IoOFP(io)) {
2029 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2030 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2031 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2034 #if defined(HAS_FCNTL) && defined(F_SETFD)
2035 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
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]);
2087 #if defined(HAS_FCNTL) && defined(F_SETFD)
2088 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2089 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2094 DIE(aTHX_ PL_no_sock_func, "socketpair");
2102 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2103 extern GETPRIVMODE();
2104 extern GETUSERMODE();
2109 register IO *io = GvIOn(gv);
2113 int mpeprivmode = 0;
2116 if (!io || !IoIFP(io))
2119 addr = SvPV(addrsv, len);
2120 TAINT_PROPER("bind");
2121 #ifdef MPE /* Deal with MPE bind() peculiarities */
2122 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2123 /* The address *MUST* stupidly be zero. */
2124 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2125 /* PRIV mode is required to bind() to ports < 1024. */
2126 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2127 ((struct sockaddr_in *)addr)->sin_port > 0) {
2128 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2133 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2134 (struct sockaddr *)addr, len) >= 0)
2137 #ifdef MPE /* Switch back to USER mode */
2148 if (ckWARN(WARN_CLOSED))
2149 report_closed_fh(gv, io, "bind", "socket");
2150 SETERRNO(EBADF,SS$_IVCHAN);
2153 DIE(aTHX_ PL_no_sock_func, "bind");
2164 register IO *io = GvIOn(gv);
2167 if (!io || !IoIFP(io))
2170 addr = SvPV(addrsv, len);
2171 TAINT_PROPER("connect");
2172 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2178 if (ckWARN(WARN_CLOSED))
2179 report_closed_fh(gv, io, "connect", "socket");
2180 SETERRNO(EBADF,SS$_IVCHAN);
2183 DIE(aTHX_ PL_no_sock_func, "connect");
2193 register IO *io = GvIOn(gv);
2195 if (!io || !IoIFP(io))
2198 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2204 if (ckWARN(WARN_CLOSED))
2205 report_closed_fh(gv, io, "listen", "socket");
2206 SETERRNO(EBADF,SS$_IVCHAN);
2209 DIE(aTHX_ PL_no_sock_func, "listen");
2221 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2222 Sock_size_t len = sizeof saddr;
2234 if (!gstio || !IoIFP(gstio))
2239 do_close(ngv, FALSE);
2241 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2244 IoIFP(nstio) = PerlIO_fdopen(fd, "r");
2245 IoOFP(nstio) = PerlIO_fdopen(fd, "w");
2246 IoTYPE(nstio) = 's';
2247 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2248 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2249 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2250 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2253 #if defined(HAS_FCNTL) && defined(F_SETFD)
2254 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2257 PUSHp((char *)&saddr, len);
2261 if (ckWARN(WARN_CLOSED))
2262 report_closed_fh(ggv, ggv ? GvIO(ggv) : 0, "accept", "socket");
2263 SETERRNO(EBADF,SS$_IVCHAN);
2269 DIE(aTHX_ PL_no_sock_func, "accept");
2279 register IO *io = GvIOn(gv);
2281 if (!io || !IoIFP(io))
2284 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2288 if (ckWARN(WARN_CLOSED))
2289 report_closed_fh(gv, io, "shutdown", "socket");
2290 SETERRNO(EBADF,SS$_IVCHAN);
2293 DIE(aTHX_ PL_no_sock_func, "shutdown");
2300 return pp_ssockopt();
2302 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2310 int optype = PL_op->op_type;
2313 unsigned int optname;
2319 if (optype == OP_GSOCKOPT)
2320 sv = sv_2mortal(NEWSV(22, 257));
2323 optname = (unsigned int) POPi;
2324 lvl = (unsigned int) POPi;
2328 if (!io || !IoIFP(io))
2331 fd = PerlIO_fileno(IoIFP(io));
2335 (void)SvPOK_only(sv);
2339 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2354 aint = (int)SvIV(sv);
2358 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2367 if (ckWARN(WARN_CLOSED))
2368 report_closed_fh(gv, io,
2369 optype == OP_GSOCKOPT ? "getsockopt" : "setsockopt",
2371 SETERRNO(EBADF,SS$_IVCHAN);
2376 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2383 return pp_getpeername();
2385 DIE(aTHX_ PL_no_sock_func, "getsockname");
2393 int optype = PL_op->op_type;
2397 register IO *io = GvIOn(gv);
2400 if (!io || !IoIFP(io))
2403 sv = sv_2mortal(NEWSV(22, 257));
2404 (void)SvPOK_only(sv);
2408 fd = PerlIO_fileno(IoIFP(io));
2410 case OP_GETSOCKNAME:
2411 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2414 case OP_GETPEERNAME:
2415 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2417 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2419 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";
2420 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2421 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2422 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2423 sizeof(u_short) + sizeof(struct in_addr))) {
2430 #ifdef BOGUS_GETNAME_RETURN
2431 /* Interactive Unix, getpeername() and getsockname()
2432 does not return valid namelen */
2433 if (len == BOGUS_GETNAME_RETURN)
2434 len = sizeof(struct sockaddr);
2442 if (ckWARN(WARN_CLOSED))
2443 report_closed_fh(gv, io,
2444 optype == OP_GETSOCKNAME ? "getsockname"
2447 SETERRNO(EBADF,SS$_IVCHAN);
2452 DIE(aTHX_ PL_no_sock_func, "getpeername");
2471 if (PL_op->op_flags & OPf_REF) {
2474 if (tmpgv != PL_defgv) {
2475 PL_laststype = OP_STAT;
2477 sv_setpv(PL_statname, "");
2478 PL_laststatval = (GvIO(tmpgv) && IoIFP(GvIOp(tmpgv))
2479 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &PL_statcache) : -1);
2481 if (PL_laststatval < 0)
2486 if (SvTYPE(sv) == SVt_PVGV) {
2490 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2491 tmpgv = (GV*)SvRV(sv);
2494 sv_setpv(PL_statname, SvPV(sv,n_a));
2497 PL_laststype = PL_op->op_type;
2498 if (PL_op->op_type == OP_LSTAT)
2499 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2502 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2503 if (PL_laststatval < 0) {
2504 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2505 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
2511 if (gimme != G_ARRAY) {
2512 if (gimme != G_VOID)
2513 XPUSHs(boolSV(max));
2519 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2520 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2521 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mode)));
2522 PUSHs(sv_2mortal(newSViv(PL_statcache.st_nlink)));
2523 #if Uid_t_size > IVSIZE
2524 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2526 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2528 #if Gid_t_size > IVSIZE
2529 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2531 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2533 #ifdef USE_STAT_RDEV
2534 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2536 PUSHs(sv_2mortal(newSVpvn("", 0)));
2538 #if Off_t_size > IVSIZE
2539 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size)));
2541 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2544 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2545 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2546 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2548 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2549 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2550 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2552 #ifdef USE_STAT_BLOCKS
2553 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blksize)));
2554 PUSHs(sv_2mortal(newSViv(PL_statcache.st_blocks)));
2556 PUSHs(sv_2mortal(newSVpvn("", 0)));
2557 PUSHs(sv_2mortal(newSVpvn("", 0)));
2567 #if defined(HAS_ACCESS) && defined(R_OK)
2569 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2570 result = access(TOPpx, R_OK);
2585 if (cando(S_IRUSR, 0, &PL_statcache))
2594 #if defined(HAS_ACCESS) && defined(W_OK)
2596 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2597 result = access(TOPpx, W_OK);
2612 if (cando(S_IWUSR, 0, &PL_statcache))
2621 #if defined(HAS_ACCESS) && defined(X_OK)
2623 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2624 result = access(TOPpx, X_OK);
2639 if (cando(S_IXUSR, 0, &PL_statcache))
2648 #ifdef PERL_EFF_ACCESS_R_OK
2650 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2651 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2666 if (cando(S_IRUSR, 1, &PL_statcache))
2675 #ifdef PERL_EFF_ACCESS_W_OK
2677 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2678 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2693 if (cando(S_IWUSR, 1, &PL_statcache))
2702 #ifdef PERL_EFF_ACCESS_X_OK
2704 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2705 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2720 if (cando(S_IXUSR, 1, &PL_statcache))
2727 I32 result = my_stat();
2736 return pp_ftrowned();
2741 I32 result = my_stat();
2745 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
2753 I32 result = my_stat();
2757 if (PL_statcache.st_size == 0)
2764 I32 result = my_stat();
2768 #if Off_t_size > IVSIZE
2769 PUSHn(PL_statcache.st_size);
2771 PUSHi(PL_statcache.st_size);
2778 I32 result = my_stat();
2782 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
2788 I32 result = my_stat();
2792 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
2798 I32 result = my_stat();
2802 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
2808 I32 result = my_stat();
2812 if (S_ISSOCK(PL_statcache.st_mode))
2819 I32 result = my_stat();
2823 if (S_ISCHR(PL_statcache.st_mode))
2830 I32 result = my_stat();
2834 if (S_ISBLK(PL_statcache.st_mode))
2841 I32 result = my_stat();
2845 if (S_ISREG(PL_statcache.st_mode))
2852 I32 result = my_stat();
2856 if (S_ISDIR(PL_statcache.st_mode))
2863 I32 result = my_stat();
2867 if (S_ISFIFO(PL_statcache.st_mode))
2874 I32 result = my_lstat();
2878 if (S_ISLNK(PL_statcache.st_mode))
2887 I32 result = my_stat();
2891 if (PL_statcache.st_mode & S_ISUID)
2901 I32 result = my_stat();
2905 if (PL_statcache.st_mode & S_ISGID)
2915 I32 result = my_stat();
2919 if (PL_statcache.st_mode & S_ISVTX)
2930 char *tmps = Nullch;
2933 if (PL_op->op_flags & OPf_REF)
2935 else if (isGV(TOPs))
2937 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2938 gv = (GV*)SvRV(POPs);
2940 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
2942 if (GvIO(gv) && IoIFP(GvIOp(gv)))
2943 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
2944 else if (tmps && isDIGIT(*tmps))
2948 if (PerlLIO_isatty(fd))
2953 #if defined(atarist) /* this will work with atariST. Configure will
2954 make guesses for other systems. */
2955 # define FILE_base(f) ((f)->_base)
2956 # define FILE_ptr(f) ((f)->_ptr)
2957 # define FILE_cnt(f) ((f)->_cnt)
2958 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
2968 register STDCHAR *s;
2975 if (PL_op->op_flags & OPf_REF)
2977 else if (isGV(TOPs))
2979 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
2980 gv = (GV*)SvRV(POPs);
2986 if (gv == PL_defgv) {
2988 io = GvIO(PL_statgv);
2991 goto really_filename;
2996 PL_laststatval = -1;
2997 sv_setpv(PL_statname, "");
2998 io = GvIO(PL_statgv);
3000 if (io && IoIFP(io)) {
3001 if (! PerlIO_has_base(IoIFP(io)))
3002 DIE(aTHX_ "-T and -B not implemented on filehandles");
3003 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3004 if (PL_laststatval < 0)
3006 if (S_ISDIR(PL_statcache.st_mode)) /* handle NFS glitch */
3007 if (PL_op->op_type == OP_FTTEXT)
3011 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3012 i = PerlIO_getc(IoIFP(io));
3014 (void)PerlIO_ungetc(IoIFP(io),i);
3016 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3018 len = PerlIO_get_bufsiz(IoIFP(io));
3019 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3020 /* sfio can have large buffers - limit to 512 */
3025 if (ckWARN(WARN_UNOPENED)) {
3027 Perl_warner(aTHX_ WARN_UNOPENED, "Test on unopened file <%s>",
3030 SETERRNO(EBADF,RMS$_IFI);
3038 PL_laststatval = -1;
3039 sv_setpv(PL_statname, SvPV(sv, n_a));
3040 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3041 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3042 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
3045 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3046 if (PL_laststatval < 0) {
3047 (void)PerlIO_close(fp);
3050 do_binmode(fp, '<', TRUE);
3051 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3052 (void)PerlIO_close(fp);
3054 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3055 RETPUSHNO; /* special case NFS directories */
3056 RETPUSHYES; /* null file is anything */
3061 /* now scan s to look for textiness */
3062 /* XXX ASCII dependent code */
3064 #if defined(DOSISH) || defined(USEMYBINMODE)
3065 /* ignore trailing ^Z on short files */
3066 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3070 for (i = 0; i < len; i++, s++) {
3071 if (!*s) { /* null never allowed in text */
3076 else if (!(isPRINT(*s) || isSPACE(*s)))
3079 else if (*s & 128) {
3081 if (!(PL_op->op_private & OPpLOCALE) || !isALPHA_LC(*s))
3086 *s != '\n' && *s != '\r' && *s != '\b' &&
3087 *s != '\t' && *s != '\f' && *s != 27)
3092 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3116 if (!tmps || !*tmps) {
3117 svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE);
3119 tmps = SvPV(*svp, n_a);
3121 if (!tmps || !*tmps) {
3122 svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE);
3124 tmps = SvPV(*svp, n_a);
3127 if (!tmps || !*tmps) {
3128 svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE);
3130 tmps = SvPV(*svp, n_a);
3133 TAINT_PROPER("chdir");
3134 PUSHi( PerlDir_chdir(tmps) >= 0 );
3136 /* Clear the DEFAULT element of ENV so we'll get the new value
3138 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3145 djSP; dMARK; dTARGET;
3148 value = (I32)apply(PL_op->op_type, MARK, SP);
3153 DIE(aTHX_ PL_no_func, "Unsupported function chown");
3164 TAINT_PROPER("chroot");
3165 PUSHi( chroot(tmps) >= 0 );
3168 DIE(aTHX_ PL_no_func, "chroot");
3174 djSP; dMARK; dTARGET;
3176 value = (I32)apply(PL_op->op_type, MARK, SP);
3184 djSP; dMARK; dTARGET;
3186 value = (I32)apply(PL_op->op_type, MARK, SP);
3194 djSP; dMARK; dTARGET;
3196 value = (I32)apply(PL_op->op_type, MARK, SP);
3208 char *tmps2 = POPpx;
3209 char *tmps = SvPV(TOPs, n_a);
3210 TAINT_PROPER("rename");
3212 anum = PerlLIO_rename(tmps, tmps2);
3214 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3215 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3218 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3219 (void)UNLINK(tmps2);
3220 if (!(anum = link(tmps, tmps2)))
3221 anum = UNLINK(tmps);
3234 char *tmps2 = POPpx;
3235 char *tmps = SvPV(TOPs, n_a);
3236 TAINT_PROPER("link");
3237 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3239 DIE(aTHX_ PL_no_func, "Unsupported function link");
3249 char *tmps2 = POPpx;
3250 char *tmps = SvPV(TOPs, n_a);
3251 TAINT_PROPER("symlink");
3252 SETi( symlink(tmps, tmps2) >= 0 );
3255 DIE(aTHX_ PL_no_func, "symlink");
3264 char buf[MAXPATHLEN];
3268 #ifndef INCOMPLETE_TAINTS
3272 len = readlink(tmps, buf, sizeof buf);
3280 RETSETUNDEF; /* just pretend it's a normal file */
3284 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3286 S_dooneliner(pTHX_ char *cmd, char *filename)
3288 char *save_filename = filename;
3294 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3295 strcpy(cmdline, cmd);
3296 strcat(cmdline, " ");
3297 for (s = cmdline + strlen(cmdline); *filename; ) {
3302 myfp = PerlProc_popen(cmdline, "r");
3306 SV *tmpsv = sv_newmortal();
3307 /* Need to save/restore 'PL_rs' ?? */
3308 s = sv_gets(tmpsv, myfp, 0);
3309 (void)PerlProc_pclose(myfp);
3313 #ifdef HAS_SYS_ERRLIST
3318 /* you don't see this */
3320 #ifdef HAS_SYS_ERRLIST
3328 if (instr(s, errmsg)) {
3335 #define EACCES EPERM
3337 if (instr(s, "cannot make"))
3338 SETERRNO(EEXIST,RMS$_FEX);
3339 else if (instr(s, "existing file"))
3340 SETERRNO(EEXIST,RMS$_FEX);
3341 else if (instr(s, "ile exists"))
3342 SETERRNO(EEXIST,RMS$_FEX);
3343 else if (instr(s, "non-exist"))
3344 SETERRNO(ENOENT,RMS$_FNF);
3345 else if (instr(s, "does not exist"))
3346 SETERRNO(ENOENT,RMS$_FNF);
3347 else if (instr(s, "not empty"))
3348 SETERRNO(EBUSY,SS$_DEVOFFLINE);
3349 else if (instr(s, "cannot access"))
3350 SETERRNO(EACCES,RMS$_PRV);
3352 SETERRNO(EPERM,RMS$_PRV);
3355 else { /* some mkdirs return no failure indication */
3356 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3357 if (PL_op->op_type == OP_RMDIR)
3362 SETERRNO(EACCES,RMS$_PRV); /* a guess */
3386 tmps = SvPV(TOPs, n_a);
3388 TAINT_PROPER("mkdir");
3390 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3392 SETi( dooneliner("mkdir", tmps) );
3393 oldumask = PerlLIO_umask(0);
3394 PerlLIO_umask(oldumask);
3395 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3407 TAINT_PROPER("rmdir");
3409 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3411 XPUSHi( dooneliner("rmdir", tmps) );
3416 /* Directory calls. */
3421 #if defined(Direntry_t) && defined(HAS_READDIR)
3423 char *dirname = POPpx;
3425 register IO *io = GvIOn(gv);
3431 PerlDir_close(IoDIRP(io));
3432 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3438 SETERRNO(EBADF,RMS$_DIR);
3441 DIE(aTHX_ PL_no_dir_func, "opendir");
3448 #if defined(Direntry_t) && defined(HAS_READDIR)
3450 Direntry_t *readdir (DIR *);
3452 register Direntry_t *dp;
3454 register IO *io = GvIOn(gv);
3457 if (!io || !IoDIRP(io))
3460 if (GIMME == G_ARRAY) {
3462 while (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
3469 if (!(IoFLAGS(io) & IOf_UNTAINT))
3472 XPUSHs(sv_2mortal(sv));
3476 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3479 sv = newSVpvn(dp->d_name, dp->d_namlen);
3481 sv = newSVpv(dp->d_name, 0);
3483 #ifndef INCOMPLETE_TAINTS
3484 if (!(IoFLAGS(io) & IOf_UNTAINT))
3487 XPUSHs(sv_2mortal(sv));
3493 SETERRNO(EBADF,RMS$_ISI);
3494 if (GIMME == G_ARRAY)
3499 DIE(aTHX_ PL_no_dir_func, "readdir");
3506 #if defined(HAS_TELLDIR) || defined(telldir)
3507 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3508 /* XXX netbsd still seemed to.
3509 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3510 --JHI 1999-Feb-02 */
3511 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3512 long telldir (DIR *);
3515 register IO *io = GvIOn(gv);
3517 if (!io || !IoDIRP(io))
3520 PUSHi( PerlDir_tell(IoDIRP(io)) );
3524 SETERRNO(EBADF,RMS$_ISI);
3527 DIE(aTHX_ PL_no_dir_func, "telldir");
3534 #if defined(HAS_SEEKDIR) || defined(seekdir)
3537 register IO *io = GvIOn(gv);
3539 if (!io || !IoDIRP(io))
3542 (void)PerlDir_seek(IoDIRP(io), along);
3547 SETERRNO(EBADF,RMS$_ISI);
3550 DIE(aTHX_ PL_no_dir_func, "seekdir");
3557 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3559 register IO *io = GvIOn(gv);
3561 if (!io || !IoDIRP(io))
3564 (void)PerlDir_rewind(IoDIRP(io));
3568 SETERRNO(EBADF,RMS$_ISI);
3571 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3578 #if defined(Direntry_t) && defined(HAS_READDIR)
3580 register IO *io = GvIOn(gv);
3582 if (!io || !IoDIRP(io))
3585 #ifdef VOID_CLOSEDIR
3586 PerlDir_close(IoDIRP(io));
3588 if (PerlDir_close(IoDIRP(io)) < 0) {
3589 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3598 SETERRNO(EBADF,RMS$_IFI);
3601 DIE(aTHX_ PL_no_dir_func, "closedir");
3605 /* Process control. */
3615 PERL_FLUSHALL_FOR_CHILD;
3621 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
3622 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3623 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3628 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3633 PERL_FLUSHALL_FOR_CHILD;
3634 childpid = PerlProc_fork();
3638 DIE(aTHX_ PL_no_func, "Unsupported function fork");
3645 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3650 childpid = wait4pid(-1, &argflags, 0);
3651 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3655 DIE(aTHX_ PL_no_func, "Unsupported function wait");
3661 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3669 childpid = wait4pid(childpid, &argflags, optype);
3670 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3674 DIE(aTHX_ PL_no_func, "Unsupported function waitpid");
3680 djSP; dMARK; dORIGMARK; dTARGET;
3685 Sigsave_t ihand,qhand; /* place to save signals during system() */
3690 if (SP - MARK == 1) {
3692 char *junk = SvPV(TOPs, n_a);
3694 TAINT_PROPER("system");
3697 PERL_FLUSHALL_FOR_CHILD;
3698 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2)
3699 if (PerlProc_pipe(pp) >= 0)
3701 while ((childpid = vfork()) == -1) {
3702 if (errno != EAGAIN) {
3707 PerlLIO_close(pp[0]);
3708 PerlLIO_close(pp[1]);
3716 PerlLIO_close(pp[1]);
3717 rsignal_save(SIGINT, SIG_IGN, &ihand);
3718 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
3720 result = wait4pid(childpid, &status, 0);
3721 } while (result == -1 && errno == EINTR);
3722 (void)rsignal_restore(SIGINT, &ihand);
3723 (void)rsignal_restore(SIGQUIT, &qhand);
3724 STATUS_NATIVE_SET(result == -1 ? -1 : status);
3725 do_execfree(); /* free any memory child malloced on vfork */
3731 while (n < sizeof(int)) {
3732 n1 = PerlLIO_read(pp[0],
3733 (void*)(((char*)&errkid)+n),
3739 PerlLIO_close(pp[0]);
3740 if (n) { /* Error */
3741 if (n != sizeof(int))
3742 DIE(aTHX_ "panic: kid popen errno read");
3743 errno = errkid; /* Propagate errno from kid */
3744 STATUS_CURRENT = -1;
3747 PUSHi(STATUS_CURRENT);
3751 PerlLIO_close(pp[0]);
3752 #if defined(HAS_FCNTL) && defined(F_SETFD)
3753 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
3756 if (PL_op->op_flags & OPf_STACKED) {
3757 SV *really = *++MARK;
3758 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
3760 else if (SP - MARK != 1)
3761 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
3763 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
3766 #else /* ! FORK or VMS or OS/2 */
3767 if (PL_op->op_flags & OPf_STACKED) {
3768 SV *really = *++MARK;
3769 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
3771 else if (SP - MARK != 1)
3772 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
3774 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3776 STATUS_NATIVE_SET(value);
3779 PUSHi(STATUS_CURRENT);
3780 #endif /* !FORK or VMS */
3786 djSP; dMARK; dORIGMARK; dTARGET;
3790 PERL_FLUSHALL_FOR_CHILD;
3791 if (PL_op->op_flags & OPf_STACKED) {
3792 SV *really = *++MARK;
3793 value = (I32)do_aexec(really, MARK, SP);
3795 else if (SP - MARK != 1)
3797 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
3801 (void ) do_aspawn(Nullsv, MARK, SP);
3805 value = (I32)do_aexec(Nullsv, MARK, SP);
3810 char *junk = SvPV(*SP, n_a);
3812 TAINT_PROPER("exec");
3815 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3818 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
3821 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
3826 #if !defined(HAS_FORK) && defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3838 djSP; dMARK; dTARGET;
3841 value = (I32)apply(PL_op->op_type, MARK, SP);
3846 DIE(aTHX_ PL_no_func, "Unsupported function kill");
3854 XPUSHi( getppid() );
3857 DIE(aTHX_ PL_no_func, "getppid");
3873 pgrp = (I32)BSD_GETPGRP(pid);
3875 if (pid != 0 && pid != PerlProc_getpid())
3876 DIE(aTHX_ "POSIX getpgrp can't take an argument");
3882 DIE(aTHX_ PL_no_func, "getpgrp()");
3901 TAINT_PROPER("setpgrp");
3903 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
3905 if ((pgrp != 0 && pgrp != PerlProc_getpid())
3906 || (pid != 0 && pid != PerlProc_getpid()))
3908 DIE(aTHX_ "setpgrp can't take arguments");
3910 SETi( setpgrp() >= 0 );
3911 #endif /* USE_BSDPGRP */
3914 DIE(aTHX_ PL_no_func, "setpgrp()");
3923 #ifdef HAS_GETPRIORITY
3926 SETi( getpriority(which, who) );
3929 DIE(aTHX_ PL_no_func, "getpriority()");
3939 #ifdef HAS_SETPRIORITY
3943 TAINT_PROPER("setpriority");
3944 SETi( setpriority(which, who, niceval) >= 0 );
3947 DIE(aTHX_ PL_no_func, "setpriority()");
3957 XPUSHn( time(Null(Time_t*)) );
3959 XPUSHi( time(Null(Time_t*)) );
3964 /* XXX The POSIX name is CLK_TCK; it is to be preferred
3965 to HZ. Probably. For now, assume that if the system
3966 defines HZ, it does so correctly. (Will this break
3968 Probably we ought to use _sysconf(_SC_CLK_TCK), if
3969 it's supported. --AD 9/96.
3985 DIE(aTHX_ "times not implemented");
3990 (void)PerlProc_times(&PL_timesbuf);
3992 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
3993 /* struct tms, though same data */
3997 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
3998 if (GIMME == G_ARRAY) {
3999 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
4000 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
4001 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
4004 #endif /* HAS_TIMES */
4017 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4018 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4019 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4025 when = (Time_t)SvNVx(POPs);
4027 when = (Time_t)SvIVx(POPs);
4030 if (PL_op->op_type == OP_LOCALTIME)
4031 tmbuf = localtime(&when);
4033 tmbuf = gmtime(&when);
4037 if (GIMME != G_ARRAY) {
4042 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4043 dayname[tmbuf->tm_wday],
4044 monname[tmbuf->tm_mon],
4049 tmbuf->tm_year + 1900);
4050 PUSHs(sv_2mortal(tsv));
4053 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4054 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4055 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4056 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4057 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4058 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4059 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4060 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4061 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4072 anum = alarm((unsigned int)anum);
4079 DIE(aTHX_ PL_no_func, "Unsupported function alarm");
4090 (void)time(&lasttime);
4095 PerlProc_sleep((unsigned int)duration);
4098 XPUSHi(when - lasttime);
4102 /* Shared memory. */
4116 return pp_shmwrite();
4121 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4122 djSP; dMARK; dTARGET;
4123 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4132 /* Message passing. */
4146 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4147 djSP; dMARK; dTARGET;
4148 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4159 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4160 djSP; dMARK; dTARGET;
4161 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4174 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4175 djSP; dMARK; dTARGET;
4176 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4183 DIE(aTHX_ "System V IPC is not implemented on this machine");
4189 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4190 djSP; dMARK; dTARGET;
4191 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4199 PUSHp(zero_but_true, ZBTLEN);
4209 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4210 djSP; dMARK; dTARGET;
4211 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4220 /* Get system info. */
4224 #ifdef HAS_GETHOSTBYNAME
4225 return pp_ghostent();
4227 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4233 #ifdef HAS_GETHOSTBYADDR
4234 return pp_ghostent();
4236 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4243 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4244 I32 which = PL_op->op_type;
4245 register char **elem;
4247 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4248 struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4249 struct hostent *PerlSock_gethostbyname(Netdb_name_t);
4250 struct hostent *PerlSock_gethostent(void);
4252 struct hostent *hent;
4257 if (which == OP_GHBYNAME)
4258 #ifdef HAS_GETHOSTBYNAME
4259 hent = PerlSock_gethostbyname(POPpx);
4261 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4263 else if (which == OP_GHBYADDR) {
4264 #ifdef HAS_GETHOSTBYADDR
4265 int addrtype = POPi;
4268 Netdb_host_t addr = (Netdb_host_t) SvPV(addrsv, addrlen);
4270 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4272 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4276 #ifdef HAS_GETHOSTENT
4277 hent = PerlSock_gethostent();
4279 DIE(aTHX_ PL_no_sock_func, "gethostent");
4282 #ifdef HOST_NOT_FOUND
4284 STATUS_NATIVE_SET(h_errno);
4287 if (GIMME != G_ARRAY) {
4288 PUSHs(sv = sv_newmortal());
4290 if (which == OP_GHBYNAME) {
4292 sv_setpvn(sv, hent->h_addr, hent->h_length);
4295 sv_setpv(sv, (char*)hent->h_name);
4301 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4302 sv_setpv(sv, (char*)hent->h_name);
4303 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4304 for (elem = hent->h_aliases; elem && *elem; elem++) {
4305 sv_catpv(sv, *elem);
4307 sv_catpvn(sv, " ", 1);
4309 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4310 sv_setiv(sv, (IV)hent->h_addrtype);
4311 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4312 len = hent->h_length;
4313 sv_setiv(sv, (IV)len);
4315 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4316 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4317 sv_setpvn(sv, *elem, len);
4320 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4322 sv_setpvn(sv, hent->h_addr, len);
4327 DIE(aTHX_ PL_no_sock_func, "gethostent");
4333 #ifdef HAS_GETNETBYNAME
4334 return pp_gnetent();
4336 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4342 #ifdef HAS_GETNETBYADDR
4343 return pp_gnetent();
4345 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4352 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4353 I32 which = PL_op->op_type;
4354 register char **elem;
4356 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4357 struct netent *PerlSock_getnetbyaddr(Netdb_net_t, int);
4358 struct netent *PerlSock_getnetbyname(Netdb_name_t);
4359 struct netent *PerlSock_getnetent(void);
4361 struct netent *nent;
4364 if (which == OP_GNBYNAME)
4365 #ifdef HAS_GETNETBYNAME
4366 nent = PerlSock_getnetbyname(POPpx);
4368 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4370 else if (which == OP_GNBYADDR) {
4371 #ifdef HAS_GETNETBYADDR
4372 int addrtype = POPi;
4373 Netdb_net_t addr = (Netdb_net_t) U_L(POPn);
4374 nent = PerlSock_getnetbyaddr(addr, addrtype);
4376 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4380 #ifdef HAS_GETNETENT
4381 nent = PerlSock_getnetent();
4383 DIE(aTHX_ PL_no_sock_func, "getnetent");
4387 if (GIMME != G_ARRAY) {
4388 PUSHs(sv = sv_newmortal());
4390 if (which == OP_GNBYNAME)
4391 sv_setiv(sv, (IV)nent->n_net);
4393 sv_setpv(sv, nent->n_name);
4399 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4400 sv_setpv(sv, nent->n_name);
4401 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4402 for (elem = nent->n_aliases; elem && *elem; elem++) {
4403 sv_catpv(sv, *elem);
4405 sv_catpvn(sv, " ", 1);
4407 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4408 sv_setiv(sv, (IV)nent->n_addrtype);
4409 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4410 sv_setiv(sv, (IV)nent->n_net);
4415 DIE(aTHX_ PL_no_sock_func, "getnetent");
4421 #ifdef HAS_GETPROTOBYNAME
4422 return pp_gprotoent();
4424 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4430 #ifdef HAS_GETPROTOBYNUMBER
4431 return pp_gprotoent();
4433 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4440 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4441 I32 which = PL_op->op_type;
4442 register char **elem;
4444 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4445 struct protoent *PerlSock_getprotobyname(Netdb_name_t);
4446 struct protoent *PerlSock_getprotobynumber(int);
4447 struct protoent *PerlSock_getprotoent(void);
4449 struct protoent *pent;
4452 if (which == OP_GPBYNAME)
4453 #ifdef HAS_GETPROTOBYNAME
4454 pent = PerlSock_getprotobyname(POPpx);
4456 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4458 else if (which == OP_GPBYNUMBER)
4459 #ifdef HAS_GETPROTOBYNUMBER
4460 pent = PerlSock_getprotobynumber(POPi);
4462 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4465 #ifdef HAS_GETPROTOENT
4466 pent = PerlSock_getprotoent();
4468 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4472 if (GIMME != G_ARRAY) {
4473 PUSHs(sv = sv_newmortal());
4475 if (which == OP_GPBYNAME)
4476 sv_setiv(sv, (IV)pent->p_proto);
4478 sv_setpv(sv, pent->p_name);
4484 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4485 sv_setpv(sv, pent->p_name);
4486 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4487 for (elem = pent->p_aliases; elem && *elem; elem++) {
4488 sv_catpv(sv, *elem);
4490 sv_catpvn(sv, " ", 1);
4492 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4493 sv_setiv(sv, (IV)pent->p_proto);
4498 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4504 #ifdef HAS_GETSERVBYNAME
4505 return pp_gservent();
4507 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4513 #ifdef HAS_GETSERVBYPORT
4514 return pp_gservent();
4516 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4523 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4524 I32 which = PL_op->op_type;
4525 register char **elem;
4527 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4528 struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t);
4529 struct servent *PerlSock_getservbyport(int, Netdb_name_t);
4530 struct servent *PerlSock_getservent(void);
4532 struct servent *sent;
4535 if (which == OP_GSBYNAME) {
4536 #ifdef HAS_GETSERVBYNAME
4537 char *proto = POPpx;
4540 if (proto && !*proto)
4543 sent = PerlSock_getservbyname(name, proto);
4545 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4548 else if (which == OP_GSBYPORT) {
4549 #ifdef HAS_GETSERVBYPORT
4550 char *proto = POPpx;
4551 unsigned short port = POPu;
4554 port = PerlSock_htons(port);
4556 sent = PerlSock_getservbyport(port, proto);
4558 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4562 #ifdef HAS_GETSERVENT
4563 sent = PerlSock_getservent();
4565 DIE(aTHX_ PL_no_sock_func, "getservent");
4569 if (GIMME != G_ARRAY) {
4570 PUSHs(sv = sv_newmortal());
4572 if (which == OP_GSBYNAME) {
4574 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4576 sv_setiv(sv, (IV)(sent->s_port));
4580 sv_setpv(sv, sent->s_name);
4586 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4587 sv_setpv(sv, sent->s_name);
4588 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4589 for (elem = sent->s_aliases; elem && *elem; elem++) {
4590 sv_catpv(sv, *elem);
4592 sv_catpvn(sv, " ", 1);
4594 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4596 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4598 sv_setiv(sv, (IV)(sent->s_port));
4600 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4601 sv_setpv(sv, sent->s_proto);
4606 DIE(aTHX_ PL_no_sock_func, "getservent");
4613 #ifdef HAS_SETHOSTENT
4614 PerlSock_sethostent(TOPi);
4617 DIE(aTHX_ PL_no_sock_func, "sethostent");
4624 #ifdef HAS_SETNETENT
4625 PerlSock_setnetent(TOPi);
4628 DIE(aTHX_ PL_no_sock_func, "setnetent");
4635 #ifdef HAS_SETPROTOENT
4636 PerlSock_setprotoent(TOPi);
4639 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4646 #ifdef HAS_SETSERVENT
4647 PerlSock_setservent(TOPi);
4650 DIE(aTHX_ PL_no_sock_func, "setservent");
4657 #ifdef HAS_ENDHOSTENT
4658 PerlSock_endhostent();
4662 DIE(aTHX_ PL_no_sock_func, "endhostent");
4669 #ifdef HAS_ENDNETENT
4670 PerlSock_endnetent();
4674 DIE(aTHX_ PL_no_sock_func, "endnetent");
4681 #ifdef HAS_ENDPROTOENT
4682 PerlSock_endprotoent();
4686 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4693 #ifdef HAS_ENDSERVENT
4694 PerlSock_endservent();
4698 DIE(aTHX_ PL_no_sock_func, "endservent");
4707 DIE(aTHX_ PL_no_func, "getpwnam");
4716 DIE(aTHX_ PL_no_func, "getpwuid");
4724 I32 which = PL_op->op_type;
4726 struct passwd *pwent;
4728 #if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4729 struct spwd *spwent = NULL;
4732 if (which == OP_GPWNAM)
4733 pwent = getpwnam(POPpx);
4734 else if (which == OP_GPWUID)
4735 pwent = getpwuid(POPi);
4738 pwent = (struct passwd *)getpwent();
4740 DIE(aTHX_ PL_no_func, "getpwent");
4744 if (which == OP_GPWNAM) {
4746 spwent = getspnam(pwent->pw_name);
4748 # ifdef HAS_GETSPUID /* AFAIK there isn't any anywhere. --jhi */
4749 else if (which == OP_GPWUID) {
4751 spwent = getspnam(pwent->pw_name);
4754 # ifdef HAS_GETSPENT
4756 spwent = (struct spwd *)getspent();
4761 if (GIMME != G_ARRAY) {
4762 PUSHs(sv = sv_newmortal());
4764 if (which == OP_GPWNAM)
4765 sv_setiv(sv, (IV)pwent->pw_uid);
4767 sv_setpv(sv, pwent->pw_name);
4773 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4774 sv_setpv(sv, pwent->pw_name);
4776 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4778 # if defined(HAS_GETSPENT) || defined(HAS_GETSPNAM)
4780 sv_setpv(sv, spwent->sp_pwdp);
4782 sv_setpv(sv, pwent->pw_passwd);
4784 sv_setpv(sv, pwent->pw_passwd);
4788 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4789 sv_setiv(sv, (IV)pwent->pw_uid);
4791 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4792 sv_setiv(sv, (IV)pwent->pw_gid);
4794 /* pw_change, pw_quota, and pw_age are mutually exclusive. */
4795 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4797 sv_setiv(sv, (IV)pwent->pw_change);
4800 sv_setiv(sv, (IV)pwent->pw_quota);
4803 sv_setpv(sv, pwent->pw_age);
4808 /* pw_class and pw_comment are mutually exclusive. */
4809 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4811 sv_setpv(sv, pwent->pw_class);
4814 sv_setpv(sv, pwent->pw_comment);
4818 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4820 sv_setpv(sv, pwent->pw_gecos);
4822 #ifndef INCOMPLETE_TAINTS
4823 /* pw_gecos is tainted because user himself can diddle with it. */
4827 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4828 sv_setpv(sv, pwent->pw_dir);
4830 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831 sv_setpv(sv, pwent->pw_shell);
4834 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4835 sv_setiv(sv, (IV)pwent->pw_expire);
4840 DIE(aTHX_ PL_no_func, "getpwent");
4847 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
4849 # ifdef HAS_SETSPENT
4854 DIE(aTHX_ PL_no_func, "setpwent");
4861 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
4863 # ifdef HAS_ENDSPENT
4868 DIE(aTHX_ PL_no_func, "endpwent");
4877 DIE(aTHX_ PL_no_func, "getgrnam");
4886 DIE(aTHX_ PL_no_func, "getgrgid");
4894 I32 which = PL_op->op_type;
4895 register char **elem;
4897 struct group *grent;
4900 if (which == OP_GGRNAM)
4901 grent = (struct group *)getgrnam(POPpx);
4902 else if (which == OP_GGRGID)
4903 grent = (struct group *)getgrgid(POPi);
4906 grent = (struct group *)getgrent();
4908 DIE(aTHX_ PL_no_func, "getgrent");
4912 if (GIMME != G_ARRAY) {
4913 PUSHs(sv = sv_newmortal());
4915 if (which == OP_GGRNAM)
4916 sv_setiv(sv, (IV)grent->gr_gid);
4918 sv_setpv(sv, grent->gr_name);
4924 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4925 sv_setpv(sv, grent->gr_name);
4927 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4929 sv_setpv(sv, grent->gr_passwd);
4932 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4933 sv_setiv(sv, (IV)grent->gr_gid);
4935 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4936 for (elem = grent->gr_mem; elem && *elem; elem++) {
4937 sv_catpv(sv, *elem);
4939 sv_catpvn(sv, " ", 1);
4945 DIE(aTHX_ PL_no_func, "getgrent");
4952 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
4956 DIE(aTHX_ PL_no_func, "setgrent");
4963 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
4967 DIE(aTHX_ PL_no_func, "endgrent");
4977 if (!(tmps = PerlProc_getlogin()))
4979 PUSHp(tmps, strlen(tmps));
4982 DIE(aTHX_ PL_no_func, "getlogin");
4986 /* Miscellaneous. */
4991 djSP; dMARK; dORIGMARK; dTARGET;
4992 register I32 items = SP - MARK;
4993 unsigned long a[20];
5000 while (++MARK <= SP) {
5001 if (SvTAINTED(*MARK)) {
5007 TAINT_PROPER("syscall");
5010 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5011 * or where sizeof(long) != sizeof(char*). But such machines will
5012 * not likely have syscall implemented either, so who cares?
5014 while (++MARK <= SP) {
5015 if (SvNIOK(*MARK) || !i)
5016 a[i++] = SvIV(*MARK);
5017 else if (*MARK == &PL_sv_undef)
5020 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5026 DIE(aTHX_ "Too many args to syscall");
5028 DIE(aTHX_ "Too few args to syscall");
5030 retval = syscall(a[0]);
5033 retval = syscall(a[0],a[1]);
5036 retval = syscall(a[0],a[1],a[2]);
5039 retval = syscall(a[0],a[1],a[2],a[3]);
5042 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5045 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5048 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5051 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5055 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5058 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5061 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5065 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5069 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5073 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5074 a[10],a[11],a[12],a[13]);
5076 #endif /* atarist */
5082 DIE(aTHX_ PL_no_func, "syscall");
5086 #ifdef FCNTL_EMULATE_FLOCK
5088 /* XXX Emulate flock() with fcntl().
5089 What's really needed is a good file locking module.
5093 fcntl_emulate_flock(int fd, int operation)
5097 switch (operation & ~LOCK_NB) {
5099 flock.l_type = F_RDLCK;
5102 flock.l_type = F_WRLCK;
5105 flock.l_type = F_UNLCK;
5111 flock.l_whence = SEEK_SET;
5112 flock.l_start = flock.l_len = (Off_t)0;
5114 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5117 #endif /* FCNTL_EMULATE_FLOCK */
5119 #ifdef LOCKF_EMULATE_FLOCK
5121 /* XXX Emulate flock() with lockf(). This is just to increase
5122 portability of scripts. The calls are not completely
5123 interchangeable. What's really needed is a good file
5127 /* The lockf() constants might have been defined in <unistd.h>.
5128 Unfortunately, <unistd.h> causes troubles on some mixed
5129 (BSD/POSIX) systems, such as SunOS 4.1.3.
5131 Further, the lockf() constants aren't POSIX, so they might not be
5132 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5133 just stick in the SVID values and be done with it. Sigh.
5137 # define F_ULOCK 0 /* Unlock a previously locked region */
5140 # define F_LOCK 1 /* Lock a region for exclusive use */
5143 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5146 # define F_TEST 3 /* Test a region for other processes locks */
5150 lockf_emulate_flock(int fd, int operation)
5156 /* flock locks entire file so for lockf we need to do the same */
5158 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5159 if (pos > 0) /* is seekable and needs to be repositioned */
5160 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5161 pos = -1; /* seek failed, so don't seek back afterwards */
5164 switch (operation) {
5166 /* LOCK_SH - get a shared lock */
5168 /* LOCK_EX - get an exclusive lock */
5170 i = lockf (fd, F_LOCK, 0);
5173 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5174 case LOCK_SH|LOCK_NB:
5175 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5176 case LOCK_EX|LOCK_NB:
5177 i = lockf (fd, F_TLOCK, 0);
5179 if ((errno == EAGAIN) || (errno == EACCES))
5180 errno = EWOULDBLOCK;
5183 /* LOCK_UN - unlock (non-blocking is a no-op) */
5185 case LOCK_UN|LOCK_NB:
5186 i = lockf (fd, F_ULOCK, 0);
5189 /* Default - can't decipher operation */
5196 if (pos > 0) /* need to restore position of the handle */
5197 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5202 #endif /* LOCKF_EMULATE_FLOCK */