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(ARGS))
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(ARGS))
524 if (!sv_isobject(TOPs)) {
532 PUSHs(sv_2mortal(newSViv(O_RDONLY)));
537 if (op = pp_entersub(ARGS))
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");
693 (void)SvREFCNT_inc(gv);
695 SvREFCNT_dec(defoutgv);
705 newdefout = (op->op_private > 0) ? ((GV *) POPs) : NULL;
707 egv = GvEGV(defoutgv);
714 GV **gvp = hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
715 if (gvp && *gvp == egv)
716 gv_efullname(TARG, defoutgv);
718 sv_setsv(TARG, sv_2mortal(newRV(egv)));
723 if (!GvIO(newdefout))
725 setdefout(newdefout);
742 if (!gv || do_eof(gv)) /* make sure we have fp with something */
746 *SvPVX(TARG) = getc(IoIFP(GvIOp(gv))); /* should never be EOF */
753 return pp_sysread(ARGS);
763 register CONTEXT *cx;
765 AV* padlist = CvPADLIST(cv);
766 SV** svp = AvARRAY(padlist);
772 PUSHBLOCK(cx, CXt_SUB, stack_sp);
775 curpad = AvARRAY((AV*)svp[1]);
777 setdefout(gv); /* locally select filehandle so $% et al work */
810 SV *tmpsv = sv_newmortal();
811 gv_efullname(tmpsv, gv);
812 DIE("Undefined format \"%s\" called",SvPVX(tmpsv));
814 DIE("Not a format reference");
816 IoFLAGS(io) &= ~IOf_DIDTOP;
818 return doform(cv,gv,op->op_next);
824 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
825 register IO *io = GvIOp(gv);
826 FILE *ofp = IoOFP(io);
830 register CONTEXT *cx;
832 DEBUG_f(fprintf(stderr,"left=%ld, todo=%ld\n",
833 (long)IoLINES_LEFT(io), (long)FmLINES(formtarget)));
834 if (IoLINES_LEFT(io) < FmLINES(formtarget) &&
835 formtarget != toptarget)
843 if (!IoTOP_NAME(io)) {
845 IoFMT_NAME(io) = savepv(GvNAME(gv));
846 sprintf(tmpbuf, "%s_TOP", IoFMT_NAME(io));
847 topgv = gv_fetchpv(tmpbuf,FALSE, SVt_PVFM);
848 if ((topgv && GvFORM(topgv)) ||
849 !gv_fetchpv("top",FALSE,SVt_PVFM))
850 IoTOP_NAME(io) = savepv(tmpbuf);
852 IoTOP_NAME(io) = savepv("top");
854 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
855 if (!topgv || !GvFORM(topgv)) {
856 IoLINES_LEFT(io) = 100000000;
859 IoTOP_GV(io) = topgv;
861 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
862 I32 lines = IoLINES_LEFT(io);
863 char *s = SvPVX(formtarget);
864 if (lines <= 0) /* Yow, header didn't even fit!!! */
866 while (lines-- > 0) {
873 fwrite1(SvPVX(formtarget), s - SvPVX(formtarget), 1, ofp);
874 sv_chop(formtarget, s);
875 FmLINES(formtarget) -= IoLINES_LEFT(io);
878 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
879 fwrite1(SvPVX(formfeed), SvCUR(formfeed), 1, ofp);
880 IoLINES_LEFT(io) = IoPAGE_LEN(io);
882 formtarget = toptarget;
883 IoFLAGS(io) |= IOf_DIDTOP;
886 DIE("bad top format reference");
889 SV *tmpsv = sv_newmortal();
890 gv_efullname(tmpsv, fgv);
891 DIE("Undefined top format \"%s\" called",SvPVX(tmpsv));
893 return doform(cv,gv,op);
905 warn("Filehandle only opened for input");
907 warn("Write on closed filehandle");
912 if ((IoLINES_LEFT(io) -= FmLINES(formtarget)) < 0) {
914 warn("page overflow");
916 if (!fwrite1(SvPVX(formtarget), 1, SvCUR(formtarget), ofp) ||
920 FmLINES(formtarget) = 0;
921 SvCUR_set(formtarget, 0);
922 *SvEND(formtarget) = '\0';
923 if (IoFLAGS(io) & IOf_FLUSH)
928 formtarget = bodytarget;
935 dSP; dMARK; dORIGMARK;
941 if (op->op_flags & OPf_STACKED)
945 if (!(io = GvIO(gv))) {
948 warn("Filehandle %s never opened", SvPV(sv,na));
950 SETERRNO(EBADF,RMS$_IFI);
953 else if (!(fp = IoOFP(io))) {
957 warn("Filehandle %s opened only for input", SvPV(sv,na));
959 warn("printf on closed filehandle %s", SvPV(sv,na));
961 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
965 do_sprintf(sv, SP - MARK, MARK + 1);
966 if (!do_print(sv, fp))
969 if (IoFLAGS(io) & IOf_FLUSH)
970 if (Fflush(fp) == EOF)
1002 tmps = SvPV(sv, len);
1003 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1004 IoLINES(GvIOp(gv)) = 0;
1015 dSP; dMARK; dORIGMARK; dTARGET;
1029 buffer = SvPV_force(bufsv, blen);
1030 length = SvIVx(*++MARK);
1032 DIE("Negative length");
1035 offset = SvIVx(*++MARK);
1039 if (!io || !IoIFP(io))
1042 if (op->op_type == OP_RECV) {
1043 bufsize = sizeof buf;
1044 buffer = SvGROW(bufsv, length+1);
1045 length = recvfrom(fileno(IoIFP(io)), buffer, length, offset,
1046 (struct sockaddr *)buf, &bufsize);
1049 SvCUR_set(bufsv, length);
1050 *SvEND(bufsv) = '\0';
1051 (void)SvPOK_only(bufsv);
1054 sv_magic(bufsv, Nullsv, 't', Nullch, 0);
1056 sv_setpvn(TARG, buf, bufsize);
1061 if (op->op_type == OP_RECV)
1062 DIE(no_sock_func, "recv");
1064 buffer = SvGROW(bufsv, length+offset+1);
1065 if (op->op_type == OP_SYSREAD) {
1066 length = read(fileno(IoIFP(io)), buffer+offset, length);
1069 #ifdef HAS_SOCKET__bad_code_maybe
1070 if (IoTYPE(io) == 's') {
1071 bufsize = sizeof buf;
1072 length = recvfrom(fileno(IoIFP(io)), buffer+offset, length, 0,
1073 (struct sockaddr *)buf, &bufsize);
1077 length = fread(buffer+offset, 1, length, IoIFP(io));
1080 SvCUR_set(bufsv, length+offset);
1081 *SvEND(bufsv) = '\0';
1082 (void)SvPOK_only(bufsv);
1085 sv_magic(bufsv, Nullsv, 't', Nullch, 0);
1097 return pp_send(ARGS);
1102 dSP; dMARK; dORIGMARK; dTARGET;
1115 buffer = SvPV(bufsv, blen);
1116 length = SvIVx(*++MARK);
1118 DIE("Negative length");
1121 if (!io || !IoIFP(io)) {
1124 if (op->op_type == OP_SYSWRITE)
1125 warn("Syswrite on closed filehandle");
1127 warn("Send on closed socket");
1130 else if (op->op_type == OP_SYSWRITE) {
1132 offset = SvIVx(*++MARK);
1135 if (length > blen - offset)
1136 length = blen - offset;
1137 length = write(fileno(IoIFP(io)), buffer+offset, length);
1140 else if (SP > MARK) {
1143 sockbuf = SvPVx(*++MARK, mlen);
1144 length = sendto(fileno(IoIFP(io)), buffer, blen, length,
1145 (struct sockaddr *)sockbuf, mlen);
1148 length = send(fileno(IoIFP(io)), buffer, blen, length);
1151 DIE(no_sock_func, "send");
1166 return pp_sysread(ARGS);
1177 gv = last_in_gv = (GV*)POPs;
1178 PUSHs(!gv || do_eof(gv) ? &sv_yes : &sv_no);
1190 gv = last_in_gv = (GV*)POPs;
1191 PUSHi( do_tell(gv) );
1202 gv = last_in_gv = (GV*)POPs;
1203 PUSHs( do_seek(gv, offset, whence) ? &sv_yes : &sv_no );
1210 Off_t len = (Off_t)POPn;
1215 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
1217 if (op->op_flags & OPf_SPECIAL) {
1218 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1219 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1220 ftruncate(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1223 else if (truncate(POPp, len) < 0)
1226 if (op->op_flags & OPf_SPECIAL) {
1227 tmpgv = gv_fetchpv(POPp,FALSE, SVt_PVIO);
1228 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1229 chsize(fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
1235 if ((tmpfd = open(POPp, 0)) < 0)
1238 if (chsize(tmpfd, len) < 0)
1248 SETERRNO(EBADF,RMS$_IFI);
1251 DIE("truncate not implemented");
1257 return pp_ioctl(ARGS);
1264 unsigned int func = U_I(POPn);
1265 int optype = op->op_type;
1271 if (!io || !argsv || !IoIFP(io)) {
1272 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
1276 if (SvPOK(argsv) || !SvNIOK(argsv)) {
1278 s = SvPV_force(argsv, len);
1279 retval = IOCPARM_LEN(func);
1281 s = Sv_Grow(argsv, retval+1);
1282 SvCUR_set(argsv, retval);
1285 s[SvCUR(argsv)] = 17; /* a little sanity check here */
1288 retval = SvIV(argsv);
1290 s = (char*)(long)retval; /* ouch */
1292 s = (char*)retval; /* ouch */
1296 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
1298 if (optype == OP_IOCTL)
1300 retval = ioctl(fileno(IoIFP(io)), func, s);
1302 DIE("ioctl is not implemented");
1305 #if defined(DOSISH) && !defined(OS2)
1306 DIE("fcntl is not implemented");
1309 # if defined(OS2) && defined(__EMX__)
1310 retval = fcntl(fileno(IoIFP(io)), func, (int)s);
1312 retval = fcntl(fileno(IoIFP(io)), func, s);
1315 DIE("fcntl is not implemented");
1320 if (s[SvCUR(argsv)] != 17)
1321 DIE("Possible memory corruption: %s overflowed 3rd argument",
1323 s[SvCUR(argsv)] = 0; /* put our null back */
1324 SvSETMAGIC(argsv); /* Assume it has changed */
1333 PUSHp("0 but true", 10);
1346 #if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
1347 # define flock lockf_emulate_flock
1350 #if defined(HAS_FLOCK) || defined(flock)
1357 fp = IoIFP(GvIOp(gv));
1361 value = (I32)(flock(fileno(fp), argtype) >= 0);
1368 DIE(no_func, "flock()");
1380 int protocol = POPi;
1388 SETERRNO(EBADF,LIB$_INVARG);
1394 do_close(gv, FALSE);
1396 TAINT_PROPER("socket");
1397 fd = socket(domain, type, protocol);
1400 IoIFP(io) = fdopen(fd, "r"); /* stdio gets confused about sockets */
1401 IoOFP(io) = fdopen(fd, "w");
1403 if (!IoIFP(io) || !IoOFP(io)) {
1404 if (IoIFP(io)) fclose(IoIFP(io));
1405 if (IoOFP(io)) fclose(IoOFP(io));
1406 if (!IoIFP(io) && !IoOFP(io)) close(fd);
1412 DIE(no_sock_func, "socket");
1419 #ifdef HAS_SOCKETPAIR
1424 int protocol = POPi;
1437 do_close(gv1, FALSE);
1439 do_close(gv2, FALSE);
1441 TAINT_PROPER("socketpair");
1442 if (socketpair(domain, type, protocol, fd) < 0)
1444 IoIFP(io1) = fdopen(fd[0], "r");
1445 IoOFP(io1) = fdopen(fd[0], "w");
1447 IoIFP(io2) = fdopen(fd[1], "r");
1448 IoOFP(io2) = fdopen(fd[1], "w");
1450 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
1451 if (IoIFP(io1)) fclose(IoIFP(io1));
1452 if (IoOFP(io1)) fclose(IoOFP(io1));
1453 if (!IoIFP(io1) && !IoOFP(io1)) close(fd[0]);
1454 if (IoIFP(io2)) fclose(IoIFP(io2));
1455 if (IoOFP(io2)) fclose(IoOFP(io2));
1456 if (!IoIFP(io2) && !IoOFP(io2)) close(fd[1]);
1462 DIE(no_sock_func, "socketpair");
1473 register IO *io = GvIOn(gv);
1476 if (!io || !IoIFP(io))
1479 addr = SvPV(addrsv, len);
1480 TAINT_PROPER("bind");
1481 if (bind(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1488 warn("bind() on closed fd");
1489 SETERRNO(EBADF,SS$_IVCHAN);
1492 DIE(no_sock_func, "bind");
1503 register IO *io = GvIOn(gv);
1506 if (!io || !IoIFP(io))
1509 addr = SvPV(addrsv, len);
1510 TAINT_PROPER("connect");
1511 if (connect(fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
1518 warn("connect() on closed fd");
1519 SETERRNO(EBADF,SS$_IVCHAN);
1522 DIE(no_sock_func, "connect");
1532 register IO *io = GvIOn(gv);
1534 if (!io || !IoIFP(io))
1537 if (listen(fileno(IoIFP(io)), backlog) >= 0)
1544 warn("listen() on closed fd");
1545 SETERRNO(EBADF,SS$_IVCHAN);
1548 DIE(no_sock_func, "listen");
1560 struct sockaddr saddr; /* use a struct to avoid alignment problems */
1561 int len = sizeof saddr;
1573 if (!gstio || !IoIFP(gstio))
1578 do_close(ngv, FALSE);
1580 fd = accept(fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
1583 IoIFP(nstio) = fdopen(fd, "r");
1584 IoOFP(nstio) = fdopen(fd, "w");
1585 IoTYPE(nstio) = 's';
1586 if (!IoIFP(nstio) || !IoOFP(nstio)) {
1587 if (IoIFP(nstio)) fclose(IoIFP(nstio));
1588 if (IoOFP(nstio)) fclose(IoOFP(nstio));
1589 if (!IoIFP(nstio) && !IoOFP(nstio)) close(fd);
1593 PUSHp((char *)&saddr, len);
1598 warn("accept() on closed fd");
1599 SETERRNO(EBADF,SS$_IVCHAN);
1605 DIE(no_sock_func, "accept");
1615 register IO *io = GvIOn(gv);
1617 if (!io || !IoIFP(io))
1620 PUSHi( shutdown(fileno(IoIFP(io)), how) >= 0 );
1625 warn("shutdown() on closed fd");
1626 SETERRNO(EBADF,SS$_IVCHAN);
1629 DIE(no_sock_func, "shutdown");
1636 return pp_ssockopt(ARGS);
1638 DIE(no_sock_func, "getsockopt");
1646 int optype = op->op_type;
1649 unsigned int optname;
1655 if (optype == OP_GSOCKOPT)
1656 sv = sv_2mortal(NEWSV(22, 257));
1659 optname = (unsigned int) POPi;
1660 lvl = (unsigned int) POPi;
1664 if (!io || !IoIFP(io))
1667 fd = fileno(IoIFP(io));
1671 (void)SvPOK_only(sv);
1675 if (getsockopt(fd, lvl, optname, SvPVX(sv), &aint) < 0)
1685 buf = SvPV(sv, len);
1686 else if (SvOK(sv)) {
1687 aint = (int)SvIV(sv);
1691 if (setsockopt(fd, lvl, optname, buf, (int)len) < 0)
1701 warn("[gs]etsockopt() on closed fd");
1702 SETERRNO(EBADF,SS$_IVCHAN);
1707 DIE(no_sock_func, "setsockopt");
1714 return pp_getpeername(ARGS);
1716 DIE(no_sock_func, "getsockname");
1724 int optype = op->op_type;
1728 register IO *io = GvIOn(gv);
1731 if (!io || !IoIFP(io))
1734 sv = sv_2mortal(NEWSV(22, 257));
1735 (void)SvPOK_only(sv);
1739 fd = fileno(IoIFP(io));
1741 case OP_GETSOCKNAME:
1742 if (getsockname(fd, (struct sockaddr *)SvPVX(sv), &aint) < 0)
1745 case OP_GETPEERNAME:
1746 if (getpeername(fd, (struct sockaddr *)SvPVX(sv), &aint) < 0)
1757 warn("get{sock, peer}name() on closed fd");
1758 SETERRNO(EBADF,SS$_IVCHAN);
1763 DIE(no_sock_func, "getpeername");
1771 return pp_stat(ARGS);
1780 if (op->op_flags & OPf_REF) {
1781 tmpgv = cGVOP->op_gv;
1783 if (tmpgv != defgv) {
1784 laststype = OP_STAT;
1786 sv_setpv(statname, "");
1787 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
1788 Fstat(fileno(IoIFP(GvIOn(tmpgv))), &statcache) < 0) {
1793 else if (laststatval < 0)
1798 if (SvTYPE(sv) == SVt_PVGV) {
1802 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1803 tmpgv = (GV*)SvRV(sv);
1806 sv_setpv(statname, SvPV(sv,na));
1809 laststype = op->op_type;
1810 if (op->op_type == OP_LSTAT)
1811 laststatval = lstat(SvPV(statname, na), &statcache);
1814 laststatval = Stat(SvPV(statname, na), &statcache);
1815 if (laststatval < 0) {
1816 if (dowarn && strchr(SvPV(statname, na), '\n'))
1817 warn(warn_nl, "stat");
1823 if (GIMME != G_ARRAY) {
1830 PUSHs(sv_2mortal(newSViv((I32)statcache.st_dev)));
1831 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ino)));
1832 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mode)));
1833 PUSHs(sv_2mortal(newSViv((I32)statcache.st_nlink)));
1834 PUSHs(sv_2mortal(newSViv((I32)statcache.st_uid)));
1835 PUSHs(sv_2mortal(newSViv((I32)statcache.st_gid)));
1836 PUSHs(sv_2mortal(newSViv((I32)statcache.st_rdev)));
1837 PUSHs(sv_2mortal(newSViv((I32)statcache.st_size)));
1838 PUSHs(sv_2mortal(newSViv((I32)statcache.st_atime)));
1839 PUSHs(sv_2mortal(newSViv((I32)statcache.st_mtime)));
1840 PUSHs(sv_2mortal(newSViv((I32)statcache.st_ctime)));
1841 #ifdef USE_STAT_BLOCKS
1842 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blksize)));
1843 PUSHs(sv_2mortal(newSViv((I32)statcache.st_blocks)));
1845 PUSHs(sv_2mortal(newSVpv("", 0)));
1846 PUSHs(sv_2mortal(newSVpv("", 0)));
1854 I32 result = my_stat(ARGS);
1858 if (cando(S_IRUSR, 0, &statcache))
1865 I32 result = my_stat(ARGS);
1869 if (cando(S_IWUSR, 0, &statcache))
1876 I32 result = my_stat(ARGS);
1880 if (cando(S_IXUSR, 0, &statcache))
1887 I32 result = my_stat(ARGS);
1891 if (cando(S_IRUSR, 1, &statcache))
1898 I32 result = my_stat(ARGS);
1902 if (cando(S_IWUSR, 1, &statcache))
1909 I32 result = my_stat(ARGS);
1913 if (cando(S_IXUSR, 1, &statcache))
1920 I32 result = my_stat(ARGS);
1929 return pp_ftrowned(ARGS);
1934 I32 result = my_stat(ARGS);
1938 if (statcache.st_uid == (op->op_type == OP_FTEOWNED ? euid : uid) )
1945 I32 result = my_stat(ARGS);
1949 if (!statcache.st_size)
1956 I32 result = my_stat(ARGS);
1960 PUSHi(statcache.st_size);
1966 I32 result = my_stat(ARGS);
1970 PUSHn( ((I32)basetime - (I32)statcache.st_mtime) / 86400.0 );
1976 I32 result = my_stat(ARGS);
1980 PUSHn( ((I32)basetime - (I32)statcache.st_atime) / 86400.0 );
1986 I32 result = my_stat(ARGS);
1990 PUSHn( ((I32)basetime - (I32)statcache.st_ctime) / 86400.0 );
1996 I32 result = my_stat(ARGS);
2000 if (S_ISSOCK(statcache.st_mode))
2007 I32 result = my_stat(ARGS);
2011 if (S_ISCHR(statcache.st_mode))
2018 I32 result = my_stat(ARGS);
2022 if (S_ISBLK(statcache.st_mode))
2029 I32 result = my_stat(ARGS);
2033 if (S_ISREG(statcache.st_mode))
2040 I32 result = my_stat(ARGS);
2044 if (S_ISDIR(statcache.st_mode))
2051 I32 result = my_stat(ARGS);
2055 if (S_ISFIFO(statcache.st_mode))
2062 I32 result = my_lstat(ARGS);
2066 if (S_ISLNK(statcache.st_mode))
2075 I32 result = my_stat(ARGS);
2079 if (statcache.st_mode & S_ISUID)
2089 I32 result = my_stat(ARGS);
2093 if (statcache.st_mode & S_ISGID)
2103 I32 result = my_stat(ARGS);
2107 if (statcache.st_mode & S_ISVTX)
2119 if (op->op_flags & OPf_REF) {
2124 gv = gv_fetchpv(tmps = POPp, FALSE, SVt_PVIO);
2125 if (GvIO(gv) && IoIFP(GvIOp(gv)))
2126 fd = fileno(IoIFP(GvIOp(gv)));
2127 else if (isDIGIT(*tmps))
2136 #if defined(atarist) /* this will work with atariST. Configure will
2137 make guesses for other systems. */
2138 # define FILE_base(f) ((f)->_base)
2139 # define FILE_ptr(f) ((f)->_ptr)
2140 # define FILE_cnt(f) ((f)->_cnt)
2141 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
2151 register STDCHAR *s;
2155 if (op->op_flags & OPf_REF) {
2157 if (cGVOP->op_gv == defgv) {
2162 goto really_filename;
2166 statgv = cGVOP->op_gv;
2167 sv_setpv(statname, "");
2170 if (io && IoIFP(io)) {
2172 Fstat(fileno(IoIFP(io)), &statcache);
2173 if (S_ISDIR(statcache.st_mode)) /* handle NFS glitch */
2174 if (op->op_type == OP_FTTEXT)
2178 if (FILE_cnt(IoIFP(io)) <= 0) {
2179 i = getc(IoIFP(io));
2181 (void)ungetc(i, IoIFP(io));
2183 if (FILE_cnt(IoIFP(io)) <= 0) /* null file is anything */
2185 len = FILE_bufsiz(IoIFP(io));
2186 s = FILE_base(IoIFP(io));
2188 DIE("-T and -B not implemented on filehandles");
2193 warn("Test on unopened file <%s>",
2194 GvENAME(cGVOP->op_gv));
2195 SETERRNO(EBADF,RMS$_IFI);
2202 sv_setpv(statname, SvPV(sv, na));
2205 i = open(SvPV(sv, na), O_RDONLY, 0);
2207 i = open(SvPV(sv, na), 0);
2210 if (dowarn && strchr(SvPV(sv, na), '\n'))
2211 warn(warn_nl, "open");
2214 Fstat(i, &statcache);
2215 len = read(i, tbuf, 512);
2218 if (S_ISDIR(statcache.st_mode) && op->op_type == OP_FTTEXT)
2219 RETPUSHNO; /* special case NFS directories */
2220 RETPUSHYES; /* null file is anything */
2225 /* now scan s to look for textiness */
2226 /* XXX ASCII dependent code */
2228 for (i = 0; i < len; i++, s++) {
2229 if (!*s) { /* null never allowed in text */
2236 *s != '\n' && *s != '\r' && *s != '\b' &&
2237 *s != '\t' && *s != '\f' && *s != 27)
2241 if ((odd * 3 > len) == (op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
2249 return pp_fttext(ARGS);
2264 if (!tmps || !*tmps) {
2265 svp = hv_fetch(GvHVn(envgv), "HOME", 4, FALSE);
2267 tmps = SvPV(*svp, na);
2269 if (!tmps || !*tmps) {
2270 svp = hv_fetch(GvHVn(envgv), "LOGDIR", 6, FALSE);
2272 tmps = SvPV(*svp, na);
2274 TAINT_PROPER("chdir");
2275 PUSHi( chdir(tmps) >= 0 );
2277 /* Clear the DEFAULT element of ENV so we'll get the new value
2279 hv_delete(GvHVn(envgv),"DEFAULT",7,G_DISCARD);
2286 dSP; dMARK; dTARGET;
2289 value = (I32)apply(op->op_type, MARK, SP);
2294 DIE(no_func, "Unsupported function chown");
2304 TAINT_PROPER("chroot");
2305 PUSHi( chroot(tmps) >= 0 );
2308 DIE(no_func, "chroot");
2314 dSP; dMARK; dTARGET;
2316 value = (I32)apply(op->op_type, MARK, SP);
2324 dSP; dMARK; dTARGET;
2326 value = (I32)apply(op->op_type, MARK, SP);
2334 dSP; dMARK; dTARGET;
2336 value = (I32)apply(op->op_type, MARK, SP);
2348 char *tmps = SvPV(TOPs, na);
2349 TAINT_PROPER("rename");
2351 anum = rename(tmps, tmps2);
2353 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
2356 if (euid || Stat(tmps2, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
2357 (void)UNLINK(tmps2);
2358 if (!(anum = link(tmps, tmps2)))
2359 anum = UNLINK(tmps);
2371 char *tmps = SvPV(TOPs, na);
2372 TAINT_PROPER("link");
2373 SETi( link(tmps, tmps2) >= 0 );
2375 DIE(no_func, "Unsupported function link");
2385 char *tmps = SvPV(TOPs, na);
2386 TAINT_PROPER("symlink");
2387 SETi( symlink(tmps, tmps2) >= 0 );
2390 DIE(no_func, "symlink");
2401 len = readlink(tmps, buf, sizeof buf);
2409 RETSETUNDEF; /* just pretend it's a normal file */
2413 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
2415 dooneliner(cmd, filename)
2421 *save_filename = filename;
2427 for (s = mybuf+strlen(mybuf); *filename; ) {
2432 myfp = my_popen(mybuf, "r");
2435 s = fgets(mybuf, sizeof mybuf, myfp);
2436 (void)my_pclose(myfp);
2438 for (errno = 1; errno < sys_nerr; errno++) {
2439 #ifdef HAS_SYS_ERRLIST
2440 if (instr(mybuf, sys_errlist[errno])) /* you don't see this */
2443 char *errmsg; /* especially if it isn't there */
2446 (errmsg = strerror(errno)) ? errmsg : "NoErRoR"))
2452 #define EACCES EPERM
2454 if (instr(mybuf, "cannot make"))
2455 SETERRNO(EEXIST,RMS$_FEX);
2456 else if (instr(mybuf, "existing file"))
2457 SETERRNO(EEXIST,RMS$_FEX);
2458 else if (instr(mybuf, "ile exists"))
2459 SETERRNO(EEXIST,RMS$_FEX);
2460 else if (instr(mybuf, "non-exist"))
2461 SETERRNO(ENOENT,RMS$_FNF);
2462 else if (instr(mybuf, "does not exist"))
2463 SETERRNO(ENOENT,RMS$_FNF);
2464 else if (instr(mybuf, "not empty"))
2465 SETERRNO(EBUSY,SS$_DEVOFFLINE);
2466 else if (instr(mybuf, "cannot access"))
2467 SETERRNO(EACCES,RMS$_PRV);
2469 SETERRNO(EPERM,RMS$_PRV);
2472 else { /* some mkdirs return no failure indication */
2473 anum = (Stat(save_filename, &statbuf) >= 0);
2474 if (op->op_type == OP_RMDIR)
2479 SETERRNO(EACCES,RMS$_PRV); /* a guess */
2495 char *tmps = SvPV(TOPs, na);
2497 TAINT_PROPER("mkdir");
2499 SETi( mkdir(tmps, mode) >= 0 );
2501 SETi( dooneliner("mkdir", tmps) );
2502 oldumask = umask(0);
2504 chmod(tmps, (mode & ~oldumask) & 0777);
2515 TAINT_PROPER("rmdir");
2517 XPUSHi( rmdir(tmps) >= 0 );
2519 XPUSHi( dooneliner("rmdir", tmps) );
2524 /* Directory calls. */
2529 #if defined(Direntry_t) && defined(HAS_READDIR)
2530 char *dirname = POPp;
2532 register IO *io = GvIOn(gv);
2538 closedir(IoDIRP(io));
2539 if (!(IoDIRP(io) = opendir(dirname)))
2545 SETERRNO(EBADF,RMS$_DIR);
2548 DIE(no_dir_func, "opendir");
2555 #if defined(Direntry_t) && defined(HAS_READDIR)
2557 Direntry_t *readdir _((DIR *));
2559 register Direntry_t *dp;
2561 register IO *io = GvIOn(gv);
2563 if (!io || !IoDIRP(io))
2566 if (GIMME == G_ARRAY) {
2568 while (dp = (Direntry_t *)readdir(IoDIRP(io))) {
2570 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2572 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2577 if (!(dp = (Direntry_t *)readdir(IoDIRP(io))))
2580 XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
2582 XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
2589 SETERRNO(EBADF,RMS$_ISI);
2590 if (GIMME == G_ARRAY)
2595 DIE(no_dir_func, "readdir");
2602 #if defined(HAS_TELLDIR) || defined(telldir)
2603 #if !defined(telldir) && !defined(HAS_TELLDIR_PROTOTYPE)
2604 long telldir _((DIR *));
2607 register IO *io = GvIOn(gv);
2609 if (!io || !IoDIRP(io))
2612 PUSHi( telldir(IoDIRP(io)) );
2616 SETERRNO(EBADF,RMS$_ISI);
2619 DIE(no_dir_func, "telldir");
2626 #if defined(HAS_SEEKDIR) || defined(seekdir)
2629 register IO *io = GvIOn(gv);
2631 if (!io || !IoDIRP(io))
2634 (void)seekdir(IoDIRP(io), along);
2639 SETERRNO(EBADF,RMS$_ISI);
2642 DIE(no_dir_func, "seekdir");
2649 #if defined(HAS_REWINDDIR) || defined(rewinddir)
2651 register IO *io = GvIOn(gv);
2653 if (!io || !IoDIRP(io))
2656 (void)rewinddir(IoDIRP(io));
2660 SETERRNO(EBADF,RMS$_ISI);
2663 DIE(no_dir_func, "rewinddir");
2670 #if defined(Direntry_t) && defined(HAS_READDIR)
2672 register IO *io = GvIOn(gv);
2674 if (!io || !IoDIRP(io))
2677 #ifdef VOID_CLOSEDIR
2678 closedir(IoDIRP(io));
2680 if (closedir(IoDIRP(io)) < 0) {
2681 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
2690 SETERRNO(EBADF,RMS$_IFI);
2693 DIE(no_dir_func, "closedir");
2697 /* Process control. */
2712 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
2713 sv_setiv(GvSV(tmpgv), (I32)getpid());
2714 hv_clear(pidstatus); /* no kids, so don't wait for 'em */
2719 DIE(no_func, "Unsupported function fork");
2732 childpid = wait(&argflags);
2734 pidgone(childpid, argflags);
2735 value = (I32)childpid;
2736 statusvalue = FIXSTATUS(argflags);
2740 DIE(no_func, "Unsupported function wait");
2755 childpid = wait4pid(childpid, &argflags, optype);
2756 value = (I32)childpid;
2757 statusvalue = FIXSTATUS(argflags);
2761 DIE(no_func, "Unsupported function wait");
2767 dSP; dMARK; dORIGMARK; dTARGET;
2772 Signal_t (*ihand)(); /* place to save signal during system() */
2773 Signal_t (*qhand)(); /* place to save signal during system() */
2775 #if defined(HAS_FORK) && !defined(VMS) && !defined(OS2)
2776 if (SP - MARK == 1) {
2778 char *junk = SvPV(TOPs, na);
2780 TAINT_PROPER("system");
2783 while ((childpid = vfork()) == -1) {
2784 if (errno != EAGAIN) {
2793 ihand = signal(SIGINT, SIG_IGN);
2794 qhand = signal(SIGQUIT, SIG_IGN);
2796 result = wait4pid(childpid, &status, 0);
2797 } while (result == -1 && errno == EINTR);
2798 (void)signal(SIGINT, ihand);
2799 (void)signal(SIGQUIT, qhand);
2800 statusvalue = FIXSTATUS(status);
2804 value = (I32)((unsigned int)status & 0xffff);
2806 do_execfree(); /* free any memory child malloced on vfork */
2811 if (op->op_flags & OPf_STACKED) {
2812 SV *really = *++MARK;
2813 value = (I32)do_aexec(really, MARK, SP);
2815 else if (SP - MARK != 1)
2816 value = (I32)do_aexec(Nullsv, MARK, SP);
2818 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2821 #else /* ! FORK or VMS or OS/2 */
2822 if (op->op_flags & OPf_STACKED) {
2823 SV *really = *++MARK;
2824 value = (I32)do_aspawn(really, MARK, SP);
2826 else if (SP - MARK != 1)
2827 value = (I32)do_aspawn(Nullsv, MARK, SP);
2829 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), na));
2831 statusvalue = FIXSTATUS(value);
2835 #endif /* !FORK or VMS */
2841 dSP; dMARK; dORIGMARK; dTARGET;
2844 if (op->op_flags & OPf_STACKED) {
2845 SV *really = *++MARK;
2846 value = (I32)do_aexec(really, MARK, SP);
2848 else if (SP - MARK != 1)
2850 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
2852 value = (I32)do_aexec(Nullsv, MARK, SP);
2856 char *junk = SvPV(*SP, na);
2858 TAINT_PROPER("exec");
2861 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), na));
2863 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
2873 dSP; dMARK; dTARGET;
2876 value = (I32)apply(op->op_type, MARK, SP);
2881 DIE(no_func, "Unsupported function kill");
2889 XPUSHi( getppid() );
2892 DIE(no_func, "getppid");
2908 value = (I32)BSD_GETPGRP(pid);
2911 DIE("POSIX getpgrp can't take an argument");
2912 value = (I32)getpgrp();
2917 DIE(no_func, "getpgrp()");
2936 TAINT_PROPER("setpgrp");
2938 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
2940 if ((pgrp != 0) || (pid != 0)) {
2941 DIE("POSIX setpgrp can't take an argument");
2943 SETi( setpgrp() >= 0 );
2944 #endif /* USE_BSDPGRP */
2947 DIE(no_func, "setpgrp()");
2956 #ifdef HAS_GETPRIORITY
2959 SETi( getpriority(which, who) );
2962 DIE(no_func, "getpriority()");
2972 #ifdef HAS_SETPRIORITY
2976 TAINT_PROPER("setpriority");
2977 SETi( setpriority(which, who, niceval) >= 0 );
2980 DIE(no_func, "setpriority()");
2989 XPUSHi( time(Null(Time_t*)) );
3001 #if defined(MSDOS) || !defined(HAS_TIMES)
3002 DIE("times not implemented");
3007 (void)times(×buf);
3009 (void)times((tbuffer_t *)×buf); /* time.h uses different name for */
3010 /* struct tms, though same data */
3016 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_utime)/HZ)));
3017 if (GIMME == G_ARRAY) {
3018 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_stime)/HZ)));
3019 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cutime)/HZ)));
3020 PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_cstime)/HZ)));
3028 return pp_gmtime(ARGS);
3036 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
3037 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3038 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
3043 when = (Time_t)SvIVx(POPs);
3045 if (op->op_type == OP_LOCALTIME)
3046 tmbuf = localtime(&when);
3048 tmbuf = gmtime(&when);
3051 if (GIMME != G_ARRAY) {
3056 sprintf(mybuf, "%s %s %2d %02d:%02d:%02d %d",
3057 dayname[tmbuf->tm_wday],
3058 monname[tmbuf->tm_mon],
3063 tmbuf->tm_year + 1900);
3064 PUSHp(mybuf, strlen(mybuf));
3067 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_sec)));
3068 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_min)));
3069 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_hour)));
3070 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mday)));
3071 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_mon)));
3072 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_year)));
3073 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_wday)));
3074 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_yday)));
3075 PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_isdst)));
3086 anum = alarm((unsigned int)anum);
3093 DIE(no_func, "Unsupported function alarm");
3104 (void)time(&lasttime);
3109 sleep((unsigned int)duration);
3112 XPUSHi(when - lasttime);
3116 /* Shared memory. */
3120 return pp_semget(ARGS);
3125 return pp_semctl(ARGS);
3130 return pp_shmwrite(ARGS);
3135 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3136 dSP; dMARK; dTARGET;
3137 I32 value = (I32)(do_shmio(op->op_type, MARK, SP) >= 0);
3142 return pp_semget(ARGS);
3146 /* Message passing. */
3150 return pp_semget(ARGS);
3155 return pp_semctl(ARGS);
3160 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3161 dSP; dMARK; dTARGET;
3162 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
3167 return pp_semget(ARGS);
3173 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3174 dSP; dMARK; dTARGET;
3175 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
3180 return pp_semget(ARGS);
3188 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3189 dSP; dMARK; dTARGET;
3190 int anum = do_ipcget(op->op_type, MARK, SP);
3197 DIE("System V IPC is not implemented on this machine");
3203 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3204 dSP; dMARK; dTARGET;
3205 int anum = do_ipcctl(op->op_type, MARK, SP);
3213 PUSHp("0 but true",10);
3217 return pp_semget(ARGS);
3223 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
3224 dSP; dMARK; dTARGET;
3225 I32 value = (I32)(do_semop(MARK, SP) >= 0);
3230 return pp_semget(ARGS);
3234 /* Get system info. */
3239 return pp_ghostent(ARGS);
3241 DIE(no_sock_func, "gethostbyname");
3248 return pp_ghostent(ARGS);
3250 DIE(no_sock_func, "gethostbyaddr");
3258 I32 which = op->op_type;
3259 register char **elem;
3261 struct hostent *gethostbyname();
3262 struct hostent *gethostbyaddr();
3263 #ifdef HAS_GETHOSTENT
3264 struct hostent *gethostent();
3266 struct hostent *hent;
3270 if (which == OP_GHBYNAME) {
3271 hent = gethostbyname(POPp);
3273 else if (which == OP_GHBYADDR) {
3274 int addrtype = POPi;
3277 char *addr = SvPV(addrsv, addrlen);
3279 hent = gethostbyaddr(addr, addrlen, addrtype);
3282 #ifdef HAS_GETHOSTENT
3283 hent = gethostent();
3285 DIE("gethostent not implemented");
3288 #ifdef HOST_NOT_FOUND
3290 statusvalue = FIXSTATUS(h_errno);
3293 if (GIMME != G_ARRAY) {
3294 PUSHs(sv = sv_newmortal());
3296 if (which == OP_GHBYNAME) {
3298 sv_setpvn(sv, hent->h_addr, hent->h_length);
3301 sv_setpv(sv, (char*)hent->h_name);
3307 PUSHs(sv = sv_mortalcopy(&sv_no));
3308 sv_setpv(sv, (char*)hent->h_name);
3309 PUSHs(sv = sv_mortalcopy(&sv_no));
3310 for (elem = hent->h_aliases; elem && *elem; elem++) {
3311 sv_catpv(sv, *elem);
3313 sv_catpvn(sv, " ", 1);
3315 PUSHs(sv = sv_mortalcopy(&sv_no));
3316 sv_setiv(sv, (I32)hent->h_addrtype);
3317 PUSHs(sv = sv_mortalcopy(&sv_no));
3318 len = hent->h_length;
3319 sv_setiv(sv, (I32)len);
3321 for (elem = hent->h_addr_list; elem && *elem; elem++) {
3322 XPUSHs(sv = sv_mortalcopy(&sv_no));
3323 sv_setpvn(sv, *elem, len);
3326 PUSHs(sv = sv_mortalcopy(&sv_no));
3328 sv_setpvn(sv, hent->h_addr, len);
3333 DIE(no_sock_func, "gethostent");
3340 return pp_gnetent(ARGS);
3342 DIE(no_sock_func, "getnetbyname");
3349 return pp_gnetent(ARGS);
3351 DIE(no_sock_func, "getnetbyaddr");
3359 I32 which = op->op_type;
3360 register char **elem;
3362 struct netent *getnetbyname();
3363 struct netent *getnetbyaddr();
3364 struct netent *getnetent();
3365 struct netent *nent;
3367 if (which == OP_GNBYNAME)
3368 nent = getnetbyname(POPp);
3369 else if (which == OP_GNBYADDR) {
3370 int addrtype = POPi;
3371 unsigned long addr = U_L(POPn);
3372 nent = getnetbyaddr((long)addr, addrtype);
3378 if (GIMME != G_ARRAY) {
3379 PUSHs(sv = sv_newmortal());
3381 if (which == OP_GNBYNAME)
3382 sv_setiv(sv, (I32)nent->n_net);
3384 sv_setpv(sv, nent->n_name);
3390 PUSHs(sv = sv_mortalcopy(&sv_no));
3391 sv_setpv(sv, nent->n_name);
3392 PUSHs(sv = sv_mortalcopy(&sv_no));
3393 for (elem = nent->n_aliases; *elem; elem++) {
3394 sv_catpv(sv, *elem);
3396 sv_catpvn(sv, " ", 1);
3398 PUSHs(sv = sv_mortalcopy(&sv_no));
3399 sv_setiv(sv, (I32)nent->n_addrtype);
3400 PUSHs(sv = sv_mortalcopy(&sv_no));
3401 sv_setiv(sv, (I32)nent->n_net);
3406 DIE(no_sock_func, "getnetent");
3413 return pp_gprotoent(ARGS);
3415 DIE(no_sock_func, "getprotobyname");
3422 return pp_gprotoent(ARGS);
3424 DIE(no_sock_func, "getprotobynumber");
3432 I32 which = op->op_type;
3433 register char **elem;
3435 struct protoent *getprotobyname();
3436 struct protoent *getprotobynumber();
3437 struct protoent *getprotoent();
3438 struct protoent *pent;
3440 if (which == OP_GPBYNAME)
3441 pent = getprotobyname(POPp);
3442 else if (which == OP_GPBYNUMBER)
3443 pent = getprotobynumber(POPi);
3445 pent = getprotoent();
3448 if (GIMME != G_ARRAY) {
3449 PUSHs(sv = sv_newmortal());
3451 if (which == OP_GPBYNAME)
3452 sv_setiv(sv, (I32)pent->p_proto);
3454 sv_setpv(sv, pent->p_name);
3460 PUSHs(sv = sv_mortalcopy(&sv_no));
3461 sv_setpv(sv, pent->p_name);
3462 PUSHs(sv = sv_mortalcopy(&sv_no));
3463 for (elem = pent->p_aliases; *elem; elem++) {
3464 sv_catpv(sv, *elem);
3466 sv_catpvn(sv, " ", 1);
3468 PUSHs(sv = sv_mortalcopy(&sv_no));
3469 sv_setiv(sv, (I32)pent->p_proto);
3474 DIE(no_sock_func, "getprotoent");
3481 return pp_gservent(ARGS);
3483 DIE(no_sock_func, "getservbyname");
3490 return pp_gservent(ARGS);
3492 DIE(no_sock_func, "getservbyport");
3500 I32 which = op->op_type;
3501 register char **elem;
3503 struct servent *getservbyname();
3504 struct servent *getservbynumber();
3505 struct servent *getservent();
3506 struct servent *sent;
3508 if (which == OP_GSBYNAME) {
3512 if (proto && !*proto)
3515 sent = getservbyname(name, proto);
3517 else if (which == OP_GSBYPORT) {
3521 sent = getservbyport(port, proto);
3524 sent = getservent();
3527 if (GIMME != G_ARRAY) {
3528 PUSHs(sv = sv_newmortal());
3530 if (which == OP_GSBYNAME) {
3532 sv_setiv(sv, (I32)ntohs(sent->s_port));
3534 sv_setiv(sv, (I32)(sent->s_port));
3538 sv_setpv(sv, sent->s_name);
3544 PUSHs(sv = sv_mortalcopy(&sv_no));
3545 sv_setpv(sv, sent->s_name);
3546 PUSHs(sv = sv_mortalcopy(&sv_no));
3547 for (elem = sent->s_aliases; *elem; elem++) {
3548 sv_catpv(sv, *elem);
3550 sv_catpvn(sv, " ", 1);
3552 PUSHs(sv = sv_mortalcopy(&sv_no));
3554 sv_setiv(sv, (I32)ntohs(sent->s_port));
3556 sv_setiv(sv, (I32)(sent->s_port));
3558 PUSHs(sv = sv_mortalcopy(&sv_no));
3559 sv_setpv(sv, sent->s_proto);
3564 DIE(no_sock_func, "getservent");
3575 DIE(no_sock_func, "sethostent");
3586 DIE(no_sock_func, "setnetent");
3597 DIE(no_sock_func, "setprotoent");
3608 DIE(no_sock_func, "setservent");
3620 DIE(no_sock_func, "endhostent");
3632 DIE(no_sock_func, "endnetent");
3644 DIE(no_sock_func, "endprotoent");
3656 DIE(no_sock_func, "endservent");
3663 return pp_gpwent(ARGS);
3665 DIE(no_func, "getpwnam");
3672 return pp_gpwent(ARGS);
3674 DIE(no_func, "getpwuid");
3682 I32 which = op->op_type;
3684 struct passwd *pwent;
3686 if (which == OP_GPWNAM)
3687 pwent = getpwnam(POPp);
3688 else if (which == OP_GPWUID)
3689 pwent = getpwuid(POPi);
3691 pwent = (struct passwd *)getpwent();
3694 if (GIMME != G_ARRAY) {
3695 PUSHs(sv = sv_newmortal());
3697 if (which == OP_GPWNAM)
3698 sv_setiv(sv, (I32)pwent->pw_uid);
3700 sv_setpv(sv, pwent->pw_name);
3706 PUSHs(sv = sv_mortalcopy(&sv_no));
3707 sv_setpv(sv, pwent->pw_name);
3708 PUSHs(sv = sv_mortalcopy(&sv_no));
3709 sv_setpv(sv, pwent->pw_passwd);
3710 PUSHs(sv = sv_mortalcopy(&sv_no));
3711 sv_setiv(sv, (I32)pwent->pw_uid);
3712 PUSHs(sv = sv_mortalcopy(&sv_no));
3713 sv_setiv(sv, (I32)pwent->pw_gid);
3714 PUSHs(sv = sv_mortalcopy(&sv_no));
3716 sv_setiv(sv, (I32)pwent->pw_change);
3719 sv_setiv(sv, (I32)pwent->pw_quota);
3722 sv_setpv(sv, pwent->pw_age);
3726 PUSHs(sv = sv_mortalcopy(&sv_no));
3728 sv_setpv(sv, pwent->pw_class);
3731 sv_setpv(sv, pwent->pw_comment);
3734 PUSHs(sv = sv_mortalcopy(&sv_no));
3735 sv_setpv(sv, pwent->pw_gecos);
3736 PUSHs(sv = sv_mortalcopy(&sv_no));
3737 sv_setpv(sv, pwent->pw_dir);
3738 PUSHs(sv = sv_mortalcopy(&sv_no));
3739 sv_setpv(sv, pwent->pw_shell);
3741 PUSHs(sv = sv_mortalcopy(&sv_no));
3742 sv_setiv(sv, (I32)pwent->pw_expire);
3747 DIE(no_func, "getpwent");
3758 DIE(no_func, "setpwent");
3769 DIE(no_func, "endpwent");
3776 return pp_ggrent(ARGS);
3778 DIE(no_func, "getgrnam");
3785 return pp_ggrent(ARGS);
3787 DIE(no_func, "getgrgid");
3795 I32 which = op->op_type;
3796 register char **elem;
3798 struct group *grent;
3800 if (which == OP_GGRNAM)
3801 grent = (struct group *)getgrnam(POPp);
3802 else if (which == OP_GGRGID)
3803 grent = (struct group *)getgrgid(POPi);
3805 grent = (struct group *)getgrent();
3808 if (GIMME != G_ARRAY) {
3809 PUSHs(sv = sv_newmortal());
3811 if (which == OP_GGRNAM)
3812 sv_setiv(sv, (I32)grent->gr_gid);
3814 sv_setpv(sv, grent->gr_name);
3820 PUSHs(sv = sv_mortalcopy(&sv_no));
3821 sv_setpv(sv, grent->gr_name);
3822 PUSHs(sv = sv_mortalcopy(&sv_no));
3823 sv_setpv(sv, grent->gr_passwd);
3824 PUSHs(sv = sv_mortalcopy(&sv_no));
3825 sv_setiv(sv, (I32)grent->gr_gid);
3826 PUSHs(sv = sv_mortalcopy(&sv_no));
3827 for (elem = grent->gr_mem; *elem; elem++) {
3828 sv_catpv(sv, *elem);
3830 sv_catpvn(sv, " ", 1);
3836 DIE(no_func, "getgrent");
3847 DIE(no_func, "setgrent");
3858 DIE(no_func, "endgrent");
3868 if (!(tmps = getlogin()))
3870 PUSHp(tmps, strlen(tmps));
3873 DIE(no_func, "getlogin");
3877 /* Miscellaneous. */
3882 dSP; dMARK; dORIGMARK; dTARGET;
3883 register I32 items = SP - MARK;
3884 unsigned long a[20];
3890 while (++MARK <= SP) {
3891 if (SvGMAGICAL(*MARK) && SvSMAGICAL(*MARK) &&
3892 (mg = mg_find(*MARK, 't')) && mg->mg_len & 1)
3896 TAINT_PROPER("syscall");
3899 /* This probably won't work on machines where sizeof(long) != sizeof(int)
3900 * or where sizeof(long) != sizeof(char*). But such machines will
3901 * not likely have syscall implemented either, so who cares?
3903 while (++MARK <= SP) {
3904 if (SvNIOK(*MARK) || !i)
3905 a[i++] = SvIV(*MARK);
3906 else if (*MARK == &sv_undef)
3909 a[i++] = (unsigned long)SvPV_force(*MARK, na);
3915 DIE("Too many args to syscall");
3917 DIE("Too few args to syscall");
3919 retval = syscall(a[0]);
3922 retval = syscall(a[0],a[1]);
3925 retval = syscall(a[0],a[1],a[2]);
3928 retval = syscall(a[0],a[1],a[2],a[3]);
3931 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
3934 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
3937 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
3940 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
3944 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
3947 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
3950 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3954 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3958 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3962 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
3963 a[10],a[11],a[12],a[13]);
3965 #endif /* atarist */
3971 DIE(no_func, "syscall");
3975 #if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
3977 /* XXX Emulate flock() with lockf(). This is just to increase
3978 portability of scripts. The calls are not completely
3979 interchangeable. What's really needed is a good file
3983 /* We might need <unistd.h> because it sometimes defines the lockf()
3984 constants. Unfortunately, <unistd.h> causes troubles on some mixed
3985 (BSD/POSIX) systems, such as SunOS 4.1.3. We could just try including
3986 <unistd.h> here in this part of the file, but that might
3987 conflict with various other #defines and includes above, such as
3988 #define vfork fork above.
3990 Further, the lockf() constants aren't POSIX, so they might not be
3991 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
3992 just stick in the SVID values and be done with it. Sigh.
3996 # define F_ULOCK 0 /* Unlock a previously locked region */
3999 # define F_LOCK 1 /* Lock a region for exclusive use */
4002 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
4005 # define F_TEST 3 /* Test a region for other processes locks */
4008 /* These are the flock() constants. Since this sytems doesn't have
4009 flock(), the values of the constants are probably not available.
4025 lockf_emulate_flock (fd, operation)
4030 switch (operation) {
4032 /* LOCK_SH - get a shared lock */
4034 /* LOCK_EX - get an exclusive lock */
4036 i = lockf (fd, F_LOCK, 0);
4039 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
4040 case LOCK_SH|LOCK_NB:
4041 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
4042 case LOCK_EX|LOCK_NB:
4043 i = lockf (fd, F_TLOCK, 0);
4045 if ((errno == EAGAIN) || (errno == EACCES))
4046 errno = EWOULDBLOCK;
4049 /* LOCK_UN - unlock */
4051 i = lockf (fd, F_ULOCK, 0);
4054 /* Default - can't decipher operation */