states/hl/cpp.st: Update C++ keyword list.
[enscript.git] / compat / xalloc.c
1 /*
2  * Non-failing memory allocation routines.
3  * Copyright (c) 1995-1999 Markku Rossi.
4  *
5  * Author: Markku Rossi <mtr@iki.fi>
6  */
7
8 /*
9  * This file is part of GNU enscript.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26
27 #include <stdio.h>
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #if STDC_HEADERS
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #else /* no STDC_HEADERS */
39
40 #if HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43
44 #if HAVE_STRING_H
45 #include <string.h>
46 #endif
47
48 #endif /* no STDC_HEADERS */
49
50 #if ENABLE_NLS
51 #include <libintl.h>
52 #define _(String) gettext (String)
53 #else
54 #define _(String) String
55 #endif
56
57 /*
58  * Global functions.
59  */
60
61 void *
62 xmalloc (size)
63      size_t size;
64 {
65   void *ptr;
66
67   ptr = malloc (size);
68   if (ptr == NULL)
69     {
70       fprintf (stderr, _("xmalloc(): couldn't allocate %d bytes\n"), size);
71       exit (1);
72     }
73
74   return ptr;
75 }
76
77
78 void *
79 xcalloc (num, size)
80      size_t num;
81      size_t size;
82 {
83   void *ptr;
84
85   ptr = calloc (num, size);
86   if (ptr == NULL)
87     {
88       fprintf (stderr, _("xcalloc(): couldn't allocate %d bytes\n"), size);
89       exit (1);
90     }
91
92   return ptr;
93 }
94
95
96 void *
97 xrealloc (ptr, size)
98      void *ptr;
99      size_t size;
100 {
101   void *nptr;
102
103   if (ptr == NULL)
104     return xmalloc (size);
105
106   nptr = realloc (ptr, size);
107   if (nptr == NULL)
108     {
109       fprintf (stderr, _("xrealloc(): couldn't reallocate %d bytes\n"), size);
110       exit (1);
111     }
112
113   return nptr;
114 }
115
116
117 void
118 xfree (ptr)
119      void *ptr;
120 {
121   if (ptr == NULL)
122     return;
123
124   free (ptr);
125 }
126
127
128 char *
129 xstrdup (str)
130      char *str;
131 {
132   char *tmp;
133
134   tmp = xmalloc (strlen (str) + 1);
135   strcpy (tmp, str);
136
137   return tmp;
138 }