summaryrefslogtreecommitdiffstats
path: root/lib/libmytinfo/addstr.c
blob: d3d74448e139aee223945f5bf2548b1af8631872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * addstr.c
 *
 * By Ross Ridge
 * Public Domain
 * 92/02/01 07:34:01
 *
 */

#include "defs.h"
#include <term.h>

#include <ctype.h>

#ifdef USE_SCCS_IDS
static char const SCCSid[] = "@(#) mytinfo addstr.c 3.2 92/02/01 public domain, By Ross Ridge";
#endif
/*
 * I think this routine could be improved, as it is now it searches a
 * linked list of strbufs for one that has enough room left for the
 * string. The only thing else I can think of doing would be to
 * expand a buffer by realloc and then fix the string pointers if it
 * moves.
 */

static struct strbuf *strbuf = NULL;

struct strbuf *
_endstr() {
	register struct strbuf *p;

	p = strbuf;
	strbuf = NULL;
	return p;
}

char *
_addstr(s)
register char *s; {
	register struct strbuf *p;
	register int l;

	if (s == NULL) {
		strbuf = NULL;
		return NULL;
	}

	if (strbuf == NULL) {
		strbuf = (struct strbuf *) malloc(sizeof(struct strbuf));
		if (strbuf == NULL)
			return NULL;
		strbuf->len = 0;
		strbuf->next = NULL;
	}
	l = strlen(s) + 1;
	if (l > MAX_CHUNK)
		return NULL;
	p = strbuf;
	while (l + p->len > MAX_CHUNK) {
		if (p->next == NULL) {
			p->next = (struct strbuf *)
					malloc(sizeof(struct strbuf));
			p = p->next;
			if (p == NULL)
				return NULL;
			p->len = 0;
			p->next = NULL;
			break;
		}
		p = p->next;
	}
	s = strcpy(p->buf + p->len, s);
	p->len += l;
	return s;
}

void
_del_strs(p)
TERMINAL *p; {
	struct strbuf *q;

	q = p->strbuf;
	while(q != NULL) {
		p->strbuf = q->next;
		free((anyptr) q);
		q = p->strbuf;
	}
}
OpenPOWER on IntegriCloud