-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
259 lines (235 loc) · 4.52 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// misc.c
//
// Miscellaneous routines for the operating system. These include
// basic routines that would be in libc if we had access to it.
//
#include "misc.h"
//----------------------------------------------------------------------
//
// dstrcpy
// dstrncpy
// dstrcat
//
// These three functions do exactly what their counterparts in the
// standard C library do.
//
//----------------------------------------------------------------------
char*
dstrcpy (char *to, const char *from)
{
char *tokeep = to;
char last;
do {
last = *(to++) = *(from++);
} while (last != '\0');
return (tokeep);
}
char*
dstrncpy (char *to, const char *from, int n)
{
char *tokeep = to;
char last;
int cur = 0;
do {
// Stop if we're exceeded the maximum string length
if (n-- <= 0) {
break;
}
last = *(to++) = *(from++);
} while (last != '\0');
return (tokeep);
}
char *
dstrcat (char *onto, const char *addn)
{
char *konto = onto;
while (*onto != '\0') {
onto++;
}
dstrcpy (onto, addn);
return (konto);
}
int
dstrncmp (const char *s1, const char *s2, int n)
{
int i;
for (i = 0; i < n; i++) {
// If they don't match, end the loop
if (*s2 == '\0') {
// If the second string is NULL, we're at the end of the string and
// have a match.
return (0);
} else if (*s1 != *s2) {
break;
}
s1++;
s2++;
}
if (i == n) {
return (0);
} else {
return (((*s1 - *s2) < 0) ? -1 : ((*s1 == *s2) ? 0 : 1));
}
}
int
dstrlen (const char *s)
{
int i = 0;
while (*(s++) != '\0') {
i++;
}
return (i);
}
const char *
dstrstr (const char *buf, const char *pattern)
{
int n = dstrlen (pattern);
while (*buf != '\0') {
if (dstrncmp (buf, pattern, n) == 0) {
return (buf);
}
buf++;
}
return ((char *)0);
}
//----------------------------------------------------------------------
//
// dindex
// dmindex
//
// dindex is identical to the libc function index. dmindex is
// similar to dindex, but it reports the index in the string of the
// first match for any one of the characters passed (rather than
// matching only on a single specific character).
//
//----------------------------------------------------------------------
const char *
dmindex (const char *s, const char *match)
{
const char *c;
while (*s != '\0') {
for (c = match; *c != '\0'; c++) {
if (*s == *c) {
return (s);
}
}
}
return ((const char *)0);
}
const char*
dindex (const char *s, int c)
{
while (*s != '\0') {
if (*s == c) {
return (s);
}
s++;
}
return ((char *)0);
}
//----------------------------------------------------------------------
//
// ditoa: Convert an integer into an ASCII number.
// dstrtol: Convert ASCII into an integer
//
//----------------------------------------------------------------------
void
ditoa (int n, char *buf)
{
int k;
if (n < 0) {
*(buf++) = '-';
n = -n;
} else if (n == 0) {
*(buf++) = '0';
} else {
for (k = 1; k <= n; k *= 10) {
}
do {
k /= 10;
*(buf++) = (n / k) + '0';
n %= k;
} while (k > 1);
}
*buf = '\0';
}
int
dstrtol (char *c, char **newpos, int base)
{
int v = 0;
int curdigit;
int sign;
while ((*c == ' ') || (*c == '\t') || (*c == '\n')) {
c++;
}
if (newpos != (char **)0) {
*newpos = c;
}
if (*c == '\0') {
return (v);
}
if (*c == '-') {
sign = -1;
c++;
} else {
sign = 1;
}
if (base == 0) {
if (*c == '0') {
c++;
if ((*c == 'x') || (*c == 'X')) {
base = 16;
c++;
} else {
base = 8;
}
} else {
base = 10;
}
}
if ((base <= 0) || (base > 16)) {
return (v);
}
do {
if ((*c >= '0') && (*c <= '9')) {
curdigit = (*c) - '0';
} else if ((*c >= 'a') && (*c <= 'z')) {
curdigit = (*c) + 10 - 'a';
} else if ((*c >= 'A') && (*c <= 'Z')) {
curdigit = (*c) + 10 - 'A';
} else {
curdigit = 1000;
}
if (curdigit < base) {
v *= base;
v += curdigit;
c++;
}
} while (curdigit < base);
if (newpos != (char **)0) {
*newpos = c;
}
v *= sign;
return (v);
}
//----------------------------------------------------------------------
//
// bcopy: Copy bytes from one location to another.
// bzero: Set all the bytes in a region to zero.
//
//----------------------------------------------------------------------
void
bcopy (char *src, char *dst, int count)
{
while (count-- > 0) {
*(dst++) = *(src++);
}
}
void
bzero (char *dst, int count)
{
while (count-- > 0) {
*(dst++) = 0;
}
}