xref: /netbsd-src/lib/libc/gdtoa/g__fmt.c (revision 374ecd4b4c804d95399b3e6adc00aa721086bc65)
17684d5e0Skleink /****************************************************************
27684d5e0Skleink 
37684d5e0Skleink The author of this software is David M. Gay.
47684d5e0Skleink 
57684d5e0Skleink Copyright (C) 1998 by Lucent Technologies
67684d5e0Skleink All Rights Reserved
77684d5e0Skleink 
87684d5e0Skleink Permission to use, copy, modify, and distribute this software and
97684d5e0Skleink its documentation for any purpose and without fee is hereby
107684d5e0Skleink granted, provided that the above copyright notice appear in all
117684d5e0Skleink copies and that both that the copyright notice and this
127684d5e0Skleink permission notice and warranty disclaimer appear in supporting
137684d5e0Skleink documentation, and that the name of Lucent or any of its entities
147684d5e0Skleink not be used in advertising or publicity pertaining to
157684d5e0Skleink distribution of the software without specific, written prior
167684d5e0Skleink permission.
177684d5e0Skleink 
187684d5e0Skleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
197684d5e0Skleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
207684d5e0Skleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
217684d5e0Skleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
227684d5e0Skleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
237684d5e0Skleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
247684d5e0Skleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
257684d5e0Skleink THIS SOFTWARE.
267684d5e0Skleink 
277684d5e0Skleink ****************************************************************/
287684d5e0Skleink 
297684d5e0Skleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
307684d5e0Skleink  * with " at " changed at "@" and " dot " changed to ".").	*/
317684d5e0Skleink 
327684d5e0Skleink #include "gdtoaimp.h"
337684d5e0Skleink 
347684d5e0Skleink #ifdef USE_LOCALE
357684d5e0Skleink #include "locale.h"
367684d5e0Skleink #endif
377684d5e0Skleink 
387684d5e0Skleink  char *
397684d5e0Skleink #ifdef KR_headers
g__fmt(b,s,se,decpt,sign,blen)40185969ddSchristos g__fmt(b, s, se, decpt, sign, blen) char *b; char *s; char *se; int decpt; ULong sign; size_t blen;
417684d5e0Skleink #else
42185969ddSchristos g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen)
437684d5e0Skleink #endif
447684d5e0Skleink {
457684d5e0Skleink 	int i, j, k;
46185969ddSchristos 	char *be, *s0;
47185969ddSchristos 	size_t len;
487684d5e0Skleink #ifdef USE_LOCALE
49185969ddSchristos #ifdef NO_LOCALE_CACHE
50185969ddSchristos 	char *decimalpoint = localeconv()->decimal_point;
51185969ddSchristos 	size_t dlen = strlen(decimalpoint);
527684d5e0Skleink #else
53185969ddSchristos 	char *decimalpoint;
54185969ddSchristos 	static char *decimalpoint_cache;
55185969ddSchristos 	static size_t dlen;
56185969ddSchristos 	if (!(s0 = decimalpoint_cache)) {
57185969ddSchristos 		s0 = localeconv()->decimal_point;
58185969ddSchristos 		dlen = strlen(s0);
59*374ecd4bSchristos 		if ((decimalpoint_cache = MALLOC(strlen(s0) + 1)) != NULL) {
60185969ddSchristos 			strcpy(decimalpoint_cache, s0);
61185969ddSchristos 			s0 = decimalpoint_cache;
62185969ddSchristos 			}
63185969ddSchristos 		}
64185969ddSchristos 	decimalpoint = s0;
657684d5e0Skleink #endif
66185969ddSchristos #else
67185969ddSchristos #define dlen 0
68185969ddSchristos #endif
69185969ddSchristos 	s0 = s;
70185969ddSchristos 	len = (se-s) + dlen + 6; /* 6 = sign + e+dd + trailing null */
71185969ddSchristos 	if (blen < len)
72185969ddSchristos 		goto ret0;
73185969ddSchristos 	be = b + blen - 1;
747684d5e0Skleink 	if (sign)
757684d5e0Skleink 		*b++ = '-';
767684d5e0Skleink 	if (decpt <= -4 || decpt > se - s + 5) {
777684d5e0Skleink 		*b++ = *s++;
787684d5e0Skleink 		if (*s) {
79185969ddSchristos #ifdef USE_LOCALE
80185969ddSchristos 			while((*b = *decimalpoint++))
81185969ddSchristos 				++b;
82185969ddSchristos #else
83185969ddSchristos 			*b++ = '.';
84185969ddSchristos #endif
857684d5e0Skleink 			while((*b = *s++) !=0)
867684d5e0Skleink 				b++;
877684d5e0Skleink 			}
887684d5e0Skleink 		*b++ = 'e';
897684d5e0Skleink 		/* sprintf(b, "%+.2d", decpt - 1); */
907684d5e0Skleink 		if (--decpt < 0) {
917684d5e0Skleink 			*b++ = '-';
927684d5e0Skleink 			decpt = -decpt;
937684d5e0Skleink 			}
947684d5e0Skleink 		else
957684d5e0Skleink 			*b++ = '+';
967684d5e0Skleink 		for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
977684d5e0Skleink 		for(;;) {
987684d5e0Skleink 			i = decpt / k;
99185969ddSchristos 			if (b >= be)
100185969ddSchristos 				goto ret0;
1017684d5e0Skleink 			*b++ = i + '0';
1027684d5e0Skleink 			if (--j <= 0)
1037684d5e0Skleink 				break;
1047684d5e0Skleink 			decpt -= i*k;
1057684d5e0Skleink 			decpt *= 10;
1067684d5e0Skleink 			}
1077684d5e0Skleink 		*b = 0;
1087684d5e0Skleink 		}
1097684d5e0Skleink 	else if (decpt <= 0) {
110185969ddSchristos #ifdef USE_LOCALE
111185969ddSchristos 		while((*b = *decimalpoint++))
112185969ddSchristos 			++b;
113185969ddSchristos #else
114185969ddSchristos 		*b++ = '.';
115185969ddSchristos #endif
116185969ddSchristos 		if (be < b - decpt + (se - s))
117185969ddSchristos 			goto ret0;
1187684d5e0Skleink 		for(; decpt < 0; decpt++)
1197684d5e0Skleink 			*b++ = '0';
1207684d5e0Skleink 		while((*b = *s++) != 0)
1217684d5e0Skleink 			b++;
1227684d5e0Skleink 		}
1237684d5e0Skleink 	else {
1247684d5e0Skleink 		while((*b = *s++) != 0) {
1257684d5e0Skleink 			b++;
126185969ddSchristos 			if (--decpt == 0 && *s) {
127185969ddSchristos #ifdef USE_LOCALE
128185969ddSchristos 				while(*b = *decimalpoint++)
129185969ddSchristos 					++b;
130185969ddSchristos #else
131185969ddSchristos 				*b++ = '.';
132185969ddSchristos #endif
133185969ddSchristos 				}
134185969ddSchristos 			}
135185969ddSchristos 		if (b + decpt > be) {
136185969ddSchristos  ret0:
137185969ddSchristos 			b = 0;
138185969ddSchristos 			goto ret;
1397684d5e0Skleink 			}
1407684d5e0Skleink 		for(; decpt > 0; decpt--)
1417684d5e0Skleink 			*b++ = '0';
1427684d5e0Skleink 		*b = 0;
1437684d5e0Skleink 		}
144185969ddSchristos  ret:
1457684d5e0Skleink 	freedtoa(s0);
1467684d5e0Skleink 	return b;
1477684d5e0Skleink  	}
148