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_vfprintf (FILE *fp, const char *format, va_list args) /* */ 45 { 46 char buf[2000]; 47 char *output; 48 size_t len; 49 size_t lenbuf = sizeof (buf); 50 51 output = ulc_vasnprintf (buf, &lenbuf, format, args); 52 len = lenbuf; 53 54 if (!output) 55 { 56 fseterr (fp); 57 return -1; 58 } 59 60 if (fwrite (output, 1, len, fp) < len) 61 { 62 if (output != buf) 63 { 64 int saved_errno = errno; 65 free (output); 66 errno = saved_errno; 67 } 68 return -1; 69 } 70 71 if (len > INT_MAX) 72 { 73 errno = EOVERFLOW; 74 fseterr (fp); 75 return -1; 76 } 77 78 return len; 79 }