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