xref: /csrg-svn/usr.bin/window/wwenviron.c (revision 42954)
118742Sedward /*
233514Sbostic  * Copyright (c) 1983 Regents of the University of California.
333514Sbostic  * All rights reserved.
433514Sbostic  *
5*42954Sbostic  * This code is derived from software contributed to Berkeley by
6*42954Sbostic  * Edward Wang at The University of California, Berkeley.
7*42954Sbostic  *
842835Sbostic  * %sccs.include.redist.c%
918742Sedward  */
1018742Sedward 
1133514Sbostic #ifndef lint
12*42954Sbostic static char sccsid[] = "@(#)wwenviron.c	3.26 (Berkeley) 06/06/90";
1333514Sbostic #endif /* not lint */
1433514Sbostic 
1514689Sedward #include "ww.h"
1642834Sedward #ifdef POSIX_TTY
1742834Sedward #include <sys/ioctl.h>
1842834Sedward #endif
1916556Sedward #include <sys/signal.h>
2014689Sedward 
2114888Sedward /*
2214888Sedward  * Set up the environment of this process to run in window 'wp'.
2314888Sedward  */
2414689Sedward wwenviron(wp)
2514689Sedward register struct ww *wp;
2614689Sedward {
2714689Sedward 	register i;
2837461Sbostic #ifndef TIOCSCTTY
2915659Sedward 	int pgrp = getpid();
3037461Sbostic #endif
3132322Sedward 	char buf[1024];
3214689Sedward 
3337461Sbostic #ifndef TIOCSCTTY
3416313Sedward 	if ((i = open("/dev/tty", 0)) < 0)
3516313Sedward 		goto bad;
3629723Sedward 	if (ioctl(i, TIOCNOTTY, (char *)0) < 0)
3716313Sedward 		goto bad;
3814689Sedward 	(void) close(i);
3937461Sbostic #endif
4036801Sedward 	if ((i = wp->ww_socket) < 0) {
4139203Sedward 		struct winsize winsize;
4239203Sedward 
4336801Sedward 		if ((i = open(wp->ww_ttyname, 2)) < 0)
4436801Sedward 			goto bad;
4536801Sedward 		if (wwsettty(i, &wwwintty, (struct ww_tty *)0) < 0)
4636801Sedward 			goto bad;
4739203Sedward 		winsize.ws_row = wp->ww_w.nr;
4839203Sedward 		winsize.ws_col = wp->ww_w.nc;
4939203Sedward 		winsize.ws_xpixel = winsize.ws_ypixel = 0;
5039203Sedward 		if (ioctl(i, TIOCSWINSZ, (char *)&winsize) < 0)
5139203Sedward 			goto bad;
5236801Sedward 	}
5315632Sedward 	(void) dup2(i, 0);
5415632Sedward 	(void) dup2(i, 1);
5515632Sedward 	(void) dup2(i, 2);
5615632Sedward 	for (i = wwdtablesize - 1; i > 2; i--)
5715632Sedward 		(void) close(i);
5837461Sbostic #ifdef TIOCSCTTY
5937461Sbostic 	(void) setsid(0);
6037461Sbostic 	(void) ioctl(0, TIOCSCTTY, 0);
6137461Sbostic #else
6229723Sedward 	(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
6315659Sedward 	(void) setpgrp(pgrp, pgrp);
6437461Sbostic #endif
6531217Sedward 	/* SIGPIPE is the only one we ignore */
6616556Sedward 	(void) signal(SIGPIPE, SIG_DFL);
6731217Sedward 	(void) sigsetmask(0);
6832322Sedward 	/*
6932322Sedward 	 * Two conditions that make destructive setenv ok:
7032322Sedward 	 * 1. setenv() copies the string,
7132322Sedward 	 * 2. we've already called tgetent which copies the termcap entry.
7232322Sedward 	 */
7334803Sedward 	(void) sprintf(buf, "%sco#%d:li#%d:%s",
7434803Sedward 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
7532322Sedward 	(void) setenv("TERMCAP", buf, 1);
7632322Sedward 	(void) sprintf(buf, "%d", wp->ww_id + 1);
7732322Sedward 	(void) setenv("WINDOW_ID", buf, 1);
7816313Sedward 	return 0;
7916313Sedward bad:
8016313Sedward 	wwerrno = WWE_SYS;
8116313Sedward 	return -1;
8214689Sedward }
83