some coverity fixes.
[minix.git] / lib / libc / gdtoa / g__fmt.c
blobde85452b5895e9e0cb8de48ce8ca1b38b03d6c8d
1 /* $NetBSD: g__fmt.c,v 1.1.1.1 2006/01/25 15:18:41 kleink Exp $ */
3 /****************************************************************
5 The author of this software is David M. Gay.
7 Copyright (C) 1998 by Lucent Technologies
8 All Rights Reserved
10 Permission to use, copy, modify, and distribute this software and
11 its documentation for any purpose and without fee is hereby
12 granted, provided that the above copyright notice appear in all
13 copies and that both that the copyright notice and this
14 permission notice and warranty disclaimer appear in supporting
15 documentation, and that the name of Lucent or any of its entities
16 not be used in advertising or publicity pertaining to
17 distribution of the software without specific, written prior
18 permission.
20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 THIS SOFTWARE.
29 ****************************************************************/
31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
32 * with " at " changed at "@" and " dot " changed to "."). */
34 #include "gdtoaimp.h"
36 #ifdef USE_LOCALE
37 #include "locale.h"
38 #endif
40 char *
41 #ifdef KR_headers
42 g__fmt(b, s, se, decpt, sign) char *b; char *s; char *se; int decpt; ULong sign;
43 #else
44 g__fmt(char *b, char *s, char *se, int decpt, ULong sign)
45 #endif
47 int i, j, k;
48 char *s0 = s;
49 #ifdef USE_LOCALE
50 char decimalpoint = *localeconv()->decimal_point;
51 #else
52 #define decimalpoint '.'
53 #endif
54 if (sign)
55 *b++ = '-';
56 if (decpt <= -4 || decpt > se - s + 5) {
57 *b++ = *s++;
58 if (*s) {
59 *b++ = decimalpoint;
60 while((*b = *s++) !=0)
61 b++;
63 *b++ = 'e';
64 /* sprintf(b, "%+.2d", decpt - 1); */
65 if (--decpt < 0) {
66 *b++ = '-';
67 decpt = -decpt;
69 else
70 *b++ = '+';
71 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
72 for(;;) {
73 i = decpt / k;
74 *b++ = i + '0';
75 if (--j <= 0)
76 break;
77 decpt -= i*k;
78 decpt *= 10;
80 *b = 0;
82 else if (decpt <= 0) {
83 *b++ = decimalpoint;
84 for(; decpt < 0; decpt++)
85 *b++ = '0';
86 while((*b = *s++) !=0)
87 b++;
89 else {
90 while((*b = *s++) !=0) {
91 b++;
92 if (--decpt == 0 && *s)
93 *b++ = decimalpoint;
95 for(; decpt > 0; decpt--)
96 *b++ = '0';
97 *b = 0;
99 freedtoa(s0);
100 return b;