2 * Copyright (C) 1984-2009 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * Operating system dependent routines.
15 * Most of the stuff in here is based on Unix, but an attempt
16 * has been made to make things work on other operating systems.
17 * This will sometimes result in a loss of functionality, unless
18 * someone rewrites code specifically for the new operating system.
20 * The makefile provides defines to decide whether various
21 * Unix features are present.
38 #define time_type time_t
40 #define time_type long
44 * BSD setjmp() saves (and longjmp() restores) the signal mask.
45 * This costs a system call or two per setjmp(), so if possible we clear the
46 * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
47 * On other systems, setjmp() doesn't affect the signal mask and so
48 * _setjmp() does not exist; we just use setjmp().
50 #if HAVE__SETJMP && HAVE_SIGSETMASK
51 #define SET_JUMP _setjmp
52 #define LONG_JUMP _longjmp
54 #define SET_JUMP setjmp
55 #define LONG_JUMP longjmp
60 static jmp_buf read_label
;
65 * Like read() system call, but is deliberately interruptible.
66 * A call to intread() from a signal handler will interrupt
67 * any pending iread().
78 #if MSDOS_COMPILER==WIN32C
82 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
94 if (SET_JUMP(read_label
))
97 * We jumped here from intread.
104 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
120 #if MSDOS_COMPILER==DJGPPC
124 * Don't try reading from a TTY until a character is
125 * available, because that makes some background programs
126 * believe DOS is busy in a way that prevents those
127 * programs from working while "less" waits.
132 FD_SET(fd
, &readfds
);
133 if (select(fd
+1, &readfds
, 0, 0, 0) == -1)
137 n
= read(fd
, buf
, len
);
140 * This is a kludge to workaround a problem on some systems
141 * where terminating a remote tty connection causes read() to
142 * start returning 0 forever, instead of -1.
145 extern int ignore_eoi
;
148 static int consecutive_nulls
= 0;
152 consecutive_nulls
= 0;
153 if (consecutive_nulls
> 20)
163 * Certain values of errno indicate we should just retry the read.
165 #if MUST_DEFINE_ERRNO
183 * Interrupt a pending iread().
188 LONG_JUMP(read_label
, 1);
192 * Return the current time.
208 * Local version of strerror, if not available from the system.
216 extern char *sys_errlist
[];
220 return sys_errlist
[err
];
221 sprintf(buf
, "Error %d", err
);
224 return ("cannot open");
230 * errno_message: Return an error message based on the value of "errno".
233 errno_message(filename
)
240 #if MUST_DEFINE_ERRNO
247 len
= strlen(filename
) + strlen(p
) + 3;
248 m
= (char *) ecalloc(len
, sizeof(char));
249 SNPRINTF2(m
, len
, "%s: %s", filename
, p
);
254 * Return the ratio of two POSITIONS, as a percentage.
255 * {{ Assumes a POSITION is a long int. }}
261 POSITION num100
= num
* 100;
263 if (num100
/ 100 == num
)
264 return (num100
/ den
);
266 return (num
/ (den
/ 100));
270 * Return the specified percentage of a POSITION.
273 percent_pos(pos
, percent
, fraction
)
278 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
279 long perden
= (percent
* (NUM_FRAC_DENOM
/ 100)) + (fraction
/ 100);
284 temp
= pos
* perden
; /* This might overflow. */
285 if (temp
/ perden
== pos
)
287 return (temp
/ NUM_FRAC_DENOM
);
289 /* Above calculation overflows;
290 * use a method that is less precise but won't overflow. */
291 return (perden
* (pos
/ NUM_FRAC_DENOM
));
296 * strchr is used by regexp.c.
303 for ( ; *s
!= '\0'; s
++)
314 memcpy(dst
, src
, len
)
319 char *dstp
= (char *) dst
;
320 char *srcp
= (char *) src
;
323 for (i
= 0; i
< len
; i
++)
332 * This implements an ANSI-style intercept setup for Microware C 3.2
335 os9_signal(type
, handler
)
337 RETSIGTYPE (*handler
)();
350 if (_gs_opt(f
, &sgbuf
) < 0)
352 return (sgbuf
.sg_class
== 0);