Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/fmtulong.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* fmtulong.c -- Convert unsigned long int to string. */ | - | ||||||||||||||||||||||||
2 | - | |||||||||||||||||||||||||
3 | /* Copyright (C) 1998-2011 Free Software Foundation, Inc. | - | ||||||||||||||||||||||||
4 | - | |||||||||||||||||||||||||
5 | This file is part of GNU Bash, the Bourne Again SHell. | - | ||||||||||||||||||||||||
6 | - | |||||||||||||||||||||||||
7 | 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. | - | ||||||||||||||||||||||||
11 | - | |||||||||||||||||||||||||
12 | 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. | - | ||||||||||||||||||||||||
16 | - | |||||||||||||||||||||||||
17 | 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 | */ | - | ||||||||||||||||||||||||
20 | - | |||||||||||||||||||||||||
21 | #ifdef HAVE_CONFIG_H | - | ||||||||||||||||||||||||
22 | # include <config.h> | - | ||||||||||||||||||||||||
23 | #endif | - | ||||||||||||||||||||||||
24 | - | |||||||||||||||||||||||||
25 | #if defined (HAVE_UNISTD_H) | - | ||||||||||||||||||||||||
26 | # include <unistd.h> | - | ||||||||||||||||||||||||
27 | #endif | - | ||||||||||||||||||||||||
28 | - | |||||||||||||||||||||||||
29 | #if defined (HAVE_LIMITS_H) | - | ||||||||||||||||||||||||
30 | # include <limits.h> | - | ||||||||||||||||||||||||
31 | #endif | - | ||||||||||||||||||||||||
32 | - | |||||||||||||||||||||||||
33 | #include <bashansi.h> | - | ||||||||||||||||||||||||
34 | #ifdef HAVE_STDDEF_H | - | ||||||||||||||||||||||||
35 | # include <stddef.h> | - | ||||||||||||||||||||||||
36 | #endif | - | ||||||||||||||||||||||||
37 | - | |||||||||||||||||||||||||
38 | #ifdef HAVE_STDINT_H | - | ||||||||||||||||||||||||
39 | # include <stdint.h> | - | ||||||||||||||||||||||||
40 | #endif | - | ||||||||||||||||||||||||
41 | #ifdef HAVE_INTTYPES_H | - | ||||||||||||||||||||||||
42 | # include <inttypes.h> | - | ||||||||||||||||||||||||
43 | #endif | - | ||||||||||||||||||||||||
44 | #include <chartypes.h> | - | ||||||||||||||||||||||||
45 | #include <errno.h> | - | ||||||||||||||||||||||||
46 | - | |||||||||||||||||||||||||
47 | #include <bashintl.h> | - | ||||||||||||||||||||||||
48 | - | |||||||||||||||||||||||||
49 | #include "stdc.h" | - | ||||||||||||||||||||||||
50 | - | |||||||||||||||||||||||||
51 | #include <typemax.h> | - | ||||||||||||||||||||||||
52 | - | |||||||||||||||||||||||||
53 | #ifndef errno | - | ||||||||||||||||||||||||
54 | extern int errno; | - | ||||||||||||||||||||||||
55 | #endif | - | ||||||||||||||||||||||||
56 | - | |||||||||||||||||||||||||
57 | #define x_digs "0123456789abcdef" | - | ||||||||||||||||||||||||
58 | #define X_digs "0123456789ABCDEF" | - | ||||||||||||||||||||||||
59 | - | |||||||||||||||||||||||||
60 | /* XXX -- assumes uppercase letters, lowercase letters, and digits are | - | ||||||||||||||||||||||||
61 | contiguous */ | - | ||||||||||||||||||||||||
62 | #define FMTCHAR(x) \ | - | ||||||||||||||||||||||||
63 | ((x) < 10) ? (x) + '0' \ | - | ||||||||||||||||||||||||
64 | : (((x) < 36) ? (x) - 10 + 'a' \ | - | ||||||||||||||||||||||||
65 | : (((x) < 62) ? (x) - 36 + 'A' \ | - | ||||||||||||||||||||||||
66 | : (((x) == 62) ? '@' : '_'))) | - | ||||||||||||||||||||||||
67 | - | |||||||||||||||||||||||||
68 | #ifndef FL_PREFIX | - | ||||||||||||||||||||||||
69 | # define FL_PREFIX 0x01 /* add 0x, 0X, or 0 prefix as appropriate */ | - | ||||||||||||||||||||||||
70 | # define FL_ADDBASE 0x02 /* add base# prefix to converted value */ | - | ||||||||||||||||||||||||
71 | # define FL_HEXUPPER 0x04 /* use uppercase when converting to hex */ | - | ||||||||||||||||||||||||
72 | # define FL_UNSIGNED 0x08 /* don't add any sign */ | - | ||||||||||||||||||||||||
73 | #endif | - | ||||||||||||||||||||||||
74 | - | |||||||||||||||||||||||||
75 | #ifndef LONG | - | ||||||||||||||||||||||||
76 | # define LONG long | - | ||||||||||||||||||||||||
77 | # define UNSIGNED_LONG unsigned long | - | ||||||||||||||||||||||||
78 | #endif | - | ||||||||||||||||||||||||
79 | - | |||||||||||||||||||||||||
80 | /* `unsigned long' (or unsigned long long) to string conversion for a given | - | ||||||||||||||||||||||||
81 | base. The caller passes the output buffer and the size. This should | - | ||||||||||||||||||||||||
82 | check for buffer underflow, but currently does not. */ | - | ||||||||||||||||||||||||
83 | char * | - | ||||||||||||||||||||||||
84 | fmtulong (ui, base, buf, len, flags) | - | ||||||||||||||||||||||||
85 | UNSIGNED_LONG ui; | - | ||||||||||||||||||||||||
86 | int base; | - | ||||||||||||||||||||||||
87 | char *buf; | - | ||||||||||||||||||||||||
88 | size_t len; | - | ||||||||||||||||||||||||
89 | int flags; | - | ||||||||||||||||||||||||
90 | { | - | ||||||||||||||||||||||||
91 | char *p; | - | ||||||||||||||||||||||||
92 | int sign; | - | ||||||||||||||||||||||||
93 | LONG si; | - | ||||||||||||||||||||||||
94 | - | |||||||||||||||||||||||||
95 | if (base == 0)
| 0-95950340 | ||||||||||||||||||||||||
96 | base = 10; never executed: base = 10; | 0 | ||||||||||||||||||||||||
97 | - | |||||||||||||||||||||||||
98 | if (base < 2 || base > 64)
| 0-95950340 | ||||||||||||||||||||||||
99 | { | - | ||||||||||||||||||||||||
100 | #if 1 | - | ||||||||||||||||||||||||
101 | /* XXX - truncation possible with long translation */ | - | ||||||||||||||||||||||||
102 | strncpy (buf, _("invalid base"), len - 1); | - | ||||||||||||||||||||||||
103 | buf[len-1] = '\0'; | - | ||||||||||||||||||||||||
104 | errno = EINVAL; | - | ||||||||||||||||||||||||
105 | return (p = buf); never executed: return (p = buf); | 0 | ||||||||||||||||||||||||
106 | #else | - | ||||||||||||||||||||||||
107 | base = 10; | - | ||||||||||||||||||||||||
108 | #endif | - | ||||||||||||||||||||||||
109 | } | - | ||||||||||||||||||||||||
110 | - | |||||||||||||||||||||||||
111 | sign = 0; | - | ||||||||||||||||||||||||
112 | if ((flags & FL_UNSIGNED) == 0 && (LONG)ui < 0)
| 1267-95948465 | ||||||||||||||||||||||||
113 | { | - | ||||||||||||||||||||||||
114 | ui = -ui; | - | ||||||||||||||||||||||||
115 | sign = '-'; | - | ||||||||||||||||||||||||
116 | } executed 1267 times by 1 test: end of block Executed by:
| 1267 | ||||||||||||||||||||||||
117 | - | |||||||||||||||||||||||||
118 | p = buf + len - 2; | - | ||||||||||||||||||||||||
119 | p[1] = '\0'; | - | ||||||||||||||||||||||||
120 | - | |||||||||||||||||||||||||
121 | /* handle common cases explicitly */ | - | ||||||||||||||||||||||||
122 | switch (base) | - | ||||||||||||||||||||||||
123 | { | - | ||||||||||||||||||||||||
124 | case 10: executed 95948927 times by 1 test: case 10: Executed by:
| 95948927 | ||||||||||||||||||||||||
125 | if (ui < 10)
| 9495036-86453891 | ||||||||||||||||||||||||
126 | { | - | ||||||||||||||||||||||||
127 | *p-- = TOCHAR (ui); | - | ||||||||||||||||||||||||
128 | break; executed 86453891 times by 1 test: break; Executed by:
| 86453891 | ||||||||||||||||||||||||
129 | } | - | ||||||||||||||||||||||||
130 | /* Favor signed arithmetic over unsigned arithmetic; it is faster on | - | ||||||||||||||||||||||||
131 | many machines. */ | - | ||||||||||||||||||||||||
132 | if ((LONG)ui < 0)
| 12-9495024 | ||||||||||||||||||||||||
133 | { | - | ||||||||||||||||||||||||
134 | *p-- = TOCHAR (ui % 10); | - | ||||||||||||||||||||||||
135 | si = ui / 10; | - | ||||||||||||||||||||||||
136 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||||||||
137 | else | - | ||||||||||||||||||||||||
138 | si = ui; executed 9495024 times by 1 test: si = ui; Executed by:
| 9495024 | ||||||||||||||||||||||||
139 | do | - | ||||||||||||||||||||||||
140 | *p-- = TOCHAR (si % 10); executed 32688266 times by 1 test: *p-- = ((si % 10) + '0'); Executed by:
| 32688266 | ||||||||||||||||||||||||
141 | while (si /= 10);
| 9495036-23193230 | ||||||||||||||||||||||||
142 | break; executed 9495036 times by 1 test: break; Executed by:
| 9495036 | ||||||||||||||||||||||||
143 | - | |||||||||||||||||||||||||
144 | case 8: executed 14 times by 1 test: case 8: Executed by:
| 14 | ||||||||||||||||||||||||
145 | do | - | ||||||||||||||||||||||||
146 | *p-- = TOCHAR (ui & 7); executed 38 times by 1 test: *p-- = ((ui & 7) + '0'); Executed by:
| 38 | ||||||||||||||||||||||||
147 | while (ui >>= 3);
| 14-24 | ||||||||||||||||||||||||
148 | break; executed 14 times by 1 test: break; Executed by:
| 14 | ||||||||||||||||||||||||
149 | - | |||||||||||||||||||||||||
150 | case 16: executed 1399 times by 1 test: case 16: Executed by:
| 1399 | ||||||||||||||||||||||||
151 | do | - | ||||||||||||||||||||||||
152 | *p-- = (flags & FL_HEXUPPER) ? X_digs[ui & 15] : x_digs[ui & 15]; executed 3498 times by 1 test: *p-- = (flags & 0x04) ? "0123456789ABCDEF"[ui & 15] : "0123456789abcdef"[ui & 15]; Executed by:
| 20-3498 | ||||||||||||||||||||||||
153 | while (ui >>= 4);
| 1399-2099 | ||||||||||||||||||||||||
154 | break; executed 1399 times by 1 test: break; Executed by:
| 1399 | ||||||||||||||||||||||||
155 | - | |||||||||||||||||||||||||
156 | case 2: never executed: case 2: | 0 | ||||||||||||||||||||||||
157 | do | - | ||||||||||||||||||||||||
158 | *p-- = TOCHAR (ui & 1); never executed: *p-- = ((ui & 1) + '0'); | 0 | ||||||||||||||||||||||||
159 | while (ui >>= 1);
| 0 | ||||||||||||||||||||||||
160 | break; never executed: break; | 0 | ||||||||||||||||||||||||
161 | - | |||||||||||||||||||||||||
162 | default: never executed: default: | 0 | ||||||||||||||||||||||||
163 | do | - | ||||||||||||||||||||||||
164 | *p-- = FMTCHAR (ui % base); never executed: *p-- = ((ui % base) < 10) ? (ui % base) + '0' : (((ui % base) < 36) ? (ui % base) - 10 + 'a' : (((ui % base) < 62) ? (ui % base) - 36 + 'A' : (((ui % base) == 62) ? '@' : '_')));
| 0 | ||||||||||||||||||||||||
165 | while (ui /= base);
| 0 | ||||||||||||||||||||||||
166 | break; never executed: break; | 0 | ||||||||||||||||||||||||
167 | } | - | ||||||||||||||||||||||||
168 | - | |||||||||||||||||||||||||
169 | if ((flags & FL_PREFIX) && (base == 8 || base == 16))
| 0-95950340 | ||||||||||||||||||||||||
170 | { | - | ||||||||||||||||||||||||
171 | if (base == 16)
| 0 | ||||||||||||||||||||||||
172 | { | - | ||||||||||||||||||||||||
173 | *p-- = (flags & FL_HEXUPPER) ? 'X' : 'x';
| 0 | ||||||||||||||||||||||||
174 | *p-- = '0'; | - | ||||||||||||||||||||||||
175 | } never executed: end of block | 0 | ||||||||||||||||||||||||
176 | else if (p[1] != '0')
| 0 | ||||||||||||||||||||||||
177 | *p-- = '0'; never executed: *p-- = '0'; | 0 | ||||||||||||||||||||||||
178 | } never executed: end of block | 0 | ||||||||||||||||||||||||
179 | else if ((flags & FL_ADDBASE) && base != 10)
| 0-95950340 | ||||||||||||||||||||||||
180 | { | - | ||||||||||||||||||||||||
181 | *p-- = '#'; | - | ||||||||||||||||||||||||
182 | *p-- = TOCHAR (base % 10); | - | ||||||||||||||||||||||||
183 | if (base > 10)
| 0 | ||||||||||||||||||||||||
184 | *p-- = TOCHAR (base / 10); never executed: *p-- = ((base / 10) + '0'); | 0 | ||||||||||||||||||||||||
185 | } never executed: end of block | 0 | ||||||||||||||||||||||||
186 | - | |||||||||||||||||||||||||
187 | if (sign)
| 1267-95949073 | ||||||||||||||||||||||||
188 | *p-- = '-'; executed 1267 times by 1 test: *p-- = '-'; Executed by:
| 1267 | ||||||||||||||||||||||||
189 | - | |||||||||||||||||||||||||
190 | return (p + 1); executed 95950340 times by 1 test: return (p + 1); Executed by:
| 95950340 | ||||||||||||||||||||||||
191 | } | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |