1 /* $RCSfile: doio.c,v $$Revision: 4.1 $$Date: 92/08/07 17:19:42 $
3 * Copyright (c) 1991, 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.
9 * Revision 4.1 92/08/07 17:19:42 lwall
12 * Revision 4.0.1.6 92/06/11 21:08:16 lwall
13 * patch34: some systems don't declare h_errno extern in header files
15 * Revision 4.0.1.5 92/06/08 13:00:21 lwall
16 * patch20: some machines don't define ENOTSOCK in errno.h
17 * patch20: new warnings for failed use of stat operators on filenames with \n
18 * patch20: wait failed when STDOUT or STDERR reopened to a pipe
19 * patch20: end of file latch not reset on reopen of STDIN
20 * patch20: seek(HANDLE, 0, 1) went to eof because of ancient Ultrix workaround
21 * patch20: fixed memory leak on system() for vfork() machines
22 * patch20: get*by* routines now return something useful in a scalar context
23 * patch20: h_errno now accessible via $?
25 * Revision 4.0.1.4 91/11/05 16:51:43 lwall
26 * patch11: prepared for ctype implementations that don't define isascii()
27 * patch11: perl mistook some streams for sockets because they return mode 0 too
28 * patch11: reopening STDIN, STDOUT and STDERR failed on some machines
29 * patch11: certain perl errors should set EBADF so that $! looks better
30 * patch11: truncate on a closed filehandle could dump
31 * patch11: stats of _ forgot whether prior stat was actually lstat
32 * patch11: -T returned true on NFS directory
34 * Revision 4.0.1.3 91/06/10 01:21:19 lwall
35 * patch10: read didn't work from character special files open for writing
36 * patch10: close-on-exec wrongly set on system file descriptors
38 * Revision 4.0.1.2 91/06/07 10:53:39 lwall
39 * patch4: new copyright notice
40 * patch4: system fd's are now treated specially
41 * patch4: added $^F variable to specify maximum system fd, default 2
42 * patch4: character special files now opened with bidirectional stdio buffers
43 * patch4: taintchecks could improperly modify parent in vfork()
44 * patch4: many, many itty-bitty portability fixes
46 * Revision 4.0.1.1 91/04/11 17:41:06 lwall
47 * patch1: hopefully straightened out some of the Xenix mess
49 * Revision 4.0 91/03/20 01:07:06 lwall
57 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
87 register IO *io = GvIO(gv);
88 char *myname = savestr(name);
92 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
93 FILE *saveifp = Nullfp;
94 FILE *saveofp = Nullfp;
97 mode[0] = mode[1] = mode[2] = '\0';
99 forkprocess = 1; /* assume true if no fork */
100 while (len && isSPACE(name[len-1]))
103 io = GvIO(gv) = newIO();
105 fd = fileno(io->ifp);
108 else if (fd <= maxsysfd) {
114 else if (io->type == '|')
115 result = my_pclose(io->ifp);
116 else if (io->ifp != io->ofp) {
118 result = fclose(io->ofp);
119 fclose(io->ifp); /* clear stdio, fd already closed */
122 result = fclose(io->ifp);
125 result = fclose(io->ifp);
126 if (result == EOF && fd > maxsysfd)
127 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
129 io->ofp = io->ifp = Nullfp;
131 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
143 for (name++; isSPACE(*name); name++) ;
146 TAINT_PROPER("piped open");
147 fp = my_popen(name,"w");
150 else if (*name == '>') {
151 TAINT_PROPER("open");
154 mode[0] = io->type = 'a';
163 while (isSPACE(*name))
168 gv = gv_fetchpv(name,FALSE);
169 if (!gv || !GvIO(gv)) {
175 if (GvIO(gv) && GvIO(gv)->ifp) {
176 fd = fileno(GvIO(gv)->ifp);
177 if (GvIO(gv)->type == 's')
183 if (!(fp = fdopen(fd = dup(fd),mode))) {
188 while (isSPACE(*name))
190 if (strEQ(name,"-")) {
195 fp = fopen(name,mode);
203 while (isSPACE(*name))
207 if (strEQ(name,"-")) {
212 fp = fopen(name,mode);
214 else if (name[len-1] == '|') {
216 while (len && isSPACE(name[len-1]))
219 for (; isSPACE(*name); name++) ;
222 TAINT_PROPER("piped open");
223 fp = my_popen(name,"r");
229 for (; isSPACE(*name); name++) ;
230 if (strEQ(name,"-")) {
235 fp = fopen(name,"r");
239 if (dowarn && io->type == '<' && strchr(name, '\n'))
240 warn(warn_nl, "open");
246 io->type != '|' && io->type != '-') {
247 if (fstat(fileno(fp),&statbuf) < 0) {
251 if (S_ISSOCK(statbuf.st_mode))
252 io->type = 's'; /* in case a socket was passed in to us */
256 !(statbuf.st_mode & S_IFMT)
261 I32 buflen = sizeof tokenbuf;
262 if (getsockname(fileno(fp), tokenbuf, &buflen) >= 0
263 || errno != ENOTSOCK)
264 io->type = 's'; /* some OS's return 0 on fstat()ed socket */
265 /* but some return 0 for streams too, sigh */
269 if (saveifp) { /* must use old fp? */
270 fd = fileno(saveifp);
272 fflush(saveofp); /* emulate fclose() */
273 if (saveofp != saveifp) { /* was a socket? */
279 if (fd != fileno(fp)) {
283 dup2(fileno(fp), fd);
284 sv = *av_fetch(fdpid,fileno(fp),TRUE);
285 SvUPGRADE(sv, SVt_IV);
288 sv = *av_fetch(fdpid,fd,TRUE);
289 SvUPGRADE(sv, SVt_IV);
297 #if defined(HAS_FCNTL) && defined(FFt_SETFD)
299 fcntl(fd,FFt_SETFD,fd > maxsysfd);
304 || (io->type == '>' && S_ISCHR(statbuf.st_mode)) ) {
305 if (!(io->ofp = fdopen(fileno(fp),"w"))) {
328 #ifndef FLEXFILENAMES
336 argvoutgv = gv_fetchpv("ARGVOUT",TRUE);
337 if (filemode & (S_ISUID|S_ISGID)) {
338 fflush(GvIO(argvoutgv)->ifp); /* chmod must follow last write */
340 (void)fchmod(lastfd,filemode);
342 (void)chmod(oldname,filemode);
346 while (av_len(GvAV(gv)) >= 0) {
347 sv = av_shift(GvAV(gv));
348 sv_setsv(GvSV(gv),sv);
349 SvSETMAGIC(GvSV(gv));
350 oldname = SvPVnx(GvSV(gv));
351 if (do_open(gv,oldname,SvCUR(GvSV(gv)))) {
353 TAINT_PROPER("inplace open");
354 if (strEQ(oldname,"-")) {
356 defoutgv = gv_fetchpv("STDOUT",TRUE);
357 return GvIO(gv)->ifp;
359 #ifndef FLEXFILENAMES
360 filedev = statbuf.st_dev;
361 fileino = statbuf.st_ino;
363 filemode = statbuf.st_mode;
364 fileuid = statbuf.st_uid;
365 filegid = statbuf.st_gid;
366 if (!S_ISREG(filemode)) {
367 warn("Can't do inplace edit: %s is not a regular file",
375 add_suffix(sv,inplace);
377 sv_catpv(sv,inplace);
379 #ifndef FLEXFILENAMES
380 if (stat(SvPV(sv),&statbuf) >= 0
381 && statbuf.st_dev == filedev
382 && statbuf.st_ino == fileino ) {
383 warn("Can't do inplace edit: %s > 14 characters",
392 if (rename(oldname,SvPV(sv)) < 0) {
393 warn("Can't rename %s to %s: %s, skipping file",
394 oldname, SvPV(sv), strerror(errno) );
401 (void)unlink(SvPV(sv));
402 (void)rename(oldname,SvPV(sv));
403 do_open(gv,SvPV(sv),SvCUR(GvSV(gv)));
406 (void)UNLINK(SvPV(sv));
407 if (link(oldname,SvPV(sv)) < 0) {
408 warn("Can't rename %s to %s: %s, skipping file",
409 oldname, SvPV(sv), strerror(errno) );
414 (void)UNLINK(oldname);
419 if (UNLINK(oldname) < 0) {
420 warn("Can't rename %s to %s: %s, skipping file",
421 oldname, SvPV(sv), strerror(errno) );
427 fatal("Can't do inplace edit without backup");
432 sv_catpv(sv,oldname);
433 errno = 0; /* in case sprintf set errno */
434 if (!do_open(argvoutgv,SvPV(sv),SvCUR(sv))) {
435 warn("Can't do inplace edit on %s: %s",
436 oldname, strerror(errno) );
441 defoutgv = argvoutgv;
442 lastfd = fileno(GvIO(argvoutgv)->ifp);
443 (void)fstat(lastfd,&statbuf);
445 (void)fchmod(lastfd,filemode);
447 (void)chmod(oldname,filemode);
449 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
451 (void)fchown(lastfd,fileuid,filegid);
454 (void)chown(oldname,fileuid,filegid);
460 return GvIO(gv)->ifp;
463 fprintf(stderr,"Can't open %s: %s\n",SvPVn(sv), strerror(errno));
467 (void)do_close(argvoutgv,FALSE);
468 defoutgv = gv_fetchpv("STDOUT",TRUE);
475 do_pipe(sv, rgv, wgv)
493 rstio = GvIO(rgv) = newIO();
497 wstio = GvIO(wgv) = newIO();
503 rstio->ifp = fdopen(fd[0], "r");
504 wstio->ofp = fdopen(fd[1], "w");
505 wstio->ifp = wstio->ofp;
508 if (!rstio->ifp || !wstio->ofp) {
509 if (rstio->ifp) fclose(rstio->ifp);
511 if (wstio->ofp) fclose(wstio->ofp);
516 sv_setsv(sv,&sv_yes);
520 sv_setsv(sv,&sv_undef);
526 do_close(gv,explicit)
541 if (!io) { /* never opened */
542 if (dowarn && explicit)
543 warn("Close on unopened file <%s>",GvENAME(gv));
547 if (io->type == '|') {
548 status = my_pclose(io->ifp);
549 retval = (status == 0);
550 statusvalue = (unsigned short)status & 0xffff;
552 else if (io->type == '-')
555 if (io->ofp && io->ofp != io->ifp) { /* a socket */
556 retval = (fclose(io->ofp) != EOF);
557 fclose(io->ifp); /* clear stdio, fd already closed */
560 retval = (fclose(io->ifp) != EOF);
562 io->ofp = io->ifp = Nullfp;
567 io->lines_left = io->page_len;
587 #ifdef STDSTDIO /* (the code works without this) */
588 if (io->ifp->_cnt > 0) /* cheat a little, since */
589 return FALSE; /* this is the most usual case */
594 (void)ungetc(ch, io->ifp);
598 if (io->ifp->_cnt < -1)
601 if (gv == argvgv) { /* not necessarily a real EOF yet? */
602 if (!nextargv(argvgv)) /* get another fp handy */
606 return TRUE; /* normal fp, definitely end of file */
624 #ifdef ULTRIX_STDIO_BOTCH
626 (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */
629 return ftell(io->ifp);
633 warn("tell() on unopened file");
639 do_seek(gv, pos, whence)
653 #ifdef ULTRIX_STDIO_BOTCH
655 (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */
658 return fseek(io->ifp, pos, whence) >= 0;
662 warn("seek() on unopened file");
668 do_ctl(optype,gv,func,argstr)
678 if (!gv || !argstr || !(io = GvIO(gv)) || !io->ifp) {
679 errno = EBADF; /* well, sort of... */
683 if (SvPOK(argstr) || !SvNIOK(argstr)) {
689 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
693 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
695 retval = 256; /* otherwise guess at what's safe */
697 if (SvCUR(argstr) < retval) {
698 Sv_Grow(argstr,retval+1);
699 SvCUR_set(argstr, retval);
703 s[SvCUR(argstr)] = 17; /* a little sanity check here */
706 retval = SvIVn(argstr);
708 s = (char*)(long)retval; /* ouch */
710 s = (char*)retval; /* ouch */
715 if (optype == OP_IOCTL)
716 retval = ioctl(fileno(io->ifp), func, s);
719 fatal("fcntl is not implemented");
722 retval = fcntl(fileno(io->ifp), func, s);
724 fatal("fcntl is not implemented");
732 if (s[SvCUR(argstr)] != 17)
733 fatal("Return value overflowed string");
734 s[SvCUR(argstr)] = 0; /* put our null back */
739 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(FFt_FREESP)
740 /* code courtesy of William Kucharski */
743 I32 chsize(fd, length)
744 I32 fd; /* file descriptor */
745 off_t length; /* length to set file to */
751 if (fstat(fd, &filebuf) < 0)
754 if (filebuf.st_size < length) {
756 /* extend file length */
758 if ((lseek(fd, (length - 1), 0)) < 0)
761 /* write a "0" byte */
763 if ((write(fd, "", 1)) != 1)
767 /* truncate length */
772 fl.l_type = FFt_WRLCK; /* write lock on file space */
775 * This relies on the UNDOCUMENTED FFt_FREESP argument to
776 * fcntl(2), which truncates the file so that it ends at the
777 * position indicated by fl.l_start.
779 * Will minor miracles never cease?
782 if (fcntl(fd, FFt_FREESP, &fl) < 0)
789 #endif /* FFt_FREESP */
792 looks_like_number(sv)
801 send = s + SvCUR(sv);
806 if (*s == '+' || *s == '-')
814 else if (s == SvPV(sv))
820 if (*s == 'e' || *s == 'E') {
822 if (*s == '+' || *s == '-')
842 /* assuming fp is checked earlier */
848 if (SvIOK(sv) && SvIV(sv) != 0) {
849 fprintf(fp, ofmt, (double)SvIV(sv));
852 if ( (SvNOK(sv) && SvNV(sv) != 0.0)
853 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
854 fprintf(fp, ofmt, SvNV(sv));
858 switch (SvTYPE(sv)) {
862 fprintf(fp, "%s", sv_2pv(sv));
867 fprintf(fp, "%d", SvIV(sv));
873 if (SvCUR(sv) && (fwrite(tmps,1,SvCUR(sv),fp) == 0 || ferror(fp)))
885 if (op->op_flags & OPf_SPECIAL) {
887 io = GvIO(cGVOP->op_gv);
889 statgv = cGVOP->op_gv;
890 sv_setpv(statname,"");
892 return (laststatval = fstat(fileno(io->ifp), &statcache));
895 if (cGVOP->op_gv == defgv)
898 warn("Stat on unopened file <%s>",
899 GvENAME(cGVOP->op_gv));
901 sv_setpv(statname,"");
902 return (laststatval = -1);
909 sv_setpv(statname,SvPVn(sv));
911 laststatval = stat(SvPVn(sv),&statcache);
912 if (laststatval < 0 && dowarn && strchr(SvPVn(sv), '\n'))
913 warn(warn_nl, "stat");
924 if (op->op_flags & OPf_SPECIAL) {
926 if (cGVOP->op_gv == defgv) {
927 if (laststype != OP_LSTAT)
928 fatal("The stat preceding -l _ wasn't an lstat");
931 fatal("You can't use -l on a filehandle");
934 laststype = OP_LSTAT;
938 sv_setpv(statname,SvPVn(sv));
940 laststatval = lstat(SvPVn(sv),&statcache);
942 laststatval = stat(SvPVn(sv),&statcache);
944 if (laststatval < 0 && dowarn && strchr(SvPVn(sv), '\n'))
945 warn(warn_nl, "lstat");
950 do_aexec(really,mark,sp)
959 New(401,Argv, sp - mark + 1, char*);
961 while (++mark <= sp) {
963 *a++ = SvPVnx(*mark);
968 if (*Argv[0] != '/') /* will execvp use PATH? */
969 TAINT_ENV(); /* testing IFS here is overkill, probably */
970 if (really && *(tmps = SvPVn(really)))
973 execvp(Argv[0],Argv);
984 Argv = Null(char **);
1000 /* save an extra exec if possible */
1003 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
1019 if (s[-1] == '\'') {
1021 execl(cshname,"csh", flags,ncmd,(char*)0);
1029 /* see if there are shell metacharacters in it */
1032 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1035 for (s = cmd; *s; s++) {
1036 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1037 if (*s == '\n' && !s[1]) {
1042 execl("/bin/sh","sh","-c",cmd,(char*)0);
1046 New(402,Argv, (s - cmd) / 2 + 2, char*);
1047 Cmd = nsavestr(cmd, s-cmd);
1049 for (s = Cmd; *s;) {
1050 while (*s && isSPACE(*s)) s++;
1053 while (*s && !isSPACE(*s)) s++;
1059 execvp(Argv[0],Argv);
1060 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1077 register I32 tot = 0;
1079 SV **oldmark = mark;
1082 while (++mark <= sp)
1083 TAINT_IF((*mark)->sv_tainted);
1088 TAINT_PROPER("chmod");
1091 val = SvIVnx(*mark);
1092 while (++mark <= sp) {
1093 if (chmod(SvPVnx(*mark),val))
1100 TAINT_PROPER("chown");
1101 if (sp - mark > 2) {
1103 val = SvIVnx(*++mark);
1104 val2 = SvIVnx(*++mark);
1105 while (++mark <= sp) {
1106 if (chown(SvPVnx(*mark),val,val2))
1114 TAINT_PROPER("kill");
1115 s = SvPVnx(*++mark);
1118 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1120 if (!(val = whichsig(s)))
1121 fatal("Unrecognized signal name \"%s\"",s);
1124 val = SvIVnx(*mark);
1127 while (++mark <= sp) {
1128 I32 proc = SvIVnx(*mark);
1130 if (killpg(proc,val)) /* BSD */
1132 if (kill(-proc,val)) /* SYSV */
1138 while (++mark <= sp) {
1139 if (kill(SvIVnx(*mark),val))
1146 TAINT_PROPER("unlink");
1148 while (++mark <= sp) {
1150 if (euid || unsafe) {
1154 else { /* don't let root wipe out directories without -U */
1156 if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1158 if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1169 TAINT_PROPER("utime");
1170 if (sp - mark > 2) {
1172 struct utimbuf utbuf;
1180 Zero(&utbuf, sizeof utbuf, char);
1181 utbuf.actime = SvIVnx(*++mark); /* time accessed */
1182 utbuf.modtime = SvIVnx(*++mark); /* time modified */
1184 while (++mark <= sp) {
1185 if (utime(SvPVnx(*mark),&utbuf))
1196 /* Do the permissions allow some operation? Assumes statcache already set. */
1199 cando(bit, effective, statbufp)
1202 register struct stat *statbufp;
1205 /* [Comments and code from Len Reed]
1206 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1207 * to write-protected files. The execute permission bit is set
1208 * by the Miscrosoft C library stat() function for the following:
1213 * All files and directories are readable.
1214 * Directories and special files, e.g. "CON", cannot be
1216 * [Comment by Tom Dinger -- a directory can have the write-protect
1217 * bit set in the file system, but DOS permits changes to
1218 * the directory anyway. In addition, all bets are off
1219 * here for networked software, such as Novell and
1223 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1224 * too so it will actually look into the files for magic numbers
1226 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1229 if ((effective ? euid : uid) == 0) { /* root is special */
1230 if (bit == S_IXUSR) {
1231 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1235 return TRUE; /* root reads and writes anything */
1238 if (statbufp->st_uid == (effective ? euid : uid) ) {
1239 if (statbufp->st_mode & bit)
1240 return TRUE; /* ok as "user" */
1242 else if (ingroup((I32)statbufp->st_gid,effective)) {
1243 if (statbufp->st_mode & bit >> 3)
1244 return TRUE; /* ok as "group" */
1246 else if (statbufp->st_mode & bit >> 6)
1247 return TRUE; /* ok as "other" */
1249 #endif /* ! MSDOS */
1253 ingroup(testgid,effective)
1257 if (testgid == (effective ? egid : gid))
1259 #ifdef HAS_GETGROUPS
1264 GROUPSTYPE gary[NGROUPS];
1267 anum = getgroups(NGROUPS,gary);
1269 if (gary[anum] == testgid)
1276 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1279 do_ipcget(optype, mark, sp)
1287 key = (key_t)SvNVnx(*++mark);
1288 n = (optype == OP_MSGGET) ? 0 : SvIVnx(*++mark);
1289 flags = SvIVnx(*++mark);
1295 return msgget(key, flags);
1299 return semget(key, n, flags);
1303 return shmget(key, n, flags);
1305 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1307 fatal("%s not implemented", op_name[optype]);
1310 return -1; /* should never happen */
1314 do_ipcctl(optype, mark, sp)
1321 I32 id, n, cmd, infosize, getinfo, ret;
1323 id = SvIVnx(*++mark);
1324 n = (optype == OP_SEMCTL) ? SvIVnx(*++mark) : 0;
1325 cmd = SvIVnx(*++mark);
1328 getinfo = (cmd == IPC_STAT);
1334 if (cmd == IPC_STAT || cmd == IPC_SET)
1335 infosize = sizeof(struct msqid_ds);
1340 if (cmd == IPC_STAT || cmd == IPC_SET)
1341 infosize = sizeof(struct shmid_ds);
1346 if (cmd == IPC_STAT || cmd == IPC_SET)
1347 infosize = sizeof(struct semid_ds);
1348 else if (cmd == GETALL || cmd == SETALL)
1350 struct semid_ds semds;
1351 if (semctl(id, 0, IPC_STAT, &semds) == -1)
1353 getinfo = (cmd == GETALL);
1354 infosize = semds.sem_nsems * sizeof(short);
1355 /* "short" is technically wrong but much more portable
1356 than guessing about u_?short(_t)? */
1360 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1362 fatal("%s not implemented", op_name[optype]);
1370 SvGROW(astr, infosize+1);
1376 if (SvCUR(astr) != infosize)
1385 I32 i = SvIVn(astr);
1386 a = (char *)i; /* ouch */
1393 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1398 ret = semctl(id, n, cmd, (struct semid_ds *)a);
1403 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1407 if (getinfo && ret >= 0) {
1408 SvCUR_set(astr, infosize);
1409 *SvEND(astr) = '\0';
1422 I32 id, msize, flags;
1424 id = SvIVnx(*++mark);
1426 flags = SvIVnx(*++mark);
1428 if ((msize = SvCUR(mstr) - sizeof(long)) < 0) {
1433 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1435 fatal("msgsnd not implemented");
1448 I32 id, msize, flags, ret;
1450 id = SvIVnx(*++mark);
1452 msize = SvIVnx(*++mark);
1453 mtype = (long)SvIVnx(*++mark);
1454 flags = SvIVnx(*++mark);
1456 if (SvCUR(mstr) < sizeof(long)+msize+1) {
1457 SvGROW(mstr, sizeof(long)+msize+1);
1461 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1463 SvCUR_set(mstr, sizeof(long)+ret);
1464 *SvEND(mstr) = '\0';
1468 fatal("msgrcv not implemented");
1482 id = SvIVnx(*++mark);
1484 opbuf = SvPVn(opstr);
1485 opsize = SvCUR(opstr);
1486 if (opsize < sizeof(struct sembuf)
1487 || (opsize % sizeof(struct sembuf)) != 0) {
1492 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1494 fatal("semop not implemented");
1499 do_shmio(optype, mark, sp)
1507 I32 id, mpos, msize;
1508 struct shmid_ds shmds;
1510 extern char *shmat();
1513 id = SvIVnx(*++mark);
1515 mpos = SvIVnx(*++mark);
1516 msize = SvIVnx(*++mark);
1518 if (shmctl(id, IPC_STAT, &shmds) == -1)
1520 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1521 errno = EFAULT; /* can't do as caller requested */
1524 shm = (char*)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1525 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1528 if (optype == OP_SHMREAD) {
1529 if (SvCUR(mstr) < msize) {
1530 SvGROW(mstr, msize+1);
1533 Copy(shm + mpos, mbuf, msize, char);
1534 SvCUR_set(mstr, msize);
1535 *SvEND(mstr) = '\0';
1540 if ((n = SvCUR(mstr)) > msize)
1542 Copy(mbuf, shm + mpos, n, char);
1544 memzero(shm + mpos + n, msize - n);
1548 fatal("shm I/O not implemented");
1552 #endif /* SYSV IPC */