1*89a07cf8Schristos /* $NetBSD: itoa.c,v 1.1.1.1 2016/01/13 18:41:48 christos Exp $ */
2*89a07cf8Schristos
3*89a07cf8Schristos /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002, 2004
4*89a07cf8Schristos Free Software Foundation, Inc.
5*89a07cf8Schristos Written by James Clark (jjc@jclark.com)
6*89a07cf8Schristos
7*89a07cf8Schristos This file is part of groff.
8*89a07cf8Schristos
9*89a07cf8Schristos groff is free software; you can redistribute it and/or modify it under
10*89a07cf8Schristos the terms of the GNU General Public License as published by the Free
11*89a07cf8Schristos Software Foundation; either version 2, or (at your option) any later
12*89a07cf8Schristos version.
13*89a07cf8Schristos
14*89a07cf8Schristos groff is distributed in the hope that it will be useful, but WITHOUT ANY
15*89a07cf8Schristos WARRANTY; without even the implied warranty of MERCHANTABILITY or
16*89a07cf8Schristos FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17*89a07cf8Schristos for more details.
18*89a07cf8Schristos
19*89a07cf8Schristos You should have received a copy of the GNU General Public License along
20*89a07cf8Schristos with groff; see the file COPYING. If not, write to the Free Software
21*89a07cf8Schristos Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
22*89a07cf8Schristos
23*89a07cf8Schristos #define INT_DIGITS 19 /* enough for 64 bit integer */
24*89a07cf8Schristos #define UINT_DIGITS 20
25*89a07cf8Schristos
26*89a07cf8Schristos #ifdef __cplusplus
27*89a07cf8Schristos extern "C" {
28*89a07cf8Schristos #endif
29*89a07cf8Schristos
i_to_a(int i)30*89a07cf8Schristos char *i_to_a(int i)
31*89a07cf8Schristos {
32*89a07cf8Schristos /* Room for INT_DIGITS digits, - and '\0' */
33*89a07cf8Schristos static char buf[INT_DIGITS + 2];
34*89a07cf8Schristos char *p = buf + INT_DIGITS + 1; /* points to terminating '\0' */
35*89a07cf8Schristos if (i >= 0) {
36*89a07cf8Schristos do {
37*89a07cf8Schristos *--p = '0' + (i % 10);
38*89a07cf8Schristos i /= 10;
39*89a07cf8Schristos } while (i != 0);
40*89a07cf8Schristos return p;
41*89a07cf8Schristos }
42*89a07cf8Schristos else { /* i < 0 */
43*89a07cf8Schristos do {
44*89a07cf8Schristos *--p = '0' - (i % 10);
45*89a07cf8Schristos i /= 10;
46*89a07cf8Schristos } while (i != 0);
47*89a07cf8Schristos *--p = '-';
48*89a07cf8Schristos }
49*89a07cf8Schristos return p;
50*89a07cf8Schristos }
51*89a07cf8Schristos
ui_to_a(unsigned int i)52*89a07cf8Schristos char *ui_to_a(unsigned int i)
53*89a07cf8Schristos {
54*89a07cf8Schristos /* Room for UINT_DIGITS digits and '\0' */
55*89a07cf8Schristos static char buf[UINT_DIGITS + 1];
56*89a07cf8Schristos char *p = buf + UINT_DIGITS; /* points to terminating '\0' */
57*89a07cf8Schristos do {
58*89a07cf8Schristos *--p = '0' + (i % 10);
59*89a07cf8Schristos i /= 10;
60*89a07cf8Schristos } while (i != 0);
61*89a07cf8Schristos return p;
62*89a07cf8Schristos }
63*89a07cf8Schristos
64*89a07cf8Schristos #ifdef __cplusplus
65*89a07cf8Schristos }
66*89a07cf8Schristos #endif
67