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))) {
203 for (; isSPACE(*name); name++) ;
204 if (strEQ(name,"-")) {
209 fp = fopen(name,mode);
213 else if (*name == '<') {
215 for (name++; isSPACE(*name); name++) ;
219 if (strEQ(name,"-")) {
224 fp = fopen(name,mode);
226 else if (name[len-1] == '|') {
228 while (len && isSPACE(name[len-1]))
231 for (; isSPACE(*name); name++) ;
234 TAINT_PROPER("piped open");
235 fp = my_popen(name,"r");
241 for (; isSPACE(*name); name++) ;
242 if (strEQ(name,"-")) {
247 fp = fopen(name,"r");
251 if (dowarn && IoTYPE(io) == '<' && strchr(name, '\n'))
252 warn(warn_nl, "open");
256 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
257 if (Fstat(fileno(fp),&statbuf) < 0) {
261 if (S_ISSOCK(statbuf.st_mode))
262 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
266 !(statbuf.st_mode & S_IFMT)
271 int buflen = sizeof tokenbuf;
272 if (getsockname(fileno(fp), (struct sockaddr *)tokenbuf, &buflen) >= 0
273 || errno != ENOTSOCK)
274 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
275 /* but some return 0 for streams too, sigh */
279 if (saveifp) { /* must use old fp? */
280 fd = fileno(saveifp);
282 Fflush(saveofp); /* emulate fclose() */
283 if (saveofp != saveifp) { /* was a socket? */
289 if (fd != fileno(fp)) {
293 dup2(fileno(fp), fd);
294 sv = *av_fetch(fdpid,fileno(fp),TRUE);
295 (void)SvUPGRADE(sv, SVt_IV);
298 sv = *av_fetch(fdpid,fd,TRUE);
299 (void)SvUPGRADE(sv, SVt_IV);
307 #if defined(HAS_FCNTL) && defined(F_SETFD)
309 fcntl(fd,F_SETFD,fd > maxsysfd);
313 if (IoTYPE(io) == 's'
314 || (IoTYPE(io) == '>' && S_ISCHR(statbuf.st_mode)) ) {
315 if (!(IoOFP(io) = fdopen(fileno(fp),"w"))) {
329 IoTYPE(io) = savetype;
338 #ifndef FLEXFILENAMES
346 argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
347 if (filemode & (S_ISUID|S_ISGID)) {
348 Fflush(IoIFP(GvIOn(argvoutgv))); /* chmod must follow last write */
350 (void)fchmod(lastfd,filemode);
352 (void)chmod(oldname,filemode);
356 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);
524 /* explicit renamed to avoid C++ conflict -- kja */
526 #ifndef CAN_PROTOTYPE
527 do_close(gv,not_implicit)
531 do_close(GV *gv, bool not_implicit)
532 #endif /* CAN_PROTOTYPE */
539 if (!gv || SvTYPE(gv) != SVt_PVGV) {
540 SETERRNO(EBADF,SS$_IVCHAN);
544 if (!io) { /* never opened */
545 if (dowarn && not_implicit)
546 warn("Close on unopened file <%s>",GvENAME(gv));
549 retval = io_close(io);
553 IoLINES_LEFT(io) = IoPAGE_LEN(io);
567 if (IoTYPE(io) == '|') {
568 status = my_pclose(IoIFP(io));
569 retval = (status == 0);
570 statusvalue = FIXSTATUS(status);
572 else if (IoTYPE(io) == '-')
575 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
576 retval = (fclose(IoOFP(io)) != EOF);
577 fclose(IoIFP(io)); /* clear stdio, fd already closed */
580 retval = (fclose(IoIFP(io)) != EOF);
582 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 my_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)
923 New(401,Argv, sp - mark + 1, char*);
925 while (++mark <= sp) {
927 *a++ = SvPVx(*mark, na);
932 if (*Argv[0] != '/') /* will execvp use PATH? */
933 TAINT_ENV(); /* testing IFS here is overkill, probably */
934 if (really && *(tmps = SvPV(really, na)))
937 execvp(Argv[0],Argv);
939 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
950 Argv = Null(char **);
966 while (*cmd && isSPACE(*cmd))
969 /* save an extra exec if possible */
972 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
990 execl(cshname,"csh", flags,ncmd,(char*)0);
998 /* see if there are shell metacharacters in it */
1000 if (*cmd == '.' && isSPACE(cmd[1]))
1003 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1006 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1010 for (s = cmd; *s; s++) {
1011 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1012 if (*s == '\n' && !s[1]) {
1017 execl(SH_PATH, "sh", "-c", cmd, (char*)0);
1022 New(402,Argv, (s - cmd) / 2 + 2, char*);
1023 Cmd = savepvn(cmd, s-cmd);
1025 for (s = Cmd; *s;) {
1026 while (*s && isSPACE(*s)) s++;
1029 while (*s && !isSPACE(*s)) s++;
1035 execvp(Argv[0],Argv);
1036 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1041 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
1055 register I32 tot = 0;
1057 SV **oldmark = mark;
1060 while (++mark <= sp) {
1062 if (SvMAGICAL(*mark) && (mg = mg_find(*mark, 't')) && mg->mg_len & 1)
1069 TAINT_PROPER("chmod");
1073 while (++mark <= sp) {
1074 if (chmod(SvPVx(*mark, na),val))
1081 TAINT_PROPER("chown");
1082 if (sp - mark > 2) {
1083 val = SvIVx(*++mark);
1084 val2 = SvIVx(*++mark);
1086 while (++mark <= sp) {
1087 if (chown(SvPVx(*mark, na),val,val2))
1095 TAINT_PROPER("kill");
1096 s = SvPVx(*++mark, na);
1099 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1101 if (!(val = whichsig(s)))
1102 croak("Unrecognized signal name \"%s\"",s);
1107 /* kill() doesn't do process groups (job trees?) under VMS */
1108 if (val < 0) val = -val;
1109 if (val == SIGKILL) {
1110 # include <starlet.h>
1111 /* Use native sys$delprc() to insure that target process is
1112 * deleted; supervisor-mode images don't pay attention to
1113 * CRTL's emulation of Unix-style signals and kill()
1115 while (++mark <= sp) {
1116 I32 proc = SvIVx(*mark);
1117 register unsigned long int __vmssts;
1118 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1122 case SS$_NOSUCHNODE:
1123 SETERRNO(ESRCH,__vmssts);
1126 SETERRNO(EPERM,__vmssts);
1129 SETERRNO(EVMSERR,__vmssts);
1138 while (++mark <= sp) {
1139 I32 proc = SvIVx(*mark);
1141 if (killpg(proc,val)) /* BSD */
1143 if (kill(-proc,val)) /* SYSV */
1149 while (++mark <= sp) {
1150 if (kill(SvIVx(*mark),val))
1157 TAINT_PROPER("unlink");
1159 while (++mark <= sp) {
1160 s = SvPVx(*mark, na);
1161 if (euid || unsafe) {
1165 else { /* don't let root wipe out directories without -U */
1167 if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1169 if (Stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1181 TAINT_PROPER("utime");
1182 if (sp - mark > 2) {
1183 #if defined(I_UTIME) || defined(VMS)
1184 struct utimbuf utbuf;
1192 Zero(&utbuf, sizeof utbuf, char);
1194 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1195 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1197 utbuf.actime = SvIVx(*++mark); /* time accessed */
1198 utbuf.modtime = SvIVx(*++mark); /* time modified */
1201 while (++mark <= sp) {
1202 if (utime(SvPVx(*mark, na),&utbuf))
1214 /* Do the permissions allow some operation? Assumes statcache already set. */
1215 #ifndef VMS /* VMS' cando is in vms.c */
1217 cando(bit, effective, statbufp)
1220 register struct stat *statbufp;
1223 /* [Comments and code from Len Reed]
1224 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1225 * to write-protected files. The execute permission bit is set
1226 * by the Miscrosoft C library stat() function for the following:
1231 * All files and directories are readable.
1232 * Directories and special files, e.g. "CON", cannot be
1234 * [Comment by Tom Dinger -- a directory can have the write-protect
1235 * bit set in the file system, but DOS permits changes to
1236 * the directory anyway. In addition, all bets are off
1237 * here for networked software, such as Novell and
1241 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1242 * too so it will actually look into the files for magic numbers
1244 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1247 if ((effective ? euid : uid) == 0) { /* root is special */
1248 if (bit == S_IXUSR) {
1249 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1253 return TRUE; /* root reads and writes anything */
1256 if (statbufp->st_uid == (effective ? euid : uid) ) {
1257 if (statbufp->st_mode & bit)
1258 return TRUE; /* ok as "user" */
1260 else if (ingroup((I32)statbufp->st_gid,effective)) {
1261 if (statbufp->st_mode & bit >> 3)
1262 return TRUE; /* ok as "group" */
1264 else if (statbufp->st_mode & bit >> 6)
1265 return TRUE; /* ok as "other" */
1267 #endif /* ! MSDOS */
1272 ingroup(testgid,effective)
1276 if (testgid == (effective ? egid : gid))
1278 #ifdef HAS_GETGROUPS
1283 Groups_t gary[NGROUPS];
1286 anum = getgroups(NGROUPS,gary);
1288 if (gary[anum] == testgid)
1295 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1298 do_ipcget(optype, mark, sp)
1306 key = (key_t)SvNVx(*++mark);
1307 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1308 flags = SvIVx(*++mark);
1314 return msgget(key, flags);
1318 return semget(key, n, flags);
1322 return shmget(key, n, flags);
1324 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1326 croak("%s not implemented", op_desc[optype]);
1329 return -1; /* should never happen */
1333 do_ipcctl(optype, mark, sp)
1340 I32 id, n, cmd, infosize, getinfo;
1343 id = SvIVx(*++mark);
1344 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1345 cmd = SvIVx(*++mark);
1348 getinfo = (cmd == IPC_STAT);
1354 if (cmd == IPC_STAT || cmd == IPC_SET)
1355 infosize = sizeof(struct msqid_ds);
1360 if (cmd == IPC_STAT || cmd == IPC_SET)
1361 infosize = sizeof(struct shmid_ds);
1366 if (cmd == IPC_STAT || cmd == IPC_SET)
1367 infosize = sizeof(struct semid_ds);
1368 else if (cmd == GETALL || cmd == SETALL)
1370 struct semid_ds semds;
1371 if (semctl(id, 0, IPC_STAT, &semds) == -1)
1373 getinfo = (cmd == GETALL);
1374 infosize = semds.sem_nsems * sizeof(short);
1375 /* "short" is technically wrong but much more portable
1376 than guessing about u_?short(_t)? */
1380 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1382 croak("%s not implemented", op_desc[optype]);
1391 SvPV_force(astr, len);
1392 a = SvGROW(astr, infosize+1);
1396 a = SvPV(astr, len);
1397 if (len != infosize)
1398 croak("Bad arg length for %s, is %d, should be %d",
1399 op_desc[optype], len, infosize);
1405 a = (char *)i; /* ouch */
1412 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1417 ret = semctl(id, n, cmd, (struct semid_ds *)a);
1422 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1426 if (getinfo && ret >= 0) {
1427 SvCUR_set(astr, infosize);
1428 *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");
1467 I32 id, msize, flags, ret;
1470 id = SvIVx(*++mark);
1472 msize = SvIVx(*++mark);
1473 mtype = (long)SvIVx(*++mark);
1474 flags = SvIVx(*++mark);
1475 if (SvTHINKFIRST(mstr)) {
1476 if (SvREADONLY(mstr))
1477 croak("Can't msgrcv to readonly var");
1481 SvPV_force(mstr, len);
1482 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1485 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1487 SvCUR_set(mstr, sizeof(long)+ret);
1488 *SvEND(mstr) = '\0';
1492 croak("msgrcv not implemented");
1507 id = SvIVx(*++mark);
1509 opbuf = SvPV(opstr, opsize);
1510 if (opsize < sizeof(struct sembuf)
1511 || (opsize % sizeof(struct sembuf)) != 0) {
1512 SETERRNO(EINVAL,LIB$_INVARG);
1516 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1518 croak("semop not implemented");
1523 do_shmio(optype, mark, sp)
1531 I32 id, mpos, msize;
1533 struct shmid_ds shmds;
1535 id = SvIVx(*++mark);
1537 mpos = SvIVx(*++mark);
1538 msize = SvIVx(*++mark);
1540 if (shmctl(id, IPC_STAT, &shmds) == -1)
1542 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1543 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1546 shm = (Shmat_t)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1547 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1549 if (optype == OP_SHMREAD) {
1550 SvPV_force(mstr, len);
1551 mbuf = SvGROW(mstr, msize+1);
1553 Copy(shm + mpos, mbuf, msize, char);
1554 SvCUR_set(mstr, msize);
1555 *SvEND(mstr) = '\0';
1561 mbuf = SvPV(mstr, len);
1562 if ((n = len) > msize)
1564 Copy(mbuf, shm + mpos, n, char);
1566 memzero(shm + mpos + n, msize - n);
1570 croak("shm I/O not implemented");
1574 #endif /* SYSV IPC */