cutils header

The documentation of QEMU’s qemu/cutils.h header.

void pstrcpy(char * buf, int buf_size, const char * str)

Parameters

char * buf
buffer to copy string into
int buf_size
size of buf in bytes
const char * str
string to copy

Description

Copy str into buf, including the trailing NUL, but do not write more than buf_size bytes. The resulting buffer is always NUL terminated (even if the source string was too long). If buf_size is zero or negative then no bytes are copied.

This function is similar to strncpy(), but avoids two of that function’s problems:

  • if str fits in the buffer, pstrcpy() does not zero-fill the remaining space at the end of buf
  • if str is too long, pstrcpy() will copy the first buf_size-1 bytes and then add a NUL
void strpadcpy(char * buf, int buf_size, const char * str, char pad)

Parameters

char * buf
buffer to copy string into
int buf_size
size of buf in bytes
const char * str
string to copy
char pad
character to pad the remainder of buf with

Description

Copy str into buf (but not its trailing NUL!), and then pad the rest of the buffer with the pad character. If str is too large for the buffer then it is truncated, so that buf contains the first buf_size characters of str, with no terminator.

char * pstrcat(char * buf, int buf_size, const char * s)

Parameters

char * buf
buffer containing existing string
int buf_size
size of buf in bytes
const char * s
string to concatenate to buf

Description

Append a copy of s to the string already in buf, but do not allow the buffer to overflow. If the existing contents of buf plus str would total more than buf_size bytes, then write as much of str as will fit followed by a NUL terminator.

buf must already contain a NUL-terminated string, or the behaviour is undefined.

Return

buf.

int strstart(const char * str, const char * val, const char ** ptr)

Parameters

const char * str
string to test
const char * val
prefix string to look for
const char ** ptr
NULL, or pointer to be written to indicate start of the remainder of the string

Description

Test whether str starts with the prefix val. If it does (including the degenerate case where str and val are equal) then return true. If ptr is not NULL then a pointer to the first character following the prefix is written to it. If val is not a prefix of str then return false (and ptr is not written to).

Return

true if str starts with prefix val, false otherwise.

int stristart(const char * str, const char * val, const char ** ptr)

Parameters

const char * str
string to test
const char * val
prefix string to look for
const char ** ptr
NULL, or pointer to be written to indicate start of the remainder of the string

Description

Test whether str starts with the case-insensitive prefix val. This function behaves identically to strstart(), except that the comparison is made after calling qemu_toupper() on each pair of characters.

Return

true if str starts with case-insensitive prefix val,
false otherwise.
int qemu_strnlen(const char * s, int max_len)

Parameters

const char * s
string
int max_len
maximum number of bytes in s to scan

Description

Return the length of the string s, like strlen(), but do not examine more than max_len bytes of the memory pointed to by s. If no NUL terminator is found within max_len bytes, then return max_len instead.

This function has the same behaviour as the POSIX strnlen() function.

Return

length of s in bytes, or max_len, whichever is smaller.

char * qemu_strsep(char ** input, const char * delim)

Parameters

char ** input
pointer to string to parse
const char * delim
string containing delimiter characters to search for

Description

Locate the first occurrence of any character in delim within the string referenced by input, and replace it with a NUL. The location of the next character after the delimiter character is stored into input. If the end of the string was reached without finding a delimiter character, then NULL is stored into input. If input points to a NULL pointer on entry, return NULL. The return value is always the original value of *input (and so now points to a NUL-terminated string corresponding to the part of the input up to the first delimiter).

This function has the same behaviour as the BSD strsep() function.

Return

the pointer originally in input.

int qemu_pstrcmp0(const char ** str1, const char ** str2)

Parameters

const char ** str1
a non-NULL pointer to a C string (*str1 can be NULL)
const char ** str2
a non-NULL pointer to a C string (*str2 can be NULL)

Description

Compares *str1 and *str2 with g_strcmp0().

Return

an integer less than, equal to, or greater than zero, if *str1 is <, == or > than *str2.