1/* __gmp_fprintf_funs -- support for formatted output to FILEs.
2
3   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
4   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5   FUTURE GNU MP RELEASES.
6
7Copyright 2001 Free Software Foundation, Inc.
8
9This file is part of the GNU MP Library.
10
11The GNU MP Library is free software; you can redistribute it and/or modify
12it under the terms of either:
13
14  * the GNU Lesser General Public License as published by the Free
15    Software Foundation; either version 3 of the License, or (at your
16    option) any later version.
17
18or
19
20  * the GNU General Public License as published by the Free Software
21    Foundation; either version 2 of the License, or (at your option) any
22    later version.
23
24or both in parallel, as here.
25
26The GNU MP Library is distributed in the hope that it will be useful, but
27WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29for more details.
30
31You should have received copies of the GNU General Public License and the
32GNU Lesser General Public License along with the GNU MP Library.  If not,
33see https://www.gnu.org/licenses/.  */
34
35#include <stdarg.h>
36#include <stdio.h>
37#include <string.h>
38
39#include "gmp-impl.h"
40
41/* SunOS 4 stdio.h doesn't provide a prototype for this */
42#if ! HAVE_DECL_VFPRINTF
43int vfprintf (FILE *, const char *, va_list);
44#endif
45
46
47static int
48gmp_fprintf_memory (FILE *fp, const char *str, size_t len)
49{
50  return fwrite (str, 1, len, fp);
51}
52
53/* glibc putc is a function, at least when it's in multi-threaded mode or
54   some such, so fwrite chunks instead of making many calls. */
55static int
56gmp_fprintf_reps (FILE *fp, int c, int reps)
57{
58  char  buf[256];
59  int   i, piece, ret;
60  ASSERT (reps >= 0);
61
62  memset (buf, c, MIN (reps, sizeof (buf)));
63  for (i = reps; i > 0; i -= sizeof (buf))
64    {
65      piece = MIN (i, sizeof (buf));
66      ret = fwrite (buf, 1, piece, fp);
67      if (ret == -1)
68        return ret;
69      ASSERT (ret == piece);
70    }
71
72  return reps;
73}
74
75const struct doprnt_funs_t  __gmp_fprintf_funs = {
76  (doprnt_format_t) vfprintf,
77  (doprnt_memory_t) gmp_fprintf_memory,
78  (doprnt_reps_t)   gmp_fprintf_reps,
79};
80