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 * "Far below them they saw the white waters pour into a foaming bowl, and
12 * then swirl darkly about a deep oval basin in the rocks, until they found
13 * their way out again through a narrow gate, and flowed away, fuming and
14 * chattering, into calmer and more level reaches."
20 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
30 # ifndef HAS_SHMAT_PROTOTYPE
31 extern Shmat_t shmat _((int, char *, int));
46 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
47 # include <sys/socket.h>
51 # include <net/errno.h>
57 do_open(gv,name,len,as_raw,rawmode,rawperm,supplied_fp)
65 register IO *io = GvIOn(gv);
66 FILE *saveifp = Nullfp;
67 FILE *saveofp = Nullfp;
74 forkprocess = 1; /* assume true if no fork */
77 fd = fileno(IoIFP(io));
78 if (IoTYPE(io) == '-')
80 else if (fd <= maxsysfd) {
83 savetype = IoTYPE(io);
86 else if (IoTYPE(io) == '|')
87 result = my_pclose(IoIFP(io));
88 else if (IoIFP(io) != IoOFP(io)) {
90 result = fclose(IoOFP(io));
91 fclose(IoIFP(io)); /* clear stdio, fd already closed */
94 result = fclose(IoIFP(io));
97 result = fclose(IoIFP(io));
98 if (result == EOF && fd > maxsysfd)
99 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
101 IoOFP(io) = IoIFP(io) = Nullfp;
105 result = rawmode & 3;
106 IoTYPE(io) = "<>++"[result];
107 writing = (result > 0);
108 fd = open(name, rawmode, rawperm);
112 fp = fdopen(fd, ((result == 0) ? "r"
113 : (result == 1) ? "w"
121 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
124 myname = savepvn(name, len);
127 while (len && isSPACE(name[len-1]))
130 mode[0] = mode[1] = mode[2] = '\0';
132 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
140 for (name++; isSPACE(*name); name++) ;
143 TAINT_PROPER("piped open");
144 if (dowarn && name[strlen(name)-1] == '|')
145 warn("Can't do bidirectional pipe");
146 fp = my_popen(name,"w");
149 else if (*name == '>') {
150 TAINT_PROPER("open");
153 mode[0] = IoTYPE(io) = 'a';
168 if (!*name && supplied_fp)
172 for (; isSPACE(*name); name++) ;
177 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
181 SETERRNO(EINVAL,SS$_IVCHAN);
186 fd = fileno(IoIFP(thatio));
187 if (IoTYPE(thatio) == 's')
195 if (!(fp = fdopen(fd,mode)))
202 for (; isSPACE(*name); name++) ;
203 if (strEQ(name,"-")) {
208 fp = fopen(name,mode);
212 else if (*name == '<') {
214 for (name++; isSPACE(*name); name++) ;
218 if (strEQ(name,"-")) {
223 fp = fopen(name,mode);
225 else if (name[len-1] == '|') {
227 while (len && isSPACE(name[len-1]))
230 for (; isSPACE(*name); name++) ;
233 TAINT_PROPER("piped open");
234 fp = my_popen(name,"r");
240 for (; isSPACE(*name); name++) ;
241 if (strEQ(name,"-")) {
246 fp = fopen(name,"r");
250 if (dowarn && IoTYPE(io) == '<' && strchr(name, '\n'))
251 warn(warn_nl, "open");
255 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
256 if (Fstat(fileno(fp),&statbuf) < 0) {
260 if (S_ISSOCK(statbuf.st_mode))
261 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
265 !(statbuf.st_mode & S_IFMT)
270 int buflen = sizeof tokenbuf;
271 if (getsockname(fileno(fp), (struct sockaddr *)tokenbuf, &buflen) >= 0
272 || errno != ENOTSOCK)
273 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
274 /* but some return 0 for streams too, sigh */
278 if (saveifp) { /* must use old fp? */
279 fd = fileno(saveifp);
281 Fflush(saveofp); /* emulate fclose() */
282 if (saveofp != saveifp) { /* was a socket? */
288 if (fd != fileno(fp)) {
292 dup2(fileno(fp), fd);
293 sv = *av_fetch(fdpid,fileno(fp),TRUE);
294 (void)SvUPGRADE(sv, SVt_IV);
297 sv = *av_fetch(fdpid,fd,TRUE);
298 (void)SvUPGRADE(sv, SVt_IV);
306 #if defined(HAS_FCNTL) && defined(F_SETFD)
308 fcntl(fd,F_SETFD,fd > maxsysfd);
312 if (IoTYPE(io) == 's'
313 || (IoTYPE(io) == '>' && S_ISCHR(statbuf.st_mode)) ) {
314 if (!(IoOFP(io) = fdopen(fileno(fp),"w"))) {
328 IoTYPE(io) = savetype;
337 #ifndef FLEXFILENAMES
345 argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
346 if (filemode & (S_ISUID|S_ISGID)) {
347 Fflush(IoIFP(GvIOn(argvoutgv))); /* chmod must follow last write */
349 (void)fchmod(lastfd,filemode);
351 (void)chmod(oldname,filemode);
355 while (av_len(GvAV(gv)) >= 0) {
358 sv = av_shift(GvAV(gv));
360 sv_setsv(GvSV(gv),sv);
361 SvSETMAGIC(GvSV(gv));
362 oldname = SvPVx(GvSV(gv), len);
363 if (do_open(gv,oldname,len,FALSE,0,0,Nullfp)) {
365 TAINT_PROPER("inplace open");
366 if (strEQ(oldname,"-")) {
367 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
368 return IoIFP(GvIOp(gv));
370 #ifndef FLEXFILENAMES
371 filedev = statbuf.st_dev;
372 fileino = statbuf.st_ino;
374 filemode = statbuf.st_mode;
375 fileuid = statbuf.st_uid;
376 filegid = statbuf.st_gid;
377 if (!S_ISREG(filemode)) {
378 warn("Can't do inplace edit: %s is not a regular file",
385 add_suffix(sv,inplace);
387 sv_catpv(sv,inplace);
389 #ifndef FLEXFILENAMES
390 if (Stat(SvPVX(sv),&statbuf) >= 0
391 && statbuf.st_dev == filedev
392 && statbuf.st_ino == fileino ) {
393 warn("Can't do inplace edit: %s > 14 characters",
401 if (rename(oldname,SvPVX(sv)) < 0) {
402 warn("Can't rename %s to %s: %s, skipping file",
403 oldname, SvPVX(sv), Strerror(errno) );
409 (void)unlink(SvPVX(sv));
410 (void)rename(oldname,SvPVX(sv));
411 do_open(gv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp);
414 (void)UNLINK(SvPVX(sv));
415 if (link(oldname,SvPVX(sv)) < 0) {
416 warn("Can't rename %s to %s: %s, skipping file",
417 oldname, SvPVX(sv), Strerror(errno) );
421 (void)UNLINK(oldname);
426 if (UNLINK(oldname) < 0) {
427 warn("Can't rename %s to %s: %s, skipping file",
428 oldname, SvPVX(sv), Strerror(errno) );
433 croak("Can't do inplace edit without backup");
438 sv_catpv(sv,oldname);
439 SETERRNO(0,0); /* in case sprintf set errno */
440 if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp)) {
441 warn("Can't do inplace edit on %s: %s",
442 oldname, Strerror(errno) );
446 setdefout(argvoutgv);
447 lastfd = fileno(IoIFP(GvIOp(argvoutgv)));
448 (void)Fstat(lastfd,&statbuf);
450 (void)fchmod(lastfd,filemode);
452 (void)chmod(oldname,filemode);
454 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
456 (void)fchown(lastfd,fileuid,filegid);
459 (void)chown(oldname,fileuid,filegid);
464 return IoIFP(GvIOp(gv));
467 fprintf(stderr,"Can't open %s: %s\n",SvPV(sv, na), Strerror(errno));
470 (void)do_close(argvoutgv,FALSE);
471 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
478 do_pipe(sv, rgv, wgv)
502 IoIFP(rstio) = fdopen(fd[0], "r");
503 IoOFP(wstio) = fdopen(fd[1], "w");
504 IoIFP(wstio) = IoOFP(wstio);
507 if (!IoIFP(rstio) || !IoOFP(wstio)) {
508 if (IoIFP(rstio)) fclose(IoIFP(rstio));
510 if (IoOFP(wstio)) fclose(IoOFP(wstio));
515 sv_setsv(sv,&sv_yes);
519 sv_setsv(sv,&sv_undef);
525 #ifndef CAN_PROTOTYPE
526 do_close(gv,explicit)
530 do_close(GV *gv, bool explicit)
531 #endif /* CAN_PROTOTYPE */
538 if (!gv || SvTYPE(gv) != SVt_PVGV) {
539 SETERRNO(EBADF,SS$_IVCHAN);
543 if (!io) { /* never opened */
544 if (dowarn && explicit)
545 warn("Close on unopened file <%s>",GvENAME(gv));
548 retval = io_close(io);
552 IoLINES_LEFT(io) = IoPAGE_LEN(io);
566 if (IoTYPE(io) == '|') {
567 status = my_pclose(IoIFP(io));
568 retval = (status == 0);
569 statusvalue = FIXSTATUS(status);
571 else if (IoTYPE(io) == '-')
574 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
575 retval = (fclose(IoOFP(io)) != EOF);
576 fclose(IoIFP(io)); /* clear stdio, fd already closed */
579 retval = (fclose(IoIFP(io)) != EOF);
581 IoOFP(io) = IoIFP(io) = Nullfp;
602 #ifdef USE_STDIO_PTR /* (the code works without this) */
603 if (FILE_cnt(IoIFP(io)) > 0) /* cheat a little, since */
604 return FALSE; /* this is the most usual case */
607 ch = getc(IoIFP(io));
609 (void)ungetc(ch, IoIFP(io));
612 #if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE)
613 if (FILE_cnt(IoIFP(io)) < -1)
614 FILE_cnt(IoIFP(io)) = -1;
616 if (op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
617 if (!nextargv(argvgv)) /* get another fp handy */
621 return TRUE; /* normal fp, definitely end of file */
636 if (!io || !IoIFP(io))
639 #ifdef ULTRIX_STDIO_BOTCH
641 (void)fseek (IoIFP(io), 0L, 2); /* ultrix 1.2 workaround */
644 return ftell(IoIFP(io));
648 warn("tell() on unopened file");
649 SETERRNO(EBADF,RMS$_IFI);
654 do_seek(gv, pos, whence)
665 if (!io || !IoIFP(io))
668 #ifdef ULTRIX_STDIO_BOTCH
670 (void)fseek (IoIFP(io), 0L, 2); /* ultrix 1.2 workaround */
673 return fseek(IoIFP(io), pos, whence) >= 0;
677 warn("seek() on unopened file");
678 SETERRNO(EBADF,RMS$_IFI);
682 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
683 /* code courtesy of William Kucharski */
686 I32 chsize(fd, length)
687 I32 fd; /* file descriptor */
688 Off_t length; /* length to set file to */
694 if (Fstat(fd, &filebuf) < 0)
697 if (filebuf.st_size < length) {
699 /* extend file length */
701 if ((lseek(fd, (length - 1), 0)) < 0)
704 /* write a "0" byte */
706 if ((write(fd, "", 1)) != 1)
710 /* truncate length */
715 fl.l_type = F_WRLCK; /* write lock on file space */
718 * This relies on the UNDOCUMENTED F_FREESP argument to
719 * fcntl(2), which truncates the file so that it ends at the
720 * position indicated by fl.l_start.
722 * Will minor miracles never cease?
725 if (fcntl(fd, F_FREESP, &fl) < 0)
732 #endif /* F_FREESP */
735 looks_like_number(sv)
750 send = s + SvCUR(sv);
756 if (*s == '+' || *s == '-')
764 else if (s == SvPVX(sv))
770 if (*s == 'e' || *s == 'E') {
772 if (*s == '+' || *s == '-')
792 /* assuming fp is checked earlier */
798 if (SvIOK(sv) && SvIVX(sv) != 0) {
799 fprintf(fp, ofmt, (double)SvIVX(sv));
802 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
803 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
804 fprintf(fp, ofmt, SvNVX(sv));
808 switch (SvTYPE(sv)) {
817 fprintf(fp, "%ld", (long)SvIVX(sv));
822 tmps = SvPV(sv, len);
825 if (len && (fwrite1(tmps,1,len,fp) == 0 || ferror(fp)))
838 if (op->op_flags & OPf_REF) {
840 tmpgv = cGVOP->op_gv;
843 if (io && IoIFP(io)) {
845 sv_setpv(statname,"");
847 return (laststatval = Fstat(fileno(IoIFP(io)), &statcache));
853 warn("Stat on unopened file <%s>",
856 sv_setpv(statname,"");
857 return (laststatval = -1);
863 if (SvTYPE(sv) == SVt_PVGV) {
867 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
868 tmpgv = (GV*)SvRV(sv);
873 sv_setpv(statname,SvPV(sv, na));
875 laststatval = Stat(SvPV(sv, na),&statcache);
876 if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n'))
877 warn(warn_nl, "stat");
888 if (op->op_flags & OPf_REF) {
890 if (cGVOP->op_gv == defgv) {
891 if (laststype != OP_LSTAT)
892 croak("The stat preceding -l _ wasn't an lstat");
895 croak("You can't use -l on a filehandle");
898 laststype = OP_LSTAT;
902 sv_setpv(statname,SvPV(sv, na));
904 laststatval = lstat(SvPV(sv, na),&statcache);
906 laststatval = Stat(SvPV(sv, na),&statcache);
908 if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n'))
909 warn(warn_nl, "lstat");
914 do_aexec(really,mark,sp)
924 New(401,Argv, sp - mark + 1, char*);
926 while (++mark <= sp) {
928 *a++ = SvPVx(*mark, na);
933 if (*Argv[0] != '/') /* will execvp use PATH? */
934 TAINT_ENV(); /* testing IFS here is overkill, probably */
935 if (really && *(tmps = SvPV(really, na)))
938 execvp(Argv[0],Argv);
940 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
951 Argv = Null(char **);
967 while (*cmd && isSPACE(*cmd))
970 /* save an extra exec if possible */
973 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
991 execl(cshname,"csh", flags,ncmd,(char*)0);
999 /* see if there are shell metacharacters in it */
1001 if (*cmd == '.' && isSPACE(cmd[1]))
1004 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1007 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1011 for (s = cmd; *s; s++) {
1012 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1013 if (*s == '\n' && !s[1]) {
1018 execl("/bin/sh","sh","-c",cmd,(char*)0);
1023 New(402,Argv, (s - cmd) / 2 + 2, char*);
1024 Cmd = savepvn(cmd, s-cmd);
1026 for (s = Cmd; *s;) {
1027 while (*s && isSPACE(*s)) s++;
1030 while (*s && !isSPACE(*s)) s++;
1036 execvp(Argv[0],Argv);
1037 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1042 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
1057 register I32 tot = 0;
1059 SV **oldmark = mark;
1062 while (++mark <= sp) {
1064 if (SvMAGICAL(*mark) && (mg = mg_find(*mark, 't')) && mg->mg_len & 1)
1071 TAINT_PROPER("chmod");
1075 while (++mark <= sp) {
1076 if (chmod(SvPVx(*mark, na),val))
1083 TAINT_PROPER("chown");
1084 if (sp - mark > 2) {
1085 val = SvIVx(*++mark);
1086 val2 = SvIVx(*++mark);
1088 while (++mark <= sp) {
1089 if (chown(SvPVx(*mark, na),val,val2))
1097 TAINT_PROPER("kill");
1098 s = SvPVx(*++mark, na);
1101 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1103 if (!(val = whichsig(s)))
1104 croak("Unrecognized signal name \"%s\"",s);
1109 /* kill() doesn't do process groups (job trees?) under VMS */
1110 if (val < 0) val = -val;
1111 if (val == SIGKILL) {
1112 # include <starlet.h>
1113 /* Use native sys$delprc() to insure that target process is
1114 * deleted; supervisor-mode images don't pay attention to
1115 * CRTL's emulation of Unix-style signals and kill()
1117 while (++mark <= sp) {
1118 I32 proc = SvIVx(*mark);
1119 register unsigned long int __vmssts;
1120 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1124 case SS$_NOSUCHNODE:
1125 SETERRNO(ESRCH,__vmssts);
1128 SETERRNO(EPERM,__vmssts);
1131 SETERRNO(EVMSERR,__vmssts);
1140 while (++mark <= sp) {
1141 I32 proc = SvIVx(*mark);
1143 if (killpg(proc,val)) /* BSD */
1145 if (kill(-proc,val)) /* SYSV */
1151 while (++mark <= sp) {
1152 if (kill(SvIVx(*mark),val))
1159 TAINT_PROPER("unlink");
1161 while (++mark <= sp) {
1162 s = SvPVx(*mark, na);
1163 if (euid || unsafe) {
1167 else { /* don't let root wipe out directories without -U */
1169 if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1171 if (Stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1183 TAINT_PROPER("utime");
1184 if (sp - mark > 2) {
1185 #if defined(I_UTIME) || defined(VMS)
1186 struct utimbuf utbuf;
1194 Zero(&utbuf, sizeof utbuf, char);
1195 utbuf.actime = SvIVx(*++mark); /* time accessed */
1196 utbuf.modtime = SvIVx(*++mark); /* time modified */
1198 while (++mark <= sp) {
1199 if (utime(SvPVx(*mark, na),&utbuf))
1211 /* Do the permissions allow some operation? Assumes statcache already set. */
1212 #ifndef VMS /* VMS' cando is in vms.c */
1214 cando(bit, effective, statbufp)
1217 register struct stat *statbufp;
1220 /* [Comments and code from Len Reed]
1221 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1222 * to write-protected files. The execute permission bit is set
1223 * by the Miscrosoft C library stat() function for the following:
1228 * All files and directories are readable.
1229 * Directories and special files, e.g. "CON", cannot be
1231 * [Comment by Tom Dinger -- a directory can have the write-protect
1232 * bit set in the file system, but DOS permits changes to
1233 * the directory anyway. In addition, all bets are off
1234 * here for networked software, such as Novell and
1238 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1239 * too so it will actually look into the files for magic numbers
1241 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1244 if ((effective ? euid : uid) == 0) { /* root is special */
1245 if (bit == S_IXUSR) {
1246 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1250 return TRUE; /* root reads and writes anything */
1253 if (statbufp->st_uid == (effective ? euid : uid) ) {
1254 if (statbufp->st_mode & bit)
1255 return TRUE; /* ok as "user" */
1257 else if (ingroup((I32)statbufp->st_gid,effective)) {
1258 if (statbufp->st_mode & bit >> 3)
1259 return TRUE; /* ok as "group" */
1261 else if (statbufp->st_mode & bit >> 6)
1262 return TRUE; /* ok as "other" */
1264 #endif /* ! MSDOS */
1269 ingroup(testgid,effective)
1273 if (testgid == (effective ? egid : gid))
1275 #ifdef HAS_GETGROUPS
1280 Groups_t gary[NGROUPS];
1283 anum = getgroups(NGROUPS,gary);
1285 if (gary[anum] == testgid)
1292 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1295 do_ipcget(optype, mark, sp)
1304 key = (key_t)SvNVx(*++mark);
1305 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1306 flags = SvIVx(*++mark);
1312 return msgget(key, flags);
1316 return semget(key, n, flags);
1320 return shmget(key, n, flags);
1322 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1324 croak("%s not implemented", op_desc[optype]);
1327 return -1; /* should never happen */
1331 do_ipcctl(optype, mark, sp)
1339 I32 id, n, cmd, infosize, getinfo;
1342 id = SvIVx(*++mark);
1343 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1344 cmd = SvIVx(*++mark);
1347 getinfo = (cmd == IPC_STAT);
1353 if (cmd == IPC_STAT || cmd == IPC_SET)
1354 infosize = sizeof(struct msqid_ds);
1359 if (cmd == IPC_STAT || cmd == IPC_SET)
1360 infosize = sizeof(struct shmid_ds);
1365 if (cmd == IPC_STAT || cmd == IPC_SET)
1366 infosize = sizeof(struct semid_ds);
1367 else if (cmd == GETALL || cmd == SETALL)
1369 struct semid_ds semds;
1370 if (semctl(id, 0, IPC_STAT, &semds) == -1)
1372 getinfo = (cmd == GETALL);
1373 infosize = semds.sem_nsems * sizeof(short);
1374 /* "short" is technically wrong but much more portable
1375 than guessing about u_?short(_t)? */
1379 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1381 croak("%s not implemented", op_desc[optype]);
1390 SvPV_force(astr, len);
1391 a = SvGROW(astr, infosize+1);
1395 a = SvPV(astr, len);
1396 if (len != infosize)
1397 croak("Bad arg length for %s, is %d, should be %d",
1398 op_desc[optype], len, infosize);
1404 a = (char *)i; /* ouch */
1411 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1416 ret = semctl(id, n, cmd, (struct semid_ds *)a);
1421 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1425 if (getinfo && ret >= 0) {
1426 SvCUR_set(astr, infosize);
1427 *SvEND(astr) = '\0';
1442 I32 id, msize, flags;
1445 id = SvIVx(*++mark);
1447 flags = SvIVx(*++mark);
1448 mbuf = SvPV(mstr, len);
1449 if ((msize = len - sizeof(long)) < 0)
1450 croak("Arg too short for msgsnd");
1452 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1454 croak("msgsnd not implemented");
1468 I32 id, msize, flags, ret;
1471 id = SvIVx(*++mark);
1473 msize = SvIVx(*++mark);
1474 mtype = (long)SvIVx(*++mark);
1475 flags = SvIVx(*++mark);
1476 if (SvTHINKFIRST(mstr)) {
1477 if (SvREADONLY(mstr))
1478 croak("Can't msgrcv to readonly var");
1482 SvPV_force(mstr, len);
1483 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1486 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1488 SvCUR_set(mstr, sizeof(long)+ret);
1489 *SvEND(mstr) = '\0';
1493 croak("msgrcv not implemented");
1509 id = SvIVx(*++mark);
1511 opbuf = SvPV(opstr, opsize);
1512 if (opsize < sizeof(struct sembuf)
1513 || (opsize % sizeof(struct sembuf)) != 0) {
1514 SETERRNO(EINVAL,LIB$_INVARG);
1518 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1520 croak("semop not implemented");
1525 do_shmio(optype, mark, sp)
1534 I32 id, mpos, msize;
1536 struct shmid_ds shmds;
1538 id = SvIVx(*++mark);
1540 mpos = SvIVx(*++mark);
1541 msize = SvIVx(*++mark);
1543 if (shmctl(id, IPC_STAT, &shmds) == -1)
1545 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1546 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1549 shm = (Shmat_t)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1550 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1552 if (optype == OP_SHMREAD) {
1553 SvPV_force(mstr, len);
1554 mbuf = SvGROW(mstr, msize+1);
1556 Copy(shm + mpos, mbuf, msize, char);
1557 SvCUR_set(mstr, msize);
1558 *SvEND(mstr) = '\0';
1564 mbuf = SvPV(mstr, len);
1565 if ((n = len) > msize)
1567 Copy(mbuf, shm + mpos, n, char);
1569 memzero(shm + mpos + n, msize - n);
1573 croak("shm I/O not implemented");
1577 #endif /* SYSV IPC */