118742Sedward /* 233514Sbostic * Copyright (c) 1983 Regents of the University of California. 333514Sbostic * 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*45033Sedward static char sccsid[] = "@(#)wwenviron.c 3.27 (Berkeley) 08/12/90"; 1333514Sbostic #endif /* not lint */ 1433514Sbostic 1514689Sedward #include "ww.h" 16*45033Sedward #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 */ 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; 43*45033Sedward if (wwsettty(i, &wwwintty) < 0) 4436801Sedward goto bad; 45*45033Sedward 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); 5115632Sedward for (i = wwdtablesize - 1; i > 2; i--) 5215632Sedward (void) close(i); 5337461Sbostic #ifdef TIOCSCTTY 54*45033Sedward (void) setsid(); 5537461Sbostic (void) ioctl(0, TIOCSCTTY, 0); 5637461Sbostic #else 5729723Sedward (void) ioctl(0, TIOCSPGRP, (char *)&pgrp); 5815659Sedward (void) setpgrp(pgrp, pgrp); 5937461Sbostic #endif 6031217Sedward /* SIGPIPE is the only one we ignore */ 6116556Sedward (void) signal(SIGPIPE, SIG_DFL); 6231217Sedward (void) sigsetmask(0); 6332322Sedward /* 6432322Sedward * Two conditions that make destructive setenv ok: 6532322Sedward * 1. setenv() copies the string, 6632322Sedward * 2. we've already called tgetent which copies the termcap entry. 6732322Sedward */ 6834803Sedward (void) sprintf(buf, "%sco#%d:li#%d:%s", 6934803Sedward WWT_TERMCAP, wp->ww_w.nc, wp->ww_w.nr, wwwintermcap); 7032322Sedward (void) setenv("TERMCAP", buf, 1); 7132322Sedward (void) sprintf(buf, "%d", wp->ww_id + 1); 7232322Sedward (void) setenv("WINDOW_ID", buf, 1); 7316313Sedward return 0; 7416313Sedward bad: 7516313Sedward wwerrno = WWE_SYS; 7616313Sedward return -1; 7714689Sedward } 78