
/**********************************************************************

  Copyright 2019 Andrey V.Kosteltsev

  Licensed under the Radix.pro License, Version 1.0 (the "License");
  you may not use this file  except  in compliance with the License.
  You may obtain a copy of the License at

     https://radix.pro/licenses/LICENSE-1.0-en_US.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.

 **********************************************************************/

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <unistd.h>

#include <msglog.h>


char *xstrdup( const char *str )
{
  char   *ret = (char *)NULL;
  size_t  len;

  if( !str ) return ret;

  len = strlen( str ) + 1;

  ret = (char *)malloc( len );
  if( !ret )
    FATAL_ERROR( "Out of memory, strdup failed (tried to allocate %lu bytes)", (unsigned long)len );

  ret[len-1] = '\0';
  ret = strncpy( ret, str, len-1 );
  return ret;
}

void *xmalloc( size_t size )
{
  void *ret = NULL;

  ret = malloc( size );
  if( !ret )
  {
    FATAL_ERROR( "Out of memory, malloc failed (tried to allocate %lu bytes)", (unsigned long)size );
  }
  memset( ret, 0, size );
  return ret;
}

void *xrealloc( void *ptr, size_t size )
{
  void *ret = NULL;

  ret = realloc( ptr, size );
  if( !ret && !size )
    ret = realloc( ptr, 1 );
  if( !ret )
    FATAL_ERROR( "Out of memory, realloc failed" );
  return ret;
}
