1 | /*
|
---|
2 | * TNET A server program for MINIX which implements the TCP/IP
|
---|
3 | * suite of networking protocols. It is based on the
|
---|
4 | * TCP/IP code written by Phil Karn et al, as found in
|
---|
5 | * his NET package for Packet Radio communications.
|
---|
6 | *
|
---|
7 | * Handle the allocation of a PTY.
|
---|
8 | *
|
---|
9 | * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
|
---|
10 | */
|
---|
11 | #include <sys/types.h>
|
---|
12 | #include <errno.h>
|
---|
13 | #include <fcntl.h>
|
---|
14 | #include <stdlib.h>
|
---|
15 | #include <unistd.h>
|
---|
16 | #include <string.h>
|
---|
17 | #include <stdio.h>
|
---|
18 | #include "telnetd.h"
|
---|
19 |
|
---|
20 |
|
---|
21 | #define DEV_DIR "/dev"
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Allocate a PTY, by trying to open one repeatedly,
|
---|
25 | * until all PTY channels are done. If at that point
|
---|
26 | * no PTY is found, go into panic mode :-(
|
---|
27 | */
|
---|
28 | int get_pty(pty_fdp, tty_namep)
|
---|
29 | int *pty_fdp;
|
---|
30 | char **tty_namep;
|
---|
31 | {
|
---|
32 | char buff[128], temp[128];
|
---|
33 | register int i, j;
|
---|
34 | int pty_fd;
|
---|
35 | static char tty_name[128];
|
---|
36 |
|
---|
37 | for(i = 'p'; i < 'w'; i++) {
|
---|
38 | j = 0;
|
---|
39 | do {
|
---|
40 | sprintf(buff, "%s/pty%c%c",
|
---|
41 | DEV_DIR, i, (j < 10) ? j + '0' : j + 'a' - 10);
|
---|
42 |
|
---|
43 | if (opt_d == 1) {
|
---|
44 | (void) write(2, "Testing: ", 9);
|
---|
45 | (void) write(2, buff, strlen(buff));
|
---|
46 | (void) write(2, "...: ", 5);
|
---|
47 | }
|
---|
48 |
|
---|
49 | pty_fd = open(buff, O_RDWR);
|
---|
50 | if (opt_d == 1) {
|
---|
51 | if (pty_fd < 0) sprintf(temp, "error %d\r\n", errno);
|
---|
52 | else sprintf(temp, "OK\r\n");
|
---|
53 | (void) write(2, temp, strlen(temp));
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (pty_fd >= 0) break;
|
---|
57 |
|
---|
58 | j++;
|
---|
59 | if (j == 16) break;
|
---|
60 | } while(1);
|
---|
61 |
|
---|
62 | /* Did we find one? */
|
---|
63 | if (j < 16) break;
|
---|
64 | }
|
---|
65 | if (pty_fd < 0) return(-1);
|
---|
66 |
|
---|
67 | if (opt_d == 1) {
|
---|
68 | sprintf(temp, "File %s, desc %d\n", buff, pty_fd);
|
---|
69 | (void) write(1, temp, strlen(temp));
|
---|
70 | }
|
---|
71 |
|
---|
72 | sprintf(tty_name, "%s/tty%c%c", DEV_DIR,
|
---|
73 | i, (j < 10) ? j + '0' : j + 'a' - 10);
|
---|
74 |
|
---|
75 | *pty_fdp = pty_fd;
|
---|
76 | *tty_namep = tty_name;
|
---|
77 | return(0);
|
---|
78 | }
|
---|