Commit | Line | Data |
---|---|---|
e53fdda9 | 1 | #ifndef lint |
60de5df9 | 2 | static char sccsid[] = "@(#)string.c 3.8 %G%"; |
e53fdda9 EW |
3 | #endif |
4 | ||
60de5df9 EW |
5 | /* |
6 | * Copyright (c) 1983 Regents of the University of California, | |
7 | * All rights reserved. Redistribution permitted subject to | |
8 | * the terms of the Berkeley Software License Agreement. | |
9 | */ | |
10 | ||
e53fdda9 EW |
11 | #include "string.h" |
12 | ||
13 | char *malloc(); | |
a639e3be | 14 | char *sprintf(); |
e53fdda9 EW |
15 | |
16 | char * | |
17 | str_cpy(s) | |
18 | register char *s; | |
19 | { | |
20 | char *str; | |
21 | register char *p; | |
22 | ||
90ede912 | 23 | str = p = str_alloc(strlen(s) + 1); |
e53fdda9 EW |
24 | if (p == 0) |
25 | return 0; | |
26 | while (*p++ = *s++) | |
27 | ; | |
28 | return str; | |
29 | } | |
30 | ||
0180c2d9 EW |
31 | char * |
32 | str_ncpy(s, n) | |
33 | register char *s; | |
34 | register n; | |
35 | { | |
36 | int l = strlen(s); | |
37 | char *str; | |
38 | register char *p; | |
39 | ||
40 | if (n > l) | |
41 | n = l; | |
42 | str = p = str_alloc(n + 1); | |
43 | if (p == 0) | |
44 | return 0; | |
45 | while (--n >= 0) | |
46 | *p++ = *s++; | |
47 | *p = 0; | |
48 | return str; | |
49 | } | |
50 | ||
e53fdda9 EW |
51 | char * |
52 | str_itoa(i) | |
53 | int i; | |
54 | { | |
55 | char buf[30]; | |
56 | ||
57 | (void) sprintf(buf, "%d", i); | |
58 | return str_cpy(buf); | |
59 | } | |
60 | ||
61 | char * | |
62 | str_cat(s1, s2) | |
63 | char *s1, *s2; | |
64 | { | |
65 | char *str; | |
66 | register char *p, *q; | |
67 | ||
90ede912 | 68 | str = p = str_alloc(strlen(s1) + strlen(s2) + 1); |
e53fdda9 EW |
69 | if (p == 0) |
70 | return 0; | |
71 | for (q = s1; *p++ = *q++;) | |
72 | ; | |
73 | for (q = s2, p--; *p++ = *q++;) | |
74 | ; | |
75 | return str; | |
76 | } | |
77 | ||
e53fdda9 EW |
78 | /* |
79 | * match s against p. | |
80 | * s can be a prefix of p with at least min characters. | |
81 | */ | |
82 | str_match(s, p, min) | |
83 | register char *s, *p; | |
84 | register min; | |
85 | { | |
86 | for (; *s && *p && *s == *p; s++, p++, min--) | |
87 | ; | |
88 | return *s == *p || *s == 0 && min <= 0; | |
89 | } | |
90ede912 EW |
90 | |
91 | #ifdef STR_DEBUG | |
90ede912 EW |
92 | char * |
93 | str_alloc(l) | |
94 | int l; | |
95 | { | |
96 | register struct string *s; | |
97 | ||
98 | s = (struct string *) malloc((unsigned)l + str_offset); | |
99 | if (s == 0) | |
100 | return 0; | |
4222244b EW |
101 | if (str_head.s_forw == 0) |
102 | str_head.s_forw = str_head.s_back = &str_head; | |
90ede912 EW |
103 | s->s_forw = str_head.s_forw; |
104 | s->s_back = &str_head; | |
105 | str_head.s_forw = s; | |
106 | s->s_forw->s_back = s; | |
107 | return s->s_data; | |
108 | } | |
109 | ||
110 | str_free(str) | |
111 | char *str; | |
112 | { | |
c116b1d3 | 113 | register struct string *s; |
90ede912 EW |
114 | |
115 | for (s = str_head.s_forw; s != &str_head && s->s_data != str; | |
116 | s = s->s_forw) | |
117 | ; | |
118 | if (s == &str_head) | |
119 | abort(); | |
120 | s->s_back->s_forw = s->s_forw; | |
121 | s->s_forw->s_back = s->s_back; | |
c116b1d3 | 122 | free((char *)s); |
90ede912 EW |
123 | } |
124 | #endif |