src/server/util/io.c

changeset 32
ebba53de8b18
parent 25
5dee29c7c530
child 41
bb7a1f5a8b48
equal deleted inserted replaced
31:280250e45ba6 32:ebba53de8b18
28 28
29 #include <unistd.h> 29 #include <unistd.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <sys/uio.h> 31 #include <sys/uio.h>
32 32
33 #include <limits.h> /* asprintf */
34
33 #include "io.h" 35 #include "io.h"
34 #include "pool.h" 36 #include "pool.h"
35 37
36 IOStream native_io_funcs = { 38 IOStream native_io_funcs = {
37 system_write, 39 system_write,
137 } 139 }
138 140
139 ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) { 141 ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) {
140 return writev(fd, io->iov, io->iovctn); 142 return writev(fd, io->iov, io->iovctn);
141 } 143 }
144
145
146 /* TODO: add asprintf to new file */
147
148 /*
149 * asprintf implementation for Solaris 10
150 * source from OpenSolaris
151 * file: /onnv/onnv-gate/usr/src/lib/libc/port/print/asprintf.c
152 */
153
154 /*
155 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
156 * Use is subject to license terms.
157 */
158
159 /*
160 * Copyright (c) 2004 Darren Tucker.
161 *
162 * Based originally on asprintf.c from OpenBSD:
163 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
164 *
165 * Permission to use, copy, modify, and distribute this software for any
166 * purpose with or without fee is hereby granted, provided that the above
167 * copyright notice and this permission notice appear in all copies.
168 *
169 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
170 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
171 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
172 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
173 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
174 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
175 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
176 */
177
178 #ifdef __SunOS_5_10
179
180 #define INIT_SZ 128
181
182 /* VARARGS2 */
183 int
184 vasprintf(char **str, const char *format, va_list ap)
185 {
186 char string[INIT_SZ];
187 char *newstr;
188 int ret;
189 size_t len;
190
191 *str = NULL;
192 ret = vsnprintf(string, INIT_SZ, format, ap);
193 if (ret < 0) /* retain the value of errno from vsnprintf() */
194 return (-1);
195 if (ret < INIT_SZ) {
196 len = ret + 1;
197 if ((newstr = malloc(len)) == NULL)
198 return (-1); /* retain errno from malloc() */
199 (void) strlcpy(newstr, string, len);
200 *str = newstr;
201 return (ret);
202 }
203 /*
204 * We will perform this loop more than once only if some other
205 * thread modifies one of the vasprintf() arguments after our
206 * previous call to vsnprintf().
207 */
208 for (;;) {
209 if (ret == INT_MAX) { /* Bad length */
210 errno = ENOMEM;
211 return (-1);
212 }
213 len = ret + 1;
214 if ((newstr = malloc(len)) == NULL)
215 return (-1); /* retain errno from malloc() */
216 ret = vsnprintf(newstr, len, format, ap);
217 if (ret < 0) { /* retain errno from vsnprintf() */
218 free(newstr);
219 return (-1);
220 }
221 if (ret < len) {
222 *str = newstr;
223 return (ret);
224 }
225 free(newstr);
226 }
227 }
228
229 int
230 asprintf(char **str, const char *format, ...)
231 {
232 va_list ap;
233 int ret;
234
235 *str = NULL;
236 va_start(ap, format);
237 ret = vasprintf(str, format, ap);
238 va_end(ap);
239
240 return (ret);
241 }
242
243 #endif

mercurial