Updated German translated manpages
[amule.git] / src / utils / cas / lines.c
blobf4b2b93da8782c10932ac5827cd7c65f9d74bf7d
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 n, 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 /* Try to print in the allocated space. */
40 va_start(ap, format);
41 n = vsnprintf(p, size, format, ap);
42 va_end(ap);
43 /* If that worked, set the line. */
44 if (n > -1 && n < size) {
45 break;
47 /* Else try again with more space. */
48 if (n > -1) /* glibc 2.1 */
49 size = n+1; /* precisely what is needed */
50 else /* glibc 2.0 */
51 size *= 2; /* twice the old size */
52 if ((tmp = realloc(p, size)) == NULL) {
53 free(p);
54 p = NULL;
55 break;
56 } else {
57 p = tmp;
60 lines[line] = p;
63 void AppendToLine(char *lines[], int line, const char *text)
65 /* Are we trying to append to an empty line? */
66 if (lines[line] == NULL)
67 CreateLine(lines, line, text);
68 else {
69 /* Calculate the new required size... */
70 int size = strlen(lines[line]) + strlen(text) + 1;
71 if ((lines[line] = realloc(lines[line], size)) == NULL)
72 return;
73 /* ... and append the new text. */
74 strcat(lines[line], text);