1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/ioctl.h>
34 #include <termios.h>
35 #include <unistd.h>
36
getpt(void)37 int getpt(void) {
38 return posix_openpt(O_RDWR|O_NOCTTY);
39 }
40
grantpt(int)41 int grantpt(int) {
42 return 0;
43 }
44
posix_openpt(int flags)45 int posix_openpt(int flags) {
46 return open("/dev/ptmx", flags);
47 }
48
ptsname(int fd)49 char* ptsname(int fd) {
50 static char buf[64];
51 return ptsname_r(fd, buf, sizeof(buf)) == 0 ? buf : NULL;
52 }
53
ptsname_r(int fd,char * buf,size_t len)54 int ptsname_r(int fd, char* buf, size_t len) {
55 if (buf == NULL) {
56 errno = EINVAL;
57 return errno;
58 }
59
60 unsigned int pty_num;
61 if (ioctl(fd, TIOCGPTN, &pty_num) != 0) {
62 errno = ENOTTY;
63 return errno;
64 }
65
66 if (snprintf(buf, len, "/dev/pts/%u", pty_num) >= static_cast<int>(len)) {
67 errno = ERANGE;
68 return errno;
69 }
70
71 return 0;
72 }
73
ttyname(int fd)74 char* ttyname(int fd) {
75 static char buf[64];
76 return ttyname_r(fd, buf, sizeof(buf)) == 0 ? buf : NULL;
77 }
78
ttyname_r(int fd,char * buf,size_t len)79 int ttyname_r(int fd, char* buf, size_t len) {
80 if (buf == NULL) {
81 errno = EINVAL;
82 return errno;
83 }
84
85 if (!isatty(fd)) {
86 return errno;
87 }
88
89 char path[64];
90 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
91
92 ssize_t count = readlink(path, buf, len);
93 if (count == -1) {
94 return errno;
95 }
96 if (static_cast<size_t>(count) == len) {
97 errno = ERANGE;
98 return errno;
99 }
100 buf[count] = '\0';
101 return 0;
102 }
103
unlockpt(int fd)104 int unlockpt(int fd) {
105 int unlock = 0;
106 return ioctl(fd, TIOCSPTLCK, &unlock);
107 }
108