]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/xz/src/xz/util.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / xz / src / xz / util.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       util.c
4 /// \brief      Miscellaneous utility functions
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "private.h"
14 #include <stdarg.h>
15
16
17 extern void *
18 xrealloc(void *ptr, size_t size)
19 {
20         assert(size > 0);
21
22         ptr = realloc(ptr, size);
23         if (ptr == NULL)
24                 message_fatal("%s", strerror(errno));
25
26         return ptr;
27 }
28
29
30 extern char *
31 xstrdup(const char *src)
32 {
33         assert(src != NULL);
34         const size_t size = strlen(src) + 1;
35         char *dest = xmalloc(size);
36         return memcpy(dest, src, size);
37 }
38
39
40 extern uint64_t
41 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
42 {
43         uint64_t result = 0;
44
45         // Skip blanks.
46         while (*value == ' ' || *value == '\t')
47                 ++value;
48
49         // Accept special value "max". Supporting "min" doesn't seem useful.
50         if (strcmp(value, "max") == 0)
51                 return max;
52
53         if (*value < '0' || *value > '9')
54                 message_fatal(_("%s: Value is not a non-negative "
55                                 "decimal integer"), value);
56
57         do {
58                 // Don't overflow.
59                 if (result > (UINT64_MAX - 9) / 10)
60                         goto error;
61
62                 result *= 10;
63                 result += *value - '0';
64                 ++value;
65         } while (*value >= '0' && *value <= '9');
66
67         if (*value != '\0') {
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;
80
81                 ++value;
82
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)
87                         multiplier = 0;
88
89                 if (multiplier == 0) {
90                         message(V_ERROR, _("%s: Invalid multiplier suffix"),
91                                         value - 1);
92                         message_fatal(_("Valid suffixes are `KiB' (2^10), "
93                                         "`MiB' (2^20), and `GiB' (2^30)."));
94                 }
95
96                 // Don't overflow here either.
97                 if (result > UINT64_MAX / multiplier)
98                         goto error;
99
100                 result *= multiplier;
101         }
102
103         if (result < min || result > max)
104                 goto error;
105
106         return result;
107
108 error:
109         message_fatal(_("Value of the option `%s' must be in the range "
110                                 "[%" PRIu64 ", %" PRIu64 "]"),
111                                 name, min, max);
112 }
113
114
115 extern uint64_t
116 round_up_to_mib(uint64_t n)
117 {
118         return (n >> 20) + ((n & ((UINT32_C(1) << 20) - 1)) != 0);
119 }
120
121
122 extern const char *
123 uint64_to_str(uint64_t value, uint32_t slot)
124 {
125         // 2^64 with thousand separators is 26 bytes plus trailing '\0'.
126         static char bufs[4][32];
127
128         assert(slot < ARRAY_SIZE(bufs));
129
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,
134                                 UINT64_C(1));
135                 thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
136         }
137
138         if (thousand == WORKS)
139                 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64, value);
140         else
141                 snprintf(bufs[slot], sizeof(bufs[slot]), "%" PRIu64, value);
142
143         return bufs[slot];
144 }
145
146
147 extern const char *
148 uint64_to_nicestr(uint64_t value, enum nicestr_unit unit_min,
149                 enum nicestr_unit unit_max, bool always_also_bytes,
150                 uint32_t slot)
151 {
152         assert(unit_min <= unit_max);
153         assert(unit_max <= NICESTR_TIB);
154
155         enum nicestr_unit unit = NICESTR_B;
156         const char *str;
157
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);
162         } else {
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);
167                 do {
168                         d /= 1024.0;
169                         ++unit;
170                 } while (unit < unit_min || (d > 9999.9 && unit < unit_max));
171
172                 str = double_to_str(d);
173         }
174
175         static const char suffix[5][4] = { "B", "KiB", "MiB", "GiB", "TiB" };
176
177         // Minimum buffer size:
178         // 26   2^64 with thousand separators
179         //  4   " KiB"
180         //  2   " ("
181         // 26   2^64 with thousand separators
182         //  3   " B)"
183         //  1   '\0'
184         // 62   Total
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]);
189
190         if (always_also_bytes && value >= 10000)
191                 snprintf(pos, left, " (%s B)", uint64_to_str(value, slot));
192
193         return buf[slot];
194 }
195
196
197 extern const char *
198 double_to_str(double value)
199 {
200         static char buf[64];
201
202         static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
203         if (thousand == UNKNOWN) {
204                 buf[0] = '\0';
205                 snprintf(buf, sizeof(buf), "%'.1f", 2.0);
206                 thousand = buf[0] == '2' ? WORKS : BROKEN;
207         }
208
209         if (thousand == WORKS)
210                 snprintf(buf, sizeof(buf), "%'.1f", value);
211         else
212                 snprintf(buf, sizeof(buf), "%.1f", value);
213
214         return buf;
215 }
216
217
218 extern void
219 my_snprintf(char **pos, size_t *left, const char *fmt, ...)
220 {
221         va_list ap;
222         va_start(ap, fmt);
223         const int len = vsnprintf(*pos, *left, fmt, ap);
224         va_end(ap);
225
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) {
230                 *left = 0;
231         } else {
232                 *pos += len;
233                 *left -= len;
234         }
235
236         return;
237 }
238
239
240 /*
241 /// \brief      Simple quoting to get rid of ASCII control characters
242 ///
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.
245 ///
246 extern char *
247 str_quote(const char *str)
248 {
249         size_t dest_len = 0;
250         bool has_ctrl = false;
251
252         while (str[dest_len] != '\0')
253                 if (*(unsigned char *)(str + dest_len++) < 0x20)
254                         has_ctrl = true;
255
256         char *dest = malloc(dest_len + 1);
257         if (dest != NULL) {
258                 if (has_ctrl) {
259                         for (size_t i = 0; i < dest_len; ++i)
260                                 if (*(unsigned char *)(str + i) < 0x20)
261                                         dest[i] = '?';
262                                 else
263                                         dest[i] = str[i];
264
265                         dest[dest_len] = '\0';
266
267                 } else {
268                         // Usually there are no control characters,
269                         // so we can optimize.
270                         memcpy(dest, str, dest_len + 1);
271                 }
272         }
273
274         return dest;
275 }
276 */
277
278
279 extern bool
280 is_empty_filename(const char *filename)
281 {
282         if (filename[0] == '\0') {
283                 message_error(_("Empty filename, skipping"));
284                 return true;
285         }
286
287         return false;
288 }
289
290
291 extern bool
292 is_tty_stdin(void)
293 {
294         const bool ret = isatty(STDIN_FILENO);
295
296         if (ret)
297                 message_error(_("Compressed data cannot be read from "
298                                 "a terminal"));
299
300         return ret;
301 }
302
303
304 extern bool
305 is_tty_stdout(void)
306 {
307         const bool ret = isatty(STDOUT_FILENO);
308
309         if (ret)
310                 message_error(_("Compressed data cannot be written to "
311                                 "a terminal"));
312
313         return ret;
314 }