1 /* Copyright (C) 1992-2021 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 #include <sysdep.h>
25 
26 /* The difference here is that the termios structure used in the
27    kernel is not the same as we use in the libc.  Therefore we must
28    translate it here.  */
29 #include <kernel_termios.h>
30 
31 /* Put the state of FD into *TERMIOS_P.  */
32 int
__tcgetattr(int fd,struct termios * termios_p)33 __tcgetattr (int fd, struct termios *termios_p)
34 {
35   struct __kernel_termios k_termios;
36   int retval;
37 
38   retval = INLINE_SYSCALL (ioctl, 3, fd, TCGETS, &k_termios);
39 
40   if (__glibc_likely (retval == 0))
41     {
42       termios_p->c_iflag = k_termios.c_iflag;
43       termios_p->c_oflag = k_termios.c_oflag;
44       termios_p->c_cflag = k_termios.c_cflag;
45       termios_p->c_lflag = k_termios.c_lflag;
46       termios_p->c_line = k_termios.c_line;
47 #if _HAVE_STRUCT_TERMIOS_C_ISPEED
48 # if _HAVE_C_ISPEED
49       termios_p->c_ispeed = k_termios.c_ispeed;
50 # else
51       termios_p->c_ispeed = k_termios.c_cflag & (CBAUD | CBAUDEX);
52 # endif
53 #endif
54 #if _HAVE_STRUCT_TERMIOS_C_OSPEED
55 # if _HAVE_C_OSPEED
56       termios_p->c_ospeed = k_termios.c_ospeed;
57 # else
58       termios_p->c_ospeed = k_termios.c_cflag & (CBAUD | CBAUDEX);
59 # endif
60 #endif
61       if (sizeof (cc_t) == 1 || _POSIX_VDISABLE == 0
62 	  || (unsigned char) _POSIX_VDISABLE == (unsigned char) -1)
63 	memset (__mempcpy (&termios_p->c_cc[0], &k_termios.c_cc[0],
64 			   __KERNEL_NCCS * sizeof (cc_t)),
65 		_POSIX_VDISABLE, (NCCS - __KERNEL_NCCS) * sizeof (cc_t));
66       else
67 	{
68 	  memcpy (&termios_p->c_cc[0], &k_termios.c_cc[0],
69 		  __KERNEL_NCCS * sizeof (cc_t));
70 
71 	  for (size_t cnt = __KERNEL_NCCS; cnt < NCCS; ++cnt)
72 	    termios_p->c_cc[cnt] = _POSIX_VDISABLE;
73 	}
74     }
75 
76   return retval;
77 }
78 
79 libc_hidden_def (__tcgetattr)
80 weak_alias (__tcgetattr, tcgetattr)
81