New CS-Library String Handling Routines

cs_strlcpy, cs_strlcat, and cs_snprintf are the three new CS-Library string handling routines.

cs_strlcpy

Safe string copy function. Copies at most target_size-1 characters from source_str to target_str, truncating if necessary. The result is always a null terminated string except when source_str or target_str are NULL, or target_size is 0.

Syntax

CS_RETCODE cs_strlcpy(target_str, source_str, target_size)

 CS_CHAR           *target_str;
 CS_CHAR           *source_str;
 CS_INT                *target_size;

Parameters

  • target_str

    The target string where source string is to be copied.

  • source_str

    The source string to be copied.

  • target_size

    Size of the target string

Return value

  • 0 if source_str is NULL, target_str is NULL, or target_size is 0.

  • target_size in case of an overflow.

  • strlen(source_str) in all other cases.

cs_strlcat

Safe string concatenation function. Appends at most target_size - strlen(target_str) - 1 characters of source_str to target_str. The result is always a null terminated string, except when source_str or target_str are NULL, or target_size is 0, or the string pointed to by target_str is longer than target_size bytes.

Syntax

CS_RETCODE cs_strlcat(target_str, source_str, target_size)

 CS_CHAR           *target_str;
 CS_CHAR           *source_str;
 CS_INT                *target_size;

Parameters

  • target_str

    The target string where source string is to be appended.

  • source_str

    The source string to be appended.

  • target_size

    Size of the target string

Return value

  • 0 if source_str is NULL, target_str is NULL, or target_size is 0

  • target_size in case of an overflow

  • strlen(target_str) + strlen(source_str) in all other cases

cs_snprintf

A common snprintf like function for all platforms, providing formatted output conversion. The result is always a null terminated string.

Syntax

void cs_snprintf(char *str, size_t size, const char *format, ...)

Parameters

  • str

    String into which the output is written to.

  • size

    Maximum number of bytes to write.

  • format

    Character string composed of zero or more conversion directives.

Return value

None