Chameleon

Chameleon Commit Details

Date:2011-08-06 18:27:15 (8 years 17 days ago)
Author:Evan Lojewski
Commit:1292
Parents: 1291
Message:Add additional functions to klibc. Remove duplicates from libsa
Changes:
A/branches/xZenu/src/modules/klibc/strlcpy.c
A/branches/xZenu/src/modules/klibc/strcat.c
A/branches/xZenu/src/modules/klibc/strstr.c
A/branches/xZenu/src/modules/klibc/strncpy.c
A/branches/xZenu/src/modules/klibc/strchr.c
A/branches/xZenu/src/modules/klibc/strlen.c
A/branches/xZenu/src/modules/klibc/strcmp.c
A/branches/xZenu/src/modules/klibc/strncat.c
A/branches/xZenu/src/modules/klibc/memcmp.c
A/branches/xZenu/src/modules/klibc/bzero.c
A/branches/xZenu/src/modules/klibc/zalloc.c
A/branches/xZenu/src/modules/klibc/strcpy.c
A/branches/xZenu/src/modules/klibc/strncmp.c
A/branches/xZenu/src/modules/klibc/memcpy.c
A/branches/xZenu/src/modules/klibc/memset.c
M/branches/xZenu/src/arch/i386/libsa/Makefile
M/branches/xZenu/src/arch/i386/libsa/string.c
M/branches/xZenu/src/arch/i386/cdboot/Makefile
M/branches/xZenu/src/modules/klibc/strdup.c
M/branches/xZenu/src/arch/i386/boot2/Makefile
M/branches/xZenu/src/modules/klibc/Makefile

File differences

branches/xZenu/src/modules/klibc/strcpy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
* strcpy.c
*
* strcpy()
*/
#include <string.h>
char *strcpy(char *dst, const char *src)
{
char *q = dst;
const char *p = src;
char ch;
do {
*q++ = ch = *p++;
} while (ch);
return dst;
}
branches/xZenu/src/modules/klibc/Makefile
88
99
1010
11
11
1212
1313
1414
1515
16
16
1717
1818
1919
2020
2121
2222
23
23
24
2425
2526
2627
DIR = klibc
MODULE_OBJS = klibc \
MODULE_OBJS = klibc zalloc\
__ashldi3 __ashrdi3 __clzsi2 __divdi3 __divsi3 \
__lshrdi3 __moddi3 __modsi3 __udivdi3 \
__udivmoddi4 __udivmodsi4 __udivsi3 \
__umoddi3 __umodsi3 \
strntoumax strntoimax atol atoll \
strntoumax strntoimax atoi atol atoll \
strcasecmp strncasecmp strlcat strndup strnlen \
strsep strtoimax strtok_r strtok strtol strtoll \
strtotimespec strtotimeval \
strtoul strtoull strtoumax strxspn strpbrk \
bsearch calloc \
jrand48 lrand48 mrand48 srand48 nrand48 seed48 \
memccpy memchr memmem memmove memrchr memswap \
memccpy memcpy memset bzero memchr memmem memmove memrchr memswap memcmp \
strcmp strncmp strcpy strncpy strlcpy strstr strncat strcat strdup strncasecmp strchr strlen \
qsort sha1hash onexit atexit exit \
snprintf vsnprintf sscanf vsscanf\
fwrite fprintf vfprintf printf
branches/xZenu/src/modules/klibc/strlcpy.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
/*
* strlcpy.c
*/
#include <string.h>
size_t strlcpy(char *dst, const char *src, size_t size)
{
size_t bytes = 0;
char *q = dst;
const char *p = src;
char ch;
while ((ch = *p++)) {
if (bytes + 1 < size)
*q++ = ch;
bytes++;
}
/* If size == 0 there is no space for a final null... */
if (size)
*q = '\0';
return bytes;
}
branches/xZenu/src/modules/klibc/strcat.c
1
2
3
4
5
6
7
8
9
10
11
/*
* strcat.c
*/
#include <string.h>
char *strcat(char *dst, const char *src)
{
strcpy(strchr(dst, '\0'), src);
return dst;
}
branches/xZenu/src/modules/klibc/strlen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
* strlen()
*/
#include <string.h>
size_t strlen(const char *s)
{
const char *ss = s;
while (*ss)
ss++;
return ss - s;
}
branches/xZenu/src/modules/klibc/strcmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* strcmp.c
*/
#include <string.h>
int strcmp(const char *s1, const char *s2)
{
const unsigned char *c1 = (const unsigned char *)s1;
const unsigned char *c2 = (const unsigned char *)s2;
unsigned char ch;
int d = 0;
while (1) {
d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
break;
}
return d;
}
branches/xZenu/src/modules/klibc/bzero.c
1
2
3
4
5
6
#include <string.h>
void bzero(void *dst, size_t n)
{
memset(dst, 0, n);
}
branches/xZenu/src/modules/klibc/strdup.c
22
33
44
5
5
6
67
78
89
* strdup.c
*/
#include "libsaio.h"
#include <string.h>
#include <stdlib.h>
char *strdup(const char *s)
{
branches/xZenu/src/modules/klibc/zalloc.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
330
331
/*
* Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
* Reserved. This file contains Original Code and/or Modifications of
* Original Code as defined in and that are subject to the Apple Public
* Source License Version 2.0 (the "License"). You may not use this file
* except in compliance with the License. Please obtain a copy of the
* License at http://www.apple.com/publicsource and read it before using
* this file.
*
* The Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* Copyright 1993 NeXT Computer, Inc.
* All rights reserved.
*
* Sam's simple memory allocator.
*
*/
#include "libsa.h"
//#include "saio_internal.h" - Azi: needed for ZDEBUG (printf)
#include "memory.h"
#define ZDEBUG 0 //Azi: booter doesn't load with this enabled; instant reboot at "boot1: ..."
#if ZDEBUG
int zout;
#endif
typedef struct {
char * start;
size_t size;
} zmem;
static zmem * zalloced;
static zmem * zavailable;
static short availableNodes, allocedNodes, totalNodes;
static char * zalloc_base;
static char * zalloc_end;
static void (*zerror)(char *, size_t, const char *, int);
static void zallocate(char * start,int size);
static void zinsert(zmem * zp, int ndx);
static void zdelete(zmem * zp, int ndx);
static void zcoalesce(void);
#if ZDEBUG
size_t zalloced_size;
#endif
#define ZALLOC_NODES16384
static void malloc_error(char *addr, size_t size, const char *file, int line)
{
#ifdef i386
asm volatile ("hlt");
#endif
}
// define the block of memory that the allocator will use
void malloc_init(char * start, int size, int nodes, void (*malloc_err_fn)(char *, size_t, const char *, int))
{
zalloc_base = start ? start : (char *)ZALLOC_ADDR;
totalNodes = nodes ? nodes : ZALLOC_NODES;
zalloced = (zmem *) zalloc_base;
zavailable = (zmem *) zalloc_base + sizeof(zmem) * totalNodes;
zavailable[0].start = (char *)zavailable + sizeof(zmem) * totalNodes;
if (size == 0) size = ZALLOC_LEN;
zavailable[0].size = size - (zavailable[0].start - zalloc_base);
zalloc_end = zalloc_base + size;
availableNodes = 1;
allocedNodes = 0;
zerror = malloc_err_fn ? malloc_err_fn : malloc_error;
}
#define BEST_FIT 1
#undef malloc
void *malloc(size_t size)
{
return safe_malloc(size, __FILE__, __LINE__);
}
void * safe_malloc(size_t size, const char *file, int line)
{
int i;
#if BEST_FIT
int bestFit;
size_t smallestSize;
#endif
char * ret = 0;
if ( !zalloc_base )
{
// this used to follow the bss but some bios' corrupted it...
malloc_init((char *)ZALLOC_ADDR, ZALLOC_LEN, ZALLOC_NODES, malloc_error);
}
size = ((size + 0xf) & ~0xf);
if (size == 0) {
if (zerror) (*zerror)((char *)0xdeadbeef, 0, file, line);
}
#if BEST_FIT
smallestSize = 0;
bestFit = -1;
#endif
for (i = 0; i < availableNodes; i++)
{
// find node with equal size, or if not found,
// then smallest node that fits.
if ( zavailable[i].size == size )
{
zallocate(ret = zavailable[i].start, size);
zdelete(zavailable, i); availableNodes--;
goto done;
}
#if BEST_FIT
else
{
if ((zavailable[i].size > size) &&
((smallestSize == 0) ||
(zavailable[i].size < smallestSize)))
{
bestFit = i;
smallestSize = zavailable[i].size;
}
}
#else
else if ( zavailable[i].size > size )
{
zallocate(ret = zavailable[i].start, size);
zavailable[i].start += size;
zavailable[i].size -= size;
goto done;
}
#endif
}
#if BEST_FIT
if (bestFit != -1)
{
zallocate(ret = zavailable[bestFit].start, size);
zavailable[bestFit].start += size;
zavailable[bestFit].size -= size;
}
#endif
done:
if ((ret == 0) || (ret + size >= zalloc_end))
{
if (zerror) (*zerror)(ret, size, file, line);
}
if (ret != 0)
{
bzero(ret, size);
}
#if ZDEBUG
zalloced_size += size;
#endif
return (void *) ret;
}
void free(void * pointer)
{
unsigned long rp;
int i, found = 0;
size_t tsize = 0;
char * start = pointer;
#if i386
// Get return address of our caller,
// in case we have to report an error below.
asm volatile ("movl %%esp, %%eax\n\t"
"subl $4, %%eax\n\t"
"movl 0(%%eax), %%eax" : "=a" (rp) );
#else
rp = 0;
#endif
if ( !start ) return;
for (i = 0; i < allocedNodes; i++)
{
if ( zalloced[i].start == start )
{
tsize = zalloced[i].size;
#if ZDEBUG
zout -= tsize;
printf(" zz out %d\n",zout);
#endif
zdelete(zalloced, i); allocedNodes--;
found = 1;
#if ZDEBUG
memset(pointer, 0x5A, tsize);
#endif
break;
}
}
if ( !found ) {
if (zerror) (*zerror)(pointer, rp, "free", 0);
else return;
}
#if ZDEBUG
zalloced_size -= tsize;
#endif
for (i = 0; i < availableNodes; i++)
{
if ((start + tsize) == zavailable[i].start) // merge it in
{
zavailable[i].start = start;
zavailable[i].size += tsize;
zcoalesce();
return;
}
if ((i > 0) &&
(zavailable[i-1].start + zavailable[i-1].size == start))
{
zavailable[i-1].size += tsize;
zcoalesce();
return;
}
if ((start + tsize) < zavailable[i].start)
{
if (++availableNodes > totalNodes) {
if (zerror) (*zerror)((char *)0xf000f000, 0, "free", 0);
}
zinsert(zavailable, i);
zavailable[i].start = start;
zavailable[i].size = tsize;
return;
}
}
if (++availableNodes > totalNodes) {
if (zerror) (*zerror)((char *)0xf000f000, 1, "free", 0);
}
zavailable[i].start = start;
zavailable[i].size = tsize;
zcoalesce();
return;
}
static void
zallocate(char * start,int size)
{
#if ZDEBUG
zout += size;
printf(" alloc %d, total 0x%x\n",size,zout);
#endif
zalloced[allocedNodes].start = start;
zalloced[allocedNodes].size = size;
if (++allocedNodes > totalNodes) {
if (zerror) (*zerror)((char *)0xf000f000, 2, "zallocate", 0);
};
}
static void
zinsert(zmem * zp, int ndx)
{
int i;
zmem *z1, *z2;
i = totalNodes-2;
z1 = zp + i;
z2 = z1 + 1;
for (; i >= ndx; i--, z1--, z2--)
{
*z2 = *z1;
}
}
static void
zdelete(zmem * zp, int ndx)
{
int i;
zmem *z1, *z2;
z1 = zp + ndx;
z2 = z1 + 1;
for (i = ndx; i < totalNodes-1; i++, z1++, z2++)
{
*z1 = *z2;
}
}
static void
zcoalesce(void)
{
int i;
for (i = 0; i < availableNodes-1; i++)
{
if ( zavailable[i].start + zavailable[i].size ==
zavailable[i+1].start )
{
zavailable[i].size += zavailable[i+1].size;
zdelete(zavailable, i+1); availableNodes--;
return;
}
}
}
/* This is the simplest way possible. Should fix this. */
void * realloc(void * start, size_t newsize)
{
void * newstart = safe_malloc(newsize, __FILE__, __LINE__);
bcopy(start, newstart, newsize);
free(start);
return newstart;
}
branches/xZenu/src/modules/klibc/strchr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* strchr.c
*/
#include <string.h>
char *strchr(const char *s, int c)
{
while (*s != (char)c) {
if (!*s)
return NULL;
s++;
}
return (char *)s;
}
//__ALIAS(char *, index, (const char *, int), strchr)
branches/xZenu/src/modules/klibc/memcpy.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
/*
* memcpy.c
*/
#include <string.h>
#include <stdint.h>
void *memcpy(void *dst, const void *src, size_t n)
{
const char *p = src;
char *q = dst;
#if defined(__i386__)
size_t nl = n >> 2;
asm volatile ("cld ; rep ; movsl ; movl %3,%0 ; rep ; movsb":"+c" (nl),
"+S"(p), "+D"(q)
:"r"(n & 3));
#elif defined(__x86_64__)
size_t nq = n >> 3;
asm volatile ("cld ; rep ; movsq ; movl %3,%%ecx ; rep ; movsb":"+c"
(nq), "+S"(p), "+D"(q)
:"r"((uint32_t) (n & 7)));
#else
while (n--) {
*q++ = *p++;
}
#endif
return dst;
}
branches/xZenu/src/modules/klibc/strstr.c
1
2
3
4
5
6
7
8
9
10
11
/*
* strstr.c
*/
#include <string.h>
void *memmem(const void *haystack, size_t n, const void *needle, size_t m);
char *strstr(const char *haystack, const char *needle)
{
return (char *)memmem(haystack, strlen(haystack), needle,
strlen(needle));
}
branches/xZenu/src/modules/klibc/strncpy.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
/*
* strncpy.c
*/
#include <string.h>
char *strncpy(char *dst, const char *src, size_t n)
{
char *q = dst;
const char *p = src;
char ch;
while (n) {
n--;
*q++ = ch = *p++;
if (!ch)
break;
}
/* The specs say strncpy() fills the entire buffer with NUL. Sigh. */
memset(q, 0, n);
return dst;
}
branches/xZenu/src/modules/klibc/strncat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* strncat.c
*/
#include <string.h>
char *strncat(char *dst, const char *src, size_t n)
{
char *q = strchr(dst, '\0');
const char *p = src;
char ch;
while (n--) {
*q++ = ch = *p++;
if (!ch)
return dst;
}
*q = '\0';
return dst;
}
branches/xZenu/src/modules/klibc/memcmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* memcmp.c
*/
#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *c1 = s1, *c2 = s2;
int d = 0;
while (n--) {
d = (int)*c1++ - (int)*c2++;
if (d)
break;
}
return d;
}
branches/xZenu/src/modules/klibc/strncmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* strncmp.c
*/
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n)
{
const unsigned char *c1 = (const unsigned char *)s1;
const unsigned char *c2 = (const unsigned char *)s2;
unsigned char ch;
int d = 0;
while (n--) {
d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
break;
}
return d;
}
branches/xZenu/src/modules/klibc/memset.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
/*
* memset.c
*/
#include <string.h>
#include <stdint.h>
void *memset(void *dst, int c, size_t n)
{
char *q = dst;
#if defined(__i386__)
size_t nl = n >> 2;
asm volatile ("cld ; rep ; stosl ; movl %3,%0 ; rep ; stosb"
: "+c" (nl), "+D" (q)
: "a" ((unsigned char)c * 0x01010101U), "r" (n & 3));
#elif defined(__x86_64__)
size_t nq = n >> 3;
asm volatile ("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
:"+c" (nq), "+D" (q)
: "a" ((unsigned char)c * 0x0101010101010101U),
"r" ((uint32_t) n & 7));
#else
while (n--) {
*q++ = c;
}
#endif
return dst;
}
branches/xZenu/src/arch/i386/boot2/Makefile
131131
132132
133133
134
134
135135
136136
137137
@${MACHOCONV} $(SYMROOT)/boot2.sys $(SYMROOT)/boot &> /dev/null
@echo "\t******* Patching at $(PATCH_ADDR) ******"
@stat -f%z $(SYMROOT)/boot | perl -ane "print pack('V',@F[0]);" | dd conv=notrunc of=${SYMROOT}/boot.sys bs=1 count=4 seek=$(PATCH_ADDR) &> /dev/null
@stat -f %z $(SYMROOT)/boot | perl -ane "print pack('V',@F[0]);" | dd conv=notrunc of=${SYMROOT}/boot.sys bs=1 count=4 seek=$(PATCH_ADDR) &> /dev/null
@echo "\t[MACHOCONV] boot"
@${MACHOCONV} $(SYMROOT)/boot.sys $(SYMROOT)/boot
branches/xZenu/src/arch/i386/cdboot/Makefile
1818
1919
2020
21
21
2222
2323
2424
@dd if=$(SYMROOT)/boot of=$(SYMROOT)/cdboot conv=sync bs=2k seek=1 &> /dev/null
@# Update cdboot with boot file size info
@stat -f%z $(SYMROOT)/boot \
@stat -f %z $(SYMROOT)/boot \
| perl -ane "print pack('V',@F[0]);" \
| dd of=$(SYMROOT)/cdboot bs=1 count=4 seek=2044 conv=notrunc &> /dev/null
branches/xZenu/src/arch/i386/libsa/string.c
2525
2626
2727
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
4928
5029
51
52
53
54
55
56
57
58
59
60
61
6230
6331
6432
......
6634
6735
6836
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
8537
8638
8739
8840
8941
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
10642
10743
10844
10945
11046
111
112
113
114
115
116
117
11847
11948
120
121
122
123
124
125
126
127
128
129
13049
13150
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
20251
20352
20453
......
21059
21160
21261
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
62
27063
27164
27265
#include "libsa.h"
void * memset(void * dst, int val, size_t len)
{
asm volatile ( "rep; stosb"
: "=c" (len), "=D" (dst)
: "0" (len), "1" (dst), "a" (val)
: "memory" );
return dst;
}
#if 0
void * memcpy(void * dst, const void * src, size_t len)
{
asm volatile ( "rep; movsb"
: "=c" (len), "=D" (dst), "=S" (src)
: "0" (len), "1" (dst), "2" (src)
: "memory" );
return dst;
}
void bcopy(const void * src, void * dst, size_t len)
{
memcpy(dst, src, len);
}
void bzero(void * dst, size_t len)
{
memset(dst, 0, len);
}
#else
void * memcpy(void * dst, const void * src, size_t len)
{
asm volatile ( "cld \n\t"
"movl %%ecx, %%edx \n\t"
"shrl $2, %%ecx \n\t"
"movl %%edx, %%ecx \n\t"
"andl $3, %%ecx \n\t"
"rep; movsb \n\t"
: "=D" (dst)
: "c" (len), "D" (dst), "S" (src)
: "memory", "%edx" );
return dst;
}
void bcopy(const void * src, void * dst, size_t len)
{
asm volatile ( "cld \n\t"
"movl %%ecx, %%edx \n\t"
"shrl $2, %%ecx \n\t"
"rep; movsl \n\t"
"movl %%edx, %%ecx \n\t"
"andl $3, %%ecx \n\t"
"rep; movsb \n\t"
:
: "c" (len), "D" (dst), "S" (src)
: "memory", "%edx" );
}
void bzero(void * dst, size_t len)
{
asm volatile ( "xorl %%eax, %%eax \n\t"
"cld \n\t"
"movl %%ecx, %%edx \n\t"
"shrl $2, %%ecx \n\t"
"rep; stosl \n\t"
"movl %%edx, %%ecx \n\t"
"andl $3, %%ecx \n\t"
"rep; stosb \n\t"
:
: "c" (len), "D" (dst)
: "memory", "%eax" );
}
#endif
/* #if DONT_USE_GCC_BUILT_IN_STRLEN */
#define tolower(c) ((int)((c) & ~0x20))
#define toupper(c) ((int)((c) | 0x20))
size_t strlen(const char * s)
{
int n = 0;
while (*s++) n++;
return(n);
}
/*#endif*/
/* NOTE: Moved from ntfs.c */
int
memcmp(const void *p1, const void *p2, size_t len)
{
while (len--) {
if (*(const char*)(p1++) != *(const char*)(p2++))
return -1;
}
return 0;
}
int
strcmp(const char * s1, const char * s2)
{
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return (*s1 - *s2);
}
int strncmp(const char * s1, const char * s2, size_t len)
{
register int n = len;
while (--n >= 0 && *s1 == *s2++)
if (*s1++ == '\0')
return(0);
return(n<0 ? 0 : *s1 - *--s2);
}
char *
strcpy(char * s1, const char * s2)
{
register char *ret = s1;
while (*s1++ = *s2++)
continue;
return ret;
}
char *
strncpy(char * s1, const char * s2, size_t n)
{
register char *ret = s1;
while (n && (*s1++ = *s2++))
n--;
return ret;
}
size_t
strlcpy(char * s1, const char * s2, size_t n)
{
while (n && (*s1++ = *s2++))
n--;
if (!n) *--s1=0;
return strlen(s2);
}
char *
strstr(const char *in, const char *str)
{
char c;
size_t len;
c = *str++;
if (!c)
return (char *) in;// Trivial empty string case
len = strlen(str);
do {
char sc;
do {
sc = *in++;
if (!sc)
return (char *) 0;
} while (sc != c);
} while (strncmp(in, str, len) != 0);
return (char *) (in - 1);
}
int
ptol(const char *str)
{
register int c = *str;
else c = 0;
return c;
}
int
atoi(const char *str)
{
register int sum = 0;
while (*str == ' ' || *str == '\t')
str++;
while (*str >= '0' && *str <= '9') {
sum *= 10;
sum += *str++ - '0';
}
return sum;
}
char *strncat(char *s1, const char *s2, size_t n)
{
register char *ret = s1;
while (*s1)
s1++;
while (n-- && *s2)
*s1++ = *s2++;
*s1 = '\0';
return ret;
}
char *strcat(char *s1, const char *s2)
{
return(strncat(s1, s2, strlen(s2)));
}
char *strdup(const char *s1)
{
return strcpy(malloc(strlen(s1) + 1), s1);
}
#if STRNCASECMP
int strncasecmp(const char *s1, const char *s2, size_t len)
{
register int n = len;
while (--n >= 0 && tolower(*s1) == tolower(*s2++))
if (*s1++ == '\0')
return(0);
return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
}
#endif
char* strchr(const char *str, int c)
{
do
{
if(*str == c)
return (char*)str;
}
while(*(str++));
return 0;
}
char* strbreak(const char *str, char **next, long *len)
{
branches/xZenu/src/arch/i386/libsa/Makefile
2121
2222
2323
24
24
2525
2626
2727
INC = -I. -I$(SYMROOT) -I$(LIBSAIODIR) -I${SRCROOT}/include
OBJECTS = prf printf zalloc \
OBJECTS = prf printf \
string strtol error \
setjmp qsort efi_tables

Archive Download the corresponding diff file

Revision: 1292