Home | History | Annotate | Download | only in gnulib
      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, write to the
     16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     17  * Boston, MA 02111-1307, USA.
     18  */
     19 
     20 /*
     21  * Modified by the GLib Team and others 2003.  See the AUTHORS
     22  * file for a list of people on the GLib Team.  See the ChangeLog
     23  * files for a list of changes.  These files are distributed with
     24  * GLib at ftp://ftp.gtk.org/pub/gtk/.
     25  */
     26 
     27 #ifdef HAVE_CONFIG_H
     28 # include <config.h>
     29 #endif
     30 #include <string.h>
     31 #include <stdlib.h>
     32 #include <stdio.h>
     33 #include "g-gnulib.h"
     34 #include "vasnprintf.h"
     35 #include "printf.h"
     36 
     37 int _g_gnulib_printf (char const *format, ...)
     38 {
     39   va_list args;
     40   int retval;
     41 
     42   va_start (args, format);
     43   retval = _g_gnulib_vprintf (format, args);
     44   va_end (args);
     45 
     46   return retval;
     47 }
     48 
     49 int _g_gnulib_fprintf (FILE *file, char const *format, ...)
     50 {
     51   va_list args;
     52   int retval;
     53 
     54   va_start (args, format);
     55   retval = _g_gnulib_vfprintf (file, format, args);
     56   va_end (args);
     57 
     58   return retval;
     59 }
     60 
     61 int _g_gnulib_sprintf (char *string, char const *format, ...)
     62 {
     63   va_list args;
     64   int retval;
     65 
     66   va_start (args, format);
     67   retval = _g_gnulib_vsprintf (string, format, args);
     68   va_end (args);
     69 
     70   return retval;
     71 }
     72 
     73 int _g_gnulib_snprintf (char *string, size_t n, char const *format, ...)
     74 {
     75   va_list args;
     76   int retval;
     77 
     78   va_start (args, format);
     79   retval = _g_gnulib_vsnprintf (string, n, format, args);
     80   va_end (args);
     81 
     82   return retval;
     83 }
     84 
     85 int _g_gnulib_vprintf (char const *format, va_list args)
     86 {
     87   return _g_gnulib_vfprintf (stdout, format, args);
     88 }
     89 
     90 int _g_gnulib_vfprintf (FILE *file, char const *format, va_list args)
     91 {
     92   char *result;
     93   size_t length;
     94 
     95   result = vasnprintf (NULL, &length, format, args);
     96   if (result == NULL)
     97     return -1;
     98 
     99   fwrite (result, 1, length, file);
    100   free (result);
    101 
    102   return length;
    103 }
    104 
    105 int _g_gnulib_vsprintf (char *string, char const *format, va_list args)
    106 {
    107   char *result;
    108   size_t length;
    109 
    110   result = vasnprintf (NULL, &length, format, args);
    111   if (result == NULL)
    112     return -1;
    113 
    114   memcpy (string, result, length + 1);
    115   free (result);
    116 
    117   return length;
    118 }
    119 
    120 int _g_gnulib_vsnprintf (char *string, size_t n, char const *format, va_list args)
    121 {
    122   char *result;
    123   size_t length;
    124 
    125   result = vasnprintf (NULL, &length, format, args);
    126   if (result == NULL)
    127     return -1;
    128 
    129   if (n > 0)
    130     {
    131       memcpy (string, result, MIN(length + 1, n));
    132       string[n - 1] = 0;
    133     }
    134 
    135   free (result);
    136 
    137   return length;
    138 }
    139 
    140 int _g_gnulib_vasprintf (char **result, char const *format, va_list args)
    141 {
    142   size_t length;
    143 
    144   *result = vasnprintf (NULL, &length, format, args);
    145   if (*result == NULL)
    146     return -1;
    147 
    148   return length;
    149 }
    150 
    151 
    152 
    153 
    154 
    155