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));
89 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
90 static int dooneliner _((char *cmd, char *filename));
100 fp = my_popen(tmps, "r");
102 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
103 if (GIMME == G_SCALAR) {
104 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
114 if (sv_gets(sv, fp, 0) == Nullch) {
118 XPUSHs(sv_2mortal(sv));
119 if (SvLEN(sv) - SvCUR(sv) > 20) {
120 SvLEN_set(sv, SvCUR(sv)+1);
121 Renew(SvPVX(sv), SvLEN(sv), char);
125 statusvalue = FIXSTATUS(my_pclose(fp));
129 if (GIMME == G_SCALAR)
141 SAVESPTR(last_in_gv); /* We don't want this to be permanent. */
142 last_in_gv = (GV*)*stack_sp--;
144 SAVESPTR(rs); /* This is not permanent, either. */
145 rs = sv_2mortal(newSVpv("", 1));
152 result = do_readline();
159 last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*stack_sp--)), na), TRUE,SVt_PVIO);
160 return do_readline();
165 last_in_gv = cGVOP->op_gv;
166 return do_readline();
173 if (SP - MARK != 1) {
175 do_join(TARG, &sv_no, MARK, SP);
176 tmps = SvPV(TARG, na);
180 tmps = SvPV(TOPs, na);
182 if (!tmps || !*tmps) {
183 SV *error = GvSV(errgv);
184 (void)SvUPGRADE(error, SVt_PV);
185 if (SvPOK(error) && SvCUR(error))
186 sv_catpv(error, "\t...caught");
187 tmps = SvPV(error, na);
190 tmps = "Warning: something's wrong";
199 if (SP - MARK != 1) {
201 do_join(TARG, &sv_no, MARK, SP);
202 tmps = SvPV(TARG, na);
206 tmps = SvPV(TOPs, na);
208 if (!tmps || !*tmps) {
209 SV *error = GvSV(errgv);
210 (void)SvUPGRADE(error, SVt_PV);
211 if (SvPOK(error) && SvCUR(error))
212 sv_catpv(error, "\t...propagated");
213 tmps = SvPV(error, na);
232 else if (SvTYPE(TOPs) == SVt_PVGV)
235 DIE(no_usym, "filehandle");
237 tmps = SvPV(sv, len);
238 if (do_open(gv, tmps, len, FALSE, 0, 0, Nullfp)) {
239 IoLINES(GvIOp(gv)) = 0;
240 PUSHi( (I32)forkprocess );
242 else if (forkprocess == 0) /* we are a new child */
259 PUSHs( do_close(gv, TRUE) ? &sv_yes : &sv_no );
279 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
280 DIE(no_usym, "filehandle");
285 do_close(rgv, FALSE);
287 do_close(wgv, FALSE);
292 IoIFP(rstio) = fdopen(fd[0], "r");
293 IoOFP(wstio) = fdopen(fd[1], "w");
294 IoIFP(wstio) = IoOFP(wstio);
298 if (!IoIFP(rstio) || !IoOFP(wstio)) {
299 if (IoIFP(rstio)) fclose(IoIFP(rstio));
301 if (IoOFP(wstio)) fclose(IoOFP(wstio));
311 DIE(no_func, "pipe");
324 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io)))
342 TAINT_PROPER("umask");
345 DIE(no_func, "Unsupported function umask");
363 if (!(io = GvIO(gv)) || !(fp = IoIFP(io)))
368 if (!fflush(fp) && (fp->_flag |= _IOBIN))
373 if (setmode(fileno(fp), OP_BINARY) != -1)
391 SV **mark = stack_base + ++*markstack_ptr; /* reuse in entersub */
392 I32 markoff = mark - stack_base - 1;
396 if (SvTYPE(varsv) == SVt_PVHV)
397 methname = "TIEHASH";
398 else if (SvTYPE(varsv) == SVt_PVAV)
399 methname = "TIEARRAY";
400 else if (SvTYPE(varsv) == SVt_PVGV)
401 methname = "TIEHANDLE";
403 methname = "TIESCALAR";
405 stash = gv_stashsv(mark[1], FALSE);
406 if (!stash || !(gv = gv_fetchmethod(stash, methname)) || !GvCV(gv))
407 DIE("Can't locate object method \"%s\" via package \"%s\"",
408 methname, SvPV(mark[1],na));
410 Zero(&myop, 1, BINOP);
411 myop.op_last = (OP *) &myop;
412 myop.op_next = Nullop;
413 myop.op_flags = OPf_KNOW|OPf_STACKED;
422 if (op = pp_entersub())
427 if (sv_isobject(sv)) {
428 if (SvTYPE(varsv) == SVt_PVHV || SvTYPE(varsv) == SVt_PVAV) {
429 sv_unmagic(varsv, 'P');
430 sv_magic(varsv, sv, 'P', Nullch, 0);
433 sv_unmagic(varsv, 'q');
434 sv_magic(varsv, sv, 'q', Nullch, 0);
438 SP = stack_base + markoff;
446 if (SvTYPE(TOPs) == SVt_PVHV || SvTYPE(TOPs) == SVt_PVAV)
447 sv_unmagic(TOPs, 'P');
449 sv_unmagic(TOPs, 'q');
461 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
462 mg = mg_find(sv, 'P') ;
464 mg = mg_find(sv, 'q') ;
467 PUSHs(sv_2mortal(newSVsv(mg->mg_obj))) ;
487 sv = sv_mortalcopy(&sv_no);
488 sv_setpv(sv, "AnyDBM_File");
489 stash = gv_stashsv(sv, FALSE);
490 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")) || !GvCV(gv)) {
492 perl_require_pv("AnyDBM_File.pm");
494 if (!(gv = gv_fetchmethod(stash, "TIEHASH")) || !GvCV(gv))
495 DIE("No dbm on this machine");
498 Zero(&myop, 1, BINOP);
499 myop.op_last = (OP *) &myop;
500 myop.op_next = Nullop;
501 myop.op_flags = OPf_KNOW|OPf_STACKED;
513 PUSHs(sv_2mortal(newSViv(O_RDWR|O_CREAT)));
515 PUSHs(sv_2mortal(newSViv(O_RDWR)));
520 if (op = pp_entersub())
524 if (!sv_isobject(TOPs)) {
532 PUSHs(sv_2mortal(newSViv(O_RDONLY)));
537 if (op = pp_entersub())
542 if (sv_isobject(TOPs))
543 sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
550 return pp_untie(ARGS);
564 struct timeval timebuf;
565 struct timeval *tbuf = &timebuf;
568 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
573 # if BYTEORDER & 0xf0000
574 # define ORDERBYTE (0x88888888 - BYTEORDER)
576 # define ORDERBYTE (0x4444 - BYTEORDER)
582 for (i = 1; i <= 3; i++) {
590 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
592 growsize = sizeof(fd_set);
594 growsize = maxlen; /* little endians can use vecs directly */
603 masksize = NFDBITS / NBBY;
605 masksize = sizeof(long); /* documented int, everyone seems to use long */
607 growsize = maxlen + (masksize - (maxlen % masksize));
608 Zero(&fd_sets[0], 4, char*);
616 timebuf.tv_sec = (long)value;
617 value -= (double)timebuf.tv_sec;
618 timebuf.tv_usec = (long)(value * 1000000.0);
621 tbuf = Null(struct timeval*);
623 for (i = 1; i <= 3; i++) {
630 SvPV_force(sv,na); /* force string conversion */
633 Sv_Grow(sv, growsize);
637 while (++j <= growsize) {
641 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
643 New(403, fd_sets[i], growsize, char);
644 for (offset = 0; offset < growsize; offset += masksize) {
645 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
646 fd_sets[i][j+offset] = s[(k % masksize) + offset];
649 fd_sets[i] = SvPVX(sv);
655 (Select_fd_set_t) fd_sets[1],
656 (Select_fd_set_t) fd_sets[2],
657 (Select_fd_set_t) fd_sets[3],
659 for (i = 1; i <= 3; i++) {
662 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
664 for (offset = 0; offset < growsize; offset += masksize) {
665 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
666 s[(k % masksize) + offset] = fd_sets[i][j+offset];
668 Safefree(fd_sets[i]);
675 if (GIMME == G_ARRAY && tbuf) {
676 value = (double)(timebuf.tv_sec) +
677 (double)(timebuf.tv_usec) / 1000000.0;
678 PUSHs(sv = sv_mortalcopy(&sv_no));
683 DIE("select not implemented");
692 (void)SvREFCNT_inc(gv);
694 SvREFCNT_dec(defoutgv);
704 newdefout = (op->op_private > 0) ? ((GV *) POPs) : NULL;
706 egv = GvEGV(defoutgv);
713 GV **gvp = hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
714 if (gvp && *gvp == egv)
715 gv_efullname(TARG, defoutgv);
717 sv_setsv(TARG, sv_2mortal(newRV(egv)));
722 if (!GvIO(newdefout))
724 setdefout(newdefout);
741 if (!gv || do_eof(gv)) /* make sure we have fp with something */
745 *SvPVX(TARG) = getc(IoIFP(GvIOp(gv))); /* should never be EOF */
752 return pp_sysread(ARGS);
761 register CONTEXT *cx;
763 AV* padlist = CvPADLIST(cv);
764 SV** svp = AvARRAY(padlist);
770 PUSHBLOCK(cx, CXt_SUB, stack_sp);
773 curpad = AvARRAY((AV*)svp[1]);
775 setdefout(gv); /* locally select filehandle so $% et al work */
808 SV *tmpsv = sv_newmortal();
809 gv_efullname(tmpsv, gv);
810 DIE("Undefined format \"%s\" called",SvPVX(tmpsv));
812 DIE("Not a format reference");
814 IoFLAGS(io) &= ~IOf_DIDTOP;
816 return doform(cv,gv,op->op_next);
822 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
823 register IO *io = GvIOp(gv);
824 FILE *ofp = IoOFP(io);
828 register CONTEXT *cx;
830 DEBUG_f(fprintf(stderr,"left=%ld, todo=%ld\n",
831 (long)IoLINES_LEFT(io), (long)FmLINES(formtarget)));
832 if (IoLINES_LEFT(io) < FmLINES(formtarget) &&
833 formtarget != toptarget)
841 if (!IoTOP_NAME(io)) {
843 IoFMT_NAME(io) = savepv(GvNAME(gv));
844 sprintf(tmpbuf, "%s_TOP", IoFMT_NAME(io));
845 topgv = gv_fetchpv(tmpbuf,FALSE, SVt_PVFM);
846 if ((topgv && GvFORM(topgv)) ||
847 !gv_fetchpv("top",FALSE,SVt_PVFM))
848 IoTOP_NAME(io) = savepv(tmpbuf);
850 IoTOP_NAME(io) = savepv("top");
852 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
853 if (!topgv || !GvFORM(topgv)) {
854 IoLINES_LEFT(io) = 100000000;
857 IoTOP_GV(io) = topgv;
859 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
860 I32 lines = IoLINES_LEFT(io);
861 char *s = SvPVX(formtarget);
862 if (lines <= 0) /* Yow, header didn't even fit!!! */
864 while (lines-- > 0) {
871 fwrite1(SvPVX(formtarget), s - SvPVX(formtarget), 1, ofp);
872 sv_chop(formtarget, s);
873 FmLINES(formtarget) -= IoLINES_LEFT(io);
876 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
877 fwrite1(SvPVX(formfeed), SvCUR(formfeed), 1, ofp);
878 IoLINES_LEFT(io) = IoPAGE_LEN(io);
880 formtarget = toptarget;
881 IoFLAGS(io) |= IOf_DIDTOP;
884 DIE("bad top format reference");
887 SV *tmpsv = sv_newmortal();
888 gv_efullname(tmpsv, fgv);
889 DIE("Undefined top format \"%s\" called",SvPVX(tmpsv));
891 return doform(cv,gv,op);
903 warn("Filehandle only opened for input");
905 warn("Write on closed filehandle");
910 if ((IoLINES_LEFT(io) -= FmLINES(formtarget)) < 0) {
912 warn("page overflow");
914 if (!fwrite1(SvPVX(formtarget), 1, SvCUR(formtarget), ofp) ||
918 FmLINES(formtarget) = 0;
919 SvCUR_set(formtarget, 0);
920 *SvEND(formtarget) = '\0';
921 if (IoFLAGS(io) & IOf_FLUSH)
926 formtarget = bodytarget;
933 dSP; dMARK; dORIGMARK;
939 if (op->op_flags & OPf_STACKED)
943 if (!(io = GvIO(gv))) {
946 warn("Filehandle %s never opened", SvPV(sv,na));
948 SETERRNO(EBADF,RMS$_IFI);
951 else if (!(fp = IoOFP(io))) {
955 warn("Filehandle %s opened only for input", SvPV(sv,na));
957 warn("printf on closed filehandle %s", SvPV(sv,na));
959 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
963 do_sprintf(sv, SP - MARK, MARK + 1);
964 if (!do_print(sv, fp))
967 if (IoFLAGS(io) & IOf_FLUSH)
968 if (fflush(fp) == EOF)
1001 tmps = SvPV(sv, len);
1002 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1003 IoLINES(GvIOp(gv)) = 0;
1014 dSP; dMARK; dORIGMARK; dTARGET;
1028 buffer = SvPV_force(bufsv, blen);
1029 length = SvIVx(*++MARK);
1031 DIE("Negative length");
1034 offset = SvIVx(*++MARK);
1038 if (!io || !IoIFP(io))
1041 if (op->op_type == OP_RECV) {
1042 bufsize = sizeof buf;
1043 buffer = SvGROW(bufsv, length+1);
1044 length = recvfrom(fileno(IoIFP(io)), buffer, length, offset,
1045 (struct sockaddr *)buf, &bufsize);
1048 SvCUR_set(bufsv, length);
1049 *SvEND(bufsv) = '\0';
1050 (void)SvPOK_only(bufsv);
1053 sv_magic(bufsv, Nullsv, 't', Nullch, 0);
1055 sv_setpvn(TARG, buf, bufsize);
1060 if (op->op_type == OP_RECV)
1061 DIE(no_sock_func, "recv");
1063 buffer = SvGROW(bufsv, length+offset+1);
1064 if (op->op_type == OP_SYSREAD) {
1065 length = read(fileno(IoIFP(io)), buffer+offset, length);
1068 #ifdef HAS_SOCKET__bad_code_maybe
1069 if (IoTYPE(io) == 's') {
1070 bufsize = sizeof buf;
1071 length = recvfrom(fileno(IoIFP(io)), buffer+offset, length, 0,
1072 (struct sockaddr *)buf, &bufsize);
1076 length = fread(buffer+offset, 1, length, IoIFP(io));
1079 SvCUR_set(bufsv, length+offset);
1080 *SvEND(bufsv) = '\0';
1081 (void)SvPOK_only(bufsv);
1084 sv_magic(bufsv, Nullsv, 't', Nullch, 0);
1096 return pp_send(ARGS);
1101 dSP; dMARK; dORIGMARK; dTARGET;
1114 buffer = SvPV(bufsv, blen);
1115 length = SvIVx(*++MARK);
1117 DIE("Negative length");
1120 if (!io || !IoIFP(io)) {
1123 if (op->op_type == OP_SYSWRITE)
1124 warn("Syswrite on closed filehandle");
1126 warn("Send on closed socket");
1129 else if (op->op_type == OP_SYSWRITE) {
1131 offset = SvIVx(*++MARK);
1134 if (length > blen - offset)
1135 length = blen - offset;
1136 length = write(fileno(IoIFP(io)), buffer+offset, length);
1139 else if (SP > MARK) {
1142 sockbuf = SvPVx(*++MARK, mlen);
1143 length = sendto(fileno(IoIFP(io)), buffer, blen, length,
1144 (struct sockaddr *)sockbuf, mlen);
1147 length = send(fileno(IoIFP(io)), buffer, blen, length);
1150 DIE(no_sock_func, "send");
1165 return pp_sysread(ARGS);
1176 gv = last_in_gv = (GV*)POPs;
1177 PUSHs(!gv || do_eof(gv) ? &sv_yes : &sv_no);
1189 gv = last_in_gv = (GV*)POPs;
1190 PUSHi( do_tell(gv) );
1201 gv = last_in_gv = (GV*)POPs;
1202 PUSHs( do_seek(gv, offset, whence) ? &sv_yes : &sv_no );
1209 Off_t len = (Off_t)POPn;
1214 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
1216 if (op->op_flags & OPf_SPECIAL) {
1217 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1218 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1219 ftruncate(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1222 else if (truncate(POPp, len) < 0)
1225 if (op->op_flags & OPf_SPECIAL) {
1226 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1227 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1228 chsize(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1234 if ((tmpfd = open(POPp, 0)) < 0)
1237 if (chsize(tmpfd, len) < 0)
1247 SETERRNO(EBADF,RMS$_IFI);
1250 DIE("truncate not implemented");
1256 return pp_ioctl(ARGS);
1263 unsigned int func = U_I(POPn);
1264 int optype = op->op_type;
1270 if (!io || !argsv || !IoIFP(io)) {
1271 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
1275 if (SvPOK(argsv) || !SvNIOK(argsv)) {
1277 s = SvPV_force(argsv, len);
1278 retval = IOCPARM_LEN(func);
1280 s = Sv_Grow(argsv, retval+1);
1281 SvCUR_set(argsv, retval);
1284 s[SvCUR(argsv)] = 17; /* a little sanity check here */
1287 retval = SvIV(argsv);
1289 s = (char*)(long)retval; /* ouch */
1291 s = (char*)retval; /* ouch */
1295 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1297 if (optype == OP_IOCTL)
1299 retval = ioctl(fileno(IoIFP(io)), func, s);
1301 DIE("ioctl is not implemented");
1304 #if defined(DOSISH) && !defined(OS2)
1305 DIE("fcntl is not implemented");
1308 # if defined(OS2) && defined(__EMX__)
1309 retval = fcntl(fileno(IoIFP(io)), func, (int)s);
1311 retval = fcntl(fileno(IoIFP(io)), func, s);
1314 DIE("fcntl is not implemented");
1319 if (s[SvCUR(argsv)] != 17)
1320 DIE("Possible memory corruption: %s overflowed 3rd argument",
1322 s[SvCUR(argsv)] = 0; /* put our null back */
1323 SvSETMAGIC(argsv); /* Assume it has changed */
1332 PUSHp("0 but true", 10);
1345 #if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
1346 # define flock lockf_emulate_flock
1349 #if defined(HAS_FLOCK) || defined(flock)
1356 fp = IoIFP(GvIOp(gv));
1360 value = (I32)(flock(fileno(fp), argtype) >= 0);
1367 DIE(no_func, "flock()");
1379 int protocol = POPi;
1387 SETERRNO(EBADF,LIB$_INVARG);
1393 do_close(gv, FALSE);
1395 TAINT_PROPER("socket");
1396 fd = socket(domain, type, protocol);
1399 IoIFP(io) = fdopen(fd, "r"); /* stdio gets confused about sockets */
1400 IoOFP(io) = fdopen(fd, "w");
1402 if (!IoIFP(io) || !IoOFP(io)) {
1403 if (IoIFP(io)) fclose(IoIFP(io));
1404 if (IoOFP(io)) fclose(IoOFP(io));
1405 if (!IoIFP(io) && !IoOFP(io)) close(fd);
1411 DIE(no_sock_func, "socket");
1418 #ifdef HAS_SOCKETPAIR
1423 int protocol = POPi;
1436 do_close(gv1, FALSE);
1438 do_close(gv2, FALSE);
1440 TAINT_PROPER("socketpair");
1441 if (socketpair(domain, type, protocol, fd) < 0)
1443 IoIFP(io1) = fdopen(fd[0], "r");
1444 IoOFP(io1) = fdopen(fd[0], "w");
1446 IoIFP(io2) = fdopen(fd[1], "r");
1447 IoOFP(io2) = fdopen(fd[1], "w");
1449 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
1450 if (IoIFP(io1)) fclose(IoIFP(io1));
1451 if (IoOFP(io1)) fclose(IoOFP(io1));
1452 if (!IoIFP(io1) && !IoOFP(io1)) close(fd[0]);
1453 if (IoIFP(io2)) fclose(IoIFP(io2));
1454 if (IoOFP(io2)) fclose(IoOFP(io2));
1455 if (!IoIFP(io2) && !IoOFP(io2)) close(fd[1]);
1461 DIE(no_sock_func, "socketpair");
1472 register IO *io = GvIOn(gv);
1475 if (!io || !IoIFP(io))
1478 addr = SvPV(addrsv, len);
1479 TAINT_PROPER("bind");
1480 if (bind(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1487 warn("bind() on closed fd");
1488 SETERRNO(EBADF,SS$_IVCHAN);
1491 DIE(no_sock_func, "bind");
1502 register IO *io = GvIOn(gv);
1505 if (!io || !IoIFP(io))
1508 addr = SvPV(addrsv, len);
1509 TAINT_PROPER("connect");
1510 if (connect(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1517 warn("connect() on closed fd");
1518 SETERRNO(EBADF,SS$_IVCHAN);
1521 DIE(no_sock_func, "connect");
1531 register IO *io = GvIOn(gv);
1533 if (!io || !IoIFP(io))
1536 if (listen(fileno(IoIFP(io)), backlog) >= 0)
1543 warn("listen() on closed fd");
1544 SETERRNO(EBADF,SS$_IVCHAN);
1547 DIE(no_sock_func, "listen");
1559 struct sockaddr saddr; /* use a struct to avoid alignment problems */
1560 int len = sizeof saddr;
1572 if (!gstio || !IoIFP(gstio))
1577 do_close(ngv, FALSE);
1579 fd = accept(fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
1582 IoIFP(nstio) = fdopen(fd, "r");
1583 IoOFP(nstio) = fdopen(fd, "w");
1584 IoTYPE(nstio) = 's';
1585 if (!IoIFP(nstio) || !IoOFP(nstio)) {
1586 if (IoIFP(nstio)) fclose(IoIFP(nstio));
1587 if (IoOFP(nstio)) fclose(IoOFP(nstio));
1588 if (!IoIFP(nstio) && !IoOFP(nstio)) close(fd);
1592 PUSHp((char *)&saddr, len);
1597 warn("accept() on closed fd");
1598 SETERRNO(EBADF,SS$_IVCHAN);
1604 DIE(no_sock_func, "accept");
1614 register IO *io = GvIOn(gv);
1616 if (!io || !IoIFP(io))
1619 PUSHi( shutdown(fileno(IoIFP(io)), how) >= 0 );
1624 warn("shutdown() on closed fd");
1625 SETERRNO(EBADF,SS$_IVCHAN);
1628 DIE(no_sock_func, "shutdown");
1635 return pp_ssockopt(ARGS);
1637 DIE(no_sock_func, "getsockopt");
1645 int optype = op->op_type;
1648 unsigned int optname;
1654 if (optype == OP_GSOCKOPT)
1655 sv = sv_2mortal(NEWSV(22, 257));
1658 optname = (unsigned int) POPi;
1659 lvl = (unsigned int) POPi;
1663 if (!io || !IoIFP(io))
1666 fd = fileno(IoIFP(io));
1670 (void)SvPOK_only(sv);
1674 if (getsockopt(fd, lvl, optname, SvPVX(sv), &aint) < 0)
1684 buf = SvPV(sv, len);
1685 else if (SvOK(sv)) {
1686 aint = (int)SvIV(sv);
1690 if (setsockopt(fd, lvl, optname, buf, (int)len) < 0)
1700 warn("[gs]etsockopt() on closed fd");
1701 SETERRNO(EBADF,SS$_IVCHAN);
1706 DIE(no_sock_func, "setsockopt");
1713 return pp_getpeername(ARGS);
1715 DIE(no_sock_func, "getsockname");
1723 int optype = op->op_type;
1727 register IO *io = GvIOn(gv);
1730 if (!io || !IoIFP(io))
1733 sv = sv_2mortal(NEWSV(22, 257));
1734 (void)SvPOK_only(sv);
1738 fd = fileno(IoIFP(io));
1740 case OP_GETSOCKNAME:
1741 if (getsockname(fd, (struct sockaddr *)SvPVX(sv), &aint) < 0)
1744 case OP_GETPEERNAME:
1745 if (getpeername(fd, (struct sockaddr *)SvPVX(sv), &aint) < 0)
1756 warn("get{sock, peer}name() on closed fd");
1757 SETERRNO(EBADF,SS$_IVCHAN);
1762 DIE(no_sock_func, "getpeername");
1770 return pp_stat(ARGS);
1779 if (op->op_flags & OPf_REF) {
1780 tmpgv = cGVOP->op_gv;
1782 if (tmpgv != defgv) {
1783 laststype = OP_STAT;
1785 sv_setpv(statname, "");
1786 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1787 Fstat(fileno(IoIFP(GvIOn(tmpgv))), &statcache) < 0) {
1792 else if (laststatval < 0)
1797 if (SvTYPE(sv) == SVt_PVGV) {
1801 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1802 tmpgv = (GV*)SvRV(sv);
1805 sv_setpv(statname, SvPV(sv,na));
1808 laststype = op->op_type;
1809 if (op->op_type == OP_LSTAT)
1810 laststatval = lstat(SvPV(statname, na), &statcache);
1813 laststatval = Stat(SvPV(statname, na), &statcache);
1814 if (laststatval < 0) {
1815 if (dowarn && strchr(SvPV(statname, na), '\n'))
1816 warn(warn_nl, "stat");
1822 if (GIMME != G_ARRAY) {
1829 PUSHs(sv_2mortal(newSViv((I32)statcache.st_dev)));
1830 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ino)));
1831 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mode)));
1832 PUSHs(sv_2mortal(newSViv((I32)statcache.st_nlink)));
1833 PUSHs(sv_2mortal(newSViv((I32)statcache.st_uid)));
1834 PUSHs(sv_2mortal(newSViv((I32)statcache.st_gid)));
1835 PUSHs(sv_2mortal(newSViv((I32)statcache.st_rdev)));
1836 PUSHs(sv_2mortal(newSViv((I32)statcache.st_size)));
1837 PUSHs(sv_2mortal(newSViv((I32)statcache.st_atime)));
1838 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mtime)));
1839 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ctime)));
1840 #ifdef USE_STAT_BLOCKS
1841 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blksize)));
1842 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blocks)));
1844 PUSHs(sv_2mortal(newSVpv("", 0)));
1845 PUSHs(sv_2mortal(newSVpv("", 0)));
1853 I32 result = my_stat(ARGS);
1857 if (cando(S_IRUSR, 0, &statcache))
1864 I32 result = my_stat(ARGS);
1868 if (cando(S_IWUSR, 0, &statcache))
1875 I32 result = my_stat(ARGS);
1879 if (cando(S_IXUSR, 0, &statcache))
1886 I32 result = my_stat(ARGS);
1890 if (cando(S_IRUSR, 1, &statcache))
1897 I32 result = my_stat(ARGS);
1901 if (cando(S_IWUSR, 1, &statcache))
1908 I32 result = my_stat(ARGS);
1912 if (cando(S_IXUSR, 1, &statcache))
1919 I32 result = my_stat(ARGS);
1928 return pp_ftrowned(ARGS);
1933 I32 result = my_stat(ARGS);
1937 if (statcache.st_uid == (op->op_type == OP_FTEOWNED ? euid : uid) )
1944 I32 result = my_stat(ARGS);
1948 if (!statcache.st_size)
1955 I32 result = my_stat(ARGS);
1959 PUSHi(statcache.st_size);
1965 I32 result = my_stat(ARGS);
1969 PUSHn( (basetime - statcache.st_mtime) / 86400.0 );
1975 I32 result = my_stat(ARGS);
1979 PUSHn( (basetime - statcache.st_atime) / 86400.0 );
1985 I32 result = my_stat(ARGS);
1989 PUSHn( (basetime - statcache.st_ctime) / 86400.0 );
1995 I32 result = my_stat(ARGS);
1999 if (S_ISSOCK(statcache.st_mode))
2006 I32 result = my_stat(ARGS);
2010 if (S_ISCHR(statcache.st_mode))
2017 I32 result = my_stat(ARGS);
2021 if (S_ISBLK(statcache.st_mode))
2028 I32 result = my_stat(ARGS);
2032 if (S_ISREG(statcache.st_mode))
2039 I32 result = my_stat(ARGS);
2043 if (S_ISDIR(statcache.st_mode))
2050 I32 result = my_stat(ARGS);
2054 if (S_ISFIFO(statcache.st_mode))
2061 I32 result = my_lstat(ARGS);
2065 if (S_ISLNK(statcache.st_mode))
2074 I32 result = my_stat(ARGS);
2078 if (statcache.st_mode & S_ISUID)
2088 I32 result = my_stat(ARGS);
2092 if (statcache.st_mode & S_ISGID)
2102 I32 result = my_stat(ARGS);
2106 if (statcache.st_mode & S_ISVTX)
2118 if (op->op_flags & OPf_REF) {
2123 gv = gv_fetchpv(tmps = POPp, FALSE, SVt_PVIO);
2124 if (GvIO(gv) && IoIFP(GvIOp(gv)))
2125 fd = fileno(IoIFP(GvIOp(gv)));
2126 else if (isDIGIT(*tmps))
2135 #if defined(atarist) /* this will work with atariST. Configure will
2136 make guesses for other systems. */
2137 # define FILE_base(f) ((f)->_base)
2138 # define FILE_ptr(f) ((f)->_ptr)
2139 # define FILE_cnt(f) ((f)->_cnt)
2140 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
2150 register STDCHAR *s;
2154 if (op->op_flags & OPf_REF) {
2156 if (cGVOP->op_gv == defgv) {
2161 goto really_filename;
2165 statgv = cGVOP->op_gv;
2166 sv_setpv(statname, "");
2169 if (io && IoIFP(io)) {
2171 Fstat(fileno(IoIFP(io)), &statcache);
2172 if (S_ISDIR(statcache.st_mode)) /* handle NFS glitch */
2173 if (op->op_type == OP_FTTEXT)
2177 if (FILE_cnt(IoIFP(io)) <= 0) {
2178 i = getc(IoIFP(io));
2180 (void)ungetc(i, IoIFP(io));
2182 if (FILE_cnt(IoIFP(io)) <= 0) /* null file is anything */
2184 len = FILE_bufsiz(IoIFP(io));
2185 s = FILE_base(IoIFP(io));
2187 DIE("-T and -B not implemented on filehandles");
2192 warn("Test on unopened file <%s>",
2193 GvENAME(cGVOP->op_gv));
2194 SETERRNO(EBADF,RMS$_IFI);
2201 sv_setpv(statname, SvPV(sv, na));
2204 i = open(SvPV(sv, na), O_RDONLY, 0);
2206 i = open(SvPV(sv, na), 0);
2209 if (dowarn && strchr(SvPV(sv, na), '\n'))
2210 warn(warn_nl, "open");
2213 Fstat(i, &statcache);
2214 len = read(i, tbuf, 512);
2217 if (S_ISDIR(statcache.st_mode) && op->op_type == OP_FTTEXT)
2218 RETPUSHNO; /* special case NFS directories */
2219 RETPUSHYES; /* null file is anything */
2224 /* now scan s to look for textiness */
2225 /* XXX ASCII dependent code */
2227 for (i = 0; i < len; i++, s++) {
2228 if (!*s) { /* null never allowed in text */
2235 *s != '\n' && *s != '\r' && *s != '\b' &&
2236 *s != '\t' && *s != '\f' && *s != 27)
2240 if ((odd * 3 > len) == (op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
2248 return pp_fttext(ARGS);
2263 if (!tmps || !*tmps) {
2264 svp = hv_fetch(GvHVn(envgv), "HOME", 4, FALSE);
2266 tmps = SvPV(*svp, na);
2268 if (!tmps || !*tmps) {
2269 svp = hv_fetch(GvHVn(envgv), "LOGDIR", 6, FALSE);
2271 tmps = SvPV(*svp, na);
2273 TAINT_PROPER("chdir");
2274 PUSHi( chdir(tmps) >= 0 );
2276 /* Clear the DEFAULT element of ENV so we'll get the new value
2278 hv_delete(GvHVn(envgv),"DEFAULT",7,G_DISCARD);
2285 dSP; dMARK; dTARGET;
2288 value = (I32)apply(op->op_type, MARK, SP);
2293 DIE(no_func, "Unsupported function chown");
2303 TAINT_PROPER("chroot");
2304 PUSHi( chroot(tmps) >= 0 );
2307 DIE(no_func, "chroot");
2313 dSP; dMARK; dTARGET;
2315 value = (I32)apply(op->op_type, MARK, SP);
2323 dSP; dMARK; dTARGET;
2325 value = (I32)apply(op->op_type, MARK, SP);
2333 dSP; dMARK; dTARGET;
2335 value = (I32)apply(op->op_type, MARK, SP);
2347 char *tmps = SvPV(TOPs, na);
2348 TAINT_PROPER("rename");
2350 anum = rename(tmps, tmps2);
2352 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
2355 if (euid || Stat(tmps2, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
2356 (void)UNLINK(tmps2);
2357 if (!(anum = link(tmps, tmps2)))
2358 anum = UNLINK(tmps);
2370 char *tmps = SvPV(TOPs, na);
2371 TAINT_PROPER("link");
2372 SETi( link(tmps, tmps2) >= 0 );
2374 DIE(no_func, "Unsupported function link");
2384 char *tmps = SvPV(TOPs, na);
2385 TAINT_PROPER("symlink");
2386 SETi( symlink(tmps, tmps2) >= 0 );
2389 DIE(no_func, "symlink");
2400 len = readlink(tmps, buf, sizeof buf);
2408 RETSETUNDEF; /* just pretend it's a normal file */
2412 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
2414 dooneliner(cmd, filename)
2420 *save_filename = filename;
2426 for (s = mybuf+strlen(mybuf); *filename; ) {
2431 myfp = my_popen(mybuf, "r");
2434 s = fgets(mybuf, sizeof mybuf, myfp);
2435 (void)my_pclose(myfp);
2437 for (errno = 1; errno < sys_nerr; errno++) {
2438 #ifdef HAS_SYS_ERRLIST
2439 if (instr(mybuf, sys_errlist[errno])) /* you don't see this */
2442 char *errmsg; /* especially if it isn't there */
2445 (errmsg = strerror(errno)) ? errmsg : "NoErRoR"))
2451 #define EACCES EPERM
2453 if (instr(mybuf, "cannot make"))
2454 SETERRNO(EEXIST,RMS$_FEX);
2455 else if (instr(mybuf, "existing file"))
2456 SETERRNO(EEXIST,RMS$_FEX);
2457 else if (instr(mybuf, "ile exists"))
2458 SETERRNO(EEXIST,RMS$_FEX);
2459 else if (instr(mybuf, "non-exist"))
2460 SETERRNO(ENOENT,RMS$_FNF);
2461 else if (instr(mybuf, "does not exist"))
2462 SETERRNO(ENOENT,RMS$_FNF);
2463 else if (instr(mybuf, "not empty"))
2464 SETERRNO(EBUSY,SS$_DEVOFFLINE);
2465 else if (instr(mybuf, "cannot access"))
2466 SETERRNO(EACCES,RMS$_PRV);
2468 SETERRNO(EPERM,RMS$_PRV);
2471 else { /* some mkdirs return no failure indication */
2472 anum = (Stat(save_filename, &statbuf) >= 0);
2473 if (op->op_type == OP_RMDIR)
2478 SETERRNO(EACCES,RMS$_PRV); /* a guess */
2494 char *tmps = SvPV(TOPs, na);
2496 TAINT_PROPER("mkdir");
2498 SETi( mkdir(tmps, mode) >= 0 );
2500 SETi( dooneliner("mkdir", tmps) );
2501 oldumask = umask(0);
2503 chmod(tmps, (mode & ~oldumask) & 0777);
2514 TAINT_PROPER("rmdir");
2516 XPUSHi( rmdir(tmps) >= 0 );
2518 XPUSHi( dooneliner("rmdir", tmps) );
2523 /* Directory calls. */
2528 #if defined(Direntry_t) && defined(HAS_READDIR)
2529 char *dirname = POPp;
2531 register IO *io = GvIOn(gv);
2537 closedir(IoDIRP(io));
2538 if (!(IoDIRP(io) = opendir(dirname)))
2544 SETERRNO(EBADF,RMS$_DIR);
2547 DIE(no_dir_func, "opendir");
2554 #if defined(Direntry_t) && defined(HAS_READDIR)
2556 Direntry_t *readdir _((DIR *));
2558 register Direntry_t *dp;
2560 register IO *io = GvIOn(gv);
2562 if (!io || !IoDIRP(io))
2565 if (GIMME == G_ARRAY) {
2567 while (dp = (Direntry_t *)readdir(IoDIRP(io))) {
2569 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2571 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2576 if (!(dp = (Direntry_t *)readdir(IoDIRP(io))))
2579 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2581 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2588 SETERRNO(EBADF,RMS$_ISI);
2589 if (GIMME == G_ARRAY)
2594 DIE(no_dir_func, "readdir");
2601 #if defined(HAS_TELLDIR) || defined(telldir)
2602 #if !defined(telldir) && !defined(HAS_TELLDIR_PROTOTYPE)
2603 long telldir _((DIR *));
2606 register IO *io = GvIOn(gv);
2608 if (!io || !IoDIRP(io))
2611 PUSHi( telldir(IoDIRP(io)) );
2615 SETERRNO(EBADF,RMS$_ISI);
2618 DIE(no_dir_func, "telldir");
2625 #if defined(HAS_SEEKDIR) || defined(seekdir)
2628 register IO *io = GvIOn(gv);
2630 if (!io || !IoDIRP(io))
2633 (void)seekdir(IoDIRP(io), along);
2638 SETERRNO(EBADF,RMS$_ISI);
2641 DIE(no_dir_func, "seekdir");
2648 #if defined(HAS_REWINDDIR) || defined(rewinddir)
2650 register IO *io = GvIOn(gv);
2652 if (!io || !IoDIRP(io))
2655 (void)rewinddir(IoDIRP(io));
2659 SETERRNO(EBADF,RMS$_ISI);
2662 DIE(no_dir_func, "rewinddir");
2669 #if defined(Direntry_t) && defined(HAS_READDIR)
2671 register IO *io = GvIOn(gv);
2673 if (!io || !IoDIRP(io))
2676 #ifdef VOID_CLOSEDIR
2677 closedir(IoDIRP(io));
2679 if (closedir(IoDIRP(io)) < 0) {
2680 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
2689 SETERRNO(EBADF,RMS$_IFI);
2692 DIE(no_dir_func, "closedir");
2696 /* Process control. */
2711 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
2712 sv_setiv(GvSV(tmpgv), (I32)getpid());
2713 hv_clear(pidstatus); /* no kids, so don't wait for 'em */
2718 DIE(no_func, "Unsupported function fork");
2731 childpid = wait(&argflags);
2733 pidgone(childpid, argflags);
2734 value = (I32)childpid;
2735 statusvalue = FIXSTATUS(argflags);
2739 DIE(no_func, "Unsupported function wait");
2754 childpid = wait4pid(childpid, &argflags, optype);
2755 value = (I32)childpid;
2756 statusvalue = FIXSTATUS(argflags);
2760 DIE(no_func, "Unsupported function wait");
2766 dSP; dMARK; dORIGMARK; dTARGET;
2771 Signal_t (*ihand)(); /* place to save signal during system() */
2772 Signal_t (*qhand)(); /* place to save signal during system() */
2774 #if defined(HAS_FORK) && !defined(VMS)
2775 if (SP - MARK == 1) {
2777 char *junk = SvPV(TOPs, na);
2779 TAINT_PROPER("system");
2782 while ((childpid = vfork()) == -1) {
2783 if (errno != EAGAIN) {
2792 ihand = signal(SIGINT, SIG_IGN);
2793 qhand = signal(SIGQUIT, SIG_IGN);
2795 result = wait4pid(childpid, &status, 0);
2796 } while (result == -1 && errno == EINTR);
2797 (void)signal(SIGINT, ihand);
2798 (void)signal(SIGQUIT, qhand);
2799 statusvalue = FIXSTATUS(status);
2803 value = (I32)((unsigned int)status & 0xffff);
2805 do_execfree(); /* free any memory child malloced on vfork */
2810 if (op->op_flags & OPf_STACKED) {
2811 SV *really = *++MARK;
2812 value = (I32)do_aexec(really, MARK, SP);
2814 else if (SP - MARK != 1)
2815 value = (I32)do_aexec(Nullsv, MARK, SP);
2817 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2820 #else /* ! FORK or VMS */
2821 if (op->op_flags & OPf_STACKED) {
2822 SV *really = *++MARK;
2823 value = (I32)do_aspawn(really, MARK, SP);
2825 else if (SP - MARK != 1)
2826 value = (I32)do_aspawn(Nullsv, MARK, SP);
2828 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), na));
2830 statusvalue = FIXSTATUS(value);
2834 #endif /* !FORK or VMS */
2840 dSP; dMARK; dORIGMARK; dTARGET;
2843 if (op->op_flags & OPf_STACKED) {
2844 SV *really = *++MARK;
2845 value = (I32)do_aexec(really, MARK, SP);
2847 else if (SP - MARK != 1)
2849 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
2851 value = (I32)do_aexec(Nullsv, MARK, SP);
2855 char *junk = SvPV(*SP, na);
2857 TAINT_PROPER("exec");
2860 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), na));
2862 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2872 dSP; dMARK; dTARGET;
2875 value = (I32)apply(op->op_type, MARK, SP);
2880 DIE(no_func, "Unsupported function kill");
2888 XPUSHi( getppid() );
2891 DIE(no_func, "getppid");
2907 value = (I32)getpgrp(pid);
2910 DIE("POSIX getpgrp can't take an argument");
2911 value = (I32)getpgrp();
2916 DIE(no_func, "getpgrp()");
2935 TAINT_PROPER("setpgrp");
2937 SETi( setpgrp(pid, pgrp) >= 0 );
2939 if ((pgrp != 0) || (pid != 0)) {
2940 DIE("POSIX setpgrp can't take an argument");
2942 SETi( setpgrp() >= 0 );
2943 #endif /* USE_BSDPGRP */
2946 DIE(no_func, "setpgrp()");
2955 #ifdef HAS_GETPRIORITY
2958 SETi( getpriority(which, who) );
2961 DIE(no_func, "getpriority()");
2971 #ifdef HAS_SETPRIORITY
2975 TAINT_PROPER("setpriority");
2976 SETi( setpriority(which, who, niceval) >= 0 );
2979 DIE(no_func, "setpriority()");
2988 XPUSHi( time(Null(Time_t*)) );
3000 #if defined(MSDOS) || !defined(HAS_TIMES)
3001 DIE("times not implemented");
3006 (void)times(×buf);
3008 (void)times((tbuffer_t *)×buf); /* time.h uses different name for */
3009 /* struct tms, though same data */
3015 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_utime)/HZ)));
3016 if (GIMME == G_ARRAY) {
3017 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_stime)/HZ)));
3018 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cutime)/HZ)));
3019 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cstime)/HZ)));
3027 return pp_gmtime(ARGS);
3035 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
3036 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3037 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
3042 when = (Time_t)SvIVx(POPs);
3044 if (op->op_type == OP_LOCALTIME)
3045 tmbuf = localtime(&when);
3047 tmbuf = gmtime(&when);
3050 if (GIMME != G_ARRAY) {
3055 sprintf(mybuf, "%s %s %2d %02d:%02d:%02d %d",
3056 dayname[tmbuf->tm_wday],
3057 monname[tmbuf->tm_mon],
3062 tmbuf->tm_year + 1900);
3063 PUSHp(mybuf, strlen(mybuf));
3066 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_sec)));
3067 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_min)));
3068 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_hour)));
3069 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mday)));
3070 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mon)));
3071 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_year)));
3072 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_wday)));
3073 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_yday)));
3074 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_isdst)));
3085 anum = alarm((unsigned int)anum);
3092 DIE(no_func, "Unsupported function alarm");
3103 (void)time(&lasttime);
3108 sleep((unsigned int)duration);
3111 XPUSHi(when - lasttime);
3115 /* Shared memory. */
3119 return pp_semget(ARGS);
3124 return pp_semctl(ARGS);
3129 return pp_shmwrite(ARGS);
3134 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3135 dSP; dMARK; dTARGET;
3136 I32 value = (I32)(do_shmio(op->op_type, MARK, SP) >= 0);
3141 return pp_semget(ARGS);
3145 /* Message passing. */
3149 return pp_semget(ARGS);
3154 return pp_semctl(ARGS);
3159 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3160 dSP; dMARK; dTARGET;
3161 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
3166 return pp_semget(ARGS);
3172 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3173 dSP; dMARK; dTARGET;
3174 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
3179 return pp_semget(ARGS);
3187 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3188 dSP; dMARK; dTARGET;
3189 int anum = do_ipcget(op->op_type, MARK, SP);
3196 DIE("System V IPC is not implemented on this machine");
3202 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3203 dSP; dMARK; dTARGET;
3204 int anum = do_ipcctl(op->op_type, MARK, SP);
3212 PUSHp("0 but true",10);
3216 return pp_semget(ARGS);
3222 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3223 dSP; dMARK; dTARGET;
3224 I32 value = (I32)(do_semop(MARK, SP) >= 0);
3229 return pp_semget(ARGS);
3233 /* Get system info. */
3238 return pp_ghostent(ARGS);
3240 DIE(no_sock_func, "gethostbyname");
3247 return pp_ghostent(ARGS);
3249 DIE(no_sock_func, "gethostbyaddr");
3257 I32 which = op->op_type;
3258 register char **elem;
3260 struct hostent *gethostbyname();
3261 struct hostent *gethostbyaddr();
3262 #ifdef HAS_GETHOSTENT
3263 struct hostent *gethostent();
3265 struct hostent *hent;
3269 if (which == OP_GHBYNAME) {
3270 hent = gethostbyname(POPp);
3272 else if (which == OP_GHBYADDR) {
3273 int addrtype = POPi;
3276 char *addr = SvPV(addrsv, addrlen);
3278 hent = gethostbyaddr(addr, addrlen, addrtype);
3281 #ifdef HAS_GETHOSTENT
3282 hent = gethostent();
3284 DIE("gethostent not implemented");
3287 #ifdef HOST_NOT_FOUND
3289 statusvalue = FIXSTATUS(h_errno);
3292 if (GIMME != G_ARRAY) {
3293 PUSHs(sv = sv_newmortal());
3295 if (which == OP_GHBYNAME) {
3297 sv_setpvn(sv, hent->h_addr, hent->h_length);
3300 sv_setpv(sv, (char*)hent->h_name);
3306 PUSHs(sv = sv_mortalcopy(&sv_no));
3307 sv_setpv(sv, (char*)hent->h_name);
3308 PUSHs(sv = sv_mortalcopy(&sv_no));
3309 for (elem = hent->h_aliases; elem && *elem; elem++) {
3310 sv_catpv(sv, *elem);
3312 sv_catpvn(sv, " ", 1);
3314 PUSHs(sv = sv_mortalcopy(&sv_no));
3315 sv_setiv(sv, (I32)hent->h_addrtype);
3316 PUSHs(sv = sv_mortalcopy(&sv_no));
3317 len = hent->h_length;
3318 sv_setiv(sv, (I32)len);
3320 for (elem = hent->h_addr_list; elem && *elem; elem++) {
3321 XPUSHs(sv = sv_mortalcopy(&sv_no));
3322 sv_setpvn(sv, *elem, len);
3325 PUSHs(sv = sv_mortalcopy(&sv_no));
3327 sv_setpvn(sv, hent->h_addr, len);
3332 DIE(no_sock_func, "gethostent");
3339 return pp_gnetent(ARGS);
3341 DIE(no_sock_func, "getnetbyname");
3348 return pp_gnetent(ARGS);
3350 DIE(no_sock_func, "getnetbyaddr");
3358 I32 which = op->op_type;
3359 register char **elem;
3361 struct netent *getnetbyname();
3362 struct netent *getnetbyaddr();
3363 struct netent *getnetent();
3364 struct netent *nent;
3366 if (which == OP_GNBYNAME)
3367 nent = getnetbyname(POPp);
3368 else if (which == OP_GNBYADDR) {
3369 int addrtype = POPi;
3370 unsigned long addr = U_L(POPn);
3371 nent = getnetbyaddr((long)addr, addrtype);
3377 if (GIMME != G_ARRAY) {
3378 PUSHs(sv = sv_newmortal());
3380 if (which == OP_GNBYNAME)
3381 sv_setiv(sv, (I32)nent->n_net);
3383 sv_setpv(sv, nent->n_name);
3389 PUSHs(sv = sv_mortalcopy(&sv_no));
3390 sv_setpv(sv, nent->n_name);
3391 PUSHs(sv = sv_mortalcopy(&sv_no));
3392 for (elem = nent->n_aliases; *elem; elem++) {
3393 sv_catpv(sv, *elem);
3395 sv_catpvn(sv, " ", 1);
3397 PUSHs(sv = sv_mortalcopy(&sv_no));
3398 sv_setiv(sv, (I32)nent->n_addrtype);
3399 PUSHs(sv = sv_mortalcopy(&sv_no));
3400 sv_setiv(sv, (I32)nent->n_net);
3405 DIE(no_sock_func, "getnetent");
3412 return pp_gprotoent(ARGS);
3414 DIE(no_sock_func, "getprotobyname");
3421 return pp_gprotoent(ARGS);
3423 DIE(no_sock_func, "getprotobynumber");
3431 I32 which = op->op_type;
3432 register char **elem;
3434 struct protoent *getprotobyname();
3435 struct protoent *getprotobynumber();
3436 struct protoent *getprotoent();
3437 struct protoent *pent;
3439 if (which == OP_GPBYNAME)
3440 pent = getprotobyname(POPp);
3441 else if (which == OP_GPBYNUMBER)
3442 pent = getprotobynumber(POPi);
3444 pent = getprotoent();
3447 if (GIMME != G_ARRAY) {
3448 PUSHs(sv = sv_newmortal());
3450 if (which == OP_GPBYNAME)
3451 sv_setiv(sv, (I32)pent->p_proto);
3453 sv_setpv(sv, pent->p_name);
3459 PUSHs(sv = sv_mortalcopy(&sv_no));
3460 sv_setpv(sv, pent->p_name);
3461 PUSHs(sv = sv_mortalcopy(&sv_no));
3462 for (elem = pent->p_aliases; *elem; elem++) {
3463 sv_catpv(sv, *elem);
3465 sv_catpvn(sv, " ", 1);
3467 PUSHs(sv = sv_mortalcopy(&sv_no));
3468 sv_setiv(sv, (I32)pent->p_proto);
3473 DIE(no_sock_func, "getprotoent");
3480 return pp_gservent(ARGS);
3482 DIE(no_sock_func, "getservbyname");
3489 return pp_gservent(ARGS);
3491 DIE(no_sock_func, "getservbyport");
3499 I32 which = op->op_type;
3500 register char **elem;
3502 struct servent *getservbyname();
3503 struct servent *getservbynumber();
3504 struct servent *getservent();
3505 struct servent *sent;
3507 if (which == OP_GSBYNAME) {
3511 if (proto && !*proto)
3514 sent = getservbyname(name, proto);
3516 else if (which == OP_GSBYPORT) {
3520 sent = getservbyport(port, proto);
3523 sent = getservent();
3526 if (GIMME != G_ARRAY) {
3527 PUSHs(sv = sv_newmortal());
3529 if (which == OP_GSBYNAME) {
3531 sv_setiv(sv, (I32)ntohs(sent->s_port));
3533 sv_setiv(sv, (I32)(sent->s_port));
3537 sv_setpv(sv, sent->s_name);
3543 PUSHs(sv = sv_mortalcopy(&sv_no));
3544 sv_setpv(sv, sent->s_name);
3545 PUSHs(sv = sv_mortalcopy(&sv_no));
3546 for (elem = sent->s_aliases; *elem; elem++) {
3547 sv_catpv(sv, *elem);
3549 sv_catpvn(sv, " ", 1);
3551 PUSHs(sv = sv_mortalcopy(&sv_no));
3553 sv_setiv(sv, (I32)ntohs(sent->s_port));
3555 sv_setiv(sv, (I32)(sent->s_port));
3557 PUSHs(sv = sv_mortalcopy(&sv_no));
3558 sv_setpv(sv, sent->s_proto);
3563 DIE(no_sock_func, "getservent");
3574 DIE(no_sock_func, "sethostent");
3585 DIE(no_sock_func, "setnetent");
3596 DIE(no_sock_func, "setprotoent");
3607 DIE(no_sock_func, "setservent");
3619 DIE(no_sock_func, "endhostent");
3631 DIE(no_sock_func, "endnetent");
3643 DIE(no_sock_func, "endprotoent");
3655 DIE(no_sock_func, "endservent");
3662 return pp_gpwent(ARGS);
3664 DIE(no_func, "getpwnam");
3671 return pp_gpwent(ARGS);
3673 DIE(no_func, "getpwuid");
3681 I32 which = op->op_type;
3683 struct passwd *pwent;
3685 if (which == OP_GPWNAM)
3686 pwent = getpwnam(POPp);
3687 else if (which == OP_GPWUID)
3688 pwent = getpwuid(POPi);
3690 pwent = (struct passwd *)getpwent();
3693 if (GIMME != G_ARRAY) {
3694 PUSHs(sv = sv_newmortal());
3696 if (which == OP_GPWNAM)
3697 sv_setiv(sv, (I32)pwent->pw_uid);
3699 sv_setpv(sv, pwent->pw_name);
3705 PUSHs(sv = sv_mortalcopy(&sv_no));
3706 sv_setpv(sv, pwent->pw_name);
3707 PUSHs(sv = sv_mortalcopy(&sv_no));
3708 sv_setpv(sv, pwent->pw_passwd);
3709 PUSHs(sv = sv_mortalcopy(&sv_no));
3710 sv_setiv(sv, (I32)pwent->pw_uid);
3711 PUSHs(sv = sv_mortalcopy(&sv_no));
3712 sv_setiv(sv, (I32)pwent->pw_gid);
3713 PUSHs(sv = sv_mortalcopy(&sv_no));
3715 sv_setiv(sv, (I32)pwent->pw_change);
3718 sv_setiv(sv, (I32)pwent->pw_quota);
3721 sv_setpv(sv, pwent->pw_age);
3725 PUSHs(sv = sv_mortalcopy(&sv_no));
3727 sv_setpv(sv, pwent->pw_class);
3730 sv_setpv(sv, pwent->pw_comment);
3733 PUSHs(sv = sv_mortalcopy(&sv_no));
3734 sv_setpv(sv, pwent->pw_gecos);
3735 PUSHs(sv = sv_mortalcopy(&sv_no));
3736 sv_setpv(sv, pwent->pw_dir);
3737 PUSHs(sv = sv_mortalcopy(&sv_no));
3738 sv_setpv(sv, pwent->pw_shell);
3740 PUSHs(sv = sv_mortalcopy(&sv_no));
3741 sv_setiv(sv, (I32)pwent->pw_expire);
3746 DIE(no_func, "getpwent");
3757 DIE(no_func, "setpwent");
3768 DIE(no_func, "endpwent");
3775 return pp_ggrent(ARGS);
3777 DIE(no_func, "getgrnam");
3784 return pp_ggrent(ARGS);
3786 DIE(no_func, "getgrgid");
3794 I32 which = op->op_type;
3795 register char **elem;
3797 struct group *grent;
3799 if (which == OP_GGRNAM)
3800 grent = (struct group *)getgrnam(POPp);
3801 else if (which == OP_GGRGID)
3802 grent = (struct group *)getgrgid(POPi);
3804 grent = (struct group *)getgrent();
3807 if (GIMME != G_ARRAY) {
3808 PUSHs(sv = sv_newmortal());
3810 if (which == OP_GGRNAM)
3811 sv_setiv(sv, (I32)grent->gr_gid);
3813 sv_setpv(sv, grent->gr_name);
3819 PUSHs(sv = sv_mortalcopy(&sv_no));
3820 sv_setpv(sv, grent->gr_name);
3821 PUSHs(sv = sv_mortalcopy(&sv_no));
3822 sv_setpv(sv, grent->gr_passwd);
3823 PUSHs(sv = sv_mortalcopy(&sv_no));
3824 sv_setiv(sv, (I32)grent->gr_gid);
3825 PUSHs(sv = sv_mortalcopy(&sv_no));
3826 for (elem = grent->gr_mem; *elem; elem++) {
3827 sv_catpv(sv, *elem);
3829 sv_catpvn(sv, " ", 1);
3835 DIE(no_func, "getgrent");
3846 DIE(no_func, "setgrent");
3857 DIE(no_func, "endgrent");
3867 if (!(tmps = getlogin()))
3869 PUSHp(tmps, strlen(tmps));
3872 DIE(no_func, "getlogin");
3876 /* Miscellaneous. */
3881 dSP; dMARK; dORIGMARK; dTARGET;
3882 register I32 items = SP - MARK;
3883 unsigned long a[20];
3889 while (++MARK <= SP) {
3890 if (SvGMAGICAL(*MARK) && SvSMAGICAL(*MARK) &&
3891 (mg = mg_find(*MARK, 't')) && mg->mg_len & 1)
3895 TAINT_PROPER("syscall");
3898 /* This probably won't work on machines where sizeof(long) != sizeof(int)
3899 * or where sizeof(long) != sizeof(char*). But such machines will
3900 * not likely have syscall implemented either, so who cares?
3902 while (++MARK <= SP) {
3903 if (SvNIOK(*MARK) || !i)
3904 a[i++] = SvIV(*MARK);
3905 else if (*MARK == &sv_undef)
3908 a[i++] = (unsigned long)SvPV_force(*MARK, na);
3914 DIE("Too many args to syscall");
3916 DIE("Too few args to syscall");
3918 retval = syscall(a[0]);
3921 retval = syscall(a[0],a[1]);
3924 retval = syscall(a[0],a[1],a[2]);
3927 retval = syscall(a[0],a[1],a[2],a[3]);
3930 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
3933 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
3936 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
3939 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
3943 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
3946 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
3949 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3953 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3957 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3961 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3962 a[10],a[11],a[12],a[13]);
3964 #endif /* atarist */
3970 DIE(no_func, "syscall");
3974 #if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
3976 /* XXX Emulate flock() with lockf(). This is just to increase
3977 portability of scripts. The calls are not completely
3978 interchangeable. What's really needed is a good file
3982 /* We might need <unistd.h> because it sometimes defines the lockf()
3983 constants. Unfortunately, <unistd.h> causes troubles on some mixed
3984 (BSD/POSIX) systems, such as SunOS 4.1.3. We could just try including
3985 <unistd.h> here in this part of the file, but that might
3986 conflict with various other #defines and includes above, such as
3987 #define vfork fork above.
3989 Further, the lockf() constants aren't POSIX, so they might not be
3990 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
3991 just stick in the SVID values and be done with it. Sigh.
3995 # define F_ULOCK 0 /* Unlock a previously locked region */
3998 # define F_LOCK 1 /* Lock a region for exclusive use */
4001 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
4004 # define F_TEST 3 /* Test a region for other processes locks */
4007 /* These are the flock() constants. Since this sytems doesn't have
4008 flock(), the values of the constants are probably not available.
4024 lockf_emulate_flock (fd, operation)
4029 switch (operation) {
4031 /* LOCK_SH - get a shared lock */
4033 /* LOCK_EX - get an exclusive lock */
4035 i = lockf (fd, F_LOCK, 0);
4038 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
4039 case LOCK_SH|LOCK_NB:
4040 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
4041 case LOCK_EX|LOCK_NB:
4042 i = lockf (fd, F_TLOCK, 0);
4044 if ((errno == EAGAIN) || (errno == EACCES))
4045 errno = EWOULDBLOCK;
4048 /* LOCK_UN - unlock */
4050 i = lockf (fd, F_ULOCK, 0);
4053 /* Default - can't decipher operation */