xref: /illumos-gate/usr/src/lib/libc/port/gen/lltostr.c (revision b6094a86b9116e02728dfbe9a1d4ecfa4cfd90f5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  *	lltostr -- convert long long to decimal string
34  *
35  *	char *
36  *	lltostr(value, ptr)
37  *	long long value;
38  *	char *ptr;
39  *
40  *	Ptr is assumed to point to the byte following a storage area
41  *	into which the decimal representation of "value" is to be
42  *	placed as a string.  Lltostr converts "value" to decimal and
43  *	produces the string, and returns a pointer to the beginning
44  *	of the string.  No leading zeroes are produced, and no
45  *	terminating null is produced.  The low-order digit of the
46  *	result always occupies memory position ptr-1.
47  *	Lltostr's behavior is undefined if "value" is negative.  A single
48  *	zero digit is produced if "value" is zero.
49  */
50 
51 #pragma weak _lltostr = lltostr
52 #pragma weak _ulltostr = ulltostr
53 
54 #include "lint.h"
55 #include <sys/types.h>
56 #include <stdlib.h>
57 
58 char *
59 lltostr(longlong_t value, char *ptr)
60 {
61 	longlong_t t;
62 
63 #ifdef _ILP32
64 	if (!(0xffffffff00000000ULL & value)) {
65 		ulong_t t, val = (ulong_t)value;
66 
67 		do {
68 			*--ptr = (char)('0' + val - 10 * (t = val / 10));
69 		} while ((val = t) != 0);
70 
71 		return (ptr);
72 	}
73 #endif
74 
75 	do {
76 		*--ptr = (char)('0' + value - 10 * (t = value / 10));
77 	} while ((value = t) != 0);
78 
79 	return (ptr);
80 }
81 
82 char *
83 ulltostr(u_longlong_t value, char *ptr)
84 {
85 	u_longlong_t t;
86 
87 #ifdef _ILP32
88 	if (!(0xffffffff00000000ULL & value)) {
89 		ulong_t t, val = (ulong_t)value;
90 
91 		do {
92 			*--ptr = (char)('0' + val - 10 * (t = val / 10));
93 		} while ((val = t) != 0);
94 
95 		return (ptr);
96 	}
97 #endif
98 
99 	do {
100 		*--ptr = (char)('0' + value - 10 * (t = value / 10));
101 	} while ((value = t) != 0);
102 
103 	return (ptr);
104 }
105