Commit | Line | Data |
---|---|---|
aa149fde | 1 | #ifndef lint |
ac62cc78 | 2 | static char *sccsid = "@(#)wwenviron.c 3.4 83/11/29"; |
aa149fde EW |
3 | #endif |
4 | ||
5 | #include "ww.h" | |
6 | ||
224ba859 EW |
7 | /* |
8 | * Set up the environment of this process to run in window 'wp'. | |
9 | * Can't report errors in any intelligent way, so don't. | |
10 | */ | |
aa149fde EW |
11 | wwenviron(wp) |
12 | register struct ww *wp; | |
13 | { | |
ac62cc78 EW |
14 | static char **termcap = 0; |
15 | static char *tbuf; | |
aa149fde | 16 | register i; |
aa149fde EW |
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 | ||
224ba859 EW |
24 | i = open("/dev/tty", 0); |
25 | if (i < 0) { | |
26 | perror("/dev/tty"); | |
27 | return; | |
28 | } | |
29 | if (ioctl(i, (int)TIOCNOTTY, (char *)0) < 0) { | |
30 | perror("ioctl(TIOCNOTTY)"); | |
31 | return; | |
32 | } | |
aa149fde | 33 | (void) close(i); |
224ba859 EW |
34 | if (open(wp->ww_ttyname, 0) < 0) { |
35 | perror(wp->ww_ttyname); | |
36 | return; | |
37 | } | |
aa149fde | 38 | |
ac62cc78 EW |
39 | /* |
40 | * Do this only once if vfork(). | |
41 | */ | |
42 | if (termcap == 0) { | |
43 | extern char **environ; | |
44 | static char **env; | |
45 | register char **p, **q; | |
46 | ||
47 | for (i = 0, p = environ; *p; p++, i++) | |
48 | ; | |
49 | env = (char **)malloc((unsigned)(i + 3) * sizeof (char *)); | |
50 | if (env == 0) | |
51 | return; | |
52 | if ((tbuf = malloc((unsigned) 1024)) == 0) | |
53 | return; | |
54 | for (p = environ, q = env; *p; p++, q++) { | |
55 | if (strncmp(*p, "TERM=", 5) == 0) | |
56 | *q = WWT_TERM; | |
57 | else if (strncmp(*p, "TERMCAP=", 8) == 0) | |
58 | termcap = q; | |
59 | else | |
60 | *q = *p; | |
61 | } | |
62 | if (termcap == 0) | |
63 | termcap = q++; | |
64 | *q = 0; | |
65 | environ = env; | |
aa149fde | 66 | } |
a598c465 EW |
67 | *termcap = sprintf(tbuf, "TERMCAP=%sco#%d:li#%d:", |
68 | WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr); | |
aa149fde EW |
69 | if (wwavailmodes & WWM_REV) |
70 | (void) strcat(tbuf, WWT_REV); | |
71 | if (wwavailmodes & WWM_UL) | |
72 | (void) strcat(tbuf, WWT_UL); | |
73 | (void) strcat(tbuf, wwkeys); | |
aa149fde | 74 | } |