xref: /csrg-svn/usr.bin/window/wwenviron.c (revision 37461)
118742Sedward /*
233514Sbostic  * Copyright (c) 1983 Regents of the University of California.
333514Sbostic  * All rights reserved.
433514Sbostic  *
533514Sbostic  * Redistribution and use in source and binary forms are permitted
634909Sbostic  * provided that the above copyright notice and this paragraph are
734909Sbostic  * duplicated in all such forms and that any documentation,
834909Sbostic  * advertising materials, and other materials related to such
934909Sbostic  * distribution and use acknowledge that the software was developed
1034909Sbostic  * by the University of California, Berkeley.  The name of the
1134909Sbostic  * University may not be used to endorse or promote products derived
1234909Sbostic  * from this software without specific prior written permission.
1334909Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434909Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534909Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1618742Sedward  */
1718742Sedward 
1833514Sbostic #ifndef lint
19*37461Sbostic static char sccsid[] = "@(#)wwenviron.c	3.22 (Berkeley) 04/20/89";
2033514Sbostic #endif /* not lint */
2133514Sbostic 
2214689Sedward #include "ww.h"
2316556Sedward #include <sys/signal.h>
2414689Sedward 
2514888Sedward /*
2614888Sedward  * Set up the environment of this process to run in window 'wp'.
2714888Sedward  */
2814689Sedward wwenviron(wp)
2914689Sedward register struct ww *wp;
3014689Sedward {
3114689Sedward 	register i;
32*37461Sbostic #ifndef TIOCSCTTY
3315659Sedward 	int pgrp = getpid();
34*37461Sbostic #endif
3532322Sedward 	char buf[1024];
3614689Sedward 
37*37461Sbostic #ifndef TIOCSCTTY
3816313Sedward 	if ((i = open("/dev/tty", 0)) < 0)
3916313Sedward 		goto bad;
4029723Sedward 	if (ioctl(i, TIOCNOTTY, (char *)0) < 0)
4116313Sedward 		goto bad;
4214689Sedward 	(void) close(i);
43*37461Sbostic #endif
4436801Sedward 	if ((i = wp->ww_socket) < 0) {
4536801Sedward 		if ((i = open(wp->ww_ttyname, 2)) < 0)
4636801Sedward 			goto bad;
4736801Sedward 		if (wwsettty(i, &wwwintty, (struct ww_tty *)0) < 0)
4836801Sedward 			goto bad;
4936801Sedward 	}
5015632Sedward 	(void) dup2(i, 0);
5115632Sedward 	(void) dup2(i, 1);
5215632Sedward 	(void) dup2(i, 2);
5315632Sedward 	for (i = wwdtablesize - 1; i > 2; i--)
5415632Sedward 		(void) close(i);
55*37461Sbostic #ifdef TIOCSCTTY
56*37461Sbostic 	(void) setsid(0);
57*37461Sbostic 	(void) ioctl(0, TIOCSCTTY, 0);
58*37461Sbostic #else
5929723Sedward 	(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
6015659Sedward 	(void) setpgrp(pgrp, pgrp);
61*37461Sbostic #endif
6231217Sedward 	/* SIGPIPE is the only one we ignore */
6316556Sedward 	(void) signal(SIGPIPE, SIG_DFL);
6431217Sedward 	(void) sigsetmask(0);
6532322Sedward 	/*
6632322Sedward 	 * Two conditions that make destructive setenv ok:
6732322Sedward 	 * 1. setenv() copies the string,
6832322Sedward 	 * 2. we've already called tgetent which copies the termcap entry.
6932322Sedward 	 */
7034803Sedward 	(void) sprintf(buf, "%sco#%d:li#%d:%s",
7134803Sedward 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
7232322Sedward 	(void) setenv("TERMCAP", buf, 1);
7332322Sedward 	(void) sprintf(buf, "%d", wp->ww_id + 1);
7432322Sedward 	(void) setenv("WINDOW_ID", buf, 1);
7516313Sedward 	return 0;
7616313Sedward bad:
7716313Sedward 	wwerrno = WWE_SYS;
7816313Sedward 	return -1;
7914689Sedward }
80