Skip to content

Commit e060e1d

Browse files
committed
stdio: Make legacy nano printf code at least compile
Remove 'struct _reent' dregs. Add __nothrow as needed to stdio.h. Clean up some simple compiler warnings about undefined behavior. There are many gaps in standards conformance with this; making it pass the test suite would be a significant effort. But, at least it builds again. Signed-off-by: Keith Packard <[email protected]>
1 parent 51a8b32 commit e060e1d

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

newlib/libc/stdio/nano-vfprintf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ VFPRINTF (
617617
n = 0;
618618
}
619619
else
620-
n = _printf_float (data, &prt_data, fp, pfunc, &ap_copy);
620+
n = _printf_float (&prt_data, fp, pfunc, &ap_copy);
621621
}
622622
else
623623
#endif

newlib/libc/stdio/nano-vfprintf_float.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ int __exponent (char *p0, int exp, int fmtch);
6363
[aAeEfFgG]; if it is [aA], then the return string lives in BUF,
6464
otherwise the return value shares the mprec reentrant storage. */
6565
char *
66-
__cvt (struct _reent *data, _PRINTF_FLOAT_TYPE value, int ndigits, int flags,
66+
__cvt (_PRINTF_FLOAT_TYPE value, int ndigits, int flags,
6767
char *sign, int *decpt, int ch, int *length, char *buf)
6868
{
6969
int mode, dsgn;
7070
char *digits, *bp, *rve;
7171
union double_union tmp;
7272

73+
(void) buf;
7374
tmp.d = value;
7475
/* This will check for "< 0" and "-0.0". */
7576
if (word0 (tmp) & Sign_bit)
@@ -160,10 +161,9 @@ __exponent (char *p0, int exp, int fmtch)
160161

161162
/* Decode and print floating point number specified by "eEfgG". */
162163
int
163-
_printf_float (struct _reent *data,
164-
struct _prt_data_t *pdata,
164+
_printf_float (struct _prt_data_t *pdata,
165165
FILE * fp,
166-
int (*pfunc) (struct _reent *, FILE *, const char *,
166+
int (*pfunc) (FILE *, const char *,
167167
size_t len), va_list * ap)
168168
{
169169
#define _fpvalue (pdata->_double_)
@@ -178,8 +178,7 @@ _printf_float (struct _reent *data,
178178
int expsize = 0;
179179
/* Actual number of digits returned by cvt. */
180180
int ndig = 0;
181-
char *cp;
182-
int n;
181+
char *cp = NULL;
183182
/* Field size expanded by dprec(not for _printf_float). */
184183
int realsz;
185184
char code = pdata->code;
@@ -234,7 +233,7 @@ _printf_float (struct _reent *data,
234233

235234
pdata->flags |= FPT;
236235

237-
cp = __cvt (data, _fpvalue, pdata->prec, pdata->flags, &softsign,
236+
cp = __cvt (_fpvalue, pdata->prec, pdata->flags, &softsign,
238237
&expt, code, &ndig, cp);
239238

240239
if (code == 'g' || code == 'G')
@@ -285,7 +284,7 @@ _printf_float (struct _reent *data,
285284
if (softsign)
286285
pdata->l_buf[0] = '-';
287286
print_float:
288-
if (_printf_common (data, pdata, &realsz, fp, pfunc) == -1)
287+
if (_printf_common (pdata, &realsz, fp, pfunc) == -1)
289288
goto error;
290289

291290
if ((pdata->flags & FPT) == 0)

newlib/libc/stdio/nano-vfscanf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ _SVFSCANF (
397397
ret = _scanf_i (&scan_data, fp, &ap_copy);
398398
#ifdef __IO_FLOATING_POINT
399399
else if (_scanf_float)
400-
ret = _scanf_float (rptr, &scan_data, fp, &ap_copy);
400+
ret = _scanf_float (&scan_data, fp, &ap_copy);
401401
#endif
402402

403403
if (ret == MATCH_FAILURE)

newlib/libc/stdio/nano-vfscanf_float.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232

3333
#ifdef __IO_FLOATING_POINT
3434
int
35-
_scanf_float (struct _reent *rptr,
36-
struct _scan_data_t *pdata,
35+
_scanf_float (struct _scan_data_t *pdata,
3736
FILE *fp, va_list *ap)
3837
{
3938
int c;
@@ -84,7 +83,7 @@ _scanf_float (struct _reent *rptr,
8483
}
8584
goto fskip;
8685
}
87-
/* Fall through. */
86+
__fallthrough;
8887
case '1':
8988
case '2':
9089
case '3':
@@ -212,7 +211,7 @@ _scanf_float (struct _reent *rptr,
212211
++pdata->nread;
213212
if (--fp->_r > 0)
214213
fp->_p++;
215-
else if (pdata->pfn_refill (rptr, fp))
214+
else if (pdata->pfn_refill (fp))
216215
/* "EOF". */
217216
break;
218217
}
@@ -234,7 +233,7 @@ _scanf_float (struct _reent *rptr,
234233
guarantee that in all implementations of ungetc. */
235234
while (p > pdata->buf)
236235
{
237-
pdata->pfn_ungetc (rptr, *--p, fp); /* "[-+nNaA]". */
236+
pdata->pfn_ungetc (*--p, fp); /* "[-+nNaA]". */
238237
--pdata->nread;
239238
}
240239
return MATCH_FAILURE;
@@ -248,14 +247,14 @@ _scanf_float (struct _reent *rptr,
248247
if (infcount >= 3) /* valid 'inf', but short of 'infinity'. */
249248
while (infcount-- > 3)
250249
{
251-
pdata->pfn_ungetc (rptr, *--p, fp); /* "[iInNtT]". */
250+
pdata->pfn_ungetc (*--p, fp); /* "[iInNtT]". */
252251
--pdata->nread;
253252
}
254253
else
255254
{
256255
while (p > pdata->buf)
257256
{
258-
pdata->pfn_ungetc (rptr, *--p, fp); /* "[-+iInN]". */
257+
pdata->pfn_ungetc (*--p, fp); /* "[-+iInN]". */
259258
--pdata->nread;
260259
}
261260
return MATCH_FAILURE;
@@ -271,7 +270,7 @@ _scanf_float (struct _reent *rptr,
271270
/* No digits at all. */
272271
while (p > pdata->buf)
273272
{
274-
pdata->pfn_ungetc (rptr, *--p, fp); /* "[-+.]". */
273+
pdata->pfn_ungetc (*--p, fp); /* "[-+.]". */
275274
--pdata->nread;
276275
}
277276
return MATCH_FAILURE;
@@ -281,11 +280,11 @@ _scanf_float (struct _reent *rptr,
281280
--pdata->nread;
282281
if (c != 'e' && c != 'E')
283282
{
284-
pdata->pfn_ungetc (rptr, c, fp); /* "[-+]". */
283+
pdata->pfn_ungetc (c, fp); /* "[-+]". */
285284
c = *--p;
286285
--pdata->nread;
287286
}
288-
pdata->pfn_ungetc (rptr, c, fp); /* "[eE]". */
287+
pdata->pfn_ungetc (c, fp); /* "[eE]". */
289288
}
290289
if ((pdata->flags & SUPPRESS) == 0)
291290
{

newlib/libc/stdio/stdio.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ int siprintf (char *, const char *, ...)
446446
__picolibc_format(__printf__, 2, 3) __nothrow;
447447
#define __i_sprintf siprintf
448448
int siscanf (const char *, const char *, ...)
449-
__picolibc_format(__scanf__, 2, 3);
449+
__picolibc_format(__scanf__, 2, 3) __nothrow;
450450
int sniprintf (char *, size_t, const char *, ...)
451451
__picolibc_format(__printf__, 3, 4) __nothrow;
452452
#define __i_snprintf sniprintf
@@ -467,9 +467,9 @@ int viprintf (const char *, va_list)
467467
int viscanf (const char *, va_list)
468468
__picolibc_format(__scanf__, 1, 0);
469469
int vsiprintf (char *, const char *, va_list)
470-
__picolibc_format(__printf__, 2, 0);
470+
__picolibc_format(__printf__, 2, 0) __nothrow;
471471
int vsiscanf (const char *, const char *, va_list)
472-
__picolibc_format(__scanf__, 2, 0);
472+
__picolibc_format(__scanf__, 2, 0) __nothrow;
473473
int vsniprintf (char *, size_t, const char *, va_list)
474474
__picolibc_format(__printf__, 3, 0) __nothrow;
475475
#endif /* __MISC_VISIBLE */

0 commit comments

Comments
 (0)