1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Miscellaneous utility functions
6 // Copyright (C) 2007 Lasse Collin
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
24 xrealloc(void *ptr, size_t size)
28 ptr = realloc(ptr, size);
30 message_fatal("%s", strerror(errno));
37 xstrdup(const char *src)
40 const size_t size = strlen(src) + 1;
41 char *dest = xmalloc(size);
42 return memcpy(dest, src, size);
47 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
52 while (*value == ' ' || *value == '\t')
55 if (*value < '0' || *value > '9')
56 message_fatal(_("%s: Value is not a non-negative "
57 "decimal integer"), value);
61 if (result > (UINT64_MAX - 9) / 10)
65 result += *value - '0';
67 } while (*value >= '0' && *value <= '9');
75 { "k", UINT64_C(1000) },
76 { "kB", UINT64_C(1000) },
77 { "M", UINT64_C(1000000) },
78 { "MB", UINT64_C(1000000) },
79 { "G", UINT64_C(1000000000) },
80 { "GB", UINT64_C(1000000000) },
81 { "Ki", UINT64_C(1024) },
82 { "KiB", UINT64_C(1024) },
83 { "Mi", UINT64_C(1048576) },
84 { "MiB", UINT64_C(1048576) },
85 { "Gi", UINT64_C(1073741824) },
86 { "GiB", UINT64_C(1073741824) }
89 uint64_t multiplier = 0;
90 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
91 if (strcmp(value, suffixes[i].name) == 0) {
92 multiplier = suffixes[i].multiplier;
97 if (multiplier == 0) {
98 message(V_ERROR, _("%s: Invalid multiplier suffix. "
99 "Valid suffixes:"), value);
100 message_fatal("`k' (10^3), `M' (10^6), `G' (10^9) "
101 "`Ki' (2^10), `Mi' (2^20), "
105 // Don't overflow here either.
106 if (result > UINT64_MAX / multiplier)
109 result *= multiplier;
112 if (result < min || result > max)
118 message_fatal(_("Value of the option `%s' must be in the range "
119 "[%llu, %llu]"), name,
120 (unsigned long long)(min),
121 (unsigned long long)(max));
126 /// \brief Simple quoting to get rid of ASCII control characters
128 /// This is not so cool and locale-dependent, but should be good enough
129 /// At least we don't print any control characters on the terminal.
132 str_quote(const char *str)
135 bool has_ctrl = false;
137 while (str[dest_len] != '\0')
138 if (*(unsigned char *)(str + dest_len++) < 0x20)
141 char *dest = malloc(dest_len + 1);
144 for (size_t i = 0; i < dest_len; ++i)
145 if (*(unsigned char *)(str + i) < 0x20)
150 dest[dest_len] = '\0';
153 // Usually there are no control characters,
154 // so we can optimize.
155 memcpy(dest, str, dest_len + 1);
165 is_empty_filename(const char *filename)
167 if (filename[0] == '\0') {
168 message_error(_("Empty filename, skipping"));
179 const bool ret = isatty(STDIN_FILENO);
182 message_error(_("Compressed data not read from a terminal "
183 "unless `--force' is used."));
192 const bool ret = isatty(STDOUT_FILENO);
195 message_error(_("Compressed data not written to a terminal "
196 "unless `--force' is used."));