Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

misc.c File Reference


Detailed Description

Miscellaneous functions.

Author:
Raphael Manfredi
Date:
2001-2003

#include "common.h"
#include "base32.h"
#include "endian.h"
#include "misc.h"
#include "glib-missing.h"
#include "sha1.h"
#include "tm.h"
#include "walloc.h"
#include "utf8.h"
#include "override.h"

Defines

#define srandom(x)   srand(x)
#define random()   rand()
#define RANDOM_MASK   0xffffffff
#define RANDOM_MAXV   RAND_MAX
#define TM_YEAR_BASE   1900
#define ESCAPE_CHAR   '\\'
#define CHAR_IS_SPACE(c)   ((c) == ' ' || (c) == '\t' || (c) == '\n')
#define CHAR_IS_SAFE(c, strict)   (isprint((c)) || (!(strict) && CHAR_IS_SPACE(c)))
#define GENERATE_PARSE_UNSIGNED(NAME, TYPE)
#define GENERATE_PARSE_UINTX(bits)   GENERATE_PARSE_UNSIGNED(CAT2(parse_uint,bits), CAT2(guint,bits))
#define REPLACE(x)   { *len = CONST_STRLEN(x); return (x); }

Functions

 RCSID ("$Id:misc.c, v 1.114 2005/12/27 13:10:50 cbiere Exp $")
size_t strlcpy (gchar *dst, const gchar *src, size_t dst_size)
 Needs to be defined if we are not using Glib 2.

size_t strlcat (gchar *dst, const gchar *src, size_t dst_size)
size_t concat_strings (gchar *dst, size_t size, const gchar *s,...)
 Concatenates a variable number of NUL-terminated strings into ``dst''.

size_t w_concat_strings (gchar **dst_ptr, const gchar *first,...)
 Concatenates a variable number of NUL-terminated strings into buffer which will be allocated using walloc().

gchar * is_strprefix (const gchar *str, const gchar *prefix)
 Checks whether ``prefix'' is a prefix of ``str''.

gchar * is_strcaseprefix (const gchar *str, const gchar *prefix)
 Checks whether ``prefix'' is a prefix of ``str'' performing an case-insensitive (ASCII only) check.

gboolean file_exists (const gchar *f)
 Check for file existence.

size_t print_uint16_hex (gchar *dst, guint16 v)
 Prints the unsigned 16-bit value ``v'' in hexadecimal presentation as NUL-terminated string to ``dst'' and returns the length of the resulting string.

size_t ipv6_to_string_buf (const uint8_t *ipv6, gchar *dst, size_t size)
 Prints the IPv6 address ``ipv6'' to ``dst''.

const gchar * ipv6_to_string (const guint8 *ipv6)
 Prints the IPv6 address ``ipv6'' to a static buffer.

size_t ip_to_string_buf (guint32 ip, gchar *buf, size_t size)
const gchar * ip_to_string (guint32 ip)
const gchar * ip_to_string2 (guint32 ip)
const gchar * ip_port_to_string (guint32 ip, guint16 port)
const gchar * hostname_port_to_string (const gchar *hostname, guint16 port)
guint32 string_to_ip (const gchar *s)
gboolean parse_ipv6_addr (const gchar *s, guint8 *dst, const gchar **endptr)
 Parses an IPv6 address.

gboolean string_to_ip_strict (const gchar *s, guint32 *addr, const gchar **endptr)
 A strict string to IP address conversion; when other stuff from misc.[ch] is not sufficient.

gboolean string_to_ip_port (const gchar *s, guint32 *ip_ptr, guint16 *port_ptr)
 Decompiles ip:port into ip and port.

const gchar * local_hostname (void)
gint str_chomp (gchar *str, gint len)
 Remove antepenultimate char of string if it is a "\r" followed by "\n".

gboolean is_absolute_path (const char *path)
 Check whether path is an absolute path.

gboolean is_directory (const gchar *path)
 Check whether path is a directory.

gboolean is_regular (const gchar *path)
 Check whether path points to a regular file.

gboolean is_symlink (const gchar *path)
 Check whether path is a symbolic link.

gchar size_scale (guint64 v, guint *q, guint *r, const gchar *s)
 Scales v so that quotient and reminder are both in the range "0..1023".

gchar norm_size_scale (guint64 v, guint *q, guint *r)
gchar kb_size_scale (guint64 v, guint *q, guint *r)
 Same as norm_size_scale() but assumes v is already divided by 1024.

const gchar * short_size (guint64 size)
const gchar * short_html_size (guint64 size)
 Like short_size() but with unbreakable space between the digits and unit.

const gchar * short_kb_size (guint64 size)
const gchar * compact_kb_size (guint32 size)
gchar * compact_value (gchar *buf, size_t size, guint64 v)
gchar * short_value (gchar *buf, size_t size, guint64 v)
const gchar * compact_size (guint64 size)
const gchar * compact_rate (guint64 rate)
const gchar * short_rate (guint64 rate)
gchar * short_time (gint t)
gchar * short_uptime (gint uptime)
 Alternate time formatter for uptime.

gchar * guid_hex_str (const gchar *guid)
gchar * data_hex_str (const gchar *data, size_t len)
gint hex2int (guchar c)
 Converts a hexadecimal char (0-9, A-F, a-f) to an integer.

gint dec2int (guchar c)
 Converts a decimal char (0-9) to an integer.

gint alnum2int (guchar c)
 Converts an alphanumeric char (0-9, A-Z, a-z) to an integer.

void hex2int_init (void)
 Initializes the lookup table for hex2int().

void dec2int_init (void)
 Initializes the lookup table for dec2int().

void alnum2int_init (void)
 Initializes the lookup table for alnum2int().

gboolean hex_to_guid (const gchar *hexguid, gchar *guid)
 Converts hexadecimal string into a GUID.

gchar * guid_base32_str (const gchar *guid)
 Converts GUID into its base32 representation, without the trailing padding.

gchar * base32_to_guid (const gchar *base32)
 Decode the base32 representation of a GUID.

gchar * sha1_base32 (const gchar *sha1)
 Convert binary SHA1 into a base32 string.

gchar * base32_sha1 (const gchar *base32)
 Convert base32 string into binary SHA1.

const gchar * timestamp_utc_to_string (time_t date)
 Convert time to an ISO 8601 timestamp, e.g.

size_t timestamp_to_string_buf (time_t date, gchar *dst, size_t size)
 Convert time to ISO 8601 date plus time, e.g.

const gchar * timestamp_to_string (time_t date)
 Convert time to ISO 8601 date plus time, e.g.

size_t time_locale_to_string_buf (time_t t, gchar *dst, size_t size)
 Convert time (without the date) to a human-readable string using the time representation of the current locale.

gint tm_diff (const struct tm *a, const struct tm *b)
 Compute the difference in seconds between two tm structs (a - b).

size_t timestamp_rfc822_to_string_buf (time_t date, gchar *buf, size_t size)
 Convert time to RFC-822 style date, into supplied string buffer.

const gchar * timestamp_rfc822_to_string (time_t date)
 Convert time to RFC-822 style date.

const gchar * timestamp_rfc822_to_string2 (time_t date)
 Same as date_to_rfc822_gchar(), to be able to use the two in the same printf() line.

size_t timestamp_rfc1123_to_string_buf (time_t date, gchar *buf, size_t size)
 Convert time to RFC-1123 style date, into supplied string buffer.

const gchar * timestamp_rfc1123_to_string (time_t date)
 Convert time to RFC-1123 style date.

guint32 next_pow2 (guint32 n)
gint highest_bit_set (guint32 n)
 Determine the highest bit set in `n', -1 if value was 0.

gfloat force_range (gfloat val, gfloat min, gfloat max)
 Enforce range boundaries on a given floating point number.

guint32 random_value (guint32 max)
void random_init (void)
 Initialize random number generator.

gboolean is_printable (const gchar *buf, gint len)
 Check whether buffer contains printable data, suitable for "%s" printing.

void dump_hex_header (FILE *out)
 Display header line for hex dumps.

void dump_hex (FILE *out, const gchar *title, gconstpointer data, gint b)
 Displays hex & ascii lines to the terminal (for debug) Displays the "title" then the characters in "s", # of bytes to print in "b".

void locale_strlower (gchar *dst, const gchar *src)
 Copies ``src'' to ``dst'', converting all upper-case characters to lower-case.

void ascii_strlower (gchar *dst, const gchar *src)
 Copies ``src'' to ``dst'', converting all ASCII upper-case characters to ASCII lower-case.

gint ascii_strcasecmp (const gchar *s1, const gchar *s2)
 Same as strcasecmp() but only case-insensitive for ASCII characters.

gint ascii_strncasecmp (const gchar *s1, const gchar *s2, size_t len)
 Same as strncasecmp() but only case-insensitive for ASCII characters.

gchar * ascii_strcasestr (const gchar *haystack, const gchar *needle)
 Same as strstr() but case-insensitive with respect to ASCII characters.

gint strcmp_delimit_full (const gchar *a, const gchar *b, const gchar *delimit, gboolean case_sensitive)
 Compare two strings up to the specified delimiters.

gint strcmp_delimit (const gchar *a, const gchar *b, const gchar *delimit)
 Compare two strings case-senstive up to the specified delimiters.

gint strcasecmp_delimit (const gchar *a, const gchar *b, const gchar *delimit)
 Compare two strings case-insensitive up to the specified delimiters.

void guid_random_fill (gchar *xuid)
 Generate a new random GUID within given `xuid'.

gchar * unique_filename (const gchar *path, const gchar *file, const gchar *ext)
 Determine unique filename for `file' in `path', with optional trailing extension `ext'.

gchar * hex_escape (const gchar *name, gboolean strict)
 Escape all non-printable chars into the hexadecimal "\xhh" form.

gboolean escape_control_char (guchar c)
 Checks whether the given character is a control character that should be escaped.

gchar * control_escape (const gchar *s)
 Escape all ASCII control chars (except into the hexadecimal "\xhh" form.

guint char_to_printf_escape (guchar c, gchar *esc)
const gchar * lazy_string_to_printf_escape (const gchar *src)
 Escapes a string so that it can be used careless with the POSIX printf tool.

gboolean string_to_ip_and_mask (const gchar *str, guint32 *ip, guint32 *netmask)
 Extracts the IP address into `ip' and the netmask into `netmask'.

gint do_stat (const gchar *path, struct stat *buf)
 Wrapper for the stat() system call.

gchar * make_pathname (const gchar *dir, const gchar *file)
 Create new pathname from the concatenation of the dirname and the basename of the file.

gchar * short_filename (gchar *fullname)
 Determine stripped down path, removing SRC_PREFIX if present.

gint create_directory (const gchar *dir)
 Creates the given directory including sub-directories if necessary.

gboolean filepath_exists (const gchar *dir, const gchar *file)
 Check whether file given by its dirname and its basename exists.

size_t uint32_to_string_buf (guint64 v, gchar *dst, size_t size)
size_t uint64_to_string_buf (guint64 v, gchar *dst, size_t size)
const gchar * uint32_to_string (guint32 v)
const gchar * uint64_to_string (guint64 v)
const gchar * uint64_to_string2 (guint64 v)
gint parse_major_minor (const gchar *src, gchar const **endptr, guint *major, guint *minor)
guint8 find_common_leading (guint32 ip1, guint32 ip2)
 Find amount of common leading bits between two IP addresses.

void ip_range_split (guint32 lower_ip, guint32 upper_ip, cidr_split_t cb, gpointer udata)
 Computes the set of CIDR ranges that make up the set of IPs between two boundary IPs, included.

signal_handler_t set_signal (gint signo, signal_handler_t handler)
 Installs a signal handler.

const gchar * html_escape_replacement (gchar c, size_t *len)
size_t html_escape (const gchar *src, gchar *dst, size_t dst_size)
 Copies the NUL-terminated string ``src'' to ``dst'' replacing all characters which are reserved in HTML with a replacement string.

gint canonize_path (gchar *dst, const gchar *path)
 Creates the canonical representation of a path.

guint compat_max_fd (void)
gint compat_mkdir (const gchar *path, mode_t mode)
glong compat_pagesize_intern (void)
size_t compat_pagesize (void)
gpointer compat_page_align (size_t size)
 Allocates a page-aligned memory chunk.

void compat_page_free (gpointer p, size_t size)
gboolean compat_is_superuser (void)
void misc_init (void)

Variables

const char hex_alphabet [] = "0123456789ABCDEF"
const char hex_alphabet_lower [] = "0123456789abcdef"
gint8 char2int_tabs [3][(size_t)(guchar)-1+1]
const gint8 * hex2int_tab = char2int_tabs[0]
const gint8 * dec2int_tab = char2int_tabs[1]
const gint8 * alnum2int_tab = char2int_tabs[2]
const gchar days [7][4]
const gchar months [12][4]
gint do_errno


Define Documentation

#define CHAR_IS_SAFE c,
strict   )     (isprint((c)) || (!(strict) && CHAR_IS_SPACE(c)))
 

#define CHAR_IS_SPACE  )     ((c) == ' ' || (c) == '\t' || (c) == '\n')
 

#define ESCAPE_CHAR   '\\'
 

#define GENERATE_PARSE_UINTX bits   )     GENERATE_PARSE_UNSIGNED(CAT2(parse_uint,bits), CAT2(guint,bits))
 

#define GENERATE_PARSE_UNSIGNED NAME,
TYPE   ) 
 

 
#define random  )     rand()
 

#define RANDOM_MASK   0xffffffff
 

#define RANDOM_MAXV   RAND_MAX
 

#define REPLACE  )     { *len = CONST_STRLEN(x); return (x); }
 

#define srandom  )     srand(x)
 

#define TM_YEAR_BASE   1900
 


Function Documentation

gint alnum2int guchar  c  ) 
 

Converts an alphanumeric char (0-9, A-Z, a-z) to an integer.

Passing a character which is not an alphanumeric ASCII character causes an assertion failure.

Parameters:
c the decimal ASCII character to convert.
Returns:
"0..36" for valid decimal ASCII characters.

void alnum2int_init void   )  [static]
 

Initializes the lookup table for alnum2int().

gint ascii_strcasecmp const gchar *  s1,
const gchar *  s2
 

Same as strcasecmp() but only case-insensitive for ASCII characters.

gchar* ascii_strcasestr const gchar *  haystack,
const gchar *  needle
 

Same as strstr() but case-insensitive with respect to ASCII characters.

void ascii_strlower gchar *  dst,
const gchar *  src
 

Copies ``src'' to ``dst'', converting all ASCII upper-case characters to ASCII lower-case.

``dst'' and ``src'' may be identical but must not overlap otherwise.

gint ascii_strncasecmp const gchar *  s1,
const gchar *  s2,
size_t  len
 

Same as strncasecmp() but only case-insensitive for ASCII characters.

gchar* base32_sha1 const gchar *  base32  ) 
 

Convert base32 string into binary SHA1.

Parameters:
base32 a buffer holding SHA1_BASE32_SIZE or more bytes.
Returns:
Returns pointer to static data or NULL if the input wasn't a validly base32 encoded SHA1.

gchar* base32_to_guid const gchar *  base32  ) 
 

Decode the base32 representation of a GUID.

Returns:
pointer to static data, or NULL if the input was not valid base32.

gint canonize_path gchar *  dst,
const gchar *  path
 

Creates the canonical representation of a path.

``dst'' and ``src'' may be identical but must not overlap otherwise.

Parameters:
dst the destination, must be sufficiently long.
path a NUL-terminated string representing the input path.
Returns:
zero on sucess, non-zero on failure.

TODO: Add overlap check.

guint char_to_printf_escape guchar  c,
gchar *  esc
[static]
 

const gchar* compact_kb_size guint32  size  ) 
 

Returns:
a number of Kbytes in a compact readable form

const gchar* compact_rate guint64  rate  ) 
 

const gchar* compact_size guint64  size  ) 
 

gchar* compact_value gchar *  buf,
size_t  size,
guint64  v
 

gboolean compat_is_superuser void   ) 
 

guint compat_max_fd void   ) 
 

gint compat_mkdir const gchar *  path,
mode_t  mode
 

gpointer compat_page_align size_t  size  ) 
 

Allocates a page-aligned memory chunk.

Parameters:
size The size in bytes to allocate; will be rounded to the pagesize.

void compat_page_free gpointer  p,
size_t  size
 

size_t compat_pagesize void   ) 
 

glong compat_pagesize_intern void   )  [static]
 

size_t concat_strings gchar *  dst,
size_t  size,
const gchar *  s,
... 
 

Concatenates a variable number of NUL-terminated strings into ``dst''.

The resulting string will be NUL-terminated unless ``size'' is zero. The returned value is the length of the resulting string if ``dst'' had been large enough. If the returned value is equal to or greater than ``size'' the string is truncated. If ``size'' is zero, ``dst'' may be NULL to calculate the resulting string length.

The list of strings must be terminated by a NULL pointer. The first list element may be NULL in which case zero is returned.

Parameters:
dst the destination buffer.
size the number of bytes ``dst'' can hold.
s the first source string or NULL.
Returns:
the sum of the lengths of all passed strings.

gchar* control_escape const gchar *  s  ) 
 

Escape all ASCII control chars (except into the hexadecimal "\xhh" form.

Returns:
new escaped string, or the original string if no escaping occurred.

gint create_directory const gchar *  dir  ) 
 

Creates the given directory including sub-directories if necessary.

The path must be absolute.

Parameters:
dir the pathname of the directory to create.
Returns:
On success, zero is returned. On failure, -1 is returned and errno indicates the reason.

Bug:
FIXME: This might fail with ``fancy'' file permissions. The directories should be created from leaf to root instead of vice-versa.

gchar* data_hex_str const gchar *  data,
size_t  len
 

Returns:
hexadecimal string representation of "small" binary buffer.
Note:
Buffer must be less than 40 chars, or only the first 40 chars are represented with a trailing "..." added to show it is incomplete.

gint dec2int guchar  c  ) 
 

Converts a decimal char (0-9) to an integer.

Passing a character which is not a decimal ASCII character causes an assertion failure.

Parameters:
c the decimal ASCII character to convert.
Returns:
"0..9" for valid decimal ASCII characters.

void dec2int_init void   )  [static]
 

Initializes the lookup table for dec2int().

gint do_stat const gchar *  path,
struct stat *  buf
 

Wrapper for the stat() system call.

void dump_hex FILE *  out,
const gchar *  title,
gconstpointer  data,
gint  b
 

Displays hex & ascii lines to the terminal (for debug) Displays the "title" then the characters in "s", # of bytes to print in "b".

void dump_hex_header FILE *  out  )  [inline, static]
 

Display header line for hex dumps.

gboolean escape_control_char guchar  c  )  [inline, static]
 

Checks whether the given character is a control character that should be escaped.

Returns:
TRUE if "c" should be escaped, FALSE otherwise.

gboolean file_exists const gchar *  f  ) 
 

Check for file existence.

gboolean filepath_exists const gchar *  dir,
const gchar *  file
 

Check whether file given by its dirname and its basename exists.

guint8 find_common_leading guint32  ip1,
guint32  ip2
[static]
 

Find amount of common leading bits between two IP addresses.

gfloat force_range gfloat  val,
gfloat  min,
gfloat  max
 

Enforce range boundaries on a given floating point number.

Parameters:
val The value to force within the range.
min The minimum value which val can be.
max The maximum value with val can be.
Returns:
The new value of val which will be between min and max.

gchar* guid_base32_str const gchar *  guid  ) 
 

Converts GUID into its base32 representation, without the trailing padding.

Returns:
pointer to static data.

gchar* guid_hex_str const gchar *  guid  ) 
 

Returns:
hexadecimal string representing given GUID.

void guid_random_fill gchar *  xuid  ) 
 

Generate a new random GUID within given `xuid'.

gint hex2int guchar  c  ) 
 

Converts a hexadecimal char (0-9, A-F, a-f) to an integer.

Passing a character which is not a hexadecimal ASCII character causes an assertion failure.

Parameters:
c the hexadecimal ASCII character to convert.
Returns:
"0..15" for valid hexadecimal ASCII characters.

void hex2int_init void   )  [static]
 

Initializes the lookup table for hex2int().

gchar* hex_escape const gchar *  name,
gboolean  strict
 

Escape all non-printable chars into the hexadecimal "\xhh" form.

Returns:
new escaped string, or the original string if no escaping occurred.

gboolean hex_to_guid const gchar *  hexguid,
gchar *  guid
 

Converts hexadecimal string into a GUID.

Parameters:
hexguid the hexadecimal representation to convert
guid the 16-byte array into which the decoded GUID is written to
Returns:
TRUE if OK.

gint highest_bit_set guint32  n  ) 
 

Determine the highest bit set in `n', -1 if value was 0.

const gchar* hostname_port_to_string const gchar *  hostname,
guint16  port
 

size_t html_escape const gchar *  src,
gchar *  dst,
size_t  dst_size
 

Copies the NUL-terminated string ``src'' to ``dst'' replacing all characters which are reserved in HTML with a replacement string.

Parameters:
src a NUL-terminated string.
dst the destination buffer, may be NULL if ``size'' is zero.
dst_size the size in bytes of the destination buffer.
Returns:
the length in bytes of resulting string assuming size was sufficiently large.

const gchar* html_escape_replacement gchar  c,
size_t *  len
[inline, static]
 

const gchar* ip_port_to_string guint32  ip,
guint16  port
 

void ip_range_split guint32  lower_ip,
guint32  upper_ip,
cidr_split_t  cb,
gpointer  udata
 

Computes the set of CIDR ranges that make up the set of IPs between two boundary IPs, included.

For instance, for the range 2.0.0.0 - 2.138.24.150, we have:

2.0.0.0/9, 2.128.0.0/13, 2.136.0.0/15, 2.138.0.0/20, 2.138.16.0/21, 2.138.24.0/25, 2.138.24.128/28, 2.138.24.144/30, 2.138.24.148, 2.138.24.149 and 2.138.24.150.

For each identified CIDR range, invoke the supplied callback, along with the trailing user-supplied `udata' pointer.

Parameters:
lower_ip the lower-bound IP
upper_ip the upper-bound IP
cb the callback, invoked as callback(ip, bits, udata)
udata the trailing parameter passed as-is to the callbacks

const gchar* ip_to_string guint32  ip  ) 
 

const gchar* ip_to_string2 guint32  ip  ) 
 

size_t ip_to_string_buf guint32  ip,
gchar *  buf,
size_t  size
 

const gchar* ipv6_to_string const guint8 *  ipv6  ) 
 

Prints the IPv6 address ``ipv6'' to a static buffer.

Parameters:
ipv6 the IPv6 address; must point to a buffer of 16 bytes.
Returns:
a pointer to a static buffer holding a NUL-terminated string representing the given IPv6 address.

size_t ipv6_to_string_buf const uint8_t *  ipv6,
gchar *  dst,
size_t  size
 

Prints the IPv6 address ``ipv6'' to ``dst''.

The string written to ``dst'' is always NUL-terminated unless ``size'' is zero. If ``size'' is too small, the string will be truncated.

Parameters:
dst the destination buffer; may be NULL iff ``size'' is zero.
ipv6 the IPv6 address; must point to a buffer of 16 bytes.
size the size of ``dst'' in bytes.
Returns:
The length of the resulting string assuming ``size'' is sufficient.

gboolean is_absolute_path const char *  path  ) 
 

Check whether path is an absolute path.

gboolean is_directory const gchar *  path  ) 
 

Check whether path is a directory.

gboolean is_printable const gchar *  buf,
gint  len
 

Check whether buffer contains printable data, suitable for "%s" printing.

If not, consider dump_hex().

gboolean is_regular const gchar *  path  ) 
 

Check whether path points to a regular file.

gchar* is_strcaseprefix const gchar *  str,
const gchar *  prefix
 

Checks whether ``prefix'' is a prefix of ``str'' performing an case-insensitive (ASCII only) check.

Maybe skip_caseprefix() would be a better name.

Parameters:
str a NUL-terminated string
prefix a NUL-terminated string
Returns:
NULL, if ``prefix'' is not a prefix of ``str''. Otherwise, a pointer to the first character in ``str'' after the prefix.

gchar* is_strprefix const gchar *  str,
const gchar *  prefix
 

Checks whether ``prefix'' is a prefix of ``str''.

Maybe skip_prefix() would be a better name.

Parameters:
str a NUL-terminated string
prefix a NUL-terminated string
Returns:
NULL, if ``prefix'' is not a prefix of ``str''. Otherwise, a pointer to the first character in ``str'' after the prefix.

gboolean is_symlink const gchar *  path  ) 
 

Check whether path is a symbolic link.

gchar kb_size_scale guint64  v,
guint *  q,
guint *  r
[inline, static]
 

Same as norm_size_scale() but assumes v is already divided by 1024.

const gchar* lazy_string_to_printf_escape const gchar *  src  ) 
 

Escapes a string so that it can be used careless with the POSIX printf tool.

Therefore it's absolutely paranoid and escapes everything but ASCII alphanumerics, dots, hyphen and underscores.

Note:
Hex sequences are always two digits long, so "\xAAA" is the same as "\xAA" "A". In C this not necessarily true and could be understood as a wide-char sequence.
Parameters:
src The string to escape.
Returns:
The escaped string. MUST NOT be freed.

const gchar* local_hostname void   ) 
 

Returns:
local host name, as pointer to static data.

void locale_strlower gchar *  dst,
const gchar *  src
 

Copies ``src'' to ``dst'', converting all upper-case characters to lower-case.

``dst'' and ``src'' may point to the same object. The conversion depends on the current locale.

gchar* make_pathname const gchar *  dir,
const gchar *  file
 

Create new pathname from the concatenation of the dirname and the basename of the file.

A directory separator is insert, unless "dir" already ends with one or "filename" starts with one.

Parameters:
dir The directory path.
file The filename.
Returns:
A newly allocated string.

void misc_init void   ) 
 

guint32 next_pow2 guint32  n  ) 
 

Returns:
the closest power of two greater or equal to `n'. next_pow2(0) and next_pow2(0x8.......) return 0.

gchar norm_size_scale guint64  v,
guint *  q,
guint *  r
[inline, static]
 

gboolean parse_ipv6_addr const gchar *  s,
guint8 *  dst,
const gchar **  endptr
 

Parses an IPv6 address.

Parameters:
s the string to parse.
dst will hold the IPv6 address on success; must point to 16 or more bytes .
endptr if not NULL, it will point to the next character after the parsed address on success. On failure it will point to the character which caused the failure.
Returns:
FALSE if ``s'' is not a valid IPv6 address; TRUE on success.

gint parse_major_minor const gchar *  src,
gchar const **  endptr,
guint *  major,
guint *  minor
 

size_t print_uint16_hex gchar *  dst,
guint16  v
[inline, static]
 

Prints the unsigned 16-bit value ``v'' in hexadecimal presentation as NUL-terminated string to ``dst'' and returns the length of the resulting string.

``dst'' must point to a buffer of 5 or more bytes.

Parameters:
dst the destination buffer.
v the 16-bit value.
Returns:
the length of resulting string.

void random_init void   ) 
 

Initialize random number generator.

guint32 random_value guint32  max  ) 
 

Returns:
random value between (0..max).

RCSID "$Id:misc.  c,
v 1.114 2005/12/27 13:10:50 cbiere Exp $" 
 

signal_handler_t set_signal gint  signo,
signal_handler_t  handler
 

Installs a signal handler.

The signal handler is not reset to the default handler after delivery. If the signal is SIGALRM, the handler is installed so that interrupted system calls fail with EINTR. Handlers for other all signals are installed so that interrupted system calls are restarted instead.

Parameters:
signo the signal number.
handler the signal handler to install.
Returns:
the previous signal handler or SIG_ERR on failure.

gchar* sha1_base32 const gchar *  sha1  ) 
 

Convert binary SHA1 into a base32 string.

Returns:
pointer to static data.

gchar* short_filename gchar *  fullname  ) 
 

Determine stripped down path, removing SRC_PREFIX if present.

Returns:
pointer within supplied string.

const gchar* short_html_size guint64  size  ) 
 

Like short_size() but with unbreakable space between the digits and unit.

const gchar* short_kb_size guint64  size  ) 
 

const gchar* short_rate guint64  rate  ) 
 

const gchar* short_size guint64  size  ) 
 

gchar* short_time gint  t  ) 
 

Returns:
time spent in seconds in a consise short readable form

gchar* short_uptime gint  uptime  ) 
 

Alternate time formatter for uptime.

gchar* short_value gchar *  buf,
size_t  size,
guint64  v
 

gchar size_scale guint64  v,
guint *  q,
guint *  r,
const gchar *  s
[inline, static]
 

Scales v so that quotient and reminder are both in the range "0..1023".

Parameters:
v no document.
q pointer to a guint; will hold the quotient.
r pointer to a guint; will hold the reminder.
s a string holding the scale prefixes; must be sufficiently long.
Returns:
the appropriate prefix character from "s".

gint str_chomp gchar *  str,
gint  len
 

Remove antepenultimate char of string if it is a "\r" followed by "\n".

Remove final char of string if it is a "\n" or "\r". If len is 0, compute it.

Returns:
new string length.

gint strcasecmp_delimit const gchar *  a,
const gchar *  b,
const gchar *  delimit
 

Compare two strings case-insensitive up to the specified delimiters.

gint strcmp_delimit const gchar *  a,
const gchar *  b,
const gchar *  delimit
 

Compare two strings case-senstive up to the specified delimiters.

gint strcmp_delimit_full const gchar *  a,
const gchar *  b,
const gchar *  delimit,
gboolean  case_sensitive
[static]
 

Compare two strings up to the specified delimiters.

guint32 string_to_ip const gchar *  s  ) 
 

Returns:
0 if ``s'' is not a valid IPv4 address. Otherwise, the parsed IPv4 address in host byte order.

gboolean string_to_ip_and_mask const gchar *  str,
guint32 *  ip,
guint32 *  netmask
 

Extracts the IP address into `ip' and the netmask into `netmask'.

Returns:
whether the supplied string represents a valid ip/mask combination.
Accepted forms: "a.b.c.d" implies /32 "a.b.c.d/e" whereas e [1..32] "a.b.c.d/w.x.y.z"

If the IP address or the netmask is zero, the function will return FALSE.

gboolean string_to_ip_port const gchar *  s,
guint32 *  ip_ptr,
guint16 *  port_ptr
 

Decompiles ip:port into ip and port.

Leading spaces are ignored.

Returns:
TRUE if it parsed correctly, FALSE otherwise.

gboolean string_to_ip_strict const gchar *  s,
guint32 *  addr,
const gchar **  endptr
 

A strict string to IP address conversion; when other stuff from misc.[ch] is not sufficient.

Returns:
TRUE if ``s'' pointed to a string representation of an IPv4 address, otherwise FALSE.
If successful, ``*addr'' will be set to the IPv4 address in NATIVE byte order and ``*endptr'' will point to the character after the IPv4 address. ``addr'' and ``endptr'' may be NULL.

size_t strlcat gchar *  dst,
const gchar *  src,
size_t  dst_size
 

size_t strlcpy gchar *  dst,
const gchar *  src,
size_t  dst_size
 

Needs to be defined if we are not using Glib 2.

size_t time_locale_to_string_buf time_t  t,
gchar *  dst,
size_t  size
 

Convert time (without the date) to a human-readable string using the time representation of the current locale.

Parameters:
t time to convert.
dst buffer to hold the resulting string.
size the size of the dst buffer.
Returns:
length of the created string.

const gchar* timestamp_rfc1123_to_string time_t  date  ) 
 

Convert time to RFC-1123 style date.

Returns:
pointer to static data.

size_t timestamp_rfc1123_to_string_buf time_t  date,
gchar *  buf,
size_t  size
[static]
 

Convert time to RFC-1123 style date, into supplied string buffer.

Parameters:
date The timestamp.
buf The destination buffer to hold the resulting string. Must be greater than zero.
size The size of of "buf".
Returns:
The length of the created string.

const gchar* timestamp_rfc822_to_string time_t  date  ) 
 

Convert time to RFC-822 style date.

Returns:
pointer to static data.

const gchar* timestamp_rfc822_to_string2 time_t  date  ) 
 

Same as date_to_rfc822_gchar(), to be able to use the two in the same printf() line.

size_t timestamp_rfc822_to_string_buf time_t  date,
gchar *  buf,
size_t  size
[static]
 

Convert time to RFC-822 style date, into supplied string buffer.

Parameters:
date The timestamp.
buf The destination buffer to hold the resulting string. Must be greater than zero.
size The size of of "buf".
Returns:
The length of the created string.

const gchar* timestamp_to_string time_t  date  ) 
 

Convert time to ISO 8601 date plus time, e.g.

"2005-11-10 20:21:57".

Returns:
pointer to static data.

size_t timestamp_to_string_buf time_t  date,
gchar *  dst,
size_t  size
 

Convert time to ISO 8601 date plus time, e.g.

"2002-06-09 14:54:42".

Returns:
The length of the created string.

const gchar* timestamp_utc_to_string time_t  date  ) 
 

Convert time to an ISO 8601 timestamp, e.g.

"2002-06-09T14:54:42Z".

Returns:
pointer to static data.

gint tm_diff const struct tm *  a,
const struct tm *  b
[static]
 

Compute the difference in seconds between two tm structs (a - b).

Comes from glibc-2.2.5.

const gchar* uint32_to_string guint32  v  ) 
 

size_t uint32_to_string_buf guint64  v,
gchar *  dst,
size_t  size
 

const gchar* uint64_to_string guint64  v  ) 
 

const gchar* uint64_to_string2 guint64  v  ) 
 

size_t uint64_to_string_buf guint64  v,
gchar *  dst,
size_t  size
 

gchar* unique_filename const gchar *  path,
const gchar *  file,
const gchar *  ext
 

Determine unique filename for `file' in `path', with optional trailing extension `ext'.

If no `ext' is wanted, one must supply an empty string.

Returns:
the chosen unique complete filename as a pointer which must be freed.

size_t w_concat_strings gchar **  dst_ptr,
const gchar *  first,
... 
 

Concatenates a variable number of NUL-terminated strings into buffer which will be allocated using walloc().

The list of strings must be terminated by a NULL pointer. The first list element may be NULL in which case 1 is returned.

Parameters:
dst_ptr if not NULL, it will point to the allocated buffer.
first the first source string or NULL.
Returns:
The sum of the lengths of all passed strings plus 1 for the the trailing NUL. Use this as size argument for wfree() to release the allocated buffer.


Variable Documentation

const gint8* alnum2int_tab = char2int_tabs[2]
 

gint8 char2int_tabs[3][(size_t) (guchar) -1 + 1] [static]
 

const gchar days[7][4] [static]
 

Initial value:

    { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

const gint8* dec2int_tab = char2int_tabs[1]
 

gint do_errno
 

const gint8* hex2int_tab = char2int_tabs[0]
 

const char hex_alphabet[] = "0123456789ABCDEF" [static]
 

const char hex_alphabet_lower[] = "0123456789abcdef" [static]
 

const gchar months[12][4] [static]
 

Initial value:

 {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
}


Generated on Sun Feb 12 10:50:04 2006 for Gtk-Gnutella by doxygen 1.3.6