Blame gnulib/lib/vasprintf.c

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