{ char windowid_env_var[40]; sprintf(windowid_env_var, "WINDOWID=%ld", windowid); putenv(windowid_env_var); }
static char *pty_init(void *frontend, void **backend_handle, Config *cfg,		      char *host, int port, char **realhost, int nodelay){    int slavefd;    pid_t pid, pgrp;    pty_frontend = frontend;    *backend_handle = NULL;	       /* we can't sensibly use this, sadly */    pty_term_width = cfg->width;    pty_term_height = cfg->height;    if (pty_master_fd < 0)	pty_open_master();    /*     * Set the backspace character to be whichever of ^H and ^? is     * specified by bksp_is_delete.     */    {	struct termios attrs;	tcgetattr(pty_master_fd, &attrs);	attrs.c_cc[VERASE] = cfg->bksp_is_delete ? '\177' : '\010';	tcsetattr(pty_master_fd, TCSANOW, &attrs);    }    /*     * Stamp utmp (that is, tell the utmp helper process to do so),     * or not.     */    if (!cfg->stamp_utmp)	close(pty_utmp_helper_pipe);   /* just let the child process die */    else {	char *location = get_x_display(pty_frontend);	int len = strlen(location)+1, pos = 0;   /* +1 to include NUL */	while (pos < len) {	    int ret = write(pty_utmp_helper_pipe, location+pos, len - pos);	    if (ret < 0) {		perror("pterm: writing to utmp helper process");		close(pty_utmp_helper_pipe);   /* arrgh, just give up */		break;	    }	    pos += ret;	}    }    /*     * Fork and execute the command.     */    pid = fork();    if (pid < 0) {	perror("fork");	exit(1);    }    if (pid == 0) {	int i;	/*	 * We are the child.	 */	slavefd = open(pty_name, O_RDWR);	if (slavefd < 0) {	    perror("slave pty: open");	    _exit(1);	}	close(pty_master_fd);	fcntl(slavefd, F_SETFD, 0);    /* don't close on exec */	dup2(slavefd, 0);	dup2(slavefd, 1);	dup2(slavefd, 2);	setsid();	ioctl(slavefd, TIOCSCTTY, 1);	pgrp = getpid();	tcsetpgrp(slavefd, pgrp);	setpgrp();	close(open(pty_name, O_WRONLY, 0));	setpgrp();	/* Close everything _else_, for tidiness. */	for (i = 3; i < 1024; i++)	    close(i);	{	    char term_env_var[10 + sizeof(cfg->termtype)];	    sprintf(term_env_var, "TERM=%s", cfg->termtype);	    putenv(term_env_var);	}	/*	 * SIGINT and SIGQUIT may have been set to ignored by our	 * parent, particularly by things like sh -c 'pterm &' and	 * some window managers. Reverse this for our child process.	 */	putty_signal(SIGINT, SIG_DFL);	putty_signal(SIGQUIT, SIG_DFL);	if (pty_argv)	    execvp(pty_argv[0], pty_argv);	else {	    char *shell = getenv("SHELL");	    char *shellname;	    if (cfg->login_shell) {		char *p = strrchr(shell, '/');		shellname = smalloc(2+strlen(shell));		p = p ? p+1 : shell;		sprintf(shellname, "-%s", p);	    } else		shellname = shell;	    execl(getenv("SHELL"), shellname, NULL);	}	/*	 * If we're here, exec has gone badly foom.	 */	perror("exec");	_exit(127);    } else {	pty_child_pid = pid;	pty_child_dead = FALSE;    }          return NULL;}