1 /* 2 compat: Some compatibility functions (basic memory and string stuff) 3 4 The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX. 5 So anything possibly somewhat advanced should be considered to be put here, with proper #ifdef;-) 6 7 copyright 2007-2016 by the mpg123 project - free software under the terms of the LGPL 2.1 8 see COPYING and AUTHORS files in distribution or http://mpg123.org 9 initially written by Thomas Orgis, Windows Unicode stuff by JonY. 10 */ 11 12 #include "compat.h" 13 #include "debug.h" 14 15 /* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */ 16 void *safe_realloc(void *ptr, size_t size) 17 { 18 if(ptr == NULL) return malloc(size); 19 else return realloc(ptr, size); 20 } 21 22 #ifndef HAVE_STRERROR 23 const char *strerror(int errnum) 24 { 25 extern int sys_nerr; 26 extern char *sys_errlist[]; 27 28 return (errnum < sys_nerr) ? sys_errlist[errnum] : ""; 29 } 30 #endif 31 32 char* compat_strdup(const char *src) 33 { 34 char *dest = NULL; 35 if(src) 36 { 37 size_t len; 38 len = strlen(src)+1; 39 if((dest = malloc(len))) 40 memcpy(dest, src, len); 41 } 42 return dest; 43 } 44