xref: /plan9-contrib/sys/src/ape/lib/ap/plan9/_fdinfo.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #define  _BSDTIME_EXTENSION
2 #include "lib.h"
3 #include <sys/stat.h>
4 #include <stdlib.h>
5 
6 Fdinfo _fdinfo[OPEN_MAX];
7 
8 /*
9    called from _envsetup, either with the value of the environment
10    variable _fdinfo (from s to se-1), or with s==0 if there was no _fdinfo
11 */
12 void
13 _fdinit(char *s, char *se)
14 {
15 	int i;
16 	Fdinfo *fi;
17 	unsigned long fd, fl, ofl;
18 	char *e;
19 	struct stat sbuf;
20 
21 	if(s){
22 		while(s < se){
23 			fd = strtoul(s, &e, 10);
24 			if(s == e)
25 				break;
26 			s = e;
27 			fl = strtoul(s, &e, 10);
28 			if(s == e)
29 				break;
30 			s = e;
31 			ofl = strtoul(s, &e, 10);
32 			if(s == e)
33 				break;
34 			s = e;
35 			if(fd < OPEN_MAX){
36 				fi = &_fdinfo[fd];
37 				fi->flags = fl;
38 				fi->oflags = ofl;
39 /* hack, until all posix apps recompiled: */
40 				if(_isatty(fd))
41 					fi->flags |= FD_ISTTY;
42 			}
43 		}
44 	} else {
45 		for(i = 0; i <= 2; i++) {
46 			fi = &_fdinfo[i];
47 			fi->flags = FD_ISOPEN;
48 			fi->oflags = (i == 0)? O_RDONLY : O_WRONLY;
49 			if(_isatty(i))
50 				fi->flags |= FD_ISTTY;
51 		}
52 	}
53 	for(i = 0; i < OPEN_MAX; i++) {
54 		fi = &_fdinfo[i];
55 		if(fi->flags&FD_ISOPEN){
56 			if(fstat(i, &sbuf) >= 0) {
57 				fi->uid = sbuf.st_uid;
58 				fi->gid = sbuf.st_gid;
59 			}
60 		}
61 	}
62 }
63 
64