Home | History | Annotate | Line # | Download | only in dist
os.c revision 1.1.1.3
      1      1.1    tron /*
      2  1.1.1.3  simonb  * Copyright (C) 1984-2023  Mark Nudelman
      3      1.1    tron  *
      4      1.1    tron  * You may distribute under the terms of either the GNU General Public
      5      1.1    tron  * License or the Less License, as specified in the README file.
      6      1.1    tron  *
      7  1.1.1.2    tron  * For more information, see the README file.
      8      1.1    tron  */
      9      1.1    tron 
     10      1.1    tron 
     11      1.1    tron /*
     12      1.1    tron  * Operating system dependent routines.
     13      1.1    tron  *
     14      1.1    tron  * Most of the stuff in here is based on Unix, but an attempt
     15      1.1    tron  * has been made to make things work on other operating systems.
     16      1.1    tron  * This will sometimes result in a loss of functionality, unless
     17      1.1    tron  * someone rewrites code specifically for the new operating system.
     18      1.1    tron  *
     19      1.1    tron  * The makefile provides defines to decide whether various
     20      1.1    tron  * Unix features are present.
     21      1.1    tron  */
     22      1.1    tron 
     23      1.1    tron #include "less.h"
     24      1.1    tron #include <signal.h>
     25      1.1    tron #include <setjmp.h>
     26  1.1.1.3  simonb #if MSDOS_COMPILER==WIN32C
     27  1.1.1.3  simonb #include <windows.h>
     28  1.1.1.3  simonb #endif
     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.1.3  simonb #if defined(__APPLE__)
     40  1.1.1.3  simonb #include <sys/utsname.h>
     41  1.1.1.3  simonb #endif
     42  1.1.1.3  simonb 
     43  1.1.1.3  simonb #if HAVE_POLL && !MSDOS_COMPILER
     44  1.1.1.3  simonb #define USE_POLL 1
     45  1.1.1.3  simonb static int use_poll = TRUE;
     46      1.1    tron #else
     47  1.1.1.3  simonb #define USE_POLL 0
     48  1.1.1.3  simonb #endif
     49  1.1.1.3  simonb #if USE_POLL
     50  1.1.1.3  simonb #include <poll.h>
     51  1.1.1.3  simonb static int any_data = FALSE;
     52      1.1    tron #endif
     53      1.1    tron 
     54      1.1    tron /*
     55      1.1    tron  * BSD setjmp() saves (and longjmp() restores) the signal mask.
     56      1.1    tron  * This costs a system call or two per setjmp(), so if possible we clear the
     57      1.1    tron  * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
     58      1.1    tron  * On other systems, setjmp() doesn't affect the signal mask and so
     59      1.1    tron  * _setjmp() does not exist; we just use setjmp().
     60      1.1    tron  */
     61      1.1    tron #if HAVE__SETJMP && HAVE_SIGSETMASK
     62  1.1.1.3  simonb #define SET_JUMP        _setjmp
     63  1.1.1.3  simonb #define LONG_JUMP       _longjmp
     64      1.1    tron #else
     65  1.1.1.3  simonb #define SET_JUMP        setjmp
     66  1.1.1.3  simonb #define LONG_JUMP       longjmp
     67      1.1    tron #endif
     68      1.1    tron 
     69      1.1    tron public int reading;
     70  1.1.1.3  simonb public int waiting_for_data;
     71  1.1.1.3  simonb public int consecutive_nulls = 0;
     72      1.1    tron 
     73  1.1.1.3  simonb /* Milliseconds to wait for data before displaying "waiting for data" message. */
     74  1.1.1.3  simonb static int waiting_for_data_delay = 4000;
     75      1.1    tron static jmp_buf read_label;
     76      1.1    tron 
     77      1.1    tron extern int sigs;
     78  1.1.1.3  simonb extern int ignore_eoi;
     79  1.1.1.3  simonb extern int exit_F_on_close;
     80  1.1.1.3  simonb extern int follow_mode;
     81  1.1.1.3  simonb extern int scanning_eof;
     82  1.1.1.3  simonb extern char intr_char;
     83  1.1.1.3  simonb #if !MSDOS_COMPILER
     84  1.1.1.3  simonb extern int tty;
     85  1.1.1.3  simonb #endif
     86  1.1.1.3  simonb #if LESSTEST
     87  1.1.1.3  simonb extern char *ttyin_name;
     88  1.1.1.3  simonb #endif /*LESSTEST*/
     89  1.1.1.3  simonb 
     90  1.1.1.3  simonb public void init_poll(void)
     91  1.1.1.3  simonb {
     92  1.1.1.3  simonb 	char *delay = lgetenv("LESS_DATA_DELAY");
     93  1.1.1.3  simonb 	int idelay = (delay == NULL) ? 0 : atoi(delay);
     94  1.1.1.3  simonb 	if (idelay > 0)
     95  1.1.1.3  simonb 		waiting_for_data_delay = idelay;
     96  1.1.1.3  simonb #if USE_POLL
     97  1.1.1.3  simonb #if defined(__APPLE__)
     98  1.1.1.3  simonb 	/* In old versions of MacOS, poll() does not work with /dev/tty. */
     99  1.1.1.3  simonb 	struct utsname uts;
    100  1.1.1.3  simonb 	if (uname(&uts) < 0 || lstrtoi(uts.release, NULL, 10) < 20)
    101  1.1.1.3  simonb 		use_poll = FALSE;
    102  1.1.1.3  simonb #endif
    103  1.1.1.3  simonb #endif
    104  1.1.1.3  simonb }
    105  1.1.1.3  simonb 
    106  1.1.1.3  simonb #if USE_POLL
    107  1.1.1.3  simonb /*
    108  1.1.1.3  simonb  * Check whether data is available, either from a file/pipe or from the tty.
    109  1.1.1.3  simonb  * Return READ_AGAIN if no data currently available, but caller should retry later.
    110  1.1.1.3  simonb  * Return READ_INTR to abort F command (forw_loop).
    111  1.1.1.3  simonb  * Return 0 if safe to read from fd.
    112  1.1.1.3  simonb  */
    113  1.1.1.3  simonb static int check_poll(int fd, int tty)
    114  1.1.1.3  simonb {
    115  1.1.1.3  simonb 	struct pollfd poller[2] = { { fd, POLLIN, 0 }, { tty, POLLIN, 0 } };
    116  1.1.1.3  simonb 	int timeout = (waiting_for_data && !(scanning_eof && follow_mode == FOLLOW_NAME)) ? -1 : waiting_for_data_delay;
    117  1.1.1.3  simonb 	if (!any_data)
    118  1.1.1.3  simonb 	{
    119  1.1.1.3  simonb 		/*
    120  1.1.1.3  simonb 		 * Don't do polling if no data has yet been received,
    121  1.1.1.3  simonb 		 * to allow a program piping data into less to have temporary
    122  1.1.1.3  simonb 		 * access to the tty (like sudo asking for a password).
    123  1.1.1.3  simonb 		 */
    124  1.1.1.3  simonb 		return (0);
    125  1.1.1.3  simonb 	}
    126  1.1.1.3  simonb 	poll(poller, 2, timeout);
    127  1.1.1.3  simonb #if LESSTEST
    128  1.1.1.3  simonb 	if (ttyin_name == NULL) /* Check for ^X only on a real tty. */
    129  1.1.1.3  simonb #endif /*LESSTEST*/
    130  1.1.1.3  simonb 	{
    131  1.1.1.3  simonb 		if (poller[1].revents & POLLIN)
    132  1.1.1.3  simonb 		{
    133  1.1.1.3  simonb 			LWCHAR ch = getchr();
    134  1.1.1.3  simonb 			if (ch == intr_char)
    135  1.1.1.3  simonb 				/* Break out of "waiting for data". */
    136  1.1.1.3  simonb 				return (READ_INTR);
    137  1.1.1.3  simonb 			ungetcc_back(ch);
    138  1.1.1.3  simonb 		}
    139  1.1.1.3  simonb 	}
    140  1.1.1.3  simonb 	if (ignore_eoi && exit_F_on_close && (poller[0].revents & (POLLHUP|POLLIN)) == POLLHUP)
    141  1.1.1.3  simonb 		/* Break out of F loop on HUP due to --exit-follow-on-close. */
    142  1.1.1.3  simonb 		return (READ_INTR);
    143  1.1.1.3  simonb 	if ((poller[0].revents & (POLLIN|POLLHUP|POLLERR)) == 0)
    144  1.1.1.3  simonb 		/* No data available; let caller take action, then try again. */
    145  1.1.1.3  simonb 		return (READ_AGAIN);
    146  1.1.1.3  simonb 	/* There is data (or HUP/ERR) available. Safe to call read() without blocking. */
    147  1.1.1.3  simonb 	return (0);
    148  1.1.1.3  simonb }
    149  1.1.1.3  simonb #endif /* USE_POLL */
    150  1.1.1.3  simonb 
    151  1.1.1.3  simonb public int supports_ctrl_x(void)
    152  1.1.1.3  simonb {
    153  1.1.1.3  simonb #if USE_POLL
    154  1.1.1.3  simonb 	return (use_poll);
    155  1.1.1.3  simonb #else
    156  1.1.1.3  simonb 	return (FALSE);
    157  1.1.1.3  simonb #endif /* USE_POLL */
    158  1.1.1.3  simonb }
    159      1.1    tron 
    160      1.1    tron /*
    161      1.1    tron  * Like read() system call, but is deliberately interruptible.
    162      1.1    tron  * A call to intread() from a signal handler will interrupt
    163      1.1    tron  * any pending iread().
    164      1.1    tron  */
    165  1.1.1.3  simonb public int iread(int fd, unsigned char *buf, unsigned int len)
    166      1.1    tron {
    167  1.1.1.3  simonb 	int n;
    168      1.1    tron 
    169      1.1    tron start:
    170      1.1    tron #if MSDOS_COMPILER==WIN32C
    171      1.1    tron 	if (ABORT_SIGS())
    172      1.1    tron 		return (READ_INTR);
    173      1.1    tron #else
    174      1.1    tron #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
    175      1.1    tron 	if (kbhit())
    176      1.1    tron 	{
    177      1.1    tron 		int c;
    178      1.1    tron 
    179      1.1    tron 		c = getch();
    180      1.1    tron 		if (c == '\003')
    181      1.1    tron 			return (READ_INTR);
    182      1.1    tron 		ungetch(c);
    183      1.1    tron 	}
    184      1.1    tron #endif
    185      1.1    tron #endif
    186  1.1.1.3  simonb 	if (!reading && SET_JUMP(read_label))
    187      1.1    tron 	{
    188      1.1    tron 		/*
    189      1.1    tron 		 * We jumped here from intread.
    190      1.1    tron 		 */
    191      1.1    tron 		reading = 0;
    192      1.1    tron #if HAVE_SIGPROCMASK
    193      1.1    tron 		{
    194      1.1    tron 		  sigset_t mask;
    195      1.1    tron 		  sigemptyset(&mask);
    196      1.1    tron 		  sigprocmask(SIG_SETMASK, &mask, NULL);
    197      1.1    tron 		}
    198      1.1    tron #else
    199      1.1    tron #if HAVE_SIGSETMASK
    200      1.1    tron 		sigsetmask(0);
    201      1.1    tron #else
    202      1.1    tron #ifdef _OSK
    203      1.1    tron 		sigmask(~0);
    204      1.1    tron #endif
    205      1.1    tron #endif
    206      1.1    tron #endif
    207  1.1.1.3  simonb #if !MSDOS_COMPILER
    208  1.1.1.3  simonb 		if (fd != tty && !ABORT_SIGS())
    209  1.1.1.3  simonb 			/* Non-interrupt signal like SIGWINCH. */
    210  1.1.1.3  simonb 			return (READ_AGAIN);
    211  1.1.1.3  simonb #endif
    212      1.1    tron 		return (READ_INTR);
    213      1.1    tron 	}
    214      1.1    tron 
    215      1.1    tron 	flush();
    216      1.1    tron 	reading = 1;
    217      1.1    tron #if MSDOS_COMPILER==DJGPPC
    218      1.1    tron 	if (isatty(fd))
    219      1.1    tron 	{
    220      1.1    tron 		/*
    221      1.1    tron 		 * Don't try reading from a TTY until a character is
    222      1.1    tron 		 * available, because that makes some background programs
    223      1.1    tron 		 * believe DOS is busy in a way that prevents those
    224      1.1    tron 		 * programs from working while "less" waits.
    225  1.1.1.3  simonb 		 * {{ This code was added 12 Jan 2007; still needed? }}
    226      1.1    tron 		 */
    227      1.1    tron 		fd_set readfds;
    228      1.1    tron 
    229      1.1    tron 		FD_ZERO(&readfds);
    230      1.1    tron 		FD_SET(fd, &readfds);
    231      1.1    tron 		if (select(fd+1, &readfds, 0, 0, 0) == -1)
    232  1.1.1.3  simonb 		{
    233  1.1.1.3  simonb 			reading = 0;
    234  1.1.1.3  simonb 			return (READ_ERR);
    235  1.1.1.3  simonb 		}
    236  1.1.1.3  simonb 	}
    237  1.1.1.3  simonb #endif
    238  1.1.1.3  simonb #if USE_POLL
    239  1.1.1.3  simonb 	if (fd != tty && use_poll)
    240  1.1.1.3  simonb 	{
    241  1.1.1.3  simonb 		int ret = check_poll(fd, tty);
    242  1.1.1.3  simonb 		if (ret != 0)
    243  1.1.1.3  simonb 		{
    244  1.1.1.3  simonb 			if (ret == READ_INTR)
    245  1.1.1.3  simonb 				sigs |= S_INTERRUPT;
    246  1.1.1.3  simonb 			reading = 0;
    247  1.1.1.3  simonb 			return (ret);
    248  1.1.1.3  simonb 		}
    249  1.1.1.3  simonb 	}
    250  1.1.1.3  simonb #else
    251  1.1.1.3  simonb #if MSDOS_COMPILER==WIN32C
    252  1.1.1.3  simonb 	if (win32_kbhit())
    253  1.1.1.3  simonb 	{
    254  1.1.1.3  simonb 		int c;
    255  1.1.1.3  simonb 
    256  1.1.1.3  simonb 		c = WIN32getch();
    257  1.1.1.3  simonb 		if (c == intr_char)
    258  1.1.1.3  simonb 		{
    259  1.1.1.3  simonb 			sigs |= S_INTERRUPT;
    260  1.1.1.3  simonb 			reading = 0;
    261  1.1.1.3  simonb 			return (READ_INTR);
    262  1.1.1.3  simonb 		}
    263  1.1.1.3  simonb 		WIN32ungetch(c);
    264      1.1    tron 	}
    265      1.1    tron #endif
    266  1.1.1.3  simonb #endif
    267      1.1    tron 	n = read(fd, buf, len);
    268  1.1.1.3  simonb 	reading = 0;
    269      1.1    tron #if 1
    270      1.1    tron 	/*
    271      1.1    tron 	 * This is a kludge to workaround a problem on some systems
    272      1.1    tron 	 * where terminating a remote tty connection causes read() to
    273      1.1    tron 	 * start returning 0 forever, instead of -1.
    274      1.1    tron 	 */
    275      1.1    tron 	{
    276      1.1    tron 		if (!ignore_eoi)
    277      1.1    tron 		{
    278      1.1    tron 			if (n == 0)
    279      1.1    tron 				consecutive_nulls++;
    280      1.1    tron 			else
    281      1.1    tron 				consecutive_nulls = 0;
    282      1.1    tron 			if (consecutive_nulls > 20)
    283      1.1    tron 				quit(QUIT_ERROR);
    284      1.1    tron 		}
    285      1.1    tron 	}
    286      1.1    tron #endif
    287      1.1    tron 	if (n < 0)
    288      1.1    tron 	{
    289      1.1    tron #if HAVE_ERRNO
    290      1.1    tron 		/*
    291      1.1    tron 		 * Certain values of errno indicate we should just retry the read.
    292      1.1    tron 		 */
    293      1.1    tron #if MUST_DEFINE_ERRNO
    294      1.1    tron 		extern int errno;
    295      1.1    tron #endif
    296      1.1    tron #ifdef EINTR
    297      1.1    tron 		if (errno == EINTR)
    298      1.1    tron 			goto start;
    299      1.1    tron #endif
    300      1.1    tron #ifdef EAGAIN
    301      1.1    tron 		if (errno == EAGAIN)
    302      1.1    tron 			goto start;
    303      1.1    tron #endif
    304      1.1    tron #endif
    305  1.1.1.3  simonb 		return (READ_ERR);
    306      1.1    tron 	}
    307  1.1.1.3  simonb #if USE_POLL
    308  1.1.1.3  simonb 	if (fd != tty && n > 0)
    309  1.1.1.3  simonb 		any_data = TRUE;
    310  1.1.1.3  simonb #endif
    311      1.1    tron 	return (n);
    312      1.1    tron }
    313      1.1    tron 
    314      1.1    tron /*
    315      1.1    tron  * Interrupt a pending iread().
    316      1.1    tron  */
    317  1.1.1.3  simonb public void intread(void)
    318      1.1    tron {
    319      1.1    tron 	LONG_JUMP(read_label, 1);
    320      1.1    tron }
    321      1.1    tron 
    322      1.1    tron /*
    323      1.1    tron  * Return the current time.
    324      1.1    tron  */
    325      1.1    tron #if HAVE_TIME
    326  1.1.1.3  simonb public time_type get_time(void)
    327      1.1    tron {
    328      1.1    tron 	time_type t;
    329      1.1    tron 
    330      1.1    tron 	time(&t);
    331      1.1    tron 	return (t);
    332      1.1    tron }
    333      1.1    tron #endif
    334      1.1    tron 
    335      1.1    tron 
    336      1.1    tron #if !HAVE_STRERROR
    337      1.1    tron /*
    338      1.1    tron  * Local version of strerror, if not available from the system.
    339      1.1    tron  */
    340  1.1.1.3  simonb static char * strerror(int err)
    341      1.1    tron {
    342  1.1.1.3  simonb 	static char buf[INT_STRLEN_BOUND(int)+12];
    343      1.1    tron #if HAVE_SYS_ERRLIST
    344      1.1    tron 	extern char *sys_errlist[];
    345      1.1    tron 	extern int sys_nerr;
    346      1.1    tron 
    347      1.1    tron 	if (err < sys_nerr)
    348      1.1    tron 		return sys_errlist[err];
    349  1.1.1.3  simonb #endif
    350      1.1    tron 	sprintf(buf, "Error %d", err);
    351      1.1    tron 	return buf;
    352      1.1    tron }
    353      1.1    tron #endif
    354      1.1    tron 
    355      1.1    tron /*
    356      1.1    tron  * errno_message: Return an error message based on the value of "errno".
    357      1.1    tron  */
    358  1.1.1.3  simonb public char * errno_message(char *filename)
    359      1.1    tron {
    360  1.1.1.3  simonb 	char *p;
    361  1.1.1.3  simonb 	char *m;
    362      1.1    tron 	int len;
    363      1.1    tron #if HAVE_ERRNO
    364      1.1    tron #if MUST_DEFINE_ERRNO
    365      1.1    tron 	extern int errno;
    366      1.1    tron #endif
    367      1.1    tron 	p = strerror(errno);
    368      1.1    tron #else
    369      1.1    tron 	p = "cannot open";
    370      1.1    tron #endif
    371  1.1.1.3  simonb 	len = (int) (strlen(filename) + strlen(p) + 3);
    372      1.1    tron 	m = (char *) ecalloc(len, sizeof(char));
    373      1.1    tron 	SNPRINTF2(m, len, "%s: %s", filename, p);
    374      1.1    tron 	return (m);
    375      1.1    tron }
    376      1.1    tron 
    377  1.1.1.3  simonb /*
    378  1.1.1.3  simonb  * Return a description of a signal.
    379  1.1.1.3  simonb  * The return value is good until the next call to this function.
    380  1.1.1.3  simonb  */
    381  1.1.1.3  simonb public char * signal_message(int sig)
    382  1.1.1.3  simonb {
    383  1.1.1.3  simonb 	static char sigbuf[sizeof("Signal ") + INT_STRLEN_BOUND(sig) + 1];
    384  1.1.1.3  simonb #if HAVE_STRSIGNAL
    385  1.1.1.3  simonb 	char *description = strsignal(sig);
    386  1.1.1.3  simonb 	if (description)
    387  1.1.1.3  simonb 		return description;
    388      1.1    tron #endif
    389  1.1.1.3  simonb 	sprintf(sigbuf, "Signal %d", sig);
    390  1.1.1.3  simonb 	return sigbuf;
    391  1.1.1.3  simonb }
    392  1.1.1.3  simonb 
    393  1.1.1.3  simonb /*
    394  1.1.1.3  simonb  * Return (VAL * NUM) / DEN, where DEN is positive
    395  1.1.1.3  simonb  * and min(VAL, NUM) <= DEN so the result cannot overflow.
    396  1.1.1.3  simonb  * Round to the nearest integer, breaking ties by rounding to even.
    397  1.1.1.3  simonb  */
    398  1.1.1.3  simonb public uintmax muldiv(uintmax val, uintmax num, uintmax den)
    399  1.1.1.3  simonb {
    400  1.1.1.3  simonb 	/*
    401  1.1.1.3  simonb 	 * Like round(val * (double) num / den), but without rounding error.
    402  1.1.1.3  simonb 	 * Overflow cannot occur, so there is no need for floating point.
    403  1.1.1.3  simonb 	 */
    404  1.1.1.3  simonb 	uintmax q = val / den;
    405  1.1.1.3  simonb 	uintmax r = val % den;
    406  1.1.1.3  simonb 	uintmax qnum = q * num;
    407  1.1.1.3  simonb 	uintmax rnum = r * num;
    408  1.1.1.3  simonb 	uintmax quot = qnum + rnum / den;
    409  1.1.1.3  simonb 	uintmax rem = rnum % den;
    410  1.1.1.3  simonb 	return quot + (den / 2 < rem + (quot & ~den & 1));
    411      1.1    tron }
    412      1.1    tron 
    413      1.1    tron /*
    414      1.1    tron  * Return the ratio of two POSITIONS, as a percentage.
    415      1.1    tron  * {{ Assumes a POSITION is a long int. }}
    416      1.1    tron  */
    417  1.1.1.3  simonb public int percentage(POSITION num, POSITION den)
    418      1.1    tron {
    419      1.1    tron 	return (int) muldiv(num,  (POSITION) 100, den);
    420      1.1    tron }
    421      1.1    tron 
    422      1.1    tron /*
    423      1.1    tron  * Return the specified percentage of a POSITION.
    424  1.1.1.3  simonb  * Assume (0 <= POS && 0 <= PERCENT <= 100
    425  1.1.1.3  simonb  *	   && 0 <= FRACTION < (PERCENT == 100 ? 1 : NUM_FRAC_DENOM)),
    426  1.1.1.3  simonb  * so the result cannot overflow.  Round to even.
    427      1.1    tron  */
    428  1.1.1.3  simonb public POSITION percent_pos(POSITION pos, int percent, long fraction)
    429      1.1    tron {
    430  1.1.1.3  simonb 	/*
    431  1.1.1.3  simonb 	 * Change from percent (parts per 100)
    432  1.1.1.3  simonb 	 * to pctden (parts per 100 * NUM_FRAC_DENOM).
    433  1.1.1.3  simonb 	 */
    434  1.1.1.3  simonb 	POSITION pctden = (percent * NUM_FRAC_DENOM) + fraction;
    435      1.1    tron 
    436  1.1.1.3  simonb 	return (POSITION) muldiv(pos, pctden, 100 * (POSITION) NUM_FRAC_DENOM);
    437      1.1    tron }
    438      1.1    tron 
    439      1.1    tron #if !HAVE_STRCHR
    440      1.1    tron /*
    441      1.1    tron  * strchr is used by regexp.c.
    442      1.1    tron  */
    443  1.1.1.3  simonb char * strchr(char *s, char c)
    444      1.1    tron {
    445      1.1    tron 	for ( ;  *s != '\0';  s++)
    446      1.1    tron 		if (*s == c)
    447      1.1    tron 			return (s);
    448      1.1    tron 	if (c == '\0')
    449      1.1    tron 		return (s);
    450      1.1    tron 	return (NULL);
    451      1.1    tron }
    452      1.1    tron #endif
    453      1.1    tron 
    454      1.1    tron #if !HAVE_MEMCPY
    455  1.1.1.3  simonb void * memcpy(void *dst, void *src, int len)
    456      1.1    tron {
    457      1.1    tron 	char *dstp = (char *) dst;
    458      1.1    tron 	char *srcp = (char *) src;
    459      1.1    tron 	int i;
    460      1.1    tron 
    461      1.1    tron 	for (i = 0;  i < len;  i++)
    462      1.1    tron 		dstp[i] = srcp[i];
    463      1.1    tron 	return (dst);
    464      1.1    tron }
    465      1.1    tron #endif
    466      1.1    tron 
    467      1.1    tron #ifdef _OSK_MWC32
    468      1.1    tron 
    469      1.1    tron /*
    470      1.1    tron  * This implements an ANSI-style intercept setup for Microware C 3.2
    471      1.1    tron  */
    472  1.1.1.3  simonb public int os9_signal(int type, RETSIGTYPE (*handler)())
    473      1.1    tron {
    474      1.1    tron 	intercept(handler);
    475      1.1    tron }
    476      1.1    tron 
    477      1.1    tron #include <sgstat.h>
    478      1.1    tron 
    479  1.1.1.3  simonb int isatty(int f)
    480      1.1    tron {
    481      1.1    tron 	struct sgbuf sgbuf;
    482      1.1    tron 
    483      1.1    tron 	if (_gs_opt(f, &sgbuf) < 0)
    484      1.1    tron 		return -1;
    485      1.1    tron 	return (sgbuf.sg_class == 0);
    486      1.1    tron }
    487      1.1    tron 
    488      1.1    tron #endif
    489  1.1.1.3  simonb 
    490  1.1.1.3  simonb public void sleep_ms(int ms)
    491  1.1.1.3  simonb {
    492  1.1.1.3  simonb #if MSDOS_COMPILER==WIN32C
    493  1.1.1.3  simonb 	Sleep(ms);
    494  1.1.1.3  simonb #else
    495  1.1.1.3  simonb #if HAVE_NANOSLEEP
    496  1.1.1.3  simonb 	int sec = ms / 1000;
    497  1.1.1.3  simonb 	struct timespec t = { sec, (ms - sec*1000) * 1000000 };
    498  1.1.1.3  simonb 	nanosleep(&t, NULL);
    499  1.1.1.3  simonb #else
    500  1.1.1.3  simonb #if HAVE_USLEEP
    501  1.1.1.3  simonb 	usleep(ms);
    502  1.1.1.3  simonb #else
    503  1.1.1.3  simonb 	sleep(ms / 1000 + (ms % 1000 != 0));
    504  1.1.1.3  simonb #endif
    505  1.1.1.3  simonb #endif
    506  1.1.1.3  simonb #endif
    507  1.1.1.3  simonb }
    508