118742Sedward /*
2*62479Sbostic * Copyright (c) 1983, 1993
3*62479Sbostic * The Regents of the University of California. All rights reserved.
433514Sbostic *
542954Sbostic * This code is derived from software contributed to Berkeley by
642954Sbostic * Edward Wang at The University of California, Berkeley.
742954Sbostic *
842835Sbostic * %sccs.include.redist.c%
918742Sedward */
1018742Sedward
1133514Sbostic #ifndef lint
12*62479Sbostic static char sccsid[] = "@(#)wwenviron.c 8.1 (Berkeley) 06/06/93";
1333514Sbostic #endif /* not lint */
1433514Sbostic
1514689Sedward #include "ww.h"
1645033Sedward #if !defined(OLD_TTY) && !defined(TIOCSCTTY) && !defined(TIOCNOTTY)
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 */
wwenviron(wp)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) {
4136801Sedward if ((i = open(wp->ww_ttyname, 2)) < 0)
4236801Sedward goto bad;
4345033Sedward if (wwsettty(i, &wwwintty) < 0)
4436801Sedward goto bad;
4545033Sedward if (wwsetttysize(i, wp->ww_w.nr, wp->ww_w.nc) < 0)
4639203Sedward goto bad;
4736801Sedward }
4815632Sedward (void) dup2(i, 0);
4915632Sedward (void) dup2(i, 1);
5015632Sedward (void) dup2(i, 2);
5156015Sedward (void) close(i);
5237461Sbostic #ifdef TIOCSCTTY
5345033Sedward (void) setsid();
5437461Sbostic (void) ioctl(0, TIOCSCTTY, 0);
5537461Sbostic #else
5629723Sedward (void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
5715659Sedward (void) setpgrp(pgrp, pgrp);
5837461Sbostic #endif
5931217Sedward /* SIGPIPE is the only one we ignore */
6016556Sedward (void) signal(SIGPIPE, SIG_DFL);
6131217Sedward (void) sigsetmask(0);
6232322Sedward /*
6332322Sedward * Two conditions that make destructive setenv ok:
6432322Sedward * 1. setenv() copies the string,
6532322Sedward * 2. we've already called tgetent which copies the termcap entry.
6632322Sedward */
6734803Sedward (void) sprintf(buf, "%sco#%d:li#%d:%s",
6834803Sedward WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
6932322Sedward (void) setenv("TERMCAP", buf, 1);
7032322Sedward (void) sprintf(buf, "%d", wp->ww_id + 1);
7132322Sedward (void) setenv("WINDOW_ID", buf, 1);
7216313Sedward return 0;
7316313Sedward bad:
7416313Sedward wwerrno = WWE_SYS;
7516313Sedward return -1;
7614689Sedward }
77