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