/src/glib/glib/gnulib/printf.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
35 | 0 | va_list args; |
36 | 0 | int retval; |
37 | |
|
38 | 0 | va_start (args, format); |
39 | 0 | retval = _g_gnulib_vprintf (format, args); |
40 | 0 | va_end (args); |
41 | |
|
42 | 0 | return retval; |
43 | 0 | } |
44 | | |
45 | | int _g_gnulib_fprintf (FILE *file, char const *format, ...) |
46 | 34.9k | { |
47 | 34.9k | va_list args; |
48 | 34.9k | int retval; |
49 | | |
50 | 34.9k | va_start (args, format); |
51 | 34.9k | retval = _g_gnulib_vfprintf (file, format, args); |
52 | 34.9k | va_end (args); |
53 | | |
54 | 34.9k | return retval; |
55 | 34.9k | } |
56 | | |
57 | | int _g_gnulib_sprintf (char *string, char const *format, ...) |
58 | 0 | { |
59 | 0 | va_list args; |
60 | 0 | int retval; |
61 | |
|
62 | 0 | va_start (args, format); |
63 | 0 | retval = _g_gnulib_vsprintf (string, format, args); |
64 | 0 | va_end (args); |
65 | | |
66 | 0 | return retval; |
67 | 0 | } |
68 | | |
69 | | int _g_gnulib_snprintf (char *string, size_t n, char const *format, ...) |
70 | 0 | { |
71 | 0 | va_list args; |
72 | 0 | int retval; |
73 | |
|
74 | 0 | va_start (args, format); |
75 | 0 | retval = _g_gnulib_vsnprintf (string, n, format, args); |
76 | 0 | va_end (args); |
77 | | |
78 | 0 | return retval; |
79 | 0 | } |
80 | | |
81 | | int _g_gnulib_vprintf (char const *format, va_list args) |
82 | 0 | { |
83 | 0 | return _g_gnulib_vfprintf (stdout, format, args); |
84 | 0 | } |
85 | | |
86 | | int _g_gnulib_vfprintf (FILE *file, char const *format, va_list args) |
87 | 34.9k | { |
88 | 34.9k | char *result; |
89 | 34.9k | size_t length, rlength; |
90 | | |
91 | 34.9k | result = vasnprintf (NULL, &length, format, args); |
92 | 34.9k | if (result == NULL) |
93 | 0 | return -1; |
94 | | |
95 | 34.9k | rlength = fwrite (result, 1, length, file); |
96 | 34.9k | free (result); |
97 | | |
98 | 34.9k | return rlength; |
99 | 34.9k | } |
100 | | |
101 | | int _g_gnulib_vsprintf (char *string, char const *format, va_list args) |
102 | 0 | { |
103 | 0 | char *result; |
104 | 0 | size_t length; |
105 | |
|
106 | 0 | result = vasnprintf (NULL, &length, format, args); |
107 | 0 | if (result == NULL) |
108 | 0 | return -1; |
109 | | |
110 | 0 | memcpy (string, result, length + 1); |
111 | 0 | free (result); |
112 | | |
113 | 0 | return length; |
114 | 0 | } |
115 | | |
116 | | int _g_gnulib_vsnprintf (char *string, size_t n, char const *format, va_list args) |
117 | 0 | { |
118 | 0 | char *result; |
119 | 0 | size_t length; |
120 | |
|
121 | 0 | result = vasnprintf (NULL, &length, format, args); |
122 | 0 | if (result == NULL) |
123 | 0 | return -1; |
124 | | |
125 | 0 | if (n > 0) |
126 | 0 | { |
127 | 0 | memcpy (string, result, MIN(length + 1, n)); |
128 | 0 | string[n - 1] = 0; |
129 | 0 | } |
130 | |
|
131 | 0 | free (result); |
132 | | |
133 | 0 | return length; |
134 | 0 | } |
135 | | |
136 | | int _g_gnulib_vasprintf (char **result, char const *format, va_list args) |
137 | 70.3M | { |
138 | 70.3M | size_t length; |
139 | | |
140 | 70.3M | *result = vasnprintf (NULL, &length, format, args); |
141 | 70.3M | if (*result == NULL) |
142 | 0 | return -1; |
143 | | |
144 | 70.3M | return length; |
145 | 70.3M | } |