]> git.cworth.org Git - grrobot/blob - src/grr_util.c
Fixed messages and scrolling
[grrobot] / src / grr_util.c
1 /* grr_board_view - GTK+ widget for displaying an rr_board
2  *
3  * Copyright © 2003 Carl Worth
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of Carl Worth
10  * not be used in advertising or publicity pertaining to distribution
11  * of the software without specific, written prior permission.
12  * Carl Worth makes no representations about the suitability of this
13  * software for any purpose.  It is provided "as is" without express
14  * or implied warranty.
15  * 
16  * CARL WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
18  * NO EVENT SHALL CARL WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
20  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Carl Worth <carl@theworths.org>
25  */
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "grr_util.h"
32
33 int
34 grr_sprintf_alloc (char **str, const char *fmt, ...)
35 {
36     int ret;
37     va_list ap;
38
39     va_start (ap, fmt);
40     ret = grr_sprintf_alloc_va (str, fmt, ap);
41     va_end (ap);
42
43     return ret;
44 }
45
46 /* ripped more or less straight out of PRINTF(3) */
47 int
48 grr_sprintf_alloc_va (char **str, const char *fmt, va_list ap)
49 {
50     char *new_str;
51     /* Guess we need no more than 100 bytes. */
52     int n, size = 100;
53  
54     if ((*str = malloc (size)) == NULL)
55         return -1;
56     while (1) {
57         /* Try to print in the allocated space. */
58         n = vsnprintf (*str, size, fmt, ap);
59         /* If that worked, return the size. */
60         if (n > -1 && n < size)
61             return n;
62         /* Else try again with more space. */
63         if (n > -1)    /* glibc 2.1 */
64             size = n+1; /* precisely what is needed */
65         else           /* glibc 2.0 */
66             size *= 2;  /* twice the old size */
67         new_str = realloc(*str, size);
68         if (new_str == NULL) {
69             free(*str);
70             *str = NULL;
71             return -1;
72         }
73         *str = new_str;
74     }
75 }
76
77