root/maint/gnulib/lib/unistdio/ulc-fprintf.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ulc_fprintf

   1 /* Formatted output to a stream.
   2    Copyright (C) 2004, 2006-2021 Free Software Foundation, Inc.
   3 
   4    This file is free software.
   5    It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
   6    You can redistribute it and/or modify it under either
   7      - the terms of the GNU Lesser General Public License as published
   8        by the Free Software Foundation; either version 3, or (at your
   9        option) any later version, or
  10      - the terms of the GNU General Public License as published by the
  11        Free Software Foundation; either version 2, or (at your option)
  12        any later version, or
  13      - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
  14 
  15    This file is distributed in the hope that it will be useful,
  16    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18    Lesser General Public License and the GNU General Public License
  19    for more details.
  20 
  21    You should have received a copy of the GNU Lesser General Public
  22    License and of the GNU General Public License along with this
  23    program.  If not, see <https://www.gnu.org/licenses/>.  */
  24 
  25 #ifdef HAVE_CONFIG_H
  26 # include <config.h>
  27 #endif
  28 
  29 /* Specification.  */
  30 #include "unistdio.h"
  31 
  32 #include <errno.h>
  33 #include <limits.h>
  34 #include <stdarg.h>
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 
  38 #include "fseterr.h"
  39 
  40 /* Print formatted output to the stream FP.
  41    Return string length of formatted string.  On error, return a negative
  42    value.  */
  43 int
  44 ulc_fprintf (FILE *fp, const char *format, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46   char buf[2000];
  47   char *output;
  48   size_t len;
  49   size_t lenbuf = sizeof (buf);
  50   va_list args;
  51 
  52   va_start (args, format);
  53   output = ulc_vasnprintf (buf, &lenbuf, format, args);
  54   len = lenbuf;
  55   va_end (args);
  56 
  57   if (!output)
  58     {
  59       fseterr (fp);
  60       return -1;
  61     }
  62 
  63   if (fwrite (output, 1, len, fp) < len)
  64     {
  65       if (output != buf)
  66         {
  67           int saved_errno = errno;
  68           free (output);
  69           errno = saved_errno;
  70         }
  71       return -1;
  72     }
  73 
  74   if (len > INT_MAX)
  75     {
  76       errno = EOVERFLOW;
  77       fseterr (fp);
  78       return -1;
  79     }
  80 
  81   return len;
  82 }

/* [previous][next][first][last][top][bottom][index][help] */