Annotation of src/usr.bin/less/os.c, Revision 1.13
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: * errno_message: Return an error message based on the value of "errno".
60: */
1.12 nicm 61: char *
62: errno_message(char *filename)
1.1 etheisen 63: {
1.12 nicm 64: return (easprintf("%s: %s", filename, strerror(errno)));
1.1 etheisen 65: }
66:
1.12 nicm 67: static off_t
68: muldiv(off_t val, off_t num, off_t den)
1.8 shadchin 69: {
1.12 nicm 70: double v = (((double)val) * num) / den;
71: return ((off_t)(v + 0.5));
1.8 shadchin 72: }
73:
1.1 etheisen 74: /*
1.12 nicm 75: * Return the ratio of two off_t, as a percentage.
76: * {{ Assumes a off_t is a long int. }}
1.6 millert 77: */
1.12 nicm 78: int
79: percentage(off_t num, off_t den)
1.6 millert 80: {
1.12 nicm 81: return ((int)muldiv(num, (off_t)100, den));
1.6 millert 82: }
83:
84: /*
1.12 nicm 85: * Return the specified percentage of a off_t.
1.1 etheisen 86: */
1.12 nicm 87: off_t
88: percent_pos(off_t pos, int percent, long fraction)
1.1 etheisen 89: {
1.12 nicm 90: /*
91: * Change percent (parts per 100) to perden
92: * (parts per NUM_FRAC_DENOM).
93: */
94: off_t perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
1.6 millert 95:
1.8 shadchin 96: if (perden == 0)
1.6 millert 97: return (0);
1.12 nicm 98: return (muldiv(pos, perden, (off_t)NUM_FRAC_DENOM));
1.6 millert 99: }