|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved.
4: *
5: * This code is derived from software contributed to Berkeley by
6: * Edward Wang at The University of California, Berkeley.
7: *
8: * Redistribution and use in source and binary forms are permitted provided
9: * that: (1) source distributions retain this entire copyright notice and
10: * comment, and (2) distributions including binaries display the following
11: * acknowledgement: ``This product includes software developed by the
12: * University of California, Berkeley and its contributors'' in the
13: * documentation or other materials provided with the distribution and in
14: * all advertising materials mentioning features or use of this software.
15: * Neither the name of the University nor the names of its contributors may
16: * be used to endorse or promote products derived from this software without
17: * specific prior written permission.
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21: */
22:
23: #ifndef lint
24: static char sccsid[] = "@(#)string.c 3.13 (Berkeley) 6/6/90";
25: #endif /* not lint */
26:
27: #include "string.h"
28:
29: char *malloc();
30:
31: char *
32: str_cpy(s)
33: register char *s;
34: {
35: char *str;
36: register char *p;
37:
38: str = p = str_alloc(strlen(s) + 1);
39: if (p == 0)
40: return 0;
41: while (*p++ = *s++)
42: ;
43: return str;
44: }
45:
46: char *
47: str_ncpy(s, n)
48: register char *s;
49: register n;
50: {
51: int l = strlen(s);
52: char *str;
53: register char *p;
54:
55: if (n > l)
56: n = l;
57: str = p = str_alloc(n + 1);
58: if (p == 0)
59: return 0;
60: while (--n >= 0)
61: *p++ = *s++;
62: *p = 0;
63: return str;
64: }
65:
66: char *
67: str_itoa(i)
68: int i;
69: {
70: char buf[30];
71:
72: (void) sprintf(buf, "%d", i);
73: return str_cpy(buf);
74: }
75:
76: char *
77: str_cat(s1, s2)
78: char *s1, *s2;
79: {
80: char *str;
81: register char *p, *q;
82:
83: str = p = str_alloc(strlen(s1) + strlen(s2) + 1);
84: if (p == 0)
85: return 0;
86: for (q = s1; *p++ = *q++;)
87: ;
88: for (q = s2, p--; *p++ = *q++;)
89: ;
90: return str;
91: }
92:
93: /*
94: * match s against p.
95: * s can be a prefix of p with at least min characters.
96: */
97: str_match(s, p, min)
98: register char *s, *p;
99: register min;
100: {
101: for (; *s && *p && *s == *p; s++, p++, min--)
102: ;
103: return *s == *p || *s == 0 && min <= 0;
104: }
105:
106: #ifdef STR_DEBUG
107: char *
108: str_alloc(l)
109: int l;
110: {
111: register struct string *s;
112:
113: s = (struct string *) malloc((unsigned)l + str_offset);
114: if (s == 0)
115: return 0;
116: if (str_head.s_forw == 0)
117: str_head.s_forw = str_head.s_back = &str_head;
118: s->s_forw = str_head.s_forw;
119: s->s_back = &str_head;
120: str_head.s_forw = s;
121: s->s_forw->s_back = s;
122: return s->s_data;
123: }
124:
125: str_free(str)
126: char *str;
127: {
128: register struct string *s;
129:
130: for (s = str_head.s_forw; s != &str_head && s->s_data != str;
131: s = s->s_forw)
132: ;
133: if (s == &str_head)
134: abort();
135: s->s_back->s_forw = s->s_forw;
136: s->s_forw->s_back = s->s_back;
137: free((char *)s);
138: }
139: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.