This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Backport of change #25831 by Nicholas, and the config part of #25832
authorH.Merijn Brand <h.m.brand@xs4all.nl>
Mon, 24 Oct 2005 23:31:40 +0000 (23:31 +0000)
committerH.Merijn Brand <h.m.brand@xs4all.nl>
Mon, 24 Oct 2005 23:31:40 +0000 (23:31 +0000)
Add a probe for whether sprintf returns the length of the buffer.

p4raw-link: @25831 on //depot/perl: 2218ba893e8101a0e20f65401c95ee06bccdc347

p4raw-id: //depot/metaconfig@25836

U/perl/d_sprintf_len.U [new file with mode: 0644]

diff --git a/U/perl/d_sprintf_len.U b/U/perl/d_sprintf_len.U
new file mode 100644 (file)
index 0000000..db6be52
--- /dev/null
@@ -0,0 +1,86 @@
+?RCS: $Id$
+?RCS:
+?RCS: Copyright (c) 2005 H.Merijn Brand
+?RCS:
+?RCS: You may distribute under the terms of either the GNU General Public
+?RCS: License or the Artistic License, as specified in the README file.
+?RCS:
+?MAKE:d_sprintf_returns_strlen: Compile cat rm run i_stdlib i_string i_math echo
+?MAKE: -pick add $@ %<
+?S:d_sprintf_returns_strlen:
+?S:    This variable defines whether sprintf returns the length of the string
+?S:    (as per the ANSI spec). Some C libraries retain compatibility with
+?S:    pre-ANSI C and return a pointer to the passed in buffer; for these
+?S:    this variable will be undef.
+?S:.
+?C:SPRINTF_RETURNS_STRLEN:
+?C:    This variable defines whether sprintf returns the length of the string
+?C:    (as per the ANSI spec). Some C libraries retain compatibility with
+?C:    pre-ANSI C and return a pointer to the passed in buffer; for these
+?C:    this variable will be undef.
+?C:.
+?H:#$d_sprintf_returns_strlen SPRINTF_RETURNS_STRLEN   /**/
+?H:.
+?T:xxx
+: see if sprintf returns the length of the string in the buffer as per ANSI
+$echo "Checking whether sprintf returns the length of the string..." >&4
+$cat <<EOP >try.c
+#include <stdio.h>
+#$i_stdlib I_STDLIB
+#ifdef I_STDLIB
+#include <stdlib.h>
+#endif
+#$i_string I_STRING
+#ifdef I_STRING
+#  include <string.h>
+#else
+#  include <strings.h>
+#endif
+#$i_math I_MATH
+#ifdef I_MATH
+#include <math.h>
+#endif
+
+char buffer[256];
+
+int check (size_t expect, int test) {
+  size_t got = strlen(buffer);
+  if (expect == got)
+    return 0;
+
+  printf("expected %ld, got %ld in test %d '%s'\n", (long) expect, (long) got,
+       test, buffer);
+  exit (test);
+}
+
+int main(int argc, char **argv) {
+  int test = 0;
+
+  check(sprintf(buffer, ""), ++test);
+  check(sprintf(buffer, "%s %s", "perl", "rules"), ++test);
+  check(sprintf(buffer, "I like %g", atan2(0,-1)), ++test);
+
+  return 0;
+}
+EOP
+set try
+
+d_sprintf_returns_strlen="$undef"
+if eval $compile; then
+    xxx="`$run ./try`"
+    case "$?" in
+       0) cat >&4 <<EOM
+sprintf returns the length of the string (as ANSI says it should)
+EOM
+       d_sprintf_returns_strlen="$define"
+       ;;
+       *) cat >&4 <<EOM
+sprintf does not return the length of the string (how old is this system?)
+EOM
+       d_sprintf_returns_strlen="$undef"
+        ;;
+    esac
+fi
+
+$rm -f try.* try
+