Blame glib/gnulib/printf.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 2003 Matthias Clasen
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 2003.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <config.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include "g-gnulib.h"
Packit ae235b
#include "vasnprintf.h"
Packit ae235b
#include "printf.h"
Packit ae235b
Packit ae235b
int _g_gnulib_printf (char const *format, ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  int retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = _g_gnulib_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_fprintf (FILE *file, char const *format, ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  int retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = _g_gnulib_vfprintf (file, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_sprintf (char *string, char const *format, ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  int retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = _g_gnulib_vsprintf (string, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_snprintf (char *string, size_t n, char const *format, ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  int retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = _g_gnulib_vsnprintf (string, n, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_vprintf (char const *format, va_list args)         
Packit ae235b
{
Packit ae235b
  return _g_gnulib_vfprintf (stdout, format, args);
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_vfprintf (FILE *file, char const *format, va_list args)
Packit ae235b
{
Packit ae235b
  char *result;
Packit ae235b
  size_t length, rlength;
Packit ae235b
Packit ae235b
  result = vasnprintf (NULL, &length, format, args);
Packit ae235b
  if (result == NULL) 
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  rlength = fwrite (result, 1, length, file);
Packit ae235b
  free (result);
Packit ae235b
  
Packit ae235b
  return rlength;
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_vsprintf (char *string, char const *format, va_list args)
Packit ae235b
{
Packit ae235b
  char *result;
Packit ae235b
  size_t length;
Packit ae235b
Packit ae235b
  result = vasnprintf (NULL, &length, format, args);
Packit ae235b
  if (result == NULL) 
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  memcpy (string, result, length + 1);
Packit ae235b
  free (result);
Packit ae235b
  
Packit ae235b
  return length;  
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_vsnprintf (char *string, size_t n, char const *format, va_list args)
Packit ae235b
{
Packit ae235b
  char *result;
Packit ae235b
  size_t length;
Packit ae235b
Packit ae235b
  result = vasnprintf (NULL, &length, format, args);
Packit ae235b
  if (result == NULL) 
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  if (n > 0) 
Packit ae235b
    {
Packit ae235b
      memcpy (string, result, MIN(length + 1, n));
Packit ae235b
      string[n - 1] = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  free (result);
Packit ae235b
  
Packit ae235b
  return length;  
Packit ae235b
}
Packit ae235b
Packit ae235b
int _g_gnulib_vasprintf (char **result, char const *format, va_list args)
Packit ae235b
{
Packit ae235b
  size_t length;
Packit ae235b
Packit ae235b
  *result = vasnprintf (NULL, &length, format, args);
Packit ae235b
  if (*result == NULL) 
Packit ae235b
    return -1;
Packit ae235b
  
Packit ae235b
  return length;  
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b