.
[glibc/history.git] / sysdeps / unix / bsd / tcgetattr.c
blob5743ffa4b5014346202c0123af12e9cd81e1cd1e
1 /* Copyright (C) 1991, 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stddef.h>
21 #include <termios.h>
23 #include "bsdtty.h"
25 extern const speed_t __bsd_speeds[]; /* Defined in tcsetattr.c. */
27 /* Put the state of FD into *TERMIOS_P. */
28 int
29 __tcgetattr (fd, termios_p)
30 int fd;
31 struct termios *termios_p;
33 struct sgttyb buf;
34 struct tchars tchars;
35 struct ltchars ltchars;
36 int local;
37 #ifdef TIOCGETX
38 int extra;
39 #endif
41 if (termios_p == NULL)
43 __set_errno (EINVAL);
44 return -1;
47 if (__ioctl(fd, TIOCGETP, &buf) < 0 ||
48 __ioctl(fd, TIOCGETC, &tchars) < 0 ||
49 __ioctl(fd, TIOCGLTC, &ltchars) < 0 ||
50 #ifdef TIOCGETX
51 __ioctl(fd, TIOCGETX, &extra) < 0 ||
52 #endif
53 __ioctl(fd, TIOCLGET, &local) < 0)
54 return -1;
56 termios_p->__ispeed = __bsd_speeds[(unsigned char) buf.sg_ispeed];
57 termios_p->__ospeed = __bsd_speeds[(unsigned char) buf.sg_ospeed];
59 termios_p->c_iflag = 0;
60 termios_p->c_oflag = 0;
61 termios_p->c_cflag = 0;
62 termios_p->c_lflag = 0;
63 termios_p->c_oflag |= CREAD | HUPCL;
64 #ifdef LPASS8
65 if (local & LPASS8)
66 termios_p->c_oflag |= CS8;
67 else
68 #endif
69 termios_p->c_oflag |= CS7;
70 if (!(buf.sg_flags & RAW))
72 termios_p->c_iflag |= IXON;
73 termios_p->c_cflag |= OPOST;
74 #ifndef NOISIG
75 termios_p->c_lflag |= ISIG;
76 #endif
78 if ((buf.sg_flags & (CBREAK|RAW)) == 0)
79 termios_p->c_lflag |= ICANON;
80 if (!(buf.sg_flags & RAW) && !(local & LLITOUT))
81 termios_p->c_oflag |= OPOST;
82 if (buf.sg_flags & CRMOD)
83 termios_p->c_iflag |= ICRNL;
84 if (buf.sg_flags & TANDEM)
85 termios_p->c_iflag |= IXOFF;
86 #ifdef TIOCGETX
87 if (!(extra & NOISIG))
88 termios_p->c_lflag |= ISIG;
89 if (extra & STOPB)
90 termios_p->c_cflag |= CSTOPB;
91 #endif
93 switch (buf.sg_flags & (EVENP|ODDP))
95 case EVENP|ODDP:
96 break;
97 case ODDP:
98 termios_p->c_cflag |= PARODD;
99 default:
100 termios_p->c_cflag |= PARENB;
101 termios_p->c_iflag |= IGNPAR | INPCK;
102 break;
104 if (buf.sg_flags & ECHO)
105 termios_p->c_lflag |= _ECHO;
106 if (local & LCRTERA)
107 termios_p->c_lflag |= ECHOE;
108 if (local & LCRTKIL)
109 termios_p->c_lflag |= ECHOK;
110 if (local & LTOSTOP)
111 termios_p->c_lflag |= _TOSTOP;
112 if (local & LNOFLSH)
113 termios_p->c_lflag |= _NOFLSH;
115 termios_p->c_cc[VEOF] = tchars.t_eofc;
116 termios_p->c_cc[VEOL] = '\n';
117 termios_p->c_cc[VERASE] = buf.sg_erase;
118 termios_p->c_cc[VKILL] = buf.sg_kill;
119 termios_p->c_cc[VINTR] = tchars.t_intrc;
120 termios_p->c_cc[VQUIT] = tchars.t_quitc;
121 termios_p->c_cc[VSTART] = tchars.t_startc;
122 termios_p->c_cc[VSTOP] = tchars.t_stopc;
123 termios_p->c_cc[VSUSP] = ltchars.t_suspc;
124 termios_p->c_cc[VMIN] = -1;
125 termios_p->c_cc[VTIME] = -1;
127 return 0;
130 weak_alias (__tcgetattr, tcgetattr)