OSDN Git Service

Hide mostly used functions
[uclinux-h8/uClibc.git] / libc / stdlib / bsd_getpt.c
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <unistd.h>
24
25
26 /* Prefix for master pseudo terminal nodes.  */
27 #define _PATH_PTY "/dev/pty"
28
29
30 /* Letters indicating a series of pseudo terminals.  */
31 #ifndef PTYNAME1
32 #define PTYNAME1 "pqrsPQRS"
33 #endif
34 const char _ptyname1[] = PTYNAME1;
35
36 /* Letters indicating the position within a series.  */
37 #ifndef PTYNAME2
38 #define PTYNAME2 "0123456789abcdefghijklmnopqrstuv";
39 #endif
40 const char _ptyname2[] = PTYNAME2;
41
42
43 /* Open a master pseudo terminal and return its file descriptor.  */
44 int
45 __getpt (void)
46 {
47   char buf[sizeof (_PATH_PTY) + 2];
48   const char *p, *q;
49   char *s;
50
51   __memcpy (buf, _PATH_PTY, sizeof (_PATH_PTY));
52   s = buf + __strlen (buf);
53
54   /* s[0] and s[1] will be filled in the loop.  */
55   s[2] = '\0';
56
57   for (p = _ptyname1; *p != '\0'; ++p)
58     {
59       s[0] = *p;
60
61       for (q = _ptyname2; *q != '\0'; ++q)
62         {
63           int fd;
64
65           s[1] = *q;
66
67           fd = __open (buf, O_RDWR);
68           if (fd != -1)
69             return fd;
70
71           if (errno == ENOENT)
72             return -1;
73         }
74     }
75
76   errno = ENOENT;
77   return -1;
78 }