Annotation of src/usr.bin/less/os.c, Revision 1.12
1.1 etheisen 1: /*
1.11 shadchin 2: * Copyright (C) 1984-2012 Mark Nudelman
1.1 etheisen 3: *
1.6 millert 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.
1.1 etheisen 6: *
1.11 shadchin 7: * For more information, see the README file.
1.1 etheisen 8: */
1.12 ! nicm 9: /*
! 10: * Modified for use with illumos.
! 11: * Copyright 2014 Garrett D'Amore <garrett@damore.org>
! 12: */
1.1 etheisen 13:
14: /*
15: * Operating system dependent routines.
16: *
17: * Most of the stuff in here is based on Unix, but an attempt
18: * has been made to make things work on other operating systems.
19: * This will sometimes result in a loss of functionality, unless
20: * someone rewrites code specifically for the new operating system.
21: *
22: * The makefile provides defines to decide whether various
23: * Unix features are present.
24: */
25:
26: #include "less.h"
27: #include <signal.h>
28: #include <time.h>
29: #include <errno.h>
30:
1.10 millert 31: extern volatile sig_atomic_t sigs;
1.6 millert 32:
1.1 etheisen 33: /*
34: * Like read() system call, but is deliberately interruptible.
35: */
1.12 ! nicm 36: int
! 37: iread(int fd, unsigned char *buf, unsigned int len)
1.1 etheisen 38: {
1.12 ! nicm 39: int n;
1.1 etheisen 40:
1.8 shadchin 41: start:
1.1 etheisen 42: flush();
43: n = read(fd, buf, len);
1.12 ! nicm 44: if (n < 0) {
1.8 shadchin 45: /*
1.12 ! nicm 46: * Certain values of errno indicate we should just retry the
! 47: * read.
1.8 shadchin 48: */
49: if (errno == EINTR)
1.9 millert 50: return (READ_INTR);
1.8 shadchin 51: if (errno == EAGAIN)
52: goto start;
53: return (-1);
54: }
1.1 etheisen 55: return (n);
56: }
57:
58: /*
59: * Return the current time.
60: */
1.12 ! nicm 61: long
! 62: get_time(void)
1.1 etheisen 63: {
1.12 ! nicm 64: time_t t;
1.1 etheisen 65:
1.12 ! nicm 66: (void) time(&t);
1.1 etheisen 67: return (t);
68: }
69:
70: /*
71: * errno_message: Return an error message based on the value of "errno".
72: */
1.12 ! nicm 73: char *
! 74: errno_message(char *filename)
1.1 etheisen 75: {
1.12 ! nicm 76: return (easprintf("%s: %s", filename, strerror(errno)));
1.1 etheisen 77: }
78:
1.12 ! nicm 79: static off_t
! 80: muldiv(off_t val, off_t num, off_t den)
1.8 shadchin 81: {
1.12 ! nicm 82: double v = (((double)val) * num) / den;
! 83: return ((off_t)(v + 0.5));
1.8 shadchin 84: }
85:
1.1 etheisen 86: /*
1.12 ! nicm 87: * Return the ratio of two off_t, as a percentage.
! 88: * {{ Assumes a off_t is a long int. }}
1.6 millert 89: */
1.12 ! nicm 90: int
! 91: percentage(off_t num, off_t den)
1.6 millert 92: {
1.12 ! nicm 93: return ((int)muldiv(num, (off_t)100, den));
1.6 millert 94: }
95:
96: /*
1.12 ! nicm 97: * Return the specified percentage of a off_t.
1.1 etheisen 98: */
1.12 ! nicm 99: off_t
! 100: percent_pos(off_t pos, int percent, long fraction)
1.1 etheisen 101: {
1.12 ! nicm 102: /*
! 103: * Change percent (parts per 100) to perden
! 104: * (parts per NUM_FRAC_DENOM).
! 105: */
! 106: off_t perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
1.6 millert 107:
1.8 shadchin 108: if (perden == 0)
1.6 millert 109: return (0);
1.12 ! nicm 110: return (muldiv(pos, perden, (off_t)NUM_FRAC_DENOM));
1.6 millert 111: }