1 // SPDX-License-Identifier: 0BSD
2
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file util.c
6 /// \brief Miscellaneous utility functions
7 //
8 // Author: Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "private.h"
13 #include <stdarg.h>
14
15 #if defined(_WIN32) && !defined(__CYGWIN__)
16 # include <io.h>
17 #endif
18
19
20 /// Buffers for uint64_to_str() and uint64_to_nicestr()
21 static char bufs[4][128];
22
23
24 // Thousand separator support in uint64_to_str() and uint64_to_nicestr():
25 //
26 // DJGPP 2.05 added support for thousands separators but it's broken
27 // at least under WinXP with Finnish locale that uses a non-breaking space
28 // as the thousands separator. Workaround by disabling thousands separators
29 // for DJGPP builds.
30 //
31 // MSVC doesn't support thousand separators.
32 //
33 // MinGW-w64 supports thousand separators only with its own stdio functions
34 // which our sysdefs.h disables when _UCRT && HAVE_SMALL.
35 #if defined(__DJGPP__) || defined(_MSC_VER) \
36 || (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0)
37 # define FORMAT_THOUSAND_SEP(prefix, suffix) prefix suffix
38 # define check_thousand_sep(slot) do { } while (0)
39 #else
40 # define FORMAT_THOUSAND_SEP(prefix, suffix) ((thousand == WORKS) \
41 ? prefix "'" suffix \
42 : prefix suffix)
43
44 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
45
46 /// Check if thousands separator is supported. Run-time checking is easiest
47 /// because it seems to be sometimes lacking even on a POSIXish system.
48 /// Note that trying to use thousands separators when snprintf() doesn't
49 /// support them results in undefined behavior. This just has happened to
50 /// work well enough in practice.
51 ///
52 /// This must be called before using the FORMAT_THOUSAND_SEP macro.
53 static void
check_thousand_sep(uint32_t slot)54 check_thousand_sep(uint32_t slot)
55 {
56 if (thousand == UNKNOWN) {
57 bufs[slot][0] = '\0';
58 snprintf(bufs[slot], sizeof(bufs[slot]), "%'u", 1U);
59 thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
60 }
61
62 return;
63 }
64 #endif
65
66
67 extern void *
xrealloc(void * ptr,size_t size)68 xrealloc(void *ptr, size_t size)
69 {
70 assert(size > 0);
71
72 // Save ptr so that we can free it if realloc fails.
73 // The point is that message_fatal ends up calling stdio functions
74 // which in some libc implementations might allocate memory from
75 // the heap. Freeing ptr improves the chances that there's free
76 // memory for stdio functions if they need it.
77 void *p = ptr;
78 ptr = realloc(ptr, size);
79
80 if (ptr == NULL) {
81 const int saved_errno = errno;
82 free(p);
83 message_fatal("%s", strerror(saved_errno));
84 }
85
86 return ptr;
87 }
88
89
90 extern char *
xstrdup(const char * src)91 xstrdup(const char *src)
92 {
93 assert(src != NULL);
94 const size_t size = strlen(src) + 1;
95 char *dest = xmalloc(size);
96 return memcpy(dest, src, size);
97 }
98
99
100 extern uint64_t
str_to_uint64(const char * name,const char * value,uint64_t min,uint64_t max)101 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
102 {
103 uint64_t result = 0;
104
105 // Skip blanks.
106 while (*value == ' ' || *value == '\t')
107 ++value;
108
109 // Accept special value "max". Supporting "min" doesn't seem useful.
110 if (strcmp(value, "max") == 0)
111 return max;
112
113 if (*value < '0' || *value > '9')
114 message_fatal(_("%s: %s"), value,
115 _("Value is not a non-negative decimal integer"));
116
117 do {
118 // Don't overflow.
119 if (result > UINT64_MAX / 10)
120 goto error;
121
122 result *= 10;
123
124 // Another overflow check
125 const uint32_t add = (uint32_t)(*value - '0');
126 if (UINT64_MAX - add < result)
127 goto error;
128
129 result += add;
130 ++value;
131 } while (*value >= '0' && *value <= '9');
132
133 if (*value != '\0') {
134 // Look for suffix. Originally this supported both base-2
135 // and base-10, but since there seems to be little need
136 // for base-10 in this program, treat everything as base-2
137 // and also be more relaxed about the case of the first
138 // letter of the suffix.
139 uint64_t multiplier = 0;
140 if (*value == 'k' || *value == 'K')
141 multiplier = UINT64_C(1) << 10;
142 else if (*value == 'm' || *value == 'M')
143 multiplier = UINT64_C(1) << 20;
144 else if (*value == 'g' || *value == 'G')
145 multiplier = UINT64_C(1) << 30;
146
147 ++value;
148
149 // Allow also e.g. Ki, KiB, and KB.
150 if (*value != '\0' && strcmp(value, "i") != 0
151 && strcmp(value, "iB") != 0
152 && strcmp(value, "B") != 0)
153 multiplier = 0;
154
155 if (multiplier == 0) {
156 message(V_ERROR, _("%s: Invalid multiplier suffix"),
157 value - 1);
158 message_fatal(_("Valid suffixes are 'KiB' (2^10), "
159 "'MiB' (2^20), and 'GiB' (2^30)."));
160 }
161
162 // Don't overflow here either.
163 if (result > UINT64_MAX / multiplier)
164 goto error;
165
166 result *= multiplier;
167 }
168
169 if (result < min || result > max)
170 goto error;
171
172 return result;
173
174 error:
175 message_fatal(_("Value of the option '%s' must be in the range "
176 "[%" PRIu64 ", %" PRIu64 "]"),
177 name, min, max);
178 }
179
180
181 extern uint64_t
round_up_to_mib(uint64_t n)182 round_up_to_mib(uint64_t n)
183 {
184 return (n >> 20) + ((n & ((UINT32_C(1) << 20) - 1)) != 0);
185 }
186
187
188 extern const char *
uint64_to_str(uint64_t value,uint32_t slot)189 uint64_to_str(uint64_t value, uint32_t slot)
190 {
191 assert(slot < ARRAY_SIZE(bufs));
192
193 check_thousand_sep(slot);
194
195 snprintf(bufs[slot], sizeof(bufs[slot]),
196 FORMAT_THOUSAND_SEP("%", PRIu64), value);
197
198 return bufs[slot];
199 }
200
201
202 extern const char *
uint64_to_nicestr(uint64_t value,enum nicestr_unit unit_min,enum nicestr_unit unit_max,bool always_also_bytes,uint32_t slot)203 uint64_to_nicestr(uint64_t value, enum nicestr_unit unit_min,
204 enum nicestr_unit unit_max, bool always_also_bytes,
205 uint32_t slot)
206 {
207 assert(unit_min <= unit_max);
208 assert(unit_max <= NICESTR_TIB);
209 assert(slot < ARRAY_SIZE(bufs));
210
211 check_thousand_sep(slot);
212
213 enum nicestr_unit unit = NICESTR_B;
214 char *pos = bufs[slot];
215 size_t left = sizeof(bufs[slot]);
216
217 if ((unit_min == NICESTR_B && value < 10000)
218 || unit_max == NICESTR_B) {
219 // The value is shown as bytes.
220 my_snprintf(&pos, &left, FORMAT_THOUSAND_SEP("%", "u"),
221 (unsigned int)value);
222 } else {
223 // Scale the value to a nicer unit. Unless unit_min and
224 // unit_max limit us, we will show at most five significant
225 // digits with one decimal place.
226 double d = (double)(value);
227 do {
228 d /= 1024.0;
229 ++unit;
230 } while (unit < unit_min || (d > 9999.9 && unit < unit_max));
231
232 my_snprintf(&pos, &left, FORMAT_THOUSAND_SEP("%", ".1f"), d);
233 }
234
235 static const char suffix[5][4] = { "B", "KiB", "MiB", "GiB", "TiB" };
236 my_snprintf(&pos, &left, " %s", suffix[unit]);
237
238 if (always_also_bytes && value >= 10000)
239 snprintf(pos, left, FORMAT_THOUSAND_SEP(" (%", PRIu64 " B)"),
240 value);
241
242 return bufs[slot];
243 }
244
245
246 extern void
my_snprintf(char ** pos,size_t * left,const char * fmt,...)247 my_snprintf(char **pos, size_t *left, const char *fmt, ...)
248 {
249 va_list ap;
250 va_start(ap, fmt);
251 const int len = vsnprintf(*pos, *left, fmt, ap);
252 va_end(ap);
253
254 // If an error occurred, we want the caller to think that the whole
255 // buffer was used. This way no more data will be written to the
256 // buffer. We don't need better error handling here, although it
257 // is possible that the result looks garbage on the terminal if
258 // e.g. an UTF-8 character gets split. That shouldn't (easily)
259 // happen though, because the buffers used have some extra room.
260 if (len < 0 || (size_t)(len) >= *left) {
261 *left = 0;
262 } else {
263 *pos += len;
264 *left -= (size_t)(len);
265 }
266
267 return;
268 }
269
270
271 extern bool
is_tty(int fd)272 is_tty(int fd)
273 {
274 #if defined(_WIN32) && !defined(__CYGWIN__)
275 // There is no need to check if handle == INVALID_HANDLE_VALUE
276 // because it will return false anyway when used in GetConsoleMode().
277 // The resulting HANDLE is owned by the file descriptor.
278 // The HANDLE must not be closed here.
279 intptr_t handle = _get_osfhandle(fd);
280 DWORD mode;
281
282 // GetConsoleMode() is an easy way to tell if the HANDLE is a
283 // console or not. We do not care about the value of mode since we
284 // do not plan to use any further Windows console functions.
285 return GetConsoleMode((HANDLE)handle, &mode);
286 #else
287 return isatty(fd);
288 #endif
289 }
290
291
292 extern bool
is_tty_stdin(void)293 is_tty_stdin(void)
294 {
295 const bool ret = is_tty(STDIN_FILENO);
296
297 if (ret)
298 message_error(_("Compressed data cannot be read from "
299 "a terminal"));
300
301 return ret;
302 }
303
304
305 extern bool
is_tty_stdout(void)306 is_tty_stdout(void)
307 {
308 const bool ret = is_tty(STDOUT_FILENO);
309
310 if (ret)
311 message_error(_("Compressed data cannot be written to "
312 "a terminal"));
313
314 return ret;
315 }
316