1 /* $Header: doio.c,v 3.0.1.14 91/01/11 17:51:04 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.14 91/01/11 17:51:04 lwall
10 * patch42: ANSIfied the stat mode checking
11 * patch42: the -i switch is now much more robust and informative
12 * patch42: close on a pipe didn't return failure correctly
13 * patch42: stat on temp values could wipe them out prematurely, i.e. grep(-d,<*>)
14 * patch42: -l didn't work right with _
16 * Revision 3.0.1.13 90/11/10 01:17:37 lwall
17 * patch38: -e _ was wrong if last stat failed
18 * patch38: more msdos/os2 upgrades
20 * Revision 3.0.1.12 90/10/20 02:04:18 lwall
21 * patch37: split out separate Sys V IPC features
23 * Revision 3.0.1.11 90/10/15 16:16:11 lwall
24 * patch29: added SysV IPC
25 * patch29: file - didn't auto-close cleanly
26 * patch29: close; core dumped
27 * patch29: more MSDOS and OS/2 updates, from Kai Uwe Rommel
28 * patch29: various portability fixes
29 * patch29: *foo now prints as *package'foo
31 * Revision 3.0.1.10 90/08/13 22:14:29 lwall
32 * patch28: close-on-exec problems on dup'ed file descriptors
33 * patch28: F_FREESP wasn't implemented the way I thought
35 * Revision 3.0.1.9 90/08/09 02:56:19 lwall
36 * patch19: various MSDOS and OS/2 patches folded in
37 * patch19: prints now check error status better
38 * patch19: printing a list with null elements only printed front of list
39 * patch19: on machines with vfork child would allocate memory in parent
40 * patch19: getsockname and getpeername gave bogus warning on error
41 * patch19: MACH doesn't have seekdir or telldir
43 * Revision 3.0.1.8 90/03/27 15:44:02 lwall
44 * patch16: MSDOS support
45 * patch16: support for machines that can't cast negative floats to unsigned ints
46 * patch16: system() can lose arguments passed to shell scripts on SysV machines
48 * Revision 3.0.1.7 90/03/14 12:26:24 lwall
49 * patch15: commands involving execs could cause malloc arena corruption
51 * Revision 3.0.1.6 90/03/12 16:30:07 lwall
52 * patch13: system 'FOO=bar command' didn't invoke sh as it should
54 * Revision 3.0.1.5 90/02/28 17:01:36 lwall
55 * patch9: open(FOO,"$filename\0") will now protect trailing spaces in filename
56 * patch9: removed obsolete checks to avoid opening block devices
57 * patch9: removed references to acusec and modusec that some utime.h's have
58 * patch9: added pipe function
60 * Revision 3.0.1.4 89/12/21 19:55:10 lwall
61 * patch7: select now works on big-endian machines
62 * patch7: errno may now be a macro with an lvalue
63 * patch7: ANSI strerror() is now supported
64 * patch7: Configure now detects DG/UX thingies like [sg]etpgrp2 and utime.h
66 * Revision 3.0.1.3 89/11/17 15:13:06 lwall
67 * patch5: some systems have symlink() but not lstat()
68 * patch5: some systems have dirent.h but not readdir()
70 * Revision 3.0.1.2 89/11/11 04:25:51 lwall
71 * patch2: orthogonalized the file modes some so we can have <& +<& etc.
72 * patch2: do_open() now detects sockets passed to process from parent
73 * patch2: fd's above 2 are now closed on exec
74 * patch2: csh code can now use csh from other than /bin
75 * patch2: getsockopt, get{sock,peer}name didn't define result properly
76 * patch2: warn("shutdown") was replicated
77 * patch2: gethostbyname was misdeclared
78 * patch2: telldir() is sometimes a macro
80 * Revision 3.0.1.1 89/10/26 23:10:05 lwall
81 * patch1: Configure now checks for BSD shadow passwords
83 * Revision 3.0 89/10/18 15:10:54 lwall
92 #include <sys/socket.h>
96 #if defined(SELECT) && (defined(M_UNIX) || defined(M_XENIX))
97 #include <sys/select.h>
126 int laststatval = -1;
129 do_open(stab,name,len)
135 register STIO *stio = stab_io(stab);
136 char *myname = savestr(name);
140 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
143 forkprocess = 1; /* assume true if no fork */
144 while (len && isspace(name[len-1]))
147 stio = stab_io(stab) = stio_new();
148 else if (stio->ifp) {
149 fd = fileno(stio->ifp);
150 if (stio->type == '|')
151 result = mypclose(stio->ifp);
152 else if (stio->type == '-')
154 else if (stio->ifp != stio->ofp) {
156 result = fclose(stio->ofp);
157 fclose(stio->ifp); /* clear stdio, fd already closed */
160 result = fclose(stio->ifp);
163 result = fclose(stio->ifp);
164 if (result == EOF && fd > 2)
165 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
167 stio->ofp = stio->ifp = Nullfp;
169 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
180 for (name++; isspace(*name); name++) ;
183 taintproper("Insecure dependency in piped open");
185 fp = mypopen(name,"w");
188 else if (*name == '>') {
190 taintproper("Insecure dependency in open");
194 mode[0] = stio->type = 'a';
203 while (isspace(*name))
208 stab = stabent(name,FALSE);
209 if (!stab || !stab_io(stab))
211 if (stab_io(stab) && stab_io(stab)->ifp) {
212 fd = fileno(stab_io(stab)->ifp);
213 if (stab_io(stab)->type == 's')
219 fp = fdopen(dup(fd),mode);
222 while (isspace(*name))
224 if (strEQ(name,"-")) {
229 fp = fopen(name,mode);
237 while (isspace(*name))
241 if (strEQ(name,"-")) {
246 fp = fopen(name,mode);
248 else if (name[len-1] == '|') {
251 taintproper("Insecure dependency in piped open");
254 while (len && isspace(name[len-1]))
256 for (; isspace(*name); name++) ;
257 fp = mypopen(name,"r");
262 for (; isspace(*name); name++) ;
263 if (strEQ(name,"-")) {
268 fp = fopen(name,"r");
275 stio->type != '|' && stio->type != '-') {
276 if (fstat(fileno(fp),&statbuf) < 0) {
280 if (S_ISSOCK(statbuf.st_mode))
281 stio->type = 's'; /* in case a socket was passed in to us */
283 else if (!(statbuf.st_mode & S_IFMT))
284 stio->type = 's'; /* some OS's return 0 on fstat()ed socket */
287 #if defined(FCNTL) && defined(F_SETFD)
289 fcntl(fd,F_SETFD,fd >= 3);
293 if (stio->type != 's')
296 stio->ofp = fdopen(fileno(fp),"w");
313 while (alen(stab_xarray(stab)) >= 0) {
314 str = ashift(stab_xarray(stab));
315 str_sset(stab_val(stab),str);
316 STABSET(stab_val(stab));
317 oldname = str_get(stab_val(stab));
318 if (do_open(stab,oldname,stab_val(stab)->str_cur)) {
321 taintproper("Insecure dependency in inplace open");
323 if (strEQ(oldname,"-")) {
325 defoutstab = stabent("STDOUT",TRUE);
326 return stab_io(stab)->ifp;
328 filedev = statbuf.st_dev;
329 fileino = statbuf.st_ino;
330 filemode = statbuf.st_mode;
331 fileuid = statbuf.st_uid;
332 filegid = statbuf.st_gid;
333 if (!S_ISREG(filemode)) {
334 warn("Can't do inplace edit: %s is not a regular file",
336 do_close(stab,FALSE);
342 add_suffix(str,inplace);
344 str_cat(str,inplace);
346 #ifndef FLEXFILENAMES
347 if (stat(str->str_ptr,&statbuf) >= 0
348 && statbuf.st_dev == filedev
349 && statbuf.st_ino == fileino ) {
350 warn("Can't do inplace edit: %s > 14 characters",
352 do_close(stab,FALSE);
359 if (rename(oldname,str->str_ptr) < 0) {
360 warn("Can't rename %s to %s: %s, skipping file",
361 oldname, str->str_ptr, strerror(errno) );
362 do_close(stab,FALSE);
367 do_close(stab,FALSE);
368 (void)unlink(str->str_ptr);
369 (void)rename(oldname,str->str_ptr);
370 do_open(stab,str->str_ptr,stab_val(stab)->str_cur);
373 (void)UNLINK(str->str_ptr);
374 if (link(oldname,str->str_ptr) < 0) {
375 warn("Can't rename %s to %s: %s, skipping file",
376 oldname, str->str_ptr, strerror(errno) );
377 do_close(stab,FALSE);
381 (void)UNLINK(oldname);
386 (void)UNLINK(oldname);
388 fatal("Can't do inplace edit without backup");
393 str_cat(str,oldname);
394 errno = 0; /* in case sprintf set errno */
395 if (!do_open(argvoutstab,str->str_ptr,str->str_cur))
396 warn("Can't do inplace edit on %s: %s",
397 oldname, strerror(errno) );
398 defoutstab = argvoutstab;
400 (void)fchmod(fileno(stab_io(argvoutstab)->ifp),filemode);
402 (void)chmod(oldname,filemode);
405 (void)fchown(fileno(stab_io(argvoutstab)->ifp),fileuid,filegid);
408 (void)chown(oldname,fileuid,filegid);
413 return stab_io(stab)->ifp;
416 fprintf(stderr,"Can't open %s: %s\n",str_get(str), strerror(errno));
420 (void)do_close(argvoutstab,FALSE);
421 defoutstab = stabent("STDOUT",TRUE);
428 do_pipe(str, rstab, wstab)
433 register STIO *rstio;
434 register STIO *wstio;
442 rstio = stab_io(rstab);
443 wstio = stab_io(wstab);
446 rstio = stab_io(rstab) = stio_new();
448 do_close(rstab,FALSE);
450 wstio = stab_io(wstab) = stio_new();
452 do_close(wstab,FALSE);
456 rstio->ifp = fdopen(fd[0], "r");
457 wstio->ofp = fdopen(fd[1], "w");
458 wstio->ifp = wstio->ofp;
462 str_sset(str,&str_yes);
466 str_sset(str,&str_undef);
472 do_close(stab,explicit)
484 stio = stab_io(stab);
485 if (!stio) { /* never opened */
486 if (dowarn && explicit)
487 warn("Close on unopened file <%s>",stab_name(stab));
491 if (stio->type == '|') {
492 status = mypclose(stio->ifp);
493 retval = (status == 0);
494 statusvalue = (unsigned short)status & 0xffff;
496 else if (stio->type == '-')
499 if (stio->ofp && stio->ofp != stio->ifp) { /* a socket */
500 retval = (fclose(stio->ofp) != EOF);
501 fclose(stio->ifp); /* clear stdio, fd already closed */
504 retval = (fclose(stio->ifp) != EOF);
506 stio->ofp = stio->ifp = Nullfp;
521 if (!stab) { /* eof() */
523 stio = stab_io(argvstab);
528 stio = stab_io(stab);
535 #ifdef STDSTDIO /* (the code works without this) */
536 if (stio->ifp->_cnt > 0) /* cheat a little, since */
537 return FALSE; /* this is the most usual case */
540 ch = getc(stio->ifp);
542 (void)ungetc(ch, stio->ifp);
545 if (!stab) { /* not necessarily a real EOF yet? */
546 if (!nextargv(argvstab)) /* get another fp handy */
550 return TRUE; /* normal fp, definitely end of file */
564 stio = stab_io(stab);
565 if (!stio || !stio->ifp)
569 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
571 return ftell(stio->ifp);
575 warn("tell() on unopened file");
580 do_seek(stab, pos, whence)
590 stio = stab_io(stab);
591 if (!stio || !stio->ifp)
595 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
597 return fseek(stio->ifp, pos, whence) >= 0;
601 warn("seek() on unopened file");
606 do_ctl(optype,stab,func,argstr)
616 if (!stab || !argstr)
618 stio = stab_io(stab);
622 if (argstr->str_pok || !argstr->str_nok) {
623 if (!argstr->str_pok)
628 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
632 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
634 retval = 256; /* otherwise guess at what's safe */
636 if (argstr->str_cur < retval) {
637 Str_Grow(argstr,retval+1);
638 argstr->str_cur = retval;
642 s[argstr->str_cur] = 17; /* a little sanity check here */
645 retval = (int)str_gnum(argstr);
647 s = (char*)(long)retval; /* ouch */
649 s = (char*)retval; /* ouch */
654 if (optype == O_IOCTL)
655 retval = ioctl(fileno(stio->ifp), func, s);
658 fatal("fcntl is not implemented");
661 retval = fcntl(fileno(stio->ifp), func, s);
663 fatal("fcntl is not implemented");
670 if (argstr->str_pok) {
671 if (s[argstr->str_cur] != 17)
672 fatal("Return value overflowed string");
673 s[argstr->str_cur] = 0; /* put our null back */
679 do_stat(str,arg,gimme,arglast)
685 register ARRAY *ary = stack;
686 register int sp = arglast[0] + 1;
689 if ((arg[1].arg_type & A_MASK) == A_WORD) {
690 tmpstab = arg[1].arg_ptr.arg_stab;
691 if (tmpstab != defstab) {
693 str_set(statname,"");
694 if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
695 fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
700 else if (laststatval < 0)
704 str_set(statname,str_get(ary->ary_array[sp]));
707 if (arg->arg_type == O_LSTAT)
708 laststatval = lstat(str_get(statname),&statcache);
711 laststatval = stat(str_get(statname),&statcache);
716 if (gimme != G_ARRAY) {
718 str_sset(str,&str_yes);
720 str_sset(str,&str_undef);
722 ary->ary_array[sp] = str;
728 (void)astore(ary,++sp,
729 str_2static(str_nmake((double)statcache.st_dev)));
730 (void)astore(ary,++sp,
731 str_2static(str_nmake((double)statcache.st_ino)));
732 (void)astore(ary,++sp,
733 str_2static(str_nmake((double)statcache.st_mode)));
734 (void)astore(ary,++sp,
735 str_2static(str_nmake((double)statcache.st_nlink)));
736 (void)astore(ary,++sp,
737 str_2static(str_nmake((double)statcache.st_uid)));
738 (void)astore(ary,++sp,
739 str_2static(str_nmake((double)statcache.st_gid)));
740 (void)astore(ary,++sp,
741 str_2static(str_nmake((double)statcache.st_rdev)));
742 (void)astore(ary,++sp,
743 str_2static(str_nmake((double)statcache.st_size)));
744 (void)astore(ary,++sp,
745 str_2static(str_nmake((double)statcache.st_atime)));
746 (void)astore(ary,++sp,
747 str_2static(str_nmake((double)statcache.st_mtime)));
748 (void)astore(ary,++sp,
749 str_2static(str_nmake((double)statcache.st_ctime)));
751 (void)astore(ary,++sp,
752 str_2static(str_nmake((double)statcache.st_blksize)));
753 (void)astore(ary,++sp,
754 str_2static(str_nmake((double)statcache.st_blocks)));
756 (void)astore(ary,++sp,
757 str_2static(str_make("",0)));
758 (void)astore(ary,++sp,
759 str_2static(str_make("",0)));
762 (void)astore(ary,++sp,str_nmake(0.0));
768 #if !defined(TRUNCATE) && !defined(CHSIZE) && defined(F_FREESP)
769 /* code courtesy of William Kucharski */
772 int chsize(fd, length)
773 int fd; /* file descriptor */
774 off_t length; /* length to set file to */
780 if (fstat(fd, &filebuf) < 0)
783 if (filebuf.st_size < length) {
785 /* extend file length */
787 if ((lseek(fd, (length - 1), 0)) < 0)
790 /* write a "0" byte */
792 if ((write(fd, "", 1)) != 1)
796 /* truncate length */
801 fl.l_type = F_WRLCK; /* write lock on file space */
804 * This relies on the UNDOCUMENTED F_FREESP argument to
805 * fcntl(2), which truncates the file so that it ends at the
806 * position indicated by fl.l_start.
808 * Will minor miracles never cease?
811 if (fcntl(fd, F_FREESP, &fl) < 0)
818 #endif /* F_FREESP */
821 do_truncate(str,arg,gimme,arglast)
827 register ARRAY *ary = stack;
828 register int sp = arglast[0] + 1;
829 off_t len = (off_t)str_gnum(ary->ary_array[sp+1]);
833 #if defined(TRUNCATE) || defined(CHSIZE)
835 if ((arg[1].arg_type & A_MASK) == A_WORD) {
836 tmpstab = arg[1].arg_ptr.arg_stab;
837 if (!stab_io(tmpstab) ||
838 ftruncate(fileno(stab_io(tmpstab)->ifp), len) < 0)
841 else if (truncate(str_get(ary->ary_array[sp]), len) < 0)
844 if ((arg[1].arg_type & A_MASK) == A_WORD) {
845 tmpstab = arg[1].arg_ptr.arg_stab;
846 if (!stab_io(tmpstab) ||
847 chsize(fileno(stab_io(tmpstab)->ifp), len) < 0)
853 if ((tmpfd = open(str_get(ary->ary_array[sp]), 0)) < 0)
856 if (chsize(tmpfd, len) < 0)
864 str_sset(str,&str_yes);
866 str_sset(str,&str_undef);
868 ary->ary_array[sp] = str;
871 fatal("truncate not implemented");
876 looks_like_number(str)
885 send = s + str->str_cur;
890 if (*s == '+' || *s == '-')
898 else if (s == str->str_ptr)
904 if (*s == 'e' || *s == 'E') {
906 if (*s == '+' || *s == '-')
927 warn("print to unopened file");
933 ((str->str_nok && str->str_u.str_nval != 0.0)
934 || (looks_like_number(str) && str_gnum(str) != 0.0) ) ) {
935 fprintf(fp, ofmt, str->str_u.str_nval);
940 if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'B' && tmps[3] == '\0'
941 && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
942 STR *tmpstr = str_static(&str_undef);
943 stab_fullname(tmpstr,((STAB*)str));/* a stab value, be nice */
948 if (str->str_cur && (fwrite(tmps,1,str->str_cur,fp) == 0 || ferror(fp)))
955 do_aprint(arg,fp,arglast)
960 register STR **st = stack->ary_array;
961 register int sp = arglast[1];
963 register int items = arglast[2] - sp;
967 warn("print to unopened file");
971 if (arg->arg_type == O_PRTF) {
972 do_sprintf(arg->arg_ptr.arg_str,items,st);
973 retval = do_print(arg->arg_ptr.arg_str,fp);
976 retval = (items <= 0);
977 for (; items > 0; items--,st++) {
978 if (retval && ofslen) {
979 if (fwrite(ofs, 1, ofslen, fp) == 0 || ferror(fp)) {
984 if (!(retval = do_print(*st, fp)))
987 if (retval && orslen)
988 if (fwrite(ors, 1, orslen, fp) == 0 || ferror(fp))
1001 if (arg[1].arg_type & A_DONT) {
1002 stio = stab_io(arg[1].arg_ptr.arg_stab);
1003 if (stio && stio->ifp) {
1004 statstab = arg[1].arg_ptr.arg_stab;
1005 str_set(statname,"");
1006 return (laststatval = fstat(fileno(stio->ifp), &statcache));
1009 if (arg[1].arg_ptr.arg_stab == defstab)
1012 warn("Stat on unopened file <%s>",
1013 stab_name(arg[1].arg_ptr.arg_stab));
1014 statstab = Nullstab;
1015 str_set(statname,"");
1016 return (laststatval = -1);
1020 statstab = Nullstab;
1021 str_set(statname,str_get(str));
1022 return (laststatval = stat(str_get(str),&statcache));
1031 if (arg[1].arg_type & A_DONT)
1032 fatal("You must supply explicit filename with -l");
1034 statstab = Nullstab;
1035 str_set(statname,str_get(str));
1037 return (laststatval = lstat(str_get(str),&statcache));
1039 return (laststatval = stat(str_get(str),&statcache));
1052 register STDCHAR *s;
1053 register STIO *stio;
1055 if (arg[1].arg_type & A_DONT) {
1056 if (arg[1].arg_ptr.arg_stab == defstab) {
1058 stio = stab_io(statstab);
1061 goto really_filename;
1065 statstab = arg[1].arg_ptr.arg_stab;
1066 str_set(statname,"");
1067 stio = stab_io(statstab);
1069 if (stio && stio->ifp) {
1071 fstat(fileno(stio->ifp),&statcache);
1072 if (stio->ifp->_cnt <= 0) {
1073 i = getc(stio->ifp);
1075 (void)ungetc(i,stio->ifp);
1077 if (stio->ifp->_cnt <= 0) /* null file is anything */
1079 len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
1080 s = stio->ifp->_base;
1082 fatal("-T and -B not implemented on filehandles\n");
1087 warn("Test on unopened file <%s>",
1088 stab_name(arg[1].arg_ptr.arg_stab));
1093 statstab = Nullstab;
1094 str_set(statname,str_get(str));
1096 i = open(str_get(str),0);
1099 fstat(i,&statcache);
1100 len = read(i,tbuf,512);
1101 if (len <= 0) /* null file is anything */
1107 /* now scan s to look for textiness */
1109 for (i = 0; i < len; i++,s++) {
1110 if (!*s) { /* null never allowed in text */
1117 *s != '\n' && *s != '\r' && *s != '\b' &&
1118 *s != '\t' && *s != '\f' && *s != 27)
1122 if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
1129 do_aexec(really,arglast)
1133 register STR **st = stack->ary_array;
1134 register int sp = arglast[1];
1135 register int items = arglast[2] - sp;
1141 New(401,argv, items+1, char*);
1143 for (st += ++sp; items > 0; items--,st++) {
1145 *a++ = str_get(*st);
1151 if (*argv[0] != '/') /* will execvp use PATH? */
1152 taintenv(); /* testing IFS here is overkill, probably */
1154 if (really && *(tmps = str_get(really)))
1157 execvp(argv[0],argv);
1163 static char **Argv = Null(char **);
1164 static char *Cmd = Nullch;
1171 Argv = Null(char **);
1189 taintproper("Insecure dependency in exec");
1192 /* save an extra exec if possible */
1195 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
1211 if (s[-1] == '\'') {
1213 execl(cshname,"csh", flags,ncmd,(char*)0);
1221 /* see if there are shell metacharacters in it */
1223 for (s = cmd; *s && isalpha(*s); s++) ; /* catch VAR=val gizmo */
1226 for (s = cmd; *s; s++) {
1227 if (*s != ' ' && !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1228 if (*s == '\n' && !s[1]) {
1233 execl("/bin/sh","sh","-c",cmd,(char*)0);
1237 New(402,Argv, (s - cmd) / 2 + 2, char*);
1238 Cmd = nsavestr(cmd, s-cmd);
1240 for (s = Cmd; *s;) {
1241 while (*s && isspace(*s)) s++;
1244 while (*s && !isspace(*s)) s++;
1250 execvp(Argv[0],Argv);
1251 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1262 do_socket(stab, arglast)
1266 register STR **st = stack->ary_array;
1267 register int sp = arglast[1];
1268 register STIO *stio;
1269 int domain, type, protocol, fd;
1274 stio = stab_io(stab);
1276 stio = stab_io(stab) = stio_new();
1278 do_close(stab,FALSE);
1280 domain = (int)str_gnum(st[++sp]);
1281 type = (int)str_gnum(st[++sp]);
1282 protocol = (int)str_gnum(st[++sp]);
1284 taintproper("Insecure dependency in socket");
1286 fd = socket(domain,type,protocol);
1289 stio->ifp = fdopen(fd, "r"); /* stdio gets confused about sockets */
1290 stio->ofp = fdopen(fd, "w");
1297 do_bind(stab, arglast)
1301 register STR **st = stack->ary_array;
1302 register int sp = arglast[1];
1303 register STIO *stio;
1309 stio = stab_io(stab);
1310 if (!stio || !stio->ifp)
1313 addr = str_get(st[++sp]);
1315 taintproper("Insecure dependency in bind");
1317 return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1321 warn("bind() on closed fd");
1327 do_connect(stab, arglast)
1331 register STR **st = stack->ary_array;
1332 register int sp = arglast[1];
1333 register STIO *stio;
1339 stio = stab_io(stab);
1340 if (!stio || !stio->ifp)
1343 addr = str_get(st[++sp]);
1345 taintproper("Insecure dependency in connect");
1347 return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1351 warn("connect() on closed fd");
1357 do_listen(stab, arglast)
1361 register STR **st = stack->ary_array;
1362 register int sp = arglast[1];
1363 register STIO *stio;
1369 stio = stab_io(stab);
1370 if (!stio || !stio->ifp)
1373 backlog = (int)str_gnum(st[++sp]);
1374 return listen(fileno(stio->ifp), backlog) >= 0;
1378 warn("listen() on closed fd");
1383 do_accept(str, nstab, gstab)
1388 register STIO *nstio;
1389 register STIO *gstio;
1390 int len = sizeof buf;
1398 gstio = stab_io(gstab);
1399 nstio = stab_io(nstab);
1401 if (!gstio || !gstio->ifp)
1404 nstio = stab_io(nstab) = stio_new();
1405 else if (nstio->ifp)
1406 do_close(nstab,FALSE);
1408 fd = accept(fileno(gstio->ifp),buf,&len);
1411 nstio->ifp = fdopen(fd, "r");
1412 nstio->ofp = fdopen(fd, "w");
1415 str_nset(str, buf, len);
1420 warn("accept() on closed fd");
1422 str_sset(str,&str_undef);
1427 do_shutdown(stab, arglast)
1431 register STR **st = stack->ary_array;
1432 register int sp = arglast[1];
1433 register STIO *stio;
1439 stio = stab_io(stab);
1440 if (!stio || !stio->ifp)
1443 how = (int)str_gnum(st[++sp]);
1444 return shutdown(fileno(stio->ifp), how) >= 0;
1448 warn("shutdown() on closed fd");
1454 do_sopt(optype, stab, arglast)
1459 register STR **st = stack->ary_array;
1460 register int sp = arglast[1];
1461 register STIO *stio;
1469 stio = stab_io(stab);
1470 if (!stio || !stio->ifp)
1473 fd = fileno(stio->ifp);
1474 lvl = (int)str_gnum(st[sp+1]);
1475 optname = (int)str_gnum(st[sp+2]);
1478 st[sp] = str_2static(str_new(257));
1479 st[sp]->str_cur = 256;
1480 st[sp]->str_pok = 1;
1481 if (getsockopt(fd, lvl, optname, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1486 if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
1496 warn("[gs]etsockopt() on closed fd");
1497 st[sp] = &str_undef;
1503 do_getsockname(optype, stab, arglast)
1508 register STR **st = stack->ary_array;
1509 register int sp = arglast[1];
1510 register STIO *stio;
1516 stio = stab_io(stab);
1517 if (!stio || !stio->ifp)
1520 st[sp] = str_2static(str_new(257));
1521 st[sp]->str_cur = 256;
1522 st[sp]->str_pok = 1;
1523 fd = fileno(stio->ifp);
1526 if (getsockname(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1530 if (getpeername(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1539 warn("get{sock,peer}name() on closed fd");
1541 st[sp] = &str_undef;
1547 do_ghent(which,gimme,arglast)
1552 register ARRAY *ary = stack;
1553 register int sp = arglast[0];
1554 register char **elem;
1556 struct hostent *gethostbyname();
1557 struct hostent *gethostbyaddr();
1559 struct hostent *gethostent();
1561 struct hostent *hent;
1564 if (gimme != G_ARRAY) {
1565 astore(ary, ++sp, str_static(&str_undef));
1569 if (which == O_GHBYNAME) {
1570 char *name = str_get(ary->ary_array[sp+1]);
1572 hent = gethostbyname(name);
1574 else if (which == O_GHBYADDR) {
1575 STR *addrstr = ary->ary_array[sp+1];
1576 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1577 char *addr = str_get(addrstr);
1579 hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
1583 hent = gethostent();
1585 fatal("gethostent not implemented");
1589 (void)astore(ary, ++sp, str = str_static(&str_no));
1590 str_set(str, hent->h_name);
1591 (void)astore(ary, ++sp, str = str_static(&str_no));
1592 for (elem = hent->h_aliases; *elem; elem++) {
1593 str_cat(str, *elem);
1595 str_ncat(str," ",1);
1597 (void)astore(ary, ++sp, str = str_static(&str_no));
1598 str_numset(str, (double)hent->h_addrtype);
1599 (void)astore(ary, ++sp, str = str_static(&str_no));
1600 len = hent->h_length;
1601 str_numset(str, (double)len);
1603 for (elem = hent->h_addr_list; *elem; elem++) {
1604 (void)astore(ary, ++sp, str = str_static(&str_no));
1605 str_nset(str, *elem, len);
1608 (void)astore(ary, ++sp, str = str_static(&str_no));
1609 str_nset(str, hent->h_addr, len);
1614 (void)astore(ary, ++sp, str_static(&str_no));
1622 do_gnent(which,gimme,arglast)
1627 register ARRAY *ary = stack;
1628 register int sp = arglast[0];
1629 register char **elem;
1631 struct netent *getnetbyname();
1632 struct netent *getnetbyaddr();
1633 struct netent *getnetent();
1634 struct netent *nent;
1636 if (gimme != G_ARRAY) {
1637 astore(ary, ++sp, str_static(&str_undef));
1641 if (which == O_GNBYNAME) {
1642 char *name = str_get(ary->ary_array[sp+1]);
1644 nent = getnetbyname(name);
1646 else if (which == O_GNBYADDR) {
1647 STR *addrstr = ary->ary_array[sp+1];
1648 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1649 char *addr = str_get(addrstr);
1651 nent = getnetbyaddr(addr,addrtype);
1658 (void)astore(ary, ++sp, str = str_static(&str_no));
1659 str_set(str, nent->n_name);
1660 (void)astore(ary, ++sp, str = str_static(&str_no));
1661 for (elem = nent->n_aliases; *elem; elem++) {
1662 str_cat(str, *elem);
1664 str_ncat(str," ",1);
1666 (void)astore(ary, ++sp, str = str_static(&str_no));
1667 str_numset(str, (double)nent->n_addrtype);
1668 (void)astore(ary, ++sp, str = str_static(&str_no));
1669 str_numset(str, (double)nent->n_net);
1673 (void)astore(ary, ++sp, str_static(&str_no));
1681 do_gpent(which,gimme,arglast)
1686 register ARRAY *ary = stack;
1687 register int sp = arglast[0];
1688 register char **elem;
1690 struct protoent *getprotobyname();
1691 struct protoent *getprotobynumber();
1692 struct protoent *getprotoent();
1693 struct protoent *pent;
1695 if (gimme != G_ARRAY) {
1696 astore(ary, ++sp, str_static(&str_undef));
1700 if (which == O_GPBYNAME) {
1701 char *name = str_get(ary->ary_array[sp+1]);
1703 pent = getprotobyname(name);
1705 else if (which == O_GPBYNUMBER) {
1706 int proto = (int)str_gnum(ary->ary_array[sp+1]);
1708 pent = getprotobynumber(proto);
1711 pent = getprotoent();
1715 (void)astore(ary, ++sp, str = str_static(&str_no));
1716 str_set(str, pent->p_name);
1717 (void)astore(ary, ++sp, str = str_static(&str_no));
1718 for (elem = pent->p_aliases; *elem; elem++) {
1719 str_cat(str, *elem);
1721 str_ncat(str," ",1);
1723 (void)astore(ary, ++sp, str = str_static(&str_no));
1724 str_numset(str, (double)pent->p_proto);
1728 (void)astore(ary, ++sp, str_static(&str_no));
1736 do_gsent(which,gimme,arglast)
1741 register ARRAY *ary = stack;
1742 register int sp = arglast[0];
1743 register char **elem;
1745 struct servent *getservbyname();
1746 struct servent *getservbynumber();
1747 struct servent *getservent();
1748 struct servent *sent;
1750 if (gimme != G_ARRAY) {
1751 astore(ary, ++sp, str_static(&str_undef));
1755 if (which == O_GSBYNAME) {
1756 char *name = str_get(ary->ary_array[sp+1]);
1757 char *proto = str_get(ary->ary_array[sp+2]);
1759 if (proto && !*proto)
1762 sent = getservbyname(name,proto);
1764 else if (which == O_GSBYPORT) {
1765 int port = (int)str_gnum(ary->ary_array[sp+1]);
1766 char *proto = str_get(ary->ary_array[sp+2]);
1768 sent = getservbyport(port,proto);
1771 sent = getservent();
1774 (void)astore(ary, ++sp, str = str_static(&str_no));
1775 str_set(str, sent->s_name);
1776 (void)astore(ary, ++sp, str = str_static(&str_no));
1777 for (elem = sent->s_aliases; *elem; elem++) {
1778 str_cat(str, *elem);
1780 str_ncat(str," ",1);
1782 (void)astore(ary, ++sp, str = str_static(&str_no));
1784 str_numset(str, (double)ntohs(sent->s_port));
1786 str_numset(str, (double)(sent->s_port));
1788 (void)astore(ary, ++sp, str = str_static(&str_no));
1789 str_set(str, sent->s_proto);
1793 (void)astore(ary, ++sp, str_static(&str_no));
1804 do_select(gimme,arglast)
1808 register STR **st = stack->ary_array;
1809 register int sp = arglast[0];
1817 struct timeval timebuf;
1818 struct timeval *tbuf = &timebuf;
1820 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1826 #if BYTEORDER & 0xf0000
1827 #define ORDERBYTE (0x88888888 - BYTEORDER)
1829 #define ORDERBYTE (0x4444 - BYTEORDER)
1834 for (i = 1; i <= 3; i++) {
1835 j = st[sp+i]->str_cur;
1840 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1841 growsize = maxlen; /* little endians can use vecs directly */
1849 masksize = NFDBITS / NBBY;
1851 masksize = sizeof(long); /* documented int, everyone seems to use long */
1853 growsize = maxlen + (masksize - (maxlen % masksize));
1854 Zero(&fd_sets[0], 4, char*);
1857 for (i = 1; i <= 3; i++) {
1862 Str_Grow(str,growsize);
1863 s = str_get(str) + j;
1864 while (++j <= growsize) {
1868 else if (str->str_ptr) {
1869 Safefree(str->str_ptr);
1870 str->str_ptr = Nullch;
1873 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1876 New(403, fd_sets[i], growsize, char);
1877 for (offset = 0; offset < growsize; offset += masksize) {
1878 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1879 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1885 if (str->str_nok || str->str_pok) {
1886 value = str_gnum(str);
1889 timebuf.tv_sec = (long)value;
1890 value -= (double)timebuf.tv_sec;
1891 timebuf.tv_usec = (long)(value * 1000000.0);
1894 tbuf = Null(struct timeval*);
1896 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1910 for (i = 1; i <= 3; i++) {
1914 for (offset = 0; offset < growsize; offset += masksize) {
1915 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1916 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1922 st[++sp] = str_static(&str_no);
1923 str_numset(st[sp], (double)nfound);
1924 if (gimme == G_ARRAY && tbuf) {
1925 value = (double)(timebuf.tv_sec) +
1926 (double)(timebuf.tv_usec) / 1000000.0;
1927 st[++sp] = str_static(&str_no);
1928 str_numset(st[sp], value);
1936 do_spair(stab1, stab2, arglast)
1941 register STR **st = stack->ary_array;
1942 register int sp = arglast[2];
1943 register STIO *stio1;
1944 register STIO *stio2;
1945 int domain, type, protocol, fd[2];
1947 if (!stab1 || !stab2)
1950 stio1 = stab_io(stab1);
1951 stio2 = stab_io(stab2);
1953 stio1 = stab_io(stab1) = stio_new();
1954 else if (stio1->ifp)
1955 do_close(stab1,FALSE);
1957 stio2 = stab_io(stab2) = stio_new();
1958 else if (stio2->ifp)
1959 do_close(stab2,FALSE);
1961 domain = (int)str_gnum(st[++sp]);
1962 type = (int)str_gnum(st[++sp]);
1963 protocol = (int)str_gnum(st[++sp]);
1965 taintproper("Insecure dependency in socketpair");
1968 if (socketpair(domain,type,protocol,fd) < 0)
1971 fatal("Socketpair unimplemented");
1973 stio1->ifp = fdopen(fd[0], "r");
1974 stio1->ofp = fdopen(fd[0], "w");
1976 stio2->ifp = fdopen(fd[1], "r");
1977 stio2->ofp = fdopen(fd[1], "w");
1986 do_gpwent(which,gimme,arglast)
1992 register ARRAY *ary = stack;
1993 register int sp = arglast[0];
1995 struct passwd *getpwnam();
1996 struct passwd *getpwuid();
1997 struct passwd *getpwent();
1998 struct passwd *pwent;
2000 if (gimme != G_ARRAY) {
2001 astore(ary, ++sp, str_static(&str_undef));
2005 if (which == O_GPWNAM) {
2006 char *name = str_get(ary->ary_array[sp+1]);
2008 pwent = getpwnam(name);
2010 else if (which == O_GPWUID) {
2011 int uid = (int)str_gnum(ary->ary_array[sp+1]);
2013 pwent = getpwuid(uid);
2019 (void)astore(ary, ++sp, str = str_static(&str_no));
2020 str_set(str, pwent->pw_name);
2021 (void)astore(ary, ++sp, str = str_static(&str_no));
2022 str_set(str, pwent->pw_passwd);
2023 (void)astore(ary, ++sp, str = str_static(&str_no));
2024 str_numset(str, (double)pwent->pw_uid);
2025 (void)astore(ary, ++sp, str = str_static(&str_no));
2026 str_numset(str, (double)pwent->pw_gid);
2027 (void)astore(ary, ++sp, str = str_static(&str_no));
2029 str_numset(str, (double)pwent->pw_change);
2032 str_numset(str, (double)pwent->pw_quota);
2035 str_set(str, pwent->pw_age);
2039 (void)astore(ary, ++sp, str = str_static(&str_no));
2041 str_set(str,pwent->pw_class);
2044 str_set(str, pwent->pw_comment);
2047 (void)astore(ary, ++sp, str = str_static(&str_no));
2048 str_set(str, pwent->pw_gecos);
2049 (void)astore(ary, ++sp, str = str_static(&str_no));
2050 str_set(str, pwent->pw_dir);
2051 (void)astore(ary, ++sp, str = str_static(&str_no));
2052 str_set(str, pwent->pw_shell);
2054 (void)astore(ary, ++sp, str = str_static(&str_no));
2055 str_numset(str, (double)pwent->pw_expire);
2061 fatal("password routines not implemented");
2066 do_ggrent(which,gimme,arglast)
2072 register ARRAY *ary = stack;
2073 register int sp = arglast[0];
2074 register char **elem;
2076 struct group *getgrnam();
2077 struct group *getgrgid();
2078 struct group *getgrent();
2079 struct group *grent;
2081 if (gimme != G_ARRAY) {
2082 astore(ary, ++sp, str_static(&str_undef));
2086 if (which == O_GGRNAM) {
2087 char *name = str_get(ary->ary_array[sp+1]);
2089 grent = getgrnam(name);
2091 else if (which == O_GGRGID) {
2092 int gid = (int)str_gnum(ary->ary_array[sp+1]);
2094 grent = getgrgid(gid);
2100 (void)astore(ary, ++sp, str = str_static(&str_no));
2101 str_set(str, grent->gr_name);
2102 (void)astore(ary, ++sp, str = str_static(&str_no));
2103 str_set(str, grent->gr_passwd);
2104 (void)astore(ary, ++sp, str = str_static(&str_no));
2105 str_numset(str, (double)grent->gr_gid);
2106 (void)astore(ary, ++sp, str = str_static(&str_no));
2107 for (elem = grent->gr_mem; *elem; elem++) {
2108 str_cat(str, *elem);
2110 str_ncat(str," ",1);
2116 fatal("group routines not implemented");
2121 do_dirop(optype,stab,gimme,arglast)
2127 #if defined(DIRENT) && defined(READDIR)
2128 register ARRAY *ary = stack;
2129 register STR **st = ary->ary_array;
2130 register int sp = arglast[1];
2131 register STIO *stio;
2136 struct DIRENT *readdir();
2137 register struct DIRENT *dp;
2141 if (!(stio = stab_io(stab)))
2142 stio = stab_io(stab) = stio_new();
2143 if (!stio->dirp && optype != O_OPENDIR)
2149 closedir(stio->dirp);
2150 if (!(stio->dirp = opendir(str_get(st[sp+1]))))
2154 if (gimme == G_ARRAY) {
2156 while (dp = readdir(stio->dirp)) {
2158 (void)astore(ary,++sp,
2159 str_2static(str_make(dp->d_name,dp->d_namlen)));
2161 (void)astore(ary,++sp,
2162 str_2static(str_make(dp->d_name,0)));
2167 if (!(dp = readdir(stio->dirp)))
2169 st[sp] = str_static(&str_undef);
2171 str_nset(st[sp], dp->d_name, dp->d_namlen);
2173 str_set(st[sp], dp->d_name);
2183 st[sp] = str_static(&str_undef);
2184 str_numset(st[sp], (double)telldir(stio->dirp));
2187 st[sp] = str_static(&str_undef);
2188 along = (long)str_gnum(st[sp+1]);
2189 (void)seekdir(stio->dirp,along);
2193 st[sp] = str_static(&str_undef);
2194 (void)rewinddir(stio->dirp);
2197 st[sp] = str_static(&str_undef);
2198 (void)closedir(stio->dirp);
2205 st[sp] = &str_undef;
2209 fatal("Unimplemented directory operation");
2217 register STR **st = stack->ary_array;
2218 register int sp = arglast[1];
2219 register int items = arglast[2] - sp;
2222 register int tot = 0;
2226 for (st += ++sp; items--; st++)
2227 tainted |= (*st)->str_tainted;
2228 st = stack->ary_array;
2230 items = arglast[2] - sp;
2235 taintproper("Insecure dependency in chmod");
2239 val = (int)str_gnum(st[++sp]);
2241 if (chmod(str_get(st[++sp]),val))
2249 taintproper("Insecure dependency in chown");
2254 val = (int)str_gnum(st[++sp]);
2255 val2 = (int)str_gnum(st[++sp]);
2257 if (chown(str_get(st[++sp]),val,val2))
2266 taintproper("Insecure dependency in kill");
2270 s = str_get(st[++sp]);
2272 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
2274 if (!(val = whichsig(s)))
2275 fatal("Unrecognized signal name \"%s\"",s);
2278 val = (int)str_gnum(st[sp]);
2282 int proc = (int)str_gnum(st[++sp]);
2284 if (killpg(proc,val)) /* BSD */
2286 if (kill(-proc,val)) /* SYSV */
2293 if (kill((int)(str_gnum(st[++sp])),val))
2302 taintproper("Insecure dependency in unlink");
2306 s = str_get(st[++sp]);
2307 if (euid || unsafe) {
2311 else { /* don't let root wipe out directories without -U */
2313 if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
2315 if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
2327 taintproper("Insecure dependency in utime");
2331 struct utimbuf utbuf;
2339 Zero(&utbuf, sizeof utbuf, char);
2340 utbuf.actime = (long)str_gnum(st[++sp]); /* time accessed */
2341 utbuf.modtime = (long)str_gnum(st[++sp]); /* time modified */
2346 if (utime(str_get(st[++sp]),&utbuf))
2358 /* Do the permissions allow some operation? Assumes statcache already set. */
2361 cando(bit, effective, statbufp)
2364 register struct stat *statbufp;
2366 if ((effective ? euid : uid) == 0) { /* root is special */
2367 if (bit == S_IXUSR) {
2368 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
2372 return TRUE; /* root reads and writes anything */
2375 if (statbufp->st_uid == (effective ? euid : uid) ) {
2376 if (statbufp->st_mode & bit)
2377 return TRUE; /* ok as "user" */
2379 else if (ingroup((int)statbufp->st_gid,effective)) {
2380 if (statbufp->st_mode & bit >> 3)
2381 return TRUE; /* ok as "group" */
2383 else if (statbufp->st_mode & bit >> 6)
2384 return TRUE; /* ok as "other" */
2389 ingroup(testgid,effective)
2393 if (testgid == (effective ? egid : gid))
2400 GIDTYPE gary[NGROUPS];
2403 anum = getgroups(NGROUPS,gary);
2405 if (gary[anum] == testgid)
2415 do_ipcget(optype, arglast)
2419 register STR **st = stack->ary_array;
2420 register int sp = arglast[0];
2424 key = (key_t)str_gnum(st[++sp]);
2425 n = (optype == O_MSGGET) ? 0 : (int)str_gnum(st[++sp]);
2426 flags = (int)str_gnum(st[++sp]);
2432 return msgget(key, flags);
2436 return semget(key, n, flags);
2440 return shmget(key, n, flags);
2442 #if !defined(IPCMSG) || !defined(IPCSEM) || !defined(IPCSHM)
2444 fatal("%s not implemented", opname[optype]);
2447 return -1; /* should never happen */
2451 do_ipcctl(optype, arglast)
2455 register STR **st = stack->ary_array;
2456 register int sp = arglast[0];
2459 int id, n, cmd, infosize, getinfo, ret;
2461 id = (int)str_gnum(st[++sp]);
2462 n = (optype == O_SEMCTL) ? (int)str_gnum(st[++sp]) : 0;
2463 cmd = (int)str_gnum(st[++sp]);
2467 getinfo = (cmd == IPC_STAT);
2473 if (cmd == IPC_STAT || cmd == IPC_SET)
2474 infosize = sizeof(struct msqid_ds);
2479 if (cmd == IPC_STAT || cmd == IPC_SET)
2480 infosize = sizeof(struct shmid_ds);
2485 if (cmd == IPC_STAT || cmd == IPC_SET)
2486 infosize = sizeof(struct semid_ds);
2487 else if (cmd == GETALL || cmd == SETALL)
2489 struct semid_ds semds;
2490 if (semctl(id, 0, IPC_STAT, &semds) == -1)
2492 getinfo = (cmd == GETALL);
2493 infosize = semds.sem_nsems * sizeof(ushort);
2497 #if !defined(IPCMSG) || !defined(IPCSEM) || !defined(IPCSHM)
2499 fatal("%s not implemented", opname[optype]);
2507 STR_GROW(astr, infosize+1);
2513 if (astr->str_cur != infosize)
2522 int i = (int)str_gnum(astr);
2523 a = (char *)i; /* ouch */
2530 ret = msgctl(id, cmd, a);
2535 ret = semctl(id, n, cmd, a);
2540 ret = shmctl(id, cmd, a);
2544 if (getinfo && ret >= 0) {
2545 astr->str_cur = infosize;
2546 astr->str_ptr[infosize] = '\0';
2556 register STR **st = stack->ary_array;
2557 register int sp = arglast[0];
2560 int id, msize, flags;
2562 id = (int)str_gnum(st[++sp]);
2564 flags = (int)str_gnum(st[++sp]);
2565 mbuf = str_get(mstr);
2566 if ((msize = mstr->str_cur - sizeof(long)) < 0) {
2571 return msgsnd(id, mbuf, msize, flags);
2573 fatal("msgsnd not implemented");
2582 register STR **st = stack->ary_array;
2583 register int sp = arglast[0];
2587 int id, msize, flags, ret;
2589 id = (int)str_gnum(st[++sp]);
2591 msize = (int)str_gnum(st[++sp]);
2592 mtype = (long)str_gnum(st[++sp]);
2593 flags = (int)str_gnum(st[++sp]);
2594 mbuf = str_get(mstr);
2595 if (mstr->str_cur < sizeof(long)+msize+1) {
2596 STR_GROW(mstr, sizeof(long)+msize+1);
2597 mbuf = str_get(mstr);
2600 ret = msgrcv(id, mbuf, msize, mtype, flags);
2602 mstr->str_cur = sizeof(long)+ret;
2603 mstr->str_ptr[sizeof(long)+ret] = '\0';
2607 fatal("msgrcv not implemented");
2616 register STR **st = stack->ary_array;
2617 register int sp = arglast[0];
2622 id = (int)str_gnum(st[++sp]);
2624 opbuf = str_get(opstr);
2625 opsize = opstr->str_cur;
2626 if (opsize < sizeof(struct sembuf)
2627 || (opsize % sizeof(struct sembuf)) != 0) {
2632 return semop(id, opbuf, opsize/sizeof(struct sembuf));
2634 fatal("semop not implemented");
2639 do_shmio(optype, arglast)
2644 register STR **st = stack->ary_array;
2645 register int sp = arglast[0];
2648 int id, mpos, msize;
2649 struct shmid_ds shmds;
2650 extern char *shmat();
2652 id = (int)str_gnum(st[++sp]);
2654 mpos = (int)str_gnum(st[++sp]);
2655 msize = (int)str_gnum(st[++sp]);
2657 if (shmctl(id, IPC_STAT, &shmds) == -1)
2659 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2660 errno = EFAULT; /* can't do as caller requested */
2663 shm = shmat(id, (char *)NULL, (optype == O_SHMREAD) ? SHM_RDONLY : 0);
2664 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2666 mbuf = str_get(mstr);
2667 if (optype == O_SHMREAD) {
2668 if (mstr->str_cur < msize) {
2669 STR_GROW(mstr, msize+1);
2670 mbuf = str_get(mstr);
2672 bcopy(shm + mpos, mbuf, msize);
2673 mstr->str_cur = msize;
2674 mstr->str_ptr[msize] = '\0';
2679 if ((n = mstr->str_cur) > msize)
2681 bcopy(mbuf, shm + mpos, n);
2683 bzero(shm + mpos + n, msize - n);
2687 fatal("shm I/O not implemented");
2691 #endif /* SYSVIPC */