xref: /original-bsd/usr.bin/window/wwenviron.c (revision a9157423)
1 #ifndef lint
2 static	char *sccsid = "@(#)wwenviron.c	3.2 83/08/18";
3 #endif
4 
5 #include "ww.h"
6 
7 extern char **environ;
8 
9 wwenviron(wp)
10 register struct ww *wp;
11 {
12 	register i;
13 	register char **p, **q;
14 	char **termcap = 0;
15 	char **env;
16 	char *tbuf;
17 
18 	(void) dup2(wp->ww_tty, 0);
19 	(void) dup2(wp->ww_tty, 1);
20 	(void) dup2(wp->ww_tty, 2);
21 	for (i = wwdtablesize - 1; i > 2; i--)
22 		(void) close(i);
23 
24 	i = open("/dev/tty");
25 	(void) ioctl(i, (int)TIOCNOTTY, (char *)0);
26 	(void) close(i);
27 	(void) open(wp->ww_ttyname, 0);
28 
29 	for (i = 0, p = environ; *p; p++, i++)
30 		;
31 	if ((env = (char **)malloc((unsigned)(i + 3) * sizeof (char *))) == 0)
32 		return;			/* can't report error */
33 	if ((tbuf = malloc((unsigned) 1024)) == 0)
34 		return;			/* can't report error */
35 	for (p = environ, q = env; *p; p++, q++) {
36 		if (strncmp(*p, "TERM=", 5) == 0)
37 			*q = WWT_TERM;
38 		else if (strncmp(*p, "TERMCAP=", 8) == 0)
39 			termcap = q;
40 		else
41 			*q = *p;
42 	}
43 	if (termcap == 0)
44 		termcap = q++;
45 	*q = 0;
46 	*termcap = sprintf(tbuf, "TERMCAP=%sco#%d:li#%d:",
47 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr);
48 	if (wwavailmodes & WWM_REV)
49 		(void) strcat(tbuf, WWT_REV);
50 	if (wwavailmodes & WWM_UL)
51 		(void) strcat(tbuf, WWT_UL);
52 	(void) strcat(tbuf, wwkeys);
53 	environ = env;
54 }
55