Removed HAVE_CONFIG_H
[amule.git] / src / utils / cas / lines.c
blobc572e996f6e3caf9157dd8f9ed5e187f27488696
1 /*
2 * This file is part of aMule.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #include "lines.h"
27 void CreateLine(char *lines[], int line, const char *format, ...)
29 /* Guess we need no more than 80 bytes. */
30 int size = 100;
31 char *p;
32 char *tmp;
33 va_list ap;
34 if ((p = malloc(size)) == NULL) {
35 lines[line] = NULL;
36 return;
38 while (1) {
39 int n;
40 /* Try to print in the allocated space. */
41 va_start(ap, format);
42 n = vsnprintf(p, size, format, ap);
43 va_end(ap);
44 /* If that worked, set the line. */
45 if (n > -1 && n < size) {
46 break;
48 /* Else try again with more space. */
49 if (n > -1) /* glibc 2.1 */
50 size = n+1; /* precisely what is needed */
51 else /* glibc 2.0 */
52 size *= 2; /* twice the old size */
53 if ((tmp = realloc(p, size)) == NULL) {
54 free(p);
55 p = NULL;
56 break;
57 } else {
58 p = tmp;
61 lines[line] = p;
64 void AppendToLine(char *lines[], int line, const char *text)
66 /* Are we trying to append to an empty line? */
67 if (lines[line] == NULL)
68 CreateLine(lines, line, text);
69 else {
70 /* Calculate the new required size... */
71 int size = strlen(lines[line]) + strlen(text) + 1;
72 if ((lines[line] = realloc(lines[line], size)) == NULL)
73 return;
74 /* ... and append the new text. */
75 strcat(lines[line], text);