printffuns.c revision 1.1.1.1
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 the GNU Lesser General Public License as published by
13the Free Software Foundation; either version 3 of the License, or (at your
14option) any later version.
15
16The GNU MP Library is distributed in the hope that it will be useful, but
17WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23
24#include "config.h"
25
26#if HAVE_STDARG
27#include <stdarg.h>
28#else
29#include <varargs.h>
30#endif
31
32#include <stdio.h>
33#include <string.h>
34
35#include "gmp.h"
36#include "gmp-impl.h"
37
38/* SunOS 4 stdio.h doesn't provide a prototype for this */
39#if ! HAVE_DECL_VFPRINTF
40int vfprintf __GMP_PROTO ((FILE *, const char *, va_list));
41#endif
42
43
44static int
45gmp_fprintf_memory (FILE *fp, const char *str, size_t len)
46{
47  return fwrite (str, 1, len, fp);
48}
49
50/* glibc putc is a function, at least when it's in multi-threaded mode or
51   some such, so fwrite chunks instead of making many calls. */
52static int
53gmp_fprintf_reps (FILE *fp, int c, int reps)
54{
55  char  buf[256];
56  int   i, piece, ret;
57  ASSERT (reps >= 0);
58
59  memset (buf, c, MIN (reps, sizeof (buf)));
60  for (i = reps; i > 0; i -= sizeof (buf))
61    {
62      piece = MIN (i, sizeof (buf));
63      ret = fwrite (buf, 1, piece, fp);
64      if (ret == -1)
65        return ret;
66      ASSERT (ret == piece);
67    }
68
69  return reps;
70}
71
72const struct doprnt_funs_t  __gmp_fprintf_funs = {
73  (doprnt_format_t) vfprintf,
74  (doprnt_memory_t) gmp_fprintf_memory,
75  (doprnt_reps_t)   gmp_fprintf_reps,
76};
77