1 /*
2 * Copyright (c) 2012-2014 ARM Ltd
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the company may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 #define _DEFAULT_SOURCE
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <stdint.h>
36 #include <wchar.h>
37 #include <sys/lock.h>
38 #include <stdarg.h>
39 #include "local.h"
40 #include "../stdlib/local.h"
41 #include "fvwrite.h"
42 #include "vfieeefp.h"
43 #include "nano-vfprintf_local.h"
44
45 /* Decode and print non-floating point data. */
46 int
_printf_common(struct _prt_data_t * pdata,int * realsz,FILE * fp,int (* pfunc)(FILE *,const char *,size_t len))47 _printf_common (
48 struct _prt_data_t *pdata,
49 int *realsz,
50 FILE *fp,
51 int (*pfunc)(FILE *,
52 const char *, size_t len))
53 {
54 int n;
55 /*
56 * All reasonable formats wind up here. At this point, `cp'
57 * points to a string which (if not flags&LADJUST) should be
58 * padded out to `width' places. If flags&ZEROPAD, it should
59 * first be prefixed by any sign or other prefix; otherwise,
60 * it should be blank padded before the prefix is emitted.
61 * After any left-hand padding and prefixing, emit zeroes
62 * required by a decimal [diouxX] precision, then print the
63 * string proper, then emit zeroes required by any leftover
64 * floating precision; finally, if LADJUST, pad with blanks.
65 * If flags&FPT, ch must be in [aAeEfg].
66 *
67 * Compute actual size, so we know how much to pad.
68 * size excludes decimal prec; realsz includes it.
69 */
70 *realsz = pdata->dprec > pdata->size ? pdata->dprec : pdata->size;
71 if (pdata->l_buf[0])
72 (*realsz)++;
73
74 if (pdata->flags & HEXPREFIX)
75 *realsz += 2;
76
77 /* Right-adjusting blank padding. */
78 if ((pdata->flags & (LADJUST|ZEROPAD)) == 0)
79 PAD (pdata->width - *realsz, pdata->blank);
80
81 /* Prefix. */
82 n = 0;
83 if (pdata->l_buf[0])
84 n++;
85
86 if (pdata->flags & HEXPREFIX)
87 {
88 pdata->l_buf[n++] = '0';
89 pdata->l_buf[n++] = pdata->l_buf[2];
90 }
91
92 PRINT (pdata->l_buf, n);
93 n = pdata->width - *realsz;
94 if ((pdata->flags & (LADJUST|ZEROPAD)) != ZEROPAD || n < 0)
95 n = 0;
96
97 if (pdata->dprec > pdata->size)
98 n += pdata->dprec - pdata->size;
99
100 PAD (n, pdata->zero);
101 return 0;
102 error:
103 return -1;
104 }
105 int
_printf_i(struct _prt_data_t * pdata,FILE * fp,int (* pfunc)(FILE *,const char *,size_t len),va_list * ap)106 _printf_i (struct _prt_data_t *pdata, FILE *fp,
107 int (*pfunc)(FILE *, const char *, size_t len),
108 va_list *ap)
109 {
110 /* Field size expanded by dprec. */
111 int realsz;
112 u_quad_t _uquad;
113 int base;
114 int n;
115 char *cp = pdata->buf + BUF;
116 char *xdigs = "0123456789ABCDEF";
117
118 /* Decoding the conversion specifier. */
119 switch (pdata->code)
120 {
121 case 'c':
122 *--cp = GET_ARG (N, *ap, int);
123 pdata->size = 1;
124 goto non_number_nosign;
125 case 'd':
126 case 'i':
127 _uquad = SARG (pdata->flags);
128 if ((long) _uquad < 0)
129 {
130 _uquad = -_uquad;
131 pdata->l_buf[0] = '-';
132 }
133 base = 10;
134 goto number;
135 case 'u':
136 case 'o':
137 _uquad = UARG (pdata->flags);
138 base = (pdata->code == 'o') ? 8 : 10;
139 goto nosign;
140 case 'X':
141 pdata->l_buf[2] = 'X';
142 goto hex;
143 case 'p':
144 /*
145 * ``The argument shall be a pointer to void. The
146 * value of the pointer is converted to a sequence
147 * of printable characters, in an implementation-
148 * defined manner.''
149 * -- ANSI X3J11
150 */
151 pdata->flags |= HEXPREFIX;
152 if (sizeof (void*) > sizeof (int))
153 pdata->flags |= LONGINT;
154 __PICOLIBC_FALLTHROUGH;
155 /* NOSTRICT. */
156 case 'x':
157 pdata->l_buf[2] = 'x';
158 xdigs = "0123456789abcdef";
159 hex:
160 _uquad = UARG (pdata->flags);
161 base = 16;
162 if (pdata->flags & ALT)
163 pdata->flags |= HEXPREFIX;
164
165 /* Leading 0x/X only if non-zero. */
166 if (_uquad == 0)
167 pdata->flags &= ~HEXPREFIX;
168
169 /* Unsigned conversions. */
170 nosign:
171 pdata->l_buf[0] = '\0';
172 /*
173 * ``... diouXx conversions ... if a precision is
174 * specified, the 0 flag will be ignored.''
175 * -- ANSI X3J11
176 */
177 number:
178 if ((pdata->dprec = pdata->prec) >= 0)
179 pdata->flags &= ~ZEROPAD;
180
181 /*
182 * ``The result of converting a zero value with an
183 * explicit precision of zero is no characters.''
184 * -- ANSI X3J11
185 */
186 if (_uquad != 0 || pdata->prec != 0)
187 {
188 do
189 {
190 *--cp = xdigs[_uquad % base];
191 _uquad /= base;
192 }
193 while (_uquad);
194 }
195 /* For 'o' conversion, '#' increases the precision to force the first
196 digit of the result to be zero. */
197 if (base == 8 && (pdata->flags & ALT) && pdata->prec <= pdata->size)
198 *--cp = '0';
199
200 pdata->size = pdata->buf + BUF - cp;
201 break;
202 case 'n':
203 if (pdata->flags & LONGINT)
204 *GET_ARG (N, *ap, long_ptr_t) = pdata->ret;
205 else if (pdata->flags & SHORTINT)
206 *GET_ARG (N, *ap, short_ptr_t) = pdata->ret;
207 else
208 *GET_ARG (N, *ap, int_ptr_t) = pdata->ret;
209 __PICOLIBC_FALLTHROUGH;
210 case '\0':
211 pdata->size = 0;
212 break;
213 case 's':
214 cp = GET_ARG (N, *ap, char_ptr_t);
215 /* Precision gives the maximum number of chars to be written from a
216 string, and take prec == -1 into consideration.
217 Use normal Newlib approach here to support case where cp is not
218 nul-terminated. */
219 if (cp == NULL)
220 cp = "(null)";
221 char *p = memchr (cp, 0, pdata->prec);
222
223 if (p != NULL)
224 pdata->prec = p - cp;
225
226 pdata->size = pdata->prec;
227 goto non_number_nosign;
228 default:
229 /* "%?" prints ?, unless ? is NUL. */
230 /* Pretend it was %c with argument ch. */
231 *--cp = pdata->code;
232 pdata->size = 1;
233 non_number_nosign:
234 pdata->l_buf[0] = '\0';
235 break;
236 }
237
238 /* Output. */
239 n = _printf_common (pdata, &realsz, fp, pfunc);
240 if (n == -1)
241 goto error;
242
243 PRINT (cp, pdata->size);
244 /* Left-adjusting padding (always blank). */
245 if (pdata->flags & LADJUST)
246 PAD (pdata->width - realsz, pdata->blank);
247
248 return (pdata->width > realsz ? pdata->width : realsz);
249 error:
250 return -1;
251 }
252
253