---
name: "libwget-utils"
section: "3"
platform: "linux"
package: "wget2-dev"
version: "2.2.0+ds-1+deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/libwget-utils"
---

# libwget-utils(3)

General utility functions

## NAME

libwget-utils - General utility functions

## SYNOPSIS

### Functions

size\_t **wget\_strlcpy** (char \*dst, const char \*src, size\_t size)\
 ssize\_t **wget\_strscpy** (char \*dst, const char \*src, size\_t size)\
 int **wget\_strcmp** (const char \*s1, const char \*s2)\
 int **wget\_strcasecmp** (const char \*s1, const char \*s2)\
 int **wget\_strcasecmp\_ascii** (const char \*s1, const char \*s2)\
 int **wget\_strncasecmp\_ascii** (const char \*s1, const char \*s2, size\_t n)\
 char \* **wget\_strtolower** (char \*s)\
 int **wget\_strncmp** (const char \*s1, const char \*s2, size\_t n)\
 int **wget\_strncasecmp** (const char \*s1, const char \*s2, size\_t n)\
 void **wget\_memtohex** (const unsigned char \*src, size\_t src\_len, char \*dst, size\_t dst\_size)\
 void **wget\_millisleep** (int ms)\
 long long **wget\_get\_timemillis** (void)\
 int **wget\_percent\_unescape** (char \*src)\
 int **wget\_match\_tail** (const char \*s, const char \*tail)\
 int **wget\_match\_tail\_nocase** (const char \*s, const char \*tail)\
 char \* **wget\_strnglob** (const char \*str, size\_t n, int flags)\
 char \* **wget\_human\_readable** (char \*buf, size\_t bufsize, uint64\_t n)\
 int **wget\_get\_screen\_size** (int \*width, int \*height)\

## Detailed Description

This is a collections of short routines that are used with libwget and/or Wget code. They may be useful to other developers that is why they are exported.

## Function Documentation

### size\_t wget\_strlcpy (char \* dst, const char \* src, size\_t size)

**Parameters**

*dst* Output string buffer \
 *src* Input string \
 *size* Size of `dst`

**Returns**

Length of `src`

Copy string `src` into `dst` with overflow checking.

This is the same as snprintf(dst,size,'%s',src) but faster and more elegant.

If `src` is NULL, the return value is 0 and nothing is written. If \\ dst is NULL, the return value is the length of `src` and nothing is written.

### ssize\_t wget\_strscpy (char \* dst, const char \* src, size\_t size)

**Parameters**

*dst* Output string buffer \
 *src* Input string \
 *size* Size of `dst`

**Returns**

Number of copied bytes (excluding trailing 0) or -1 when `src` doesn't fit into `dst`

Copy string `src` into `dst` with overflow checking.

If either `dst` is NULL or `size` is 0, the return value is -1 and nothing is written. If `src` is NULL and size is 0, the return value is -1. If `src` is NULL and size is \>0, the return value is 0 and `dst` is an empty string.

Else the return value is the number of bytes copied to `dst` excluding the terminating 0.

### int wget\_strcmp (const char \* s1, const char \* s2)

**Parameters**

*s1* String \
 *s2* String

**Returns**

0 if both `s1` and `s2` are NULL\
\
  \-1 if `s1` is NULL and `s2` is not NULL\
\
  1 if `s1` is not NULL and `s2` is NULL else it returns strcmp(`s1`, `s2`)

This functions compares `s1` and `s2` in the same way as strcmp() does, except that it also handles NULL values.

### int wget\_strcasecmp (const char \* s1, const char \* s2)

**Parameters**

*s1* String \
 *s2* String

**Returns**

0 if both `s1` and `s2` are NULL\
\
  \-1 if `s1` is NULL and `s2` is not NULL\
\
  1 if `s1` is not NULL and `s2` is NULL else it returns strcasecmp(`s1`, `s2`)

This functions compares `s1` and `s2` in the same way as strcasecmp() does, except that it also handles NULL values.

### int wget\_strcasecmp\_ascii (const char \* s1, const char \* s2)

**Parameters**

*s1* String \
 *s2* String

**Returns**

0 if both `s1` and `s2` are the same disregarding case for ASCII letters a-z\
\
  0 if both `s1` and `s2` are NULL\
\
  \<0 if `s1` is NULL and `s2` is not NULL or `s1` is smaller than `s2` \
\
  \>0 if `s2` is NULL and `s1` is not NULL or `s1` is greater than `s2`.

This functions compares `s1` and `s2` as ASCII strings, case insensitive. It also accepts NULL values.

### int wget\_strncasecmp\_ascii (const char \* s1, const char \* s2, size\_t n)

**Parameters**

*s1* String \
 *s2* String \
 *n* Max. number of chars to compare

**Returns**

0 if both `s1` and `s2` are the same disregarding case for ASCII letters a-z\
\
  0 if both `s1` and `s2` are NULL\
\
  \<0 if `s1` is NULL and `s2` is not NULL or `s1` is smaller than `s2` \
\
  \>0 if `s2` is NULL and `s1` is not NULL or `s1` is greater than `s2`.

This functions compares `s1` and `s2` as ASCII strings, case insensitive, up to a max number of `n` chars. It also accepts NULL values.

### char \* wget\_strtolower (char \* s)

**Parameters**

*s* String to convert

**Returns**

Value of s

Converts ASCII string `s` to lowercase in place.

### int wget\_strncmp (const char \* s1, const char \* s2, size\_t n)

**Parameters**

*s1* String \
 *s2* String \
 *n* Max. number of chars to compare

**Returns**

0 if both `s1` and `s2` are the same or if both `s1` and `s2` are NULL\
\
  \<0 if `s1` is NULL and `s2` is not NULL or `s1` is smaller than `s2` \
\
  \>0 if `s2` is NULL and `s1` is not NULL or `s1` is greater than `s2`.

This functions compares `s1` and `s2` in the same way as strncmp() does, except that it also handles NULL values.

### int wget\_strncasecmp (const char \* s1, const char \* s2, size\_t n)

**Parameters**

*s1* String \
 *s2* String \
 *n* Max. number of chars to compare

**Returns**

0 if both `s1` and `s2` are the same disregarding case or if both `s1` and `s2` are NULL\
\
  \<0 if `s1` is NULL and `s2` is not NULL or `s1` is smaller than `s2` \
\
  \>0 if `s2` is NULL and `s1` is not NULL or `s1` is greater than `s2`.

This functions compares `s1` and `s2` in the same way as strncasecmp() does, except that it also handles NULL values.

### void wget\_memtohex (const unsigned char \* src, size\_t src\_len, char \* dst, size\_t dst\_size)

**Parameters**

*src* Pointer to input buffer \
 *src\_len* Number of bytes to encode \
 *dst* Buffer to hold the encoded string \
 *dst\_size* Size of `dst` in bytes

Encodes a number of bytes into a lowercase hexadecimal C string.

### void wget\_millisleep (int ms)

**Parameters**

*ms* Number of milliseconds to sleep

Pause for `ms` milliseconds.

### long long wget\_get\_timemillis (void)

Return the current milliseconds since the epoch.

### int wget\_percent\_unescape (char \* src)

**Parameters**

*src* String to unescape

**Returns**

0 if the string did not change\
\
  1 if unescaping took place

Does an inline percent unescape. Each occurrence of xx (x = hex digit) will converted into it's byte representation.

### int wget\_match\_tail (const char \* s, const char \* tail)

**Parameters**

*s* String \
 *tail* String

**Returns**

1 if `tail` matches the end of `s`, 0 if not

Checks if `tail` matches the end of the string `s`.

### int wget\_match\_tail\_nocase (const char \* s, const char \* tail)

**Parameters**

*s* String \
 *tail* String

**Returns**

1 if `tail` matches the end of `s`, 0 if not

Checks if `tail` matches the end of the string `s`, disregarding the case, ASCII only.

### char \* wget\_strnglob (const char \* str, size\_t n, int flags)

**Parameters**

*str* String to run glob() against \
 *n* Length of string \
 *flags* Flags to pass to glob()

**Returns**

Expanded string after running glob

Finds a pathname by running [glob(3)](/glob/3.md) on the pattern in the first `n` bytes of `globstr`. Returns a newly allocated string with the first `n` bytes replaced with the matching pattern obtained via [glob(3)](/glob/3.md) if one was found. Otherwise it returns NULL.

### char \* wget\_human\_readable (char \* buf, size\_t bufsize, uint64\_t n)

**Parameters**

*buf* Result buffer \
 *bufsize* Size of /p buf \
 *n* Number to convert

**Returns**

Pointer to printable representation of `n`

Returns a human readable representation of `n`. `n`, a byte quantity, is converted to a human-readable abbreviated form a la sizes printed by \`ls -lh'. The result is written into the provided buffer.

Unlike \`with\_thousand\_seps', this approximates to the nearest unit. Quoting GNU libit: 'Most people visually process strings of 3-4 digits effectively, but longer strings of digits are more prone to misinterpretation.  Hence, converting to an abbreviated form usually improves readability.'

This intentionally uses kilobyte (KB), megabyte (MB), etc. in their original computer-related meaning of 'powers of 1024'. We don't use the '\*bibyte' names invented in 1998, and seldom used in practice. Wikipedia's entry on 'binary prefix' discusses this in some detail.

### int wget\_get\_screen\_size (int \* width, int \* height)

**Parameters**

*width* Number of columns in terminal \
 *height* Number of rows in terminal

**Returns**

Upon successful completion, `wget_get_screen_size` will return 0, and the values of `width` and `height` will be set accordingly. If an error was encountered, the function will return -1 without touching the values of `width` and `height`.

Get the size of the terminal to which the output is currently printed (stderr). This function accepts two int pointers and will set their values to the width and height of the active terminal in number of columns. If either of the parameter is NULL, its value will not be set by the function.

## Author

Generated automatically by Doxygen for wget2 from the source code.
