~ubuntu-branches/ubuntu/precise/xorg-server/precise-updates

« back to all changes in this revision

Viewing changes to os/xprintf.c

Tags: 2:1.10.1-2
* Build xserver-xorg-core-udeb on hurd-i386.  Thanks, Samuel Thibault!
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * printf routines which xalloc their buffer
3
 
 */ 
 
1
/**
 
2
 * @file
 
3
 *
 
4
 * @section DESCRIPTION
 
5
 *
 
6
 * These functions provide a portable implementation of the common (but not
 
7
 * yet universal) asprintf & vasprintf routines to allocate a buffer big
 
8
 * enough to sprintf the arguments to.  The XNF variants terminate the server
 
9
 * if the allocation fails.
 
10
 */
4
11
/*
5
12
 * Copyright (c) 2004 Alexander Gottwald
6
13
 *
26
33
 * holders shall not be used in advertising or otherwise to promote the sale,
27
34
 * use or other dealings in this Software without prior written authorization.
28
35
 */
 
36
/*
 
37
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 
38
 *
 
39
 * Permission is hereby granted, free of charge, to any person obtaining a
 
40
 * copy of this software and associated documentation files (the "Software"),
 
41
 * to deal in the Software without restriction, including without limitation
 
42
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
43
 * and/or sell copies of the Software, and to permit persons to whom the
 
44
 * Software is furnished to do so, subject to the following conditions:
 
45
 *
 
46
 * The above copyright notice and this permission notice (including the next
 
47
 * paragraph) shall be included in all copies or substantial portions of the
 
48
 * Software.
 
49
 *
 
50
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
51
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
52
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
53
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
54
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
55
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
56
 * DEALINGS IN THE SOFTWARE.
 
57
 */
 
58
 
29
59
#ifdef HAVE_DIX_CONFIG_H
30
60
#include <dix-config.h>
31
61
#endif
35
65
#include <stdarg.h>
36
66
#include <stdio.h>
37
67
 
 
68
#ifdef asprintf
 
69
# undef asprintf
 
70
#endif
 
71
#ifdef vasprintf
 
72
# undef vasprintf
 
73
#endif
 
74
 
38
75
#ifndef va_copy
39
76
# ifdef __va_copy
40
77
#  define va_copy __va_copy
42
79
#  error "no working va_copy was found"
43
80
# endif
44
81
#endif
45
 
    
 
82
 
 
83
/**
 
84
 * Varargs sprintf that allocates a string buffer the right size for
 
85
 * the pattern & data provided and prints the requested data to it.
 
86
 *
 
87
 * @param ret     Pointer to which the newly allocated buffer is written
 
88
 *                (contents undefined on error)
 
89
 * @param format  printf style format string
 
90
 * @param va      variable argument list
 
91
 * @return        size of allocated buffer, or -1 on error.
 
92
 */
 
93
int
 
94
Xvasprintf(char **ret, const char * _X_RESTRICT_KYWD format, va_list va)
 
95
{
 
96
#ifdef HAVE_VASPRINTF
 
97
    return vasprintf(ret, format, va);
 
98
#else
 
99
    int size;
 
100
    va_list va2;
 
101
 
 
102
    va_copy(va2, va);
 
103
    size = vsnprintf(NULL, 0, format, va2);
 
104
    va_end(va2);
 
105
 
 
106
    *ret = malloc(size + 1);
 
107
    if (*ret == NULL)
 
108
        return -1;
 
109
 
 
110
    vsnprintf(*ret, size + 1, format, va);
 
111
    (*ret)[size] = 0;
 
112
    return size;
 
113
#endif
 
114
}
 
115
 
 
116
#ifndef HAVE_VASPRINTF
 
117
# define vasprintf Xvasprintf
 
118
#endif
 
119
 
 
120
/**
 
121
 * sprintf that allocates a string buffer the right size for
 
122
 * the pattern & data provided and prints the requested data to it.
 
123
 *
 
124
 * @param ret     Pointer to which the newly allocated buffer is written
 
125
 *                (contents undefined on error)
 
126
 * @param format  printf style format string
 
127
 * @param ...     arguments for specified format
 
128
 * @return        size of allocated buffer, or -1 on error.
 
129
 */
 
130
int
 
131
Xasprintf(char ** ret, const char * _X_RESTRICT_KYWD format, ...)
 
132
{
 
133
    int size;
 
134
    va_list va;
 
135
    va_start(va, format);
 
136
    size = vasprintf(ret, format, va);
 
137
    va_end(va);
 
138
    return size;
 
139
}
 
140
 
 
141
/**
 
142
 * Varargs sprintf that allocates a string buffer the right size for
 
143
 * the pattern & data provided and prints the requested data to it.
 
144
 * On failure, issues a FatalError message and aborts the server.
 
145
 *
 
146
 * @param ret     Pointer to which the newly allocated buffer is written
 
147
 *                (contents undefined on error)
 
148
 * @param format  printf style format string
 
149
 * @param va      variable argument list
 
150
 * @return        size of allocated buffer
 
151
 */
 
152
int
 
153
XNFvasprintf(char **ret, const char * _X_RESTRICT_KYWD format, va_list va)
 
154
{
 
155
    int size = vasprintf(ret, format, va);
 
156
    if ((size == -1) || (*ret == NULL)) {
 
157
        Error("XNFvasprintf");
 
158
        FatalError("XNFvasprintf failed");
 
159
    }
 
160
    return size;
 
161
}
 
162
 
 
163
/**
 
164
 * sprintf that allocates a string buffer the right size for
 
165
 * the pattern & data provided and prints the requested data to it.
 
166
 * On failure, issues a FatalError message and aborts the server.
 
167
 *
 
168
 * @param ret     Pointer to which the newly allocated buffer is written
 
169
 *                (contents undefined on error)
 
170
 * @param format  printf style format string
 
171
 * @param ...     arguments for specified format
 
172
 * @return        size of allocated buffer
 
173
 */
 
174
int
 
175
XNFasprintf(char ** ret, const char * _X_RESTRICT_KYWD format, ...)
 
176
{
 
177
    int size;
 
178
    va_list va;
 
179
    va_start(va, format);
 
180
    size = XNFvasprintf(ret, format, va);
 
181
    va_end(va);
 
182
    return size;
 
183
}
 
184
 
 
185
/* Old api, now deprecated, may be removed in the future */
46
186
char *
47
187
Xvprintf(const char *format, va_list va)
48
188
{
49
189
    char *ret;
50
 
    int size;
51
 
    va_list va2;
52
 
 
53
 
    va_copy(va2, va);
54
 
    size = vsnprintf(NULL, 0, format, va2);
55
 
    va_end(va2);
56
 
 
57
 
    ret = (char *)malloc(size + 1);
58
 
    if (ret == NULL)
59
 
        return NULL;
60
 
 
61
 
    vsnprintf(ret, size + 1, format, va);
62
 
    ret[size] = 0;
 
190
 
 
191
    if (vasprintf(&ret, format, va) == -1)
 
192
        ret = NULL;
 
193
 
63
194
    return ret;
64
195
}
65
196
 
68
199
    char *ret;
69
200
    va_list va;
70
201
    va_start(va, format);
71
 
    ret = Xvprintf(format, va);
 
202
    if (vasprintf(&ret, format, va) == -1)
 
203
        ret = NULL;
72
204
    va_end(va);
73
205
    return ret;
74
206
}
77
209
XNFvprintf(const char *format, va_list va)
78
210
{
79
211
    char *ret;
80
 
    int size;
81
 
    va_list va2;
82
 
 
83
 
    va_copy(va2, va);
84
 
    size = vsnprintf(NULL, 0, format, va2);
85
 
    va_end(va2);
86
 
 
87
 
    ret = (char *)xnfalloc(size + 1);
88
 
    if (ret == NULL)
89
 
        return NULL;
90
 
 
91
 
    vsnprintf(ret, size + 1, format, va);
92
 
    ret[size] = 0;
 
212
 
 
213
    XNFvasprintf(&ret, format, va);
 
214
 
93
215
    return ret;
94
216
}
95
217
 
98
220
    char *ret;
99
221
    va_list va;
100
222
    va_start(va, format);
101
 
    ret = XNFvprintf(format, va);
 
223
    XNFvasprintf(&ret, format, va);
102
224
    va_end(va);
103
225
    return ret;
104
226
}