Correct math and limerick.
[dragonfly.git] / contrib / less / os.c
blobeb75bbae464cd954425705be04bf38bf4d866935
1 /*
2 * Copyright (C) 1984-2012 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
8 */
12 * Operating system dependent routines.
14 * Most of the stuff in here is based on Unix, but an attempt
15 * has been made to make things work on other operating systems.
16 * This will sometimes result in a loss of functionality, unless
17 * someone rewrites code specifically for the new operating system.
19 * The makefile provides defines to decide whether various
20 * Unix features are present.
23 #include "less.h"
24 #include <signal.h>
25 #include <setjmp.h>
26 #if HAVE_TIME_H
27 #include <time.h>
28 #endif
29 #if HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #if HAVE_VALUES_H
33 #include <values.h>
34 #endif
36 #if HAVE_TIME_T
37 #define time_type time_t
38 #else
39 #define time_type long
40 #endif
43 * BSD setjmp() saves (and longjmp() restores) the signal mask.
44 * This costs a system call or two per setjmp(), so if possible we clear the
45 * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
46 * On other systems, setjmp() doesn't affect the signal mask and so
47 * _setjmp() does not exist; we just use setjmp().
49 #if HAVE__SETJMP && HAVE_SIGSETMASK
50 #define SET_JUMP _setjmp
51 #define LONG_JUMP _longjmp
52 #else
53 #define SET_JUMP setjmp
54 #define LONG_JUMP longjmp
55 #endif
57 public int reading;
59 static jmp_buf read_label;
61 extern int sigs;
64 * Like read() system call, but is deliberately interruptible.
65 * A call to intread() from a signal handler will interrupt
66 * any pending iread().
68 public int
69 iread(fd, buf, len)
70 int fd;
71 char *buf;
72 unsigned int len;
74 register int n;
76 start:
77 #if MSDOS_COMPILER==WIN32C
78 if (ABORT_SIGS())
79 return (READ_INTR);
80 #else
81 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
82 if (kbhit())
84 int c;
86 c = getch();
87 if (c == '\003')
88 return (READ_INTR);
89 ungetch(c);
91 #endif
92 #endif
93 if (SET_JUMP(read_label))
96 * We jumped here from intread.
98 reading = 0;
99 #if HAVE_SIGPROCMASK
101 sigset_t mask;
102 sigemptyset(&mask);
103 sigprocmask(SIG_SETMASK, &mask, NULL);
105 #else
106 #if HAVE_SIGSETMASK
107 sigsetmask(0);
108 #else
109 #ifdef _OSK
110 sigmask(~0);
111 #endif
112 #endif
113 #endif
114 return (READ_INTR);
117 flush();
118 reading = 1;
119 #if MSDOS_COMPILER==DJGPPC
120 if (isatty(fd))
123 * Don't try reading from a TTY until a character is
124 * available, because that makes some background programs
125 * believe DOS is busy in a way that prevents those
126 * programs from working while "less" waits.
128 fd_set readfds;
130 FD_ZERO(&readfds);
131 FD_SET(fd, &readfds);
132 if (select(fd+1, &readfds, 0, 0, 0) == -1)
133 return (-1);
135 #endif
136 n = read(fd, buf, len);
137 #if 1
139 * This is a kludge to workaround a problem on some systems
140 * where terminating a remote tty connection causes read() to
141 * start returning 0 forever, instead of -1.
144 extern int ignore_eoi;
145 if (!ignore_eoi)
147 static int consecutive_nulls = 0;
148 if (n == 0)
149 consecutive_nulls++;
150 else
151 consecutive_nulls = 0;
152 if (consecutive_nulls > 20)
153 quit(QUIT_ERROR);
156 #endif
157 reading = 0;
158 if (n < 0)
160 #if HAVE_ERRNO
162 * Certain values of errno indicate we should just retry the read.
164 #if MUST_DEFINE_ERRNO
165 extern int errno;
166 #endif
167 #ifdef EINTR
168 if (errno == EINTR)
169 goto start;
170 #endif
171 #ifdef EAGAIN
172 if (errno == EAGAIN)
173 goto start;
174 #endif
175 #endif
176 return (-1);
178 return (n);
182 * Interrupt a pending iread().
184 public void
185 intread()
187 LONG_JUMP(read_label, 1);
191 * Return the current time.
193 #if HAVE_TIME
194 public long
195 get_time()
197 time_type t;
199 time(&t);
200 return (t);
202 #endif
205 #if !HAVE_STRERROR
207 * Local version of strerror, if not available from the system.
209 static char *
210 strerror(err)
211 int err;
213 #if HAVE_SYS_ERRLIST
214 static char buf[16];
215 extern char *sys_errlist[];
216 extern int sys_nerr;
218 if (err < sys_nerr)
219 return sys_errlist[err];
220 sprintf(buf, "Error %d", err);
221 return buf;
222 #else
223 return ("cannot open");
224 #endif
226 #endif
229 * errno_message: Return an error message based on the value of "errno".
231 public char *
232 errno_message(filename)
233 char *filename;
235 register char *p;
236 register char *m;
237 int len;
238 #if HAVE_ERRNO
239 #if MUST_DEFINE_ERRNO
240 extern int errno;
241 #endif
242 p = strerror(errno);
243 #else
244 p = "cannot open";
245 #endif
246 len = strlen(filename) + strlen(p) + 3;
247 m = (char *) ecalloc(len, sizeof(char));
248 SNPRINTF2(m, len, "%s: %s", filename, p);
249 return (m);
252 /* #define HAVE_FLOAT 0 */
254 static POSITION
255 muldiv(val, num, den)
256 POSITION val, num, den;
258 #if HAVE_FLOAT
259 double v = (((double) val) * num) / den;
260 return ((POSITION) (v + 0.5));
261 #else
262 POSITION v = ((POSITION) val) * num;
264 if (v / num == val)
265 /* No overflow */
266 return (POSITION) (v / den);
267 else
268 /* Above calculation overflows;
269 * use a method that is less precise but won't overflow. */
270 return (POSITION) (val / (den / num));
271 #endif
275 * Return the ratio of two POSITIONS, as a percentage.
276 * {{ Assumes a POSITION is a long int. }}
278 public int
279 percentage(num, den)
280 POSITION num, den;
282 return (int) muldiv(num, (POSITION) 100, den);
286 * Return the specified percentage of a POSITION.
288 public POSITION
289 percent_pos(pos, percent, fraction)
290 POSITION pos;
291 int percent;
292 long fraction;
294 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
295 POSITION perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
297 if (perden == 0)
298 return (0);
299 return (POSITION) muldiv(pos, perden, (POSITION) NUM_FRAC_DENOM);
302 #if !HAVE_STRCHR
304 * strchr is used by regexp.c.
306 char *
307 strchr(s, c)
308 char *s;
309 int c;
311 for ( ; *s != '\0'; s++)
312 if (*s == c)
313 return (s);
314 if (c == '\0')
315 return (s);
316 return (NULL);
318 #endif
320 #if !HAVE_MEMCPY
321 VOID_POINTER
322 memcpy(dst, src, len)
323 VOID_POINTER dst;
324 VOID_POINTER src;
325 int len;
327 char *dstp = (char *) dst;
328 char *srcp = (char *) src;
329 int i;
331 for (i = 0; i < len; i++)
332 dstp[i] = srcp[i];
333 return (dst);
335 #endif
337 #ifdef _OSK_MWC32
340 * This implements an ANSI-style intercept setup for Microware C 3.2
342 public int
343 os9_signal(type, handler)
344 int type;
345 RETSIGTYPE (*handler)();
347 intercept(handler);
350 #include <sgstat.h>
352 int
353 isatty(f)
354 int f;
356 struct sgbuf sgbuf;
358 if (_gs_opt(f, &sgbuf) < 0)
359 return -1;
360 return (sgbuf.sg_class == 0);
363 #endif