Tools to make secret sharing easier.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

135 lines
2.6 KiB

/*
* xmem - Incenp.org Notch Library: die-on-error memory functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <xmem.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#ifdef ENABLE_NLS
#include <libintl.h>
#define n(a,b,n) ngettext(a,b,n)
#else
#define n(a,b,n) ((n) > 1 ? (b) : (a))
#endif
void (*xmem_error)(size_t) = NULL;
#define mem_error(a) \
do { \
if ( xmem_error ) \
(*xmem_error)((a)); \
err(EXIT_FAILURE, n("Cannot allocate %lu byte", \
"Cannot allocate %lu bytes", (a)), \
(a)); \
} while ( 0 )
void *
xmalloc(size_t s)
{
void *p;
if ( ! (p = malloc(s)) && s )
mem_error(s);
return p;
}
void *
xcalloc(size_t n, size_t s)
{
void *p;
if ( ! (p = calloc(n, s)) && n && s )
mem_error(n * s);
return p;
}
void *
xrealloc(void *p, size_t s)
{
void *np;
if ( ! (np = realloc(p, s)) && s )
mem_error(s);
return np;
}
char *
xstrdup(const char *s)
{
char *dup;
size_t len;
len = strlen(s);
if ( ! (dup = malloc(len)) )
mem_error(len);
strcpy(dup, s);
return dup;
}
char *
xstrndup(const char *s, size_t n)
{
char *dup;
if ( ! (dup = malloc(n)) )
mem_error(n);
strncpy(dup, s, n);
return dup;
}
int
xasprintf(char **s, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = xvasprintf(s, fmt, ap);
va_end(ap);
return ret;
}
int
xvasprintf(char **s, const char *fmt, va_list ap)
{
int n;
va_list aq;
n = vsnprintf(NULL, 0, fmt, ap) + 1;
if ( ! (*s = malloc(n)) )
mem_error((size_t) n);
va_copy(ap, aq);
n = vsnprintf(*s, n, fmt, aq);
va_end(aq);
return n;
}