xref: /original-bsd/lib/libc/stdlib/ldiv.c (revision a6b493b4)
1 /*
2  * Copyright (c) 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)ldiv.c	5.1 (Berkeley) 05/16/90";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <stdlib.h>		/* ldiv_t */
16 
17 /*
18  * I AM NOT SURE THIS IS COMPLETELY PORTABLE
19  * (or that it is even right)
20  */
21 ldiv_t
22 ldiv(num, denom)
23 	long num, denom;
24 {
25 	ldiv_t r;
26 
27 	/* see div.c for comments */
28 
29 	if (num > 0 && denom < 0) {
30 		num = -num;
31 		denom = -denom;
32 	}
33 	r.quot = num / denom;
34 	r.rem = num % denom;
35 	if (num < 0 && denom > 0) {
36 		if (r.rem > 0) {
37 			r.quot++;
38 			r.rem -= denom;
39 		}
40 	}
41 	return (r);
42 }
43