xref: /original-bsd/usr.bin/window/wwenviron.c (revision 0125a717)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)wwenviron.c	3.18 (Berkeley) 02/21/88";
15 #endif /* not lint */
16 
17 #include "ww.h"
18 #include <sys/signal.h>
19 
20 /*
21  * Set up the environment of this process to run in window 'wp'.
22  */
23 wwenviron(wp)
24 register struct ww *wp;
25 {
26 	register i;
27 	int pgrp = getpid();
28 	char buf[1024];
29 
30 	if ((i = open("/dev/tty", 0)) < 0)
31 		goto bad;
32 	if (ioctl(i, TIOCNOTTY, (char *)0) < 0)
33 		goto bad;
34 	(void) close(i);
35 	if ((i = wp->ww_socket) < 0 && (i = open(wp->ww_ttyname, 2)) < 0)
36 		goto bad;
37 	(void) dup2(i, 0);
38 	(void) dup2(i, 1);
39 	(void) dup2(i, 2);
40 	for (i = wwdtablesize - 1; i > 2; i--)
41 		(void) close(i);
42 	(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
43 	(void) setpgrp(pgrp, pgrp);
44 	/* SIGPIPE is the only one we ignore */
45 	(void) signal(SIGPIPE, SIG_DFL);
46 	(void) sigsetmask(0);
47 	/*
48 	 * Two conditions that make destructive setenv ok:
49 	 * 1. setenv() copies the string,
50 	 * 2. we've already called tgetent which copies the termcap entry.
51 	 */
52 	(void) sprintf(buf, "%sco#%d:li#%d:%s%s%s%s%s%s%s",
53 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr,
54 		wwavailmodes & WWM_REV ? WWT_REV : "",
55 		wwavailmodes & WWM_BLK ? WWT_BLK : "",
56 		wwavailmodes & WWM_UL ? WWT_UL : "",
57 		wwavailmodes & WWM_GRP ? WWT_GRP : "",
58 		wwavailmodes & WWM_DIM ? WWT_DIM : "",
59 		wwavailmodes & WWM_USR ? WWT_USR : "",
60 		wwkeys);
61 	(void) setenv("TERMCAP", buf, 1);
62 	(void) sprintf(buf, "%d", wp->ww_id + 1);
63 	(void) setenv("WINDOW_ID", buf, 1);
64 	return 0;
65 bad:
66 	wwerrno = WWE_SYS;
67 	return -1;
68 }
69