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