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 / 10)
64 // Another overflow check
65 const uint32_t add = *value - '0';
66 if (UINT64_MAX - add < result)
71 } while (*value >= '0' && *value <= '9');
74 // Look for suffix. Originally this supported both base-2
75 // and base-10, but since there seems to be little need
76 // for base-10 in this program, treat everything as base-2
77 // and also be more relaxed about the case of the first
78 // letter of the suffix.
79 uint64_t multiplier = 0;
80 if (*value == 'k' || *value == 'K')
81 multiplier = UINT64_C(1) << 10;
82 else if (*value == 'm' || *value == 'M')
83 multiplier = UINT64_C(1) << 20;
84 else if (*value == 'g' || *value == 'G')
85 multiplier = UINT64_C(1) << 30;
89 // Allow also e.g. Ki, KiB, and KB.
90 if (*value != '\0' && strcmp(value, "i") != 0
91 && strcmp(value, "iB") != 0
92 && strcmp(value, "B") != 0)
95 if (multiplier == 0) {
96 message(V_ERROR, _("%s: Invalid multiplier suffix"),
98 message_fatal(_("Valid suffixes are `KiB' (2^10), "
99 "`MiB' (2^20), and `GiB' (2^30)."));
102 // Don't overflow here either.
103 if (result > UINT64_MAX / multiplier)
106 result *= multiplier;
109 if (result < min || result > max)
115 message_fatal(_("Value of the option `%s' must be in the range "
116 "[%" PRIu64 ", %" PRIu64 "]"),
122 round_up_to_mib(uint64_t n)
124 return (n >> 20) + ((n & ((UINT32_C(1) << 20) - 1)) != 0);
129 uint64_to_str(uint64_t value, uint32_t slot)
131 // 2^64 with thousand separators is 26 bytes plus trailing '\0'.
132 static char bufs[4][32];
134 assert(slot < ARRAY_SIZE(bufs));
136 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
137 if (thousand == UNKNOWN) {
138 bufs[slot][0] = '\0';
139 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64,
141 thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
144 if (thousand == WORKS)
145 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64, value);
147 snprintf(bufs[slot], sizeof(bufs[slot]), "%" PRIu64, value);
154 uint64_to_nicestr(uint64_t value, enum nicestr_unit unit_min,
155 enum nicestr_unit unit_max, bool always_also_bytes,
158 assert(unit_min <= unit_max);
159 assert(unit_max <= NICESTR_TIB);
161 enum nicestr_unit unit = NICESTR_B;
164 if ((unit_min == NICESTR_B && value < 10000)
165 || unit_max == NICESTR_B) {
166 // The value is shown as bytes.
167 str = uint64_to_str(value, slot);
169 // Scale the value to a nicer unit. Unless unit_min and
170 // unit_max limit us, we will show at most five significant
171 // digits with one decimal place.
172 double d = (double)(value);
176 } while (unit < unit_min || (d > 9999.9 && unit < unit_max));
178 str = double_to_str(d);
181 static const char suffix[5][4] = { "B", "KiB", "MiB", "GiB", "TiB" };
183 // Minimum buffer size:
184 // 26 2^64 with thousand separators
187 // 26 2^64 with thousand separators
191 static char buf[4][64];
192 char *pos = buf[slot];
193 size_t left = sizeof(buf[slot]);
194 my_snprintf(&pos, &left, "%s %s", str, suffix[unit]);
196 if (always_also_bytes && value >= 10000)
197 snprintf(pos, left, " (%s B)", uint64_to_str(value, slot));
204 double_to_str(double value)
208 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
209 if (thousand == UNKNOWN) {
211 snprintf(buf, sizeof(buf), "%'.1f", 2.0);
212 thousand = buf[0] == '2' ? WORKS : BROKEN;
215 if (thousand == WORKS)
216 snprintf(buf, sizeof(buf), "%'.1f", value);
218 snprintf(buf, sizeof(buf), "%.1f", value);
225 my_snprintf(char **pos, size_t *left, const char *fmt, ...)
229 const int len = vsnprintf(*pos, *left, fmt, ap);
232 // If an error occurred, we want the caller to think that the whole
233 // buffer was used. This way no more data will be written to the
234 // buffer. We don't need better error handling here.
235 if (len < 0 || (size_t)(len) >= *left) {
247 /// \brief Simple quoting to get rid of ASCII control characters
249 /// This is not so cool and locale-dependent, but should be good enough
250 /// At least we don't print any control characters on the terminal.
253 str_quote(const char *str)
256 bool has_ctrl = false;
258 while (str[dest_len] != '\0')
259 if (*(unsigned char *)(str + dest_len++) < 0x20)
262 char *dest = malloc(dest_len + 1);
265 for (size_t i = 0; i < dest_len; ++i)
266 if (*(unsigned char *)(str + i) < 0x20)
271 dest[dest_len] = '\0';
274 // Usually there are no control characters,
275 // so we can optimize.
276 memcpy(dest, str, dest_len + 1);
286 is_empty_filename(const char *filename)
288 if (filename[0] == '\0') {
289 message_error(_("Empty filename, skipping"));
300 const bool ret = isatty(STDIN_FILENO);
303 message_error(_("Compressed data cannot be read from "
313 const bool ret = isatty(STDOUT_FILENO);
316 message_error(_("Compressed data cannot be written to "