blob: 2765221eb28f0813da78e1413979d23692ac5a2d [file] [log] [blame]
maxwen27116ba2015-08-14 21:41:28 +02001/*
2 * Copyright (C) 2014 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/* #ifndef BIONIC_L (implementation was made after BIONIC_L (l-preview) */
30
31#include <errno.h>
32#include <fcntl.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <sys/ioctl.h>
36#include <termios.h>
37#include <unistd.h>
38
39#include <fcntl.h>
40/* if this constant is not defined, we are
41 ttyname is not in bionic */
42#ifndef SPLICE_F_GIFT
43
44int posix_openpt(int flags) {
45 return open("/dev/ptmx", flags);
46}
47
48int getpt(void) {
49 return posix_openpt(O_RDWR|O_NOCTTY);
50}
51
52#ifndef __BIONIC__
53int grantpt(int) {
54 return 0;
55}
56#endif
57
58char* ptsname(int fd) {
59 static char buf[64];
60 return ptsname_r(fd, buf, sizeof(buf)) == 0 ? buf : NULL;
61}
62
63int ptsname_r(int fd, char* buf, size_t len) {
64 if (buf == NULL) {
65 errno = EINVAL;
66 return errno;
67 }
68
69 unsigned int pty_num;
70 if (ioctl(fd, TIOCGPTN, &pty_num) != 0) {
71 errno = ENOTTY;
72 return errno;
73 }
74
75 if (snprintf(buf, len, "/dev/pts/%u", pty_num) >= (int) len) {
76 errno = ERANGE;
77 return errno;
78 }
79
80 return 0;
81}
82
83int bb_ttyname_r(int fd, char* buf, size_t len) {
84 if (buf == NULL) {
85 errno = EINVAL;
86 return errno;
87 }
88
89 if (!isatty(fd)) {
90 return errno;
91 }
92
93 char path[64];
94 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
95
96 ssize_t count = readlink(path, buf, len);
97 if (count == -1) {
98 return errno;
99 }
100 if ((size_t) (count) == len) {
101 errno = ERANGE;
102 return errno;
103 }
104 buf[count] = '\0';
105 return 0;
106}
107
108char* bb_ttyname(int fd) {
109 static char buf[64];
110 return bb_ttyname_r(fd, buf, sizeof(buf)) == 0 ? buf : NULL;
111}
112
113int unlockpt(int fd) {
114 int unlock = 0;
115 return ioctl(fd, TIOCSPTLCK, &unlock);
116}
117
118#endif