1 /* $NetBSD: os.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
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.
39 #define time_type time_t
41 #define time_type long
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
55 #define SET_JUMP setjmp
56 #define LONG_JUMP longjmp
61 static jmp_buf read_label
;
66 static char *strerror
__P((int));
70 * Like read() system call, but is deliberately interruptible.
71 * A call to intread() from a signal handler will interrupt
72 * any pending iread().
83 #if MSDOS_COMPILER==WIN32C
87 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
99 if (SET_JUMP(read_label
))
102 * We jumped here from intread.
109 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
125 #if MSDOS_COMPILER==DJGPPC
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.
137 FD_SET(fd
, &readfds
);
138 if (select(fd
+1, &readfds
, 0, 0, 0) == -1)
142 n
= read(fd
, buf
, len
);
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
;
153 static int consecutive_nulls
= 0;
157 consecutive_nulls
= 0;
158 if (consecutive_nulls
> 20)
168 * Certain values of errno indicate we should just retry the read.
170 #if MUST_DEFINE_ERRNO
188 * Interrupt a pending iread().
193 LONG_JUMP(read_label
, 1);
197 * Return the current time.
213 * Local version of strerror, if not available from the system.
221 extern char *sys_errlist
[];
225 return sys_errlist
[err
];
226 sprintf(buf
, "Error %d", err
);
229 return ("cannot open");
235 * errno_message: Return an error message based on the value of "errno".
238 errno_message(filename
)
241 register const char *p
;
245 #if MUST_DEFINE_ERRNO
252 len
= strlen(filename
) + strlen(p
) + 3;
253 m
= (char *) ecalloc(len
, sizeof(char));
254 SNPRINTF2(m
, len
, "%s: %s", filename
, p
);
258 /* #define HAVE_FLOAT 0 */
261 muldiv(val
, num
, den
)
262 POSITION val
, num
, den
;
265 double v
= (((double) val
) * num
) / den
;
266 return ((POSITION
) (v
+ 0.5));
268 POSITION v
= ((POSITION
) val
) * num
;
272 return (POSITION
) (v
/ den
);
274 /* Above calculation overflows;
275 * use a method that is less precise but won't overflow. */
276 return (POSITION
) (val
/ (den
/ num
));
281 * Return the ratio of two POSITIONS, as a percentage.
282 * {{ Assumes a POSITION is a long int. }}
288 return (int) muldiv(num
, (POSITION
) 100, den
);
292 * Return the specified percentage of a POSITION.
295 percent_pos(pos
, percent
, fraction
)
300 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
301 POSITION perden
= (percent
* (NUM_FRAC_DENOM
/ 100)) + (fraction
/ 100);
305 return (POSITION
) muldiv(pos
, perden
, (POSITION
) NUM_FRAC_DENOM
);
310 * strchr is used by regexp.c.
317 for ( ; *s
!= '\0'; s
++)
328 memcpy(dst
, src
, len
)
333 char *dstp
= (char *) dst
;
334 char *srcp
= (char *) src
;
337 for (i
= 0; i
< len
; i
++)
346 * This implements an ANSI-style intercept setup for Microware C 3.2
349 os9_signal(type
, handler
)
351 RETSIGTYPE (*handler
)();
364 if (_gs_opt(f
, &sgbuf
) < 0)
366 return (sgbuf
.sg_class
== 0);