xref: /csrg-svn/usr.bin/window/string.c (revision 15824)
1 #ifndef lint
2 static	char *sccsid = "@(#)string.c	3.4 84/01/09";
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 struct string str_head = {
67 	&str_head, &str_head
68 };
69 
70 char *
71 str_alloc(l)
72 int l;
73 {
74 	register struct string *s;
75 
76 	s = (struct string *) malloc((unsigned)l + str_offset);
77 	if (s == 0)
78 		return 0;
79 	s->s_forw = str_head.s_forw;
80 	s->s_back = &str_head;
81 	str_head.s_forw = s;
82 	s->s_forw->s_back = s;
83 	return s->s_data;
84 }
85 
86 str_free(str)
87 char *str;
88 {
89 	register struct string *s;
90 
91 	for (s = str_head.s_forw; s != &str_head && s->s_data != str;
92 	     s = s->s_forw)
93 		;
94 	if (s == &str_head)
95 		abort();
96 	s->s_back->s_forw = s->s_forw;
97 	s->s_forw->s_back = s->s_back;
98 	free((char *)s);
99 }
100 #endif
101