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 /* Omit this -- it causes too much grief on mixed systems.
26 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
27 # include <sys/socket.h>
31 # include <net/errno.h>
39 #include <sys/select.h>
52 struct passwd *getpwnam _((char *));
53 struct passwd *getpwuid _((Uid_t));
55 struct passwd *getpwent _((void));
62 struct group *getgrnam _((char *));
63 struct group *getgrgid _((Gid_t));
65 struct group *getgrent _((void));
79 # define getpgrp getpgrp2
83 # define setpgrp setpgrp2
86 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
87 static int dooneliner _((char *cmd, char *filename));
97 fp = my_popen(tmps, "r");
99 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
100 if (GIMME == G_SCALAR) {
101 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
111 if (sv_gets(sv, fp, 0) == Nullch) {
115 XPUSHs(sv_2mortal(sv));
116 if (SvLEN(sv) - SvCUR(sv) > 20) {
117 SvLEN_set(sv, SvCUR(sv)+1);
118 Renew(SvPVX(sv), SvLEN(sv), char);
122 statusvalue = my_pclose(fp);
126 if (GIMME == G_SCALAR)
140 SAVESPTR(last_in_gv); /* We don't want this to be permanent. */
141 last_in_gv = (GV*)*stack_sp--;
153 result = do_readline();
160 last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*stack_sp--)), na), TRUE,SVt_PVIO);
161 return do_readline();
166 last_in_gv = cGVOP->op_gv;
167 return do_readline();
174 if (SP - MARK != 1) {
176 do_join(TARG, &sv_no, MARK, SP);
177 tmps = SvPV(TARG, na);
181 tmps = SvPV(TOPs, na);
183 if (!tmps || !*tmps) {
184 SV *error = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
185 (void)SvUPGRADE(error, SVt_PV);
186 if (SvPOK(error) && SvCUR(error))
187 sv_catpv(error, "\t...caught");
188 tmps = SvPV(error, na);
191 tmps = "Warning: something's wrong";
200 if (SP - MARK != 1) {
202 do_join(TARG, &sv_no, MARK, SP);
203 tmps = SvPV(TARG, na);
207 tmps = SvPV(TOPs, na);
209 if (!tmps || !*tmps) {
210 SV *error = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
211 (void)SvUPGRADE(error, SVt_PV);
212 if (SvPOK(error) && SvCUR(error))
213 sv_catpv(error, "\t...propagated");
214 tmps = SvPV(error, na);
236 tmps = SvPV(sv, len);
237 if (do_open(gv, tmps, len,Nullfp)) {
238 IoLINES(GvIOp(gv)) = 0;
239 PUSHi( (I32)forkprocess );
241 else if (forkprocess == 0) /* we are a new child */
258 PUSHs( do_close(gv, TRUE) ? &sv_yes : &sv_no );
282 do_close(rgv, FALSE);
284 do_close(wgv, FALSE);
289 IoIFP(rstio) = fdopen(fd[0], "r");
290 IoOFP(wstio) = fdopen(fd[1], "w");
291 IoIFP(wstio) = IoOFP(wstio);
295 if (!IoIFP(rstio) || !IoOFP(wstio)) {
296 if (IoIFP(rstio)) fclose(IoIFP(rstio));
298 if (IoOFP(wstio)) fclose(IoOFP(wstio));
308 DIE(no_func, "pipe");
321 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io)))
339 TAINT_PROPER("umask");
342 DIE(no_func, "Unsupported function umask");
360 if (!(io = GvIO(gv)) || !(fp = IoIFP(io)))
365 if (!fflush(fp) && (fp->_flag |= _IOBIN))
370 if (setmode(fileno(fp), OP_BINARY) != -1)
388 SV **mark = stack_base + ++*markstack_ptr; /* reuse in entersub */
389 I32 markoff = mark - stack_base - 1;
393 if (SvTYPE(varsv) == SVt_PVHV)
394 methname = "TIEHASH";
395 else if (SvTYPE(varsv) == SVt_PVAV)
396 methname = "TIEARRAY";
397 else if (SvTYPE(varsv) == SVt_PVGV)
398 methname = "TIEHANDLE";
400 methname = "TIESCALAR";
402 stash = gv_stashsv(mark[1], FALSE);
403 if (!stash || !(gv = gv_fetchmethod(stash, methname)) || !GvCV(gv))
404 DIE("Can't locate object method \"%s\" via package \"%s\"",
405 methname, SvPV(mark[1],na));
407 Zero(&myop, 1, BINOP);
408 myop.op_last = (OP *) &myop;
409 myop.op_next = Nullop;
410 myop.op_flags = OPf_KNOW|OPf_STACKED;
419 if (op = pp_entersub())
424 if (sv_isobject(sv)) {
425 if (SvTYPE(varsv) == SVt_PVHV || SvTYPE(varsv) == SVt_PVAV) {
426 sv_unmagic(varsv, 'P');
427 sv_magic(varsv, sv, 'P', Nullch, 0);
430 sv_unmagic(varsv, 'q');
431 sv_magic(varsv, sv, 'q', Nullch, 0);
435 SP = stack_base + markoff;
443 if (SvTYPE(TOPs) == SVt_PVHV || SvTYPE(TOPs) == SVt_PVAV)
444 sv_unmagic(TOPs, 'P');
446 sv_unmagic(TOPs, 'q');
462 sv = sv_mortalcopy(&sv_no);
463 sv_setpv(sv, "AnyDBM_File");
464 stash = gv_stashsv(sv, FALSE);
465 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")) || !GvCV(gv)) {
467 perl_requirepv("AnyDBM_File.pm");
469 if (!(gv = gv_fetchmethod(stash, "TIEHASH")) || !GvCV(gv))
470 DIE("No dbm on this machine");
473 Zero(&myop, 1, BINOP);
474 myop.op_last = (OP *) &myop;
475 myop.op_next = Nullop;
476 myop.op_flags = OPf_KNOW|OPf_STACKED;
488 PUSHs(sv_2mortal(newSViv(O_RDWR|O_CREAT)));
490 PUSHs(sv_2mortal(newSViv(O_RDWR)));
495 if (op = pp_entersub())
499 if (!sv_isobject(TOPs)) {
507 PUSHs(sv_2mortal(newSViv(O_RDONLY)));
512 if (op = pp_entersub())
517 if (sv_isobject(TOPs))
518 sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
525 return pp_untie(ARGS);
539 struct timeval timebuf;
540 struct timeval *tbuf = &timebuf;
543 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
548 # if BYTEORDER & 0xf0000
549 # define ORDERBYTE (0x88888888 - BYTEORDER)
551 # define ORDERBYTE (0x4444 - BYTEORDER)
557 for (i = 1; i <= 3; i++) {
565 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
566 growsize = maxlen; /* little endians can use vecs directly */
574 masksize = NFDBITS / NBBY;
576 masksize = sizeof(long); /* documented int, everyone seems to use long */
578 growsize = maxlen + (masksize - (maxlen % masksize));
579 Zero(&fd_sets[0], 4, char*);
587 timebuf.tv_sec = (long)value;
588 value -= (double)timebuf.tv_sec;
589 timebuf.tv_usec = (long)(value * 1000000.0);
592 tbuf = Null(struct timeval*);
594 for (i = 1; i <= 3; i++) {
601 SvPV_force(sv,na); /* force string conversion */
604 Sv_Grow(sv, growsize);
606 while (++j <= growsize) {
610 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
612 New(403, fd_sets[i], growsize, char);
613 for (offset = 0; offset < growsize; offset += masksize) {
614 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
615 fd_sets[i][j+offset] = s[(k % masksize) + offset];
618 fd_sets[i] = SvPVX(sv);
624 (Select_fd_set_t) fd_sets[1],
625 (Select_fd_set_t) fd_sets[2],
626 (Select_fd_set_t) fd_sets[3],
628 for (i = 1; i <= 3; i++) {
631 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
633 for (offset = 0; offset < growsize; offset += masksize) {
634 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
635 s[(k % masksize) + offset] = fd_sets[i][j+offset];
637 Safefree(fd_sets[i]);
644 if (GIMME == G_ARRAY && tbuf) {
645 value = (double)(timebuf.tv_sec) +
646 (double)(timebuf.tv_usec) / 1000000.0;
647 PUSHs(sv = sv_mortalcopy(&sv_no));
652 DIE("select not implemented");
659 GV *oldgv = defoutgv;
660 if (op->op_private > 0) {
661 defoutgv = (GV*)POPs;
665 gv_efullname(TARG, oldgv);
681 if (!gv || do_eof(gv)) /* make sure we have fp with something */
685 *SvPVX(TARG) = getc(IoIFP(GvIOp(gv))); /* should never be EOF */
692 return pp_sysread(ARGS);
701 register CONTEXT *cx;
703 AV* padlist = CvPADLIST(cv);
704 SV** svp = AvARRAY(padlist);
710 PUSHBLOCK(cx, CXt_SUB, stack_sp);
713 curpad = AvARRAY((AV*)svp[1]);
715 defoutgv = gv; /* locally select filehandle so $% et al work */
748 SV *tmpstr = sv_newmortal();
749 gv_efullname(tmpstr, gv);
750 DIE("Undefined format \"%s\" called",SvPVX(tmpstr));
752 DIE("Not a format reference");
755 return doform(cv,gv,op->op_next);
761 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
762 register IO *io = GvIOp(gv);
763 FILE *ofp = IoOFP(io);
767 register CONTEXT *cx;
769 DEBUG_f(fprintf(stderr,"left=%ld, todo=%ld\n",
770 (long)IoLINES_LEFT(io), (long)FmLINES(formtarget)));
771 if (IoLINES_LEFT(io) < FmLINES(formtarget) &&
772 formtarget != toptarget)
778 if (!IoTOP_NAME(io)) {
780 IoFMT_NAME(io) = savepv(GvNAME(gv));
781 sprintf(tmpbuf, "%s_TOP", IoFMT_NAME(io));
782 topgv = gv_fetchpv(tmpbuf,FALSE, SVt_PVFM);
783 if ((topgv && GvFORM(topgv)) ||
784 !gv_fetchpv("top",FALSE,SVt_PVFM))
785 IoTOP_NAME(io) = savepv(tmpbuf);
787 IoTOP_NAME(io) = savepv("top");
789 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
790 if (!topgv || !GvFORM(topgv)) {
791 IoLINES_LEFT(io) = 100000000;
794 IoTOP_GV(io) = topgv;
796 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
797 fwrite1(SvPVX(formfeed), SvCUR(formfeed), 1, ofp);
798 IoLINES_LEFT(io) = IoPAGE_LEN(io);
800 formtarget = toptarget;
801 return doform(GvFORM(IoTOP_GV(io)),gv,op);
813 warn("Filehandle only opened for input");
815 warn("Write on closed filehandle");
820 if ((IoLINES_LEFT(io) -= FmLINES(formtarget)) < 0) {
822 warn("page overflow");
824 if (!fwrite1(SvPVX(formtarget), 1, SvCUR(formtarget), ofp) ||
828 FmLINES(formtarget) = 0;
829 SvCUR_set(formtarget, 0);
830 if (IoFLAGS(io) & IOf_FLUSH)
835 formtarget = bodytarget;
842 dSP; dMARK; dORIGMARK;
848 if (op->op_flags & OPf_STACKED)
852 if (!(io = GvIO(gv))) {
854 warn("Filehandle %s never opened", GvNAME(gv));
858 else if (!(fp = IoOFP(io))) {
861 warn("Filehandle %s opened only for input", GvNAME(gv));
863 warn("printf on closed filehandle %s", GvNAME(gv));
869 do_sprintf(sv, SP - MARK, MARK + 1);
870 if (!do_print(sv, fp))
873 if (IoFLAGS(io) & IOf_FLUSH)
874 if (fflush(fp) == EOF)
891 dSP; dMARK; dORIGMARK; dTARGET;
905 buffer = SvPV_force(bufstr, blen);
906 length = SvIVx(*++MARK);
908 DIE("Negative length");
911 offset = SvIVx(*++MARK);
915 if (!io || !IoIFP(io))
918 if (op->op_type == OP_RECV) {
919 bufsize = sizeof buf;
920 buffer = SvGROW(bufstr, length+1);
921 length = recvfrom(fileno(IoIFP(io)), buffer, length, offset,
922 (struct sockaddr *)buf, &bufsize);
925 SvCUR_set(bufstr, length);
926 *SvEND(bufstr) = '\0';
927 (void)SvPOK_only(bufstr);
930 sv_magic(bufstr, Nullsv, 't', Nullch, 0);
932 sv_setpvn(TARG, buf, bufsize);
937 if (op->op_type == OP_RECV)
938 DIE(no_sock_func, "recv");
940 buffer = SvGROW(bufstr, length+offset+1);
941 if (op->op_type == OP_SYSREAD) {
942 length = read(fileno(IoIFP(io)), buffer+offset, length);
945 #ifdef HAS_SOCKET__bad_code_maybe
946 if (IoTYPE(io) == 's') {
947 bufsize = sizeof buf;
948 length = recvfrom(fileno(IoIFP(io)), buffer+offset, length, 0,
949 (struct sockaddr *)buf, &bufsize);
953 length = fread(buffer+offset, 1, length, IoIFP(io));
956 SvCUR_set(bufstr, length+offset);
957 *SvEND(bufstr) = '\0';
958 (void)SvPOK_only(bufstr);
961 sv_magic(bufstr, Nullsv, 't', Nullch, 0);
973 return pp_send(ARGS);
978 dSP; dMARK; dORIGMARK; dTARGET;
991 buffer = SvPV(bufstr, blen);
992 length = SvIVx(*++MARK);
994 DIE("Negative length");
997 if (!io || !IoIFP(io)) {
1000 if (op->op_type == OP_SYSWRITE)
1001 warn("Syswrite on closed filehandle");
1003 warn("Send on closed socket");
1006 else if (op->op_type == OP_SYSWRITE) {
1008 offset = SvIVx(*++MARK);
1011 if (length > blen - offset)
1012 length = blen - offset;
1013 length = write(fileno(IoIFP(io)), buffer+offset, length);
1016 else if (SP > MARK) {
1019 sockbuf = SvPVx(*++MARK, mlen);
1020 length = sendto(fileno(IoIFP(io)), buffer, blen, length,
1021 (struct sockaddr *)sockbuf, mlen);
1024 length = send(fileno(IoIFP(io)), buffer, blen, length);
1027 DIE(no_sock_func, "send");
1042 return pp_sysread(ARGS);
1053 gv = last_in_gv = (GV*)POPs;
1054 PUSHs(!gv || do_eof(gv) ? &sv_yes : &sv_no);
1066 gv = last_in_gv = (GV*)POPs;
1067 PUSHi( do_tell(gv) );
1078 gv = last_in_gv = (GV*)POPs;
1079 PUSHs( do_seek(gv, offset, whence) ? &sv_yes : &sv_no );
1086 Off_t len = (Off_t)POPn;
1091 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE)
1093 if (op->op_flags & OPf_SPECIAL) {
1094 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1095 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1096 ftruncate(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1099 else if (truncate(POPp, len) < 0)
1102 if (op->op_flags & OPf_SPECIAL) {
1103 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1104 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1105 chsize(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1111 if ((tmpfd = open(POPp, 0)) < 0)
1114 if (chsize(tmpfd, len) < 0)
1127 DIE("truncate not implemented");
1133 return pp_ioctl(ARGS);
1140 unsigned int func = U_I(POPn);
1141 int optype = op->op_type;
1147 if (!io || !argstr || !IoIFP(io)) {
1148 errno = EBADF; /* well, sort of... */
1152 if (SvPOK(argstr) || !SvNIOK(argstr)) {
1154 s = SvPV_force(argstr, len);
1155 retval = IOCPARM_LEN(func);
1157 s = Sv_Grow(argstr, retval+1);
1158 SvCUR_set(argstr, retval);
1161 s[SvCUR(argstr)] = 17; /* a little sanity check here */
1164 retval = SvIV(argstr);
1166 s = (char*)(long)retval; /* ouch */
1168 s = (char*)retval; /* ouch */
1172 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1174 if (optype == OP_IOCTL)
1176 retval = ioctl(fileno(IoIFP(io)), func, s);
1178 DIE("ioctl is not implemented");
1182 DIE("fcntl is not implemented");
1185 retval = fcntl(fileno(IoIFP(io)), func, s);
1187 DIE("fcntl is not implemented");
1191 if (SvPOK(argstr)) {
1192 if (s[SvCUR(argstr)] != 17)
1193 DIE("Possible memory corruption: %s overflowed 3rd argument",
1195 s[SvCUR(argstr)] = 0; /* put our null back */
1196 SvSETMAGIC(argstr); /* Assume it has changed */
1205 PUSHp("0 but true", 10);
1224 fp = IoIFP(GvIOp(gv));
1228 value = (I32)(flock(fileno(fp), argtype) >= 0);
1236 DIE(no_func, "flock()"); /* XXX emulate flock() with lockf()? */
1238 DIE(no_func, "flock()");
1251 int protocol = POPi;
1265 do_close(gv, FALSE);
1267 TAINT_PROPER("socket");
1268 fd = socket(domain, type, protocol);
1271 IoIFP(io) = fdopen(fd, "r"); /* stdio gets confused about sockets */
1272 IoOFP(io) = fdopen(fd, "w");
1274 if (!IoIFP(io) || !IoOFP(io)) {
1275 if (IoIFP(io)) fclose(IoIFP(io));
1276 if (IoOFP(io)) fclose(IoOFP(io));
1277 if (!IoIFP(io) && !IoOFP(io)) close(fd);
1283 DIE(no_sock_func, "socket");
1290 #ifdef HAS_SOCKETPAIR
1295 int protocol = POPi;
1308 do_close(gv1, FALSE);
1310 do_close(gv2, FALSE);
1312 TAINT_PROPER("socketpair");
1313 if (socketpair(domain, type, protocol, fd) < 0)
1315 IoIFP(io1) = fdopen(fd[0], "r");
1316 IoOFP(io1) = fdopen(fd[0], "w");
1318 IoIFP(io2) = fdopen(fd[1], "r");
1319 IoOFP(io2) = fdopen(fd[1], "w");
1321 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
1322 if (IoIFP(io1)) fclose(IoIFP(io1));
1323 if (IoOFP(io1)) fclose(IoOFP(io1));
1324 if (!IoIFP(io1) && !IoOFP(io1)) close(fd[0]);
1325 if (IoIFP(io2)) fclose(IoIFP(io2));
1326 if (IoOFP(io2)) fclose(IoOFP(io2));
1327 if (!IoIFP(io2) && !IoOFP(io2)) close(fd[1]);
1333 DIE(no_sock_func, "socketpair");
1344 register IO *io = GvIOn(gv);
1347 if (!io || !IoIFP(io))
1350 addr = SvPV(addrstr, len);
1351 TAINT_PROPER("bind");
1352 if (bind(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1359 warn("bind() on closed fd");
1363 DIE(no_sock_func, "bind");
1374 register IO *io = GvIOn(gv);
1377 if (!io || !IoIFP(io))
1380 addr = SvPV(addrstr, len);
1381 TAINT_PROPER("connect");
1382 if (connect(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1389 warn("connect() on closed fd");
1393 DIE(no_sock_func, "connect");
1403 register IO *io = GvIOn(gv);
1405 if (!io || !IoIFP(io))
1408 if (listen(fileno(IoIFP(io)), backlog) >= 0)
1415 warn("listen() on closed fd");
1419 DIE(no_sock_func, "listen");
1431 int len = sizeof buf;
1443 if (!gstio || !IoIFP(gstio))
1448 do_close(ngv, FALSE);
1450 fd = accept(fileno(IoIFP(gstio)), (struct sockaddr *)buf, &len);
1453 IoIFP(nstio) = fdopen(fd, "r");
1454 IoOFP(nstio) = fdopen(fd, "w");
1455 IoTYPE(nstio) = 's';
1456 if (!IoIFP(nstio) || !IoOFP(nstio)) {
1457 if (IoIFP(nstio)) fclose(IoIFP(nstio));
1458 if (IoOFP(nstio)) fclose(IoOFP(nstio));
1459 if (!IoIFP(nstio) && !IoOFP(nstio)) close(fd);
1468 warn("accept() on closed fd");
1475 DIE(no_sock_func, "accept");
1485 register IO *io = GvIOn(gv);
1487 if (!io || !IoIFP(io))
1490 PUSHi( shutdown(fileno(IoIFP(io)), how) >= 0 );
1495 warn("shutdown() on closed fd");
1499 DIE(no_sock_func, "shutdown");
1506 return pp_ssockopt(ARGS);
1508 DIE(no_sock_func, "getsockopt");
1516 int optype = op->op_type;
1519 unsigned int optname;
1524 if (optype == OP_GSOCKOPT)
1525 sv = sv_2mortal(NEWSV(22, 257));
1528 optname = (unsigned int) POPi;
1529 lvl = (unsigned int) POPi;
1533 if (!io || !IoIFP(io))
1536 fd = fileno(IoIFP(io));
1540 (void)SvPOK_only(sv);
1541 if (getsockopt(fd, lvl, optname, SvPVX(sv), (int*)&SvCUR(sv)) < 0)
1550 buf = SvPV(sv, len);
1551 else if (SvOK(sv)) {
1552 aint = (int)SvIV(sv);
1556 if (setsockopt(fd, lvl, optname, buf, (int)len) < 0)
1566 warn("[gs]etsockopt() on closed fd");
1572 DIE(no_sock_func, "setsockopt");
1579 return pp_getpeername(ARGS);
1581 DIE(no_sock_func, "getsockname");
1589 int optype = op->op_type;
1593 register IO *io = GvIOn(gv);
1595 if (!io || !IoIFP(io))
1598 sv = sv_2mortal(NEWSV(22, 257));
1601 fd = fileno(IoIFP(io));
1603 case OP_GETSOCKNAME:
1604 if (getsockname(fd, (struct sockaddr *)SvPVX(sv), (int*)&SvCUR(sv)) < 0)
1607 case OP_GETPEERNAME:
1608 if (getpeername(fd, (struct sockaddr *)SvPVX(sv), (int*)&SvCUR(sv)) < 0)
1617 warn("get{sock, peer}name() on closed fd");
1623 DIE(no_sock_func, "getpeername");
1631 return pp_stat(ARGS);
1640 if (op->op_flags & OPf_REF) {
1641 tmpgv = cGVOP->op_gv;
1642 if (tmpgv != defgv) {
1643 laststype = OP_STAT;
1645 sv_setpv(statname, "");
1646 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1647 Fstat(fileno(IoIFP(GvIOn(tmpgv))), &statcache) < 0) {
1652 else if (laststatval < 0)
1656 sv_setpv(statname, POPp);
1659 laststype = op->op_type;
1660 if (op->op_type == OP_LSTAT)
1661 laststatval = lstat(SvPV(statname, na), &statcache);
1664 laststatval = Stat(SvPV(statname, na), &statcache);
1665 if (laststatval < 0) {
1666 if (dowarn && strchr(SvPV(statname, na), '\n'))
1667 warn(warn_nl, "stat");
1673 if (GIMME != G_ARRAY) {
1680 PUSHs(sv_2mortal(newSViv((I32)statcache.st_dev)));
1681 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ino)));
1682 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mode)));
1683 PUSHs(sv_2mortal(newSViv((I32)statcache.st_nlink)));
1684 PUSHs(sv_2mortal(newSViv((I32)statcache.st_uid)));
1685 PUSHs(sv_2mortal(newSViv((I32)statcache.st_gid)));
1686 PUSHs(sv_2mortal(newSViv((I32)statcache.st_rdev)));
1687 PUSHs(sv_2mortal(newSViv((I32)statcache.st_size)));
1688 PUSHs(sv_2mortal(newSViv((I32)statcache.st_atime)));
1689 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mtime)));
1690 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ctime)));
1691 #ifdef USE_STAT_BLOCKS
1692 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blksize)));
1693 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blocks)));
1695 PUSHs(sv_2mortal(newSVpv("", 0)));
1696 PUSHs(sv_2mortal(newSVpv("", 0)));
1704 I32 result = my_stat(ARGS);
1708 if (cando(S_IRUSR, 0, &statcache))
1715 I32 result = my_stat(ARGS);
1719 if (cando(S_IWUSR, 0, &statcache))
1726 I32 result = my_stat(ARGS);
1730 if (cando(S_IXUSR, 0, &statcache))
1737 I32 result = my_stat(ARGS);
1741 if (cando(S_IRUSR, 1, &statcache))
1748 I32 result = my_stat(ARGS);
1752 if (cando(S_IWUSR, 1, &statcache))
1759 I32 result = my_stat(ARGS);
1763 if (cando(S_IXUSR, 1, &statcache))
1770 I32 result = my_stat(ARGS);
1779 return pp_ftrowned(ARGS);
1784 I32 result = my_stat(ARGS);
1788 if (statcache.st_uid == (op->op_type == OP_FTEOWNED ? euid : uid) )
1795 I32 result = my_stat(ARGS);
1799 if (!statcache.st_size)
1806 I32 result = my_stat(ARGS);
1810 PUSHi(statcache.st_size);
1816 I32 result = my_stat(ARGS);
1820 PUSHn( (basetime - statcache.st_mtime) / 86400.0 );
1826 I32 result = my_stat(ARGS);
1830 PUSHn( (basetime - statcache.st_atime) / 86400.0 );
1836 I32 result = my_stat(ARGS);
1840 PUSHn( (basetime - statcache.st_ctime) / 86400.0 );
1846 I32 result = my_stat(ARGS);
1850 if (S_ISSOCK(statcache.st_mode))
1857 I32 result = my_stat(ARGS);
1861 if (S_ISCHR(statcache.st_mode))
1868 I32 result = my_stat(ARGS);
1872 if (S_ISBLK(statcache.st_mode))
1879 I32 result = my_stat(ARGS);
1883 if (S_ISREG(statcache.st_mode))
1890 I32 result = my_stat(ARGS);
1894 if (S_ISDIR(statcache.st_mode))
1901 I32 result = my_stat(ARGS);
1905 if (S_ISFIFO(statcache.st_mode))
1912 I32 result = my_lstat(ARGS);
1916 if (S_ISLNK(statcache.st_mode))
1925 I32 result = my_stat(ARGS);
1929 if (statcache.st_mode & S_ISUID)
1939 I32 result = my_stat(ARGS);
1943 if (statcache.st_mode & S_ISGID)
1953 I32 result = my_stat(ARGS);
1957 if (statcache.st_mode & S_ISVTX)
1969 if (op->op_flags & OPf_REF) {
1974 gv = gv_fetchpv(tmps = POPp, FALSE, SVt_PVIO);
1975 if (GvIO(gv) && IoIFP(GvIOp(gv)))
1976 fd = fileno(IoIFP(GvIOp(gv)));
1977 else if (isDIGIT(*tmps))
1986 #if defined(USE_STD_STDIO) || defined(atarist) /* this will work with atariST */
1987 # define FBASE(f) ((f)->_base)
1988 # define FSIZE(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
1989 # define FPTR(f) ((f)->_ptr)
1990 # define FCOUNT(f) ((f)->_cnt)
1992 # if defined(USE_LINUX_STDIO)
1993 # define FBASE(f) ((f)->_IO_read_base)
1994 # define FSIZE(f) ((f)->_IO_read_end - FBASE(f))
1995 # define FPTR(f) ((f)->_IO_read_ptr)
1996 # define FCOUNT(f) ((f)->_IO_read_end - FPTR(f))
2007 register STDCHAR *s;
2011 if (op->op_flags & OPf_REF) {
2013 if (cGVOP->op_gv == defgv) {
2018 goto really_filename;
2022 statgv = cGVOP->op_gv;
2023 sv_setpv(statname, "");
2026 if (io && IoIFP(io)) {
2028 Fstat(fileno(IoIFP(io)), &statcache);
2029 if (S_ISDIR(statcache.st_mode)) /* handle NFS glitch */
2030 if (op->op_type == OP_FTTEXT)
2034 if (FCOUNT(IoIFP(io)) <= 0) {
2035 i = getc(IoIFP(io));
2037 (void)ungetc(i, IoIFP(io));
2039 if (FCOUNT(IoIFP(io)) <= 0) /* null file is anything */
2041 len = FSIZE(IoIFP(io));
2042 s = FBASE(IoIFP(io));
2044 DIE("-T and -B not implemented on filehandles");
2049 warn("Test on unopened file <%s>",
2050 GvENAME(cGVOP->op_gv));
2058 sv_setpv(statname, SvPV(sv, na));
2061 i = open(SvPV(sv, na), O_RDONLY, 0);
2063 i = open(SvPV(sv, na), 0);
2066 if (dowarn && strchr(SvPV(sv, na), '\n'))
2067 warn(warn_nl, "open");
2070 Fstat(i, &statcache);
2071 len = read(i, tbuf, 512);
2074 if (S_ISDIR(statcache.st_mode) && op->op_type == OP_FTTEXT)
2075 RETPUSHNO; /* special case NFS directories */
2076 RETPUSHYES; /* null file is anything */
2081 /* now scan s to look for textiness */
2083 for (i = 0; i < len; i++, s++) {
2084 if (!*s) { /* null never allowed in text */
2091 *s != '\n' && *s != '\r' && *s != '\b' &&
2092 *s != '\t' && *s != '\f' && *s != 27)
2096 if ((odd * 30 > len) == (op->op_type == OP_FTTEXT)) /* allow 30% odd */
2104 return pp_fttext(ARGS);
2119 if (!tmps || !*tmps) {
2120 svp = hv_fetch(GvHVn(envgv), "HOME", 4, FALSE);
2122 tmps = SvPV(*svp, na);
2124 if (!tmps || !*tmps) {
2125 svp = hv_fetch(GvHVn(envgv), "LOGDIR", 6, FALSE);
2127 tmps = SvPV(*svp, na);
2129 TAINT_PROPER("chdir");
2130 PUSHi( chdir(tmps) >= 0 );
2136 dSP; dMARK; dTARGET;
2139 value = (I32)apply(op->op_type, MARK, SP);
2144 DIE(no_func, "Unsupported function chown");
2154 TAINT_PROPER("chroot");
2155 PUSHi( chroot(tmps) >= 0 );
2158 DIE(no_func, "chroot");
2164 dSP; dMARK; dTARGET;
2166 value = (I32)apply(op->op_type, MARK, SP);
2174 dSP; dMARK; dTARGET;
2176 value = (I32)apply(op->op_type, MARK, SP);
2184 dSP; dMARK; dTARGET;
2186 value = (I32)apply(op->op_type, MARK, SP);
2198 char *tmps = SvPV(TOPs, na);
2199 TAINT_PROPER("rename");
2201 anum = rename(tmps, tmps2);
2203 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
2206 if (euid || Stat(tmps2, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
2207 (void)UNLINK(tmps2);
2208 if (!(anum = link(tmps, tmps2)))
2209 anum = UNLINK(tmps);
2221 char *tmps = SvPV(TOPs, na);
2222 TAINT_PROPER("link");
2223 SETi( link(tmps, tmps2) >= 0 );
2225 DIE(no_func, "Unsupported function link");
2235 char *tmps = SvPV(TOPs, na);
2236 TAINT_PROPER("symlink");
2237 SETi( symlink(tmps, tmps2) >= 0 );
2240 DIE(no_func, "symlink");
2251 len = readlink(tmps, buf, sizeof buf);
2259 RETSETUNDEF; /* just pretend it's a normal file */
2263 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
2265 dooneliner(cmd, filename)
2276 for (s = mybuf+strlen(mybuf); *filename; ) {
2281 myfp = my_popen(mybuf, "r");
2284 s = fgets(mybuf, sizeof mybuf, myfp);
2285 (void)my_pclose(myfp);
2287 for (errno = 1; errno < sys_nerr; errno++) {
2288 #ifdef HAS_SYS_ERRLIST
2289 if (instr(mybuf, sys_errlist[errno])) /* you don't see this */
2292 char *errmsg; /* especially if it isn't there */
2295 (errmsg = strerror(errno)) ? errmsg : "NoErRoR"))
2301 #define EACCES EPERM
2303 if (instr(mybuf, "cannot make"))
2305 else if (instr(mybuf, "existing file"))
2307 else if (instr(mybuf, "ile exists"))
2309 else if (instr(mybuf, "non-exist"))
2311 else if (instr(mybuf, "does not exist"))
2313 else if (instr(mybuf, "not empty"))
2315 else if (instr(mybuf, "cannot access"))
2321 else { /* some mkdirs return no failure indication */
2322 anum = (Stat(filename, &statbuf) >= 0);
2323 if (op->op_type == OP_RMDIR)
2328 errno = EACCES; /* a guess */
2344 char *tmps = SvPV(TOPs, na);
2346 TAINT_PROPER("mkdir");
2348 SETi( mkdir(tmps, mode) >= 0 );
2350 SETi( dooneliner("mkdir", tmps) );
2351 oldumask = umask(0);
2353 chmod(tmps, (mode & ~oldumask) & 0777);
2364 TAINT_PROPER("rmdir");
2366 XPUSHi( rmdir(tmps) >= 0 );
2368 XPUSHi( dooneliner("rmdir", tmps) );
2373 /* Directory calls. */
2378 #if defined(Direntry_t) && defined(HAS_READDIR)
2379 char *dirname = POPp;
2381 register IO *io = GvIOn(gv);
2387 closedir(IoDIRP(io));
2388 if (!(IoDIRP(io) = opendir(dirname)))
2397 DIE(no_dir_func, "opendir");
2404 #if defined(Direntry_t) && defined(HAS_READDIR)
2406 Direntry_t *readdir _((DIR *));
2408 register Direntry_t *dp;
2410 register IO *io = GvIOn(gv);
2412 if (!io || !IoDIRP(io))
2415 if (GIMME == G_ARRAY) {
2417 while (dp = (Direntry_t *)readdir(IoDIRP(io))) {
2419 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2421 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2426 if (!(dp = (Direntry_t *)readdir(IoDIRP(io))))
2429 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2431 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2439 if (GIMME == G_ARRAY)
2444 DIE(no_dir_func, "readdir");
2451 #if defined(HAS_TELLDIR) || defined(telldir)
2452 #if !defined(telldir) && !defined(HAS_TELLDIR_PROTOTYPE)
2453 long telldir _((DIR *));
2456 register IO *io = GvIOn(gv);
2458 if (!io || !IoDIRP(io))
2461 PUSHi( telldir(IoDIRP(io)) );
2468 DIE(no_dir_func, "telldir");
2475 #if defined(HAS_SEEKDIR) || defined(seekdir)
2478 register IO *io = GvIOn(gv);
2480 if (!io || !IoDIRP(io))
2483 (void)seekdir(IoDIRP(io), along);
2491 DIE(no_dir_func, "seekdir");
2498 #if defined(HAS_REWINDDIR) || defined(rewinddir)
2500 register IO *io = GvIOn(gv);
2502 if (!io || !IoDIRP(io))
2505 (void)rewinddir(IoDIRP(io));
2512 DIE(no_dir_func, "rewinddir");
2519 #if defined(Direntry_t) && defined(HAS_READDIR)
2521 register IO *io = GvIOn(gv);
2523 if (!io || !IoDIRP(io))
2526 #ifdef VOID_CLOSEDIR
2527 closedir(IoDIRP(io));
2529 if (closedir(IoDIRP(io)) < 0)
2540 DIE(no_dir_func, "closedir");
2544 /* Process control. */
2559 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
2560 sv_setiv(GvSV(tmpgv), (I32)getpid());
2561 hv_clear(pidstatus); /* no kids, so don't wait for 'em */
2566 DIE(no_func, "Unsupported function fork");
2579 childpid = wait(&argflags);
2581 pidgone(childpid, argflags);
2582 value = (I32)childpid;
2583 statusvalue = (U16)argflags;
2587 DIE(no_func, "Unsupported function wait");
2602 childpid = wait4pid(childpid, &argflags, optype);
2603 value = (I32)childpid;
2604 statusvalue = (U16)argflags;
2608 DIE(no_func, "Unsupported function wait");
2614 dSP; dMARK; dORIGMARK; dTARGET;
2619 Signal_t (*ihand)(); /* place to save signal during system() */
2620 Signal_t (*qhand)(); /* place to save signal during system() */
2622 #if defined(HAS_FORK) && !defined(VMS)
2623 if (SP - MARK == 1) {
2625 char *junk = SvPV(TOPs, na);
2627 TAINT_PROPER("system");
2630 while ((childpid = vfork()) == -1) {
2631 if (errno != EAGAIN) {
2640 ihand = signal(SIGINT, SIG_IGN);
2641 qhand = signal(SIGQUIT, SIG_IGN);
2642 result = wait4pid(childpid, &status, 0);
2643 (void)signal(SIGINT, ihand);
2644 (void)signal(SIGQUIT, qhand);
2645 statusvalue = (U16)status;
2649 value = (I32)((unsigned int)status & 0xffff);
2651 do_execfree(); /* free any memory child malloced on vfork */
2656 if (op->op_flags & OPf_STACKED) {
2657 SV *really = *++MARK;
2658 value = (I32)do_aexec(really, MARK, SP);
2660 else if (SP - MARK != 1)
2661 value = (I32)do_aexec(Nullsv, MARK, SP);
2663 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2666 #else /* ! FORK or VMS */
2667 if (op->op_flags & OPf_STACKED) {
2668 SV *really = *++MARK;
2669 value = (I32)do_aspawn(really, MARK, SP);
2671 else if (SP - MARK != 1)
2672 value = (I32)do_aspawn(Nullsv, MARK, SP);
2674 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), na));
2679 #endif /* !FORK or VMS */
2685 dSP; dMARK; dORIGMARK; dTARGET;
2688 if (op->op_flags & OPf_STACKED) {
2689 SV *really = *++MARK;
2690 value = (I32)do_aexec(really, MARK, SP);
2692 else if (SP - MARK != 1)
2694 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
2696 value = (I32)do_aexec(Nullsv, MARK, SP);
2700 char *junk = SvPV(*SP, na);
2702 TAINT_PROPER("exec");
2705 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), na));
2707 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2717 dSP; dMARK; dTARGET;
2720 value = (I32)apply(op->op_type, MARK, SP);
2725 DIE(no_func, "Unsupported function kill");
2733 XPUSHi( getppid() );
2736 DIE(no_func, "getppid");
2752 value = (I32)getpgrp(pid);
2755 DIE("POSIX getpgrp can't take an argument");
2756 value = (I32)getpgrp();
2761 DIE(no_func, "getpgrp()");
2780 TAINT_PROPER("setpgrp");
2782 SETi( setpgrp(pid, pgrp) >= 0 );
2784 if ((pgrp != 0) || (pid != 0)) {
2785 DIE("POSIX setpgrp can't take an argument");
2787 SETi( setpgrp() >= 0 );
2788 #endif /* USE_BSDPGRP */
2791 DIE(no_func, "setpgrp()");
2800 #ifdef HAS_GETPRIORITY
2803 SETi( getpriority(which, who) );
2806 DIE(no_func, "getpriority()");
2816 #ifdef HAS_SETPRIORITY
2820 TAINT_PROPER("setpriority");
2821 SETi( setpriority(which, who, niceval) >= 0 );
2824 DIE(no_func, "setpriority()");
2833 XPUSHi( time(Null(Time_t*)) );
2845 #if defined(MSDOS) || !defined(HAS_TIMES)
2846 DIE("times not implemented");
2851 (void)times(×buf);
2853 (void)times((tbuffer_t *)×buf); /* time.h uses different name for */
2854 /* struct tms, though same data */
2858 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_utime)/HZ)));
2859 if (GIMME == G_ARRAY) {
2860 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_stime)/HZ)));
2861 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cutime)/HZ)));
2862 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cstime)/HZ)));
2870 return pp_gmtime(ARGS);
2878 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
2879 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2880 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2885 when = (Time_t)SvIVx(POPs);
2887 if (op->op_type == OP_LOCALTIME)
2888 tmbuf = localtime(&when);
2890 tmbuf = gmtime(&when);
2893 if (GIMME != G_ARRAY) {
2898 sprintf(mybuf, "%s %s %2d %02d:%02d:%02d %d",
2899 dayname[tmbuf->tm_wday],
2900 monname[tmbuf->tm_mon],
2905 tmbuf->tm_year + 1900);
2906 PUSHp(mybuf, strlen(mybuf));
2909 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_sec)));
2910 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_min)));
2911 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_hour)));
2912 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mday)));
2913 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mon)));
2914 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_year)));
2915 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_wday)));
2916 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_yday)));
2917 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_isdst)));
2928 anum = alarm((unsigned int)anum);
2935 DIE(no_func, "Unsupported function alarm");
2946 (void)time(&lasttime);
2951 sleep((unsigned int)duration);
2954 XPUSHi(when - lasttime);
2958 /* Shared memory. */
2962 return pp_semget(ARGS);
2967 return pp_semctl(ARGS);
2972 return pp_shmwrite(ARGS);
2977 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2978 dSP; dMARK; dTARGET;
2979 I32 value = (I32)(do_shmio(op->op_type, MARK, SP) >= 0);
2988 /* Message passing. */
2992 return pp_semget(ARGS);
2997 return pp_semctl(ARGS);
3002 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3003 dSP; dMARK; dTARGET;
3004 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
3015 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3016 dSP; dMARK; dTARGET;
3017 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
3030 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3031 dSP; dMARK; dTARGET;
3032 int anum = do_ipcget(op->op_type, MARK, SP);
3039 DIE("System V IPC is not implemented on this machine");
3045 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3046 dSP; dMARK; dTARGET;
3047 int anum = do_ipcctl(op->op_type, MARK, SP);
3055 PUSHp("0 but true",10);
3065 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3066 dSP; dMARK; dTARGET;
3067 I32 value = (I32)(do_semop(MARK, SP) >= 0);
3076 /* Get system info. */
3081 return pp_ghostent(ARGS);
3083 DIE(no_sock_func, "gethostbyname");
3090 return pp_ghostent(ARGS);
3092 DIE(no_sock_func, "gethostbyaddr");
3100 I32 which = op->op_type;
3101 register char **elem;
3103 struct hostent *gethostbyname();
3104 struct hostent *gethostbyaddr();
3105 #ifdef HAS_GETHOSTENT
3106 struct hostent *gethostent();
3108 struct hostent *hent;
3112 if (which == OP_GHBYNAME) {
3113 hent = gethostbyname(POPp);
3115 else if (which == OP_GHBYADDR) {
3116 int addrtype = POPi;
3119 char *addr = SvPV(addrstr, addrlen);
3121 hent = gethostbyaddr(addr, addrlen, addrtype);
3124 #ifdef HAS_GETHOSTENT
3125 hent = gethostent();
3127 DIE("gethostent not implemented");
3130 #ifdef HOST_NOT_FOUND
3132 statusvalue = (U16)h_errno & 0xffff;
3135 if (GIMME != G_ARRAY) {
3136 PUSHs(sv = sv_newmortal());
3138 if (which == OP_GHBYNAME) {
3139 sv_setpvn(sv, hent->h_addr, hent->h_length);
3142 sv_setpv(sv, (char*)hent->h_name);
3148 PUSHs(sv = sv_mortalcopy(&sv_no));
3149 sv_setpv(sv, (char*)hent->h_name);
3150 PUSHs(sv = sv_mortalcopy(&sv_no));
3151 for (elem = hent->h_aliases; elem && *elem; elem++) {
3152 sv_catpv(sv, *elem);
3154 sv_catpvn(sv, " ", 1);
3156 PUSHs(sv = sv_mortalcopy(&sv_no));
3157 sv_setiv(sv, (I32)hent->h_addrtype);
3158 PUSHs(sv = sv_mortalcopy(&sv_no));
3159 len = hent->h_length;
3160 sv_setiv(sv, (I32)len);
3162 for (elem = hent->h_addr_list; elem && *elem; elem++) {
3163 XPUSHs(sv = sv_mortalcopy(&sv_no));
3164 sv_setpvn(sv, *elem, len);
3167 PUSHs(sv = sv_mortalcopy(&sv_no));
3168 sv_setpvn(sv, hent->h_addr, len);
3173 DIE(no_sock_func, "gethostent");
3180 return pp_gnetent(ARGS);
3182 DIE(no_sock_func, "getnetbyname");
3189 return pp_gnetent(ARGS);
3191 DIE(no_sock_func, "getnetbyaddr");
3199 I32 which = op->op_type;
3200 register char **elem;
3202 struct netent *getnetbyname();
3203 struct netent *getnetbyaddr();
3204 struct netent *getnetent();
3205 struct netent *nent;
3207 if (which == OP_GNBYNAME)
3208 nent = getnetbyname(POPp);
3209 else if (which == OP_GNBYADDR) {
3210 int addrtype = POPi;
3211 unsigned long addr = U_L(POPn);
3212 nent = getnetbyaddr((long)addr, addrtype);
3218 if (GIMME != G_ARRAY) {
3219 PUSHs(sv = sv_newmortal());
3221 if (which == OP_GNBYNAME)
3222 sv_setiv(sv, (I32)nent->n_net);
3224 sv_setpv(sv, nent->n_name);
3230 PUSHs(sv = sv_mortalcopy(&sv_no));
3231 sv_setpv(sv, nent->n_name);
3232 PUSHs(sv = sv_mortalcopy(&sv_no));
3233 for (elem = nent->n_aliases; *elem; elem++) {
3234 sv_catpv(sv, *elem);
3236 sv_catpvn(sv, " ", 1);
3238 PUSHs(sv = sv_mortalcopy(&sv_no));
3239 sv_setiv(sv, (I32)nent->n_addrtype);
3240 PUSHs(sv = sv_mortalcopy(&sv_no));
3241 sv_setiv(sv, (I32)nent->n_net);
3246 DIE(no_sock_func, "getnetent");
3253 return pp_gprotoent(ARGS);
3255 DIE(no_sock_func, "getprotobyname");
3262 return pp_gprotoent(ARGS);
3264 DIE(no_sock_func, "getprotobynumber");
3272 I32 which = op->op_type;
3273 register char **elem;
3275 struct protoent *getprotobyname();
3276 struct protoent *getprotobynumber();
3277 struct protoent *getprotoent();
3278 struct protoent *pent;
3280 if (which == OP_GPBYNAME)
3281 pent = getprotobyname(POPp);
3282 else if (which == OP_GPBYNUMBER)
3283 pent = getprotobynumber(POPi);
3285 pent = getprotoent();
3288 if (GIMME != G_ARRAY) {
3289 PUSHs(sv = sv_newmortal());
3291 if (which == OP_GPBYNAME)
3292 sv_setiv(sv, (I32)pent->p_proto);
3294 sv_setpv(sv, pent->p_name);
3300 PUSHs(sv = sv_mortalcopy(&sv_no));
3301 sv_setpv(sv, pent->p_name);
3302 PUSHs(sv = sv_mortalcopy(&sv_no));
3303 for (elem = pent->p_aliases; *elem; elem++) {
3304 sv_catpv(sv, *elem);
3306 sv_catpvn(sv, " ", 1);
3308 PUSHs(sv = sv_mortalcopy(&sv_no));
3309 sv_setiv(sv, (I32)pent->p_proto);
3314 DIE(no_sock_func, "getprotoent");
3321 return pp_gservent(ARGS);
3323 DIE(no_sock_func, "getservbyname");
3330 return pp_gservent(ARGS);
3332 DIE(no_sock_func, "getservbyport");
3340 I32 which = op->op_type;
3341 register char **elem;
3343 struct servent *getservbyname();
3344 struct servent *getservbynumber();
3345 struct servent *getservent();
3346 struct servent *sent;
3348 if (which == OP_GSBYNAME) {
3352 if (proto && !*proto)
3355 sent = getservbyname(name, proto);
3357 else if (which == OP_GSBYPORT) {
3361 sent = getservbyport(port, proto);
3364 sent = getservent();
3367 if (GIMME != G_ARRAY) {
3368 PUSHs(sv = sv_newmortal());
3370 if (which == OP_GSBYNAME) {
3372 sv_setiv(sv, (I32)ntohs(sent->s_port));
3374 sv_setiv(sv, (I32)(sent->s_port));
3378 sv_setpv(sv, sent->s_name);
3384 PUSHs(sv = sv_mortalcopy(&sv_no));
3385 sv_setpv(sv, sent->s_name);
3386 PUSHs(sv = sv_mortalcopy(&sv_no));
3387 for (elem = sent->s_aliases; *elem; elem++) {
3388 sv_catpv(sv, *elem);
3390 sv_catpvn(sv, " ", 1);
3392 PUSHs(sv = sv_mortalcopy(&sv_no));
3394 sv_setiv(sv, (I32)ntohs(sent->s_port));
3396 sv_setiv(sv, (I32)(sent->s_port));
3398 PUSHs(sv = sv_mortalcopy(&sv_no));
3399 sv_setpv(sv, sent->s_proto);
3404 DIE(no_sock_func, "getservent");
3415 DIE(no_sock_func, "sethostent");
3426 DIE(no_sock_func, "setnetent");
3437 DIE(no_sock_func, "setprotoent");
3448 DIE(no_sock_func, "setservent");
3460 DIE(no_sock_func, "endhostent");
3472 DIE(no_sock_func, "endnetent");
3484 DIE(no_sock_func, "endprotoent");
3496 DIE(no_sock_func, "endservent");
3503 return pp_gpwent(ARGS);
3505 DIE(no_func, "getpwnam");
3512 return pp_gpwent(ARGS);
3514 DIE(no_func, "getpwuid");
3522 I32 which = op->op_type;
3524 struct passwd *pwent;
3526 if (which == OP_GPWNAM)
3527 pwent = getpwnam(POPp);
3528 else if (which == OP_GPWUID)
3529 pwent = getpwuid(POPi);
3531 pwent = (struct passwd *)getpwent();
3534 if (GIMME != G_ARRAY) {
3535 PUSHs(sv = sv_newmortal());
3537 if (which == OP_GPWNAM)
3538 sv_setiv(sv, (I32)pwent->pw_uid);
3540 sv_setpv(sv, pwent->pw_name);
3546 PUSHs(sv = sv_mortalcopy(&sv_no));
3547 sv_setpv(sv, pwent->pw_name);
3548 PUSHs(sv = sv_mortalcopy(&sv_no));
3549 sv_setpv(sv, pwent->pw_passwd);
3550 PUSHs(sv = sv_mortalcopy(&sv_no));
3551 sv_setiv(sv, (I32)pwent->pw_uid);
3552 PUSHs(sv = sv_mortalcopy(&sv_no));
3553 sv_setiv(sv, (I32)pwent->pw_gid);
3554 PUSHs(sv = sv_mortalcopy(&sv_no));
3556 sv_setiv(sv, (I32)pwent->pw_change);
3559 sv_setiv(sv, (I32)pwent->pw_quota);
3562 sv_setpv(sv, pwent->pw_age);
3566 PUSHs(sv = sv_mortalcopy(&sv_no));
3568 sv_setpv(sv, pwent->pw_class);
3571 sv_setpv(sv, pwent->pw_comment);
3574 PUSHs(sv = sv_mortalcopy(&sv_no));
3575 sv_setpv(sv, pwent->pw_gecos);
3576 PUSHs(sv = sv_mortalcopy(&sv_no));
3577 sv_setpv(sv, pwent->pw_dir);
3578 PUSHs(sv = sv_mortalcopy(&sv_no));
3579 sv_setpv(sv, pwent->pw_shell);
3581 PUSHs(sv = sv_mortalcopy(&sv_no));
3582 sv_setiv(sv, (I32)pwent->pw_expire);
3587 DIE(no_func, "getpwent");
3598 DIE(no_func, "setpwent");
3609 DIE(no_func, "endpwent");
3616 return pp_ggrent(ARGS);
3618 DIE(no_func, "getgrnam");
3625 return pp_ggrent(ARGS);
3627 DIE(no_func, "getgrgid");
3635 I32 which = op->op_type;
3636 register char **elem;
3638 struct group *grent;
3640 if (which == OP_GGRNAM)
3641 grent = (struct group *)getgrnam(POPp);
3642 else if (which == OP_GGRGID)
3643 grent = (struct group *)getgrgid(POPi);
3645 grent = (struct group *)getgrent();
3648 if (GIMME != G_ARRAY) {
3649 PUSHs(sv = sv_newmortal());
3651 if (which == OP_GGRNAM)
3652 sv_setiv(sv, (I32)grent->gr_gid);
3654 sv_setpv(sv, grent->gr_name);
3660 PUSHs(sv = sv_mortalcopy(&sv_no));
3661 sv_setpv(sv, grent->gr_name);
3662 PUSHs(sv = sv_mortalcopy(&sv_no));
3663 sv_setpv(sv, grent->gr_passwd);
3664 PUSHs(sv = sv_mortalcopy(&sv_no));
3665 sv_setiv(sv, (I32)grent->gr_gid);
3666 PUSHs(sv = sv_mortalcopy(&sv_no));
3667 for (elem = grent->gr_mem; *elem; elem++) {
3668 sv_catpv(sv, *elem);
3670 sv_catpvn(sv, " ", 1);
3676 DIE(no_func, "getgrent");
3687 DIE(no_func, "setgrent");
3698 DIE(no_func, "endgrent");
3708 if (!(tmps = getlogin()))
3710 PUSHp(tmps, strlen(tmps));
3713 DIE(no_func, "getlogin");
3717 /* Miscellaneous. */
3722 dSP; dMARK; dORIGMARK; dTARGET;
3723 register I32 items = SP - MARK;
3724 unsigned long a[20];
3729 while (++MARK <= SP) {
3730 if (SvGMAGICAL(*MARK) && SvSMAGICAL(*MARK) && mg_find(*MARK, 't'))
3734 TAINT_PROPER("syscall");
3737 /* This probably won't work on machines where sizeof(long) != sizeof(int)
3738 * or where sizeof(long) != sizeof(char*). But such machines will
3739 * not likely have syscall implemented either, so who cares?
3741 while (++MARK <= SP) {
3742 if (SvNIOK(*MARK) || !i)
3743 a[i++] = SvIV(*MARK);
3745 a[i++] = (unsigned long)SvPVX(*MARK);
3751 DIE("Too many args to syscall");
3753 DIE("Too few args to syscall");
3755 retval = syscall(a[0]);
3758 retval = syscall(a[0],a[1]);
3761 retval = syscall(a[0],a[1],a[2]);
3764 retval = syscall(a[0],a[1],a[2],a[3]);
3767 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
3770 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
3773 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
3776 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
3780 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
3783 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
3786 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3790 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3794 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3798 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3799 a[10],a[11],a[12],a[13]);
3801 #endif /* atarist */
3807 DIE(no_func, "syscall");