5 Compatibility Functions

Some systems provide useful functions, but you cannot use these without worrying about your program not being portable to other systems. That is why Mu provides the functions described below. On systems where these functions are provided, Mu will use the provided functions. This is because, in many cases, the functions are hard optimized, and the alternatives provided by Mu will not be as efficient.

The functions described in this chapter are declared in mu/compat.h.

The functions described in this chapter are the only symbols not beginning with ‘mu_’ (or ‘MU_’ for macros). The reason for this is so that you can simply include mu/compat.h in source files where you use these functions, and it automatically becomes portable (as long as it doesn’t have any other portability issues).

You don’t have to worry about defining any feature test macros, such as _GNU_SOURCE, although it doesn’t hurt to do so. Just make sure you include mu/compat.h after any system headers.

All of the functions below set the global variable errno on failure. See (libc)Error Reporting.

Function: int asprintf (char **ptr, const char *format, …)

This function returns a formatted string in *ptr based on the printf-style format string format. *ptr is dynamically allocated and must be passed to free. The return value is the number of characters in *ptr on success, or -1 on error. See (libc)Dynamic Output.

Function: int vasprintf (char **ptr, const char *format, va_list ap)

Like asprintf (see above), but takes a va_list argument, ap, instead of variable arguments. See (libc)Variable Arguments Output.

Function: char * strchrnul (const char *string, int c)

Returns a pointer to the first occurrence of c (converted to a char) in string, or a pointer to the terminating null byte if c does not occur in string. See (libc)Search Functions

Function: void * reallocarray (void *ptr, size_t count, eltsize)

Equivalent to realloc(ptr, count * eltsize), except that this function will fail safely and reliably in the event that count * eltsize overflows. See (libc)Changing Block Size.