Update.
[glibc.git] / misc / efgcvt.c
blob8c92766c638b73d45ffe795ed6e53632e2272e7a
1 /* Compatibility functions for floating point formatting.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <float.h>
23 #include <bits/libc-lock.h>
25 #ifndef FLOAT_TYPE
26 # define FLOAT_TYPE double
27 # define FUNC_PREFIX
28 # define FLOAT_FMT_FLAG
29 /* Actually we have to write (DBL_DIG + log10 (DBL_MAX_10_EXP)) but we
30 don't have log10 available in the preprocessor. */
31 # define MAXDIG (DBL_DIG + 3)
32 #endif
34 #define APPEND(a, b) APPEND2 (a, b)
35 #define APPEND2(a, b) a##b
38 #define FCVT_BUFFER APPEND (FUNC_PREFIX, fcvt_buffer)
39 #define ECVT_BUFFER APPEND (FUNC_PREFIX, ecvt_buffer)
42 static char *FCVT_BUFFER;
43 static char *ECVT_BUFFER;
46 static void
47 APPEND (FUNC_PREFIX, fcvt_allocate) (void)
49 FCVT_BUFFER = (char *) malloc (MAXDIG);
52 char *
53 APPEND (FUNC_PREFIX, fcvt) (value, ndigit, decpt, sign)
54 FLOAT_TYPE value;
55 int ndigit, *decpt, *sign;
57 __libc_once_define (static, once);
58 __libc_once (once, APPEND (FUNC_PREFIX, fcvt_allocate));
60 if (FCVT_BUFFER == NULL)
61 /* If no core is available we don't have a chance to run the
62 program successfully and so returning NULL is an acceptable
63 result. */
64 return NULL;
66 (void) APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
67 FCVT_BUFFER, MAXDIG);
69 return FCVT_BUFFER;
73 static void
74 APPEND (FUNC_PREFIX, ecvt_allocate) (void)
76 ECVT_BUFFER = (char *) malloc (MAXDIG);
79 char *
80 APPEND (FUNC_PREFIX, ecvt) (value, ndigit, decpt, sign)
81 FLOAT_TYPE value;
82 int ndigit, *decpt, *sign;
84 __libc_once_define (static, once);
85 __libc_once (once, APPEND (FUNC_PREFIX, ecvt_allocate));
87 if (ECVT_BUFFER == NULL)
88 /* If no core is available we don't have a chance to run the
89 program successfully and so returning NULL is an acceptable
90 result. */
91 return NULL;
93 (void) APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign,
94 ECVT_BUFFER, MAXDIG);
96 return ECVT_BUFFER;
99 char *
100 APPEND (FUNC_PREFIX, gcvt) (value, ndigit, buf)
101 FLOAT_TYPE value;
102 int ndigit;
103 char *buf;
105 sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", ndigit, value);
106 return buf;
110 /* Make sure the memory is freed if the programs ends while in
111 memory-debugging mode and something actually was allocated. */
112 static void
113 __attribute__ ((unused))
114 free_mem (void)
116 if (FCVT_BUFFER != NULL)
117 free (FCVT_BUFFER);
118 if (ECVT_BUFFER != NULL)
119 free (ECVT_BUFFER);
122 text_set_element (__libc_subfreeres, free_mem);