(root)/
glib-2.79.0/
glib/
gnulib/
printf.c
       1  /* GLIB - Library of useful routines for C programming
       2   * Copyright (C) 2003 Matthias Clasen
       3   *
       4   * This library is free software; you can redistribute it and/or
       5   * modify it under the terms of the GNU Lesser General Public
       6   * License as published by the Free Software Foundation; either
       7   * version 2 of the License, or (at your option) any later version.
       8   *
       9   * This library is distributed in the hope that it will be useful,
      10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
      12   * Lesser General Public License for more details.
      13   *
      14   * You should have received a copy of the GNU Lesser General Public
      15   * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      16   */
      17  
      18  /*
      19   * Modified by the GLib Team and others 2003.  See the AUTHORS
      20   * file for a list of people on the GLib Team.  See the ChangeLog
      21   * files for a list of changes.  These files are distributed with
      22   * GLib at ftp://ftp.gtk.org/pub/gtk/. 
      23   */
      24  
      25  #include <config.h>
      26  #include <string.h>
      27  #include <stdlib.h>
      28  #include <stdio.h>
      29  #include "g-gnulib.h"
      30  #include "vasnprintf.h"
      31  #include "printf.h"
      32  
      33  int _g_gnulib_printf (char const *format, ...)
      34  {
      35    va_list args;
      36    int retval;
      37  
      38    va_start (args, format);
      39    retval = _g_gnulib_vprintf (format, args);
      40    va_end (args);
      41  
      42    return retval;
      43  }
      44  
      45  int _g_gnulib_fprintf (FILE *file, char const *format, ...)
      46  {
      47    va_list args;
      48    int retval;
      49  
      50    va_start (args, format);
      51    retval = _g_gnulib_vfprintf (file, format, args);
      52    va_end (args);
      53    
      54    return retval;
      55  }
      56  
      57  int _g_gnulib_sprintf (char *string, char const *format, ...)
      58  {
      59    va_list args;
      60    int retval;
      61  
      62    va_start (args, format);
      63    retval = _g_gnulib_vsprintf (string, format, args);
      64    va_end (args);
      65    
      66    return retval;
      67  }
      68  
      69  int _g_gnulib_snprintf (char *string, size_t n, char const *format, ...)
      70  {
      71    va_list args;
      72    int retval;
      73  
      74    va_start (args, format);
      75    retval = _g_gnulib_vsnprintf (string, n, format, args);
      76    va_end (args);
      77    
      78    return retval;
      79  }
      80  
      81  int _g_gnulib_vprintf (char const *format, va_list args)         
      82  {
      83    return _g_gnulib_vfprintf (stdout, format, args);
      84  }
      85  
      86  int _g_gnulib_vfprintf (FILE *file, char const *format, va_list args)
      87  {
      88    char *result;
      89    size_t length, rlength;
      90  
      91    result = vasnprintf (NULL, &length, format, args);
      92    if (result == NULL) 
      93      return -1;
      94  
      95    rlength = fwrite (result, 1, length, file);
      96    free (result);
      97    
      98    return rlength;
      99  }
     100  
     101  int _g_gnulib_vsprintf (char *string, char const *format, va_list args)
     102  {
     103    char *result;
     104    size_t length;
     105  
     106    result = vasnprintf (NULL, &length, format, args);
     107    if (result == NULL) 
     108      return -1;
     109  
     110    memcpy (string, result, length + 1);
     111    free (result);
     112    
     113    return length;  
     114  }
     115  
     116  int _g_gnulib_vsnprintf (char *string, size_t n, char const *format, va_list args)
     117  {
     118    char *result;
     119    size_t length;
     120  
     121    result = vasnprintf (NULL, &length, format, args);
     122    if (result == NULL) 
     123      return -1;
     124  
     125    if (n > 0) 
     126      {
     127        memcpy (string, result, MIN(length + 1, n));
     128        string[n - 1] = 0;
     129      }
     130  
     131    free (result);
     132    
     133    return length;  
     134  }
     135  
     136  int _g_gnulib_vasprintf (char **result, char const *format, va_list args)
     137  {
     138    size_t length;
     139  
     140    *result = vasnprintf (NULL, &length, format, args);
     141    if (*result == NULL) 
     142      return -1;
     143    
     144    return length;  
     145  }