From 506a044efbd6c756e499c134263a27323ad770b6 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Wed, 17 Sep 2003 16:31:38 +0000 Subject: Support to compile on AIX git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@736 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/configure.in b/configure.in index 0999d7e..8634b0a 100644 --- a/configure.in +++ b/configure.in @@ -27,6 +27,12 @@ AC_MINIX AC_PROG_MAKE_SET AC_PROG_AWK +AC_FUNC_MALLOC +AC_FUNC_REALLOC +jm_PREREQ_ERROR +AC_FUNC_ERROR_AT_LINE + +AC_CONFIG_LIBOBJ_DIR(lib) AC_FUNC_GETLOADAVG($topdir/lib) ifdef([AC_FUNC_STRTOD],[AC_FUNC_STRTOD],[AM_FUNC_STRTOD]) 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 noinst_HEADERS = getopt.h gettext.h fsusage.h mountlist.h -libnagiosplug_a_SOURCES = getopt.c getopt1.c getloadavg.c snprintf.c fsusage.c mountlist.c +libnagiosplug_a_SOURCES = getopt.c getopt1.c snprintf.c fsusage.c mountlist.c xmalloc.c -EXTRA_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 +libnagiosplug_a_LIBADD = @LIBOBJS@ +libnagiosplug_a_DEPENDENCIES = $(libnagiosplug_a_LIBADD) + +EXTRA_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 \ + malloc.c realloc.c strtod.c INCLUDES = -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 @@ +/* Error handler for noninteractive utilities + Copyright (C) 1990-1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/* Written by David MacKenzie . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#ifdef _LIBC +# include +#else +# include "gettext.h" +#endif + +#ifdef _LIBC +# include +# define mbsrtowcs __mbsrtowcs +#endif + +#if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC +# if __STDC__ +# include +# define VA_START(args, lastarg) va_start(args, lastarg) +# else +# include +# define VA_START(args, lastarg) va_start(args) +# endif +#else +# define va_alist a1, a2, a3, a4, a5, a6, a7, a8 +# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; +#endif + +#if STDC_HEADERS || _LIBC +# include +# include +#else +void exit (); +#endif + +#include "error.h" + +#if !_LIBC +# include "unlocked-io.h" +#endif + +#ifndef _ +# define _(String) String +#endif + +/* If NULL, error will flush stdout, then print on stderr the program + name, a colon and a space. Otherwise, error will call this + function without parameters instead. */ +void (*error_print_progname) ( +#if __STDC__ - 0 + void +#endif + ); + +/* This variable is incremented each time `error' is called. */ +unsigned int error_message_count; + +#ifdef _LIBC +/* In the GNU C library, there is a predefined variable for this. */ + +# define program_name program_invocation_name +# include +# include + +/* In GNU libc we want do not want to use the common name `error' directly. + Instead make it a weak alias. */ +extern void __error (int status, int errnum, const char *message, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); +extern void __error_at_line (int status, int errnum, const char *file_name, + unsigned int line_number, const char *message, + ...) + __attribute__ ((__format__ (__printf__, 5, 6)));; +# define error __error +# define error_at_line __error_at_line + +# include +# define fflush(s) INTUSE(_IO_fflush) (s) +# undef putc +# define putc(c, fp) INTUSE(_IO_putc) (c, fp) + +#else /* not _LIBC */ + +# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P +# ifndef HAVE_DECL_STRERROR_R +"this configure-time declaration test was not run" +# endif +char *strerror_r (); +# endif + +/* The calling program should define program_name and set it to the + name of the executing program. */ +extern char *progname; + +# if HAVE_STRERROR_R || defined strerror_r +# define __strerror_r strerror_r +# else +# if HAVE_STRERROR +# ifndef HAVE_DECL_STRERROR +"this configure-time declaration test was not run" +# endif +# if !HAVE_DECL_STRERROR +char *strerror (); +# endif +# else +static char * +private_strerror (int errnum) +{ + extern char *sys_errlist[]; + extern int sys_nerr; + + if (errnum > 0 && errnum <= sys_nerr) + return _(sys_errlist[errnum]); + return _("Unknown system error"); +} +# define strerror private_strerror +# endif /* HAVE_STRERROR */ +# endif /* HAVE_STRERROR_R || defined strerror_r */ +#endif /* not _LIBC */ + +static void +print_errno_message (int errnum) +{ + char const *s; + +#if defined HAVE_STRERROR_R || _LIBC + char errbuf[1024]; +# if STRERROR_R_CHAR_P || _LIBC + s = __strerror_r (errnum, errbuf, sizeof errbuf); +# else + if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0) + s = errbuf; + else + s = 0; +# endif +#else + s = strerror (errnum); +#endif + +#if !_LIBC + if (! s) + s = _("Unknown system error"); +#endif + +#if _LIBC + if (_IO_fwide (stderr, 0) > 0) + { + __fwprintf (stderr, L": %s", s); + return; + } +#endif + + fprintf (stderr, ": %s", s); +} + +#ifdef VA_START +static void +error_tail (int status, int errnum, const char *message, va_list args) +{ +# if HAVE_VPRINTF || _LIBC +# if _LIBC + if (_IO_fwide (stderr, 0) > 0) + { +# define ALLOCA_LIMIT 2000 + size_t len = strlen (message) + 1; + wchar_t *wmessage = NULL; + mbstate_t st; + size_t res; + const char *tmp; + + do + { + if (len < ALLOCA_LIMIT) + wmessage = (wchar_t *) alloca (len * sizeof (wchar_t)); + else + { + if (wmessage != NULL && len / 2 < ALLOCA_LIMIT) + wmessage = NULL; + + wmessage = (wchar_t *) realloc (wmessage, + len * sizeof (wchar_t)); + + if (wmessage == NULL) + { + fputws_unlocked (L"out of memory\n", stderr); + return; + } + } + + memset (&st, '\0', sizeof (st)); + tmp =message; + } + while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len); + + if (res == (size_t) -1) + /* The string cannot be converted. */ + wmessage = (wchar_t *) L"???"; + + __vfwprintf (stderr, wmessage, args); + } + else +# endif + vfprintf (stderr, message, args); +# else + _doprnt (message, args, stderr); +# endif + va_end (args); + + ++error_message_count; + if (errnum) + print_errno_message (errnum); +# if _LIBC + if (_IO_fwide (stderr, 0) > 0) + putwc (L'\n', stderr); + else +# endif + putc ('\n', stderr); + fflush (stderr); + if (status) + exit (status); +} +#endif + + +/* Print the program name and error message MESSAGE, which is a printf-style + format string with optional args. + If ERRNUM is nonzero, print its corresponding system error message. + Exit with status STATUS if it is nonzero. */ +/* VARARGS */ +void +#if defined VA_START && __STDC__ +error (int status, int errnum, const char *message, ...) +#else +error (status, errnum, message, va_alist) + int status; + int errnum; + char *message; + va_dcl +#endif +{ +#ifdef VA_START + va_list args; +#endif + + fflush (stdout); +#ifdef _LIBC + _IO_flockfile (stderr); +#endif + if (error_print_progname) + (*error_print_progname) (); + else + { +#if _LIBC + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s: ", progname); + else +#endif + fprintf (stderr, "%s: ", progname); + } + +#ifdef VA_START + VA_START (args, message); + error_tail (status, errnum, message, args); +#else + fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); + + ++error_message_count; + if (errnum) + print_errno_message (errnum); + putc ('\n', stderr); + fflush (stderr); + if (status) + exit (status); +#endif + +#ifdef _LIBC + _IO_funlockfile (stderr); +#endif +} + +/* Sometimes we want to have at most one error per line. This + variable controls whether this mode is selected or not. */ +int error_one_per_line; + +void +#if defined VA_START && __STDC__ +error_at_line (int status, int errnum, const char *file_name, + unsigned int line_number, const char *message, ...) +#else +error_at_line (status, errnum, file_name, line_number, message, va_alist) + int status; + int errnum; + const char *file_name; + unsigned int line_number; + char *message; + va_dcl +#endif +{ +#ifdef VA_START + va_list args; +#endif + + if (error_one_per_line) + { + static const char *old_file_name; + static unsigned int old_line_number; + + if (old_line_number == line_number + && (file_name == old_file_name + || strcmp (old_file_name, file_name) == 0)) + /* Simply return and print nothing. */ + return; + + old_file_name = file_name; + old_line_number = line_number; + } + + fflush (stdout); +#ifdef _LIBC + _IO_flockfile (stderr); +#endif + if (error_print_progname) + (*error_print_progname) (); + else + { +#if _LIBC + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s: ", progname); + else +#endif + fprintf (stderr, "%s:", progname); + } + + if (file_name != NULL) + { +#if _LIBC + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s:%d: ", file_name, line_number); + else +#endif + fprintf (stderr, "%s:%d: ", file_name, line_number); + } + +#ifdef VA_START + VA_START (args, message); + error_tail (status, errnum, message, args); +#else + fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); + + ++error_message_count; + if (errnum) + print_errno_message (errnum); + putc ('\n', stderr); + fflush (stderr); + if (status) + exit (status); +#endif + +#ifdef _LIBC + _IO_funlockfile (stderr); +#endif +} + +#ifdef _LIBC +/* Make the weak alias. */ +# undef error +# undef error_at_line +weak_alias (__error, error) +weak_alias (__error_at_line, error_at_line) +#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 @@ +/* Declaration for error-reporting function + Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + + + NOTE: The canonical source of this file is maintained with the GNU C Library. + Bugs can be reported to bug-glibc@prep.ai.mit.edu. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +#ifndef _ERROR_H +#define _ERROR_H 1 + +#ifndef __attribute__ +/* This feature is available in gcc versions 2.5 and later. */ +# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) +# define __attribute__(Spec) /* empty */ +# endif +/* The __-protected variants of `format' and `printf' attributes + are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */ +# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) +# define __format__ format +# define __printf__ printf +# endif +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined (__STDC__) && __STDC__ + +/* Print a message with `fprintf (stderr, FORMAT, ...)'; + if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM). + If STATUS is nonzero, terminate the program with `exit (STATUS)'. */ + +extern void error (int status, int errnum, const char *format, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); + +extern void error_at_line (int status, int errnum, const char *fname, + unsigned int lineno, const char *format, ...) + __attribute__ ((__format__ (__printf__, 5, 6))); + +/* If NULL, error will flush stdout, then print on stderr the program + name, a colon and a space. Otherwise, error will call this + function without parameters instead. */ +extern void (*error_print_progname) (void); + +#else +void error (); +void error_at_line (); +extern void (*error_print_progname) (); +#endif + +/* This variable is incremented each time `error' is called. */ +extern unsigned int error_message_count; + +/* Sometimes we want to have at most one error per line. This + variable controls whether this mode is selected or not. */ +extern int error_one_per_line; + +#ifdef __cplusplus +} +#endif + +#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 @@ +#serial 5 + +dnl FIXME: put these prerequisite-only *.m4 files in a separate +dnl directory -- otherwise, they'll conflict with existing files. + +dnl These are the prerequisite macros for GNU's error.c file. +AC_DEFUN([jm_PREREQ_ERROR], +[ + AC_CHECK_FUNCS(strerror vprintf doprnt) + AC_CHECK_DECLS([strerror]) + AC_CHECK_HEADERS([libintl.h]) + AC_FUNC_STRERROR_R + AC_HEADER_STDC +]) 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 @@ +/* Work around bug on some systems where malloc (0) fails. + Copyright (C) 1997, 1998 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/* written by Jim Meyering */ + +#if HAVE_CONFIG_H +# include +#endif +#undef malloc + +#include + +char *malloc (); + +/* Allocate an N-byte block of memory from the heap. + If N is zero, allocate a 1-byte block. */ + +char * +rpl_malloc (size_t n) +{ + if (n == 0) + n = 1; + return malloc (n); +} 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 @@ +/* Work around bug on some systems where realloc (NULL, 0) fails. + Copyright (C) 1997 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/* written by Jim Meyering */ + +#if HAVE_CONFIG_H +# include +#endif +#undef realloc + +#include + +char *malloc (); +char *realloc (); + +/* Change the size of an allocated block of memory P to N bytes, + with error checking. If N is zero, change it to 1. If P is NULL, + use malloc. */ + +char * +rpl_realloc (p, n) + char *p; + size_t n; +{ + if (n == 0) + n = 1; + if (p == 0) + return malloc (n); + return realloc (p, n); +} 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 @@ +/* Prefer faster, non-thread-safe stdio functions if available. + + Copyright (C) 2001, 2002 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +/* Written by Jim Meyering. */ + +#ifndef UNLOCKED_IO_H +# define UNLOCKED_IO_H 1 + +# ifndef USE_UNLOCKED_IO +# define USE_UNLOCKED_IO 1 +# endif + +# if USE_UNLOCKED_IO + +/* These are wrappers for functions/macros from GNU libc. + The standard I/O functions are thread-safe. These *_unlocked ones are + more efficient but not thread-safe. That they're not thread-safe is + fine since all of the applications in this package are single threaded. */ + +# if HAVE_DECL_CLEARERR_UNLOCKED +# undef clearerr +# define clearerr(x) clearerr_unlocked (x) +# endif +# if HAVE_DECL_FEOF_UNLOCKED +# undef feof +# define feof(x) feof_unlocked (x) +# endif +# if HAVE_DECL_FERROR_UNLOCKED +# undef ferror +# define ferror(x) ferror_unlocked (x) +# endif +# if HAVE_DECL_FFLUSH_UNLOCKED +# undef fflush +# define fflush(x) fflush_unlocked (x) +# endif +# if HAVE_DECL_FGETS_UNLOCKED +# undef fgets +# define fgets(x,y,z) fgets_unlocked (x,y,z) +# endif +# if HAVE_DECL_FPUTC_UNLOCKED +# undef fputc +# define fputc(x,y) fputc_unlocked (x,y) +# endif +# if HAVE_DECL_FPUTS_UNLOCKED +# undef fputs +# define fputs(x,y) fputs_unlocked (x,y) +# endif +# if HAVE_DECL_FREAD_UNLOCKED +# undef fread +# define fread(w,x,y,z) fread_unlocked (w,x,y,z) +# endif +# if HAVE_DECL_FWRITE_UNLOCKED +# undef fwrite +# define fwrite(w,x,y,z) fwrite_unlocked (w,x,y,z) +# endif +# if HAVE_DECL_GETC_UNLOCKED +# undef getc +# define getc(x) getc_unlocked (x) +# endif +# if HAVE_DECL_GETCHAR_UNLOCKED +# undef getchar +# define getchar() getchar_unlocked () +# endif +# if HAVE_DECL_PUTC_UNLOCKED +# undef putc +# define putc(x,y) putc_unlocked (x,y) +# endif +# if HAVE_DECL_PUTCHAR_UNLOCKED +# undef putchar +# define putchar(x) putchar_unlocked (x) +# endif + +# endif /* USE_UNLOCKED_IO */ +#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 @@ +/* xalloc.h -- malloc with out-of-memory checking + Copyright (C) 1990-1998, 1999, 2000 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifndef XALLOC_H_ +# define XALLOC_H_ + +# ifndef PARAMS +# if defined PROTOTYPES || (defined __STDC__ && __STDC__) +# define PARAMS(Args) Args +# else +# define PARAMS(Args) () +# endif +# endif + +# ifndef __attribute__ +# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ +# define __attribute__(x) +# endif +# endif + +# ifndef ATTRIBUTE_NORETURN +# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) +# endif + +/* Exit value when the requested amount of memory is not available. + It is initialized to EXIT_FAILURE, but the caller may set it to + some other value. */ +extern int xalloc_exit_failure; + +/* If this pointer is non-zero, run the specified function upon each + allocation failure. It is initialized to zero. */ +extern void (*xalloc_fail_func) PARAMS ((void)); + +/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this + message is output. It is translated via gettext. + Its value is "memory exhausted". */ +extern char const xalloc_msg_memory_exhausted[]; + +/* This function is always triggered when memory is exhausted. It is + in charge of honoring the three previous items. This is the + function to call when one wants the program to die because of a + memory allocation failure. */ +extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN; + +void *xmalloc PARAMS ((size_t n)); +void *xcalloc PARAMS ((size_t n, size_t s)); +void *xrealloc PARAMS ((void *p, size_t n)); +char *xstrdup PARAMS ((const char *str)); + +# define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items))) +# define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items))) +# define XREALLOC(Ptr, Type, N_items) \ + ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items))) + +/* Declare and alloc memory for VAR of type TYPE. */ +# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) + +/* Free VAR only if non NULL. */ +# define XFREE(Var) \ + do { \ + if (Var) \ + free (Var); \ + } while (0) + +/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ +# define CCLONE(Src, Num) \ + (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num))) + +/* Return a malloc'ed copy of SRC. */ +# define CLONE(Src) CCLONE (Src, 1) + + +#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 @@ +/* xmalloc.c -- malloc with out of memory checking + Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#if HAVE_CONFIG_H +# include +#endif + +#include + +#if STDC_HEADERS +# include +#else +void *calloc (); +void *malloc (); +void *realloc (); +void free (); +#endif + +#include "gettext.h" +#define _(msgid) gettext (msgid) +#define N_(msgid) msgid + +#include "error.h" +#include "xalloc.h" + +#ifndef EXIT_FAILURE +# define EXIT_FAILURE 1 +#endif + +/* The following tests require AC_PREREQ(2.54). */ + +#ifndef HAVE_MALLOC +"you must run the autoconf test for a GNU libc compatible malloc" +#endif + +#ifndef HAVE_REALLOC +"you must run the autoconf test for a GNU libc compatible realloc" +#endif + +/* Exit value when the requested amount of memory is not available. + The caller may set it to some other value. */ +int xalloc_exit_failure = EXIT_FAILURE; + +/* If non NULL, call this function when memory is exhausted. */ +void (*xalloc_fail_func) PARAMS ((void)) = 0; + +/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message + before exiting when memory is exhausted. Goes through gettext. */ +char const xalloc_msg_memory_exhausted[] = N_("memory exhausted"); + +void +xalloc_die (void) +{ + if (xalloc_fail_func) + (*xalloc_fail_func) (); + error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); + /* The `noreturn' cannot be given to error, since it may return if + its first argument is 0. To help compilers understand the + xalloc_die does terminate, call exit. */ + exit (EXIT_FAILURE); +} + +/* Allocate N bytes of memory dynamically, with error checking. */ + +void * +xmalloc (size_t n) +{ + void *p; + + p = malloc (n); + if (p == 0) + xalloc_die (); + return p; +} + +/* Change the size of an allocated block of memory P to N bytes, + with error checking. */ + +void * +xrealloc (void *p, size_t n) +{ + p = realloc (p, n); + if (p == 0) + xalloc_die (); + return p; +} + +/* Allocate memory for N elements of S bytes, with error checking. */ + +void * +xcalloc (size_t n, size_t s) +{ + void *p; + + p = calloc (n, s); + if (p == 0) + xalloc_die (); + return p; +} -- cgit v0.10-9-g596f