ntoab.c (2276B)
1 /* -*-comment-start: "//";comment-end:""-*- 2 * GNU Mes --- Maxwell Equations of Software 3 * Copyright © 2016,2017,2018,2019,2020,2022 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> 4 * Copyright © 2024 Michael Forney <mforney@mforney.org> 5 * 6 * This file is part of GNU Mes. 7 * 8 * GNU Mes is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 3 of the License, or (at 11 * your option) any later version. 12 * 13 * GNU Mes is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #include <mes/lib.h> 23 #include <assert.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 #if __MESC__ && __arm__ 28 #define __MESC__and__arm__ 29 #endif 30 31 #if __TINYC__ && __arm__ && BOOTSTRAP 32 #define __TINYC__and__arm__and__BOOTSTRAP 33 #endif 34 35 #define __not__MESC__arm__and__not__TINYC__arm__BOOTSTRAP !defined (__MESC__and__arm__) && !defined (__TINYC__and__arm__and__BOOTSTRAP) 36 37 // FIXME: M2-Planet 1.10.0 crashes on this... 38 // #if __M2__ || (!defined (__MESC__and__arm__) && !defined (__TINYC__and__arm__and__BOOTSTRAP)) 39 #if __M2__ || __not__MESC__arm__and__not__TINYC__arm__BOOTSTRAP 40 size_t 41 __mesabi_uldiv (size_t a, size_t b, size_t *remainder) 42 { 43 remainder[0] = a % b; 44 return a / b; 45 } 46 #endif 47 48 char *__itoa_buf; 49 50 char * 51 ntoab (long x, unsigned base, int signed_p) 52 { 53 if (__itoa_buf == 0) 54 __itoa_buf = malloc (24); 55 char *p = __itoa_buf + 23; 56 57 assert_msg (base >= 8, "base >= 8"); 58 59 int sign_p = 0; 60 size_t i; 61 size_t u; 62 size_t b = base; 63 if (signed_p != 0 && x < 0) 64 { 65 sign_p = 1; 66 /* Avoid LONG_MIN */ 67 u = (-(x + 1)); 68 u = u + 1; 69 } 70 else 71 u = x; 72 73 p[0] = 0; 74 do 75 { 76 p = p - 1; 77 u = __mesabi_uldiv (u, b, &i); 78 if (i > 9) 79 p[0] = 'a' + i - 10; 80 else 81 p[0] = '0' + i; 82 } 83 while (u != 0); 84 85 if (sign_p && p[0] != '0') 86 { 87 p = p - 1; 88 p[0] = '-'; 89 } 90 91 return p; 92 }