1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Miscellaneous utility functions
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
18 xrealloc(void *ptr, size_t size)
22 ptr = realloc(ptr, size);
24 message_fatal("%s", strerror(errno));
31 xstrdup(const char *src)
34 const size_t size = strlen(src) + 1;
35 char *dest = xmalloc(size);
36 return memcpy(dest, src, size);
41 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
46 while (*value == ' ' || *value == '\t')
49 // Accept special value "max". Supporting "min" doesn't seem useful.
50 if (strcmp(value, "max") == 0)
53 if (*value < '0' || *value > '9')
54 message_fatal(_("%s: Value is not a non-negative "
55 "decimal integer"), value);
59 if (result > (UINT64_MAX - 9) / 10)
63 result += *value - '0';
65 } while (*value >= '0' && *value <= '9');
73 { "k", UINT64_C(1000) },
74 { "kB", UINT64_C(1000) },
75 { "M", UINT64_C(1000000) },
76 { "MB", UINT64_C(1000000) },
77 { "G", UINT64_C(1000000000) },
78 { "GB", UINT64_C(1000000000) },
79 { "Ki", UINT64_C(1024) },
80 { "KiB", UINT64_C(1024) },
81 { "Mi", UINT64_C(1048576) },
82 { "MiB", UINT64_C(1048576) },
83 { "Gi", UINT64_C(1073741824) },
84 { "GiB", UINT64_C(1073741824) }
87 uint64_t multiplier = 0;
88 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
89 if (strcmp(value, suffixes[i].name) == 0) {
90 multiplier = suffixes[i].multiplier;
95 if (multiplier == 0) {
96 message(V_ERROR, _("%s: Invalid multiplier suffix. "
97 "Valid suffixes:"), value);
98 message_fatal("`k' (10^3), `M' (10^6), `G' (10^9) "
99 "`Ki' (2^10), `Mi' (2^20), "
103 // Don't overflow here either.
104 if (result > UINT64_MAX / multiplier)
107 result *= multiplier;
110 if (result < min || result > max)
116 message_fatal(_("Value of the option `%s' must be in the range "
117 "[%" PRIu64 ", %" PRIu64 "]"),
123 round_up_to_mib(uint64_t n)
125 return (n >> 20) + ((n & ((UINT32_C(1) << 20) - 1)) != 0);
130 uint64_to_str(uint64_t value, uint32_t slot)
132 // 2^64 with thousand separators is 26 bytes plus trailing '\0'.
133 static char bufs[4][32];
135 assert(slot < ARRAY_SIZE(bufs));
137 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
138 if (thousand == UNKNOWN) {
139 bufs[slot][0] = '\0';
140 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64,
142 thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
145 if (thousand == WORKS)
146 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64, value);
148 snprintf(bufs[slot], sizeof(bufs[slot]), "%" PRIu64, value);
155 uint64_to_nicestr(uint64_t value, enum nicestr_unit unit_min,
156 enum nicestr_unit unit_max, bool always_also_bytes,
159 assert(unit_min <= unit_max);
160 assert(unit_max <= NICESTR_TIB);
162 enum nicestr_unit unit = NICESTR_B;
165 if ((unit_min == NICESTR_B && value < 10000)
166 || unit_max == NICESTR_B) {
167 // The value is shown as bytes.
168 str = uint64_to_str(value, slot);
170 // Scale the value to a nicer unit. Unless unit_min and
171 // unit_max limit us, we will show at most five significant
172 // digits with one decimal place.
173 double d = (double)(value);
177 } while (unit < unit_min || (d > 9999.9 && unit < unit_max));
179 str = double_to_str(d);
182 static const char suffix[5][4] = { "B", "KiB", "MiB", "GiB", "TiB" };
184 // Minimum buffer size:
185 // 26 2^64 with thousand separators
188 // 26 2^64 with thousand separators
192 static char buf[4][64];
193 char *pos = buf[slot];
194 size_t left = sizeof(buf[slot]);
195 my_snprintf(&pos, &left, "%s %s", str, suffix[unit]);
197 if (always_also_bytes && value >= 10000)
198 snprintf(pos, left, " (%s B)", uint64_to_str(value, slot));
205 double_to_str(double value)
209 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
210 if (thousand == UNKNOWN) {
212 snprintf(buf, sizeof(buf), "%'.1f", 2.0);
213 thousand = buf[0] == '2' ? WORKS : BROKEN;
216 if (thousand == WORKS)
217 snprintf(buf, sizeof(buf), "%'.1f", value);
219 snprintf(buf, sizeof(buf), "%.1f", value);
226 my_snprintf(char **pos, size_t *left, const char *fmt, ...)
230 const int len = vsnprintf(*pos, *left, fmt, ap);
233 // If an error occurred, we want the caller to think that the whole
234 // buffer was used. This way no more data will be written to the
235 // buffer. We don't need better error handling here.
236 if (len < 0 || (size_t)(len) >= *left) {
248 /// \brief Simple quoting to get rid of ASCII control characters
250 /// This is not so cool and locale-dependent, but should be good enough
251 /// At least we don't print any control characters on the terminal.
254 str_quote(const char *str)
257 bool has_ctrl = false;
259 while (str[dest_len] != '\0')
260 if (*(unsigned char *)(str + dest_len++) < 0x20)
263 char *dest = malloc(dest_len + 1);
266 for (size_t i = 0; i < dest_len; ++i)
267 if (*(unsigned char *)(str + i) < 0x20)
272 dest[dest_len] = '\0';
275 // Usually there are no control characters,
276 // so we can optimize.
277 memcpy(dest, str, dest_len + 1);
287 is_empty_filename(const char *filename)
289 if (filename[0] == '\0') {
290 message_error(_("Empty filename, skipping"));
301 const bool ret = isatty(STDIN_FILENO);
304 message_error(_("Compressed data cannot be read from "
314 const bool ret = isatty(STDOUT_FILENO);
317 message_error(_("Compressed data cannot be written to "