summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2003-09-17 16:31:38 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2003-09-17 16:31:38 (GMT)
commit506a044efbd6c756e499c134263a27323ad770b6 (patch)
tree8649f92e8fda0b5d361a70c86033fdee367f615e /lib
parente97348a8f94bf0952a1ec1c439b9837689e0b9b7 (diff)
downloadmonitoring-plugins-506a044efbd6c756e499c134263a27323ad770b6.tar.gz
Support to compile on AIX
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@736 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am11
-rw-r--r--lib/error.c389
-rw-r--r--lib/error.h78
-rw-r--r--lib/error.m414
-rw-r--r--lib/malloc.c38
-rw-r--r--lib/realloc.c44
-rw-r--r--lib/unlocked-io.h90
-rw-r--r--lib/xalloc.h87
-rw-r--r--lib/xmalloc.c113
9 files changed, 862 insertions, 2 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index deb66db..e625d29 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,8 +4,15 @@ noinst_LIBRARIES = libnagiosplug.a
4 4
5noinst_HEADERS = getopt.h gettext.h fsusage.h mountlist.h 5noinst_HEADERS = getopt.h gettext.h fsusage.h mountlist.h
6 6
7libnagiosplug_a_SOURCES = getopt.c getopt1.c getloadavg.c snprintf.c fsusage.c mountlist.c 7libnagiosplug_a_SOURCES = getopt.c getopt1.c snprintf.c fsusage.c mountlist.c xmalloc.c
8 8
9EXTRA_DIST = ulonglong.m4 codeset.m4 getloadavg.m4 gettext.m4 glibc21.m4 iconv.m4 intdiv0.m4 inttypes.m4 inttypes_h.m4 inttypes-pri.m4 isc-posix.m4 lcmessage.m4 lib-ld.m4 lib-link.m4 lib-prefix.m4 progtest.m4 stdint_h.m4 uintmax_t.m4 afs.m4 fstypename.m4 fsusage.m4 ls-mntd-fs.m4 9libnagiosplug_a_LIBADD = @LIBOBJS@
10libnagiosplug_a_DEPENDENCIES = $(libnagiosplug_a_LIBADD)
11
12EXTRA_DIST = ulonglong.m4 codeset.m4 getloadavg.m4 gettext.m4 glibc21.m4 iconv.m4 intdiv0.m4 \
13 inttypes.m4 inttypes_h.m4 inttypes-pri.m4 isc-posix.m4 lcmessage.m4 lib-ld.m4 \
14 lib-link.m4 lib-prefix.m4 progtest.m4 stdint_h.m4 uintmax_t.m4 afs.m4 \
15 fstypename.m4 fsusage.m4 ls-mntd-fs.m4 \
16 malloc.c realloc.c strtod.c
10 17
11INCLUDES = -I$(srcdir) 18INCLUDES = -I$(srcdir)
diff --git a/lib/error.c b/lib/error.c
new file mode 100644
index 0000000..2296124
--- /dev/null
+++ b/lib/error.c
@@ -0,0 +1,389 @@
1/* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
18
19#ifdef HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include <stdio.h>
24
25#ifdef _LIBC
26# include <libintl.h>
27#else
28# include "gettext.h"
29#endif
30
31#ifdef _LIBC
32# include <wchar.h>
33# define mbsrtowcs __mbsrtowcs
34#endif
35
36#if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
37# if __STDC__
38# include <stdarg.h>
39# define VA_START(args, lastarg) va_start(args, lastarg)
40# else
41# include <varargs.h>
42# define VA_START(args, lastarg) va_start(args)
43# endif
44#else
45# define va_alist a1, a2, a3, a4, a5, a6, a7, a8
46# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
47#endif
48
49#if STDC_HEADERS || _LIBC
50# include <stdlib.h>
51# include <string.h>
52#else
53void exit ();
54#endif
55
56#include "error.h"
57
58#if !_LIBC
59# include "unlocked-io.h"
60#endif
61
62#ifndef _
63# define _(String) String
64#endif
65
66/* If NULL, error will flush stdout, then print on stderr the program
67 name, a colon and a space. Otherwise, error will call this
68 function without parameters instead. */
69void (*error_print_progname) (
70#if __STDC__ - 0
71 void
72#endif
73 );
74
75/* This variable is incremented each time `error' is called. */
76unsigned int error_message_count;
77
78#ifdef _LIBC
79/* In the GNU C library, there is a predefined variable for this. */
80
81# define program_name program_invocation_name
82# include <errno.h>
83# include <libio/libioP.h>
84
85/* In GNU libc we want do not want to use the common name `error' directly.
86 Instead make it a weak alias. */
87extern void __error (int status, int errnum, const char *message, ...)
88 __attribute__ ((__format__ (__printf__, 3, 4)));
89extern void __error_at_line (int status, int errnum, const char *file_name,
90 unsigned int line_number, const char *message,
91 ...)
92 __attribute__ ((__format__ (__printf__, 5, 6)));;
93# define error __error
94# define error_at_line __error_at_line
95
96# include <libio/iolibio.h>
97# define fflush(s) INTUSE(_IO_fflush) (s)
98# undef putc
99# define putc(c, fp) INTUSE(_IO_putc) (c, fp)
100
101#else /* not _LIBC */
102
103# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
104# ifndef HAVE_DECL_STRERROR_R
105"this configure-time declaration test was not run"
106# endif
107char *strerror_r ();
108# endif
109
110/* The calling program should define program_name and set it to the
111 name of the executing program. */
112extern char *progname;
113
114# if HAVE_STRERROR_R || defined strerror_r
115# define __strerror_r strerror_r
116# else
117# if HAVE_STRERROR
118# ifndef HAVE_DECL_STRERROR
119"this configure-time declaration test was not run"
120# endif
121# if !HAVE_DECL_STRERROR
122char *strerror ();
123# endif
124# else
125static char *
126private_strerror (int errnum)
127{
128 extern char *sys_errlist[];
129 extern int sys_nerr;
130
131 if (errnum > 0 && errnum <= sys_nerr)
132 return _(sys_errlist[errnum]);
133 return _("Unknown system error");
134}
135# define strerror private_strerror
136# endif /* HAVE_STRERROR */
137# endif /* HAVE_STRERROR_R || defined strerror_r */
138#endif /* not _LIBC */
139
140static void
141print_errno_message (int errnum)
142{
143 char const *s;
144
145#if defined HAVE_STRERROR_R || _LIBC
146 char errbuf[1024];
147# if STRERROR_R_CHAR_P || _LIBC
148 s = __strerror_r (errnum, errbuf, sizeof errbuf);
149# else
150 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
151 s = errbuf;
152 else
153 s = 0;
154# endif
155#else
156 s = strerror (errnum);
157#endif
158
159#if !_LIBC
160 if (! s)
161 s = _("Unknown system error");
162#endif
163
164#if _LIBC
165 if (_IO_fwide (stderr, 0) > 0)
166 {
167 __fwprintf (stderr, L": %s", s);
168 return;
169 }
170#endif
171
172 fprintf (stderr, ": %s", s);
173}
174
175#ifdef VA_START
176static void
177error_tail (int status, int errnum, const char *message, va_list args)
178{
179# if HAVE_VPRINTF || _LIBC
180# if _LIBC
181 if (_IO_fwide (stderr, 0) > 0)
182 {
183# define ALLOCA_LIMIT 2000
184 size_t len = strlen (message) + 1;
185 wchar_t *wmessage = NULL;
186 mbstate_t st;
187 size_t res;
188 const char *tmp;
189
190 do
191 {
192 if (len < ALLOCA_LIMIT)
193 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
194 else
195 {
196 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
197 wmessage = NULL;
198
199 wmessage = (wchar_t *) realloc (wmessage,
200 len * sizeof (wchar_t));
201
202 if (wmessage == NULL)
203 {
204 fputws_unlocked (L"out of memory\n", stderr);
205 return;
206 }
207 }
208
209 memset (&st, '\0', sizeof (st));
210 tmp =message;
211 }
212 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
213
214 if (res == (size_t) -1)
215 /* The string cannot be converted. */
216 wmessage = (wchar_t *) L"???";
217
218 __vfwprintf (stderr, wmessage, args);
219 }
220 else
221# endif
222 vfprintf (stderr, message, args);
223# else
224 _doprnt (message, args, stderr);
225# endif
226 va_end (args);
227
228 ++error_message_count;
229 if (errnum)
230 print_errno_message (errnum);
231# if _LIBC
232 if (_IO_fwide (stderr, 0) > 0)
233 putwc (L'\n', stderr);
234 else
235# endif
236 putc ('\n', stderr);
237 fflush (stderr);
238 if (status)
239 exit (status);
240}
241#endif
242
243
244/* Print the program name and error message MESSAGE, which is a printf-style
245 format string with optional args.
246 If ERRNUM is nonzero, print its corresponding system error message.
247 Exit with status STATUS if it is nonzero. */
248/* VARARGS */
249void
250#if defined VA_START && __STDC__
251error (int status, int errnum, const char *message, ...)
252#else
253error (status, errnum, message, va_alist)
254 int status;
255 int errnum;
256 char *message;
257 va_dcl
258#endif
259{
260#ifdef VA_START
261 va_list args;
262#endif
263
264 fflush (stdout);
265#ifdef _LIBC
266 _IO_flockfile (stderr);
267#endif
268 if (error_print_progname)
269 (*error_print_progname) ();
270 else
271 {
272#if _LIBC
273 if (_IO_fwide (stderr, 0) > 0)
274 __fwprintf (stderr, L"%s: ", progname);
275 else
276#endif
277 fprintf (stderr, "%s: ", progname);
278 }
279
280#ifdef VA_START
281 VA_START (args, message);
282 error_tail (status, errnum, message, args);
283#else
284 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
285
286 ++error_message_count;
287 if (errnum)
288 print_errno_message (errnum);
289 putc ('\n', stderr);
290 fflush (stderr);
291 if (status)
292 exit (status);
293#endif
294
295#ifdef _LIBC
296 _IO_funlockfile (stderr);
297#endif
298}
299
300/* Sometimes we want to have at most one error per line. This
301 variable controls whether this mode is selected or not. */
302int error_one_per_line;
303
304void
305#if defined VA_START && __STDC__
306error_at_line (int status, int errnum, const char *file_name,
307 unsigned int line_number, const char *message, ...)
308#else
309error_at_line (status, errnum, file_name, line_number, message, va_alist)
310 int status;
311 int errnum;
312 const char *file_name;
313 unsigned int line_number;
314 char *message;
315 va_dcl
316#endif
317{
318#ifdef VA_START
319 va_list args;
320#endif
321
322 if (error_one_per_line)
323 {
324 static const char *old_file_name;
325 static unsigned int old_line_number;
326
327 if (old_line_number == line_number
328 && (file_name == old_file_name
329 || strcmp (old_file_name, file_name) == 0))
330 /* Simply return and print nothing. */
331 return;
332
333 old_file_name = file_name;
334 old_line_number = line_number;
335 }
336
337 fflush (stdout);
338#ifdef _LIBC
339 _IO_flockfile (stderr);
340#endif
341 if (error_print_progname)
342 (*error_print_progname) ();
343 else
344 {
345#if _LIBC
346 if (_IO_fwide (stderr, 0) > 0)
347 __fwprintf (stderr, L"%s: ", progname);
348 else
349#endif
350 fprintf (stderr, "%s:", progname);
351 }
352
353 if (file_name != NULL)
354 {
355#if _LIBC
356 if (_IO_fwide (stderr, 0) > 0)
357 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
358 else
359#endif
360 fprintf (stderr, "%s:%d: ", file_name, line_number);
361 }
362
363#ifdef VA_START
364 VA_START (args, message);
365 error_tail (status, errnum, message, args);
366#else
367 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
368
369 ++error_message_count;
370 if (errnum)
371 print_errno_message (errnum);
372 putc ('\n', stderr);
373 fflush (stderr);
374 if (status)
375 exit (status);
376#endif
377
378#ifdef _LIBC
379 _IO_funlockfile (stderr);
380#endif
381}
382
383#ifdef _LIBC
384/* Make the weak alias. */
385# undef error
386# undef error_at_line
387weak_alias (__error, error)
388weak_alias (__error_at_line, error_at_line)
389#endif
diff --git a/lib/error.h b/lib/error.h
new file mode 100644
index 0000000..177b2dc
--- /dev/null
+++ b/lib/error.h
@@ -0,0 +1,78 @@
1/* Declaration for error-reporting function
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3
4
5 NOTE: The canonical source of this file is maintained with the GNU C Library.
6 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 USA. */
22
23#ifndef _ERROR_H
24#define _ERROR_H 1
25
26#ifndef __attribute__
27/* This feature is available in gcc versions 2.5 and later. */
28# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
29# define __attribute__(Spec) /* empty */
30# endif
31/* The __-protected variants of `format' and `printf' attributes
32 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
33# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
34# define __format__ format
35# define __printf__ printf
36# endif
37#endif
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#if defined (__STDC__) && __STDC__
44
45/* Print a message with `fprintf (stderr, FORMAT, ...)';
46 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
47 If STATUS is nonzero, terminate the program with `exit (STATUS)'. */
48
49extern void error (int status, int errnum, const char *format, ...)
50 __attribute__ ((__format__ (__printf__, 3, 4)));
51
52extern void error_at_line (int status, int errnum, const char *fname,
53 unsigned int lineno, const char *format, ...)
54 __attribute__ ((__format__ (__printf__, 5, 6)));
55
56/* If NULL, error will flush stdout, then print on stderr the program
57 name, a colon and a space. Otherwise, error will call this
58 function without parameters instead. */
59extern void (*error_print_progname) (void);
60
61#else
62void error ();
63void error_at_line ();
64extern void (*error_print_progname) ();
65#endif
66
67/* This variable is incremented each time `error' is called. */
68extern unsigned int error_message_count;
69
70/* Sometimes we want to have at most one error per line. This
71 variable controls whether this mode is selected or not. */
72extern int error_one_per_line;
73
74#ifdef __cplusplus
75}
76#endif
77
78#endif /* error.h */
diff --git a/lib/error.m4 b/lib/error.m4
new file mode 100644
index 0000000..717725d
--- /dev/null
+++ b/lib/error.m4
@@ -0,0 +1,14 @@
1#serial 5
2
3dnl FIXME: put these prerequisite-only *.m4 files in a separate
4dnl directory -- otherwise, they'll conflict with existing files.
5
6dnl These are the prerequisite macros for GNU's error.c file.
7AC_DEFUN([jm_PREREQ_ERROR],
8[
9 AC_CHECK_FUNCS(strerror vprintf doprnt)
10 AC_CHECK_DECLS([strerror])
11 AC_CHECK_HEADERS([libintl.h])
12 AC_FUNC_STRERROR_R
13 AC_HEADER_STDC
14])
diff --git a/lib/malloc.c b/lib/malloc.c
new file mode 100644
index 0000000..5e7674b
--- /dev/null
+++ b/lib/malloc.c
@@ -0,0 +1,38 @@
1/* Work around bug on some systems where malloc (0) fails.
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18/* written by Jim Meyering */
19
20#if HAVE_CONFIG_H
21# include <config.h>
22#endif
23#undef malloc
24
25#include <sys/types.h>
26
27char *malloc ();
28
29/* Allocate an N-byte block of memory from the heap.
30 If N is zero, allocate a 1-byte block. */
31
32char *
33rpl_malloc (size_t n)
34{
35 if (n == 0)
36 n = 1;
37 return malloc (n);
38}
diff --git a/lib/realloc.c b/lib/realloc.c
new file mode 100644
index 0000000..d0d3e4a
--- /dev/null
+++ b/lib/realloc.c
@@ -0,0 +1,44 @@
1/* Work around bug on some systems where realloc (NULL, 0) fails.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18/* written by Jim Meyering */
19
20#if HAVE_CONFIG_H
21# include <config.h>
22#endif
23#undef realloc
24
25#include <sys/types.h>
26
27char *malloc ();
28char *realloc ();
29
30/* Change the size of an allocated block of memory P to N bytes,
31 with error checking. If N is zero, change it to 1. If P is NULL,
32 use malloc. */
33
34char *
35rpl_realloc (p, n)
36 char *p;
37 size_t n;
38{
39 if (n == 0)
40 n = 1;
41 if (p == 0)
42 return malloc (n);
43 return realloc (p, n);
44}
diff --git a/lib/unlocked-io.h b/lib/unlocked-io.h
new file mode 100644
index 0000000..a7240fb
--- /dev/null
+++ b/lib/unlocked-io.h
@@ -0,0 +1,90 @@
1/* Prefer faster, non-thread-safe stdio functions if available.
2
3 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 USA. */
19
20/* Written by Jim Meyering. */
21
22#ifndef UNLOCKED_IO_H
23# define UNLOCKED_IO_H 1
24
25# ifndef USE_UNLOCKED_IO
26# define USE_UNLOCKED_IO 1
27# endif
28
29# if USE_UNLOCKED_IO
30
31/* These are wrappers for functions/macros from GNU libc.
32 The standard I/O functions are thread-safe. These *_unlocked ones are
33 more efficient but not thread-safe. That they're not thread-safe is
34 fine since all of the applications in this package are single threaded. */
35
36# if HAVE_DECL_CLEARERR_UNLOCKED
37# undef clearerr
38# define clearerr(x) clearerr_unlocked (x)
39# endif
40# if HAVE_DECL_FEOF_UNLOCKED
41# undef feof
42# define feof(x) feof_unlocked (x)
43# endif
44# if HAVE_DECL_FERROR_UNLOCKED
45# undef ferror
46# define ferror(x) ferror_unlocked (x)
47# endif
48# if HAVE_DECL_FFLUSH_UNLOCKED
49# undef fflush
50# define fflush(x) fflush_unlocked (x)
51# endif
52# if HAVE_DECL_FGETS_UNLOCKED
53# undef fgets
54# define fgets(x,y,z) fgets_unlocked (x,y,z)
55# endif
56# if HAVE_DECL_FPUTC_UNLOCKED
57# undef fputc
58# define fputc(x,y) fputc_unlocked (x,y)
59# endif
60# if HAVE_DECL_FPUTS_UNLOCKED
61# undef fputs
62# define fputs(x,y) fputs_unlocked (x,y)
63# endif
64# if HAVE_DECL_FREAD_UNLOCKED
65# undef fread
66# define fread(w,x,y,z) fread_unlocked (w,x,y,z)
67# endif
68# if HAVE_DECL_FWRITE_UNLOCKED
69# undef fwrite
70# define fwrite(w,x,y,z) fwrite_unlocked (w,x,y,z)
71# endif
72# if HAVE_DECL_GETC_UNLOCKED
73# undef getc
74# define getc(x) getc_unlocked (x)
75# endif
76# if HAVE_DECL_GETCHAR_UNLOCKED
77# undef getchar
78# define getchar() getchar_unlocked ()
79# endif
80# if HAVE_DECL_PUTC_UNLOCKED
81# undef putc
82# define putc(x,y) putc_unlocked (x,y)
83# endif
84# if HAVE_DECL_PUTCHAR_UNLOCKED
85# undef putchar
86# define putchar(x) putchar_unlocked (x)
87# endif
88
89# endif /* USE_UNLOCKED_IO */
90#endif /* UNLOCKED_IO_H */
diff --git a/lib/xalloc.h b/lib/xalloc.h
new file mode 100644
index 0000000..098a6c2
--- /dev/null
+++ b/lib/xalloc.h
@@ -0,0 +1,87 @@
1/* xalloc.h -- malloc with out-of-memory checking
2 Copyright (C) 1990-1998, 1999, 2000 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18#ifndef XALLOC_H_
19# define XALLOC_H_
20
21# ifndef PARAMS
22# if defined PROTOTYPES || (defined __STDC__ && __STDC__)
23# define PARAMS(Args) Args
24# else
25# define PARAMS(Args) ()
26# endif
27# endif
28
29# ifndef __attribute__
30# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
31# define __attribute__(x)
32# endif
33# endif
34
35# ifndef ATTRIBUTE_NORETURN
36# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
37# endif
38
39/* Exit value when the requested amount of memory is not available.
40 It is initialized to EXIT_FAILURE, but the caller may set it to
41 some other value. */
42extern int xalloc_exit_failure;
43
44/* If this pointer is non-zero, run the specified function upon each
45 allocation failure. It is initialized to zero. */
46extern void (*xalloc_fail_func) PARAMS ((void));
47
48/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
49 message is output. It is translated via gettext.
50 Its value is "memory exhausted". */
51extern char const xalloc_msg_memory_exhausted[];
52
53/* This function is always triggered when memory is exhausted. It is
54 in charge of honoring the three previous items. This is the
55 function to call when one wants the program to die because of a
56 memory allocation failure. */
57extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN;
58
59void *xmalloc PARAMS ((size_t n));
60void *xcalloc PARAMS ((size_t n, size_t s));
61void *xrealloc PARAMS ((void *p, size_t n));
62char *xstrdup PARAMS ((const char *str));
63
64# define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items)))
65# define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items)))
66# define XREALLOC(Ptr, Type, N_items) \
67 ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items)))
68
69/* Declare and alloc memory for VAR of type TYPE. */
70# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1)
71
72/* Free VAR only if non NULL. */
73# define XFREE(Var) \
74 do { \
75 if (Var) \
76 free (Var); \
77 } while (0)
78
79/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
80# define CCLONE(Src, Num) \
81 (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num)))
82
83/* Return a malloc'ed copy of SRC. */
84# define CLONE(Src) CCLONE (Src, 1)
85
86
87#endif /* !XALLOC_H_ */
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
new file mode 100644
index 0000000..3affee7
--- /dev/null
+++ b/lib/xmalloc.c
@@ -0,0 +1,113 @@
1/* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18#if HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22#include <sys/types.h>
23
24#if STDC_HEADERS
25# include <stdlib.h>
26#else
27void *calloc ();
28void *malloc ();
29void *realloc ();
30void free ();
31#endif
32
33#include "gettext.h"
34#define _(msgid) gettext (msgid)
35#define N_(msgid) msgid
36
37#include "error.h"
38#include "xalloc.h"
39
40#ifndef EXIT_FAILURE
41# define EXIT_FAILURE 1
42#endif
43
44/* The following tests require AC_PREREQ(2.54). */
45
46#ifndef HAVE_MALLOC
47"you must run the autoconf test for a GNU libc compatible malloc"
48#endif
49
50#ifndef HAVE_REALLOC
51"you must run the autoconf test for a GNU libc compatible realloc"
52#endif
53
54/* Exit value when the requested amount of memory is not available.
55 The caller may set it to some other value. */
56int xalloc_exit_failure = EXIT_FAILURE;
57
58/* If non NULL, call this function when memory is exhausted. */
59void (*xalloc_fail_func) PARAMS ((void)) = 0;
60
61/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
62 before exiting when memory is exhausted. Goes through gettext. */
63char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
64
65void
66xalloc_die (void)
67{
68 if (xalloc_fail_func)
69 (*xalloc_fail_func) ();
70 error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
71 /* The `noreturn' cannot be given to error, since it may return if
72 its first argument is 0. To help compilers understand the
73 xalloc_die does terminate, call exit. */
74 exit (EXIT_FAILURE);
75}
76
77/* Allocate N bytes of memory dynamically, with error checking. */
78
79void *
80xmalloc (size_t n)
81{
82 void *p;
83
84 p = malloc (n);
85 if (p == 0)
86 xalloc_die ();
87 return p;
88}
89
90/* Change the size of an allocated block of memory P to N bytes,
91 with error checking. */
92
93void *
94xrealloc (void *p, size_t n)
95{
96 p = realloc (p, n);
97 if (p == 0)
98 xalloc_die ();
99 return p;
100}
101
102/* Allocate memory for N elements of S bytes, with error checking. */
103
104void *
105xcalloc (size_t n, size_t s)
106{
107 void *p;
108
109 p = calloc (n, s);
110 if (p == 0)
111 xalloc_die ();
112 return p;
113}