xref: /original-bsd/usr.bin/window/wwpty.c (revision 76210d32)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)wwpty.c	3.18 (Berkeley) 06/06/90";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #ifdef POSIX_TTY
17 #include <sys/ioctl.h>
18 #endif
19 
20 wwgetpty(w)
21 register struct ww *w;
22 {
23 	register char c, *p;
24 	int tty;
25 	int on = 1;
26 #define PTY "/dev/XtyXX"
27 #define _PT	5
28 #define _PQRS	8
29 #define _0_9	9
30 
31 	(void) strcpy(w->ww_ttyname, PTY);
32 	for (c = 'p'; c <= 'u'; c++) {
33 		w->ww_ttyname[_PT] = 'p';
34 		w->ww_ttyname[_PQRS] = c;
35 		w->ww_ttyname[_0_9] = '0';
36 		if (access(w->ww_ttyname, 0) < 0)
37 			break;
38 		for (p = "0123456789abcdef"; *p; p++) {
39 			w->ww_ttyname[_PT] = 'p';
40 			w->ww_ttyname[_0_9] = *p;
41 			if ((w->ww_pty = open(w->ww_ttyname, 2)) < 0)
42 				continue;
43 			w->ww_ttyname[_PT] = 't';
44 			if ((tty = open(w->ww_ttyname, 2)) < 0) {
45 				(void) close(w->ww_pty);
46 				continue;
47 			}
48 			(void) close(tty);
49 			if (ioctl(w->ww_pty, TIOCPKT, (char *)&on) < 0) {
50 				(void) close(w->ww_pty);
51 				continue;
52 			}
53 			return 0;
54 		}
55 	}
56 	w->ww_pty = -1;
57 	wwerrno = WWE_NOPTY;
58 	return -1;
59 }
60