blob: cd36ef33ff5338bfc84703403106c9f331942780 [file] [log] [blame]
Jari Aaltocce855b1998-04-17 19:52:44 +00001/* itos.c -- Convert integer to string. */
2
Jari Aalto7117c2d2002-07-17 14:10:11 +00003/* Copyright (C) 1998-2002 Free Software Foundation, Inc.
Jari Aaltocce855b1998-04-17 19:52:44 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
Jari Aaltocce855b1998-04-17 19:52:44 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aaltocce855b1998-04-17 19:52:44 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aaltocce855b1998-04-17 19:52:44 +000020
Jari Aaltof73dda02001-11-13 17:56:06 +000021#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
Jari Aaltocce855b1998-04-17 19:52:44 +000024
25#if defined (HAVE_UNISTD_H)
26# include <unistd.h>
27#endif
28
Jari Aaltof73dda02001-11-13 17:56:06 +000029#include <bashansi.h>
Jari Aaltocce855b1998-04-17 19:52:44 +000030#include "shell.h"
31
Jari Aaltocce855b1998-04-17 19:52:44 +000032char *
Jari Aaltob72432f1999-02-19 17:11:39 +000033inttostr (i, buf, len)
Jari Aalto7117c2d2002-07-17 14:10:11 +000034 intmax_t i;
Jari Aaltob72432f1999-02-19 17:11:39 +000035 char *buf;
Jari Aaltof73dda02001-11-13 17:56:06 +000036 size_t len;
Jari Aaltocce855b1998-04-17 19:52:44 +000037{
Jari Aalto7117c2d2002-07-17 14:10:11 +000038 return (fmtumax (i, 10, buf, len, 0));
Jari Aaltob72432f1999-02-19 17:11:39 +000039}
40
41/* Integer to string conversion. This conses the string; the
42 caller should free it. */
43char *
44itos (i)
Jari Aalto7117c2d2002-07-17 14:10:11 +000045 intmax_t i;
Jari Aaltob72432f1999-02-19 17:11:39 +000046{
Jari Aalto7117c2d2002-07-17 14:10:11 +000047 char *p, lbuf[INT_STRLEN_BOUND(intmax_t) + 1];
Jari Aaltob72432f1999-02-19 17:11:39 +000048
Jari Aalto7117c2d2002-07-17 14:10:11 +000049 p = fmtumax (i, 10, lbuf, sizeof(lbuf), 0);
Jari Aaltof73dda02001-11-13 17:56:06 +000050 return (savestring (p));
51}
52
Chet Rameyac50fba2014-02-26 09:36:43 -050053/* Integer to string conversion. This conses the string using strdup;
54 caller should free it and be prepared to deal with NULL return. */
55char *
56mitos (i)
57 intmax_t i;
58{
59 char *p, lbuf[INT_STRLEN_BOUND(intmax_t) + 1];
60
61 p = fmtumax (i, 10, lbuf, sizeof(lbuf), 0);
62 return (strdup (p));
63}
64
Jari Aaltof73dda02001-11-13 17:56:06 +000065char *
66uinttostr (i, buf, len)
Jari Aalto7117c2d2002-07-17 14:10:11 +000067 uintmax_t i;
Jari Aaltof73dda02001-11-13 17:56:06 +000068 char *buf;
69 size_t len;
70{
Jari Aalto7117c2d2002-07-17 14:10:11 +000071 return (fmtumax (i, 10, buf, len, FL_UNSIGNED));
Jari Aaltof73dda02001-11-13 17:56:06 +000072}
73
74/* Integer to string conversion. This conses the string; the
75 caller should free it. */
76char *
77uitos (i)
Jari Aalto7117c2d2002-07-17 14:10:11 +000078 uintmax_t i;
Jari Aaltof73dda02001-11-13 17:56:06 +000079{
Jari Aalto7117c2d2002-07-17 14:10:11 +000080 char *p, lbuf[INT_STRLEN_BOUND(uintmax_t) + 1];
Jari Aaltof73dda02001-11-13 17:56:06 +000081
Jari Aalto7117c2d2002-07-17 14:10:11 +000082 p = fmtumax (i, 10, lbuf, sizeof(lbuf), FL_UNSIGNED);
Jari Aaltob72432f1999-02-19 17:11:39 +000083 return (savestring (p));
Jari Aaltocce855b1998-04-17 19:52:44 +000084}