Blame src/compat/compat_str.c

Packit c32a2d
/*
Packit c32a2d
	compat: Some compatibility functions (basic memory and string stuff)
Packit c32a2d
Packit c32a2d
	The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX.
Packit c32a2d
	So anything possibly somewhat advanced should be considered to be put here, with proper #ifdef;-)
Packit c32a2d
Packit c32a2d
	copyright 2007-2016 by the mpg123 project - free software under the terms of the LGPL 2.1
Packit c32a2d
	see COPYING and AUTHORS files in distribution or http://mpg123.org
Packit c32a2d
	initially written by Thomas Orgis, Windows Unicode stuff by JonY.
Packit c32a2d
*/
Packit c32a2d
Packit c32a2d
#include "compat.h"
Packit c32a2d
#include "debug.h"
Packit c32a2d
Packit c32a2d
/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
Packit c32a2d
void *safe_realloc(void *ptr, size_t size)
Packit c32a2d
{
Packit c32a2d
	if(ptr == NULL) return malloc(size);
Packit c32a2d
	else return realloc(ptr, size);
Packit c32a2d
}
Packit c32a2d
Packit c32a2d
#ifndef HAVE_STRERROR
Packit c32a2d
const char *strerror(int errnum)
Packit c32a2d
{
Packit c32a2d
	extern int sys_nerr;
Packit c32a2d
	extern char *sys_errlist[];
Packit c32a2d
Packit c32a2d
	return (errnum < sys_nerr) ?  sys_errlist[errnum]  :  "";
Packit c32a2d
}
Packit c32a2d
#endif
Packit c32a2d
Packit c32a2d
char* compat_strdup(const char *src)
Packit c32a2d
{
Packit c32a2d
	char *dest = NULL;
Packit c32a2d
	if(src)
Packit c32a2d
	{
Packit c32a2d
		size_t len;
Packit c32a2d
		len = strlen(src)+1;
Packit c32a2d
		if((dest = malloc(len)))
Packit c32a2d
			memcpy(dest, src, len);
Packit c32a2d
	}
Packit c32a2d
	return dest;
Packit c32a2d
}