3 * Copyright (c) 1991-1994, 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.
20 /* XXX Omit this -- it causes too much grief on mixed systems.
21 Next time, I should force broken systems to unset i_unistd in
30 /* Put this after #includes because fork and vfork prototypes may
37 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
38 # include <sys/socket.h>
42 # include <net/errno.h>
50 #include <sys/select.h>
63 struct passwd *getpwnam _((char *));
64 struct passwd *getpwuid _((Uid_t));
66 struct passwd *getpwent _((void));
73 struct group *getgrnam _((char *));
74 struct group *getgrgid _((Gid_t));
76 struct group *getgrent _((void));
90 # define getpgrp getpgrp2
94 # define setpgrp setpgrp2
97 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
98 static int dooneliner _((char *cmd, char *filename));
108 fp = my_popen(tmps, "r");
110 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
111 if (GIMME == G_SCALAR) {
112 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
122 if (sv_gets(sv, fp, 0) == Nullch) {
126 XPUSHs(sv_2mortal(sv));
127 if (SvLEN(sv) - SvCUR(sv) > 20) {
128 SvLEN_set(sv, SvCUR(sv)+1);
129 Renew(SvPVX(sv), SvLEN(sv), char);
133 statusvalue = my_pclose(fp);
137 if (GIMME == G_SCALAR)
151 SAVESPTR(last_in_gv); /* We don't want this to be permanent. */
152 last_in_gv = (GV*)*stack_sp--;
164 result = do_readline();
171 last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*stack_sp--)), na), TRUE,SVt_PVIO);
172 return do_readline();
177 last_in_gv = cGVOP->op_gv;
178 return do_readline();
185 if (SP - MARK != 1) {
187 do_join(TARG, &sv_no, MARK, SP);
188 tmps = SvPV(TARG, na);
192 tmps = SvPV(TOPs, na);
194 if (!tmps || !*tmps) {
195 SV *error = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
196 (void)SvUPGRADE(error, SVt_PV);
197 if (SvPOK(error) && SvCUR(error))
198 sv_catpv(error, "\t...caught");
199 tmps = SvPV(error, na);
202 tmps = "Warning: something's wrong";
211 if (SP - MARK != 1) {
213 do_join(TARG, &sv_no, MARK, SP);
214 tmps = SvPV(TARG, na);
218 tmps = SvPV(TOPs, na);
220 if (!tmps || !*tmps) {
221 SV *error = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
222 (void)SvUPGRADE(error, SVt_PV);
223 if (SvPOK(error) && SvCUR(error))
224 sv_catpv(error, "\t...propagated");
225 tmps = SvPV(error, na);
247 tmps = SvPV(sv, len);
248 if (do_open(gv, tmps, len,Nullfp)) {
249 IoLINES(GvIOp(gv)) = 0;
250 PUSHi( (I32)forkprocess );
252 else if (forkprocess == 0) /* we are a new child */
269 PUSHs( do_close(gv, TRUE) ? &sv_yes : &sv_no );
293 do_close(rgv, FALSE);
295 do_close(wgv, FALSE);
300 IoIFP(rstio) = fdopen(fd[0], "r");
301 IoOFP(wstio) = fdopen(fd[1], "w");
302 IoIFP(wstio) = IoOFP(wstio);
306 if (!IoIFP(rstio) || !IoOFP(wstio)) {
307 if (IoIFP(rstio)) fclose(IoIFP(rstio));
309 if (IoOFP(wstio)) fclose(IoOFP(wstio));
319 DIE(no_func, "pipe");
332 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io)))
350 TAINT_PROPER("umask");
353 DIE(no_func, "Unsupported function umask");
371 if (!(io = GvIO(gv)) || !(fp = IoIFP(io)))
376 if (!fflush(fp) && (fp->_flag |= _IOBIN))
381 if (setmode(fileno(fp), OP_BINARY) != -1)
399 SV **mark = stack_base + ++*markstack_ptr; /* reuse in entersub */
400 I32 markoff = mark - stack_base - 1;
404 if (SvTYPE(varsv) == SVt_PVHV)
405 methname = "TIEHASH";
406 else if (SvTYPE(varsv) == SVt_PVAV)
407 methname = "TIEARRAY";
408 else if (SvTYPE(varsv) == SVt_PVGV)
409 methname = "TIEHANDLE";
411 methname = "TIESCALAR";
413 stash = gv_stashsv(mark[1], FALSE);
414 if (!stash || !(gv = gv_fetchmethod(stash, methname)) || !GvCV(gv))
415 DIE("Can't locate object method \"%s\" via package \"%s\"",
416 methname, SvPV(mark[1],na));
418 Zero(&myop, 1, BINOP);
419 myop.op_last = (OP *) &myop;
420 myop.op_next = Nullop;
421 myop.op_flags = OPf_KNOW|OPf_STACKED;
430 if (op = pp_entersub())
435 if (sv_isobject(sv)) {
436 if (SvTYPE(varsv) == SVt_PVHV || SvTYPE(varsv) == SVt_PVAV) {
437 sv_unmagic(varsv, 'P');
438 sv_magic(varsv, sv, 'P', Nullch, 0);
441 sv_unmagic(varsv, 'q');
442 sv_magic(varsv, sv, 'q', Nullch, 0);
446 SP = stack_base + markoff;
454 if (SvTYPE(TOPs) == SVt_PVHV || SvTYPE(TOPs) == SVt_PVAV)
455 sv_unmagic(TOPs, 'P');
457 sv_unmagic(TOPs, 'q');
473 sv = sv_mortalcopy(&sv_no);
474 sv_setpv(sv, "AnyDBM_File");
475 stash = gv_stashsv(sv, FALSE);
476 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")) || !GvCV(gv)) {
478 perl_requirepv("AnyDBM_File.pm");
480 if (!(gv = gv_fetchmethod(stash, "TIEHASH")) || !GvCV(gv))
481 DIE("No dbm on this machine");
484 Zero(&myop, 1, BINOP);
485 myop.op_last = (OP *) &myop;
486 myop.op_next = Nullop;
487 myop.op_flags = OPf_KNOW|OPf_STACKED;
499 PUSHs(sv_2mortal(newSViv(O_RDWR|O_CREAT)));
501 PUSHs(sv_2mortal(newSViv(O_RDWR)));
506 if (op = pp_entersub())
510 if (!sv_isobject(TOPs)) {
518 PUSHs(sv_2mortal(newSViv(O_RDONLY)));
523 if (op = pp_entersub())
528 if (sv_isobject(TOPs))
529 sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
536 return pp_untie(ARGS);
550 struct timeval timebuf;
551 struct timeval *tbuf = &timebuf;
554 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
559 # if BYTEORDER & 0xf0000
560 # define ORDERBYTE (0x88888888 - BYTEORDER)
562 # define ORDERBYTE (0x4444 - BYTEORDER)
568 for (i = 1; i <= 3; i++) {
576 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
577 growsize = maxlen; /* little endians can use vecs directly */
585 masksize = NFDBITS / NBBY;
587 masksize = sizeof(long); /* documented int, everyone seems to use long */
589 growsize = maxlen + (masksize - (maxlen % masksize));
590 Zero(&fd_sets[0], 4, char*);
598 timebuf.tv_sec = (long)value;
599 value -= (double)timebuf.tv_sec;
600 timebuf.tv_usec = (long)(value * 1000000.0);
603 tbuf = Null(struct timeval*);
605 for (i = 1; i <= 3; i++) {
612 SvPV_force(sv,na); /* force string conversion */
615 Sv_Grow(sv, growsize);
617 while (++j <= growsize) {
621 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
623 New(403, fd_sets[i], growsize, char);
624 for (offset = 0; offset < growsize; offset += masksize) {
625 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
626 fd_sets[i][j+offset] = s[(k % masksize) + offset];
629 fd_sets[i] = SvPVX(sv);
635 (Select_fd_set_t) fd_sets[1],
636 (Select_fd_set_t) fd_sets[2],
637 (Select_fd_set_t) fd_sets[3],
639 for (i = 1; i <= 3; i++) {
642 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
644 for (offset = 0; offset < growsize; offset += masksize) {
645 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
646 s[(k % masksize) + offset] = fd_sets[i][j+offset];
648 Safefree(fd_sets[i]);
655 if (GIMME == G_ARRAY && tbuf) {
656 value = (double)(timebuf.tv_sec) +
657 (double)(timebuf.tv_usec) / 1000000.0;
658 PUSHs(sv = sv_mortalcopy(&sv_no));
663 DIE("select not implemented");
670 GV *oldgv = defoutgv;
671 if (op->op_private > 0) {
672 defoutgv = (GV*)POPs;
676 gv_efullname(TARG, oldgv);
692 if (!gv || do_eof(gv)) /* make sure we have fp with something */
696 *SvPVX(TARG) = getc(IoIFP(GvIOp(gv))); /* should never be EOF */
703 return pp_sysread(ARGS);
712 register CONTEXT *cx;
714 AV* padlist = CvPADLIST(cv);
715 SV** svp = AvARRAY(padlist);
721 PUSHBLOCK(cx, CXt_SUB, stack_sp);
724 curpad = AvARRAY((AV*)svp[1]);
726 defoutgv = gv; /* locally select filehandle so $% et al work */
759 SV *tmpsv = sv_newmortal();
760 gv_efullname(tmpsv, gv);
761 DIE("Undefined format \"%s\" called",SvPVX(tmpsv));
763 DIE("Not a format reference");
765 IoFLAGS(io) &= ~IOf_DIDTOP;
767 return doform(cv,gv,op->op_next);
773 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
774 register IO *io = GvIOp(gv);
775 FILE *ofp = IoOFP(io);
779 register CONTEXT *cx;
781 DEBUG_f(fprintf(stderr,"left=%ld, todo=%ld\n",
782 (long)IoLINES_LEFT(io), (long)FmLINES(formtarget)));
783 if (IoLINES_LEFT(io) < FmLINES(formtarget) &&
784 formtarget != toptarget)
790 if (!IoTOP_NAME(io)) {
792 IoFMT_NAME(io) = savepv(GvNAME(gv));
793 sprintf(tmpbuf, "%s_TOP", IoFMT_NAME(io));
794 topgv = gv_fetchpv(tmpbuf,FALSE, SVt_PVFM);
795 if ((topgv && GvFORM(topgv)) ||
796 !gv_fetchpv("top",FALSE,SVt_PVFM))
797 IoTOP_NAME(io) = savepv(tmpbuf);
799 IoTOP_NAME(io) = savepv("top");
801 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
802 if (!topgv || !GvFORM(topgv)) {
803 IoLINES_LEFT(io) = 100000000;
806 IoTOP_GV(io) = topgv;
808 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
809 I32 lines = IoLINES_LEFT(io);
810 char *s = SvPVX(formtarget);
811 if (lines <= 0) /* Yow, header didn't even fit!!! */
813 while (lines-- > 0) {
820 fwrite1(SvPVX(formtarget), s - SvPVX(formtarget), 1, ofp);
821 sv_chop(formtarget, s);
822 FmLINES(formtarget) -= IoLINES_LEFT(io);
825 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
826 fwrite1(SvPVX(formfeed), SvCUR(formfeed), 1, ofp);
827 IoLINES_LEFT(io) = IoPAGE_LEN(io);
829 formtarget = toptarget;
830 IoFLAGS(io) |= IOf_DIDTOP;
831 return doform(GvFORM(IoTOP_GV(io)),gv,op);
843 warn("Filehandle only opened for input");
845 warn("Write on closed filehandle");
850 if ((IoLINES_LEFT(io) -= FmLINES(formtarget)) < 0) {
852 warn("page overflow");
854 if (!fwrite1(SvPVX(formtarget), 1, SvCUR(formtarget), ofp) ||
858 FmLINES(formtarget) = 0;
859 SvCUR_set(formtarget, 0);
860 *SvEND(formtarget) = '\0';
861 if (IoFLAGS(io) & IOf_FLUSH)
866 formtarget = bodytarget;
873 dSP; dMARK; dORIGMARK;
879 if (op->op_flags & OPf_STACKED)
883 if (!(io = GvIO(gv))) {
886 warn("Filehandle %s never opened", SvPV(sv,na));
888 SETERRNO(EBADF,RMS$_IFI);
891 else if (!(fp = IoOFP(io))) {
895 warn("Filehandle %s opened only for input", SvPV(sv,na));
897 warn("printf on closed filehandle %s", SvPV(sv,na));
899 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
903 do_sprintf(sv, SP - MARK, MARK + 1);
904 if (!do_print(sv, fp))
907 if (IoFLAGS(io) & IOf_FLUSH)
908 if (fflush(fp) == EOF)
925 dSP; dMARK; dORIGMARK; dTARGET;
939 buffer = SvPV_force(bufsv, blen);
940 length = SvIVx(*++MARK);
942 DIE("Negative length");
945 offset = SvIVx(*++MARK);
949 if (!io || !IoIFP(io))
952 if (op->op_type == OP_RECV) {
953 bufsize = sizeof buf;
954 buffer = SvGROW(bufsv, length+1);
955 length = recvfrom(fileno(IoIFP(io)), buffer, length, offset,
956 (struct sockaddr *)buf, &bufsize);
959 SvCUR_set(bufsv, length);
960 *SvEND(bufsv) = '\0';
961 (void)SvPOK_only(bufsv);
964 sv_magic(bufsv, Nullsv, 't', Nullch, 0);
966 sv_setpvn(TARG, buf, bufsize);
971 if (op->op_type == OP_RECV)
972 DIE(no_sock_func, "recv");
974 buffer = SvGROW(bufsv, length+offset+1);
975 if (op->op_type == OP_SYSREAD) {
976 length = read(fileno(IoIFP(io)), buffer+offset, length);
979 #ifdef HAS_SOCKET__bad_code_maybe
980 if (IoTYPE(io) == 's') {
981 bufsize = sizeof buf;
982 length = recvfrom(fileno(IoIFP(io)), buffer+offset, length, 0,
983 (struct sockaddr *)buf, &bufsize);
987 length = fread(buffer+offset, 1, length, IoIFP(io));
990 SvCUR_set(bufsv, length+offset);
991 *SvEND(bufsv) = '\0';
992 (void)SvPOK_only(bufsv);
995 sv_magic(bufsv, Nullsv, 't', Nullch, 0);
1007 return pp_send(ARGS);
1012 dSP; dMARK; dORIGMARK; dTARGET;
1025 buffer = SvPV(bufsv, blen);
1026 length = SvIVx(*++MARK);
1028 DIE("Negative length");
1031 if (!io || !IoIFP(io)) {
1034 if (op->op_type == OP_SYSWRITE)
1035 warn("Syswrite on closed filehandle");
1037 warn("Send on closed socket");
1040 else if (op->op_type == OP_SYSWRITE) {
1042 offset = SvIVx(*++MARK);
1045 if (length > blen - offset)
1046 length = blen - offset;
1047 length = write(fileno(IoIFP(io)), buffer+offset, length);
1050 else if (SP > MARK) {
1053 sockbuf = SvPVx(*++MARK, mlen);
1054 length = sendto(fileno(IoIFP(io)), buffer, blen, length,
1055 (struct sockaddr *)sockbuf, mlen);
1058 length = send(fileno(IoIFP(io)), buffer, blen, length);
1061 DIE(no_sock_func, "send");
1076 return pp_sysread(ARGS);
1087 gv = last_in_gv = (GV*)POPs;
1088 PUSHs(!gv || do_eof(gv) ? &sv_yes : &sv_no);
1100 gv = last_in_gv = (GV*)POPs;
1101 PUSHi( do_tell(gv) );
1112 gv = last_in_gv = (GV*)POPs;
1113 PUSHs( do_seek(gv, offset, whence) ? &sv_yes : &sv_no );
1120 Off_t len = (Off_t)POPn;
1125 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
1127 if (op->op_flags & OPf_SPECIAL) {
1128 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1129 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1130 ftruncate(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1133 else if (truncate(POPp, len) < 0)
1136 if (op->op_flags & OPf_SPECIAL) {
1137 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1138 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1139 chsize(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1145 if ((tmpfd = open(POPp, 0)) < 0)
1148 if (chsize(tmpfd, len) < 0)
1158 SETERRNO(EBADF,RMS$_IFI);
1161 DIE("truncate not implemented");
1167 return pp_ioctl(ARGS);
1174 unsigned int func = U_I(POPn);
1175 int optype = op->op_type;
1181 if (!io || !argsv || !IoIFP(io)) {
1182 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
1186 if (SvPOK(argsv) || !SvNIOK(argsv)) {
1188 s = SvPV_force(argsv, len);
1189 retval = IOCPARM_LEN(func);
1191 s = Sv_Grow(argsv, retval+1);
1192 SvCUR_set(argsv, retval);
1195 s[SvCUR(argsv)] = 17; /* a little sanity check here */
1198 retval = SvIV(argsv);
1200 s = (char*)(long)retval; /* ouch */
1202 s = (char*)retval; /* ouch */
1206 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1208 if (optype == OP_IOCTL)
1210 retval = ioctl(fileno(IoIFP(io)), func, s);
1212 DIE("ioctl is not implemented");
1216 DIE("fcntl is not implemented");
1219 retval = fcntl(fileno(IoIFP(io)), func, s);
1221 DIE("fcntl is not implemented");
1226 if (s[SvCUR(argsv)] != 17)
1227 DIE("Possible memory corruption: %s overflowed 3rd argument",
1229 s[SvCUR(argsv)] = 0; /* put our null back */
1230 SvSETMAGIC(argsv); /* Assume it has changed */
1239 PUSHp("0 but true", 10);
1252 #if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
1253 # define flock lockf_emulate_flock
1256 #if defined(HAS_FLOCK) || defined(flock)
1263 fp = IoIFP(GvIOp(gv));
1267 value = (I32)(flock(fileno(fp), argtype) >= 0);
1274 DIE(no_func, "flock()");
1286 int protocol = POPi;
1294 SETERRNO(EBADF,LIB$_INVARG);
1300 do_close(gv, FALSE);
1302 TAINT_PROPER("socket");
1303 fd = socket(domain, type, protocol);
1306 IoIFP(io) = fdopen(fd, "r"); /* stdio gets confused about sockets */
1307 IoOFP(io) = fdopen(fd, "w");
1309 if (!IoIFP(io) || !IoOFP(io)) {
1310 if (IoIFP(io)) fclose(IoIFP(io));
1311 if (IoOFP(io)) fclose(IoOFP(io));
1312 if (!IoIFP(io) && !IoOFP(io)) close(fd);
1318 DIE(no_sock_func, "socket");
1325 #ifdef HAS_SOCKETPAIR
1330 int protocol = POPi;
1343 do_close(gv1, FALSE);
1345 do_close(gv2, FALSE);
1347 TAINT_PROPER("socketpair");
1348 if (socketpair(domain, type, protocol, fd) < 0)
1350 IoIFP(io1) = fdopen(fd[0], "r");
1351 IoOFP(io1) = fdopen(fd[0], "w");
1353 IoIFP(io2) = fdopen(fd[1], "r");
1354 IoOFP(io2) = fdopen(fd[1], "w");
1356 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
1357 if (IoIFP(io1)) fclose(IoIFP(io1));
1358 if (IoOFP(io1)) fclose(IoOFP(io1));
1359 if (!IoIFP(io1) && !IoOFP(io1)) close(fd[0]);
1360 if (IoIFP(io2)) fclose(IoIFP(io2));
1361 if (IoOFP(io2)) fclose(IoOFP(io2));
1362 if (!IoIFP(io2) && !IoOFP(io2)) close(fd[1]);
1368 DIE(no_sock_func, "socketpair");
1379 register IO *io = GvIOn(gv);
1382 if (!io || !IoIFP(io))
1385 addr = SvPV(addrsv, len);
1386 TAINT_PROPER("bind");
1387 if (bind(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1394 warn("bind() on closed fd");
1395 SETERRNO(EBADF,SS$_IVCHAN);
1398 DIE(no_sock_func, "bind");
1409 register IO *io = GvIOn(gv);
1412 if (!io || !IoIFP(io))
1415 addr = SvPV(addrsv, len);
1416 TAINT_PROPER("connect");
1417 if (connect(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1424 warn("connect() on closed fd");
1425 SETERRNO(EBADF,SS$_IVCHAN);
1428 DIE(no_sock_func, "connect");
1438 register IO *io = GvIOn(gv);
1440 if (!io || !IoIFP(io))
1443 if (listen(fileno(IoIFP(io)), backlog) >= 0)
1450 warn("listen() on closed fd");
1451 SETERRNO(EBADF,SS$_IVCHAN);
1454 DIE(no_sock_func, "listen");
1462 struct sockaddr_in saddr; /* use a struct to avoid alignment problems */
1467 int len = sizeof saddr;
1479 if (!gstio || !IoIFP(gstio))
1484 do_close(ngv, FALSE);
1486 fd = accept(fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
1489 IoIFP(nstio) = fdopen(fd, "r");
1490 IoOFP(nstio) = fdopen(fd, "w");
1491 IoTYPE(nstio) = 's';
1492 if (!IoIFP(nstio) || !IoOFP(nstio)) {
1493 if (IoIFP(nstio)) fclose(IoIFP(nstio));
1494 if (IoOFP(nstio)) fclose(IoOFP(nstio));
1495 if (!IoIFP(nstio) && !IoOFP(nstio)) close(fd);
1499 PUSHp((char *)&saddr, len);
1504 warn("accept() on closed fd");
1505 SETERRNO(EBADF,SS$_IVCHAN);
1511 DIE(no_sock_func, "accept");
1521 register IO *io = GvIOn(gv);
1523 if (!io || !IoIFP(io))
1526 PUSHi( shutdown(fileno(IoIFP(io)), how) >= 0 );
1531 warn("shutdown() on closed fd");
1532 SETERRNO(EBADF,SS$_IVCHAN);
1535 DIE(no_sock_func, "shutdown");
1542 return pp_ssockopt(ARGS);
1544 DIE(no_sock_func, "getsockopt");
1552 int optype = op->op_type;
1555 unsigned int optname;
1561 if (optype == OP_GSOCKOPT)
1562 sv = sv_2mortal(NEWSV(22, 257));
1565 optname = (unsigned int) POPi;
1566 lvl = (unsigned int) POPi;
1570 if (!io || !IoIFP(io))
1573 fd = fileno(IoIFP(io));
1577 (void)SvPOK_only(sv);
1581 if (getsockopt(fd, lvl, optname, SvPVX(sv), &aint) < 0)
1591 buf = SvPV(sv, len);
1592 else if (SvOK(sv)) {
1593 aint = (int)SvIV(sv);
1597 if (setsockopt(fd, lvl, optname, buf, (int)len) < 0)
1607 warn("[gs]etsockopt() on closed fd");
1608 SETERRNO(EBADF,SS$_IVCHAN);
1613 DIE(no_sock_func, "setsockopt");
1620 return pp_getpeername(ARGS);
1622 DIE(no_sock_func, "getsockname");
1630 int optype = op->op_type;
1634 register IO *io = GvIOn(gv);
1637 if (!io || !IoIFP(io))
1640 sv = sv_2mortal(NEWSV(22, 257));
1641 (void)SvPOK_only(sv);
1645 fd = fileno(IoIFP(io));
1647 case OP_GETSOCKNAME:
1648 if (getsockname(fd, (struct sockaddr *)SvPVX(sv), &aint) < 0)
1651 case OP_GETPEERNAME:
1652 if (getpeername(fd, (struct sockaddr *)SvPVX(sv), &aint) < 0)
1663 warn("get{sock, peer}name() on closed fd");
1664 SETERRNO(EBADF,SS$_IVCHAN);
1669 DIE(no_sock_func, "getpeername");
1677 return pp_stat(ARGS);
1686 if (op->op_flags & OPf_REF) {
1687 tmpgv = cGVOP->op_gv;
1689 if (tmpgv != defgv) {
1690 laststype = OP_STAT;
1692 sv_setpv(statname, "");
1693 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1694 Fstat(fileno(IoIFP(GvIOn(tmpgv))), &statcache) < 0) {
1699 else if (laststatval < 0)
1704 if (SvTYPE(sv) == SVt_PVGV) {
1708 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1709 tmpgv = (GV*)SvRV(sv);
1712 sv_setpv(statname, SvPV(sv,na));
1715 laststype = op->op_type;
1716 if (op->op_type == OP_LSTAT)
1717 laststatval = lstat(SvPV(statname, na), &statcache);
1720 laststatval = Stat(SvPV(statname, na), &statcache);
1721 if (laststatval < 0) {
1722 if (dowarn && strchr(SvPV(statname, na), '\n'))
1723 warn(warn_nl, "stat");
1729 if (GIMME != G_ARRAY) {
1736 PUSHs(sv_2mortal(newSViv((I32)statcache.st_dev)));
1737 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ino)));
1738 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mode)));
1739 PUSHs(sv_2mortal(newSViv((I32)statcache.st_nlink)));
1740 PUSHs(sv_2mortal(newSViv((I32)statcache.st_uid)));
1741 PUSHs(sv_2mortal(newSViv((I32)statcache.st_gid)));
1742 PUSHs(sv_2mortal(newSViv((I32)statcache.st_rdev)));
1743 PUSHs(sv_2mortal(newSViv((I32)statcache.st_size)));
1744 PUSHs(sv_2mortal(newSViv((I32)statcache.st_atime)));
1745 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mtime)));
1746 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ctime)));
1747 #ifdef USE_STAT_BLOCKS
1748 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blksize)));
1749 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blocks)));
1751 PUSHs(sv_2mortal(newSVpv("", 0)));
1752 PUSHs(sv_2mortal(newSVpv("", 0)));
1760 I32 result = my_stat(ARGS);
1764 if (cando(S_IRUSR, 0, &statcache))
1771 I32 result = my_stat(ARGS);
1775 if (cando(S_IWUSR, 0, &statcache))
1782 I32 result = my_stat(ARGS);
1786 if (cando(S_IXUSR, 0, &statcache))
1793 I32 result = my_stat(ARGS);
1797 if (cando(S_IRUSR, 1, &statcache))
1804 I32 result = my_stat(ARGS);
1808 if (cando(S_IWUSR, 1, &statcache))
1815 I32 result = my_stat(ARGS);
1819 if (cando(S_IXUSR, 1, &statcache))
1826 I32 result = my_stat(ARGS);
1835 return pp_ftrowned(ARGS);
1840 I32 result = my_stat(ARGS);
1844 if (statcache.st_uid == (op->op_type == OP_FTEOWNED ? euid : uid) )
1851 I32 result = my_stat(ARGS);
1855 if (!statcache.st_size)
1862 I32 result = my_stat(ARGS);
1866 PUSHi(statcache.st_size);
1872 I32 result = my_stat(ARGS);
1876 PUSHn( (basetime - statcache.st_mtime) / 86400.0 );
1882 I32 result = my_stat(ARGS);
1886 PUSHn( (basetime - statcache.st_atime) / 86400.0 );
1892 I32 result = my_stat(ARGS);
1896 PUSHn( (basetime - statcache.st_ctime) / 86400.0 );
1902 I32 result = my_stat(ARGS);
1906 if (S_ISSOCK(statcache.st_mode))
1913 I32 result = my_stat(ARGS);
1917 if (S_ISCHR(statcache.st_mode))
1924 I32 result = my_stat(ARGS);
1928 if (S_ISBLK(statcache.st_mode))
1935 I32 result = my_stat(ARGS);
1939 if (S_ISREG(statcache.st_mode))
1946 I32 result = my_stat(ARGS);
1950 if (S_ISDIR(statcache.st_mode))
1957 I32 result = my_stat(ARGS);
1961 if (S_ISFIFO(statcache.st_mode))
1968 I32 result = my_lstat(ARGS);
1972 if (S_ISLNK(statcache.st_mode))
1981 I32 result = my_stat(ARGS);
1985 if (statcache.st_mode & S_ISUID)
1995 I32 result = my_stat(ARGS);
1999 if (statcache.st_mode & S_ISGID)
2009 I32 result = my_stat(ARGS);
2013 if (statcache.st_mode & S_ISVTX)
2025 if (op->op_flags & OPf_REF) {
2030 gv = gv_fetchpv(tmps = POPp, FALSE, SVt_PVIO);
2031 if (GvIO(gv) && IoIFP(GvIOp(gv)))
2032 fd = fileno(IoIFP(GvIOp(gv)));
2033 else if (isDIGIT(*tmps))
2042 #if defined(atarist) /* this will work with atariST. Configure will
2043 make guesses for other systems. */
2044 # define FILE_base(f) ((f)->_base)
2045 # define FILE_ptr(f) ((f)->_ptr)
2046 # define FILE_cnt(f) ((f)->_cnt)
2047 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
2057 register STDCHAR *s;
2061 if (op->op_flags & OPf_REF) {
2063 if (cGVOP->op_gv == defgv) {
2068 goto really_filename;
2072 statgv = cGVOP->op_gv;
2073 sv_setpv(statname, "");
2076 if (io && IoIFP(io)) {
2078 Fstat(fileno(IoIFP(io)), &statcache);
2079 if (S_ISDIR(statcache.st_mode)) /* handle NFS glitch */
2080 if (op->op_type == OP_FTTEXT)
2084 if (FILE_cnt(IoIFP(io)) <= 0) {
2085 i = getc(IoIFP(io));
2087 (void)ungetc(i, IoIFP(io));
2089 if (FILE_cnt(IoIFP(io)) <= 0) /* null file is anything */
2091 len = FILE_bufsiz(IoIFP(io));
2092 s = FILE_base(IoIFP(io));
2094 DIE("-T and -B not implemented on filehandles");
2099 warn("Test on unopened file <%s>",
2100 GvENAME(cGVOP->op_gv));
2101 SETERRNO(EBADF,RMS$_IFI);
2108 sv_setpv(statname, SvPV(sv, na));
2111 i = open(SvPV(sv, na), O_RDONLY, 0);
2113 i = open(SvPV(sv, na), 0);
2116 if (dowarn && strchr(SvPV(sv, na), '\n'))
2117 warn(warn_nl, "open");
2120 Fstat(i, &statcache);
2121 len = read(i, tbuf, 512);
2124 if (S_ISDIR(statcache.st_mode) && op->op_type == OP_FTTEXT)
2125 RETPUSHNO; /* special case NFS directories */
2126 RETPUSHYES; /* null file is anything */
2131 /* now scan s to look for textiness */
2133 for (i = 0; i < len; i++, s++) {
2134 if (!*s) { /* null never allowed in text */
2141 *s != '\n' && *s != '\r' && *s != '\b' &&
2142 *s != '\t' && *s != '\f' && *s != 27)
2146 if ((odd * 30 > len) == (op->op_type == OP_FTTEXT)) /* allow 30% odd */
2154 return pp_fttext(ARGS);
2169 if (!tmps || !*tmps) {
2170 svp = hv_fetch(GvHVn(envgv), "HOME", 4, FALSE);
2172 tmps = SvPV(*svp, na);
2174 if (!tmps || !*tmps) {
2175 svp = hv_fetch(GvHVn(envgv), "LOGDIR", 6, FALSE);
2177 tmps = SvPV(*svp, na);
2179 TAINT_PROPER("chdir");
2180 PUSHi( chdir(tmps) >= 0 );
2182 /* Clear the DEFAULT element of ENV so we'll get the new value
2184 hv_delete(GvHVn(envgv),"DEFAULT",7);
2191 dSP; dMARK; dTARGET;
2194 value = (I32)apply(op->op_type, MARK, SP);
2199 DIE(no_func, "Unsupported function chown");
2209 TAINT_PROPER("chroot");
2210 PUSHi( chroot(tmps) >= 0 );
2213 DIE(no_func, "chroot");
2219 dSP; dMARK; dTARGET;
2221 value = (I32)apply(op->op_type, MARK, SP);
2229 dSP; dMARK; dTARGET;
2231 value = (I32)apply(op->op_type, MARK, SP);
2239 dSP; dMARK; dTARGET;
2241 value = (I32)apply(op->op_type, MARK, SP);
2253 char *tmps = SvPV(TOPs, na);
2254 TAINT_PROPER("rename");
2256 anum = rename(tmps, tmps2);
2258 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
2261 if (euid || Stat(tmps2, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
2262 (void)UNLINK(tmps2);
2263 if (!(anum = link(tmps, tmps2)))
2264 anum = UNLINK(tmps);
2276 char *tmps = SvPV(TOPs, na);
2277 TAINT_PROPER("link");
2278 SETi( link(tmps, tmps2) >= 0 );
2280 DIE(no_func, "Unsupported function link");
2290 char *tmps = SvPV(TOPs, na);
2291 TAINT_PROPER("symlink");
2292 SETi( symlink(tmps, tmps2) >= 0 );
2295 DIE(no_func, "symlink");
2306 len = readlink(tmps, buf, sizeof buf);
2314 RETSETUNDEF; /* just pretend it's a normal file */
2318 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
2320 dooneliner(cmd, filename)
2326 *save_filename = filename;
2332 for (s = mybuf+strlen(mybuf); *filename; ) {
2337 myfp = my_popen(mybuf, "r");
2340 s = fgets(mybuf, sizeof mybuf, myfp);
2341 (void)my_pclose(myfp);
2343 for (errno = 1; errno < sys_nerr; errno++) {
2344 #ifdef HAS_SYS_ERRLIST
2345 if (instr(mybuf, sys_errlist[errno])) /* you don't see this */
2348 char *errmsg; /* especially if it isn't there */
2351 (errmsg = strerror(errno)) ? errmsg : "NoErRoR"))
2357 #define EACCES EPERM
2359 if (instr(mybuf, "cannot make"))
2360 SETERRNO(EEXIST,RMS$_FEX);
2361 else if (instr(mybuf, "existing file"))
2362 SETERRNO(EEXIST,RMS$_FEX);
2363 else if (instr(mybuf, "ile exists"))
2364 SETERRNO(EEXIST,RMS$_FEX);
2365 else if (instr(mybuf, "non-exist"))
2366 SETERRNO(ENOENT,RMS$_FNF);
2367 else if (instr(mybuf, "does not exist"))
2368 SETERRNO(ENOENT,RMS$_FNF);
2369 else if (instr(mybuf, "not empty"))
2370 SETERRNO(EBUSY,SS$_DEVOFFLINE);
2371 else if (instr(mybuf, "cannot access"))
2372 SETERRNO(EACCES,RMS$_PRV);
2374 SETERRNO(EPERM,RMS$_PRV);
2377 else { /* some mkdirs return no failure indication */
2378 anum = (Stat(save_filename, &statbuf) >= 0);
2379 if (op->op_type == OP_RMDIR)
2384 SETERRNO(EACCES,RMS$_PRV); /* a guess */
2400 char *tmps = SvPV(TOPs, na);
2402 TAINT_PROPER("mkdir");
2404 SETi( mkdir(tmps, mode) >= 0 );
2406 SETi( dooneliner("mkdir", tmps) );
2407 oldumask = umask(0);
2409 chmod(tmps, (mode & ~oldumask) & 0777);
2420 TAINT_PROPER("rmdir");
2422 XPUSHi( rmdir(tmps) >= 0 );
2424 XPUSHi( dooneliner("rmdir", tmps) );
2429 /* Directory calls. */
2434 #if defined(Direntry_t) && defined(HAS_READDIR)
2435 char *dirname = POPp;
2437 register IO *io = GvIOn(gv);
2443 closedir(IoDIRP(io));
2444 if (!(IoDIRP(io) = opendir(dirname)))
2450 SETERRNO(EBADF,RMS$_DIR);
2453 DIE(no_dir_func, "opendir");
2460 #if defined(Direntry_t) && defined(HAS_READDIR)
2462 Direntry_t *readdir _((DIR *));
2464 register Direntry_t *dp;
2466 register IO *io = GvIOn(gv);
2468 if (!io || !IoDIRP(io))
2471 if (GIMME == G_ARRAY) {
2473 while (dp = (Direntry_t *)readdir(IoDIRP(io))) {
2475 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2477 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2482 if (!(dp = (Direntry_t *)readdir(IoDIRP(io))))
2485 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2487 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2494 SETERRNO(EBADF,RMS$_ISI);
2495 if (GIMME == G_ARRAY)
2500 DIE(no_dir_func, "readdir");
2507 #if defined(HAS_TELLDIR) || defined(telldir)
2508 #if !defined(telldir) && !defined(HAS_TELLDIR_PROTOTYPE)
2509 long telldir _((DIR *));
2512 register IO *io = GvIOn(gv);
2514 if (!io || !IoDIRP(io))
2517 PUSHi( telldir(IoDIRP(io)) );
2521 SETERRNO(EBADF,RMS$_ISI);
2524 DIE(no_dir_func, "telldir");
2531 #if defined(HAS_SEEKDIR) || defined(seekdir)
2534 register IO *io = GvIOn(gv);
2536 if (!io || !IoDIRP(io))
2539 (void)seekdir(IoDIRP(io), along);
2544 SETERRNO(EBADF,RMS$_ISI);
2547 DIE(no_dir_func, "seekdir");
2554 #if defined(HAS_REWINDDIR) || defined(rewinddir)
2556 register IO *io = GvIOn(gv);
2558 if (!io || !IoDIRP(io))
2561 (void)rewinddir(IoDIRP(io));
2565 SETERRNO(EBADF,RMS$_ISI);
2568 DIE(no_dir_func, "rewinddir");
2575 #if defined(Direntry_t) && defined(HAS_READDIR)
2577 register IO *io = GvIOn(gv);
2579 if (!io || !IoDIRP(io))
2582 #ifdef VOID_CLOSEDIR
2583 closedir(IoDIRP(io));
2585 if (closedir(IoDIRP(io)) < 0) {
2586 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
2595 SETERRNO(EBADF,RMS$_IFI);
2598 DIE(no_dir_func, "closedir");
2602 /* Process control. */
2617 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
2618 sv_setiv(GvSV(tmpgv), (I32)getpid());
2619 hv_clear(pidstatus); /* no kids, so don't wait for 'em */
2624 DIE(no_func, "Unsupported function fork");
2637 childpid = wait(&argflags);
2639 pidgone(childpid, argflags);
2640 value = (I32)childpid;
2641 statusvalue = FIXSTATUS(argflags);
2645 DIE(no_func, "Unsupported function wait");
2660 childpid = wait4pid(childpid, &argflags, optype);
2661 value = (I32)childpid;
2662 statusvalue = FIXSTATUS(argflags);
2666 DIE(no_func, "Unsupported function wait");
2672 dSP; dMARK; dORIGMARK; dTARGET;
2677 Signal_t (*ihand)(); /* place to save signal during system() */
2678 Signal_t (*qhand)(); /* place to save signal during system() */
2680 #if defined(HAS_FORK) && !defined(VMS)
2681 if (SP - MARK == 1) {
2683 char *junk = SvPV(TOPs, na);
2685 TAINT_PROPER("system");
2688 while ((childpid = vfork()) == -1) {
2689 if (errno != EAGAIN) {
2698 ihand = signal(SIGINT, SIG_IGN);
2699 qhand = signal(SIGQUIT, SIG_IGN);
2701 result = wait4pid(childpid, &status, 0);
2702 } while (result == -1 && errno == EINTR);
2703 (void)signal(SIGINT, ihand);
2704 (void)signal(SIGQUIT, qhand);
2705 statusvalue = FIXSTATUS(status);
2709 value = (I32)((unsigned int)status & 0xffff);
2711 do_execfree(); /* free any memory child malloced on vfork */
2716 if (op->op_flags & OPf_STACKED) {
2717 SV *really = *++MARK;
2718 value = (I32)do_aexec(really, MARK, SP);
2720 else if (SP - MARK != 1)
2721 value = (I32)do_aexec(Nullsv, MARK, SP);
2723 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2726 #else /* ! FORK or VMS */
2727 if (op->op_flags & OPf_STACKED) {
2728 SV *really = *++MARK;
2729 value = (I32)do_aspawn(really, MARK, SP);
2731 else if (SP - MARK != 1)
2732 value = (I32)do_aspawn(Nullsv, MARK, SP);
2734 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), na));
2739 #endif /* !FORK or VMS */
2745 dSP; dMARK; dORIGMARK; dTARGET;
2748 if (op->op_flags & OPf_STACKED) {
2749 SV *really = *++MARK;
2750 value = (I32)do_aexec(really, MARK, SP);
2752 else if (SP - MARK != 1)
2754 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
2756 value = (I32)do_aexec(Nullsv, MARK, SP);
2760 char *junk = SvPV(*SP, na);
2762 TAINT_PROPER("exec");
2765 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), na));
2767 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2777 dSP; dMARK; dTARGET;
2780 value = (I32)apply(op->op_type, MARK, SP);
2785 DIE(no_func, "Unsupported function kill");
2793 XPUSHi( getppid() );
2796 DIE(no_func, "getppid");
2812 value = (I32)getpgrp(pid);
2815 DIE("POSIX getpgrp can't take an argument");
2816 value = (I32)getpgrp();
2821 DIE(no_func, "getpgrp()");
2840 TAINT_PROPER("setpgrp");
2842 SETi( setpgrp(pid, pgrp) >= 0 );
2844 if ((pgrp != 0) || (pid != 0)) {
2845 DIE("POSIX setpgrp can't take an argument");
2847 SETi( setpgrp() >= 0 );
2848 #endif /* USE_BSDPGRP */
2851 DIE(no_func, "setpgrp()");
2860 #ifdef HAS_GETPRIORITY
2863 SETi( getpriority(which, who) );
2866 DIE(no_func, "getpriority()");
2876 #ifdef HAS_SETPRIORITY
2880 TAINT_PROPER("setpriority");
2881 SETi( setpriority(which, who, niceval) >= 0 );
2884 DIE(no_func, "setpriority()");
2893 XPUSHi( time(Null(Time_t*)) );
2905 #if defined(MSDOS) || !defined(HAS_TIMES)
2906 DIE("times not implemented");
2911 (void)times(×buf);
2913 (void)times((tbuffer_t *)×buf); /* time.h uses different name for */
2914 /* struct tms, though same data */
2918 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_utime)/HZ)));
2919 if (GIMME == G_ARRAY) {
2920 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_stime)/HZ)));
2921 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cutime)/HZ)));
2922 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cstime)/HZ)));
2930 return pp_gmtime(ARGS);
2938 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
2939 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2940 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2945 when = (Time_t)SvIVx(POPs);
2947 if (op->op_type == OP_LOCALTIME)
2948 tmbuf = localtime(&when);
2950 tmbuf = gmtime(&when);
2953 if (GIMME != G_ARRAY) {
2958 sprintf(mybuf, "%s %s %2d %02d:%02d:%02d %d",
2959 dayname[tmbuf->tm_wday],
2960 monname[tmbuf->tm_mon],
2965 tmbuf->tm_year + 1900);
2966 PUSHp(mybuf, strlen(mybuf));
2969 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_sec)));
2970 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_min)));
2971 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_hour)));
2972 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mday)));
2973 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mon)));
2974 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_year)));
2975 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_wday)));
2976 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_yday)));
2977 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_isdst)));
2988 anum = alarm((unsigned int)anum);
2995 DIE(no_func, "Unsupported function alarm");
3006 (void)time(&lasttime);
3011 sleep((unsigned int)duration);
3014 XPUSHi(when - lasttime);
3018 /* Shared memory. */
3022 return pp_semget(ARGS);
3027 return pp_semctl(ARGS);
3032 return pp_shmwrite(ARGS);
3037 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3038 dSP; dMARK; dTARGET;
3039 I32 value = (I32)(do_shmio(op->op_type, MARK, SP) >= 0);
3044 return pp_semget(ARGS);
3048 /* Message passing. */
3052 return pp_semget(ARGS);
3057 return pp_semctl(ARGS);
3062 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3063 dSP; dMARK; dTARGET;
3064 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
3069 return pp_semget(ARGS);
3075 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3076 dSP; dMARK; dTARGET;
3077 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
3082 return pp_semget(ARGS);
3090 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3091 dSP; dMARK; dTARGET;
3092 int anum = do_ipcget(op->op_type, MARK, SP);
3099 DIE("System V IPC is not implemented on this machine");
3105 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3106 dSP; dMARK; dTARGET;
3107 int anum = do_ipcctl(op->op_type, MARK, SP);
3115 PUSHp("0 but true",10);
3119 return pp_semget(ARGS);
3125 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3126 dSP; dMARK; dTARGET;
3127 I32 value = (I32)(do_semop(MARK, SP) >= 0);
3132 return pp_semget(ARGS);
3136 /* Get system info. */
3141 return pp_ghostent(ARGS);
3143 DIE(no_sock_func, "gethostbyname");
3150 return pp_ghostent(ARGS);
3152 DIE(no_sock_func, "gethostbyaddr");
3160 I32 which = op->op_type;
3161 register char **elem;
3163 struct hostent *gethostbyname();
3164 struct hostent *gethostbyaddr();
3165 #ifdef HAS_GETHOSTENT
3166 struct hostent *gethostent();
3168 struct hostent *hent;
3172 if (which == OP_GHBYNAME) {
3173 hent = gethostbyname(POPp);
3175 else if (which == OP_GHBYADDR) {
3176 int addrtype = POPi;
3179 char *addr = SvPV(addrsv, addrlen);
3181 hent = gethostbyaddr(addr, addrlen, addrtype);
3184 #ifdef HAS_GETHOSTENT
3185 hent = gethostent();
3187 DIE("gethostent not implemented");
3190 #ifdef HOST_NOT_FOUND
3192 statusvalue = FIXSTATUS(h_errno);
3195 if (GIMME != G_ARRAY) {
3196 PUSHs(sv = sv_newmortal());
3198 if (which == OP_GHBYNAME) {
3199 sv_setpvn(sv, hent->h_addr, hent->h_length);
3202 sv_setpv(sv, (char*)hent->h_name);
3208 PUSHs(sv = sv_mortalcopy(&sv_no));
3209 sv_setpv(sv, (char*)hent->h_name);
3210 PUSHs(sv = sv_mortalcopy(&sv_no));
3211 for (elem = hent->h_aliases; elem && *elem; elem++) {
3212 sv_catpv(sv, *elem);
3214 sv_catpvn(sv, " ", 1);
3216 PUSHs(sv = sv_mortalcopy(&sv_no));
3217 sv_setiv(sv, (I32)hent->h_addrtype);
3218 PUSHs(sv = sv_mortalcopy(&sv_no));
3219 len = hent->h_length;
3220 sv_setiv(sv, (I32)len);
3222 for (elem = hent->h_addr_list; elem && *elem; elem++) {
3223 XPUSHs(sv = sv_mortalcopy(&sv_no));
3224 sv_setpvn(sv, *elem, len);
3227 PUSHs(sv = sv_mortalcopy(&sv_no));
3228 sv_setpvn(sv, hent->h_addr, len);
3233 DIE(no_sock_func, "gethostent");
3240 return pp_gnetent(ARGS);
3242 DIE(no_sock_func, "getnetbyname");
3249 return pp_gnetent(ARGS);
3251 DIE(no_sock_func, "getnetbyaddr");
3259 I32 which = op->op_type;
3260 register char **elem;
3262 struct netent *getnetbyname();
3263 struct netent *getnetbyaddr();
3264 struct netent *getnetent();
3265 struct netent *nent;
3267 if (which == OP_GNBYNAME)
3268 nent = getnetbyname(POPp);
3269 else if (which == OP_GNBYADDR) {
3270 int addrtype = POPi;
3271 unsigned long addr = U_L(POPn);
3272 nent = getnetbyaddr((long)addr, addrtype);
3278 if (GIMME != G_ARRAY) {
3279 PUSHs(sv = sv_newmortal());
3281 if (which == OP_GNBYNAME)
3282 sv_setiv(sv, (I32)nent->n_net);
3284 sv_setpv(sv, nent->n_name);
3290 PUSHs(sv = sv_mortalcopy(&sv_no));
3291 sv_setpv(sv, nent->n_name);
3292 PUSHs(sv = sv_mortalcopy(&sv_no));
3293 for (elem = nent->n_aliases; *elem; elem++) {
3294 sv_catpv(sv, *elem);
3296 sv_catpvn(sv, " ", 1);
3298 PUSHs(sv = sv_mortalcopy(&sv_no));
3299 sv_setiv(sv, (I32)nent->n_addrtype);
3300 PUSHs(sv = sv_mortalcopy(&sv_no));
3301 sv_setiv(sv, (I32)nent->n_net);
3306 DIE(no_sock_func, "getnetent");
3313 return pp_gprotoent(ARGS);
3315 DIE(no_sock_func, "getprotobyname");
3322 return pp_gprotoent(ARGS);
3324 DIE(no_sock_func, "getprotobynumber");
3332 I32 which = op->op_type;
3333 register char **elem;
3335 struct protoent *getprotobyname();
3336 struct protoent *getprotobynumber();
3337 struct protoent *getprotoent();
3338 struct protoent *pent;
3340 if (which == OP_GPBYNAME)
3341 pent = getprotobyname(POPp);
3342 else if (which == OP_GPBYNUMBER)
3343 pent = getprotobynumber(POPi);
3345 pent = getprotoent();
3348 if (GIMME != G_ARRAY) {
3349 PUSHs(sv = sv_newmortal());
3351 if (which == OP_GPBYNAME)
3352 sv_setiv(sv, (I32)pent->p_proto);
3354 sv_setpv(sv, pent->p_name);
3360 PUSHs(sv = sv_mortalcopy(&sv_no));
3361 sv_setpv(sv, pent->p_name);
3362 PUSHs(sv = sv_mortalcopy(&sv_no));
3363 for (elem = pent->p_aliases; *elem; elem++) {
3364 sv_catpv(sv, *elem);
3366 sv_catpvn(sv, " ", 1);
3368 PUSHs(sv = sv_mortalcopy(&sv_no));
3369 sv_setiv(sv, (I32)pent->p_proto);
3374 DIE(no_sock_func, "getprotoent");
3381 return pp_gservent(ARGS);
3383 DIE(no_sock_func, "getservbyname");
3390 return pp_gservent(ARGS);
3392 DIE(no_sock_func, "getservbyport");
3400 I32 which = op->op_type;
3401 register char **elem;
3403 struct servent *getservbyname();
3404 struct servent *getservbynumber();
3405 struct servent *getservent();
3406 struct servent *sent;
3408 if (which == OP_GSBYNAME) {
3412 if (proto && !*proto)
3415 sent = getservbyname(name, proto);
3417 else if (which == OP_GSBYPORT) {
3421 sent = getservbyport(port, proto);
3424 sent = getservent();
3427 if (GIMME != G_ARRAY) {
3428 PUSHs(sv = sv_newmortal());
3430 if (which == OP_GSBYNAME) {
3432 sv_setiv(sv, (I32)ntohs(sent->s_port));
3434 sv_setiv(sv, (I32)(sent->s_port));
3438 sv_setpv(sv, sent->s_name);
3444 PUSHs(sv = sv_mortalcopy(&sv_no));
3445 sv_setpv(sv, sent->s_name);
3446 PUSHs(sv = sv_mortalcopy(&sv_no));
3447 for (elem = sent->s_aliases; *elem; elem++) {
3448 sv_catpv(sv, *elem);
3450 sv_catpvn(sv, " ", 1);
3452 PUSHs(sv = sv_mortalcopy(&sv_no));
3454 sv_setiv(sv, (I32)ntohs(sent->s_port));
3456 sv_setiv(sv, (I32)(sent->s_port));
3458 PUSHs(sv = sv_mortalcopy(&sv_no));
3459 sv_setpv(sv, sent->s_proto);
3464 DIE(no_sock_func, "getservent");
3475 DIE(no_sock_func, "sethostent");
3486 DIE(no_sock_func, "setnetent");
3497 DIE(no_sock_func, "setprotoent");
3508 DIE(no_sock_func, "setservent");
3520 DIE(no_sock_func, "endhostent");
3532 DIE(no_sock_func, "endnetent");
3544 DIE(no_sock_func, "endprotoent");
3556 DIE(no_sock_func, "endservent");
3563 return pp_gpwent(ARGS);
3565 DIE(no_func, "getpwnam");
3572 return pp_gpwent(ARGS);
3574 DIE(no_func, "getpwuid");
3582 I32 which = op->op_type;
3584 struct passwd *pwent;
3586 if (which == OP_GPWNAM)
3587 pwent = getpwnam(POPp);
3588 else if (which == OP_GPWUID)
3589 pwent = getpwuid(POPi);
3591 pwent = (struct passwd *)getpwent();
3594 if (GIMME != G_ARRAY) {
3595 PUSHs(sv = sv_newmortal());
3597 if (which == OP_GPWNAM)
3598 sv_setiv(sv, (I32)pwent->pw_uid);
3600 sv_setpv(sv, pwent->pw_name);
3606 PUSHs(sv = sv_mortalcopy(&sv_no));
3607 sv_setpv(sv, pwent->pw_name);
3608 PUSHs(sv = sv_mortalcopy(&sv_no));
3609 sv_setpv(sv, pwent->pw_passwd);
3610 PUSHs(sv = sv_mortalcopy(&sv_no));
3611 sv_setiv(sv, (I32)pwent->pw_uid);
3612 PUSHs(sv = sv_mortalcopy(&sv_no));
3613 sv_setiv(sv, (I32)pwent->pw_gid);
3614 PUSHs(sv = sv_mortalcopy(&sv_no));
3616 sv_setiv(sv, (I32)pwent->pw_change);
3619 sv_setiv(sv, (I32)pwent->pw_quota);
3622 sv_setpv(sv, pwent->pw_age);
3626 PUSHs(sv = sv_mortalcopy(&sv_no));
3628 sv_setpv(sv, pwent->pw_class);
3631 sv_setpv(sv, pwent->pw_comment);
3634 PUSHs(sv = sv_mortalcopy(&sv_no));
3635 sv_setpv(sv, pwent->pw_gecos);
3636 PUSHs(sv = sv_mortalcopy(&sv_no));
3637 sv_setpv(sv, pwent->pw_dir);
3638 PUSHs(sv = sv_mortalcopy(&sv_no));
3639 sv_setpv(sv, pwent->pw_shell);
3641 PUSHs(sv = sv_mortalcopy(&sv_no));
3642 sv_setiv(sv, (I32)pwent->pw_expire);
3647 DIE(no_func, "getpwent");
3658 DIE(no_func, "setpwent");
3669 DIE(no_func, "endpwent");
3676 return pp_ggrent(ARGS);
3678 DIE(no_func, "getgrnam");
3685 return pp_ggrent(ARGS);
3687 DIE(no_func, "getgrgid");
3695 I32 which = op->op_type;
3696 register char **elem;
3698 struct group *grent;
3700 if (which == OP_GGRNAM)
3701 grent = (struct group *)getgrnam(POPp);
3702 else if (which == OP_GGRGID)
3703 grent = (struct group *)getgrgid(POPi);
3705 grent = (struct group *)getgrent();
3708 if (GIMME != G_ARRAY) {
3709 PUSHs(sv = sv_newmortal());
3711 if (which == OP_GGRNAM)
3712 sv_setiv(sv, (I32)grent->gr_gid);
3714 sv_setpv(sv, grent->gr_name);
3720 PUSHs(sv = sv_mortalcopy(&sv_no));
3721 sv_setpv(sv, grent->gr_name);
3722 PUSHs(sv = sv_mortalcopy(&sv_no));
3723 sv_setpv(sv, grent->gr_passwd);
3724 PUSHs(sv = sv_mortalcopy(&sv_no));
3725 sv_setiv(sv, (I32)grent->gr_gid);
3726 PUSHs(sv = sv_mortalcopy(&sv_no));
3727 for (elem = grent->gr_mem; *elem; elem++) {
3728 sv_catpv(sv, *elem);
3730 sv_catpvn(sv, " ", 1);
3736 DIE(no_func, "getgrent");
3747 DIE(no_func, "setgrent");
3758 DIE(no_func, "endgrent");
3768 if (!(tmps = getlogin()))
3770 PUSHp(tmps, strlen(tmps));
3773 DIE(no_func, "getlogin");
3777 /* Miscellaneous. */
3782 dSP; dMARK; dORIGMARK; dTARGET;
3783 register I32 items = SP - MARK;
3784 unsigned long a[20];
3790 while (++MARK <= SP) {
3791 if (SvGMAGICAL(*MARK) && SvSMAGICAL(*MARK) &&
3792 (mg = mg_find(*MARK, 't')) && mg->mg_len & 1)
3796 TAINT_PROPER("syscall");
3799 /* This probably won't work on machines where sizeof(long) != sizeof(int)
3800 * or where sizeof(long) != sizeof(char*). But such machines will
3801 * not likely have syscall implemented either, so who cares?
3803 while (++MARK <= SP) {
3804 if (SvNIOK(*MARK) || !i)
3805 a[i++] = SvIV(*MARK);
3806 else if (*MARK == &sv_undef)
3809 a[i++] = (unsigned long)SvPV_force(*MARK, na);
3815 DIE("Too many args to syscall");
3817 DIE("Too few args to syscall");
3819 retval = syscall(a[0]);
3822 retval = syscall(a[0],a[1]);
3825 retval = syscall(a[0],a[1],a[2]);
3828 retval = syscall(a[0],a[1],a[2],a[3]);
3831 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
3834 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
3837 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
3840 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
3844 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
3847 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
3850 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3854 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3858 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3862 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3863 a[10],a[11],a[12],a[13]);
3865 #endif /* atarist */
3871 DIE(no_func, "syscall");
3875 #if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
3877 /* XXX Emulate flock() with lockf(). This is just to increase
3878 portability of scripts. The calls are not completely
3879 interchangeable. What's really needed is a good file
3883 /* We might need <unistd.h> because it sometimes defines the lockf()
3884 constants. Unfortunately, <unistd.h> causes troubles on some mixed
3885 (BSD/POSIX) systems, such as SunOS 4.1.3. We could just try including
3886 <unistd.h> here in this part of the file, but that might
3887 conflict with various other #defines and includes above, such as
3888 #define vfork fork above.
3890 Further, the lockf() constants aren't POSIX, so they might not be
3891 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
3892 just stick in the SVID values and be done with it. Sigh.
3896 # define F_ULOCK 0 /* Unlock a previously locked region */
3899 # define F_LOCK 1 /* Lock a region for exclusive use */
3902 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
3905 # define F_TEST 3 /* Test a region for other processes locks */
3908 /* These are the flock() constants. Since this sytems doesn't have
3909 flock(), the values of the constants are probably not available.
3925 lockf_emulate_flock (fd, operation)
3930 switch (operation) {
3932 /* LOCK_SH - get a shared lock */
3934 /* LOCK_EX - get an exclusive lock */
3936 i = lockf (fd, F_LOCK, 0);
3939 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
3940 case LOCK_SH|LOCK_NB:
3941 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
3942 case LOCK_EX|LOCK_NB:
3943 i = lockf (fd, F_TLOCK, 0);
3945 if ((errno == EAGAIN) || (errno == EACCES))
3946 errno = EWOULDBLOCK;
3949 /* LOCK_UN - unlock */
3951 i = lockf (fd, F_ULOCK, 0);
3954 /* Default - can't decipher operation */