Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / less / dist / os.c
blob676a986930184371f54c366cc630ee843e413f3f
1 /* $NetBSD: os.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Operating system dependent routines.
16 * Most of the stuff in here is based on Unix, but an attempt
17 * has been made to make things work on other operating systems.
18 * This will sometimes result in a loss of functionality, unless
19 * someone rewrites code specifically for the new operating system.
21 * The makefile provides defines to decide whether various
22 * Unix features are present.
25 #include "less.h"
26 #include <signal.h>
27 #include <setjmp.h>
28 #if HAVE_TIME_H
29 #include <time.h>
30 #endif
31 #if HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #if HAVE_VALUES_H
35 #include <values.h>
36 #endif
38 #if HAVE_TIME_T
39 #define time_type time_t
40 #else
41 #define time_type long
42 #endif
45 * BSD setjmp() saves (and longjmp() restores) the signal mask.
46 * This costs a system call or two per setjmp(), so if possible we clear the
47 * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
48 * On other systems, setjmp() doesn't affect the signal mask and so
49 * _setjmp() does not exist; we just use setjmp().
51 #if HAVE__SETJMP && HAVE_SIGSETMASK
52 #define SET_JUMP _setjmp
53 #define LONG_JUMP _longjmp
54 #else
55 #define SET_JUMP setjmp
56 #define LONG_JUMP longjmp
57 #endif
59 public int reading;
61 static jmp_buf read_label;
63 extern int sigs;
65 #if !HAVE_STRERROR
66 static char *strerror __P((int));
67 #endif
70 * Like read() system call, but is deliberately interruptible.
71 * A call to intread() from a signal handler will interrupt
72 * any pending iread().
74 public int
75 iread(fd, buf, len)
76 int fd;
77 char *buf;
78 unsigned int len;
80 register int n;
82 start:
83 #if MSDOS_COMPILER==WIN32C
84 if (ABORT_SIGS())
85 return (READ_INTR);
86 #else
87 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
88 if (kbhit())
90 int c;
92 c = getch();
93 if (c == '\003')
94 return (READ_INTR);
95 ungetch(c);
97 #endif
98 #endif
99 if (SET_JUMP(read_label))
102 * We jumped here from intread.
104 reading = 0;
105 #if HAVE_SIGPROCMASK
107 sigset_t mask;
108 sigemptyset(&mask);
109 sigprocmask(SIG_SETMASK, &mask, NULL);
111 #else
112 #if HAVE_SIGSETMASK
113 sigsetmask(0);
114 #else
115 #ifdef _OSK
116 sigmask(~0);
117 #endif
118 #endif
119 #endif
120 return (READ_INTR);
123 flush();
124 reading = 1;
125 #if MSDOS_COMPILER==DJGPPC
126 if (isatty(fd))
129 * Don't try reading from a TTY until a character is
130 * available, because that makes some background programs
131 * believe DOS is busy in a way that prevents those
132 * programs from working while "less" waits.
134 fd_set readfds;
136 FD_ZERO(&readfds);
137 FD_SET(fd, &readfds);
138 if (select(fd+1, &readfds, 0, 0, 0) == -1)
139 return (-1);
141 #endif
142 n = read(fd, buf, len);
143 #if 1
145 * This is a kludge to workaround a problem on some systems
146 * where terminating a remote tty connection causes read() to
147 * start returning 0 forever, instead of -1.
150 extern int ignore_eoi;
151 if (!ignore_eoi)
153 static int consecutive_nulls = 0;
154 if (n == 0)
155 consecutive_nulls++;
156 else
157 consecutive_nulls = 0;
158 if (consecutive_nulls > 20)
159 quit(QUIT_ERROR);
162 #endif
163 reading = 0;
164 if (n < 0)
166 #if HAVE_ERRNO
168 * Certain values of errno indicate we should just retry the read.
170 #if MUST_DEFINE_ERRNO
171 extern int errno;
172 #endif
173 #ifdef EINTR
174 if (errno == EINTR)
175 goto start;
176 #endif
177 #ifdef EAGAIN
178 if (errno == EAGAIN)
179 goto start;
180 #endif
181 #endif
182 return (-1);
184 return (n);
188 * Interrupt a pending iread().
190 public void
191 intread()
193 LONG_JUMP(read_label, 1);
197 * Return the current time.
199 #if HAVE_TIME
200 public long
201 get_time()
203 time_type t;
205 time(&t);
206 return (t);
208 #endif
211 #if !HAVE_STRERROR
213 * Local version of strerror, if not available from the system.
215 static char *
216 strerror(err)
217 int err;
219 #if HAVE_SYS_ERRLIST
220 static char buf[16];
221 extern char *sys_errlist[];
222 extern int sys_nerr;
224 if (err < sys_nerr)
225 return sys_errlist[err];
226 sprintf(buf, "Error %d", err);
227 return buf;
228 #else
229 return ("cannot open");
230 #endif
232 #endif
235 * errno_message: Return an error message based on the value of "errno".
237 public char *
238 errno_message(filename)
239 char *filename;
241 register const char *p;
242 register char *m;
243 int len;
244 #if HAVE_ERRNO
245 #if MUST_DEFINE_ERRNO
246 extern int errno;
247 #endif
248 p = strerror(errno);
249 #else
250 p = "cannot open";
251 #endif
252 len = strlen(filename) + strlen(p) + 3;
253 m = (char *) ecalloc(len, sizeof(char));
254 SNPRINTF2(m, len, "%s: %s", filename, p);
255 return (m);
258 /* #define HAVE_FLOAT 0 */
260 static POSITION
261 muldiv(val, num, den)
262 POSITION val, num, den;
264 #if HAVE_FLOAT
265 double v = (((double) val) * num) / den;
266 return ((POSITION) (v + 0.5));
267 #else
268 POSITION v = ((POSITION) val) * num;
270 if (v / num == val)
271 /* No overflow */
272 return (POSITION) (v / den);
273 else
274 /* Above calculation overflows;
275 * use a method that is less precise but won't overflow. */
276 return (POSITION) (val / (den / num));
277 #endif
281 * Return the ratio of two POSITIONS, as a percentage.
282 * {{ Assumes a POSITION is a long int. }}
284 public int
285 percentage(num, den)
286 POSITION num, den;
288 return (int) muldiv(num, (POSITION) 100, den);
292 * Return the specified percentage of a POSITION.
294 public POSITION
295 percent_pos(pos, percent, fraction)
296 POSITION pos;
297 int percent;
298 long fraction;
300 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
301 POSITION perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
303 if (perden == 0)
304 return (0);
305 return (POSITION) muldiv(pos, perden, (POSITION) NUM_FRAC_DENOM);
308 #if !HAVE_STRCHR
310 * strchr is used by regexp.c.
312 char *
313 strchr(s, c)
314 char *s;
315 int c;
317 for ( ; *s != '\0'; s++)
318 if (*s == c)
319 return (s);
320 if (c == '\0')
321 return (s);
322 return (NULL);
324 #endif
326 #if !HAVE_MEMCPY
327 VOID_POINTER
328 memcpy(dst, src, len)
329 VOID_POINTER dst;
330 VOID_POINTER src;
331 int len;
333 char *dstp = (char *) dst;
334 char *srcp = (char *) src;
335 int i;
337 for (i = 0; i < len; i++)
338 dstp[i] = srcp[i];
339 return (dst);
341 #endif
343 #ifdef _OSK_MWC32
346 * This implements an ANSI-style intercept setup for Microware C 3.2
348 public int
349 os9_signal(type, handler)
350 int type;
351 RETSIGTYPE (*handler)();
353 intercept(handler);
356 #include <sgstat.h>
358 int
359 isatty(f)
360 int f;
362 struct sgbuf sgbuf;
364 if (_gs_opt(f, &sgbuf) < 0)
365 return -1;
366 return (sgbuf.sg_class == 0);
369 #endif