Remove 'grp' and merge into 'nss' and 'posix'
[platform/upstream/glibc.git] / misc / efgcvt-template.c
1 /* Compatibility functions for floating point formatting.
2    Copyright (C) 1995-2023 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #include <math.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/param.h>
23 #include <libc-lock.h>
24 #include <math_ldbl_opt.h>
25 #include <set-freeres.h>
26
27 #ifndef SPRINTF
28 # define SPRINTF sprintf
29 #endif
30
31 #define APPEND(a, b) APPEND2 (a, b)
32 #define APPEND2(a, b) a##b
33
34
35 #define FCVT_BUFFER APPEND (FUNC_PREFIX, fcvt_buffer)
36 #define FCVT_BUFPTR APPEND (FUNC_PREFIX, fcvt_bufptr)
37 #define ECVT_BUFFER APPEND (FUNC_PREFIX, ecvt_buffer)
38
39
40 static char FCVT_BUFFER[MAXDIG];
41 static char ECVT_BUFFER[MAXDIG];
42 static char *FCVT_BUFPTR;
43
44 char *
45 __FCVT (FLOAT_TYPE value, int ndigit, int *decpt, int *sign)
46 {
47   if (FCVT_BUFPTR == NULL)
48     {
49       if (__FCVT_R (value, ndigit, decpt, sign, FCVT_BUFFER, MAXDIG) != -1)
50         return FCVT_BUFFER;
51
52       FCVT_BUFPTR = (char *) malloc (FCVT_MAXDIG);
53       if (FCVT_BUFPTR == NULL)
54         return FCVT_BUFFER;
55     }
56
57   (void) __FCVT_R (value, ndigit, decpt, sign, FCVT_BUFPTR, FCVT_MAXDIG);
58
59   return FCVT_BUFPTR;
60 }
61
62
63 char *
64 __ECVT (FLOAT_TYPE value, int ndigit, int *decpt, int *sign)
65 {
66   (void) __ECVT_R (value, ndigit, decpt, sign, ECVT_BUFFER, MAXDIG);
67
68   return ECVT_BUFFER;
69 }
70
71 char *
72 __GCVT (FLOAT_TYPE value, int ndigit, char *buf)
73 {
74   SPRINTF (buf, "%.*" FLOAT_FMT_FLAG "g", MIN (ndigit, NDIGIT_MAX), value);
75   return buf;
76 }
77
78 weak_alias (FCVT_BUFPTR, __EFGCVT_FREEMEM_PTR);