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