2 * Copyright (C) 1984-2022 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.
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.
26 #if MSDOS_COMPILER==WIN32C
39 #if HAVE_POLL && !MSDOS_COMPILER && !defined(__APPLE__)
49 * BSD setjmp() saves (and longjmp() restores) the signal mask.
50 * This costs a system call or two per setjmp(), so if possible we clear the
51 * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
52 * On other systems, setjmp() doesn't affect the signal mask and so
53 * _setjmp() does not exist; we just use setjmp().
55 #if HAVE__SETJMP && HAVE_SIGSETMASK
56 #define SET_JUMP _setjmp
57 #define LONG_JUMP _longjmp
59 #define SET_JUMP setjmp
60 #define LONG_JUMP longjmp
64 public int consecutive_nulls
= 0;
66 static jmp_buf read_label
;
69 extern int ignore_eoi
;
70 extern int exit_F_on_close
;
77 * Return true if one of the events has occurred on the specified file.
80 poll_events(fd
, events
)
84 struct pollfd poller
= { fd
, events
, 0 };
85 int n
= poll(&poller
, 1, 0);
88 return (poller
.revents
& events
);
93 * Like read() system call, but is deliberately interruptible.
94 * A call to intread() from a signal handler will interrupt
95 * any pending iread().
106 #if MSDOS_COMPILER==WIN32C
110 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
122 if (SET_JUMP(read_label
))
125 * We jumped here from intread.
132 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
148 #if MSDOS_COMPILER==DJGPPC
152 * Don't try reading from a TTY until a character is
153 * available, because that makes some background programs
154 * believe DOS is busy in a way that prevents those
155 * programs from working while "less" waits.
160 FD_SET(fd
, &readfds
);
161 if (select(fd
+1, &readfds
, 0, 0, 0) == -1)
169 if (ignore_eoi
&& fd
!= tty
)
171 int close_events
= exit_F_on_close
? POLLERR
|POLLHUP
: POLLERR
;
172 if (poll_events(tty
, POLLIN
) && getchr() == CONTROL('X'))
178 if (poll_events(fd
, close_events
))
186 #if MSDOS_COMPILER==WIN32C
187 if (win32_kbhit() && WIN32getch() == CONTROL('X'))
195 n
= read(fd
, buf
, len
);
199 * This is a kludge to workaround a problem on some systems
200 * where terminating a remote tty connection causes read() to
201 * start returning 0 forever, instead of -1.
209 consecutive_nulls
= 0;
210 if (consecutive_nulls
> 20)
219 * Certain values of errno indicate we should just retry the read.
221 #if MUST_DEFINE_ERRNO
239 * Interrupt a pending iread().
244 LONG_JUMP(read_label
, 1);
248 * Return the current time.
264 * Local version of strerror, if not available from the system.
270 static char buf
[INT_STRLEN_BOUND(int)+12];
272 extern char *sys_errlist
[];
276 return sys_errlist
[err
];
278 sprintf(buf
, "Error %d", err
);
284 * errno_message: Return an error message based on the value of "errno".
287 errno_message(filename
)
294 #if MUST_DEFINE_ERRNO
301 len
= (int) (strlen(filename
) + strlen(p
) + 3);
302 m
= (char *) ecalloc(len
, sizeof(char));
303 SNPRINTF2(m
, len
, "%s: %s", filename
, p
);
307 /* #define HAVE_FLOAT 0 */
310 muldiv(val
, num
, den
)
311 POSITION val
, num
, den
;
314 double v
= (((double) val
) * num
) / den
;
315 return ((POSITION
) (v
+ 0.5));
317 POSITION v
= ((POSITION
) val
) * num
;
321 return (POSITION
) (v
/ den
);
323 /* Above calculation overflows;
324 * use a method that is less precise but won't overflow. */
325 return (POSITION
) (val
/ (den
/ num
));
330 * Return the ratio of two POSITIONS, as a percentage.
331 * {{ Assumes a POSITION is a long int. }}
338 return (int) muldiv(num
, (POSITION
) 100, den
);
342 * Return the specified percentage of a POSITION.
345 percent_pos(pos
, percent
, fraction
)
350 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
351 POSITION perden
= (percent
* (NUM_FRAC_DENOM
/ 100)) + (fraction
/ 100);
355 return (POSITION
) muldiv(pos
, perden
, (POSITION
) NUM_FRAC_DENOM
);
360 * strchr is used by regexp.c.
367 for ( ; *s
!= '\0'; s
++)
378 memcpy(dst
, src
, len
)
383 char *dstp
= (char *) dst
;
384 char *srcp
= (char *) src
;
387 for (i
= 0; i
< len
; i
++)
396 * This implements an ANSI-style intercept setup for Microware C 3.2
399 os9_signal(type
, handler
)
401 RETSIGTYPE (*handler
)();
414 if (_gs_opt(f
, &sgbuf
) < 0)
416 return (sgbuf
.sg_class
== 0);
425 #if MSDOS_COMPILER==WIN32C
430 struct timespec t
= { sec
, (ms
- sec
*1000) * 1000000 };
436 sleep((ms
+999) / 1000);