itoa.c revision 151497
10SN/A/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002, 2004 22362SN/A Free Software Foundation, Inc. 30SN/A Written by James Clark (jjc@jclark.com) 40SN/A 50SN/AThis file is part of groff. 60SN/A 72362SN/Agroff is free software; you can redistribute it and/or modify it under 80SN/Athe terms of the GNU General Public License as published by the Free 92362SN/ASoftware Foundation; either version 2, or (at your option) any later 100SN/Aversion. 110SN/A 120SN/Agroff is distributed in the hope that it will be useful, but WITHOUT ANY 130SN/AWARRANTY; without even the implied warranty of MERCHANTABILITY or 140SN/AFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 150SN/Afor more details. 160SN/A 170SN/AYou should have received a copy of the GNU General Public License along 180SN/Awith groff; see the file COPYING. If not, write to the Free Software 190SN/AFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ 200SN/A 212362SN/A#define INT_DIGITS 19 /* enough for 64 bit integer */ 222362SN/A#define UINT_DIGITS 20 232362SN/A 240SN/A#ifdef __cplusplus 250SN/Aextern "C" { 2614176Schegar#endif 270SN/A 280SN/Achar *i_to_a(int i) 290SN/A{ 300SN/A /* Room for INT_DIGITS digits, - and '\0' */ 310SN/A static char buf[INT_DIGITS + 2]; 320SN/A char *p = buf + INT_DIGITS + 1; /* points to terminating '\0' */ 330SN/A if (i >= 0) { 340SN/A do { 350SN/A *--p = '0' + (i % 10); 360SN/A i /= 10; 370SN/A } while (i != 0); 3810608SN/A return p; 390SN/A } 400SN/A else { /* i < 0 */ 410SN/A do { 420SN/A *--p = '0' - (i % 10); 430SN/A i /= 10; 440SN/A } while (i != 0); 450SN/A *--p = '-'; 460SN/A } 470SN/A return p; 480SN/A} 490SN/A 500SN/Achar *ui_to_a(unsigned int i) 510SN/A{ 520SN/A /* Room for UINT_DIGITS digits and '\0' */ 530SN/A static char buf[UINT_DIGITS + 1]; 540SN/A char *p = buf + UINT_DIGITS; /* points to terminating '\0' */ 550SN/A do { 560SN/A *--p = '0' + (i % 10); 570SN/A i /= 10; 580SN/A } while (i != 0); 590SN/A return p; 600SN/A} 610SN/A 620SN/A#ifdef __cplusplus 630SN/A} 640SN/A#endif 650SN/A