xref: /csrg-svn/usr.bin/window/string.c (revision 15565)
1 #ifndef lint
2 static	char *sccsid = "@(#)string.c	3.1 83/11/22";
3 #endif
4 
5 #include "string.h"
6 
7 char *malloc();
8 
9 char *
10 str_cpy(s)
11 register char *s;
12 {
13 	char *str;
14 	register char *p;
15 
16 	str = p = malloc((unsigned) strlen(s) + 1);
17 	if (p == 0)
18 		return 0;
19 	while (*p++ = *s++)
20 		;
21 	return str;
22 }
23 
24 char *
25 str_itoa(i)
26 int i;
27 {
28 	char buf[30];
29 
30 	(void) sprintf(buf, "%d", i);
31 	return str_cpy(buf);
32 }
33 
34 char *
35 str_cat(s1, s2)
36 char *s1, *s2;
37 {
38 	char *str;
39 	register char *p, *q;
40 
41 	str = p = malloc((unsigned) strlen(s1) + strlen(s2) + 1);
42 	if (p == 0)
43 		return 0;
44 	for (q = s1; *p++ = *q++;)
45 		;
46 	for (q = s2, p--; *p++ = *q++;)
47 		;
48 	return str;
49 }
50 
51 str_free(str)
52 char *str;
53 {
54 	extern char end[];
55 
56 	if (str >= end)
57 		free(str);
58 }
59 
60 /*
61  * match s against p.
62  * s can be a prefix of p with at least min characters.
63  */
64 str_match(s, p, min)
65 register char *s, *p;
66 register min;
67 {
68 	for (; *s && *p && *s == *p; s++, p++, min--)
69 		;
70 	return *s == *p || *s == 0 && min <= 0;
71 }
72