Commit | Line | Data |
---|---|---|
60de5df9 | 1 | /* |
46e9ea25 KB |
2 | * Copyright (c) 1983 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
5e8b0e60 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
60de5df9 EW |
16 | */ |
17 | ||
46e9ea25 | 18 | #ifndef lint |
5e8b0e60 | 19 | static char sccsid[] = "@(#)string.c 3.11 (Berkeley) %G%"; |
46e9ea25 KB |
20 | #endif /* not lint */ |
21 | ||
e53fdda9 EW |
22 | #include "string.h" |
23 | ||
24 | char *malloc(); | |
25 | ||
26 | char * | |
27 | str_cpy(s) | |
28 | register char *s; | |
29 | { | |
30 | char *str; | |
31 | register char *p; | |
32 | ||
90ede912 | 33 | str = p = str_alloc(strlen(s) + 1); |
e53fdda9 EW |
34 | if (p == 0) |
35 | return 0; | |
36 | while (*p++ = *s++) | |
37 | ; | |
38 | return str; | |
39 | } | |
40 | ||
0180c2d9 EW |
41 | char * |
42 | str_ncpy(s, n) | |
43 | register char *s; | |
44 | register n; | |
45 | { | |
46 | int l = strlen(s); | |
47 | char *str; | |
48 | register char *p; | |
49 | ||
50 | if (n > l) | |
51 | n = l; | |
52 | str = p = str_alloc(n + 1); | |
53 | if (p == 0) | |
54 | return 0; | |
55 | while (--n >= 0) | |
56 | *p++ = *s++; | |
57 | *p = 0; | |
58 | return str; | |
59 | } | |
60 | ||
e53fdda9 EW |
61 | char * |
62 | str_itoa(i) | |
63 | int i; | |
64 | { | |
65 | char buf[30]; | |
66 | ||
67 | (void) sprintf(buf, "%d", i); | |
68 | return str_cpy(buf); | |
69 | } | |
70 | ||
71 | char * | |
72 | str_cat(s1, s2) | |
73 | char *s1, *s2; | |
74 | { | |
75 | char *str; | |
76 | register char *p, *q; | |
77 | ||
90ede912 | 78 | str = p = str_alloc(strlen(s1) + strlen(s2) + 1); |
e53fdda9 EW |
79 | if (p == 0) |
80 | return 0; | |
81 | for (q = s1; *p++ = *q++;) | |
82 | ; | |
83 | for (q = s2, p--; *p++ = *q++;) | |
84 | ; | |
85 | return str; | |
86 | } | |
87 | ||
e53fdda9 EW |
88 | /* |
89 | * match s against p. | |
90 | * s can be a prefix of p with at least min characters. | |
91 | */ | |
92 | str_match(s, p, min) | |
93 | register char *s, *p; | |
94 | register min; | |
95 | { | |
96 | for (; *s && *p && *s == *p; s++, p++, min--) | |
97 | ; | |
98 | return *s == *p || *s == 0 && min <= 0; | |
99 | } | |
90ede912 EW |
100 | |
101 | #ifdef STR_DEBUG | |
90ede912 EW |
102 | char * |
103 | str_alloc(l) | |
104 | int l; | |
105 | { | |
106 | register struct string *s; | |
107 | ||
108 | s = (struct string *) malloc((unsigned)l + str_offset); | |
109 | if (s == 0) | |
110 | return 0; | |
4222244b EW |
111 | if (str_head.s_forw == 0) |
112 | str_head.s_forw = str_head.s_back = &str_head; | |
90ede912 EW |
113 | s->s_forw = str_head.s_forw; |
114 | s->s_back = &str_head; | |
115 | str_head.s_forw = s; | |
116 | s->s_forw->s_back = s; | |
117 | return s->s_data; | |
118 | } | |
119 | ||
120 | str_free(str) | |
121 | char *str; | |
122 | { | |
c116b1d3 | 123 | register struct string *s; |
90ede912 EW |
124 | |
125 | for (s = str_head.s_forw; s != &str_head && s->s_data != str; | |
126 | s = s->s_forw) | |
127 | ; | |
128 | if (s == &str_head) | |
129 | abort(); | |
130 | s->s_back->s_forw = s->s_forw; | |
131 | s->s_forw->s_back = s->s_back; | |
c116b1d3 | 132 | free((char *)s); |
90ede912 EW |
133 | } |
134 | #endif |