xref: /illumos-gate/usr/src/lib/libc/port/gen/lltostr.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 /*
31  *	lltostr -- convert long long to decimal string
32  *
33  *	char *
34  *	lltostr(value, ptr)
35  *	long long value;
36  *	char *ptr;
37  *
38  *	Ptr is assumed to point to the byte following a storage area
39  *	into which the decimal representation of "value" is to be
40  *	placed as a string.  Lltostr converts "value" to decimal and
41  *	produces the string, and returns a pointer to the beginning
42  *	of the string.  No leading zeroes are produced, and no
43  *	terminating null is produced.  The low-order digit of the
44  *	result always occupies memory position ptr-1.
45  *	Lltostr's behavior is undefined if "value" is negative.  A single
46  *	zero digit is produced if "value" is zero.
47  */
48 
49 #pragma weak _lltostr = lltostr
50 #pragma weak _ulltostr = ulltostr
51 
52 #include "lint.h"
53 #include <sys/types.h>
54 #include <stdlib.h>
55 
56 char *
57 lltostr(longlong_t value, char *ptr)
58 {
59 	longlong_t t;
60 
61 #ifdef _ILP32
62 	if (!(0xffffffff00000000ULL & value)) {
63 		ulong_t t, val = (ulong_t)value;
64 
65 		do {
66 			*--ptr = (char)('0' + val - 10 * (t = val / 10));
67 		} while ((val = t) != 0);
68 
69 		return (ptr);
70 	}
71 #endif
72 
73 	do {
74 		*--ptr = (char)('0' + value - 10 * (t = value / 10));
75 	} while ((value = t) != 0);
76 
77 	return (ptr);
78 }
79 
80 char *
81 ulltostr(u_longlong_t value, char *ptr)
82 {
83 	u_longlong_t t;
84 
85 #ifdef _ILP32
86 	if (!(0xffffffff00000000ULL & value)) {
87 		ulong_t t, val = (ulong_t)value;
88 
89 		do {
90 			*--ptr = (char)('0' + val - 10 * (t = val / 10));
91 		} while ((val = t) != 0);
92 
93 		return (ptr);
94 	}
95 #endif
96 
97 	do {
98 		*--ptr = (char)('0' + value - 10 * (t = value / 10));
99 	} while ((value = t) != 0);
100 
101 	return (ptr);
102 }
103