Rizin
unix-like reverse engineering framework and cli tools
|
Miscellaneous utility functions. More...
Go to the source code of this file.
Enumerations | |
enum | { UNKNOWN , WORKS , BROKEN } |
Thousand separator support in uint64_to_str() and uint64_to_nicestr() More... | |
Functions | |
void * | xrealloc (void *ptr, size_t size) |
Safe realloc() that never returns NULL. More... | |
char * | xstrdup (const char *src) |
Safe strdup() that never returns NULL. More... | |
uint64_t | str_to_uint64 (const char *name, const char *value, uint64_t min, uint64_t max) |
Fancy version of strtoull() More... | |
uint64_t | round_up_to_mib (uint64_t n) |
Round an integer up to the next full MiB and convert to MiB. More... | |
static void | check_thousand_sep (uint32_t slot) |
const char * | uint64_to_str (uint64_t value, uint32_t slot) |
Convert uint64_t to a string. More... | |
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) |
Convert uint64_t to a nice human readable string. More... | |
void | my_snprintf (char **pos, size_t *left, const char *fmt,...) |
bool | is_empty_filename (const char *filename) |
Check if filename is empty and print an error message. More... | |
bool | is_tty_stdin (void) |
Test if stdin is a terminal. More... | |
bool | is_tty_stdout (void) |
Test if stdout is a terminal. More... | |
Variables | |
static char | bufs [4][128] |
Buffers for uint64_to_str() and uint64_to_nicestr() More... | |
static enum { ... } | thousand = UNKNOWN |
Thousand separator support in uint64_to_str() and uint64_to_nicestr() More... | |
Miscellaneous utility functions.
Definition in file util.c.
anonymous enum |
Thousand separator support in uint64_to_str() and uint64_to_nicestr()
Enumerator | |
---|---|
UNKNOWN | |
WORKS | |
BROKEN |
|
static |
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.
Definition at line 156 of file util.c.
References BROKEN, bufs, snprintf, thousand, UNKNOWN, and WORKS.
Referenced by uint64_to_nicestr(), and uint64_to_str().
Check if filename is empty and print an error message.
Definition at line 264 of file util.c.
References _, and message_error().
Referenced by io_open_src().
bool is_tty_stdin | ( | void | ) |
Test if stdin is a terminal.
If stdin is a terminal, an error message is printed and exit status set to EXIT_ERROR.
Definition at line 276 of file util.c.
References _, message_error(), and STDIN_FILENO.
Referenced by main().
bool is_tty_stdout | ( | void | ) |
Test if stdout is a terminal.
If stdout is a terminal, an error message is printed and exit status set to EXIT_ERROR.
Definition at line 289 of file util.c.
References _, message_error(), and STDOUT_FILENO.
Referenced by main().
Definition at line 239 of file util.c.
References len, pos, and vsnprintf.
Referenced by get_check_names(), message_filters_to_str(), progress_sizes(), and uint64_to_nicestr().
Round an integer up to the next full MiB and convert to MiB.
This is used when printing memory usage and limit.
Definition at line 139 of file util.c.
Referenced by coder_init(), memlimit_show(), message_mem_needed(), print_info_adv(), and print_totals_adv().
Fancy version of strtoull()
name | Name of the option to show in case of an error |
value | String containing the number to be parsed; may contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi" |
min | Minimum valid value |
max | Maximum valid value |
Definition at line 58 of file util.c.
References _, add(), error(), max, message, message_fatal(), min, PRIu64, UINT64_C, UINT64_MAX, V_ERROR, and value.
Referenced by parse_block_list(), parse_memlimit(), parse_options(), and parse_real().
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 | ||
) |
Convert uint64_t to a nice human readable string.
This is like uint64_to_str() but uses B, KiB, MiB, GiB, or TiB suffix and optionally includes the exact size in parenthesis.
value | Value to be printed |
unit_min | Smallest unit to use. This and unit_max are used e.g. when showing the progress indicator to force the unit to MiB. |
unit_max | Biggest unit to use. assert(unit_min <= unit_max). |
always_also_bytes | Show also the exact byte value in parenthesis if the nicely formatted string uses bigger unit than bytes. |
slot | Which static buffer to use to hold the string. This is shared with uint64_to_str(). |
Definition at line 187 of file util.c.
References ARRAY_SIZE, assert(), bufs, check_thousand_sep(), d, my_snprintf(), NICESTR_B, NICESTR_TIB, pos, PRIu64, snprintf, suffix, thousand, value, and WORKS.
Referenced by print_adv_helper(), print_info_basic(), print_totals_basic(), and progress_sizes().
Convert uint64_t to a string.
Convert the given value to a string with locale-specific thousand separators, if supported by the snprintf() implementation. The string is stored into an internal static buffer indicated by the slot argument. A pointer to the selected buffer is returned.
This function exists, because non-POSIX systems don't support thousand separator in format strings. Solving the problem in a simple way doesn't work, because it breaks gettext (specifically, the xgettext tool).
Definition at line 171 of file util.c.
References ARRAY_SIZE, assert(), bufs, check_thousand_sep(), PRIu64, snprintf, thousand, value, and WORKS.
Referenced by coder_init(), memlimit_show(), message_mem_needed(), print_adv_helper(), print_info_adv(), print_info_basic(), print_totals_adv(), and print_totals_basic().
void* xrealloc | ( | void * | ptr, |
size_t | size | ||
) |
Safe realloc() that never returns NULL.
Definition at line 25 of file util.c.
References assert(), free(), message_fatal(), NULL, p, and realloc().
Referenced by read_name().
char* xstrdup | ( | const char * | src | ) |
Safe strdup() that never returns NULL.
Definition at line 48 of file util.c.
References assert(), dest, memcpy(), NULL, src, and xmalloc.
Referenced by parse_environment(), parse_options(), and suffix_set().
|
static |
Buffers for uint64_to_str() and uint64_to_nicestr()
Definition at line 18 of file util.c.
Referenced by check_thousand_sep(), uint64_to_nicestr(), uint64_to_str(), uv__build_coalesced_write_req(), uv__count_bufs(), uv__fs_buf_offset(), uv__fs_preadv(), uv__fs_write_all(), uv__pipe_write(), uv__pipe_write_data(), uv__pipe_write_ipc(), uv__send(), uv__tcp_try_write(), uv__tty_try_write(), uv__udp_send(), uv__udp_try_send(), uv_fs_read(), uv_fs_write(), uv_tcp_write(), uv_try_write(), uv_tty_write(), uv_tty_write_bufs(), uv_udp_send(), uv_udp_try_send(), uv_write(), and uv_write2().
enum { ... } thousand |
Thousand separator support in uint64_to_str() and uint64_to_nicestr()
Referenced by check_thousand_sep(), uint64_to_nicestr(), and uint64_to_str().