xref: /original-bsd/usr.bin/window/wwpty.c (revision 811429ef)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 06/06/93";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #include <fcntl.h>
17 #if !defined(OLD_TTY) && !defined(TIOCPKT)
18 #include <sys/ioctl.h>
19 #endif
20 
21 wwgetpty(w)
22 register struct ww *w;
23 {
24 	register char c, *p;
25 	int tty;
26 	int on = 1;
27 #define PTY "/dev/XtyXX"
28 #define _PT	5
29 #define _PQRS	8
30 #define _0_9	9
31 
32 	(void) strcpy(w->ww_ttyname, PTY);
33 	for (c = 'p'; c <= 'u'; c++) {
34 		w->ww_ttyname[_PT] = 'p';
35 		w->ww_ttyname[_PQRS] = c;
36 		w->ww_ttyname[_0_9] = '0';
37 		if (access(w->ww_ttyname, 0) < 0)
38 			break;
39 		for (p = "0123456789abcdef"; *p; p++) {
40 			w->ww_ttyname[_PT] = 'p';
41 			w->ww_ttyname[_0_9] = *p;
42 			if ((w->ww_pty = open(w->ww_ttyname, 2)) < 0)
43 				continue;
44 			w->ww_ttyname[_PT] = 't';
45 			if ((tty = open(w->ww_ttyname, 2)) < 0) {
46 				(void) close(w->ww_pty);
47 				continue;
48 			}
49 			(void) close(tty);
50 			if (ioctl(w->ww_pty, TIOCPKT, (char *)&on) < 0) {
51 				(void) close(w->ww_pty);
52 				continue;
53 			}
54 			(void) fcntl(w->ww_pty, F_SETFD, 1);
55 			return 0;
56 		}
57 	}
58 	w->ww_pty = -1;
59 	wwerrno = WWE_NOPTY;
60 	return -1;
61 }
62