Annotation of src/usr.bin/less/os.c, Revision 1.17
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:
1.16 mmcc 24: #include <errno.h>
1.1 etheisen 25: #include <signal.h>
26: #include <time.h>
1.16 mmcc 27:
28: #include "less.h"
1.1 etheisen 29:
1.10 millert 30: extern volatile sig_atomic_t sigs;
1.6 millert 31:
1.1 etheisen 32: /*
33: * Like read() system call, but is deliberately interruptible.
34: */
1.12 nicm 35: int
36: iread(int fd, unsigned char *buf, unsigned int len)
1.1 etheisen 37: {
1.12 nicm 38: int n;
1.1 etheisen 39:
1.8 shadchin 40: start:
1.15 nicm 41: flush(0);
1.1 etheisen 42: n = read(fd, buf, len);
1.17 ! deraadt 43: if (n == -1) {
1.8 shadchin 44: /*
1.12 nicm 45: * Certain values of errno indicate we should just retry the
46: * read.
1.8 shadchin 47: */
48: if (errno == EINTR)
1.9 millert 49: return (READ_INTR);
1.8 shadchin 50: if (errno == EAGAIN)
51: goto start;
52: return (-1);
53: }
1.1 etheisen 54: return (n);
55: }
56:
57: /*
58: * errno_message: Return an error message based on the value of "errno".
59: */
1.12 nicm 60: char *
61: errno_message(char *filename)
1.1 etheisen 62: {
1.12 nicm 63: return (easprintf("%s: %s", filename, strerror(errno)));
1.1 etheisen 64: }
65:
1.12 nicm 66: static off_t
67: muldiv(off_t val, off_t num, off_t den)
1.8 shadchin 68: {
1.12 nicm 69: double v = (((double)val) * num) / den;
70: return ((off_t)(v + 0.5));
1.8 shadchin 71: }
72:
1.1 etheisen 73: /*
1.12 nicm 74: * Return the ratio of two off_t, as a percentage.
75: * {{ Assumes a off_t is a long int. }}
1.6 millert 76: */
1.12 nicm 77: int
78: percentage(off_t num, off_t den)
1.6 millert 79: {
1.12 nicm 80: return ((int)muldiv(num, (off_t)100, den));
1.6 millert 81: }
82:
83: /*
1.12 nicm 84: * Return the specified percentage of a off_t.
1.1 etheisen 85: */
1.12 nicm 86: off_t
87: percent_pos(off_t pos, int percent, long fraction)
1.1 etheisen 88: {
1.12 nicm 89: /*
90: * Change percent (parts per 100) to perden
91: * (parts per NUM_FRAC_DENOM).
92: */
93: off_t perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
1.6 millert 94:
1.8 shadchin 95: if (perden == 0)
1.6 millert 96: return (0);
1.12 nicm 97: return (muldiv(pos, perden, (off_t)NUM_FRAC_DENOM));
1.6 millert 98: }