<sys/file.h> => <sys/types.h> + <fcntl.h>
[unix-history] / usr / src / usr.bin / window / wwtty.c
CommitLineData
60de5df9 1/*
46e9ea25
KB
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
3dd3a9e5
KB
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
7 *
87f529ec 8 * %sccs.include.redist.c%
60de5df9
EW
9 */
10
46e9ea25 11#ifndef lint
9de86788 12static char sccsid[] = "@(#)wwtty.c 3.17 (Berkeley) %G%";
46e9ea25
KB
13#endif /* not lint */
14
b94626a3 15#include "ww.h"
b1189050 16#include <fcntl.h>
9de86788
EW
17#if !defined(OLD_TTY) && !defined(TIOCGWINSZ)
18#include <sys/ioctl.h>
19#endif
b94626a3
EW
20
21wwgettty(d, t)
22register struct ww_tty *t;
23{
9de86788 24#ifdef OLD_TTY
20029082 25 if (ioctl(d, TIOCGETP, (char *)&t->ww_sgttyb) < 0)
03e75950 26 goto bad;
20029082 27 if (ioctl(d, TIOCGETC, (char *)&t->ww_tchars) < 0)
03e75950 28 goto bad;
20029082 29 if (ioctl(d, TIOCGLTC, (char *)&t->ww_ltchars) < 0)
03e75950 30 goto bad;
20029082 31 if (ioctl(d, TIOCLGET, (char *)&t->ww_lmode) < 0)
03e75950 32 goto bad;
20029082 33 if (ioctl(d, TIOCGETD, (char *)&t->ww_ldisc) < 0)
03e75950 34 goto bad;
bbbb7b14
EW
35#else
36 if (tcgetattr(d, &t->ww_termios) < 0)
37 goto bad;
38#endif
b1189050
EW
39 if ((t->ww_fflags = fcntl(d, F_GETFL, 0)) < 0)
40 goto bad;
b94626a3 41 return 0;
03e75950
EW
42bad:
43 wwerrno = WWE_SYS;
44 return -1;
b94626a3
EW
45}
46
4cbd8755
EW
47/*
48 * Set the modes of tty 'd' to 't'
49 * 'o' is the current modes. We set the line discipline only if
50 * it changes, to avoid unnecessary flushing of typeahead.
51 */
9de86788
EW
52wwsettty(d, t)
53register struct ww_tty *t;
b94626a3 54{
9de86788 55#ifdef OLD_TTY
bbbb7b14 56 int i;
9de86788
EW
57
58 /* XXX, for buggy tty drivers that don't wait for output to drain */
bbbb7b14
EW
59 while (ioctl(d, TIOCOUTQ, &i) >= 0 && i > 0)
60 usleep(100000);
61 if (ioctl(d, TIOCSETN, (char *)&t->ww_sgttyb) < 0)
03e75950 62 goto bad;
20029082 63 if (ioctl(d, TIOCSETC, (char *)&t->ww_tchars) < 0)
03e75950 64 goto bad;
20029082 65 if (ioctl(d, TIOCSLTC, (char *)&t->ww_ltchars) < 0)
03e75950 66 goto bad;
20029082 67 if (ioctl(d, TIOCLSET, (char *)&t->ww_lmode) < 0)
03e75950 68 goto bad;
9de86788
EW
69 if (ioctl(d, TIOCGETD, (char *)&i) < 0)
70 goto bad;
71 if (t->ww_ldisc != i &&
20029082 72 ioctl(d, TIOCSETD, (char *)&t->ww_ldisc) < 0)
03e75950 73 goto bad;
bbbb7b14 74#else
9de86788
EW
75#ifdef sun
76 /* XXX, for buggy tty drivers that don't wait for output to drain */
77 (void) tcdrain(d);
78#endif
bbbb7b14
EW
79 if (tcsetattr(d, TCSADRAIN, &t->ww_termios) < 0)
80 goto bad;
81#endif
b1189050
EW
82 if (fcntl(d, F_SETFL, t->ww_fflags) < 0)
83 goto bad;
b94626a3 84 return 0;
03e75950
EW
85bad:
86 wwerrno = WWE_SYS;
87 return -1;
b94626a3 88}
9de86788
EW
89
90/*
91 * The ttysize and stop-start routines must also work
92 * on the control side of pseudoterminals.
93 */
94
95wwgetttysize(d, r, c)
96 int *r, *c;
97{
98 struct winsize winsize;
99
100 if (ioctl(d, TIOCGWINSZ, (char *)&winsize) < 0) {
101 wwerrno = WWE_SYS;
102 return -1;
103 }
104 if (winsize.ws_row != 0)
105 *r = winsize.ws_row;
106 if (winsize.ws_col != 0)
107 *c = winsize.ws_col;
108 return 0;
109}
110
111wwsetttysize(d, r, c)
112{
113 struct winsize winsize;
114
115 winsize.ws_row = r;
116 winsize.ws_col = c;
117 winsize.ws_xpixel = winsize.ws_ypixel = 0;
118 if (ioctl(d, TIOCSWINSZ, (char *)&winsize) < 0) {
119 wwerrno = WWE_SYS;
120 return -1;
121 }
122 return 0;
123}
124
125wwstoptty(d)
126{
127#if !defined(OLD_TTY) && defined(TCOOFF)
128 /* not guaranteed to work on the pty side */
129 if (tcflow(d, TCOOFF) < 0)
130#else
131 if (ioctl(d, TIOCSTOP, (char *)0) < 0)
132#endif
133 {
134 wwerrno = WWE_SYS;
135 return -1;
136 }
137 return 0;
138}
139
140wwstarttty(d)
141{
142#if !defined(OLD_TTY) && defined(TCOON)
143 /* not guaranteed to work on the pty side */
144 if (tcflow(d, TCOON) < 0)
145#else
146 if (ioctl(d, TIOCSTART, (char *)0) < 0)
147#endif
148 {
149 wwerrno = WWE_SYS;
150 return -1;
151 }
152 return 0;
153}