forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix.c
More file actions
executable file
·226 lines (199 loc) · 4.74 KB
/
Copy pathunix.c
File metadata and controls
executable file
·226 lines (199 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* unix.c */
/* Author:
* Steve Kirkendall
* 14407 SW Teal Blvd. #C
* Beaverton, OR 97005
* kirkenda@cs.pdx.edu
*/
/* This file contains the unix-specific versions the ttyread() functions.
* There are actually three versions of ttyread() defined here, because
* BSD, SysV, and V7 all need quite different implementations.
*/
#include "config.h"
#if ANY_UNIX
# include "vi.h"
# if BSD
/* For BSD, we use select() to wait for characters to become available,
* and then do a read() to actually get the characters. We also try to
* handle SIGWINCH -- if the signal arrives during the select() call, then
* we adjust the o_columns and o_lines variables, and fake a control-L.
*/
# include <sys/types.h>
# include <sys/time.h>
int ttyread(buf, len, time)
char *buf; /* where to store the gotten characters */
int len; /* maximum number of characters to read */
int time; /* maximum time to allow for reading */
{
fd_set rd; /* the file descriptors that we want to read from */
static tty; /* 'y' if reading from tty, or 'n' if not a tty */
int i;
struct timeval t;
struct timeval *tp;
/* do we know whether this is a tty or not? */
if (!tty)
{
tty = (isatty(0) ? 'y' : 'n');
}
/* compute the timeout value */
if (time)
{
t.tv_sec = time / 10;
t.tv_usec = (time % 10) * 100000L;
tp = &t;
}
else
{
tp = (struct timeval *)0;
}
/* loop until we get characters or a definite EOF */
for (;;)
{
if (tty == 'y')
{
/* wait until timeout or characters are available */
FD_ZERO(&rd);
FD_SET(0, &rd);
i = select(1, &rd, (fd_set *)0, (fd_set *)0, tp);
}
else
{
/* if reading from a file or pipe, never timeout!
* (This also affects the way that EOF is detected)
*/
i = 1;
}
/* react accordingly... */
switch (i)
{
case -1: /* assume we got an EINTR because of SIGWINCH */
if (*o_lines != LINES || *o_columns != COLS)
{
*o_lines = LINES;
*o_columns = COLS;
#ifndef CRUNCH
if (!wset)
{
*o_window = LINES - 1;
}
#endif
if (mode != MODE_EX)
{
/* pretend the user hit ^L */
*buf = ctrl('L');
return 1;
}
}
break;
case 0: /* timeout */
return 0;
default: /* characters available */
return read(0, buf, len);
}
}
}
# else
# if M_SYSV
/* For System-V or Coherent, we use VMIN/VTIME to implement the timeout.
* For no timeout, VMIN should be 1 and VTIME should be 0; for timeout,
* VMIN should be 0 and VTIME should be the timeout value.
*/
# include <termio.h>
int ttyread(buf, len, time)
char *buf; /* where to store the gotten characters */
int len; /* maximum number of characters to read */
int time; /* maximum time to allow for reading */
{
struct termio tio;
int bytes; /* number of bytes actually read */
/* arrange for timeout */
ioctl(0, TCGETA, &tio);
if (time)
{
tio.c_cc[VMIN] = 0;
tio.c_cc[VTIME] = time;
}
else
{
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
}
ioctl(0, TCSETA, &tio);
/* Perform the read. Loop if EINTR error happens */
while ((bytes = read(0, buf, len)) < 0)
{
/* probably EINTR error because a SIGWINCH was received */
if (*o_lines != LINES || *o_columns != COLS)
{
*o_lines = LINES;
*o_columns = COLS;
#ifndef CRUNCH
if (!wset)
{
*o_window = LINES - 1;
}
#endif
if (mode != MODE_EX)
{
/* pretend the user hit ^L */
*buf = ctrl('L');
return 1;
}
}
}
/* return the number of bytes read */
return bytes;
/* NOTE: The terminal may be left in a timeout-mode after this function
* returns. This shouldn't be a problem since Elvis *NEVER* tries to
* read from the keyboard except through this function.
*/
}
# else /* any other version of UNIX, assume it is V7 compatible */
/* For V7 UNIX (including Minix) we set an alarm() before doing a blocking
* read(), and assume that the SIGALRM signal will cause the read() function
* to give up.
*/
#include <setjmp.h>
static jmp_buf env;
/*ARGSUSED*/
int dummy(signo)
int signo;
{
longjmp(env, 1);
}
int ttyread(buf, len, time)
char *buf; /* where to store the gotten characters */
int len; /* maximum number of characters to read */
int time; /* maximum time to allow for reading */
{
/* arrange for timeout */
#if __GNUC__ || _ANSI
signal(SIGALRM, (void (*)()) dummy);
#else
signal(SIGALRM, dummy);
#endif
alarm(time);
/* perform the blocking read */
if (setjmp(env) == 0)
{
len = read(0, buf, len);
}
else /* I guess we timed out */
{
len = 0;
}
/* cancel the alarm */
#if _ANSI
signal(SIGALRM, (void (*)())dummy); /* work around a bug in Minix */
#else
signal(SIGALRM, dummy); /* work around a bug in Minix */
#endif
alarm(0);
/* return the number of bytes read */
if (len < 0)
len = 0;
return len;
}
# endif /* !(M_SYSV || COHERENT) */
# endif /* !BSD */
#endif /* ANY_UNIX */