port to new kvm
[unix-history] / usr / src / usr.bin / window / wwenviron.c
CommitLineData
60de5df9 1/*
46e9ea25
KB
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
3dd3a9e5
KB
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
7 *
87f529ec 8 * %sccs.include.redist.c%
60de5df9
EW
9 */
10
46e9ea25 11#ifndef lint
9de86788 12static char sccsid[] = "@(#)wwenviron.c 3.27 (Berkeley) %G%";
46e9ea25
KB
13#endif /* not lint */
14
aa149fde 15#include "ww.h"
9de86788 16#if !defined(OLD_TTY) && !defined(TIOCSCTTY) && !defined(TIOCNOTTY)
bbbb7b14
EW
17#include <sys/ioctl.h>
18#endif
1e40e125 19#include <sys/signal.h>
aa149fde 20
224ba859
EW
21/*
22 * Set up the environment of this process to run in window 'wp'.
224ba859 23 */
aa149fde
EW
24wwenviron(wp)
25register struct ww *wp;
26{
27 register i;
06016e3e 28#ifndef TIOCSCTTY
950f1780 29 int pgrp = getpid();
06016e3e 30#endif
cd9c5720 31 char buf[1024];
aa149fde 32
06016e3e 33#ifndef TIOCSCTTY
7ecf4dca
EW
34 if ((i = open("/dev/tty", 0)) < 0)
35 goto bad;
20029082 36 if (ioctl(i, TIOCNOTTY, (char *)0) < 0)
7ecf4dca 37 goto bad;
aa149fde 38 (void) close(i);
06016e3e 39#endif
77f9a432
EW
40 if ((i = wp->ww_socket) < 0) {
41 if ((i = open(wp->ww_ttyname, 2)) < 0)
42 goto bad;
9de86788 43 if (wwsettty(i, &wwwintty) < 0)
77f9a432 44 goto bad;
9de86788 45 if (wwsetttysize(i, wp->ww_w.nr, wp->ww_w.nc) < 0)
c313fc36 46 goto bad;
77f9a432 47 }
eec72f58
EW
48 (void) dup2(i, 0);
49 (void) dup2(i, 1);
50 (void) dup2(i, 2);
51 for (i = wwdtablesize - 1; i > 2; i--)
52 (void) close(i);
06016e3e 53#ifdef TIOCSCTTY
9de86788 54 (void) setsid();
06016e3e
KB
55 (void) ioctl(0, TIOCSCTTY, 0);
56#else
20029082 57 (void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
950f1780 58 (void) setpgrp(pgrp, pgrp);
06016e3e 59#endif
3c1a9287 60 /* SIGPIPE is the only one we ignore */
1e40e125 61 (void) signal(SIGPIPE, SIG_DFL);
3c1a9287 62 (void) sigsetmask(0);
cd9c5720
EW
63 /*
64 * Two conditions that make destructive setenv ok:
65 * 1. setenv() copies the string,
66 * 2. we've already called tgetent which copies the termcap entry.
67 */
cf28fdac
EW
68 (void) sprintf(buf, "%sco#%d:li#%d:%s",
69 WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap);
cd9c5720
EW
70 (void) setenv("TERMCAP", buf, 1);
71 (void) sprintf(buf, "%d", wp->ww_id + 1);
72 (void) setenv("WINDOW_ID", buf, 1);
7ecf4dca
EW
73 return 0;
74bad:
75 wwerrno = WWE_SYS;
76 return -1;
aa149fde 77}