xref: /csrg-svn/usr.bin/window/string.c (revision 16233)
1 #ifndef lint
2 static	char *sccsid = "@(#)string.c	3.5 84/03/23";
3 #endif
4 
5 #include "string.h"
6 
7 char *malloc();
8 char *sprintf();
9 
10 char *
11 str_cpy(s)
12 register char *s;
13 {
14 	char *str;
15 	register char *p;
16 
17 	str = p = str_alloc(strlen(s) + 1);
18 	if (p == 0)
19 		return 0;
20 	while (*p++ = *s++)
21 		;
22 	return str;
23 }
24 
25 char *
26 str_itoa(i)
27 int i;
28 {
29 	char buf[30];
30 
31 	(void) sprintf(buf, "%d", i);
32 	return str_cpy(buf);
33 }
34 
35 char *
36 str_cat(s1, s2)
37 char *s1, *s2;
38 {
39 	char *str;
40 	register char *p, *q;
41 
42 	str = p = str_alloc(strlen(s1) + strlen(s2) + 1);
43 	if (p == 0)
44 		return 0;
45 	for (q = s1; *p++ = *q++;)
46 		;
47 	for (q = s2, p--; *p++ = *q++;)
48 		;
49 	return str;
50 }
51 
52 /*
53  * match s against p.
54  * s can be a prefix of p with at least min characters.
55  */
56 str_match(s, p, min)
57 register char *s, *p;
58 register min;
59 {
60 	for (; *s && *p && *s == *p; s++, p++, min--)
61 		;
62 	return *s == *p || *s == 0 && min <= 0;
63 }
64 
65 #ifdef STR_DEBUG
66 char *
67 str_alloc(l)
68 int l;
69 {
70 	register struct string *s;
71 
72 	s = (struct string *) malloc((unsigned)l + str_offset);
73 	if (s == 0)
74 		return 0;
75 	if (str_head.s_forw == 0)
76 		str_head.s_forw = str_head.s_back = &str_head;
77 	s->s_forw = str_head.s_forw;
78 	s->s_back = &str_head;
79 	str_head.s_forw = s;
80 	s->s_forw->s_back = s;
81 	return s->s_data;
82 }
83 
84 str_free(str)
85 char *str;
86 {
87 	register struct string *s;
88 
89 	for (s = str_head.s_forw; s != &str_head && s->s_data != str;
90 	     s = s->s_forw)
91 		;
92 	if (s == &str_head)
93 		abort();
94 	s->s_back->s_forw = s->s_forw;
95 	s->s_forw->s_back = s->s_back;
96 	free((char *)s);
97 }
98 #endif
99