1 /* $Header: doio.c,v 3.0.1.4 89/12/21 19:55:10 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.4 89/12/21 19:55:10 lwall
10 * patch7: select now works on big-endian machines
11 * patch7: errno may now be a macro with an lvalue
12 * patch7: ANSI strerror() is now supported
13 * patch7: Configure now detects DG/UX thingies like [sg]etpgrp2 and utime.h
15 * Revision 3.0.1.3 89/11/17 15:13:06 lwall
16 * patch5: some systems have symlink() but not lstat()
17 * patch5: some systems have dirent.h but not readdir()
19 * Revision 3.0.1.2 89/11/11 04:25:51 lwall
20 * patch2: orthogonalized the file modes some so we can have <& +<& etc.
21 * patch2: do_open() now detects sockets passed to process from parent
22 * patch2: fd's above 2 are now closed on exec
23 * patch2: csh code can now use csh from other than /bin
24 * patch2: getsockopt, get{sock,peer}name didn't define result properly
25 * patch2: warn("shutdown") was replicated
26 * patch2: gethostbyname was misdeclared
27 * patch2: telldir() is sometimes a macro
29 * Revision 3.0.1.1 89/10/26 23:10:05 lwall
30 * patch1: Configure now checks for BSD shadow passwords
32 * Revision 3.0 89/10/18 15:10:54 lwall
41 #include <sys/socket.h>
61 int len = strlen(name);
62 register STIO *stio = stab_io(stab);
63 char *myname = savestr(name);
67 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
70 forkprocess = 1; /* assume true if no fork */
71 while (len && isspace(name[len-1]))
74 stio = stab_io(stab) = stio_new();
76 fd = fileno(stio->ifp);
77 if (stio->type == '|')
78 result = mypclose(stio->ifp);
79 else if (stio->ifp != stio->ofp) {
82 result = fclose(stio->ifp);
84 else if (stio->type != '-')
85 result = fclose(stio->ifp);
88 if (result == EOF && fd > 2)
89 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
91 stio->ofp = stio->ifp = Nullfp;
93 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
104 for (name++; isspace(*name); name++) ;
107 taintproper("Insecure dependency in piped open");
109 fp = mypopen(name,"w");
112 else if (*name == '>') {
114 taintproper("Insecure dependency in open");
118 mode[0] = stio->type = 'a';
127 while (isspace(*name))
132 stab = stabent(name,FALSE);
133 if (!stab || !stab_io(stab))
135 if (stab_io(stab) && stab_io(stab)->ifp) {
136 fd = fileno(stab_io(stab)->ifp);
137 if (stab_io(stab)->type == 's')
143 fp = fdopen(dup(fd),mode);
146 while (isspace(*name))
148 if (strEQ(name,"-")) {
153 fp = fopen(name,mode);
161 while (isspace(*name))
165 if (strEQ(name,"-")) {
170 fp = fopen(name,mode);
172 else if (name[len-1] == '|') {
175 taintproper("Insecure dependency in piped open");
178 while (len && isspace(name[len-1]))
180 for (; isspace(*name); name++) ;
181 fp = mypopen(name,"r");
186 for (; isspace(*name); name++) ;
187 if (strEQ(name,"-")) {
192 fp = fopen(name,"r");
199 stio->type != '|' && stio->type != '-') {
200 if (fstat(fileno(fp),&statbuf) < 0) {
204 result = (statbuf.st_mode & S_IFMT);
205 if (result != S_IFREG &&
207 result != S_IFSOCK &&
210 result != S_IFFIFO &&
215 result != 0 && /* socket? */
221 if (result == S_IFSOCK || result == 0)
222 stio->type = 's'; /* in case a socket was passed in to us */
225 #if defined(FCNTL) && defined(F_SETFD)
232 if (stio->type != 's')
235 stio->ofp = fdopen(fileno(fp),"w");
246 int filemode,fileuid,filegid;
248 while (alen(stab_xarray(stab)) >= 0) {
249 str = ashift(stab_xarray(stab));
250 str_sset(stab_val(stab),str);
251 STABSET(stab_val(stab));
252 oldname = str_get(stab_val(stab));
253 if (do_open(stab,oldname)) {
256 taintproper("Insecure dependency in inplace open");
258 filemode = statbuf.st_mode;
259 fileuid = statbuf.st_uid;
260 filegid = statbuf.st_gid;
262 str_cat(str,inplace);
264 (void)rename(oldname,str->str_ptr);
266 (void)UNLINK(str->str_ptr);
267 (void)link(oldname,str->str_ptr);
268 (void)UNLINK(oldname);
272 (void)UNLINK(oldname);
276 str_cat(str,oldname);
277 errno = 0; /* in case sprintf set errno */
278 if (!do_open(argvoutstab,str->str_ptr))
279 fatal("Can't do inplace edit");
280 defoutstab = argvoutstab;
282 (void)fchmod(fileno(stab_io(argvoutstab)->ifp),filemode);
284 (void)chmod(oldname,filemode);
287 (void)fchown(fileno(stab_io(argvoutstab)->ifp),fileuid,filegid);
289 (void)chown(oldname,fileuid,filegid);
293 return stab_io(stab)->ifp;
296 fprintf(stderr,"Can't open %s\n",str_get(str));
300 (void)do_close(argvoutstab,FALSE);
301 defoutstab = stabent("STDOUT",TRUE);
307 do_close(stab,explicit)
312 register STIO *stio = stab_io(stab);
315 if (!stio) { /* never opened */
316 if (dowarn && explicit)
317 warn("Close on unopened file <%s>",stab_name(stab));
321 if (stio->type == '|') {
322 status = mypclose(stio->ifp);
323 retval = (status >= 0);
324 statusvalue = (unsigned)status & 0xffff;
326 else if (stio->type == '-')
329 if (stio->ofp && stio->ofp != stio->ifp) /* a socket */
331 retval = (fclose(stio->ifp) != EOF);
333 stio->ofp = stio->ifp = Nullfp;
348 if (!stab) { /* eof() */
350 stio = stab_io(argvstab);
355 stio = stab_io(stab);
362 #ifdef STDSTDIO /* (the code works without this) */
363 if (stio->ifp->_cnt > 0) /* cheat a little, since */
364 return FALSE; /* this is the most usual case */
367 ch = getc(stio->ifp);
369 (void)ungetc(ch, stio->ifp);
372 if (!stab) { /* not necessarily a real EOF yet? */
373 if (!nextargv(argvstab)) /* get another fp handy */
377 return TRUE; /* normal fp, definitely end of file */
391 stio = stab_io(stab);
392 if (!stio || !stio->ifp)
396 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
398 return ftell(stio->ifp);
402 warn("tell() on unopened file");
407 do_seek(stab, pos, whence)
417 stio = stab_io(stab);
418 if (!stio || !stio->ifp)
422 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
424 return fseek(stio->ifp, pos, whence) >= 0;
428 warn("seek() on unopened file");
433 do_ctl(optype,stab,func,argstr)
443 if (!stab || !argstr)
445 stio = stab_io(stab);
449 if (argstr->str_pok || !argstr->str_nok) {
450 if (!argstr->str_pok)
455 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
459 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
461 retval = 256; /* otherwise guess at what's safe */
463 if (argstr->str_cur < retval) {
464 str_grow(argstr,retval+1);
465 argstr->str_cur = retval;
469 s[argstr->str_cur] = 17; /* a little sanity check here */
472 retval = (int)str_gnum(argstr);
473 s = (char*)retval; /* ouch */
477 if (optype == O_IOCTL)
478 retval = ioctl(fileno(stio->ifp), func, s);
481 retval = fcntl(fileno(stio->ifp), func, s);
483 fatal("fcntl is not implemented");
489 if (argstr->str_pok) {
490 if (s[argstr->str_cur] != 17)
491 fatal("Return value overflowed string");
492 s[argstr->str_cur] = 0; /* put our null back */
498 do_stat(str,arg,gimme,arglast)
504 register ARRAY *ary = stack;
505 register int sp = arglast[0] + 1;
509 if ((arg[1].arg_type & A_MASK) == A_WORD) {
510 tmpstab = arg[1].arg_ptr.arg_stab;
511 if (tmpstab != defstab) {
513 str_set(statname,"");
514 if (!stab_io(tmpstab) ||
515 fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
521 str_sset(statname,ary->ary_array[sp]);
524 if (arg->arg_type == O_LSTAT)
525 i = lstat(str_get(statname),&statcache);
528 i = stat(str_get(statname),&statcache);
533 if (gimme != G_ARRAY) {
535 str_sset(str,&str_yes);
537 str_sset(str,&str_undef);
539 ary->ary_array[sp] = str;
545 (void)astore(ary,++sp,
546 str_2static(str_nmake((double)statcache.st_dev)));
547 (void)astore(ary,++sp,
548 str_2static(str_nmake((double)statcache.st_ino)));
549 (void)astore(ary,++sp,
550 str_2static(str_nmake((double)statcache.st_mode)));
551 (void)astore(ary,++sp,
552 str_2static(str_nmake((double)statcache.st_nlink)));
553 (void)astore(ary,++sp,
554 str_2static(str_nmake((double)statcache.st_uid)));
555 (void)astore(ary,++sp,
556 str_2static(str_nmake((double)statcache.st_gid)));
557 (void)astore(ary,++sp,
558 str_2static(str_nmake((double)statcache.st_rdev)));
559 (void)astore(ary,++sp,
560 str_2static(str_nmake((double)statcache.st_size)));
561 (void)astore(ary,++sp,
562 str_2static(str_nmake((double)statcache.st_atime)));
563 (void)astore(ary,++sp,
564 str_2static(str_nmake((double)statcache.st_mtime)));
565 (void)astore(ary,++sp,
566 str_2static(str_nmake((double)statcache.st_ctime)));
568 (void)astore(ary,++sp,
569 str_2static(str_nmake((double)statcache.st_blksize)));
570 (void)astore(ary,++sp,
571 str_2static(str_nmake((double)statcache.st_blocks)));
573 (void)astore(ary,++sp,
574 str_2static(str_make("",0)));
575 (void)astore(ary,++sp,
576 str_2static(str_make("",0)));
579 (void)astore(ary,++sp,str_nmake(0.0));
586 looks_like_number(str)
595 send = s + str->str_cur;
600 if (*s == '+' || *s == '-')
608 else if (s == str->str_ptr)
614 if (*s == 'e' || *s == 'E') {
616 if (*s == '+' || *s == '-')
637 warn("print to unopened file");
643 ((str->str_nok && str->str_u.str_nval != 0.0)
644 || (looks_like_number(str) && str_gnum(str) != 0.0) ) )
645 fprintf(fp, ofmt, str->str_u.str_nval);
648 if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'a' && tmps[3] == 'b'
649 && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
650 tmps = stab_name(((STAB*)str)); /* a stab value, be nice */
651 str = ((STAB*)str)->str_magic;
654 if (str->str_cur && fwrite(tmps,1,str->str_cur,fp) == 0)
661 do_aprint(arg,fp,arglast)
666 register STR **st = stack->ary_array;
667 register int sp = arglast[1];
669 register int items = arglast[2] - sp;
673 warn("print to unopened file");
677 if (arg->arg_type == O_PRTF) {
678 do_sprintf(arg->arg_ptr.arg_str,items,st);
679 retval = do_print(arg->arg_ptr.arg_str,fp);
682 retval = (items <= 0);
683 for (; items > 0; items--,st++) {
684 if (retval && ofslen) {
685 if (fwrite(ofs, 1, ofslen, fp) == 0) {
690 if (!(retval = do_print(*st, fp)))
693 if (retval && orslen)
694 if (fwrite(ors, 1, orslen, fp) == 0)
707 if (arg[1].arg_type & A_DONT) {
708 stio = stab_io(arg[1].arg_ptr.arg_stab);
709 if (stio && stio->ifp) {
710 statstab = arg[1].arg_ptr.arg_stab;
711 str_set(statname,"");
712 return fstat(fileno(stio->ifp), &statcache);
715 if (arg[1].arg_ptr.arg_stab == defstab)
718 warn("Stat on unopened file <%s>",
719 stab_name(arg[1].arg_ptr.arg_stab));
721 str_set(statname,"");
727 str_sset(statname,str);
728 return stat(str_get(str),&statcache);
744 if (arg[1].arg_type & A_DONT) {
745 if (arg[1].arg_ptr.arg_stab == defstab) {
747 stio = stab_io(statstab);
750 goto really_filename;
754 statstab = arg[1].arg_ptr.arg_stab;
755 str_set(statname,"");
756 stio = stab_io(statstab);
758 if (stio && stio->ifp) {
760 fstat(fileno(stio->ifp),&statcache);
761 if (stio->ifp->_cnt <= 0) {
764 (void)ungetc(i,stio->ifp);
766 if (stio->ifp->_cnt <= 0) /* null file is anything */
768 len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
769 s = stio->ifp->_base;
771 fatal("-T and -B not implemented on filehandles\n");
776 warn("Test on unopened file <%s>",
777 stab_name(arg[1].arg_ptr.arg_stab));
783 str_sset(statname,str);
785 i = open(str_get(str),0);
789 len = read(i,tbuf,512);
790 if (len <= 0) /* null file is anything */
796 /* now scan s to look for textiness */
798 for (i = 0; i < len; i++,s++) {
799 if (!*s) { /* null never allowed in text */
806 *s != '\n' && *s != '\r' && *s != '\b' &&
807 *s != '\t' && *s != '\f' && *s != 27)
811 if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
818 do_aexec(really,arglast)
822 register STR **st = stack->ary_array;
823 register int sp = arglast[1];
824 register int items = arglast[2] - sp;
830 New(401,argv, items+1, char*);
832 for (st += ++sp; items > 0; items--,st++) {
840 if (*argv[0] != '/') /* will execvp use PATH? */
841 taintenv(); /* testing IFS here is overkill, probably */
843 if (really && *(tmps = str_get(really)))
846 execvp(argv[0],argv);
863 taintproper("Insecure dependency in exec");
866 /* save an extra exec if possible */
869 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
887 execl(cshname,"csh", flags,ncmd,(char*)0);
895 /* see if there are shell metacharacters in it */
897 for (s = cmd; *s; s++) {
898 if (*s != ' ' && !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
899 if (*s == '\n' && !s[1]) {
904 execl("/bin/sh","sh","-c",cmd,(char*)0);
908 New(402,argv, (s - cmd) / 2 + 2, char*);
912 while (*s && isspace(*s)) s++;
915 while (*s && !isspace(*s)) s++;
921 execvp(argv[0],argv);
922 if (errno == ENOEXEC) /* for system V NIH syndrome */
931 do_socket(stab, arglast)
935 register STR **st = stack->ary_array;
936 register int sp = arglast[1];
938 int domain, type, protocol, fd;
943 stio = stab_io(stab);
945 stio = stab_io(stab) = stio_new();
947 do_close(stab,FALSE);
949 domain = (int)str_gnum(st[++sp]);
950 type = (int)str_gnum(st[++sp]);
951 protocol = (int)str_gnum(st[++sp]);
953 taintproper("Insecure dependency in socket");
955 fd = socket(domain,type,protocol);
958 stio->ifp = fdopen(fd, "r"); /* stdio gets confused about sockets */
959 stio->ofp = fdopen(fd, "w");
966 do_bind(stab, arglast)
970 register STR **st = stack->ary_array;
971 register int sp = arglast[1];
978 stio = stab_io(stab);
979 if (!stio || !stio->ifp)
982 addr = str_get(st[++sp]);
984 taintproper("Insecure dependency in bind");
986 return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
990 warn("bind() on closed fd");
996 do_connect(stab, arglast)
1000 register STR **st = stack->ary_array;
1001 register int sp = arglast[1];
1002 register STIO *stio;
1008 stio = stab_io(stab);
1009 if (!stio || !stio->ifp)
1012 addr = str_get(st[++sp]);
1014 taintproper("Insecure dependency in connect");
1016 return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1020 warn("connect() on closed fd");
1026 do_listen(stab, arglast)
1030 register STR **st = stack->ary_array;
1031 register int sp = arglast[1];
1032 register STIO *stio;
1038 stio = stab_io(stab);
1039 if (!stio || !stio->ifp)
1042 backlog = (int)str_gnum(st[++sp]);
1043 return listen(fileno(stio->ifp), backlog) >= 0;
1047 warn("listen() on closed fd");
1052 do_accept(str, nstab, gstab)
1057 register STIO *nstio;
1058 register STIO *gstio;
1059 int len = sizeof buf;
1067 gstio = stab_io(gstab);
1068 nstio = stab_io(nstab);
1070 if (!gstio || !gstio->ifp)
1073 nstio = stab_io(nstab) = stio_new();
1074 else if (nstio->ifp)
1075 do_close(nstab,FALSE);
1077 fd = accept(fileno(gstio->ifp),buf,&len);
1080 nstio->ifp = fdopen(fd, "r");
1081 nstio->ofp = fdopen(fd, "w");
1084 str_nset(str, buf, len);
1089 warn("accept() on closed fd");
1091 str_sset(str,&str_undef);
1096 do_shutdown(stab, arglast)
1100 register STR **st = stack->ary_array;
1101 register int sp = arglast[1];
1102 register STIO *stio;
1108 stio = stab_io(stab);
1109 if (!stio || !stio->ifp)
1112 how = (int)str_gnum(st[++sp]);
1113 return shutdown(fileno(stio->ifp), how) >= 0;
1117 warn("shutdown() on closed fd");
1123 do_sopt(optype, stab, arglast)
1128 register STR **st = stack->ary_array;
1129 register int sp = arglast[1];
1130 register STIO *stio;
1138 stio = stab_io(stab);
1139 if (!stio || !stio->ifp)
1142 fd = fileno(stio->ifp);
1143 lvl = (int)str_gnum(st[sp+1]);
1144 optname = (int)str_gnum(st[sp+2]);
1147 st[sp] = str_2static(str_new(257));
1148 st[sp]->str_cur = 256;
1149 st[sp]->str_pok = 1;
1150 if (getsockopt(fd, lvl, optname, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1155 if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
1165 warn("[gs]etsockopt() on closed fd");
1166 st[sp] = &str_undef;
1172 do_getsockname(optype, stab, arglast)
1177 register STR **st = stack->ary_array;
1178 register int sp = arglast[1];
1179 register STIO *stio;
1185 stio = stab_io(stab);
1186 if (!stio || !stio->ifp)
1189 st[sp] = str_2static(str_new(257));
1190 st[sp]->str_cur = 256;
1191 st[sp]->str_pok = 1;
1192 fd = fileno(stio->ifp);
1195 if (getsockname(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1199 if (getpeername(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1208 warn("get{sock,peer}name() on closed fd");
1209 st[sp] = &str_undef;
1215 do_ghent(which,gimme,arglast)
1220 register ARRAY *ary = stack;
1221 register int sp = arglast[0];
1222 register char **elem;
1224 struct hostent *gethostbyname();
1225 struct hostent *gethostbyaddr();
1227 struct hostent *gethostent();
1229 struct hostent *hent;
1232 if (gimme != G_ARRAY) {
1233 astore(ary, ++sp, str_static(&str_undef));
1237 if (which == O_GHBYNAME) {
1238 char *name = str_get(ary->ary_array[sp+1]);
1240 hent = gethostbyname(name);
1242 else if (which == O_GHBYADDR) {
1243 STR *addrstr = ary->ary_array[sp+1];
1244 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1245 char *addr = str_get(addrstr);
1247 hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
1251 hent = gethostent();
1253 fatal("gethostent not implemented");
1257 (void)astore(ary, ++sp, str = str_static(&str_no));
1258 str_set(str, hent->h_name);
1259 (void)astore(ary, ++sp, str = str_static(&str_no));
1260 for (elem = hent->h_aliases; *elem; elem++) {
1261 str_cat(str, *elem);
1263 str_ncat(str," ",1);
1265 (void)astore(ary, ++sp, str = str_static(&str_no));
1266 str_numset(str, (double)hent->h_addrtype);
1267 (void)astore(ary, ++sp, str = str_static(&str_no));
1268 len = hent->h_length;
1269 str_numset(str, (double)len);
1271 for (elem = hent->h_addr_list; *elem; elem++) {
1272 (void)astore(ary, ++sp, str = str_static(&str_no));
1273 str_nset(str, *elem, len);
1276 (void)astore(ary, ++sp, str = str_static(&str_no));
1277 str_nset(str, hent->h_addr, len);
1282 (void)astore(ary, ++sp, str_static(&str_no));
1290 do_gnent(which,gimme,arglast)
1295 register ARRAY *ary = stack;
1296 register int sp = arglast[0];
1297 register char **elem;
1299 struct netent *getnetbyname();
1300 struct netent *getnetbyaddr();
1301 struct netent *getnetent();
1302 struct netent *nent;
1304 if (gimme != G_ARRAY) {
1305 astore(ary, ++sp, str_static(&str_undef));
1309 if (which == O_GNBYNAME) {
1310 char *name = str_get(ary->ary_array[sp+1]);
1312 nent = getnetbyname(name);
1314 else if (which == O_GNBYADDR) {
1315 STR *addrstr = ary->ary_array[sp+1];
1316 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1317 char *addr = str_get(addrstr);
1319 nent = getnetbyaddr(addr,addrtype);
1326 (void)astore(ary, ++sp, str = str_static(&str_no));
1327 str_set(str, nent->n_name);
1328 (void)astore(ary, ++sp, str = str_static(&str_no));
1329 for (elem = nent->n_aliases; *elem; elem++) {
1330 str_cat(str, *elem);
1332 str_ncat(str," ",1);
1334 (void)astore(ary, ++sp, str = str_static(&str_no));
1335 str_numset(str, (double)nent->n_addrtype);
1336 (void)astore(ary, ++sp, str = str_static(&str_no));
1337 str_numset(str, (double)nent->n_net);
1341 (void)astore(ary, ++sp, str_static(&str_no));
1349 do_gpent(which,gimme,arglast)
1354 register ARRAY *ary = stack;
1355 register int sp = arglast[0];
1356 register char **elem;
1358 struct protoent *getprotobyname();
1359 struct protoent *getprotobynumber();
1360 struct protoent *getprotoent();
1361 struct protoent *pent;
1363 if (gimme != G_ARRAY) {
1364 astore(ary, ++sp, str_static(&str_undef));
1368 if (which == O_GPBYNAME) {
1369 char *name = str_get(ary->ary_array[sp+1]);
1371 pent = getprotobyname(name);
1373 else if (which == O_GPBYNUMBER) {
1374 int proto = (int)str_gnum(ary->ary_array[sp+1]);
1376 pent = getprotobynumber(proto);
1379 pent = getprotoent();
1383 (void)astore(ary, ++sp, str = str_static(&str_no));
1384 str_set(str, pent->p_name);
1385 (void)astore(ary, ++sp, str = str_static(&str_no));
1386 for (elem = pent->p_aliases; *elem; elem++) {
1387 str_cat(str, *elem);
1389 str_ncat(str," ",1);
1391 (void)astore(ary, ++sp, str = str_static(&str_no));
1392 str_numset(str, (double)pent->p_proto);
1396 (void)astore(ary, ++sp, str_static(&str_no));
1404 do_gsent(which,gimme,arglast)
1409 register ARRAY *ary = stack;
1410 register int sp = arglast[0];
1411 register char **elem;
1413 struct servent *getservbyname();
1414 struct servent *getservbynumber();
1415 struct servent *getservent();
1416 struct servent *sent;
1418 if (gimme != G_ARRAY) {
1419 astore(ary, ++sp, str_static(&str_undef));
1423 if (which == O_GSBYNAME) {
1424 char *name = str_get(ary->ary_array[sp+1]);
1425 char *proto = str_get(ary->ary_array[sp+2]);
1427 if (proto && !*proto)
1430 sent = getservbyname(name,proto);
1432 else if (which == O_GSBYPORT) {
1433 int port = (int)str_gnum(ary->ary_array[sp+1]);
1434 char *proto = str_get(ary->ary_array[sp+2]);
1436 sent = getservbyport(port,proto);
1439 sent = getservent();
1442 (void)astore(ary, ++sp, str = str_static(&str_no));
1443 str_set(str, sent->s_name);
1444 (void)astore(ary, ++sp, str = str_static(&str_no));
1445 for (elem = sent->s_aliases; *elem; elem++) {
1446 str_cat(str, *elem);
1448 str_ncat(str," ",1);
1450 (void)astore(ary, ++sp, str = str_static(&str_no));
1452 str_numset(str, (double)ntohs(sent->s_port));
1454 str_numset(str, (double)(sent->s_port));
1456 (void)astore(ary, ++sp, str = str_static(&str_no));
1457 str_set(str, sent->s_proto);
1461 (void)astore(ary, ++sp, str_static(&str_no));
1469 do_select(gimme,arglast)
1473 register STR **st = stack->ary_array;
1474 register int sp = arglast[0];
1482 struct timeval timebuf;
1483 struct timeval *tbuf = &timebuf;
1485 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1491 #if BYTEORDER & 0xf0000
1492 #define ORDERBYTE (0x88888888 - BYTEORDER)
1494 #define ORDERBYTE (0x4444 - BYTEORDER)
1499 for (i = 1; i <= 3; i++) {
1500 j = st[sp+i]->str_cur;
1505 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1506 growsize = maxlen; /* little endians can use vecs directly */
1514 masksize = NFDBITS / NBBY;
1516 masksize = sizeof(long); /* documented int, everyone seems to use long */
1518 growsize = maxlen + (masksize - (maxlen % masksize));
1519 Zero(&fd_sets[0], 4, char*);
1522 for (i = 1; i <= 3; i++) {
1527 str_grow(str,growsize);
1528 s = str_get(str) + j;
1529 while (++j <= growsize) {
1533 else if (str->str_ptr) {
1534 Safefree(str->str_ptr);
1535 str->str_ptr = Nullch;
1538 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1541 New(403, fd_sets[i], growsize, char);
1542 for (offset = 0; offset < growsize; offset += masksize) {
1543 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1544 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1550 if (str->str_nok || str->str_pok) {
1551 value = str_gnum(str);
1554 timebuf.tv_sec = (long)value;
1555 value -= (double)timebuf.tv_sec;
1556 timebuf.tv_usec = (long)(value * 1000000.0);
1559 tbuf = Null(struct timeval*);
1561 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1575 for (i = 1; i <= 3; i++) {
1579 for (offset = 0; offset < growsize; offset += masksize) {
1580 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1581 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1587 st[++sp] = str_static(&str_no);
1588 str_numset(st[sp], (double)nfound);
1589 if (gimme == G_ARRAY && tbuf) {
1590 value = (double)(timebuf.tv_sec) +
1591 (double)(timebuf.tv_usec) / 1000000.0;
1592 st[++sp] = str_static(&str_no);
1593 str_numset(st[sp], value);
1599 do_spair(stab1, stab2, arglast)
1604 register STR **st = stack->ary_array;
1605 register int sp = arglast[2];
1606 register STIO *stio1;
1607 register STIO *stio2;
1608 int domain, type, protocol, fd[2];
1610 if (!stab1 || !stab2)
1613 stio1 = stab_io(stab1);
1614 stio2 = stab_io(stab2);
1616 stio1 = stab_io(stab1) = stio_new();
1617 else if (stio1->ifp)
1618 do_close(stab1,FALSE);
1620 stio2 = stab_io(stab2) = stio_new();
1621 else if (stio2->ifp)
1622 do_close(stab2,FALSE);
1624 domain = (int)str_gnum(st[++sp]);
1625 type = (int)str_gnum(st[++sp]);
1626 protocol = (int)str_gnum(st[++sp]);
1628 taintproper("Insecure dependency in socketpair");
1631 if (socketpair(domain,type,protocol,fd) < 0)
1634 fatal("Socketpair unimplemented");
1636 stio1->ifp = fdopen(fd[0], "r");
1637 stio1->ofp = fdopen(fd[0], "w");
1639 stio2->ifp = fdopen(fd[1], "r");
1640 stio2->ofp = fdopen(fd[1], "w");
1649 do_gpwent(which,gimme,arglast)
1655 register ARRAY *ary = stack;
1656 register int sp = arglast[0];
1657 register char **elem;
1659 struct passwd *getpwnam();
1660 struct passwd *getpwuid();
1661 struct passwd *getpwent();
1662 struct passwd *pwent;
1665 if (gimme != G_ARRAY) {
1666 astore(ary, ++sp, str_static(&str_undef));
1670 if (which == O_GPWNAM) {
1671 char *name = str_get(ary->ary_array[sp+1]);
1673 pwent = getpwnam(name);
1675 else if (which == O_GPWUID) {
1676 int uid = (int)str_gnum(ary->ary_array[sp+1]);
1678 pwent = getpwuid(uid);
1684 (void)astore(ary, ++sp, str = str_static(&str_no));
1685 str_set(str, pwent->pw_name);
1686 (void)astore(ary, ++sp, str = str_static(&str_no));
1687 str_set(str, pwent->pw_passwd);
1688 (void)astore(ary, ++sp, str = str_static(&str_no));
1689 str_numset(str, (double)pwent->pw_uid);
1690 (void)astore(ary, ++sp, str = str_static(&str_no));
1691 str_numset(str, (double)pwent->pw_gid);
1692 (void)astore(ary, ++sp, str = str_static(&str_no));
1694 str_numset(str, (double)pwent->pw_change);
1697 str_numset(str, (double)pwent->pw_quota);
1700 str_set(str, pwent->pw_age);
1704 (void)astore(ary, ++sp, str = str_static(&str_no));
1706 str_set(str,pwent->pw_class);
1708 str_set(str, pwent->pw_comment);
1710 (void)astore(ary, ++sp, str = str_static(&str_no));
1711 str_set(str, pwent->pw_gecos);
1712 (void)astore(ary, ++sp, str = str_static(&str_no));
1713 str_set(str, pwent->pw_dir);
1714 (void)astore(ary, ++sp, str = str_static(&str_no));
1715 str_set(str, pwent->pw_shell);
1717 (void)astore(ary, ++sp, str = str_static(&str_no));
1718 str_numset(str, (double)pwent->pw_expire);
1724 fatal("password routines not implemented");
1729 do_ggrent(which,gimme,arglast)
1735 register ARRAY *ary = stack;
1736 register int sp = arglast[0];
1737 register char **elem;
1739 struct group *getgrnam();
1740 struct group *getgrgid();
1741 struct group *getgrent();
1742 struct group *grent;
1745 if (gimme != G_ARRAY) {
1746 astore(ary, ++sp, str_static(&str_undef));
1750 if (which == O_GGRNAM) {
1751 char *name = str_get(ary->ary_array[sp+1]);
1753 grent = getgrnam(name);
1755 else if (which == O_GGRGID) {
1756 int gid = (int)str_gnum(ary->ary_array[sp+1]);
1758 grent = getgrgid(gid);
1764 (void)astore(ary, ++sp, str = str_static(&str_no));
1765 str_set(str, grent->gr_name);
1766 (void)astore(ary, ++sp, str = str_static(&str_no));
1767 str_set(str, grent->gr_passwd);
1768 (void)astore(ary, ++sp, str = str_static(&str_no));
1769 str_numset(str, (double)grent->gr_gid);
1770 (void)astore(ary, ++sp, str = str_static(&str_no));
1771 for (elem = grent->gr_mem; *elem; elem++) {
1772 str_cat(str, *elem);
1774 str_ncat(str," ",1);
1780 fatal("group routines not implemented");
1785 do_dirop(optype,stab,gimme,arglast)
1791 #if defined(DIRENT) && defined(READDIR)
1792 register ARRAY *ary = stack;
1793 register STR **st = ary->ary_array;
1794 register int sp = arglast[1];
1795 register STIO *stio;
1800 struct DIRENT *readdir();
1801 register struct DIRENT *dp;
1805 if (!(stio = stab_io(stab)))
1806 stio = stab_io(stab) = stio_new();
1807 if (!stio->dirp && optype != O_OPENDIR)
1813 closedir(stio->dirp);
1814 if (!(stio->dirp = opendir(str_get(st[sp+1]))))
1818 if (gimme == G_ARRAY) {
1820 while (dp = readdir(stio->dirp)) {
1822 (void)astore(ary,++sp,
1823 str_2static(str_make(dp->d_name,dp->d_namlen)));
1825 (void)astore(ary,++sp,
1826 str_2static(str_make(dp->d_name,0)));
1831 if (!(dp = readdir(stio->dirp)))
1833 st[sp] = str_static(&str_undef);
1835 str_nset(st[sp], dp->d_name, dp->d_namlen);
1837 str_set(st[sp], dp->d_name);
1842 st[sp] = str_static(&str_undef);
1843 str_numset(st[sp], (double)telldir(stio->dirp));
1846 st[sp] = str_static(&str_undef);
1847 along = (long)str_gnum(st[sp+1]);
1848 (void)seekdir(stio->dirp,along);
1851 st[sp] = str_static(&str_undef);
1852 (void)rewinddir(stio->dirp);
1855 st[sp] = str_static(&str_undef);
1856 (void)closedir(stio->dirp);
1863 st[sp] = &str_undef;
1867 fatal("Unimplemented directory operation");
1875 register STR **st = stack->ary_array;
1876 register int sp = arglast[1];
1877 register int items = arglast[2] - sp;
1880 register int tot = 0;
1884 for (st += ++sp; items--; st++)
1885 tainted |= (*st)->str_tainted;
1886 st = stack->ary_array;
1888 items = arglast[2] - sp;
1893 taintproper("Insecure dependency in chmod");
1897 val = (int)str_gnum(st[++sp]);
1899 if (chmod(str_get(st[++sp]),val))
1906 taintproper("Insecure dependency in chown");
1911 val = (int)str_gnum(st[++sp]);
1912 val2 = (int)str_gnum(st[++sp]);
1914 if (chown(str_get(st[++sp]),val,val2))
1921 taintproper("Insecure dependency in kill");
1925 s = str_get(st[++sp]);
1927 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1929 if (!(val = whichsig(s)))
1930 fatal("Unrecognized signal name \"%s\"",s);
1933 val = (int)str_gnum(st[sp]);
1937 int proc = (int)str_gnum(st[++sp]);
1939 if (killpg(proc,val)) /* BSD */
1941 if (kill(-proc,val)) /* SYSV */
1948 if (kill((int)(str_gnum(st[++sp])),val))
1956 taintproper("Insecure dependency in unlink");
1960 s = str_get(st[++sp]);
1961 if (euid || unsafe) {
1965 else { /* don't let root wipe out directories without -U */
1967 if (lstat(s,&statbuf) < 0 ||
1969 if (stat(s,&statbuf) < 0 ||
1971 (statbuf.st_mode & S_IFMT) == S_IFDIR )
1982 taintproper("Insecure dependency in utime");
1986 struct utimbuf utbuf;
1994 utbuf.actime = (long)str_gnum(st[++sp]); /* time accessed */
1995 utbuf.modtime = (long)str_gnum(st[++sp]); /* time modified */
1997 utbuf.acusec = 0; /* hopefully I_UTIME implies these */
2004 if (utime(str_get(st[++sp]),&utbuf))
2016 /* Do the permissions allow some operation? Assumes statcache already set. */
2019 cando(bit, effective, statbufp)
2022 register struct stat *statbufp;
2024 if ((effective ? euid : uid) == 0) { /* root is special */
2025 if (bit == S_IEXEC) {
2026 if (statbufp->st_mode & 0111 ||
2027 (statbufp->st_mode & S_IFMT) == S_IFDIR )
2031 return TRUE; /* root reads and writes anything */
2034 if (statbufp->st_uid == (effective ? euid : uid) ) {
2035 if (statbufp->st_mode & bit)
2036 return TRUE; /* ok as "user" */
2038 else if (ingroup((int)statbufp->st_gid,effective)) {
2039 if (statbufp->st_mode & bit >> 3)
2040 return TRUE; /* ok as "group" */
2042 else if (statbufp->st_mode & bit >> 6)
2043 return TRUE; /* ok as "other" */
2048 ingroup(testgid,effective)
2052 if (testgid == (effective ? egid : gid))
2059 GIDTYPE gary[NGROUPS];
2062 anum = getgroups(NGROUPS,gary);
2064 if (gary[anum] == testgid)