xref: /csrg-svn/usr.bin/window/wwenviron.c (revision 42834)
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*42834Sedward static char sccsid[] = "@(#)wwenviron.c	3.24 (Berkeley) 06/02/90";
2033514Sbostic #endif /* not lint */
2133514Sbostic 
2214689Sedward #include "ww.h"
23*42834Sedward #ifdef POSIX_TTY
24*42834Sedward #include <sys/ioctl.h>
25*42834Sedward #endif
2616556Sedward #include <sys/signal.h>
2714689Sedward 
2814888Sedward /*
2914888Sedward  * Set up the environment of this process to run in window 'wp'.
3014888Sedward  */
3114689Sedward wwenviron(wp)
3214689Sedward register struct ww *wp;
3314689Sedward {
3414689Sedward 	register i;
3537461Sbostic #ifndef TIOCSCTTY
3615659Sedward 	int pgrp = getpid();
3737461Sbostic #endif
3832322Sedward 	char buf[1024];
3914689Sedward 
4037461Sbostic #ifndef TIOCSCTTY
4116313Sedward 	if ((i = open("/dev/tty", 0)) < 0)
4216313Sedward 		goto bad;
4329723Sedward 	if (ioctl(i, TIOCNOTTY, (char *)0) < 0)
4416313Sedward 		goto bad;
4514689Sedward 	(void) close(i);
4637461Sbostic #endif
4736801Sedward 	if ((i = wp->ww_socket) < 0) {
4839203Sedward 		struct winsize winsize;
4939203Sedward 
5036801Sedward 		if ((i = open(wp->ww_ttyname, 2)) < 0)
5136801Sedward 			goto bad;
5236801Sedward 		if (wwsettty(i, &wwwintty, (struct ww_tty *)0) < 0)
5336801Sedward 			goto bad;
5439203Sedward 		winsize.ws_row = wp->ww_w.nr;
5539203Sedward 		winsize.ws_col = wp->ww_w.nc;
5639203Sedward 		winsize.ws_xpixel = winsize.ws_ypixel = 0;
5739203Sedward 		if (ioctl(i, TIOCSWINSZ, (char *)&winsize) < 0)
5839203Sedward 			goto bad;
5936801Sedward 	}
6015632Sedward 	(void) dup2(i, 0);
6115632Sedward 	(void) dup2(i, 1);
6215632Sedward 	(void) dup2(i, 2);
6315632Sedward 	for (i = wwdtablesize - 1; i > 2; i--)
6415632Sedward 		(void) close(i);
6537461Sbostic #ifdef TIOCSCTTY
6637461Sbostic 	(void) setsid(0);
6737461Sbostic 	(void) ioctl(0, TIOCSCTTY, 0);
6837461Sbostic #else
6929723Sedward 	(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
7015659Sedward 	(void) setpgrp(pgrp, pgrp);
7137461Sbostic #endif
7231217Sedward 	/* SIGPIPE is the only one we ignore */
7316556Sedward 	(void) signal(SIGPIPE, SIG_DFL);
7431217Sedward 	(void) sigsetmask(0);
7532322Sedward 	/*
7632322Sedward 	 * Two conditions that make destructive setenv ok:
7732322Sedward 	 * 1. setenv() copies the string,
7832322Sedward 	 * 2. we've already called tgetent which copies the termcap entry.
7932322Sedward 	 */
8034803Sedward 	(void) sprintf(buf, "%sco#%d:li#%d:%s",
8134803Sedward 		WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
8232322Sedward 	(void) setenv("TERMCAP", buf, 1);
8332322Sedward 	(void) sprintf(buf, "%d", wp->ww_id + 1);
8432322Sedward 	(void) setenv("WINDOW_ID", buf, 1);
8516313Sedward 	return 0;
8616313Sedward bad:
8716313Sedward 	wwerrno = WWE_SYS;
8816313Sedward 	return -1;
8914689Sedward }
90