xref: /original-bsd/lib/libc/gen/termios.c (revision 9b5efc43)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)termios.c	5.4 (Berkeley) 02/23/91";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <sys/errno.h>
14 #include <sys/ioctl.h>
15 #include <sys/tty.h>
16 #include <termios.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 int
21 tcgetattr(fd, t)
22 	int fd;
23 	struct termios *t;
24 {
25 
26 	return(ioctl(fd, TIOCGETA, t));
27 }
28 
29 int
30 tcsetattr(fd, opt, t)
31 	int fd, opt;
32 	const struct termios *t;
33 {
34 	struct termios localterm;
35 
36 	if (opt & TCSASOFT) {
37 		localterm = *t;
38 		localterm.c_cflag |= CIGNORE;
39 		t = &localterm;
40 		opt &= TCSASOFT;
41 	}
42 	if (opt == TCSANOW)
43 		return (ioctl(fd, TIOCSETA, t));
44 	else if (opt == TCSADRAIN)
45 		return (ioctl(fd, TIOCSETAW, t));
46 	return (ioctl(fd, TIOCSETAF, t));
47 }
48 
49 int
50 #if __STDC__
51 tcsetpgrp(int fd, pid_t pgrp)
52 #else
53 tcsetpgrp(fd, pgrp)
54 	int fd;
55 	pid_t pgrp;
56 #endif
57 {
58 	return(ioctl(fd, TIOCSPGRP, &pgrp));
59 }
60 
61 pid_t
62 tcgetpgrp(fd)
63 {
64 	pid_t pgrp;
65 
66 	if (ioctl(fd, TIOCGPGRP, &pgrp) < 0)
67 		return(-1);
68 	return(pgrp);
69 }
70 
71 speed_t
72 cfgetospeed(t)
73 	const struct termios *t;
74 {
75 	return(t->c_ospeed);
76 }
77 
78 speed_t
79 cfgetispeed(t)
80 	const struct termios *t;
81 {
82 	return(t->c_ispeed);
83 }
84 
85 int
86 cfsetospeed(t, speed)
87 	struct termios *t;
88 	speed_t speed;
89 {
90 	t->c_ospeed = speed;
91 	return 0;
92 }
93 
94 int
95 cfsetispeed(t, speed)
96 	struct termios *t;
97 	speed_t speed;
98 {
99 	t->c_ispeed = speed;
100 	return 0;
101 }
102 
103 void
104 cfsetspeed(t, speed)
105 	struct termios *t;
106 	speed_t speed;
107 {
108 	t->c_ispeed = t->c_ospeed = speed;
109 }
110 
111 /*
112  * Make a pre-existing termios structure into "raw" mode:
113  * character-at-a-time mode with no characters interpreted,
114  * 8-bit data path.
115  */
116 void
117 cfmakeraw(t)
118 	struct termios *t;
119 {
120 	t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
121 	t->c_oflag &= ~OPOST;
122 	t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
123 	t->c_cflag &= ~(CSIZE|PARENB);
124 	t->c_cflag |= CS8;
125 	/* set MIN/TIME */
126 }
127