-
Notifications
You must be signed in to change notification settings - Fork 1
/
netlibc.c
329 lines (293 loc) · 7.69 KB
/
netlibc.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* File MSNLIB.C
* Replacement for C library for use with MS-DOS Kermit.
*
* Copyright (C) 1982, 1999, Trustees of Columbia University in the
* City of New York. The MS-DOS Kermit software may not be, in whole
* or in part, licensed or sold for profit as a software product itself,
* nor may it be included in or distributed with commercial products
* or otherwise distributed by commercial concerns to their clients
* or customers without written permission of the Office of Kermit
* Development and Distribution, Columbia University. This copyright
* notice must not be removed, altered, or obscured.
*
* Last edit
* 12 Jan 1995 v3.14
* Authors: J.R.Doupnik, USU, Frank da Cruz, Columbia Univ.
* Contains:
* strchr, strcat, strncat, strcpy, strncpy, strlen, strcmp, stricmp, strncmp
* atoi, itoa, ltoa, isdigit, ntoa.
*/
#include "tcp.h"
#include "netlibc.h"
#ifndef MSDOS
/*
In MS-DOS Kermit, these are assembler routines to avoid math library.
*/
#define ourmod(a,b) (a % b)
#define ourdiv(a, b) (a / b)
#define ourlmod(a,b) (a % b)
#define ourldiv(a, b) (a / b)
#endif /* MSDOS */
#ifndef NULL
#define NULL 0
#endif /* NULL */
/*
By the way, there is probably no point in the next #ifndef,
because size_t is either built into to the compiler or typedef'd,
rather than defined. So the #define below will always happen.
*/
#ifndef size_t
#define size_t int
#endif /* size_t */
int _acrtused; /* MS C compiler startup file quantity */
/*
_strchr
Finds first occurence of character c in string s.
Returns pointer to it if found, NULL if not found.
*/
byte *
strchr(byte *s, const byte c) {
while ((*s != (byte)'\0') && (*s != (byte)(c & 0xff))) s++;
if (*s == '\0') return(NULL);
else return(s);
}
byte FAR *
strchrf(byte FAR *s, const byte c) {
while ((*s != (byte)'\0') && (*s != (byte)(c & 0xff))) s++;
if (*s == '\0') return(NULL);
else return(s);
}
/*
_strcat
Appends entire string s2 to string s1.
Assumes there is room for s2 after end of s1.
Returns pointer to s1 or NULL if s1 is a null pointer.
*/
byte *
strcat(byte *s1, byte *s2) {
register byte *p;
if (s1 == NULL) return(NULL);
if (s2 == NULL || *s2 == '\0') return(s1);
p = s1; /* Make copy */
while (*p) p++; /* Find end */
while (*p++ = *s2++); /* Copy thru terminating NUL */
return(s1); /* Return original */
}
/*
_strncat
Appends up to n chars of string s2 to string s1.
Returns pointer to string1 or NULL if s1 is a null pointer.
*/
byte *
strncat(byte *s1, byte *s2, size_t n) {
register byte * p;
if (s1 == NULL) return(NULL);
if (s2 == NULL || *s2 == '\0') return(s1);
p = s1; /* Copy pointer */
while (*p) p++; /* Step to end of s1 */
while ((*p++ = *s2++) && (--n > 0)); /* Copy up to n bytes of s2 */
return(s1); /* Return original pointer */
}
/*
_strcpy
Copies s2 to s1, returns pointer to s1 or NULL if s1 was NULL.
*/
byte *
strcpy(byte *s1, byte *s2) {
register byte *p;
if (s1 == NULL) return(NULL);
if (s2 == NULL) s2 = "";
p = s1; /* Copy pointer */
while (*p++ = *s2++); /* Copy thru terminating NUL */
return(s1); /* Return original pointer */
}
/*
_strncpy
Copies at most n characters from s2 to to s1, returns pointer to s1.
Returns s1 or NULL if s1 was NULL.
*/
byte *
strncpy(byte *s1, byte *s2, size_t n) {
register int s2len;
register byte *p1;
if (s1 == NULL) return(NULL);
if (s2 == NULL) s2 = "";
if ((s2len = strlen(s2)) > n) s2len = n;
p1 = s1;
while (s2len-- > 0) /* Copy */
*p1++ = *s2++;
*p1 = '\0'; /* Terminate */
return(s1); /* No need to pad out, one's enuf */
}
/*
_strlen
Returns length of null-terminated string not including '\0'
*/
size_t
strlen(byte *s) {
register int i = 0;
if (s == NULL) return(0);
while (*s++) i++;
return(i);
}
/*
_strcmp
Compare null-terminated strings using ASCII values.
Case matters. Returns:
< 0 if s1 < s2,
= 0 if s1 = s2,
> 0 if s1 > s2
*/
int
strcmp(byte *s1, byte *s2) {
if (s1 == NULL) s1 = "";
if (s2 == NULL) s2 = "";
do {
if (*s1 < *s2) return(-1);
if (*s1 > *s2) return(1);
if (*s2 == '\0') return(0);
s2++;
} while (*s1++);
return(0);
}
/*
_stricmp
Like strcmp but case insenstive
*/
int
stricmp(byte *s1, byte *s2) {
register byte c1, c2;
if (s1 == NULL) s1 = "";
if (s2 == NULL) s2 = "";
do {
c1 = *s1; c2 = *s2;
if ('a' <= c1 && c1 <= 'z') c1 = c1 - (byte)('a' - 'A');
if ('a' <= c2 && c2 <= 'z') c2 = c2 - (byte)('a' - 'A');
if (c1 < c2) return(-1);
if (c1 > c2) return(1);
if (c2 == '\0') return(0);
s1++; s2++;
} while (c1 != '\0');
return(0);
}
/*
_strncmp
Compares at most n characters of strings s1 and s2.
*/
int
strncmp(byte *s1, byte *s2, size_t n) {
if (s1 == NULL) s1 = "";
if (s2 == NULL) s2 = "";
while (n-- > 0 && *s1) {
if (*s1 < *s2) return(-1);
if (*s1 > *s2) return(1);
s1++; s2++;
}
return(0);
}
/*
_atoi
Converts decimal numeric string to integer.
Breaks on first non-digit or end of string.
Returns integer.
*/
int
atoi(byte *s) {
register int i, count;
count = 0;
for (i = 0; i < 18; i++) {
if (*s < '0' || *s > '9') break;
count *= 10;
count += *s - '0'; /* ascii to binary */
s++;
}
return(count);
}
/*
_itoa
Converts integer value to ASCII digits (up to 18 characters long),
stores in string, null terminated. Returns NULL on failure,
pointer to result on success.
*/
byte *
itoa(int value, byte *string, int radix) { /* From K & R */
int c, j, sign;
register int i;
register byte *s;
if (string == NULL) return(NULL);
s = string;
if ((sign = value) < 0) /* Save sign */
value = - value; /* Force value positive */
i = 0;
do {
s[i++] = (byte)(ourmod(value, radix) + '0');
} while ((value = ourdiv(value, radix)) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
j = strlen(s) -1;
for (i = 0; i < j; i++, j--) {
c = s[i];
if (c > '9') c = c - '9' + 'A' -1;
s[i] = s[j];
s[j] = (byte)(c & 0xff);
}
return(string);
}
/*
_ltoa
Like itoa() but using long value ( < 34 ).
*/
byte *
ltoa(long value, byte *string, int radix) { /* K & R */
int c, j;
register int i;
long sign;
register byte * s;
if (string == NULL) return(NULL);
s = string;
if ((sign = value) < 0) value = - value; /* value to positive*/
i = 0;
do {
s[i++] = (byte)(ourlmod(value, radix) + '0');
} while ((value = ourldiv(value, radix)) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
j = strlen(s) - 1;
for (i = 0; i < j; i++, j--) {
c = s[i];
if (c > '9') c = c - '9' + 'A' -1;
s[i] = s[j];
s[j] = (byte)(c & 0xff);
}
return(string);
}
/*
_isdigit
Returns 1 if argument is a decimal digit, 0 otherwise.
*/
int
isdigit(const byte c) {
if ((c & 0xff) < '0' || (c & 0xff) > '9')
return(0); /* say is not a digit */
return(1);
}
/*
Convert long val to dotted decimal string at pointer p, does high order
byte first. Intended to yield dotted decimal IP addresses from longs.
*/
void
ntoa(byte *p, unsigned long val)
{
register byte *ptr;
register int i;
ptr = p;
for (i = 24; i >= 0; i -= 8)
{
itoa((int)((val >> i) & 0xff), ptr, 10); /* convert a byte */
strcat(ptr, "."); /* dot separator */
ptr = p + strlen(p);
}
*(--ptr) = '\0'; /* remove trailing dot */
}