port of netbsd's tr
[minix.git] / commands / telnetd / pty.c
blob0900a2cd0ace63004dc413e2c23bd127d0d1f5c3
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.
7 * Handle the allocation of a PTY.
9 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
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"
21 #define DEV_DIR "/dev"
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 :-(
28 int get_pty(pty_fdp, tty_namep)
29 int *pty_fdp;
30 char **tty_namep;
32 char buff[128], temp[128];
33 register int i, j;
34 int pty_fd;
35 static char tty_name[128];
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);
43 if (opt_d == 1) {
44 (void) write(2, "Testing: ", 9);
45 (void) write(2, buff, strlen(buff));
46 (void) write(2, "...: ", 5);
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));
56 if (pty_fd >= 0) break;
58 j++;
59 if (j == 16) break;
60 } while(1);
62 /* Did we find one? */
63 if (j < 16) break;
65 if (pty_fd < 0) return(-1);
67 if (opt_d == 1) {
68 sprintf(temp, "File %s, desc %d\n", buff, pty_fd);
69 (void) write(1, temp, strlen(temp));
72 sprintf(tty_name, "%s/tty%c%c", DEV_DIR,
73 i, (j < 10) ? j + '0' : j + 'a' - 10);
75 *pty_fdp = pty_fd;
76 *tty_namep = tty_name;
77 return(0);