xref: /original-bsd/usr.bin/window/wwenviron.c (revision e79a6a26)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 06/06/93";
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 	(void) close(i);
52 #ifdef TIOCSCTTY
53 	(void) setsid();
54 	(void) ioctl(0, TIOCSCTTY, 0);
55 #else
56 	(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
57 	(void) setpgrp(pgrp, pgrp);
58 #endif
59 	/* SIGPIPE is the only one we ignore */
60 	(void) signal(SIGPIPE, SIG_DFL);
61 	(void) sigsetmask(0);
62 	/*
63 	 * Two conditions that make destructive setenv ok:
64 	 * 1. setenv() copies the string,
65 	 * 2. we've already called tgetent which copies the termcap entry.
66 	 */
67 	(void) sprintf(buf, "%sco#%d:li#%d:%s",
68 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
69 	(void) setenv("TERMCAP", buf, 1);
70 	(void) sprintf(buf, "%d", wp->ww_id + 1);
71 	(void) setenv("WINDOW_ID", buf, 1);
72 	return 0;
73 bad:
74 	wwerrno = WWE_SYS;
75 	return -1;
76 }
77