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');
68 // Look for suffix. Originally this supported both base-2
69 // and base-10, but since there seems to be little need
70 // for base-10 in this program, treat everything as base-2
71 // and also be more relaxed about the case of the first
72 // letter of the suffix.
73 uint64_t multiplier = 0;
74 if (*value == 'k' || *value == 'K')
75 multiplier = UINT64_C(1) << 10;
76 else if (*value == 'm' || *value == 'M')
77 multiplier = UINT64_C(1) << 20;
78 else if (*value == 'g' || *value == 'G')
79 multiplier = UINT64_C(1) << 30;
83 // Allow also e.g. Ki, KiB, and KB.
84 if (*value != '\0' && strcmp(value, "i") != 0
85 && strcmp(value, "iB") != 0
86 && strcmp(value, "B") != 0)
89 if (multiplier == 0) {
90 message(V_ERROR, _("%s: Invalid multiplier suffix"),
92 message_fatal(_("Valid suffixes are `KiB' (2^10), "
93 "`MiB' (2^20), and `GiB' (2^30)."));
96 // Don't overflow here either.
97 if (result > UINT64_MAX / multiplier)
100 result *= multiplier;
103 if (result < min || result > max)
109 message_fatal(_("Value of the option `%s' must be in the range "
110 "[%" PRIu64 ", %" PRIu64 "]"),
116 round_up_to_mib(uint64_t n)
118 return (n >> 20) + ((n & ((UINT32_C(1) << 20) - 1)) != 0);
123 uint64_to_str(uint64_t value, uint32_t slot)
125 // 2^64 with thousand separators is 26 bytes plus trailing '\0'.
126 static char bufs[4][32];
128 assert(slot < ARRAY_SIZE(bufs));
130 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
131 if (thousand == UNKNOWN) {
132 bufs[slot][0] = '\0';
133 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64,
135 thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
138 if (thousand == WORKS)
139 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64, value);
141 snprintf(bufs[slot], sizeof(bufs[slot]), "%" PRIu64, value);
148 uint64_to_nicestr(uint64_t value, enum nicestr_unit unit_min,
149 enum nicestr_unit unit_max, bool always_also_bytes,
152 assert(unit_min <= unit_max);
153 assert(unit_max <= NICESTR_TIB);
155 enum nicestr_unit unit = NICESTR_B;
158 if ((unit_min == NICESTR_B && value < 10000)
159 || unit_max == NICESTR_B) {
160 // The value is shown as bytes.
161 str = uint64_to_str(value, slot);
163 // Scale the value to a nicer unit. Unless unit_min and
164 // unit_max limit us, we will show at most five significant
165 // digits with one decimal place.
166 double d = (double)(value);
170 } while (unit < unit_min || (d > 9999.9 && unit < unit_max));
172 str = double_to_str(d);
175 static const char suffix[5][4] = { "B", "KiB", "MiB", "GiB", "TiB" };
177 // Minimum buffer size:
178 // 26 2^64 with thousand separators
181 // 26 2^64 with thousand separators
185 static char buf[4][64];
186 char *pos = buf[slot];
187 size_t left = sizeof(buf[slot]);
188 my_snprintf(&pos, &left, "%s %s", str, suffix[unit]);
190 if (always_also_bytes && value >= 10000)
191 snprintf(pos, left, " (%s B)", uint64_to_str(value, slot));
198 double_to_str(double value)
202 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
203 if (thousand == UNKNOWN) {
205 snprintf(buf, sizeof(buf), "%'.1f", 2.0);
206 thousand = buf[0] == '2' ? WORKS : BROKEN;
209 if (thousand == WORKS)
210 snprintf(buf, sizeof(buf), "%'.1f", value);
212 snprintf(buf, sizeof(buf), "%.1f", value);
219 my_snprintf(char **pos, size_t *left, const char *fmt, ...)
223 const int len = vsnprintf(*pos, *left, fmt, ap);
226 // If an error occurred, we want the caller to think that the whole
227 // buffer was used. This way no more data will be written to the
228 // buffer. We don't need better error handling here.
229 if (len < 0 || (size_t)(len) >= *left) {
241 /// \brief Simple quoting to get rid of ASCII control characters
243 /// This is not so cool and locale-dependent, but should be good enough
244 /// At least we don't print any control characters on the terminal.
247 str_quote(const char *str)
250 bool has_ctrl = false;
252 while (str[dest_len] != '\0')
253 if (*(unsigned char *)(str + dest_len++) < 0x20)
256 char *dest = malloc(dest_len + 1);
259 for (size_t i = 0; i < dest_len; ++i)
260 if (*(unsigned char *)(str + i) < 0x20)
265 dest[dest_len] = '\0';
268 // Usually there are no control characters,
269 // so we can optimize.
270 memcpy(dest, str, dest_len + 1);
280 is_empty_filename(const char *filename)
282 if (filename[0] == '\0') {
283 message_error(_("Empty filename, skipping"));
294 const bool ret = isatty(STDIN_FILENO);
297 message_error(_("Compressed data cannot be read from "
307 const bool ret = isatty(STDOUT_FILENO);
310 message_error(_("Compressed data cannot be written to "