* modules/calloc: New file.
authorJim Meyering <jim@meyering.net>
Thu, 10 Jun 2004 08:29:25 +0000 (08:29 +0000)
committerJim Meyering <jim@meyering.net>
Thu, 10 Jun 2004 08:29:25 +0000 (08:29 +0000)
* lib/calloc.c: New file.
* m4/calloc.m4: New file.

ChangeLog
lib/ChangeLog
lib/calloc.c [new file with mode: 0644]
m4/ChangeLog
m4/calloc.m4 [new file with mode: 0644]
modules/calloc [new file with mode: 0644]

index c6c4242..224b5f8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2004-06-01  Jim Meyering  <jim@meyering.net>
+
+       * modules/calloc: New file.
+
 2004-06-01  Paul Eggert  <eggert@cs.ucla.edu>
 
        * modules/file-type: Add lib/stat-macros.h.
index ce8dbf2..881a33b 100644 (file)
@@ -1,3 +1,7 @@
+2004-06-01  Jim Meyering  <jim@meyering.net>
+
+       * calloc.c: New file.
+
 2004-06-06  Paul Eggert  <eggert@cs.ucla.edu>
 
        * getdate.y (yylex): Allow space between sign and number.
diff --git a/lib/calloc.c b/lib/calloc.c
new file mode 100644 (file)
index 0000000..2a9e6fa
--- /dev/null
@@ -0,0 +1,39 @@
+/* Work around the condition whereby calloc (n, s) fails when n*s is 0.
+   This wrapper function is required at least on Tru64 UNIX 5.1.
+   Copyright (C) 2004 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 <config.h>
+#endif
+#undef calloc
+
+#include <stdlib.h>
+
+/* Allocate and zero-fill an NxS-byte block of memory from the heap.
+   If N or S is zero, allocate and zero-fill a 1-byte block.  */
+
+void *
+rpl_calloc (size_t n, size_t s)
+{
+  if (n == 0)
+    n = 1;
+  if (s == 0)
+    s = 1;
+  return calloc (n, s);
+}
index e310404..86cfac1 100644 (file)
@@ -1,3 +1,7 @@
+2004-06-01  Jim Meyering  <jim@meyering.net>
+
+       * calloc.m4: New file.
+
 2004-06-01  Paul Eggert  <eggert@cs.ucla.edu>
 
        Merge from coreutils CVS.
diff --git a/m4/calloc.m4 b/m4/calloc.m4
new file mode 100644 (file)
index 0000000..fee15dd
--- /dev/null
@@ -0,0 +1,47 @@
+#serial 1
+
+# FIXME: remove this whole file once we can depend
+# on having the definition from autoconf.
+undefine([AC_FUNC_CALLOC])
+
+# Determine whether calloc (N, S) returns non-NULL when N*S is zero.
+# If so, define HAVE_CALLOC.  Otherwise, define calloc to rpl_calloc
+# and arrange to use a calloc wrapper function that does work in that case.
+
+# _AC_FUNC_CALLOC_IF(IF-WORKS, IF-NOT)
+# -------------------------------------
+# If `calloc (0, 0)' is properly handled, run IF-WORKS, otherwise, IF-NOT.
+AC_DEFUN([_AC_FUNC_CALLOC_IF],
+[AC_REQUIRE([AC_HEADER_STDC])dnl
+AC_CHECK_HEADERS(stdlib.h)
+AC_CACHE_CHECK([for GNU libc compatible calloc], ac_cv_func_calloc_0_nonnull,
+[AC_RUN_IFELSE(
+[AC_LANG_PROGRAM(
+[[#if STDC_HEADERS || HAVE_STDLIB_H
+# include <stdlib.h>
+#else
+char *calloc ();
+#endif
+]],
+                [exit (calloc (0, 0) ? 0 : 1);])],
+              [ac_cv_func_calloc_0_nonnull=yes],
+              [ac_cv_func_calloc_0_nonnull=no],
+              [ac_cv_func_calloc_0_nonnull=no])])
+AS_IF([test $ac_cv_func_calloc_0_nonnull = yes], [$1], [$2])
+])# AC_FUNC_CALLOC
+
+
+# AC_FUNC_CALLOC
+# ---------------
+# Report whether `calloc (0, 0)' is properly handled, and replace calloc if
+# needed.
+AC_DEFUN([AC_FUNC_CALLOC],
+[_AC_FUNC_CALLOC_IF(
+  [AC_DEFINE([HAVE_CALLOC], 1,
+            [Define to 1 if your system has a GNU libc compatible `calloc'
+             function, and to 0 otherwise.])],
+  [AC_DEFINE([HAVE_CALLOC], 0)
+   AC_LIBOBJ([calloc])
+   AC_DEFINE([calloc], [rpl_calloc],
+      [Define to rpl_calloc if the replacement function should be used.])])
+])# AC_FUNC_CALLOC
diff --git a/modules/calloc b/modules/calloc
new file mode 100644 (file)
index 0000000..7506d28
--- /dev/null
@@ -0,0 +1,19 @@
+Description:
+calloc() function that is glibc compatible.
+
+Files:
+lib/calloc.c
+m4/calloc.m4
+
+Depends-on:
+
+configure.ac:
+AC_FUNC_CALLOC
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+Maintainer:
+Jim Meyering