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 ///////////////////////////////////////////////////////////////////////////////
17 xrealloc(void *ptr, size_t size)
21 ptr = realloc(ptr, size);
23 message_fatal("%s", strerror(errno));
30 xstrdup(const char *src)
33 const size_t size = strlen(src) + 1;
34 char *dest = xmalloc(size);
35 return memcpy(dest, src, size);
40 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
45 while (*value == ' ' || *value == '\t')
48 // Accept special value "max". Supporting "min" doesn't seem useful.
49 if (strcmp(value, "max") == 0)
52 if (*value < '0' || *value > '9')
53 message_fatal(_("%s: Value is not a non-negative "
54 "decimal integer"), value);
58 if (result > (UINT64_MAX - 9) / 10)
62 result += *value - '0';
64 } while (*value >= '0' && *value <= '9');
72 { "k", UINT64_C(1000) },
73 { "kB", UINT64_C(1000) },
74 { "M", UINT64_C(1000000) },
75 { "MB", UINT64_C(1000000) },
76 { "G", UINT64_C(1000000000) },
77 { "GB", UINT64_C(1000000000) },
78 { "Ki", UINT64_C(1024) },
79 { "KiB", UINT64_C(1024) },
80 { "Mi", UINT64_C(1048576) },
81 { "MiB", UINT64_C(1048576) },
82 { "Gi", UINT64_C(1073741824) },
83 { "GiB", UINT64_C(1073741824) }
86 uint64_t multiplier = 0;
87 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
88 if (strcmp(value, suffixes[i].name) == 0) {
89 multiplier = suffixes[i].multiplier;
94 if (multiplier == 0) {
95 message(V_ERROR, _("%s: Invalid multiplier suffix. "
96 "Valid suffixes:"), value);
97 message_fatal("`k' (10^3), `M' (10^6), `G' (10^9) "
98 "`Ki' (2^10), `Mi' (2^20), "
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 uint64_to_str(uint64_t value, uint32_t slot)
124 // 2^64 with thousand separators is 26 bytes plus trailing '\0'.
125 static char bufs[4][32];
127 assert(slot < ARRAY_SIZE(bufs));
129 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
130 if (thousand == UNKNOWN) {
131 bufs[slot][0] = '\0';
132 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64,
134 thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
137 if (thousand == WORKS)
138 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64, value);
140 snprintf(bufs[slot], sizeof(bufs[slot]), "%" PRIu64, value);
147 double_to_str(double value)
149 // 64 bytes is surely enough, since it won't fit in some other
153 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
154 if (thousand == UNKNOWN) {
156 snprintf(buf, sizeof(buf), "%'.1f", 2.0);
157 thousand = buf[0] == '2' ? WORKS : BROKEN;
160 if (thousand == WORKS)
161 snprintf(buf, sizeof(buf), "%'.1f", value);
163 snprintf(buf, sizeof(buf), "%.1f", value);
170 /// \brief Simple quoting to get rid of ASCII control characters
172 /// This is not so cool and locale-dependent, but should be good enough
173 /// At least we don't print any control characters on the terminal.
176 str_quote(const char *str)
179 bool has_ctrl = false;
181 while (str[dest_len] != '\0')
182 if (*(unsigned char *)(str + dest_len++) < 0x20)
185 char *dest = malloc(dest_len + 1);
188 for (size_t i = 0; i < dest_len; ++i)
189 if (*(unsigned char *)(str + i) < 0x20)
194 dest[dest_len] = '\0';
197 // Usually there are no control characters,
198 // so we can optimize.
199 memcpy(dest, str, dest_len + 1);
209 is_empty_filename(const char *filename)
211 if (filename[0] == '\0') {
212 message_error(_("Empty filename, skipping"));
223 const bool ret = isatty(STDIN_FILENO);
226 message_error(_("Compressed data cannot be read from "
236 const bool ret = isatty(STDOUT_FILENO);
239 message_error(_("Compressed data cannot be written to "