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