]> icculus.org git repositories - icculus/xz.git/blob - src/xz/util.c
Support special value "max" where xz and xzdec accept an integer.
[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         // Accept special value "max". Supporting "min" doesn't seem useful.
49         if (strcmp(value, "max") == 0)
50                 return max;
51
52         if (*value < '0' || *value > '9')
53                 message_fatal(_("%s: Value is not a non-negative "
54                                 "decimal integer"), value);
55
56         do {
57                 // Don't overflow.
58                 if (result > (UINT64_MAX - 9) / 10)
59                         goto error;
60
61                 result *= 10;
62                 result += *value - '0';
63                 ++value;
64         } while (*value >= '0' && *value <= '9');
65
66         if (*value != '\0') {
67                 // Look for suffix.
68                 static const struct {
69                         const char name[4];
70                         uint64_t multiplier;
71                 } suffixes[] = {
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) }
84                 };
85
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;
90                                 break;
91                         }
92                 }
93
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), "
99                                         "`Gi' (2^30)");
100                 }
101
102                 // Don't overflow here either.
103                 if (result > UINT64_MAX / multiplier)
104                         goto error;
105
106                 result *= multiplier;
107         }
108
109         if (result < min || result > max)
110                 goto error;
111
112         return result;
113
114 error:
115         message_fatal(_("Value of the option `%s' must be in the range "
116                                 "[%" PRIu64 ", %" PRIu64 "]"),
117                                 name, min, max);
118 }
119
120
121 /*
122 /// \brief      Simple quoting to get rid of ASCII control characters
123 ///
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.
126 ///
127 extern char *
128 str_quote(const char *str)
129 {
130         size_t dest_len = 0;
131         bool has_ctrl = false;
132
133         while (str[dest_len] != '\0')
134                 if (*(unsigned char *)(str + dest_len++) < 0x20)
135                         has_ctrl = true;
136
137         char *dest = malloc(dest_len + 1);
138         if (dest != NULL) {
139                 if (has_ctrl) {
140                         for (size_t i = 0; i < dest_len; ++i)
141                                 if (*(unsigned char *)(str + i) < 0x20)
142                                         dest[i] = '?';
143                                 else
144                                         dest[i] = str[i];
145
146                         dest[dest_len] = '\0';
147
148                 } else {
149                         // Usually there are no control characters,
150                         // so we can optimize.
151                         memcpy(dest, str, dest_len + 1);
152                 }
153         }
154
155         return dest;
156 }
157 */
158
159
160 extern bool
161 is_empty_filename(const char *filename)
162 {
163         if (filename[0] == '\0') {
164                 message_error(_("Empty filename, skipping"));
165                 return true;
166         }
167
168         return false;
169 }
170
171
172 extern bool
173 is_tty_stdin(void)
174 {
175         const bool ret = isatty(STDIN_FILENO);
176
177         if (ret)
178                 message_error(_("Compressed data not read from a terminal "
179                                 "unless `--force' is used."));
180
181         return ret;
182 }
183
184
185 extern bool
186 is_tty_stdout(void)
187 {
188         const bool ret = isatty(STDOUT_FILENO);
189
190         if (ret)
191                 message_error(_("Compressed data not written to a terminal "
192                                 "unless `--force' is used."));
193
194         return ret;
195 }