annotate gsm-fw/sprintf/old/doprnt.c.43tahoe @ 487:f612f9c3fe77

gsm-fw/gpf/conf: GSM and GPRS stack configurations imported from Leonardo
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 29 Jun 2014 20:13:43 +0000
parents 7e45ada9c365
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * Copyright (c) 1988 Regents of the University of California.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * All rights reserved.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 *
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 * Redistribution and use in source and binary forms are permitted
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 * provided that the above copyright notice and this paragraph are
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 * duplicated in all such forms and that any documentation,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 * advertising materials, and other materials related to such
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 * distribution and use acknowledge that the software was developed
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 * by the University of California, Berkeley. The name of the
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 * University may not be used to endorse or promote products derived
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 * from this software without specific prior written permission.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 #if defined(LIBC_SCCS) && !defined(lint)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 static char sccsid[] = "@(#)doprnt.c 5.35 (Berkeley) 6/27/88";
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 #endif /* LIBC_SCCS and not lint */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 #include <sys/types.h>
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 #include <varargs.h>
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 #include <stdio.h>
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 #include <ctype.h>
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 /* 11-bit exponent (VAX G floating point) is 308 decimal digits */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 #define MAXEXP 308
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 /* 128 bit fraction takes up 39 decimal digits; max reasonable precision */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 #define MAXFRACT 39
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 #define DEFPREC 6
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 #define PUTC(ch) (void) putc(ch, fp)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 #define ARG() \
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 _ulong = flags&LONGINT ? va_arg(argp, long) : \
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 flags&SHORTINT ? va_arg(argp, short) : va_arg(argp, int);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 #define todigit(c) ((c) - '0')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 #define tochar(n) ((n) + '0')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 /* have to deal with the negative buffer count kludge */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 #define NEGATIVE_COUNT_KLUDGE
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 #define LONGINT 0x01 /* long integer */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 #define LONGDBL 0x02 /* long double; unimplemented */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 #define SHORTINT 0x04 /* short integer */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 #define ALT 0x08 /* alternate form */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 #define LADJUST 0x10 /* left adjustment */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 #define ZEROPAD 0x20 /* zero (as opposed to blank) pad */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 #define HEXPREFIX 0x40 /* add 0x or 0X prefix */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 _doprnt(fmt0, argp, fp)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 u_char *fmt0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 va_list argp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 register FILE *fp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 register u_char *fmt; /* format string */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 register int ch; /* character from fmt */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 register int cnt; /* return value accumulator */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 register int n; /* random handy integer */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 register char *t; /* buffer pointer */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 double _double; /* double precision arguments %[eEfgG] */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 u_long _ulong; /* integer arguments %[diouxX] */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 int base; /* base for [diouxX] conversion */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 int dprec; /* decimal precision in [diouxX] */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 int fieldsz; /* field size expanded by sign, etc */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 int flags; /* flags as above */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 int fpprec; /* `extra' floating precision in [eEfgG] */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 int prec; /* precision from format (%.3d), or -1 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 int realsz; /* field size expanded by decimal precision */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 int size; /* size of converted field or string */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 int width; /* width from format (%8d), or 0 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 char sign; /* sign prefix (' ', '+', '-', or \0) */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 char softsign; /* temporary negative sign for floats */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 char *digs; /* digits for [diouxX] conversion */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 if (fp->_flag & _IORW) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 fp->_flag |= _IOWRT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 fp->_flag &= ~(_IOEOF|_IOREAD);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 if ((fp->_flag & _IOWRT) == 0)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 return (EOF);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 fmt = fmt0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 digs = "0123456789abcdef";
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 for (cnt = 0;; ++fmt) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 n = fp->_cnt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93 for (t = (char *)fp->_ptr; (ch = *fmt) && ch != '%';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 ++cnt, ++fmt)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 if (--n < 0
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 #ifdef NEGATIVE_COUNT_KLUDGE
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 && (!(fp->_flag & _IOLBF) || -n >= fp->_bufsiz)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 #endif
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 || ch == '\n' && fp->_flag & _IOLBF) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100 fp->_cnt = n;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 fp->_ptr = t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 (void) _flsbuf((u_char)ch, fp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 n = fp->_cnt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 t = (char *)fp->_ptr;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 } else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 *t++ = ch;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 fp->_cnt = n;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 fp->_ptr = t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 if (!ch)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110 return (cnt);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 flags = 0; dprec = 0; fpprec = 0; width = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113 prec = -1;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 sign = '\0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 rflag: switch (*++fmt) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 case ' ':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119 * ``If the space and + flags both appear, the space
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
120 * flag will be ignored.''
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
121 * -- ANSI X3J11
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
122 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
123 if (!sign)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
124 sign = ' ';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
125 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
126 case '#':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
127 flags |= ALT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
128 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
129 case '*':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
130 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
131 * ``A negative field width argument is taken as a
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
132 * - flag followed by a positive field width.''
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
133 * -- ANSI X3J11
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
134 * They don't exclude field widths read from args.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
135 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
136 if ((width = va_arg(argp, int)) >= 0)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
137 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
138 width = -width;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
139 /* FALLTHROUGH */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
140 case '-':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
141 flags |= LADJUST;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
142 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
143 case '+':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
144 sign = '+';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
145 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
146 case '.':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
147 if (*++fmt == '*')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
148 n = va_arg(argp, int);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
149 else {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
150 n = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
151 while (isascii(*fmt) && isdigit(*fmt))
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
152 n = 10 * n + todigit(*fmt++);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
153 --fmt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
154 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
155 prec = n < 0 ? -1 : n;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
156 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
157 case '0':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
158 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
159 * ``Note that 0 is taken as a flag, not as the
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
160 * beginning of a field width.''
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
161 * -- ANSI X3J11
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
162 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
163 flags |= ZEROPAD;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
164 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
165 case '1': case '2': case '3': case '4':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
166 case '5': case '6': case '7': case '8': case '9':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
167 n = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
168 do {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
169 n = 10 * n + todigit(*fmt);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
170 } while (isascii(*++fmt) && isdigit(*fmt));
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
171 width = n;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
172 --fmt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
173 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
174 case 'L':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
175 flags |= LONGDBL;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
176 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
177 case 'h':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
178 flags |= SHORTINT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
179 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
180 case 'l':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
181 flags |= LONGINT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
182 goto rflag;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
183 case 'c':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
184 *(t = buf) = va_arg(argp, int);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
185 size = 1;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
186 sign = '\0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
187 goto pforw;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
188 case 'D':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
189 flags |= LONGINT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
190 /*FALLTHROUGH*/
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
191 case 'd':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
192 case 'i':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
193 ARG();
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
194 if ((long)_ulong < 0) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
195 _ulong = -_ulong;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
196 sign = '-';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
197 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
198 base = 10;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
199 goto number;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
200 case 'e':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
201 case 'E':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
202 case 'f':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
203 case 'g':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
204 case 'G':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
205 _double = va_arg(argp, double);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
206 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
207 * don't do unrealistic precision; just pad it with
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
208 * zeroes later, so buffer size stays rational.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
209 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
210 if (prec > MAXFRACT) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
211 if (*fmt != 'g' && *fmt != 'G' || (flags&ALT))
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
212 fpprec = prec - MAXFRACT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
213 prec = MAXFRACT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
214 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
215 else if (prec == -1)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
216 prec = DEFPREC;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
217 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
218 * softsign avoids negative 0 if _double is < 0 and
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
219 * no significant digits will be shown
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
220 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
221 if (_double < 0) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
222 softsign = '-';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
223 _double = -_double;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
224 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
225 else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
226 softsign = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
227 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
228 * cvt may have to round up past the "start" of the
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
229 * buffer, i.e. ``intf("%.2f", (double)9.999);'';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
230 * if the first char isn't NULL, it did.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
231 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
232 *buf = NULL;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
233 size = cvt(_double, prec, flags, &softsign, *fmt, buf,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
234 buf + sizeof(buf));
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
235 if (softsign)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
236 sign = '-';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
237 t = *buf ? buf : buf + 1;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
238 goto pforw;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
239 case 'n':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
240 if (flags & LONGINT)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
241 *va_arg(argp, long *) = cnt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
242 else if (flags & SHORTINT)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
243 *va_arg(argp, short *) = cnt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
244 else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
245 *va_arg(argp, int *) = cnt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
246 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
247 case 'O':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
248 flags |= LONGINT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
249 /*FALLTHROUGH*/
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
250 case 'o':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
251 ARG();
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
252 base = 8;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
253 goto nosign;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
254 case 'p':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
255 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
256 * ``The argument shall be a pointer to void. The
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
257 * value of the pointer is converted to a sequence
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
258 * of printable characters, in an implementation-
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
259 * defined manner.''
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
260 * -- ANSI X3J11
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
261 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
262 /* NOSTRICT */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
263 _ulong = (u_long)va_arg(argp, void *);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
264 base = 16;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
265 goto nosign;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
266 case 's':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
267 if (!(t = va_arg(argp, char *)))
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
268 t = "(null)";
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
269 if (prec >= 0) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
270 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
271 * can't use strlen; can only look for the
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
272 * NUL in the first `prec' characters, and
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
273 * strlen() will go further.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
274 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
275 char *p, *memchr();
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
276
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
277 if (p = memchr(t, 0, prec)) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
278 size = p - t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
279 if (size > prec)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
280 size = prec;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
281 } else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
282 size = prec;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
283 } else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
284 size = strlen(t);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
285 sign = '\0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
286 goto pforw;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
287 case 'U':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
288 flags |= LONGINT;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
289 /*FALLTHROUGH*/
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
290 case 'u':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
291 ARG();
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
292 base = 10;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
293 goto nosign;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
294 case 'X':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
295 digs = "0123456789ABCDEF";
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
296 /* FALLTHROUGH */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
297 case 'x':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
298 ARG();
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
299 base = 16;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
300 /* leading 0x/X only if non-zero */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
301 if (flags & ALT && _ulong != 0)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
302 flags |= HEXPREFIX;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
303
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
304 /* unsigned conversions */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
305 nosign: sign = '\0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
306 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
307 * ``... diouXx conversions ... if a precision is
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
308 * specified, the 0 flag will be ignored.''
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
309 * -- ANSI X3J11
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
310 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
311 number: if ((dprec = prec) >= 0)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
312 flags &= ~ZEROPAD;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
313
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
314 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
315 * ``The result of converting a zero value with an
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
316 * explicit precision of zero is no characters.''
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
317 * -- ANSI X3J11
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
318 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
319 t = buf + BUF;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
320 if (_ulong != 0 || prec != 0) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
321 do {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
322 *--t = digs[_ulong % base];
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
323 _ulong /= base;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
324 } while (_ulong);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
325 digs = "0123456789abcdef";
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
326 if (flags & ALT && base == 8 && *t != '0')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
327 *--t = '0'; /* octal leading 0 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
328 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
329 size = buf + BUF - t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
330
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
331 pforw:
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
332 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
333 * All reasonable formats wind up here. At this point,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
334 * `t' points to a string which (if not flags&LADJUST)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
335 * should be padded out to `width' places. If
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
336 * flags&ZEROPAD, it should first be prefixed by any
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
337 * sign or other prefix; otherwise, it should be blank
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
338 * padded before the prefix is emitted. After any
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
339 * left-hand padding and prefixing, emit zeroes
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
340 * required by a decimal [diouxX] precision, then print
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
341 * the string proper, then emit zeroes required by any
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
342 * leftover floating precision; finally, if LADJUST,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
343 * pad with blanks.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
344 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
345
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
346 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
347 * compute actual size, so we know how much to pad
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
348 * fieldsz excludes decimal prec; realsz includes it
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
349 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
350 fieldsz = size + fpprec;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
351 if (sign)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
352 fieldsz++;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
353 if (flags & HEXPREFIX)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
354 fieldsz += 2;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
355 realsz = dprec > fieldsz ? dprec : fieldsz;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
356
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
357 /* right-adjusting blank padding */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
358 if ((flags & (LADJUST|ZEROPAD)) == 0 && width)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
359 for (n = realsz; n < width; n++)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
360 PUTC(' ');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
361 /* prefix */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
362 if (sign)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
363 PUTC(sign);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
364 if (flags & HEXPREFIX) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
365 PUTC('0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
366 PUTC((char)*fmt);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
367 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
368 /* right-adjusting zero padding */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
369 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
370 for (n = realsz; n < width; n++)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
371 PUTC('0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
372 /* leading zeroes from decimal precision */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
373 for (n = fieldsz; n < dprec; n++)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
374 PUTC('0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
375
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
376 /* the string or number proper */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
377 if (fp->_cnt - (n = size) >= 0 &&
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
378 (fp->_flag & _IOLBF) == 0) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
379 fp->_cnt -= n;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
380 bcopy(t, (char *)fp->_ptr, n);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
381 fp->_ptr += n;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
382 } else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
383 while (--n >= 0)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
384 PUTC(*t++);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
385 /* trailing f.p. zeroes */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
386 while (--fpprec >= 0)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
387 PUTC('0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
388 /* left-adjusting padding (always blank) */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
389 if (flags & LADJUST)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
390 for (n = realsz; n < width; n++)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
391 PUTC(' ');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
392 /* finally, adjust cnt */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
393 cnt += width > realsz ? width : realsz;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
394 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
395 case '\0': /* "%?" prints ?, unless ? is NULL */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
396 return (cnt);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
397 default:
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
398 PUTC((char)*fmt);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
399 cnt++;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
400 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
401 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
402 /* NOTREACHED */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
403 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
404
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
405 static
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
406 cvt(number, prec, flags, signp, fmtch, startp, endp)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
407 double number;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
408 register int prec;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
409 int flags;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
410 u_char fmtch;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
411 char *signp, *startp, *endp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
412 {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
413 register char *p, *t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
414 register double fract;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
415 int dotrim, expcnt, gformat;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
416 double integer, tmp, modf();
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
417 char *exponent(), *round();
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
418
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
419 dotrim = expcnt = gformat = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
420 fract = modf(number, &integer);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
421
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
422 /* get an extra slot for rounding. */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
423 t = ++startp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
424
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
425 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
426 * get integer portion of number; put into the end of the buffer; the
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
427 * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
428 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
429 for (p = endp - 1; integer; ++expcnt) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
430 tmp = modf(integer / 10, &integer);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
431 *p-- = tochar((int)((tmp + .01) * 10));
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
432 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
433 switch(fmtch) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
434 case 'f':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
435 /* reverse integer into beginning of buffer */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
436 if (expcnt)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
437 for (; ++p < endp; *t++ = *p);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
438 else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
439 *t++ = '0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
440 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
441 * if precision required or alternate flag set, add in a
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
442 * decimal point.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
443 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
444 if (prec || flags&ALT)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
445 *t++ = '.';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
446 /* if requires more precision and some fraction left */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
447 if (fract) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
448 if (prec)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
449 do {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
450 fract = modf(fract * 10, &tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
451 *t++ = tochar((int)tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
452 } while (--prec && fract);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
453 if (fract)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
454 startp = round(fract, (int *)NULL, startp,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
455 t - 1, (char)0, signp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
456 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
457 for (; prec--; *t++ = '0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
458 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
459 case 'e':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
460 case 'E':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
461 eformat: if (expcnt) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
462 *t++ = *++p;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
463 if (prec || flags&ALT)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
464 *t++ = '.';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
465 /* if requires more precision and some integer left */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
466 for (; prec && ++p < endp; --prec)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
467 *t++ = *p;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
468 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
469 * if done precision and more of the integer component,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
470 * round using it; adjust fract so we don't re-round
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
471 * later.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
472 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
473 if (!prec && ++p < endp) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
474 fract = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
475 startp = round((double)0, &expcnt, startp,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
476 t - 1, *p, signp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
477 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
478 /* adjust expcnt for digit in front of decimal */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
479 --expcnt;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
480 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
481 /* until first fractional digit, decrement exponent */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
482 else if (fract) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
483 /* adjust expcnt for digit in front of decimal */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
484 for (expcnt = -1;; --expcnt) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
485 fract = modf(fract * 10, &tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
486 if (tmp)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
487 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
488 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
489 *t++ = tochar((int)tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
490 if (prec || flags&ALT)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
491 *t++ = '.';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
492 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
493 else {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
494 *t++ = '0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
495 if (prec || flags&ALT)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
496 *t++ = '.';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
497 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
498 /* if requires more precision and some fraction left */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
499 if (fract) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
500 if (prec)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
501 do {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
502 fract = modf(fract * 10, &tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
503 *t++ = tochar((int)tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
504 } while (--prec && fract);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
505 if (fract)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
506 startp = round(fract, &expcnt, startp,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
507 t - 1, (char)0, signp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
508 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
509 /* if requires more precision */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
510 for (; prec--; *t++ = '0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
511
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
512 /* unless alternate flag, trim any g/G format trailing 0's */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
513 if (gformat && !(flags&ALT)) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
514 while (t > startp && *--t == '0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
515 if (*t == '.')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
516 --t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
517 ++t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
518 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
519 t = exponent(t, expcnt, fmtch);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
520 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
521 case 'g':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
522 case 'G':
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
523 /* a precision of 0 is treated as a precision of 1. */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
524 if (!prec)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
525 ++prec;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
526 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
527 * ``The style used depends on the value converted; style e
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
528 * will be used only if the exponent resulting from the
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
529 * conversion is less than -4 or greater than the precision.''
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
530 * -- ANSI X3J11
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
531 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
532 if (expcnt > prec || !expcnt && fract && fract < .0001) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
533 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
534 * g/G format counts "significant digits, not digits of
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
535 * precision; for the e/E format, this just causes an
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
536 * off-by-one problem, i.e. g/G considers the digit
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
537 * before the decimal point significant and e/E doesn't
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
538 * count it as precision.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
539 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
540 --prec;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
541 fmtch -= 2; /* G->E, g->e */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
542 gformat = 1;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
543 goto eformat;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
544 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
545 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
546 * reverse integer into beginning of buffer,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
547 * note, decrement precision
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
548 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
549 if (expcnt)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
550 for (; ++p < endp; *t++ = *p, --prec);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
551 else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
552 *t++ = '0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
553 /*
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
554 * if precision required or alternate flag set, add in a
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
555 * decimal point. If no digits yet, add in leading 0.
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
556 */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
557 if (prec || flags&ALT) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
558 dotrim = 1;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
559 *t++ = '.';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
560 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
561 else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
562 dotrim = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
563 /* if requires more precision and some fraction left */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
564 if (fract) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
565 if (prec) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
566 do {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
567 fract = modf(fract * 10, &tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
568 *t++ = tochar((int)tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
569 } while(!tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
570 while (--prec && fract) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
571 fract = modf(fract * 10, &tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
572 *t++ = tochar((int)tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
573 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
574 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
575 if (fract)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
576 startp = round(fract, (int *)NULL, startp,
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
577 t - 1, (char)0, signp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
578 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
579 /* alternate format, adds 0's for precision, else trim 0's */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
580 if (flags&ALT)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
581 for (; prec--; *t++ = '0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
582 else if (dotrim) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
583 while (t > startp && *--t == '0');
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
584 if (*t != '.')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
585 ++t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
586 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
587 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
588 return(t - startp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
589 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
590
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
591 static char *
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
592 round(fract, exp, start, end, ch, signp)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
593 double fract;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
594 int *exp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
595 register char *start, *end;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
596 char ch, *signp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
597 {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
598 double tmp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
599
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
600 if (fract)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
601 (void)modf(fract * 10, &tmp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
602 else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
603 tmp = todigit(ch);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
604 if (tmp > 4)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
605 for (;; --end) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
606 if (*end == '.')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
607 --end;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
608 if (++*end <= '9')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
609 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
610 *end = '0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
611 if (end == start) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
612 if (exp) { /* e/E; increment exponent */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
613 *end = '1';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
614 ++*exp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
615 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
616 else { /* f; add extra digit */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
617 *--end = '1';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
618 --start;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
619 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
620 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
621 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
622 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
623 /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
624 else if (*signp == '-')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
625 for (;; --end) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
626 if (*end == '.')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
627 --end;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
628 if (*end != '0')
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
629 break;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
630 if (end == start)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
631 *signp = 0;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
632 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
633 return(start);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
634 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
635
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
636 static char *
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
637 exponent(p, exp, fmtch)
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
638 register char *p;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
639 register int exp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
640 u_char fmtch;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
641 {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
642 register char *t;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
643 char expbuf[MAXEXP];
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
644
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
645 *p++ = fmtch;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
646 if (exp < 0) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
647 exp = -exp;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
648 *p++ = '-';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
649 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
650 else
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
651 *p++ = '+';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
652 t = expbuf + MAXEXP;
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
653 if (exp > 9) {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
654 do {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
655 *--t = tochar(exp % 10);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
656 } while ((exp /= 10) > 9);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
657 *--t = tochar(exp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
658 for (; t < expbuf + MAXEXP; *p++ = *t++);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
659 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
660 else {
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
661 *p++ = '0';
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
662 *p++ = tochar(exp);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
663 }
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
664 return(p);
7e45ada9c365 gsm-fw: sprintf overhaul in preparation for adding %f format support
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
665 }