xref: /original-bsd/usr.bin/window/wwenviron.c (revision 5133e8a4)
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.27 (Berkeley) 08/12/90";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #if !defined(OLD_TTY) && !defined(TIOCSCTTY) && !defined(TIOCNOTTY)
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 		if ((i = open(wp->ww_ttyname, 2)) < 0)
42 			goto bad;
43 		if (wwsettty(i, &wwwintty) < 0)
44 			goto bad;
45 		if (wwsetttysize(i, wp->ww_w.nr, wp->ww_w.nc) < 0)
46 			goto bad;
47 	}
48 	(void) dup2(i, 0);
49 	(void) dup2(i, 1);
50 	(void) dup2(i, 2);
51 	for (i = wwdtablesize - 1; i > 2; i--)
52 		(void) close(i);
53 #ifdef TIOCSCTTY
54 	(void) setsid();
55 	(void) ioctl(0, TIOCSCTTY, 0);
56 #else
57 	(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
58 	(void) setpgrp(pgrp, pgrp);
59 #endif
60 	/* SIGPIPE is the only one we ignore */
61 	(void) signal(SIGPIPE, SIG_DFL);
62 	(void) sigsetmask(0);
63 	/*
64 	 * Two conditions that make destructive setenv ok:
65 	 * 1. setenv() copies the string,
66 	 * 2. we've already called tgetent which copies the termcap entry.
67 	 */
68 	(void) sprintf(buf, "%sco#%d:li#%d:%s",
69 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
70 	(void) setenv("TERMCAP", buf, 1);
71 	(void) sprintf(buf, "%d", wp->ww_id + 1);
72 	(void) setenv("WINDOW_ID", buf, 1);
73 	return 0;
74 bad:
75 	wwerrno = WWE_SYS;
76 	return -1;
77 }
78