2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file util.c
|
|
|
|
/// \brief Miscellaneous utility functions
|
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "private.h"
|
2010-01-24 15:57:40 +01:00
|
|
|
#include <stdarg.h>
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2010-09-10 09:30:33 +02:00
|
|
|
/// Buffers for uint64_to_str() and uint64_to_nicestr()
|
|
|
|
static char bufs[4][128];
|
|
|
|
|
|
|
|
/// Thousand separator support in uint64_to_str() and uint64_to_nicestr()
|
|
|
|
static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
|
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
extern void *
|
|
|
|
xrealloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
assert(size > 0);
|
|
|
|
|
2012-09-28 19:11:09 +02:00
|
|
|
// Save ptr so that we can free it if realloc fails.
|
|
|
|
// The point is that message_fatal ends up calling stdio functions
|
|
|
|
// which in some libc implementations might allocate memory from
|
|
|
|
// the heap. Freeing ptr improves the chances that there's free
|
|
|
|
// memory for stdio functions if they need it.
|
|
|
|
void *p = ptr;
|
2008-11-19 19:46:52 +01:00
|
|
|
ptr = realloc(ptr, size);
|
2012-09-28 19:11:09 +02:00
|
|
|
|
|
|
|
if (ptr == NULL) {
|
|
|
|
const int saved_errno = errno;
|
|
|
|
free(p);
|
|
|
|
message_fatal("%s", strerror(saved_errno));
|
|
|
|
}
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern char *
|
|
|
|
xstrdup(const char *src)
|
|
|
|
{
|
|
|
|
assert(src != NULL);
|
|
|
|
const size_t size = strlen(src) + 1;
|
|
|
|
char *dest = xmalloc(size);
|
|
|
|
return memcpy(dest, src, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
extern uint64_t
|
|
|
|
str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
|
|
|
|
{
|
|
|
|
uint64_t result = 0;
|
|
|
|
|
|
|
|
// Skip blanks.
|
|
|
|
while (*value == ' ' || *value == '\t')
|
|
|
|
++value;
|
|
|
|
|
2009-05-21 16:22:01 +02:00
|
|
|
// Accept special value "max". Supporting "min" doesn't seem useful.
|
|
|
|
if (strcmp(value, "max") == 0)
|
|
|
|
return max;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (*value < '0' || *value > '9')
|
|
|
|
message_fatal(_("%s: Value is not a non-negative "
|
|
|
|
"decimal integer"), value);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Don't overflow.
|
2010-06-11 20:43:28 +02:00
|
|
|
if (result > UINT64_MAX / 10)
|
2007-12-08 23:42:33 +01:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
result *= 10;
|
2010-06-11 20:43:28 +02:00
|
|
|
|
|
|
|
// Another overflow check
|
2019-06-23 22:19:34 +02:00
|
|
|
const uint32_t add = (uint32_t)(*value - '0');
|
2010-06-11 20:43:28 +02:00
|
|
|
if (UINT64_MAX - add < result)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
result += add;
|
2007-12-08 23:42:33 +01:00
|
|
|
++value;
|
|
|
|
} while (*value >= '0' && *value <= '9');
|
|
|
|
|
|
|
|
if (*value != '\0') {
|
Treat all integer multiplier suffixes as base-2.
Originally both base-2 and base-10 were supported, but since
there seems to be little need for base-10 in XZ Utils, treat
everything as base-2 and also be more relaxed about the case
of the first letter of the suffix. Now xz will accept e.g.
KiB, Ki, k, K, kB, and KB, and interpret them all as 1024. The
recommended spelling of the suffixes are still KiB, MiB, and GiB.
2010-03-07 12:59:32 +01:00
|
|
|
// Look for suffix. Originally this supported both base-2
|
|
|
|
// and base-10, but since there seems to be little need
|
|
|
|
// for base-10 in this program, treat everything as base-2
|
|
|
|
// and also be more relaxed about the case of the first
|
|
|
|
// letter of the suffix.
|
2007-12-08 23:42:33 +01:00
|
|
|
uint64_t multiplier = 0;
|
Treat all integer multiplier suffixes as base-2.
Originally both base-2 and base-10 were supported, but since
there seems to be little need for base-10 in XZ Utils, treat
everything as base-2 and also be more relaxed about the case
of the first letter of the suffix. Now xz will accept e.g.
KiB, Ki, k, K, kB, and KB, and interpret them all as 1024. The
recommended spelling of the suffixes are still KiB, MiB, and GiB.
2010-03-07 12:59:32 +01:00
|
|
|
if (*value == 'k' || *value == 'K')
|
|
|
|
multiplier = UINT64_C(1) << 10;
|
|
|
|
else if (*value == 'm' || *value == 'M')
|
|
|
|
multiplier = UINT64_C(1) << 20;
|
|
|
|
else if (*value == 'g' || *value == 'G')
|
|
|
|
multiplier = UINT64_C(1) << 30;
|
|
|
|
|
|
|
|
++value;
|
|
|
|
|
|
|
|
// Allow also e.g. Ki, KiB, and KB.
|
|
|
|
if (*value != '\0' && strcmp(value, "i") != 0
|
|
|
|
&& strcmp(value, "iB") != 0
|
|
|
|
&& strcmp(value, "B") != 0)
|
|
|
|
multiplier = 0;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
if (multiplier == 0) {
|
Treat all integer multiplier suffixes as base-2.
Originally both base-2 and base-10 were supported, but since
there seems to be little need for base-10 in XZ Utils, treat
everything as base-2 and also be more relaxed about the case
of the first letter of the suffix. Now xz will accept e.g.
KiB, Ki, k, K, kB, and KB, and interpret them all as 1024. The
recommended spelling of the suffixes are still KiB, MiB, and GiB.
2010-03-07 12:59:32 +01:00
|
|
|
message(V_ERROR, _("%s: Invalid multiplier suffix"),
|
|
|
|
value - 1);
|
|
|
|
message_fatal(_("Valid suffixes are `KiB' (2^10), "
|
|
|
|
"`MiB' (2^20), and `GiB' (2^30)."));
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't overflow here either.
|
|
|
|
if (result > UINT64_MAX / multiplier)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
result *= multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result < min || result > max)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
error:
|
2008-11-19 19:46:52 +01:00
|
|
|
message_fatal(_("Value of the option `%s' must be in the range "
|
2009-02-05 08:12:57 +01:00
|
|
|
"[%" PRIu64 ", %" PRIu64 "]"),
|
|
|
|
name, min, max);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 17:17:50 +01:00
|
|
|
extern uint64_t
|
|
|
|
round_up_to_mib(uint64_t n)
|
|
|
|
{
|
|
|
|
return (n >> 20) + ((n & ((UINT32_C(1) << 20) - 1)) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-11 20:15:35 +01:00
|
|
|
/// Check if thousands separator is supported. Run-time checking is easiest
|
|
|
|
/// because it seems to be sometimes lacking even on a POSIXish system.
|
|
|
|
/// Note that trying to use thousands separators when snprintf() doesn't
|
|
|
|
/// support them results in undefined behavior. This just has happened to
|
|
|
|
/// work well enough in practice.
|
|
|
|
///
|
|
|
|
/// DJGPP 2.05 added support for thousands separators but it's broken
|
|
|
|
/// at least under WinXP with Finnish locale that uses a non-breaking space
|
|
|
|
/// as the thousands separator. Workaround by disabling thousands separators
|
|
|
|
/// for DJGPP builds.
|
2010-09-10 09:30:33 +02:00
|
|
|
static void
|
|
|
|
check_thousand_sep(uint32_t slot)
|
2009-06-26 19:36:45 +02:00
|
|
|
{
|
2009-08-29 13:43:52 +02:00
|
|
|
if (thousand == UNKNOWN) {
|
|
|
|
bufs[slot][0] = '\0';
|
2020-03-11 20:15:35 +01:00
|
|
|
#ifndef __DJGPP__
|
2010-09-10 09:30:33 +02:00
|
|
|
snprintf(bufs[slot], sizeof(bufs[slot]), "%'u", 1U);
|
2020-03-11 20:15:35 +01:00
|
|
|
#endif
|
2009-08-29 13:43:52 +02:00
|
|
|
thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
|
|
|
|
}
|
|
|
|
|
2010-09-10 09:30:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern const char *
|
|
|
|
uint64_to_str(uint64_t value, uint32_t slot)
|
|
|
|
{
|
|
|
|
assert(slot < ARRAY_SIZE(bufs));
|
|
|
|
|
|
|
|
check_thousand_sep(slot);
|
|
|
|
|
2009-08-29 13:43:52 +02:00
|
|
|
if (thousand == WORKS)
|
|
|
|
snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64, value);
|
|
|
|
else
|
|
|
|
snprintf(bufs[slot], sizeof(bufs[slot]), "%" PRIu64, value);
|
|
|
|
|
2009-06-26 19:36:45 +02:00
|
|
|
return bufs[slot];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-24 15:57:40 +01:00
|
|
|
extern const char *
|
|
|
|
uint64_to_nicestr(uint64_t value, enum nicestr_unit unit_min,
|
|
|
|
enum nicestr_unit unit_max, bool always_also_bytes,
|
|
|
|
uint32_t slot)
|
|
|
|
{
|
|
|
|
assert(unit_min <= unit_max);
|
|
|
|
assert(unit_max <= NICESTR_TIB);
|
2010-09-10 09:30:33 +02:00
|
|
|
assert(slot < ARRAY_SIZE(bufs));
|
|
|
|
|
|
|
|
check_thousand_sep(slot);
|
2010-01-24 15:57:40 +01:00
|
|
|
|
|
|
|
enum nicestr_unit unit = NICESTR_B;
|
2010-09-10 09:30:33 +02:00
|
|
|
char *pos = bufs[slot];
|
|
|
|
size_t left = sizeof(bufs[slot]);
|
2010-01-24 15:57:40 +01:00
|
|
|
|
|
|
|
if ((unit_min == NICESTR_B && value < 10000)
|
|
|
|
|| unit_max == NICESTR_B) {
|
|
|
|
// The value is shown as bytes.
|
2010-09-10 09:30:33 +02:00
|
|
|
if (thousand == WORKS)
|
|
|
|
my_snprintf(&pos, &left, "%'u", (unsigned int)value);
|
|
|
|
else
|
|
|
|
my_snprintf(&pos, &left, "%u", (unsigned int)value);
|
2010-01-24 15:57:40 +01:00
|
|
|
} else {
|
|
|
|
// Scale the value to a nicer unit. Unless unit_min and
|
|
|
|
// unit_max limit us, we will show at most five significant
|
|
|
|
// digits with one decimal place.
|
|
|
|
double d = (double)(value);
|
|
|
|
do {
|
|
|
|
d /= 1024.0;
|
|
|
|
++unit;
|
|
|
|
} while (unit < unit_min || (d > 9999.9 && unit < unit_max));
|
|
|
|
|
2010-09-10 09:30:33 +02:00
|
|
|
if (thousand == WORKS)
|
|
|
|
my_snprintf(&pos, &left, "%'.1f", d);
|
|
|
|
else
|
|
|
|
my_snprintf(&pos, &left, "%.1f", d);
|
2010-01-24 15:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char suffix[5][4] = { "B", "KiB", "MiB", "GiB", "TiB" };
|
2010-09-10 09:30:33 +02:00
|
|
|
my_snprintf(&pos, &left, " %s", suffix[unit]);
|
2010-01-24 15:57:40 +01:00
|
|
|
|
2010-09-10 09:30:33 +02:00
|
|
|
if (always_also_bytes && value >= 10000) {
|
|
|
|
if (thousand == WORKS)
|
|
|
|
snprintf(pos, left, " (%'" PRIu64 " B)", value);
|
|
|
|
else
|
|
|
|
snprintf(pos, left, " (%" PRIu64 " B)", value);
|
2009-08-29 13:43:52 +02:00
|
|
|
}
|
|
|
|
|
2010-09-10 09:30:33 +02:00
|
|
|
return bufs[slot];
|
2009-06-26 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-24 15:57:40 +01:00
|
|
|
extern void
|
|
|
|
my_snprintf(char **pos, size_t *left, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
const int len = vsnprintf(*pos, *left, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
// If an error occurred, we want the caller to think that the whole
|
|
|
|
// buffer was used. This way no more data will be written to the
|
2010-09-10 09:30:33 +02:00
|
|
|
// buffer. We don't need better error handling here, although it
|
|
|
|
// is possible that the result looks garbage on the terminal if
|
|
|
|
// e.g. an UTF-8 character gets split. That shouldn't (easily)
|
|
|
|
// happen though, because the buffers used have some extra room.
|
2010-01-24 15:57:40 +01:00
|
|
|
if (len < 0 || (size_t)(len) >= *left) {
|
|
|
|
*left = 0;
|
|
|
|
} else {
|
|
|
|
*pos += len;
|
2019-06-23 22:19:34 +02:00
|
|
|
*left -= (size_t)(len);
|
2010-01-24 15:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
extern bool
|
|
|
|
is_tty_stdin(void)
|
|
|
|
{
|
|
|
|
const bool ret = isatty(STDIN_FILENO);
|
|
|
|
|
|
|
|
if (ret)
|
2010-01-13 18:10:25 +01:00
|
|
|
message_error(_("Compressed data cannot be read from "
|
|
|
|
"a terminal"));
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern bool
|
|
|
|
is_tty_stdout(void)
|
|
|
|
{
|
|
|
|
const bool ret = isatty(STDOUT_FILENO);
|
|
|
|
|
|
|
|
if (ret)
|
2010-01-13 18:10:25 +01:00
|
|
|
message_error(_("Compressed data cannot be written to "
|
|
|
|
"a terminal"));
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|