]> icculus.org git repositories - icculus/xz.git/blob - src/xz/util.c
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / 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
15
16 extern void *
17 xrealloc(void *ptr, size_t size)
18 {
19         assert(size > 0);
20
21         ptr = realloc(ptr, size);
22         if (ptr == NULL)
23                 message_fatal("%s", strerror(errno));
24
25         return ptr;
26 }
27
28
29 extern char *
30 xstrdup(const char *src)
31 {
32         assert(src != NULL);
33         const size_t size = strlen(src) + 1;
34         char *dest = xmalloc(size);
35         return memcpy(dest, src, size);
36 }
37
38
39 extern uint64_t
40 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
41 {
42         uint64_t result = 0;
43
44         // Skip blanks.
45         while (*value == ' ' || *value == '\t')
46                 ++value;
47
48         if (*value < '0' || *value > '9')
49                 message_fatal(_("%s: Value is not a non-negative "
50                                 "decimal integer"), value);
51
52         do {
53                 // Don't overflow.
54                 if (result > (UINT64_MAX - 9) / 10)
55                         goto error;
56
57                 result *= 10;
58                 result += *value - '0';
59                 ++value;
60         } while (*value >= '0' && *value <= '9');
61
62         if (*value != '\0') {
63                 // Look for suffix.
64                 static const struct {
65                         const char name[4];
66                         uint64_t multiplier;
67                 } suffixes[] = {
68                         { "k",   UINT64_C(1000) },
69                         { "kB",  UINT64_C(1000) },
70                         { "M",   UINT64_C(1000000) },
71                         { "MB",  UINT64_C(1000000) },
72                         { "G",   UINT64_C(1000000000) },
73                         { "GB",  UINT64_C(1000000000) },
74                         { "Ki",  UINT64_C(1024) },
75                         { "KiB", UINT64_C(1024) },
76                         { "Mi",  UINT64_C(1048576) },
77                         { "MiB", UINT64_C(1048576) },
78                         { "Gi",  UINT64_C(1073741824) },
79                         { "GiB", UINT64_C(1073741824) }
80                 };
81
82                 uint64_t multiplier = 0;
83                 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
84                         if (strcmp(value, suffixes[i].name) == 0) {
85                                 multiplier = suffixes[i].multiplier;
86                                 break;
87                         }
88                 }
89
90                 if (multiplier == 0) {
91                         message(V_ERROR, _("%s: Invalid multiplier suffix. "
92                                         "Valid suffixes:"), value);
93                         message_fatal("`k' (10^3), `M' (10^6), `G' (10^9) "
94                                         "`Ki' (2^10), `Mi' (2^20), "
95                                         "`Gi' (2^30)");
96                 }
97
98                 // Don't overflow here either.
99                 if (result > UINT64_MAX / multiplier)
100                         goto error;
101
102                 result *= multiplier;
103         }
104
105         if (result < min || result > max)
106                 goto error;
107
108         return result;
109
110 error:
111         message_fatal(_("Value of the option `%s' must be in the range "
112                                 "[%" PRIu64 ", %" PRIu64 "]"),
113                                 name, min, max);
114 }
115
116
117 /*
118 /// \brief      Simple quoting to get rid of ASCII control characters
119 ///
120 /// This is not so cool and locale-dependent, but should be good enough
121 /// At least we don't print any control characters on the terminal.
122 ///
123 extern char *
124 str_quote(const char *str)
125 {
126         size_t dest_len = 0;
127         bool has_ctrl = false;
128
129         while (str[dest_len] != '\0')
130                 if (*(unsigned char *)(str + dest_len++) < 0x20)
131                         has_ctrl = true;
132
133         char *dest = malloc(dest_len + 1);
134         if (dest != NULL) {
135                 if (has_ctrl) {
136                         for (size_t i = 0; i < dest_len; ++i)
137                                 if (*(unsigned char *)(str + i) < 0x20)
138                                         dest[i] = '?';
139                                 else
140                                         dest[i] = str[i];
141
142                         dest[dest_len] = '\0';
143
144                 } else {
145                         // Usually there are no control characters,
146                         // so we can optimize.
147                         memcpy(dest, str, dest_len + 1);
148                 }
149         }
150
151         return dest;
152 }
153 */
154
155
156 extern bool
157 is_empty_filename(const char *filename)
158 {
159         if (filename[0] == '\0') {
160                 message_error(_("Empty filename, skipping"));
161                 return true;
162         }
163
164         return false;
165 }
166
167
168 extern bool
169 is_tty_stdin(void)
170 {
171         const bool ret = isatty(STDIN_FILENO);
172
173         if (ret)
174                 message_error(_("Compressed data not read from a terminal "
175                                 "unless `--force' is used."));
176
177         return ret;
178 }
179
180
181 extern bool
182 is_tty_stdout(void)
183 {
184         const bool ret = isatty(STDOUT_FILENO);
185
186         if (ret)
187                 message_error(_("Compressed data not written to a terminal "
188                                 "unless `--force' is used."));
189
190         return ret;
191 }