Blame gettext-runtime/libasprintf/vasprintf.c

Packit 5b56b6
/* Formatted output to strings.
Packit 5b56b6
   Copyright (C) 1999, 2002, 2006-2007, 2015 Free Software Foundation,
Packit 5b56b6
   Inc.
Packit 5b56b6
Packit 5b56b6
   This program is free software: you can redistribute it and/or modify
Packit 5b56b6
   it under the terms of the GNU Lesser General Public License as published by
Packit 5b56b6
   the Free Software Foundation; either version 2.1 of the License, or
Packit 5b56b6
   (at your option) any later version.
Packit 5b56b6
Packit 5b56b6
   This program is distributed in the hope that it will be useful,
Packit 5b56b6
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5b56b6
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 5b56b6
   GNU Lesser General Public License for more details.
Packit 5b56b6
Packit 5b56b6
   You should have received a copy of the GNU Lesser General Public License
Packit 5b56b6
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 5b56b6
Packit 5b56b6
#include <config.h>
Packit 5b56b6
Packit 5b56b6
/* Specification.  */
Packit 5b56b6
#ifdef IN_LIBASPRINTF
Packit 5b56b6
# include "vasprintf.h"
Packit 5b56b6
#else
Packit 5b56b6
# include <stdio.h>
Packit 5b56b6
#endif
Packit 5b56b6
Packit 5b56b6
#include <errno.h>
Packit 5b56b6
#include <limits.h>
Packit 5b56b6
#include <stdlib.h>
Packit 5b56b6
Packit 5b56b6
#include "vasnprintf.h"
Packit 5b56b6
Packit 5b56b6
/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
Packit 5b56b6
#ifndef EOVERFLOW
Packit 5b56b6
# define EOVERFLOW E2BIG
Packit 5b56b6
#endif
Packit 5b56b6
Packit 5b56b6
int
Packit 5b56b6
vasprintf (char **resultp, const char *format, va_list args)
Packit 5b56b6
{
Packit 5b56b6
  size_t length;
Packit 5b56b6
  char *result = vasnprintf (NULL, &length, format, args);
Packit 5b56b6
  if (result == NULL)
Packit 5b56b6
    return -1;
Packit 5b56b6
Packit 5b56b6
  if (length > INT_MAX)
Packit 5b56b6
    {
Packit 5b56b6
      free (result);
Packit 5b56b6
      errno = EOVERFLOW;
Packit 5b56b6
      return -1;
Packit 5b56b6
    }
Packit 5b56b6
Packit 5b56b6
  *resultp = result;
Packit 5b56b6
  /* Return the number of resulting bytes, excluding the trailing NUL.  */
Packit 5b56b6
  return length;
Packit 5b56b6
}