xref: /original-bsd/usr.bin/window/wwpty.c (revision 4d072710)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)wwpty.c	3.15 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 #include "ww.h"
23 
24 wwgetpty(w)
25 register struct ww *w;
26 {
27 	register char c, *p;
28 	int tty;
29 	int on = 1;
30 #define PTY "/dev/XtyXX"
31 #define _PT	5
32 #define _PQRS	8
33 #define _0_9	9
34 
35 	(void) strcpy(w->ww_ttyname, PTY);
36 	for (c = 'p'; c <= 'u'; c++) {
37 		w->ww_ttyname[_PT] = 'p';
38 		w->ww_ttyname[_PQRS] = c;
39 		w->ww_ttyname[_0_9] = '0';
40 		if (access(w->ww_ttyname, 0) < 0)
41 			break;
42 		for (p = "0123456789abcdef"; *p; p++) {
43 			w->ww_ttyname[_PT] = 'p';
44 			w->ww_ttyname[_0_9] = *p;
45 			if ((w->ww_pty = open(w->ww_ttyname, 2)) < 0)
46 				continue;
47 			w->ww_ttyname[_PT] = 't';
48 			if ((tty = open(w->ww_ttyname, 2)) < 0) {
49 				(void) close(w->ww_pty);
50 				continue;
51 			}
52 			(void) close(tty);
53 			if (ioctl(w->ww_pty, TIOCPKT, (char *)&on) < 0) {
54 				(void) close(w->ww_pty);
55 				continue;
56 			}
57 			return 0;
58 		}
59 	}
60 	w->ww_pty = -1;
61 	wwerrno = WWE_NOPTY;
62 	return -1;
63 }
64