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