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 /// \brief Simple quoting to get rid of ASCII control characters
124 /// This is not so cool and locale-dependent, but should be good enough
125 /// At least we don't print any control characters on the terminal.
128 str_quote(const char *str)
131 bool has_ctrl = false;
133 while (str[dest_len] != '\0')
134 if (*(unsigned char *)(str + dest_len++) < 0x20)
137 char *dest = malloc(dest_len + 1);
140 for (size_t i = 0; i < dest_len; ++i)
141 if (*(unsigned char *)(str + i) < 0x20)
146 dest[dest_len] = '\0';
149 // Usually there are no control characters,
150 // so we can optimize.
151 memcpy(dest, str, dest_len + 1);
161 is_empty_filename(const char *filename)
163 if (filename[0] == '\0') {
164 message_error(_("Empty filename, skipping"));
175 const bool ret = isatty(STDIN_FILENO);
178 message_error(_("Compressed data not read from a terminal "
179 "unless `--force' is used."));
188 const bool ret = isatty(STDOUT_FILENO);
191 message_error(_("Compressed data not written to a terminal "
192 "unless `--force' is used."));