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 ///////////////////////////////////////////////////////////////////////////////
16 // Thousand separator for format strings is not supported outside POSIX.
17 // This is used in uint64_to_str() and double_to_str().
26 xrealloc(void *ptr, size_t size)
30 ptr = realloc(ptr, size);
32 message_fatal("%s", strerror(errno));
39 xstrdup(const char *src)
42 const size_t size = strlen(src) + 1;
43 char *dest = xmalloc(size);
44 return memcpy(dest, src, size);
49 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
54 while (*value == ' ' || *value == '\t')
57 // Accept special value "max". Supporting "min" doesn't seem useful.
58 if (strcmp(value, "max") == 0)
61 if (*value < '0' || *value > '9')
62 message_fatal(_("%s: Value is not a non-negative "
63 "decimal integer"), value);
67 if (result > (UINT64_MAX - 9) / 10)
71 result += *value - '0';
73 } while (*value >= '0' && *value <= '9');
81 { "k", UINT64_C(1000) },
82 { "kB", UINT64_C(1000) },
83 { "M", UINT64_C(1000000) },
84 { "MB", UINT64_C(1000000) },
85 { "G", UINT64_C(1000000000) },
86 { "GB", UINT64_C(1000000000) },
87 { "Ki", UINT64_C(1024) },
88 { "KiB", UINT64_C(1024) },
89 { "Mi", UINT64_C(1048576) },
90 { "MiB", UINT64_C(1048576) },
91 { "Gi", UINT64_C(1073741824) },
92 { "GiB", UINT64_C(1073741824) }
95 uint64_t multiplier = 0;
96 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
97 if (strcmp(value, suffixes[i].name) == 0) {
98 multiplier = suffixes[i].multiplier;
103 if (multiplier == 0) {
104 message(V_ERROR, _("%s: Invalid multiplier suffix. "
105 "Valid suffixes:"), value);
106 message_fatal("`k' (10^3), `M' (10^6), `G' (10^9) "
107 "`Ki' (2^10), `Mi' (2^20), "
111 // Don't overflow here either.
112 if (result > UINT64_MAX / multiplier)
115 result *= multiplier;
118 if (result < min || result > max)
124 message_fatal(_("Value of the option `%s' must be in the range "
125 "[%" PRIu64 ", %" PRIu64 "]"),
131 uint64_to_str(uint64_t value, uint32_t slot)
133 // 2^64 with thousand separators is 26 bytes plus trailing '\0'.
134 static char bufs[4][32];
136 assert(slot < ARRAY_SIZE(bufs));
138 snprintf(bufs[slot], sizeof(bufs[slot]), "%" THOUSAND PRIu64, value);
144 double_to_str(double value)
146 // 64 bytes is surely enough, since it won't fit in some other
150 snprintf(buf, sizeof(buf), "%" THOUSAND ".1f", value);
156 /// \brief Simple quoting to get rid of ASCII control characters
158 /// This is not so cool and locale-dependent, but should be good enough
159 /// At least we don't print any control characters on the terminal.
162 str_quote(const char *str)
165 bool has_ctrl = false;
167 while (str[dest_len] != '\0')
168 if (*(unsigned char *)(str + dest_len++) < 0x20)
171 char *dest = malloc(dest_len + 1);
174 for (size_t i = 0; i < dest_len; ++i)
175 if (*(unsigned char *)(str + i) < 0x20)
180 dest[dest_len] = '\0';
183 // Usually there are no control characters,
184 // so we can optimize.
185 memcpy(dest, str, dest_len + 1);
195 is_empty_filename(const char *filename)
197 if (filename[0] == '\0') {
198 message_error(_("Empty filename, skipping"));
209 const bool ret = isatty(STDIN_FILENO);
212 message_error(_("Compressed data not read from a terminal "
213 "unless `--force' is used."));
222 const bool ret = isatty(STDOUT_FILENO);
225 message_error(_("Compressed data not written to a terminal "
226 "unless `--force' is used."));