1 /* $OpenBSD: string.c,v 1.8 2003/08/01 22:01:37 david Exp $ */
2 /* $NetBSD: string.c,v 1.5 1995/09/29 00:44:06 cgd Exp $ */
3
4 /*
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Edward Wang at The University of California, Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)string.c 8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$OpenBSD: string.c,v 1.8 2003/08/01 22:01:37 david Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include "string.h"
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 char *
str_cpy(s)50 str_cpy(s)
51 char *s;
52 {
53 char *str;
54 char *p;
55
56 str = p = str_alloc(strlen(s) + 1);
57 if (p == 0)
58 return 0;
59 while (*p++ = *s++)
60 ;
61 return str;
62 }
63
64 char *
str_ncpy(s,n)65 str_ncpy(s, n)
66 char *s;
67 int n;
68 {
69 int l = strlen(s);
70 char *str;
71 char *p;
72
73 if (n > l)
74 n = l;
75 str = p = str_alloc(n + 1);
76 if (p == 0)
77 return 0;
78 while (--n >= 0)
79 *p++ = *s++;
80 *p = 0;
81 return str;
82 }
83
84 char *
str_itoa(i)85 str_itoa(i)
86 int i;
87 {
88 char buf[30];
89
90 (void) snprintf(buf, sizeof(buf), "%d", i);
91 return str_cpy(buf);
92 }
93
94 char *
str_cat(s1,s2)95 str_cat(s1, s2)
96 char *s1, *s2;
97 {
98 char *str;
99 char *p, *q;
100
101 str = p = str_alloc(strlen(s1) + strlen(s2) + 1);
102 if (p == 0)
103 return 0;
104 for (q = s1; *p++ = *q++;)
105 ;
106 for (q = s2, p--; *p++ = *q++;)
107 ;
108 return str;
109 }
110
111 /*
112 * match s against p.
113 * s can be a prefix of p with at least min characters.
114 */
str_match(s,p,min)115 str_match(s, p, min)
116 char *s, *p;
117 int min;
118 {
119 for (; *s && *p && *s == *p; s++, p++, min--)
120 ;
121 return *s == *p || *s == 0 && min <= 0;
122 }
123
124 #ifdef STR_DEBUG
125 char *
str_alloc(l)126 str_alloc(l)
127 int l;
128 {
129 struct string *s;
130
131 s = (struct string *) malloc(l + str_offset);
132 if (s == 0)
133 return 0;
134 if (str_head.s_forw == 0)
135 str_head.s_forw = str_head.s_back = &str_head;
136 s->s_forw = str_head.s_forw;
137 s->s_back = &str_head;
138 str_head.s_forw = s;
139 s->s_forw->s_back = s;
140 return s->s_data;
141 }
142
str_free(str)143 str_free(str)
144 char *str;
145 {
146 struct string *s;
147
148 for (s = str_head.s_forw; s != &str_head && s->s_data != str;
149 s = s->s_forw)
150 ;
151 if (s == &str_head)
152 abort();
153 s->s_back->s_forw = s->s_forw;
154 s->s_forw->s_back = s->s_back;
155 free((char *)s);
156 }
157 #endif
158