xref: /openbsd-src/usr.bin/vi/ex/ex_script.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: ex_script.c,v 1.14 2007/09/02 15:19:35 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1992, 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Brian Hirt.
11  *
12  * See the LICENSE file for redistribution information.
13  */
14 
15 #include "config.h"
16 
17 #ifndef lint
18 static const char sccsid[] = "@(#)ex_script.c	10.30 (Berkeley) 9/24/96";
19 #endif /* not lint */
20 
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/queue.h>
24 #ifdef HAVE_SYS_SELECT_H
25 #include <sys/select.h>
26 #endif
27 #include <sys/stat.h>
28 #ifdef HAVE_SYS5_PTY
29 #include <sys/stropts.h>
30 #endif
31 #include <sys/time.h>
32 #include <sys/wait.h>
33 
34 #include <bitstring.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>		/* XXX: OSF/1 bug: include before <grp.h> */
38 #include <grp.h>
39 #include <limits.h>
40 #include <poll.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <termios.h>
44 #include <unistd.h>
45 
46 #include "../common/common.h"
47 #include "../vi/vi.h"
48 #include "script.h"
49 #include "pathnames.h"
50 
51 static void	sscr_check(SCR *);
52 static int	sscr_getprompt(SCR *);
53 static int	sscr_init(SCR *);
54 static int	sscr_insert(SCR *);
55 static int	sscr_matchprompt(SCR *, char *, size_t, size_t *);
56 static int	sscr_pty(int *, int *, char *, size_t, struct termios *, void *);
57 static int	sscr_setprompt(SCR *, char *, size_t);
58 
59 /*
60  * ex_script -- : sc[ript][!] [file]
61  *	Switch to script mode.
62  *
63  * PUBLIC: int ex_script(SCR *, EXCMD *);
64  */
65 int
66 ex_script(sp, cmdp)
67 	SCR *sp;
68 	EXCMD *cmdp;
69 {
70 	/* Vi only command. */
71 	if (!F_ISSET(sp, SC_VI)) {
72 		msgq(sp, M_ERR,
73 		    "150|The script command is only available in vi mode");
74 		return (1);
75 	}
76 
77 	/* Switch to the new file. */
78 	if (cmdp->argc != 0 && ex_edit(sp, cmdp))
79 		return (1);
80 
81 	/* Create the shell, figure out the prompt. */
82 	if (sscr_init(sp))
83 		return (1);
84 
85 	return (0);
86 }
87 
88 /*
89  * sscr_init --
90  *	Create a pty setup for a shell.
91  */
92 static int
93 sscr_init(sp)
94 	SCR *sp;
95 {
96 	SCRIPT *sc;
97 	char *sh, *sh_path;
98 
99 	/* We're going to need a shell. */
100 	if (opts_empty(sp, O_SHELL, 0))
101 		return (1);
102 
103 	MALLOC_RET(sp, sc, SCRIPT *, sizeof(SCRIPT));
104 	sp->script = sc;
105 	sc->sh_prompt = NULL;
106 	sc->sh_prompt_len = 0;
107 
108 	/*
109 	 * There are two different processes running through this code.
110 	 * They are the shell and the parent.
111 	 */
112 	sc->sh_master = sc->sh_slave = -1;
113 
114 	if (tcgetattr(STDIN_FILENO, &sc->sh_term) == -1) {
115 		msgq(sp, M_SYSERR, "tcgetattr");
116 		goto err;
117 	}
118 
119 	/*
120 	 * Turn off output postprocessing and echo.
121 	 */
122 	sc->sh_term.c_oflag &= ~OPOST;
123 	sc->sh_term.c_cflag &= ~(ECHO|ECHOE|ECHONL|ECHOK);
124 
125 #ifdef TIOCGWINSZ
126 	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &sc->sh_win) == -1) {
127 		msgq(sp, M_SYSERR, "tcgetattr");
128 		goto err;
129 	}
130 
131 	if (sscr_pty(&sc->sh_master,
132 	    &sc->sh_slave, sc->sh_name, sizeof(sc->sh_name),
133 	    &sc->sh_term, &sc->sh_win) == -1) {
134 		msgq(sp, M_SYSERR, "pty");
135 		goto err;
136 	}
137 #else
138 	if (sscr_pty(&sc->sh_master,
139 	    &sc->sh_slave, sc->sh_name, sizeof(sc->sh_name),
140 	    &sc->sh_term, NULL) == -1) {
141 		msgq(sp, M_SYSERR, "pty");
142 		goto err;
143 	}
144 #endif
145 
146 	/*
147 	 * __TK__ huh?
148 	 * Don't use vfork() here, because the signal semantics differ from
149 	 * implementation to implementation.
150 	 */
151 	switch (sc->sh_pid = fork()) {
152 	case -1:			/* Error. */
153 		msgq(sp, M_SYSERR, "fork");
154 err:		if (sc->sh_master != -1)
155 			(void)close(sc->sh_master);
156 		if (sc->sh_slave != -1)
157 			(void)close(sc->sh_slave);
158 		return (1);
159 	case 0:				/* Utility. */
160 		/*
161 		 * XXX
162 		 * So that shells that do command line editing turn it off.
163 		 */
164 		if (setenv("TERM", "emacs", 1) == -1 ||
165 		    setenv("TERMCAP", "emacs:", 1) == -1 ||
166 		    setenv("EMACS", "t", 1) == -1)
167 			_exit(126);
168 
169 		(void)setsid();
170 #ifdef TIOCSCTTY
171 		/*
172 		 * 4.4BSD allocates a controlling terminal using the TIOCSCTTY
173 		 * ioctl, not by opening a terminal device file.  POSIX 1003.1
174 		 * doesn't define a portable way to do this.  If TIOCSCTTY is
175 		 * not available, hope that the open does it.
176 		 */
177 		(void)ioctl(sc->sh_slave, TIOCSCTTY, 0);
178 #endif
179 		(void)close(sc->sh_master);
180 		(void)dup2(sc->sh_slave, STDIN_FILENO);
181 		(void)dup2(sc->sh_slave, STDOUT_FILENO);
182 		(void)dup2(sc->sh_slave, STDERR_FILENO);
183 		(void)close(sc->sh_slave);
184 
185 		/* Assumes that all shells have -i. */
186 		sh_path = O_STR(sp, O_SHELL);
187 		if ((sh = strrchr(sh_path, '/')) == NULL)
188 			sh = sh_path;
189 		else
190 			++sh;
191 		execl(sh_path, sh, "-i", (char *)NULL);
192 		msgq_str(sp, M_SYSERR, sh_path, "execl: %s");
193 		_exit(127);
194 	default:			/* Parent. */
195 		break;
196 	}
197 
198 	if (sscr_getprompt(sp))
199 		return (1);
200 
201 	F_SET(sp, SC_SCRIPT);
202 	F_SET(sp->gp, G_SCRWIN);
203 	return (0);
204 }
205 
206 /*
207  * sscr_getprompt --
208  *	Eat lines printed by the shell until a line with no trailing
209  *	carriage return comes; set the prompt from that line.
210  */
211 static int
212 sscr_getprompt(sp)
213 	SCR *sp;
214 {
215 	CHAR_T *endp, *p, *t, buf[1024];
216 	SCRIPT *sc;
217 	struct pollfd pfd[1];
218 	recno_t lline;
219 	size_t llen, len;
220 	u_int value;
221 	int nr;
222 
223 	endp = buf;
224 	len = sizeof(buf);
225 
226 	/* Wait up to a second for characters to read. */
227 	sc = sp->script;
228 	pfd[0].fd = sc->sh_master;
229 	pfd[0].events = POLLIN;
230 	switch (poll(pfd, 1, 5 * 1000)) {
231 	case -1:		/* Error or interrupt. */
232 		msgq(sp, M_SYSERR, "poll");
233 		goto prompterr;
234 	case  0:		/* Timeout */
235 		msgq(sp, M_ERR, "Error: timed out");
236 		goto prompterr;
237 	default:		/* Characters to read. */
238 		break;
239 	}
240 
241 	/* Read the characters. */
242 more:	len = sizeof(buf) - (endp - buf);
243 	switch (nr = read(sc->sh_master, endp, len)) {
244 	case  0:			/* EOF. */
245 		msgq(sp, M_ERR, "Error: shell: EOF");
246 		goto prompterr;
247 	case -1:			/* Error or interrupt. */
248 		msgq(sp, M_SYSERR, "shell");
249 		goto prompterr;
250 	default:
251 		endp += nr;
252 		break;
253 	}
254 
255 	/* If any complete lines, push them into the file. */
256 	for (p = t = buf; p < endp; ++p) {
257 		value = KEY_VAL(sp, *p);
258 		if (value == K_CR || value == K_NL) {
259 			if (db_last(sp, &lline) ||
260 			    db_append(sp, 0, lline, t, p - t))
261 				goto prompterr;
262 			t = p + 1;
263 		}
264 	}
265 	if (p > buf) {
266 		memmove(buf, t, endp - t);
267 		endp = buf + (endp - t);
268 	}
269 	if (endp == buf)
270 		goto more;
271 
272 	/* Wait up 1/10 of a second to make sure that we got it all. */
273 	switch (poll(pfd, 1, 100)) {
274 	case -1:		/* Error or interrupt. */
275 		msgq(sp, M_SYSERR, "poll");
276 		goto prompterr;
277 	case  0:		/* Timeout */
278 		break;
279 	default:		/* Characters to read. */
280 		goto more;
281 	}
282 
283 	/* Timed out, so theoretically we have a prompt. */
284 	llen = endp - buf;
285 	endp = buf;
286 
287 	/* Append the line into the file. */
288 	if (db_last(sp, &lline) || db_append(sp, 0, lline, buf, llen)) {
289 prompterr:	sscr_end(sp);
290 		return (1);
291 	}
292 
293 	return (sscr_setprompt(sp, buf, llen));
294 }
295 
296 /*
297  * sscr_exec --
298  *	Take a line and hand it off to the shell.
299  *
300  * PUBLIC: int sscr_exec(SCR *, recno_t);
301  */
302 int
303 sscr_exec(sp, lno)
304 	SCR *sp;
305 	recno_t lno;
306 {
307 	SCRIPT *sc;
308 	recno_t last_lno;
309 	size_t blen, len, last_len, tlen;
310 	int isempty, matchprompt, nw, rval;
311 	char *bp, *p;
312 
313 	/* If there's a prompt on the last line, append the command. */
314 	if (db_last(sp, &last_lno))
315 		return (1);
316 	if (db_get(sp, last_lno, DBG_FATAL, &p, &last_len))
317 		return (1);
318 	if (sscr_matchprompt(sp, p, last_len, &tlen) && tlen == 0) {
319 		matchprompt = 1;
320 		GET_SPACE_RET(sp, bp, blen, last_len + 128);
321 		memmove(bp, p, last_len);
322 	} else
323 		matchprompt = 0;
324 
325 	/* Get something to execute. */
326 	if (db_eget(sp, lno, &p, &len, &isempty)) {
327 		if (isempty)
328 			goto empty;
329 		goto err1;
330 	}
331 
332 	/* Empty lines aren't interesting. */
333 	if (len == 0)
334 		goto empty;
335 
336 	/* Delete any prompt. */
337 	if (sscr_matchprompt(sp, p, len, &tlen)) {
338 		if (tlen == len) {
339 empty:			msgq(sp, M_BERR, "151|No command to execute");
340 			goto err1;
341 		}
342 		p += (len - tlen);
343 		len = tlen;
344 	}
345 
346 	/* Push the line to the shell. */
347 	sc = sp->script;
348 	if ((nw = write(sc->sh_master, p, len)) != len)
349 		goto err2;
350 	rval = 0;
351 	if (write(sc->sh_master, "\n", 1) != 1) {
352 err2:		if (nw == 0)
353 			errno = EIO;
354 		msgq(sp, M_SYSERR, "shell");
355 		goto err1;
356 	}
357 
358 	if (matchprompt) {
359 		ADD_SPACE_RET(sp, bp, blen, last_len + len);
360 		memmove(bp + last_len, p, len);
361 		if (db_set(sp, last_lno, bp, last_len + len))
362 err1:			rval = 1;
363 	}
364 	if (matchprompt)
365 		FREE_SPACE(sp, bp, blen);
366 	return (rval);
367 }
368 
369 /*
370  * sscr_input --
371  *	Read any waiting shell input.
372  *
373  * PUBLIC: int sscr_input(SCR *);
374  */
375 int
376 sscr_input(sp)
377 	SCR *sp;
378 {
379 	GS *gp;
380 	struct timeval tv;
381 	fd_set *rdfd;
382 	int maxfd;
383 
384 	gp = sp->gp;
385 
386 	/* Allocate space for rdfd. */
387 	maxfd = STDIN_FILENO;
388 	CIRCLEQ_FOREACH(sp, &gp->dq, q)
389 		if (F_ISSET(sp, SC_SCRIPT) && sp->script->sh_master > maxfd)
390 			maxfd = sp->script->sh_master;
391 	rdfd = (fd_set *)calloc(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
392 	if (rdfd == NULL) {
393 		msgq(sp, M_SYSERR, "malloc");
394 		return (1);
395 	}
396 
397 loop:	memset(rdfd, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
398 	tv.tv_sec = 0;
399 	tv.tv_usec = 0;
400 
401 	/* Set up the input mask. */
402 	CIRCLEQ_FOREACH(sp, &gp->dq, q)
403 		if (F_ISSET(sp, SC_SCRIPT))
404 			FD_SET(sp->script->sh_master, rdfd);
405 
406 	/* Check for input. */
407 	switch (select(maxfd + 1, rdfd, NULL, NULL, &tv)) {
408 	case -1:
409 		msgq(sp, M_SYSERR, "select");
410 		free(rdfd);
411 		return (1);
412 	case 0:
413 		free(rdfd);
414 		return (0);
415 	default:
416 		break;
417 	}
418 
419 	/* Read the input. */
420 	CIRCLEQ_FOREACH(sp, &gp->dq, q)
421 		if (F_ISSET(sp, SC_SCRIPT) &&
422 		    FD_ISSET(sp->script->sh_master, rdfd) && sscr_insert(sp)) {
423 			free(rdfd);
424 			return (1);
425 		}
426 	goto loop;
427 }
428 
429 /*
430  * sscr_insert --
431  *	Take a line from the shell and insert it into the file.
432  */
433 static int
434 sscr_insert(sp)
435 	SCR *sp;
436 {
437 	CHAR_T *endp, *p, *t;
438 	SCRIPT *sc;
439 	struct pollfd pfd[1];
440 	recno_t lno;
441 	size_t blen, len, tlen;
442 	u_int value;
443 	int nr, rval;
444 	char *bp;
445 
446 	/* Find out where the end of the file is. */
447 	if (db_last(sp, &lno))
448 		return (1);
449 
450 #define	MINREAD	1024
451 	GET_SPACE_RET(sp, bp, blen, MINREAD);
452 	endp = bp;
453 
454 	/* Read the characters. */
455 	rval = 1;
456 	sc = sp->script;
457 more:	switch (nr = read(sc->sh_master, endp, MINREAD)) {
458 	case  0:			/* EOF; shell just exited. */
459 		sscr_end(sp);
460 		rval = 0;
461 		goto ret;
462 	case -1:			/* Error or interrupt. */
463 		msgq(sp, M_SYSERR, "shell");
464 		goto ret;
465 	default:
466 		endp += nr;
467 		break;
468 	}
469 
470 	/* Append the lines into the file. */
471 	for (p = t = bp; p < endp; ++p) {
472 		value = KEY_VAL(sp, *p);
473 		if (value == K_CR || value == K_NL) {
474 			len = p - t;
475 			if (db_append(sp, 1, lno++, t, len))
476 				goto ret;
477 			t = p + 1;
478 		}
479 	}
480 	if (p > t) {
481 		len = p - t;
482 		/*
483 		 * If the last thing from the shell isn't another prompt, wait
484 		 * up to 1/10 of a second for more stuff to show up, so that
485 		 * we don't break the output into two separate lines.  Don't
486 		 * want to hang indefinitely because some program is hanging,
487 		 * confused the shell, or whatever.
488 		 */
489 		if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) {
490 			pfd[0].fd = sc->sh_master;
491 			pfd[0].events = POLLIN;
492 			if (poll(pfd, 1, 100) > 0) {
493 				memmove(bp, t, len);
494 				endp = bp + len;
495 				goto more;
496 			}
497 		}
498 		if (sscr_setprompt(sp, t, len))
499 			return (1);
500 		if (db_append(sp, 1, lno++, t, len))
501 			goto ret;
502 	}
503 
504 	/* The cursor moves to EOF. */
505 	sp->lno = lno;
506 	sp->cno = len ? len - 1 : 0;
507 	rval = vs_refresh(sp, 1);
508 
509 ret:	FREE_SPACE(sp, bp, blen);
510 	return (rval);
511 }
512 
513 /*
514  * sscr_setprompt --
515  *
516  * Set the prompt to the last line we got from the shell.
517  *
518  */
519 static int
520 sscr_setprompt(sp, buf, len)
521 	SCR *sp;
522 	char *buf;
523 	size_t len;
524 {
525 	SCRIPT *sc;
526 
527 	sc = sp->script;
528 	if (sc->sh_prompt)
529 		free(sc->sh_prompt);
530 	MALLOC(sp, sc->sh_prompt, char *, len + 1);
531 	if (sc->sh_prompt == NULL) {
532 		sscr_end(sp);
533 		return (1);
534 	}
535 	memmove(sc->sh_prompt, buf, len);
536 	sc->sh_prompt_len = len;
537 	sc->sh_prompt[len] = '\0';
538 	return (0);
539 }
540 
541 /*
542  * sscr_matchprompt --
543  *	Check to see if a line matches the prompt.  Nul's indicate
544  *	parts that can change, in both content and size.
545  */
546 static int
547 sscr_matchprompt(sp, lp, line_len, lenp)
548 	SCR *sp;
549 	char *lp;
550 	size_t line_len, *lenp;
551 {
552 	SCRIPT *sc;
553 	size_t prompt_len;
554 	char *pp;
555 
556 	sc = sp->script;
557 	if (line_len < (prompt_len = sc->sh_prompt_len))
558 		return (0);
559 
560 	for (pp = sc->sh_prompt;
561 	    prompt_len && line_len; --prompt_len, --line_len) {
562 		if (*pp == '\0') {
563 			for (; prompt_len && *pp == '\0'; --prompt_len, ++pp);
564 			if (!prompt_len)
565 				return (0);
566 			for (; line_len && *lp != *pp; --line_len, ++lp);
567 			if (!line_len)
568 				return (0);
569 		}
570 		if (*pp++ != *lp++)
571 			break;
572 	}
573 
574 	if (prompt_len)
575 		return (0);
576 	if (lenp != NULL)
577 		*lenp = line_len;
578 	return (1);
579 }
580 
581 /*
582  * sscr_end --
583  *	End the pipe to a shell.
584  *
585  * PUBLIC: int sscr_end(SCR *);
586  */
587 int
588 sscr_end(sp)
589 	SCR *sp;
590 {
591 	SCRIPT *sc;
592 
593 	if ((sc = sp->script) == NULL)
594 		return (0);
595 
596 	/* Turn off the script flags. */
597 	F_CLR(sp, SC_SCRIPT);
598 	sscr_check(sp);
599 
600 	/* Close down the parent's file descriptors. */
601 	if (sc->sh_master != -1)
602 	    (void)close(sc->sh_master);
603 	if (sc->sh_slave != -1)
604 	    (void)close(sc->sh_slave);
605 
606 	/* This should have killed the child. */
607 	(void)proc_wait(sp, sc->sh_pid, "script-shell", 0, 0);
608 
609 	/* Free memory. */
610 	free(sc->sh_prompt);
611 	free(sc);
612 	sp->script = NULL;
613 
614 	return (0);
615 }
616 
617 /*
618  * sscr_check --
619  *	Set/clear the global scripting bit.
620  */
621 static void
622 sscr_check(sp)
623 	SCR *sp;
624 {
625 	GS *gp;
626 
627 	gp = sp->gp;
628 	CIRCLEQ_FOREACH(sp, &gp->dq, q)
629 		if (F_ISSET(sp, SC_SCRIPT)) {
630 			F_SET(gp, G_SCRWIN);
631 			return;
632 		}
633 	F_CLR(gp, G_SCRWIN);
634 }
635 
636 #ifdef HAVE_SYS5_PTY
637 static int ptys_open(int, char *);
638 static int ptym_open(char *, size_t);
639 
640 static int
641 sscr_pty(amaster, aslave, name, namelen, termp, winp)
642 	int *amaster, *aslave;
643 	char *name;
644 	size_t namelen;
645 	struct termios *termp;
646 	void *winp;
647 {
648 	int master, slave, ttygid;
649 
650 	/* open master terminal */
651 	if ((master = ptym_open(name, namelen)) < 0)  {
652 		errno = ENOENT;	/* out of ptys */
653 		return (-1);
654 	}
655 
656 	/* open slave terminal */
657 	if ((slave = ptys_open(master, name)) >= 0) {
658 		*amaster = master;
659 		*aslave = slave;
660 	} else {
661 		errno = ENOENT;	/* out of ptys */
662 		return (-1);
663 	}
664 
665 	if (termp)
666 		(void) tcsetattr(slave, TCSAFLUSH, termp);
667 #ifdef TIOCSWINSZ
668 	if (winp != NULL)
669 		(void) ioctl(slave, TIOCSWINSZ, (struct winsize *)winp);
670 #endif
671 	return (0);
672 }
673 
674 /*
675  * ptym_open --
676  *	This function opens a master pty and returns the file descriptor
677  *	to it.  pts_name is also returned which is the name of the slave.
678  */
679 static int
680 ptym_open(pts_name, pts_namelen)
681 	char *pts_name;
682 	size_t pts_namelen;
683 {
684 	int fdm;
685 	char *ptr, *ptsname();
686 
687 	strlcpy(pts_name, _PATH_SYSV_PTY, pts_namelen);
688 	if ((fdm = open(pts_name, O_RDWR)) < 0 )
689 		return (-1);
690 
691 	if (grantpt(fdm) < 0) {
692 		close(fdm);
693 		return (-2);
694 	}
695 
696 	if (unlockpt(fdm) < 0) {
697 		close(fdm);
698 		return (-3);
699 	}
700 
701 	if (unlockpt(fdm) < 0) {
702 		close(fdm);
703 		return (-3);
704 	}
705 
706 	/* get slave's name */
707 	if ((ptr = ptsname(fdm)) == NULL) {
708 		close(fdm);
709 		return (-3);
710 	}
711 	strlcpy(pts_name, ptr, pts_namelen);
712 	return (fdm);
713 }
714 
715 /*
716  * ptys_open --
717  *	This function opens the slave pty.
718  */
719 static int
720 ptys_open(fdm, pts_name)
721 	int fdm;
722 	char *pts_name;
723 {
724 	int fds;
725 
726 	if ((fds = open(pts_name, O_RDWR)) < 0) {
727 		close(fdm);
728 		return (-5);
729 	}
730 
731 	if (ioctl(fds, I_PUSH, "ptem") < 0) {
732 		close(fds);
733 		close(fdm);
734 		return (-6);
735 	}
736 
737 	if (ioctl(fds, I_PUSH, "ldterm") < 0) {
738 		close(fds);
739 		close(fdm);
740 		return (-7);
741 	}
742 
743 	if (ioctl(fds, I_PUSH, "ttcompat") < 0) {
744 		close(fds);
745 		close(fdm);
746 		return (-8);
747 	}
748 
749 	return (fds);
750 }
751 
752 #else /* !HAVE_SYS5_PTY */
753 
754 static int
755 sscr_pty(amaster, aslave, name, namelen, termp, winp)
756 	int *amaster, *aslave;
757 	char *name;
758 	size_t namelen;
759 	struct termios *termp;
760 	void *winp;
761 {
762 	static char line[] = "/dev/ptyXX";
763 	char *cp1, *cp2;
764 	int master, slave, ttygid;
765 	struct group *gr;
766 
767 	if ((gr = getgrnam("tty")) != NULL)
768 		ttygid = gr->gr_gid;
769 	else
770 		ttygid = -1;
771 
772 	for (cp1 = "pqrs"; *cp1; cp1++) {
773 		line[8] = *cp1;
774 		for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
775 			line[5] = 'p';
776 			line[9] = *cp2;
777 			if ((master = open(line, O_RDWR, 0)) == -1) {
778 				if (errno == ENOENT)
779 					return (-1);	/* out of ptys */
780 			} else {
781 				line[5] = 't';
782 				(void) chown(line, getuid(), ttygid);
783 				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
784 #ifdef HAVE_REVOKE
785 				(void) revoke(line);
786 #endif
787 				if ((slave = open(line, O_RDWR, 0)) != -1) {
788 					*amaster = master;
789 					*aslave = slave;
790 					if (name)
791 						strlcpy(name, line, namelen);
792 					if (termp)
793 						(void) tcsetattr(slave,
794 							TCSAFLUSH, termp);
795 #ifdef TIOCSWINSZ
796 					if (winp)
797 						(void) ioctl(slave, TIOCSWINSZ,
798 							(char *)winp);
799 #endif
800 					return (0);
801 				}
802 				(void) close(master);
803 			}
804 		}
805 	}
806 	errno = ENOENT;	/* out of ptys */
807 	return (-1);
808 }
809 #endif /* HAVE_SYS5_PTY */
810