xref: /original-bsd/usr.bin/window/wwenviron.c (revision 9a897be2)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)wwenviron.c	3.26 (Berkeley) 06/06/90";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #ifdef POSIX_TTY
17 #include <sys/ioctl.h>
18 #endif
19 #include <sys/signal.h>
20 
21 /*
22  * Set up the environment of this process to run in window 'wp'.
23  */
24 wwenviron(wp)
25 register struct ww *wp;
26 {
27 	register i;
28 #ifndef TIOCSCTTY
29 	int pgrp = getpid();
30 #endif
31 	char buf[1024];
32 
33 #ifndef TIOCSCTTY
34 	if ((i = open("/dev/tty", 0)) < 0)
35 		goto bad;
36 	if (ioctl(i, TIOCNOTTY, (char *)0) < 0)
37 		goto bad;
38 	(void) close(i);
39 #endif
40 	if ((i = wp->ww_socket) < 0) {
41 		struct winsize winsize;
42 
43 		if ((i = open(wp->ww_ttyname, 2)) < 0)
44 			goto bad;
45 		if (wwsettty(i, &wwwintty, (struct ww_tty *)0) < 0)
46 			goto bad;
47 		winsize.ws_row = wp->ww_w.nr;
48 		winsize.ws_col = wp->ww_w.nc;
49 		winsize.ws_xpixel = winsize.ws_ypixel = 0;
50 		if (ioctl(i, TIOCSWINSZ, (char *)&winsize) < 0)
51 			goto bad;
52 	}
53 	(void) dup2(i, 0);
54 	(void) dup2(i, 1);
55 	(void) dup2(i, 2);
56 	for (i = wwdtablesize - 1; i > 2; i--)
57 		(void) close(i);
58 #ifdef TIOCSCTTY
59 	(void) setsid(0);
60 	(void) ioctl(0, TIOCSCTTY, 0);
61 #else
62 	(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
63 	(void) setpgrp(pgrp, pgrp);
64 #endif
65 	/* SIGPIPE is the only one we ignore */
66 	(void) signal(SIGPIPE, SIG_DFL);
67 	(void) sigsetmask(0);
68 	/*
69 	 * Two conditions that make destructive setenv ok:
70 	 * 1. setenv() copies the string,
71 	 * 2. we've already called tgetent which copies the termcap entry.
72 	 */
73 	(void) sprintf(buf, "%sco#%d:li#%d:%s",
74 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
75 	(void) setenv("TERMCAP", buf, 1);
76 	(void) sprintf(buf, "%d", wp->ww_id + 1);
77 	(void) setenv("WINDOW_ID", buf, 1);
78 	return 0;
79 bad:
80 	wwerrno = WWE_SYS;
81 	return -1;
82 }
83