Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
util.h File Reference

Miscellaneous utility functions. More...

Go to the source code of this file.

Macros

#define xmalloc(size)
 Safe malloc() that never returns NULL.
 

Enumerations

enum  nicestr_unit {
  NICESTR_B , NICESTR_KIB , NICESTR_MIB , NICESTR_GIB ,
  NICESTR_TIB
}
 

Functions

void * xrealloc (void *ptr, size_t size) lzma_attribute((__malloc__)) lzma_attr_alloc_size(2)
 Safe realloc() that never returns NULL.
 
char * xstrdup (const char *src) lzma_attribute((__malloc__))
 Safe strdup() that never returns NULL.
 
uint64_t str_to_uint64 (const char *name, const char *value, uint64_t min, uint64_t max)
 Fancy version of strtoull()
 
uint64_t round_up_to_mib (uint64_t n)
 Round an integer up to the next full MiB and convert to MiB.
 
const char * uint64_to_str (uint64_t value, uint32_t slot)
 Convert uint64_t to a string.
 
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.
 
void my_snprintf (char **pos, size_t *left, const char *fmt,...) lzma_attribute((__format__(__printf__
 Wrapper for snprintf() to help constructing a string in pieces.
 
void bool is_tty_stdin (void)
 Test if stdin is a terminal.
 
bool is_tty_stdout (void)
 Test if stdout is a terminal.
 

Detailed Description

Miscellaneous utility functions.

Macro Definition Documentation

◆ xmalloc

#define xmalloc ( size)
Value:
size_t size
Definition platform.h:559
void * xrealloc(void *ptr, size_t size) lzma_attribute((__malloc__)) lzma_attr_alloc_size(2)
Safe realloc() that never returns NULL.
Definition util.c:25
#define NULL
Definition getopt1.c:37

Safe malloc() that never returns NULL.

Note
xmalloc(), xrealloc(), and xstrdup() must not be used when there are files open for writing, that should be cleaned up before exiting.

Enumeration Type Documentation

◆ nicestr_unit

Enumerator
NICESTR_B 
NICESTR_KIB 
NICESTR_MIB 
NICESTR_GIB 
NICESTR_TIB 

Function Documentation

◆ is_tty_stdin()

void bool is_tty_stdin ( void )
extern

Test if stdin is a terminal.

If stdin is a terminal, an error message is printed and exit status set to EXIT_ERROR.

◆ is_tty_stdout()

bool is_tty_stdout ( void )
extern

Test if stdout is a terminal.

If stdout is a terminal, an error message is printed and exit status set to EXIT_ERROR.

◆ my_snprintf()

void my_snprintf ( char ** pos,
size_t * left,
const char * fmt,
... )
extern

Wrapper for snprintf() to help constructing a string in pieces.

A maximum of *left bytes is written starting from *pos. *pos and *left are updated accordingly.

◆ round_up_to_mib()

uint64_t round_up_to_mib ( uint64_t n)
extern

Round an integer up to the next full MiB and convert to MiB.

This is used when printing memory usage and limit.

◆ str_to_uint64()

uint64_t str_to_uint64 ( const char * name,
const char * value,
uint64_t min,
uint64_t max )
extern

Fancy version of strtoull()

Parameters
nameName of the option to show in case of an error
valueString containing the number to be parsed; may contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi"
minMinimum valid value
maxMaximum valid value
Returns
Parsed value that is in the range [min, max]. Does not return if an error occurs.

◆ uint64_to_nicestr()

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 )
extern

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.

Parameters
valueValue to be printed
unit_minSmallest unit to use. This and unit_max are used e.g. when showing the progress indicator to force the unit to MiB.
unit_maxBiggest unit to use. assert(unit_min <= unit_max).
always_also_bytesShow also the exact byte value in parenthesis if the nicely formatted string uses bigger unit than bytes.
slotWhich static buffer to use to hold the string. This is shared with uint64_to_str().
Returns
Pointer to statically allocated buffer containing the string.
Note
This uses double_to_str() internally so the static buffer in double_to_str() will be overwritten.

◆ uint64_to_str()

const char * uint64_to_str ( uint64_t value,
uint32_t slot )
extern

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).

◆ xrealloc()

void * xrealloc ( void * p,
size_t n )
extern

Safe realloc() that never returns NULL.

Tar related function, version 1.34

◆ xstrdup()

char * xstrdup ( char const * string)
extern

Safe strdup() that never returns NULL.

Function to duplicate a string

Parameters
stringthe input string
Returns
the duplicated new address