2 * Copyright (C) 1984-2015 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.
37 * BSD setjmp() saves (and longjmp() restores) the signal mask.
38 * This costs a system call or two per setjmp(), so if possible we clear the
39 * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
40 * On other systems, setjmp() doesn't affect the signal mask and so
41 * _setjmp() does not exist; we just use setjmp().
43 #if HAVE__SETJMP && HAVE_SIGSETMASK
44 #define SET_JUMP _setjmp
45 #define LONG_JUMP _longjmp
47 #define SET_JUMP setjmp
48 #define LONG_JUMP longjmp
53 static jmp_buf read_label
;
58 * Like read() system call, but is deliberately interruptible.
59 * A call to intread() from a signal handler will interrupt
60 * any pending iread().
71 #if MSDOS_COMPILER==WIN32C
75 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
87 if (SET_JUMP(read_label
))
90 * We jumped here from intread.
97 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
113 #if MSDOS_COMPILER==DJGPPC
117 * Don't try reading from a TTY until a character is
118 * available, because that makes some background programs
119 * believe DOS is busy in a way that prevents those
120 * programs from working while "less" waits.
125 FD_SET(fd
, &readfds
);
126 if (select(fd
+1, &readfds
, 0, 0, 0) == -1)
130 n
= read(fd
, buf
, len
);
133 * This is a kludge to workaround a problem on some systems
134 * where terminating a remote tty connection causes read() to
135 * start returning 0 forever, instead of -1.
138 extern int ignore_eoi
;
141 static int consecutive_nulls
= 0;
145 consecutive_nulls
= 0;
146 if (consecutive_nulls
> 20)
156 * Certain values of errno indicate we should just retry the read.
158 #if MUST_DEFINE_ERRNO
176 * Interrupt a pending iread().
181 LONG_JUMP(read_label
, 1);
185 * Return the current time.
201 * Local version of strerror, if not available from the system.
209 extern char *sys_errlist
[];
213 return sys_errlist
[err
];
214 sprintf(buf
, "Error %d", err
);
217 return ("cannot open");
223 * errno_message: Return an error message based on the value of "errno".
226 errno_message(filename
)
233 #if MUST_DEFINE_ERRNO
240 len
= (int) (strlen(filename
) + strlen(p
) + 3);
241 m
= (char *) ecalloc(len
, sizeof(char));
242 SNPRINTF2(m
, len
, "%s: %s", filename
, p
);
246 /* #define HAVE_FLOAT 0 */
249 muldiv(val
, num
, den
)
250 POSITION val
, num
, den
;
253 double v
= (((double) val
) * num
) / den
;
254 return ((POSITION
) (v
+ 0.5));
256 POSITION v
= ((POSITION
) val
) * num
;
260 return (POSITION
) (v
/ den
);
262 /* Above calculation overflows;
263 * use a method that is less precise but won't overflow. */
264 return (POSITION
) (val
/ (den
/ num
));
269 * Return the ratio of two POSITIONS, as a percentage.
270 * {{ Assumes a POSITION is a long int. }}
276 return (int) muldiv(num
, (POSITION
) 100, den
);
280 * Return the specified percentage of a POSITION.
283 percent_pos(pos
, percent
, fraction
)
288 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
289 POSITION perden
= (percent
* (NUM_FRAC_DENOM
/ 100)) + (fraction
/ 100);
293 return (POSITION
) muldiv(pos
, perden
, (POSITION
) NUM_FRAC_DENOM
);
298 * strchr is used by regexp.c.
305 for ( ; *s
!= '\0'; s
++)
316 memcpy(dst
, src
, len
)
321 char *dstp
= (char *) dst
;
322 char *srcp
= (char *) src
;
325 for (i
= 0; i
< len
; i
++)
334 * This implements an ANSI-style intercept setup for Microware C 3.2
337 os9_signal(type
, handler
)
339 RETSIGTYPE (*handler
)();
352 if (_gs_opt(f
, &sgbuf
) < 0)
354 return (sgbuf
.sg_class
== 0);