1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* clients/ksu/xmalloc.c - Exit-on-failure allocation wrappers */ 3 /* 4 * Copyright 1999 by the Massachusetts Institute of Technology. 5 * All Rights Reserved. 6 * 7 * Export of this software from the United States of America may 8 * require a specific license from the United States Government. 9 * It is the responsibility of any person or organization contemplating 10 * export to obtain such a license before exporting. 11 * 12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 13 * distribute this software and its documentation for any purpose and 14 * without fee is hereby granted, provided that the above copyright 15 * notice appear in all copies and that both that copyright notice and 16 * this permission notice appear in supporting documentation, and that 17 * the name of M.I.T. not be used in advertising or publicity pertaining 18 * to distribution of the software without specific, written prior 19 * permission. Furthermore if you modify this software you must label 20 * your software as modified software and not distribute it in such a 21 * fashion that it might be confused with the original M.I.T. software. 22 * M.I.T. makes no representations about the suitability of 23 * this software for any purpose. It is provided "as is" without express 24 * or implied warranty. 25 */ 26 27 #include "k5-platform.h" 28 #include "ksu.h" 29 30 void *xmalloc (size_t sz) 31 { 32 void *ret = malloc (sz); 33 if (ret == 0 && sz != 0) { 34 perror (prog_name); 35 exit (1); 36 } 37 return ret; 38 } 39 40 void *xrealloc (void *old, size_t newsz) 41 { 42 void *ret = realloc (old, newsz); 43 if (ret == 0 && newsz != 0) { 44 perror (prog_name); 45 exit (1); 46 } 47 return ret; 48 } 49 50 void *xcalloc (size_t nelts, size_t eltsz) 51 { 52 void *ret = calloc (nelts, eltsz); 53 if (ret == 0 && nelts != 0 && eltsz != 0) { 54 perror (prog_name); 55 exit (1); 56 } 57 return ret; 58 } 59 60 char *xstrdup (const char *src) 61 { 62 size_t len = strlen (src) + 1; 63 char *dst = xmalloc (len); 64 memcpy (dst, src, len); 65 return dst; 66 } 67 68 char *xasprintf (const char *format, ...) 69 { 70 char *out; 71 va_list args; 72 73 va_start (args, format); 74 if (vasprintf(&out, format, args) < 0) { 75 perror (prog_name); 76 exit (1); 77 } 78 va_end(args); 79 return out; 80 } 81