printffuns.c revision 1.1.1.3
128221Smsmith/* __gmp_fprintf_funs -- support for formatted output to FILEs.
271622Snsouch
328221Smsmith   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
428221Smsmith   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
528221Smsmith   FUTURE GNU MP RELEASES.
628221Smsmith
728221SmsmithCopyright 2001 Free Software Foundation, Inc.
828221Smsmith
928221SmsmithThis file is part of the GNU MP Library.
1028221Smsmith
1128221SmsmithThe GNU MP Library is free software; you can redistribute it and/or modify
1228221Smsmithit under the terms of either:
1328221Smsmith
1428221Smsmith  * the GNU Lesser General Public License as published by the Free
1528221Smsmith    Software Foundation; either version 3 of the License, or (at your
1628221Smsmith    option) any later version.
1728221Smsmith
1828221Smsmithor
1928221Smsmith
2028221Smsmith  * the GNU General Public License as published by the Free Software
2128221Smsmith    Foundation; either version 2 of the License, or (at your option) any
2228221Smsmith    later version.
2328221Smsmith
2428221Smsmithor both in parallel, as here.
2528221Smsmith
2650477SpeterThe GNU MP Library is distributed in the hope that it will be useful, but
2728221SmsmithWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2828221Smsmithor FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2938061Smsmithfor more details.
3038061Smsmith
3128221SmsmithYou should have received copies of the GNU General Public License and the
32187576SjhbGNU Lesser General Public License along with the GNU MP Library.  If not,
33187576Sjhbsee https://www.gnu.org/licenses/.  */
34187576Sjhb
3528221Smsmith#include <stdarg.h>
3628221Smsmith#include <stdio.h>
3728221Smsmith#include <string.h>
3840784Snsouch
3940784Snsouch#include "gmp.h"
4040784Snsouch#include "gmp-impl.h"
4140784Snsouch
4240784Snsouch/* SunOS 4 stdio.h doesn't provide a prototype for this */
4340784Snsouch#if ! HAVE_DECL_VFPRINTF
4440784Snsouchint vfprintf (FILE *, const char *, va_list);
4540784Snsouch#endif
4640784Snsouch
4740784Snsouch
4840784Snsouchstatic int
4963403Sdfrgmp_fprintf_memory (FILE *fp, const char *str, size_t len)
5063624Sdfr{
5128221Smsmith  return fwrite (str, 1, len, fp);
5228221Smsmith}
5355939Snsouch
5455939Snsouch/* glibc putc is a function, at least when it's in multi-threaded mode or
5555939Snsouch   some such, so fwrite chunks instead of making many calls. */
5655939Snsouchstatic int
5755939Snsouchgmp_fprintf_reps (FILE *fp, int c, int reps)
5855939Snsouch{
5928221Smsmith  char  buf[256];
6028221Smsmith  int   i, piece, ret;
6128221Smsmith  ASSERT (reps >= 0);
62184130Sjhb
6355939Snsouch  memset (buf, c, MIN (reps, sizeof (buf)));
6455939Snsouch  for (i = reps; i > 0; i -= sizeof (buf))
6528221Smsmith    {
6638061Smsmith      piece = MIN (i, sizeof (buf));
6738061Smsmith      ret = fwrite (buf, 1, piece, fp);
6871622Snsouch      if (ret == -1)
6938061Smsmith        return ret;
7042475Snsouch      ASSERT (ret == piece);
7142475Snsouch    }
7242475Snsouch
7342475Snsouch  return reps;
7442475Snsouch}
7542475Snsouch
7642475Snsouchconst struct doprnt_funs_t  __gmp_fprintf_funs = {
7742475Snsouch  (doprnt_format_t) vfprintf,
7842475Snsouch  (doprnt_memory_t) gmp_fprintf_memory,
7942475Snsouch  (doprnt_reps_t)   gmp_fprintf_reps,
8042475Snsouch};
8142475Snsouch