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