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
27 void CreateLine(char *lines
[], int line
, const char *format
, ...)
29 /* Guess we need no more than 80 bytes. */
34 if ((p
= malloc(size
)) == NULL
) {
39 /* Try to print in the allocated space. */
41 n
= vsnprintf(p
, size
, format
, ap
);
43 /* If that worked, set the line. */
44 if (n
> -1 && n
< size
) {
47 /* Else try again with more space. */
48 if (n
> -1) /* glibc 2.1 */
49 size
= n
+1; /* precisely what is needed */
51 size
*= 2; /* twice the old size */
52 if ((tmp
= realloc(p
, size
)) == NULL
) {
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
);
69 /* Calculate the new required size... */
70 int size
= strlen(lines
[line
]) + strlen(text
) + 1;
71 if ((lines
[line
] = realloc(lines
[line
], size
)) == NULL
)
73 /* ... and append the new text. */
74 strcat(lines
[line
], text
);