Deleted Added
full compact
os.c (195941) os.c (221715)
1/*
1/*
2 * Copyright (C) 1984-2009 Mark Nudelman
2 * Copyright (C) 1984-2011 Mark Nudelman
3 *
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.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10

--- 234 unchanged lines hidden (view full) ---

245 p = "cannot open";
246#endif
247 len = strlen(filename) + strlen(p) + 3;
248 m = (char *) ecalloc(len, sizeof(char));
249 SNPRINTF2(m, len, "%s: %s", filename, p);
250 return (m);
251}
252
3 *
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.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10

--- 234 unchanged lines hidden (view full) ---

245 p = "cannot open";
246#endif
247 len = strlen(filename) + strlen(p) + 3;
248 m = (char *) ecalloc(len, sizeof(char));
249 SNPRINTF2(m, len, "%s: %s", filename, p);
250 return (m);
251}
252
253/* #define HAVE_FLOAT 0 */
254
255 static POSITION
256muldiv(val, num, den)
257 POSITION val, num, den;
258{
259#if HAVE_FLOAT
260 double v = (((double) val) * num) / den;
261 return ((POSITION) (v + 0.5));
262#else
263 POSITION v = ((POSITION) val) * num;
264
265 if (v / num == val)
266 /* No overflow */
267 return (POSITION) (v / den);
268 else
269 /* Above calculation overflows;
270 * use a method that is less precise but won't overflow. */
271 return (POSITION) (val / (den / num));
272#endif
273}
274
253/*
254 * Return the ratio of two POSITIONS, as a percentage.
255 * {{ Assumes a POSITION is a long int. }}
256 */
257 public int
258percentage(num, den)
259 POSITION num, den;
260{
275/*
276 * Return the ratio of two POSITIONS, as a percentage.
277 * {{ Assumes a POSITION is a long int. }}
278 */
279 public int
280percentage(num, den)
281 POSITION num, den;
282{
261 POSITION num100 = num * 100;
262
263 if (num100 / 100 == num)
264 return (num100 / den);
265 else
266 return (num / (den / 100));
283 return (int) muldiv(num, (POSITION) 100, den);
267}
268
269/*
270 * Return the specified percentage of a POSITION.
271 */
272 public POSITION
273percent_pos(pos, percent, fraction)
274 POSITION pos;
275 int percent;
276 long fraction;
277{
278 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
284}
285
286/*
287 * Return the specified percentage of a POSITION.
288 */
289 public POSITION
290percent_pos(pos, percent, fraction)
291 POSITION pos;
292 int percent;
293 long fraction;
294{
295 /* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
279 long perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
280 POSITION temp;
296 POSITION perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
281
282 if (perden == 0)
283 return (0);
297
298 if (perden == 0)
299 return (0);
284 temp = pos * perden; /* This might overflow. */
285 if (temp / perden == pos)
286 /* No overflow */
287 return (temp / NUM_FRAC_DENOM);
288 else
289 /* Above calculation overflows;
290 * use a method that is less precise but won't overflow. */
291 return (perden * (pos / NUM_FRAC_DENOM));
300 return (POSITION) muldiv(pos, perden, (POSITION) NUM_FRAC_DENOM);
292}
293
294#if !HAVE_STRCHR
295/*
296 * strchr is used by regexp.c.
297 */
298 char *
299strchr(s, c)

--- 56 unchanged lines hidden ---
301}
302
303#if !HAVE_STRCHR
304/*
305 * strchr is used by regexp.c.
306 */
307 char *
308strchr(s, c)

--- 56 unchanged lines hidden ---