|
Packit |
fd8b60 |
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
Packit |
fd8b60 |
/* clients/ksu/xmalloc.c - Exit-on-failure allocation wrappers */
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright 1999 by the Massachusetts Institute of Technology.
|
|
Packit |
fd8b60 |
* All Rights Reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Export of this software from the United States of America may
|
|
Packit |
fd8b60 |
* require a specific license from the United States Government.
|
|
Packit |
fd8b60 |
* It is the responsibility of any person or organization contemplating
|
|
Packit |
fd8b60 |
* export to obtain such a license before exporting.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
|
|
Packit |
fd8b60 |
* distribute this software and its documentation for any purpose and
|
|
Packit |
fd8b60 |
* without fee is hereby granted, provided that the above copyright
|
|
Packit |
fd8b60 |
* notice appear in all copies and that both that copyright notice and
|
|
Packit |
fd8b60 |
* this permission notice appear in supporting documentation, and that
|
|
Packit |
fd8b60 |
* the name of M.I.T. not be used in advertising or publicity pertaining
|
|
Packit |
fd8b60 |
* to distribution of the software without specific, written prior
|
|
Packit |
fd8b60 |
* permission. Furthermore if you modify this software you must label
|
|
Packit |
fd8b60 |
* your software as modified software and not distribute it in such a
|
|
Packit |
fd8b60 |
* fashion that it might be confused with the original M.I.T. software.
|
|
Packit |
fd8b60 |
* M.I.T. makes no representations about the suitability of
|
|
Packit |
fd8b60 |
* this software for any purpose. It is provided "as is" without express
|
|
Packit |
fd8b60 |
* or implied warranty.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#include "k5-platform.h"
|
|
Packit |
fd8b60 |
#include "ksu.h"
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
void *xmalloc (size_t sz)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
void *ret = malloc (sz);
|
|
Packit |
fd8b60 |
if (ret == 0 && sz != 0) {
|
|
Packit |
fd8b60 |
perror (prog_name);
|
|
Packit |
fd8b60 |
exit (1);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
return ret;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
void *xrealloc (void *old, size_t newsz)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
void *ret = realloc (old, newsz);
|
|
Packit |
fd8b60 |
if (ret == 0 && newsz != 0) {
|
|
Packit |
fd8b60 |
perror (prog_name);
|
|
Packit |
fd8b60 |
exit (1);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
return ret;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
void *xcalloc (size_t nelts, size_t eltsz)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
void *ret = calloc (nelts, eltsz);
|
|
Packit |
fd8b60 |
if (ret == 0 && nelts != 0 && eltsz != 0) {
|
|
Packit |
fd8b60 |
perror (prog_name);
|
|
Packit |
fd8b60 |
exit (1);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
return ret;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
char *xstrdup (const char *src)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
size_t len = strlen (src) + 1;
|
|
Packit |
fd8b60 |
char *dst = xmalloc (len);
|
|
Packit |
fd8b60 |
memcpy (dst, src, len);
|
|
Packit |
fd8b60 |
return dst;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
char *xasprintf (const char *format, ...)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
char *out;
|
|
Packit |
fd8b60 |
va_list args;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
va_start (args, format);
|
|
Packit |
fd8b60 |
if (vasprintf(&out, format, args) < 0) {
|
|
Packit |
fd8b60 |
perror (prog_name);
|
|
Packit |
fd8b60 |
exit (1);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
va_end(args);
|
|
Packit |
fd8b60 |
return out;
|
|
Packit |
fd8b60 |
}
|