1 /* $Id: pty.c,v 1.6 2002/04/24 13:59:02 bsd Exp $ */
2
3 /*
4 * Copyright 2000, 2001, 2002 Brian S. Dean <bsd@bsdhome.com>
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY BRIAN S. DEAN ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRIAN S. DEAN BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 * DAMAGE.
29 *
30 */
31
32 #include <fcntl.h>
33 #include <string.h>
34
35
36 char * pty_versionid = "$Id: pty.c,v 1.6 2002/04/24 13:59:02 bsd Exp $";
37
38
allocate_pty(int * bank,int * unit)39 int allocate_pty(int * bank, int * unit)
40 {
41 int fd;
42 char pty [ 32 ];
43 char banks[] = "ZYXWVUTSRQPzyxwvutsrqp";
44 char units[] = "vutsrqponmlkjihgfedcba9876543210";
45 int b, u, bno, uno;
46
47 bno = 8;
48 uno = 9;
49
50 strncpy(pty, "/dev/ptyp0", sizeof(pty)-1);
51
52 for (b=0; b<sizeof(banks); b++) {
53 for (u=0; u<sizeof(units); u++) {
54 pty[bno] = banks[b];
55 pty[uno] = units[u];
56 fd = open(pty, O_RDWR|O_NONBLOCK);
57 if (fd >= 0) {
58 *bank = banks[b];
59 *unit = units[u];
60 return fd;
61 }
62 }
63 }
64
65 return -1;
66 }
67
68