1 /* $RCSfile: doio.c,v $$Revision: 4.0.1.6 $$Date: 92/06/11 21:08:16 $
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.0.1.6 92/06/11 21:08:16 lwall
10 * patch34: some systems don't declare h_errno extern in header files
12 * Revision 4.0.1.5 92/06/08 13:00:21 lwall
13 * patch20: some machines don't define ENOTSOCK in errno.h
14 * patch20: new warnings for failed use of stat operators on filenames with \n
15 * patch20: wait failed when STDOUT or STDERR reopened to a pipe
16 * patch20: end of file latch not reset on reopen of STDIN
17 * patch20: seek(HANDLE, 0, 1) went to eof because of ancient Ultrix workaround
18 * patch20: fixed memory leak on system() for vfork() machines
19 * patch20: get*by* routines now return something useful in a scalar context
20 * patch20: h_errno now accessible via $?
22 * Revision 4.0.1.4 91/11/05 16:51:43 lwall
23 * patch11: prepared for ctype implementations that don't define isascii()
24 * patch11: perl mistook some streams for sockets because they return mode 0 too
25 * patch11: reopening STDIN, STDOUT and STDERR failed on some machines
26 * patch11: certain perl errors should set EBADF so that $! looks better
27 * patch11: truncate on a closed filehandle could dump
28 * patch11: stats of _ forgot whether prior stat was actually lstat
29 * patch11: -T returned true on NFS directory
31 * Revision 4.0.1.3 91/06/10 01:21:19 lwall
32 * patch10: read didn't work from character special files open for writing
33 * patch10: close-on-exec wrongly set on system file descriptors
35 * Revision 4.0.1.2 91/06/07 10:53:39 lwall
36 * patch4: new copyright notice
37 * patch4: system fd's are now treated specially
38 * patch4: added $^F variable to specify maximum system fd, default 2
39 * patch4: character special files now opened with bidirectional stdio buffers
40 * patch4: taintchecks could improperly modify parent in vfork()
41 * patch4: many, many itty-bitty portability fixes
43 * Revision 4.0.1.1 91/04/11 17:41:06 lwall
44 * patch1: hopefully straightened out some of the Xenix mess
46 * Revision 4.0 91/03/20 01:07:06 lwall
55 #include <sys/socket.h>
58 #include <net/errno.h>
65 #include <sys/select.h>
74 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
100 #include <sys/file.h>
103 int laststatval = -1;
104 int laststype = O_STAT;
106 static char* warn_nl = "Unsuccessful %s on filename containing newline";
109 do_open(stab,name,len)
115 register STIO *stio = stab_io(stab);
116 char *myname = savestr(name);
120 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
121 FILE *saveifp = Nullfp;
122 FILE *saveofp = Nullfp;
125 mode[0] = mode[1] = mode[2] = '\0';
127 forkprocess = 1; /* assume true if no fork */
128 while (len && isSPACE(name[len-1]))
131 stio = stab_io(stab) = stio_new();
132 else if (stio->ifp) {
133 fd = fileno(stio->ifp);
134 if (stio->type == '-')
136 else if (fd <= maxsysfd) {
139 savetype = stio->type;
142 else if (stio->type == '|')
143 result = mypclose(stio->ifp);
144 else if (stio->ifp != stio->ofp) {
146 result = fclose(stio->ofp);
147 fclose(stio->ifp); /* clear stdio, fd already closed */
150 result = fclose(stio->ifp);
153 result = fclose(stio->ifp);
154 if (result == EOF && fd > maxsysfd)
155 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
157 stio->ofp = stio->ifp = Nullfp;
159 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
171 for (name++; isSPACE(*name); name++) ;
174 taintproper("Insecure dependency in piped open");
176 fp = mypopen(name,"w");
179 else if (*name == '>') {
181 taintproper("Insecure dependency in open");
185 mode[0] = stio->type = 'a';
194 while (isSPACE(*name))
199 stab = stabent(name,FALSE);
200 if (!stab || !stab_io(stab)) {
206 if (stab_io(stab) && stab_io(stab)->ifp) {
207 fd = fileno(stab_io(stab)->ifp);
208 if (stab_io(stab)->type == 's')
214 if (!(fp = fdopen(fd = dup(fd),mode))) {
219 while (isSPACE(*name))
221 if (strEQ(name,"-")) {
226 fp = fopen(name,mode);
234 while (isSPACE(*name))
238 if (strEQ(name,"-")) {
243 fp = fopen(name,mode);
245 else if (name[len-1] == '|') {
248 taintproper("Insecure dependency in piped open");
251 while (len && isSPACE(name[len-1]))
254 for (; isSPACE(*name); name++) ;
255 fp = mypopen(name,"r");
261 for (; isSPACE(*name); name++) ;
262 if (strEQ(name,"-")) {
267 fp = fopen(name,"r");
271 if (dowarn && stio->type == '<' && index(name, '\n'))
272 warn(warn_nl, "open");
278 stio->type != '|' && stio->type != '-') {
279 if (fstat(fileno(fp),&statbuf) < 0) {
283 if (S_ISSOCK(statbuf.st_mode))
284 stio->type = 's'; /* in case a socket was passed in to us */
288 !(statbuf.st_mode & S_IFMT)
293 int buflen = sizeof tokenbuf;
294 if (getsockname(fileno(fp), tokenbuf, &buflen) >= 0
295 || errno != ENOTSOCK)
296 stio->type = 's'; /* some OS's return 0 on fstat()ed socket */
297 /* but some return 0 for streams too, sigh */
301 if (saveifp) { /* must use old fp? */
302 fd = fileno(saveifp);
304 fflush(saveofp); /* emulate fclose() */
305 if (saveofp != saveifp) { /* was a socket? */
311 if (fd != fileno(fp)) {
315 dup2(fileno(fp), fd);
316 str = afetch(fdpid,fileno(fp),TRUE);
317 pid = str->str_u.str_useful;
318 str->str_u.str_useful = 0;
319 str = afetch(fdpid,fd,TRUE);
320 str->str_u.str_useful = pid;
327 #if defined(HAS_FCNTL) && defined(F_SETFD)
329 fcntl(fd,F_SETFD,fd > maxsysfd);
333 if (stio->type == 's'
334 || (stio->type == '>' && S_ISCHR(statbuf.st_mode)) ) {
335 if (!(stio->ofp = fdopen(fileno(fp),"w"))) {
349 stio->type = savetype;
358 #ifndef FLEXFILENAMES
364 static int filemode = 0;
366 static char *oldname;
369 argvoutstab = stabent("ARGVOUT",TRUE);
370 if (filemode & (S_ISUID|S_ISGID)) {
371 fflush(stab_io(argvoutstab)->ifp); /* chmod must follow last write */
373 (void)fchmod(lastfd,filemode);
375 (void)chmod(oldname,filemode);
379 while (alen(stab_xarray(stab)) >= 0) {
380 str = ashift(stab_xarray(stab));
381 str_sset(stab_val(stab),str);
382 STABSET(stab_val(stab));
383 oldname = str_get(stab_val(stab));
384 if (do_open(stab,oldname,stab_val(stab)->str_cur)) {
387 taintproper("Insecure dependency in inplace open");
389 if (strEQ(oldname,"-")) {
391 defoutstab = stabent("STDOUT",TRUE);
392 return stab_io(stab)->ifp;
394 #ifndef FLEXFILENAMES
395 filedev = statbuf.st_dev;
396 fileino = statbuf.st_ino;
398 filemode = statbuf.st_mode;
399 fileuid = statbuf.st_uid;
400 filegid = statbuf.st_gid;
401 if (!S_ISREG(filemode)) {
402 warn("Can't do inplace edit: %s is not a regular file",
404 do_close(stab,FALSE);
410 add_suffix(str,inplace);
412 str_cat(str,inplace);
414 #ifndef FLEXFILENAMES
415 if (stat(str->str_ptr,&statbuf) >= 0
416 && statbuf.st_dev == filedev
417 && statbuf.st_ino == fileino ) {
418 warn("Can't do inplace edit: %s > 14 characters",
420 do_close(stab,FALSE);
427 if (rename(oldname,str->str_ptr) < 0) {
428 warn("Can't rename %s to %s: %s, skipping file",
429 oldname, str->str_ptr, strerror(errno) );
430 do_close(stab,FALSE);
435 do_close(stab,FALSE);
436 (void)unlink(str->str_ptr);
437 (void)rename(oldname,str->str_ptr);
438 do_open(stab,str->str_ptr,stab_val(stab)->str_cur);
441 (void)UNLINK(str->str_ptr);
442 if (link(oldname,str->str_ptr) < 0) {
443 warn("Can't rename %s to %s: %s, skipping file",
444 oldname, str->str_ptr, strerror(errno) );
445 do_close(stab,FALSE);
449 (void)UNLINK(oldname);
454 if (UNLINK(oldname) < 0) {
455 warn("Can't rename %s to %s: %s, skipping file",
456 oldname, str->str_ptr, strerror(errno) );
457 do_close(stab,FALSE);
462 fatal("Can't do inplace edit without backup");
467 str_cat(str,oldname);
468 errno = 0; /* in case sprintf set errno */
469 if (!do_open(argvoutstab,str->str_ptr,str->str_cur)) {
470 warn("Can't do inplace edit on %s: %s",
471 oldname, strerror(errno) );
472 do_close(stab,FALSE);
476 defoutstab = argvoutstab;
477 lastfd = fileno(stab_io(argvoutstab)->ifp);
478 (void)fstat(lastfd,&statbuf);
480 (void)fchmod(lastfd,filemode);
482 (void)chmod(oldname,filemode);
484 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
486 (void)fchown(lastfd,fileuid,filegid);
489 (void)chown(oldname,fileuid,filegid);
495 return stab_io(stab)->ifp;
498 fprintf(stderr,"Can't open %s: %s\n",str_get(str), strerror(errno));
502 (void)do_close(argvoutstab,FALSE);
503 defoutstab = stabent("STDOUT",TRUE);
510 do_pipe(str, rstab, wstab)
515 register STIO *rstio;
516 register STIO *wstio;
524 rstio = stab_io(rstab);
525 wstio = stab_io(wstab);
528 rstio = stab_io(rstab) = stio_new();
530 do_close(rstab,FALSE);
532 wstio = stab_io(wstab) = stio_new();
534 do_close(wstab,FALSE);
538 rstio->ifp = fdopen(fd[0], "r");
539 wstio->ofp = fdopen(fd[1], "w");
540 wstio->ifp = wstio->ofp;
543 if (!rstio->ifp || !wstio->ofp) {
544 if (rstio->ifp) fclose(rstio->ifp);
546 if (wstio->ofp) fclose(wstio->ofp);
551 str_sset(str,&str_yes);
555 str_sset(str,&str_undef);
561 do_close(stab,explicit)
575 stio = stab_io(stab);
576 if (!stio) { /* never opened */
577 if (dowarn && explicit)
578 warn("Close on unopened file <%s>",stab_ename(stab));
582 if (stio->type == '|') {
583 status = mypclose(stio->ifp);
584 retval = (status == 0);
585 statusvalue = (unsigned short)status & 0xffff;
587 else if (stio->type == '-')
590 if (stio->ofp && stio->ofp != stio->ifp) { /* a socket */
591 retval = (fclose(stio->ofp) != EOF);
592 fclose(stio->ifp); /* clear stdio, fd already closed */
595 retval = (fclose(stio->ifp) != EOF);
597 stio->ofp = stio->ifp = Nullfp;
612 if (!stab) { /* eof() */
614 stio = stab_io(argvstab);
619 stio = stab_io(stab);
626 #ifdef STDSTDIO /* (the code works without this) */
627 if (stio->ifp->_cnt > 0) /* cheat a little, since */
628 return FALSE; /* this is the most usual case */
631 ch = getc(stio->ifp);
633 (void)ungetc(ch, stio->ifp);
637 if (stio->ifp->_cnt < -1)
638 stio->ifp->_cnt = -1;
640 if (!stab) { /* not necessarily a real EOF yet? */
641 if (!nextargv(argvstab)) /* get another fp handy */
645 return TRUE; /* normal fp, definitely end of file */
659 stio = stab_io(stab);
660 if (!stio || !stio->ifp)
663 #ifdef ULTRIX_STDIO_BOTCH
665 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
668 return ftell(stio->ifp);
672 warn("tell() on unopened file");
678 do_seek(stab, pos, whence)
688 stio = stab_io(stab);
689 if (!stio || !stio->ifp)
692 #ifdef ULTRIX_STDIO_BOTCH
694 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
697 return fseek(stio->ifp, pos, whence) >= 0;
701 warn("seek() on unopened file");
707 do_ctl(optype,stab,func,argstr)
717 if (!stab || !argstr || !(stio = stab_io(stab)) || !stio->ifp) {
718 errno = EBADF; /* well, sort of... */
722 if (argstr->str_pok || !argstr->str_nok) {
723 if (!argstr->str_pok)
728 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
732 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
734 retval = 256; /* otherwise guess at what's safe */
736 if (argstr->str_cur < retval) {
737 Str_Grow(argstr,retval+1);
738 argstr->str_cur = retval;
742 s[argstr->str_cur] = 17; /* a little sanity check here */
745 retval = (int)str_gnum(argstr);
747 s = (char*)(long)retval; /* ouch */
749 s = (char*)retval; /* ouch */
754 if (optype == O_IOCTL)
755 retval = ioctl(fileno(stio->ifp), func, s);
758 fatal("fcntl is not implemented");
761 retval = fcntl(fileno(stio->ifp), func, s);
763 fatal("fcntl is not implemented");
770 if (argstr->str_pok) {
771 if (s[argstr->str_cur] != 17)
772 fatal("Return value overflowed string");
773 s[argstr->str_cur] = 0; /* put our null back */
779 do_stat(str,arg,gimme,arglast)
785 register ARRAY *ary = stack;
786 register int sp = arglast[0] + 1;
789 if ((arg[1].arg_type & A_MASK) == A_WORD) {
790 tmpstab = arg[1].arg_ptr.arg_stab;
791 if (tmpstab != defstab) {
794 str_set(statname,"");
795 if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
796 fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
801 else if (laststatval < 0)
805 str_set(statname,str_get(ary->ary_array[sp]));
808 laststype = arg->arg_type;
809 if (arg->arg_type == O_LSTAT)
810 laststatval = lstat(str_get(statname),&statcache);
813 laststatval = stat(str_get(statname),&statcache);
814 if (laststatval < 0) {
815 if (dowarn && index(str_get(statname), '\n'))
816 warn(warn_nl, "stat");
821 if (gimme != G_ARRAY) {
823 str_sset(str,&str_yes);
825 str_sset(str,&str_undef);
827 ary->ary_array[sp] = str;
833 (void)astore(ary,++sp,
834 str_2mortal(str_nmake((double)statcache.st_dev)));
835 (void)astore(ary,++sp,
836 str_2mortal(str_nmake((double)statcache.st_ino)));
837 (void)astore(ary,++sp,
838 str_2mortal(str_nmake((double)statcache.st_mode)));
839 (void)astore(ary,++sp,
840 str_2mortal(str_nmake((double)statcache.st_nlink)));
841 (void)astore(ary,++sp,
842 str_2mortal(str_nmake((double)statcache.st_uid)));
843 (void)astore(ary,++sp,
844 str_2mortal(str_nmake((double)statcache.st_gid)));
845 (void)astore(ary,++sp,
846 str_2mortal(str_nmake((double)statcache.st_rdev)));
847 (void)astore(ary,++sp,
848 str_2mortal(str_nmake((double)statcache.st_size)));
849 (void)astore(ary,++sp,
850 str_2mortal(str_nmake((double)statcache.st_atime)));
851 (void)astore(ary,++sp,
852 str_2mortal(str_nmake((double)statcache.st_mtime)));
853 (void)astore(ary,++sp,
854 str_2mortal(str_nmake((double)statcache.st_ctime)));
856 (void)astore(ary,++sp,
857 str_2mortal(str_nmake((double)statcache.st_blksize)));
858 (void)astore(ary,++sp,
859 str_2mortal(str_nmake((double)statcache.st_blocks)));
861 (void)astore(ary,++sp,
862 str_2mortal(str_make("",0)));
863 (void)astore(ary,++sp,
864 str_2mortal(str_make("",0)));
867 (void)astore(ary,++sp,str_nmake(0.0));
873 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
874 /* code courtesy of William Kucharski */
877 int chsize(fd, length)
878 int fd; /* file descriptor */
879 off_t length; /* length to set file to */
885 if (fstat(fd, &filebuf) < 0)
888 if (filebuf.st_size < length) {
890 /* extend file length */
892 if ((lseek(fd, (length - 1), 0)) < 0)
895 /* write a "0" byte */
897 if ((write(fd, "", 1)) != 1)
901 /* truncate length */
906 fl.l_type = F_WRLCK; /* write lock on file space */
909 * This relies on the UNDOCUMENTED F_FREESP argument to
910 * fcntl(2), which truncates the file so that it ends at the
911 * position indicated by fl.l_start.
913 * Will minor miracles never cease?
916 if (fcntl(fd, F_FREESP, &fl) < 0)
923 #endif /* F_FREESP */
926 do_truncate(str,arg,gimme,arglast)
932 register ARRAY *ary = stack;
933 register int sp = arglast[0] + 1;
934 off_t len = (off_t)str_gnum(ary->ary_array[sp+1]);
938 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE)
940 if ((arg[1].arg_type & A_MASK) == A_WORD) {
941 tmpstab = arg[1].arg_ptr.arg_stab;
942 if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
943 ftruncate(fileno(stab_io(tmpstab)->ifp), len) < 0)
946 else if (truncate(str_get(ary->ary_array[sp]), len) < 0)
949 if ((arg[1].arg_type & A_MASK) == A_WORD) {
950 tmpstab = arg[1].arg_ptr.arg_stab;
951 if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
952 chsize(fileno(stab_io(tmpstab)->ifp), len) < 0)
958 if ((tmpfd = open(str_get(ary->ary_array[sp]), 0)) < 0)
961 if (chsize(tmpfd, len) < 0)
969 str_sset(str,&str_yes);
971 str_sset(str,&str_undef);
973 ary->ary_array[sp] = str;
976 fatal("truncate not implemented");
981 looks_like_number(str)
990 send = s + str->str_cur;
995 if (*s == '+' || *s == '-')
1003 else if (s == str->str_ptr)
1009 if (*s == 'e' || *s == 'E') {
1011 if (*s == '+' || *s == '-')
1028 register char *tmps;
1032 warn("print to unopened file");
1039 ((str->str_nok && str->str_u.str_nval != 0.0)
1040 || (looks_like_number(str) && str_gnum(str) != 0.0) ) ) {
1041 fprintf(fp, ofmt, str->str_u.str_nval);
1045 tmps = str_get(str);
1046 if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'B' && tmps[3] == '\0'
1047 && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
1048 STR *tmpstr = str_mortal(&str_undef);
1049 stab_efullname(tmpstr,((STAB*)str));/* a stab value, be nice */
1051 tmps = str->str_ptr;
1054 if (str->str_cur && (fwrite(tmps,1,str->str_cur,fp) == 0 || ferror(fp)))
1061 do_aprint(arg,fp,arglast)
1066 register STR **st = stack->ary_array;
1067 register int sp = arglast[1];
1068 register int retval;
1069 register int items = arglast[2] - sp;
1073 warn("print to unopened file");
1078 if (arg->arg_type == O_PRTF) {
1079 do_sprintf(arg->arg_ptr.arg_str,items,st);
1080 retval = do_print(arg->arg_ptr.arg_str,fp);
1083 retval = (items <= 0);
1084 for (; items > 0; items--,st++) {
1085 if (retval && ofslen) {
1086 if (fwrite(ofs, 1, ofslen, fp) == 0 || ferror(fp)) {
1091 if (!(retval = do_print(*st, fp)))
1094 if (retval && orslen)
1095 if (fwrite(ors, 1, orslen, fp) == 0 || ferror(fp))
1108 if (arg[1].arg_type & A_DONT) {
1109 stio = stab_io(arg[1].arg_ptr.arg_stab);
1110 if (stio && stio->ifp) {
1111 statstab = arg[1].arg_ptr.arg_stab;
1112 str_set(statname,"");
1114 return (laststatval = fstat(fileno(stio->ifp), &statcache));
1117 if (arg[1].arg_ptr.arg_stab == defstab)
1120 warn("Stat on unopened file <%s>",
1121 stab_ename(arg[1].arg_ptr.arg_stab));
1122 statstab = Nullstab;
1123 str_set(statname,"");
1124 return (laststatval = -1);
1128 statstab = Nullstab;
1129 str_set(statname,str_get(str));
1131 laststatval = stat(str_get(str),&statcache);
1132 if (laststatval < 0 && dowarn && index(str_get(str), '\n'))
1133 warn(warn_nl, "stat");
1143 if (arg[1].arg_type & A_DONT) {
1144 if (arg[1].arg_ptr.arg_stab == defstab) {
1145 if (laststype != O_LSTAT)
1146 fatal("The stat preceding -l _ wasn't an lstat");
1149 fatal("You can't use -l on a filehandle");
1152 laststype = O_LSTAT;
1153 statstab = Nullstab;
1154 str_set(statname,str_get(str));
1156 laststatval = lstat(str_get(str),&statcache);
1158 laststatval = stat(str_get(str),&statcache);
1160 if (laststatval < 0 && dowarn && index(str_get(str), '\n'))
1161 warn(warn_nl, "lstat");
1174 register STDCHAR *s;
1175 register STIO *stio;
1177 if (arg[1].arg_type & A_DONT) {
1178 if (arg[1].arg_ptr.arg_stab == defstab) {
1180 stio = stab_io(statstab);
1183 goto really_filename;
1187 statstab = arg[1].arg_ptr.arg_stab;
1188 str_set(statname,"");
1189 stio = stab_io(statstab);
1191 if (stio && stio->ifp) {
1192 #if defined(STDSTDIO) || defined(atarist) /* this will work with atariST */
1193 fstat(fileno(stio->ifp),&statcache);
1194 if (S_ISDIR(statcache.st_mode)) /* handle NFS glitch */
1195 return arg->arg_type == O_FTTEXT ? &str_no : &str_yes;
1196 if (stio->ifp->_cnt <= 0) {
1197 i = getc(stio->ifp);
1199 (void)ungetc(i,stio->ifp);
1201 if (stio->ifp->_cnt <= 0) /* null file is anything */
1203 len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
1204 s = stio->ifp->_base;
1206 fatal("-T and -B not implemented on filehandles");
1211 warn("Test on unopened file <%s>",
1212 stab_ename(arg[1].arg_ptr.arg_stab));
1218 statstab = Nullstab;
1219 str_set(statname,str_get(str));
1221 i = open(str_get(str),0);
1223 if (dowarn && index(str_get(str), '\n'))
1224 warn(warn_nl, "open");
1227 fstat(i,&statcache);
1228 len = read(i,tbuf,512);
1231 if (S_ISDIR(statcache.st_mode) && arg->arg_type == O_FTTEXT)
1232 return &str_no; /* special case NFS directories */
1233 return &str_yes; /* null file is anything */
1238 /* now scan s to look for textiness */
1240 for (i = 0; i < len; i++,s++) {
1241 if (!*s) { /* null never allowed in text */
1248 *s != '\n' && *s != '\r' && *s != '\b' &&
1249 *s != '\t' && *s != '\f' && *s != 27)
1253 if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
1259 static char **Argv = Null(char **);
1260 static char *Cmd = Nullch;
1263 do_aexec(really,arglast)
1267 register STR **st = stack->ary_array;
1268 register int sp = arglast[1];
1269 register int items = arglast[2] - sp;
1274 New(401,Argv, items+1, char*);
1276 for (st += ++sp; items > 0; items--,st++) {
1278 *a++ = str_get(*st);
1284 if (*Argv[0] != '/') /* will execvp use PATH? */
1285 taintenv(); /* testing IFS here is overkill, probably */
1287 if (really && *(tmps = str_get(really)))
1290 execvp(Argv[0],Argv);
1301 Argv = Null(char **);
1317 /* save an extra exec if possible */
1320 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
1336 if (s[-1] == '\'') {
1338 execl(cshname,"csh", flags,ncmd,(char*)0);
1346 /* see if there are shell metacharacters in it */
1349 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1352 for (s = cmd; *s; s++) {
1353 if (*s != ' ' && !isALPHA(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1354 if (*s == '\n' && !s[1]) {
1359 execl("/bin/sh","sh","-c",cmd,(char*)0);
1363 New(402,Argv, (s - cmd) / 2 + 2, char*);
1364 Cmd = nsavestr(cmd, s-cmd);
1366 for (s = Cmd; *s;) {
1367 while (*s && isSPACE(*s)) s++;
1370 while (*s && !isSPACE(*s)) s++;
1376 execvp(Argv[0],Argv);
1377 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1388 do_socket(stab, arglast)
1392 register STR **st = stack->ary_array;
1393 register int sp = arglast[1];
1394 register STIO *stio;
1395 int domain, type, protocol, fd;
1402 stio = stab_io(stab);
1404 stio = stab_io(stab) = stio_new();
1406 do_close(stab,FALSE);
1408 domain = (int)str_gnum(st[++sp]);
1409 type = (int)str_gnum(st[++sp]);
1410 protocol = (int)str_gnum(st[++sp]);
1412 taintproper("Insecure dependency in socket");
1414 fd = socket(domain,type,protocol);
1417 stio->ifp = fdopen(fd, "r"); /* stdio gets confused about sockets */
1418 stio->ofp = fdopen(fd, "w");
1420 if (!stio->ifp || !stio->ofp) {
1421 if (stio->ifp) fclose(stio->ifp);
1422 if (stio->ofp) fclose(stio->ofp);
1423 if (!stio->ifp && !stio->ofp) close(fd);
1431 do_bind(stab, arglast)
1435 register STR **st = stack->ary_array;
1436 register int sp = arglast[1];
1437 register STIO *stio;
1443 stio = stab_io(stab);
1444 if (!stio || !stio->ifp)
1447 addr = str_get(st[++sp]);
1449 taintproper("Insecure dependency in bind");
1451 return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1455 warn("bind() on closed fd");
1462 do_connect(stab, arglast)
1466 register STR **st = stack->ary_array;
1467 register int sp = arglast[1];
1468 register STIO *stio;
1474 stio = stab_io(stab);
1475 if (!stio || !stio->ifp)
1478 addr = str_get(st[++sp]);
1480 taintproper("Insecure dependency in connect");
1482 return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1486 warn("connect() on closed fd");
1493 do_listen(stab, arglast)
1497 register STR **st = stack->ary_array;
1498 register int sp = arglast[1];
1499 register STIO *stio;
1505 stio = stab_io(stab);
1506 if (!stio || !stio->ifp)
1509 backlog = (int)str_gnum(st[++sp]);
1510 return listen(fileno(stio->ifp), backlog) >= 0;
1514 warn("listen() on closed fd");
1520 do_accept(str, nstab, gstab)
1525 register STIO *nstio;
1526 register STIO *gstio;
1527 int len = sizeof buf;
1535 gstio = stab_io(gstab);
1536 nstio = stab_io(nstab);
1538 if (!gstio || !gstio->ifp)
1541 nstio = stab_io(nstab) = stio_new();
1542 else if (nstio->ifp)
1543 do_close(nstab,FALSE);
1545 fd = accept(fileno(gstio->ifp),(struct sockaddr *)buf,&len);
1548 nstio->ifp = fdopen(fd, "r");
1549 nstio->ofp = fdopen(fd, "w");
1551 if (!nstio->ifp || !nstio->ofp) {
1552 if (nstio->ifp) fclose(nstio->ifp);
1553 if (nstio->ofp) fclose(nstio->ofp);
1554 if (!nstio->ifp && !nstio->ofp) close(fd);
1558 str_nset(str, buf, len);
1563 warn("accept() on closed fd");
1566 str_sset(str,&str_undef);
1571 do_shutdown(stab, arglast)
1575 register STR **st = stack->ary_array;
1576 register int sp = arglast[1];
1577 register STIO *stio;
1583 stio = stab_io(stab);
1584 if (!stio || !stio->ifp)
1587 how = (int)str_gnum(st[++sp]);
1588 return shutdown(fileno(stio->ifp), how) >= 0;
1592 warn("shutdown() on closed fd");
1599 do_sopt(optype, stab, arglast)
1604 register STR **st = stack->ary_array;
1605 register int sp = arglast[1];
1606 register STIO *stio;
1609 unsigned int optname;
1614 stio = stab_io(stab);
1615 if (!stio || !stio->ifp)
1618 fd = fileno(stio->ifp);
1619 lvl = (unsigned int)str_gnum(st[sp+1]);
1620 optname = (unsigned int)str_gnum(st[sp+2]);
1623 st[sp] = str_2mortal(Str_new(22,257));
1624 st[sp]->str_cur = 256;
1625 st[sp]->str_pok = 1;
1626 if (getsockopt(fd, lvl, optname, st[sp]->str_ptr,
1627 (int*)&st[sp]->str_cur) < 0)
1632 if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
1642 warn("[gs]etsockopt() on closed fd");
1643 st[sp] = &str_undef;
1650 do_getsockname(optype, stab, arglast)
1655 register STR **st = stack->ary_array;
1656 register int sp = arglast[1];
1657 register STIO *stio;
1663 stio = stab_io(stab);
1664 if (!stio || !stio->ifp)
1667 st[sp] = str_2mortal(Str_new(22,257));
1668 st[sp]->str_cur = 256;
1669 st[sp]->str_pok = 1;
1670 fd = fileno(stio->ifp);
1673 if (getsockname(fd, st[sp]->str_ptr, (int*)&st[sp]->str_cur) < 0)
1677 if (getpeername(fd, st[sp]->str_ptr, (int*)&st[sp]->str_cur) < 0)
1686 warn("get{sock,peer}name() on closed fd");
1689 st[sp] = &str_undef;
1695 do_ghent(which,gimme,arglast)
1700 register ARRAY *ary = stack;
1701 register int sp = arglast[0];
1702 register char **elem;
1704 struct hostent *gethostbyname();
1705 struct hostent *gethostbyaddr();
1706 #ifdef HAS_GETHOSTENT
1707 struct hostent *gethostent();
1709 struct hostent *hent;
1712 if (which == O_GHBYNAME) {
1713 char *name = str_get(ary->ary_array[sp+1]);
1715 hent = gethostbyname(name);
1717 else if (which == O_GHBYADDR) {
1718 STR *addrstr = ary->ary_array[sp+1];
1719 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1720 char *addr = str_get(addrstr);
1722 hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
1725 #ifdef HAS_GETHOSTENT
1726 hent = gethostent();
1728 fatal("gethostent not implemented");
1731 #ifdef HOST_NOT_FOUND
1733 statusvalue = (unsigned short)h_errno & 0xffff;
1736 if (gimme != G_ARRAY) {
1737 astore(ary, ++sp, str = str_mortal(&str_undef));
1739 if (which == O_GHBYNAME) {
1741 str_nset(str, *hent->h_addr, hent->h_length);
1743 str_nset(str, hent->h_addr, hent->h_length);
1747 str_set(str, hent->h_name);
1754 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1755 str_set(str, hent->h_name);
1756 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1757 for (elem = hent->h_aliases; *elem; elem++) {
1758 str_cat(str, *elem);
1760 str_ncat(str," ",1);
1762 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1763 str_numset(str, (double)hent->h_addrtype);
1764 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1765 len = hent->h_length;
1766 str_numset(str, (double)len);
1768 for (elem = hent->h_addr_list; *elem; elem++) {
1769 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1770 str_nset(str, *elem, len);
1773 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1774 str_nset(str, hent->h_addr, len);
1779 (void)astore(ary, ++sp, str_mortal(&str_no));
1787 do_gnent(which,gimme,arglast)
1792 register ARRAY *ary = stack;
1793 register int sp = arglast[0];
1794 register char **elem;
1796 struct netent *getnetbyname();
1797 struct netent *getnetbyaddr();
1798 struct netent *getnetent();
1799 struct netent *nent;
1801 if (which == O_GNBYNAME) {
1802 char *name = str_get(ary->ary_array[sp+1]);
1804 nent = getnetbyname(name);
1806 else if (which == O_GNBYADDR) {
1807 unsigned long addr = U_L(str_gnum(ary->ary_array[sp+1]));
1808 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1810 nent = getnetbyaddr((long)addr,addrtype);
1815 if (gimme != G_ARRAY) {
1816 astore(ary, ++sp, str = str_mortal(&str_undef));
1818 if (which == O_GNBYNAME)
1819 str_numset(str, (double)nent->n_net);
1821 str_set(str, nent->n_name);
1828 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1829 str_set(str, nent->n_name);
1830 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1831 for (elem = nent->n_aliases; *elem; elem++) {
1832 str_cat(str, *elem);
1834 str_ncat(str," ",1);
1836 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1837 str_numset(str, (double)nent->n_addrtype);
1838 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1839 str_numset(str, (double)nent->n_net);
1843 (void)astore(ary, ++sp, str_mortal(&str_no));
1851 do_gpent(which,gimme,arglast)
1856 register ARRAY *ary = stack;
1857 register int sp = arglast[0];
1858 register char **elem;
1860 struct protoent *getprotobyname();
1861 struct protoent *getprotobynumber();
1862 struct protoent *getprotoent();
1863 struct protoent *pent;
1865 if (which == O_GPBYNAME) {
1866 char *name = str_get(ary->ary_array[sp+1]);
1868 pent = getprotobyname(name);
1870 else if (which == O_GPBYNUMBER) {
1871 int proto = (int)str_gnum(ary->ary_array[sp+1]);
1873 pent = getprotobynumber(proto);
1876 pent = getprotoent();
1878 if (gimme != G_ARRAY) {
1879 astore(ary, ++sp, str = str_mortal(&str_undef));
1881 if (which == O_GPBYNAME)
1882 str_numset(str, (double)pent->p_proto);
1884 str_set(str, pent->p_name);
1891 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1892 str_set(str, pent->p_name);
1893 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1894 for (elem = pent->p_aliases; *elem; elem++) {
1895 str_cat(str, *elem);
1897 str_ncat(str," ",1);
1899 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1900 str_numset(str, (double)pent->p_proto);
1904 (void)astore(ary, ++sp, str_mortal(&str_no));
1912 do_gsent(which,gimme,arglast)
1917 register ARRAY *ary = stack;
1918 register int sp = arglast[0];
1919 register char **elem;
1921 struct servent *getservbyname();
1922 struct servent *getservbynumber();
1923 struct servent *getservent();
1924 struct servent *sent;
1926 if (which == O_GSBYNAME) {
1927 char *name = str_get(ary->ary_array[sp+1]);
1928 char *proto = str_get(ary->ary_array[sp+2]);
1930 if (proto && !*proto)
1933 sent = getservbyname(name,proto);
1935 else if (which == O_GSBYPORT) {
1936 int port = (int)str_gnum(ary->ary_array[sp+1]);
1937 char *proto = str_get(ary->ary_array[sp+2]);
1939 sent = getservbyport(port,proto);
1942 sent = getservent();
1944 if (gimme != G_ARRAY) {
1945 astore(ary, ++sp, str = str_mortal(&str_undef));
1947 if (which == O_GSBYNAME) {
1949 str_numset(str, (double)ntohs(sent->s_port));
1951 str_numset(str, (double)(sent->s_port));
1955 str_set(str, sent->s_name);
1962 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1963 str_set(str, sent->s_name);
1964 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1965 for (elem = sent->s_aliases; *elem; elem++) {
1966 str_cat(str, *elem);
1968 str_ncat(str," ",1);
1970 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1972 str_numset(str, (double)ntohs(sent->s_port));
1974 str_numset(str, (double)(sent->s_port));
1976 (void)astore(ary, ++sp, str = str_mortal(&str_no));
1977 str_set(str, sent->s_proto);
1981 (void)astore(ary, ++sp, str_mortal(&str_no));
1988 #endif /* HAS_SOCKET */
1992 do_select(gimme,arglast)
1996 register STR **st = stack->ary_array;
1997 register int sp = arglast[0];
2005 struct timeval timebuf;
2006 struct timeval *tbuf = &timebuf;
2008 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
2014 #if BYTEORDER & 0xf0000
2015 #define ORDERBYTE (0x88888888 - BYTEORDER)
2017 #define ORDERBYTE (0x4444 - BYTEORDER)
2022 for (i = 1; i <= 3; i++) {
2023 j = st[sp+i]->str_cur;
2028 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
2029 growsize = maxlen; /* little endians can use vecs directly */
2037 masksize = NFDBITS / NBBY;
2039 masksize = sizeof(long); /* documented int, everyone seems to use long */
2041 growsize = maxlen + (masksize - (maxlen % masksize));
2042 Zero(&fd_sets[0], 4, char*);
2045 for (i = 1; i <= 3; i++) {
2050 Str_Grow(str,growsize);
2051 s = str_get(str) + j;
2052 while (++j <= growsize) {
2056 else if (str->str_ptr) {
2057 Safefree(str->str_ptr);
2058 str->str_ptr = Nullch;
2061 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
2064 New(403, fd_sets[i], growsize, char);
2065 for (offset = 0; offset < growsize; offset += masksize) {
2066 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
2067 fd_sets[i][j+offset] = s[(k % masksize) + offset];
2073 if (str->str_nok || str->str_pok) {
2074 value = str_gnum(str);
2077 timebuf.tv_sec = (long)value;
2078 value -= (double)timebuf.tv_sec;
2079 timebuf.tv_usec = (long)(value * 1000000.0);
2082 tbuf = Null(struct timeval*);
2084 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
2098 for (i = 1; i <= 3; i++) {
2102 for (offset = 0; offset < growsize; offset += masksize) {
2103 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
2104 s[(k % masksize) + offset] = fd_sets[i][j+offset];
2106 Safefree(fd_sets[i]);
2111 st[++sp] = str_mortal(&str_no);
2112 str_numset(st[sp], (double)nfound);
2113 if (gimme == G_ARRAY && tbuf) {
2114 value = (double)(timebuf.tv_sec) +
2115 (double)(timebuf.tv_usec) / 1000000.0;
2116 st[++sp] = str_mortal(&str_no);
2117 str_numset(st[sp], value);
2125 do_spair(stab1, stab2, arglast)
2130 register STR **st = stack->ary_array;
2131 register int sp = arglast[2];
2132 register STIO *stio1;
2133 register STIO *stio2;
2134 int domain, type, protocol, fd[2];
2136 if (!stab1 || !stab2)
2139 stio1 = stab_io(stab1);
2140 stio2 = stab_io(stab2);
2142 stio1 = stab_io(stab1) = stio_new();
2143 else if (stio1->ifp)
2144 do_close(stab1,FALSE);
2146 stio2 = stab_io(stab2) = stio_new();
2147 else if (stio2->ifp)
2148 do_close(stab2,FALSE);
2150 domain = (int)str_gnum(st[++sp]);
2151 type = (int)str_gnum(st[++sp]);
2152 protocol = (int)str_gnum(st[++sp]);
2154 taintproper("Insecure dependency in socketpair");
2156 #ifdef HAS_SOCKETPAIR
2157 if (socketpair(domain,type,protocol,fd) < 0)
2160 fatal("Socketpair unimplemented");
2162 stio1->ifp = fdopen(fd[0], "r");
2163 stio1->ofp = fdopen(fd[0], "w");
2165 stio2->ifp = fdopen(fd[1], "r");
2166 stio2->ofp = fdopen(fd[1], "w");
2168 if (!stio1->ifp || !stio1->ofp || !stio2->ifp || !stio2->ofp) {
2169 if (stio1->ifp) fclose(stio1->ifp);
2170 if (stio1->ofp) fclose(stio1->ofp);
2171 if (!stio1->ifp && !stio1->ofp) close(fd[0]);
2172 if (stio2->ifp) fclose(stio2->ifp);
2173 if (stio2->ofp) fclose(stio2->ofp);
2174 if (!stio2->ifp && !stio2->ofp) close(fd[1]);
2181 #endif /* HAS_SOCKET */
2184 do_gpwent(which,gimme,arglast)
2190 register ARRAY *ary = stack;
2191 register int sp = arglast[0];
2193 struct passwd *getpwnam();
2194 struct passwd *getpwuid();
2195 struct passwd *getpwent();
2196 struct passwd *pwent;
2198 if (which == O_GPWNAM) {
2199 char *name = str_get(ary->ary_array[sp+1]);
2201 pwent = getpwnam(name);
2203 else if (which == O_GPWUID) {
2204 int uid = (int)str_gnum(ary->ary_array[sp+1]);
2206 pwent = getpwuid(uid);
2211 if (gimme != G_ARRAY) {
2212 astore(ary, ++sp, str = str_mortal(&str_undef));
2214 if (which == O_GPWNAM)
2215 str_numset(str, (double)pwent->pw_uid);
2217 str_set(str, pwent->pw_name);
2223 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2224 str_set(str, pwent->pw_name);
2225 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2226 str_set(str, pwent->pw_passwd);
2227 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2228 str_numset(str, (double)pwent->pw_uid);
2229 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2230 str_numset(str, (double)pwent->pw_gid);
2231 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2233 str_numset(str, (double)pwent->pw_change);
2236 str_numset(str, (double)pwent->pw_quota);
2239 str_set(str, pwent->pw_age);
2243 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2245 str_set(str,pwent->pw_class);
2248 str_set(str, pwent->pw_comment);
2251 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2252 str_set(str, pwent->pw_gecos);
2253 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2254 str_set(str, pwent->pw_dir);
2255 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2256 str_set(str, pwent->pw_shell);
2258 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2259 str_numset(str, (double)pwent->pw_expire);
2265 fatal("password routines not implemented");
2270 do_ggrent(which,gimme,arglast)
2276 register ARRAY *ary = stack;
2277 register int sp = arglast[0];
2278 register char **elem;
2280 struct group *getgrnam();
2281 struct group *getgrgid();
2282 struct group *getgrent();
2283 struct group *grent;
2285 if (which == O_GGRNAM) {
2286 char *name = str_get(ary->ary_array[sp+1]);
2288 grent = getgrnam(name);
2290 else if (which == O_GGRGID) {
2291 int gid = (int)str_gnum(ary->ary_array[sp+1]);
2293 grent = getgrgid(gid);
2298 if (gimme != G_ARRAY) {
2299 astore(ary, ++sp, str = str_mortal(&str_undef));
2301 if (which == O_GGRNAM)
2302 str_numset(str, (double)grent->gr_gid);
2304 str_set(str, grent->gr_name);
2310 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2311 str_set(str, grent->gr_name);
2312 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2313 str_set(str, grent->gr_passwd);
2314 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2315 str_numset(str, (double)grent->gr_gid);
2316 (void)astore(ary, ++sp, str = str_mortal(&str_no));
2317 for (elem = grent->gr_mem; *elem; elem++) {
2318 str_cat(str, *elem);
2320 str_ncat(str," ",1);
2326 fatal("group routines not implemented");
2331 do_dirop(optype,stab,gimme,arglast)
2337 #if defined(DIRENT) && defined(HAS_READDIR)
2338 register ARRAY *ary = stack;
2339 register STR **st = ary->ary_array;
2340 register int sp = arglast[1];
2341 register STIO *stio;
2344 struct DIRENT *readdir();
2346 register struct DIRENT *dp;
2350 if (!(stio = stab_io(stab)))
2351 stio = stab_io(stab) = stio_new();
2352 if (!stio->dirp && optype != O_OPEN_DIR)
2358 closedir(stio->dirp);
2359 if (!(stio->dirp = opendir(str_get(st[sp+1]))))
2363 if (gimme == G_ARRAY) {
2366 while (dp = readdir(stio->dirp)) {
2368 (void)astore(ary,++sp,
2369 str_2mortal(str_make(dp->d_name,dp->d_namlen)));
2371 (void)astore(ary,++sp,
2372 str_2mortal(str_make(dp->d_name,0)));
2377 if (!(dp = readdir(stio->dirp)))
2379 st[sp] = str_mortal(&str_undef);
2381 str_nset(st[sp], dp->d_name, dp->d_namlen);
2383 str_set(st[sp], dp->d_name);
2387 #if defined(HAS_TELLDIR) || defined(telldir)
2392 st[sp] = str_mortal(&str_undef);
2393 str_numset(st[sp], (double)telldir(stio->dirp));
2397 #if defined(HAS_SEEKDIR) || defined(seekdir)
2399 st[sp] = str_mortal(&str_undef);
2400 along = (long)str_gnum(st[sp+1]);
2401 (void)seekdir(stio->dirp,along);
2404 #if defined(HAS_REWINDDIR) || defined(rewinddir)
2406 st[sp] = str_mortal(&str_undef);
2407 (void)rewinddir(stio->dirp);
2411 st[sp] = str_mortal(&str_undef);
2412 (void)closedir(stio->dirp);
2421 st[sp] = &str_undef;
2428 fatal("Unimplemented directory operation");
2436 register STR **st = stack->ary_array;
2437 register int sp = arglast[1];
2438 register int items = arglast[2] - sp;
2441 register int tot = 0;
2445 for (st += ++sp; items--; st++)
2446 tainted |= (*st)->str_tainted;
2447 st = stack->ary_array;
2449 items = arglast[2] - sp;
2454 taintproper("Insecure dependency in chmod");
2458 val = (int)str_gnum(st[++sp]);
2460 if (chmod(str_get(st[++sp]),val))
2468 taintproper("Insecure dependency in chown");
2473 val = (int)str_gnum(st[++sp]);
2474 val2 = (int)str_gnum(st[++sp]);
2476 if (chown(str_get(st[++sp]),val,val2))
2485 taintproper("Insecure dependency in kill");
2489 s = str_get(st[++sp]);
2491 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
2493 if (!(val = whichsig(s)))
2494 fatal("Unrecognized signal name \"%s\"",s);
2497 val = (int)str_gnum(st[sp]);
2501 int proc = (int)str_gnum(st[++sp]);
2503 if (killpg(proc,val)) /* BSD */
2505 if (kill(-proc,val)) /* SYSV */
2512 if (kill((int)(str_gnum(st[++sp])),val))
2521 taintproper("Insecure dependency in unlink");
2525 s = str_get(st[++sp]);
2526 if (euid || unsafe) {
2530 else { /* don't let root wipe out directories without -U */
2532 if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
2534 if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
2546 taintproper("Insecure dependency in utime");
2550 struct utimbuf utbuf;
2558 Zero(&utbuf, sizeof utbuf, char);
2559 utbuf.actime = (long)str_gnum(st[++sp]); /* time accessed */
2560 utbuf.modtime = (long)str_gnum(st[++sp]); /* time modified */
2565 if (utime(str_get(st[++sp]),&utbuf))
2577 /* Do the permissions allow some operation? Assumes statcache already set. */
2580 cando(bit, effective, statbufp)
2583 register struct stat *statbufp;
2586 /* [Comments and code from Len Reed]
2587 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2588 * to write-protected files. The execute permission bit is set
2589 * by the Miscrosoft C library stat() function for the following:
2594 * All files and directories are readable.
2595 * Directories and special files, e.g. "CON", cannot be
2597 * [Comment by Tom Dinger -- a directory can have the write-protect
2598 * bit set in the file system, but DOS permits changes to
2599 * the directory anyway. In addition, all bets are off
2600 * here for networked software, such as Novell and
2604 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2605 * too so it will actually look into the files for magic numbers
2607 return (bit & statbufp->st_mode) ? TRUE : FALSE;
2610 if ((effective ? euid : uid) == 0) { /* root is special */
2611 if (bit == S_IXUSR) {
2612 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
2616 return TRUE; /* root reads and writes anything */
2619 if (statbufp->st_uid == (effective ? euid : uid) ) {
2620 if (statbufp->st_mode & bit)
2621 return TRUE; /* ok as "user" */
2623 else if (ingroup((int)statbufp->st_gid,effective)) {
2624 if (statbufp->st_mode & bit >> 3)
2625 return TRUE; /* ok as "group" */
2627 else if (statbufp->st_mode & bit >> 6)
2628 return TRUE; /* ok as "other" */
2630 #endif /* ! MSDOS */
2634 ingroup(testgid,effective)
2638 if (testgid == (effective ? egid : gid))
2640 #ifdef HAS_GETGROUPS
2645 GROUPSTYPE gary[NGROUPS];
2648 anum = getgroups(NGROUPS,gary);
2650 if (gary[anum] == testgid)
2657 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2660 do_ipcget(optype, arglast)
2664 register STR **st = stack->ary_array;
2665 register int sp = arglast[0];
2669 key = (key_t)str_gnum(st[++sp]);
2670 n = (optype == O_MSGGET) ? 0 : (int)str_gnum(st[++sp]);
2671 flags = (int)str_gnum(st[++sp]);
2677 return msgget(key, flags);
2681 return semget(key, n, flags);
2685 return shmget(key, n, flags);
2687 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2689 fatal("%s not implemented", opname[optype]);
2692 return -1; /* should never happen */
2696 do_ipcctl(optype, arglast)
2700 register STR **st = stack->ary_array;
2701 register int sp = arglast[0];
2704 int id, n, cmd, infosize, getinfo, ret;
2706 id = (int)str_gnum(st[++sp]);
2707 n = (optype == O_SEMCTL) ? (int)str_gnum(st[++sp]) : 0;
2708 cmd = (int)str_gnum(st[++sp]);
2712 getinfo = (cmd == IPC_STAT);
2718 if (cmd == IPC_STAT || cmd == IPC_SET)
2719 infosize = sizeof(struct msqid_ds);
2724 if (cmd == IPC_STAT || cmd == IPC_SET)
2725 infosize = sizeof(struct shmid_ds);
2730 if (cmd == IPC_STAT || cmd == IPC_SET)
2731 infosize = sizeof(struct semid_ds);
2732 else if (cmd == GETALL || cmd == SETALL)
2734 struct semid_ds semds;
2735 if (semctl(id, 0, IPC_STAT, &semds) == -1)
2737 getinfo = (cmd == GETALL);
2738 infosize = semds.sem_nsems * sizeof(short);
2739 /* "short" is technically wrong but much more portable
2740 than guessing about u_?short(_t)? */
2744 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2746 fatal("%s not implemented", opname[optype]);
2754 STR_GROW(astr, infosize+1);
2760 if (astr->str_cur != infosize)
2769 int i = (int)str_gnum(astr);
2770 a = (char *)i; /* ouch */
2777 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2782 ret = semctl(id, n, cmd, a);
2787 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2791 if (getinfo && ret >= 0) {
2792 astr->str_cur = infosize;
2793 astr->str_ptr[infosize] = '\0';
2803 register STR **st = stack->ary_array;
2804 register int sp = arglast[0];
2807 int id, msize, flags;
2809 id = (int)str_gnum(st[++sp]);
2811 flags = (int)str_gnum(st[++sp]);
2812 mbuf = str_get(mstr);
2813 if ((msize = mstr->str_cur - sizeof(long)) < 0) {
2818 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2820 fatal("msgsnd not implemented");
2829 register STR **st = stack->ary_array;
2830 register int sp = arglast[0];
2834 int id, msize, flags, ret;
2836 id = (int)str_gnum(st[++sp]);
2838 msize = (int)str_gnum(st[++sp]);
2839 mtype = (long)str_gnum(st[++sp]);
2840 flags = (int)str_gnum(st[++sp]);
2841 mbuf = str_get(mstr);
2842 if (mstr->str_cur < sizeof(long)+msize+1) {
2843 STR_GROW(mstr, sizeof(long)+msize+1);
2844 mbuf = str_get(mstr);
2847 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2849 mstr->str_cur = sizeof(long)+ret;
2850 mstr->str_ptr[sizeof(long)+ret] = '\0';
2854 fatal("msgrcv not implemented");
2863 register STR **st = stack->ary_array;
2864 register int sp = arglast[0];
2869 id = (int)str_gnum(st[++sp]);
2871 opbuf = str_get(opstr);
2872 opsize = opstr->str_cur;
2873 if (opsize < sizeof(struct sembuf)
2874 || (opsize % sizeof(struct sembuf)) != 0) {
2879 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
2881 fatal("semop not implemented");
2886 do_shmio(optype, arglast)
2891 register STR **st = stack->ary_array;
2892 register int sp = arglast[0];
2895 int id, mpos, msize;
2896 struct shmid_ds shmds;
2898 extern char *shmat();
2901 id = (int)str_gnum(st[++sp]);
2903 mpos = (int)str_gnum(st[++sp]);
2904 msize = (int)str_gnum(st[++sp]);
2906 if (shmctl(id, IPC_STAT, &shmds) == -1)
2908 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2909 errno = EFAULT; /* can't do as caller requested */
2912 shm = (char*)shmat(id, (char*)NULL, (optype == O_SHMREAD) ? SHM_RDONLY : 0);
2913 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2915 mbuf = str_get(mstr);
2916 if (optype == O_SHMREAD) {
2917 if (mstr->str_cur < msize) {
2918 STR_GROW(mstr, msize+1);
2919 mbuf = str_get(mstr);
2921 Copy(shm + mpos, mbuf, msize, char);
2922 mstr->str_cur = msize;
2923 mstr->str_ptr[msize] = '\0';
2928 if ((n = mstr->str_cur) > msize)
2930 Copy(mbuf, shm + mpos, n, char);
2932 memzero(shm + mpos + n, msize - n);
2936 fatal("shm I/O not implemented");
2940 #endif /* SYSV IPC */