Chameleon

Chameleon Commit Details

Date:2015-02-27 00:23:50 (4 years 4 months ago)
Author:ErmaC
Commit:2586
Parents: 2585
Message:Implement strlcat (credits to cparm)
Changes:
M/branches/ErmaC/Enoch/i386/libsa/string.c
M/branches/ErmaC/Enoch/i386/libsa/libsa.h

File differences

branches/ErmaC/Enoch/i386/libsa/libsa.h
22
33
44
5
5
66
77
88
......
1010
1111
1212
13
13
1414
1515
1616
......
1818
1919
2020
21
21
2222
2323
2424
......
8989
9090
9191
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
109110
110111
111112
112113
113114
114
115
115
116
116117
117118
118119
119120
120121
121122
122
123
123
124
124125
125126
126127
127128
128
129
130
129
130
131
131132
132133
133134
134135
135
136
136137
137138
138139
139140
140
141
142
141
142
143
143144
144145
145146
146147
147148
148
149
150
151
149
150
151
152
152153
153154
154155
155156
156
157
157158
158159
159160
* 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
* 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,
* 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@
*/
#error bzero is defined.
#endif
extern void * memset(void * dst, int c, size_t n);
extern void * memcpy(void * dst, const void * src, size_t len);
extern int memcmp(const void * p1, const void * p2, size_t len);
extern int strcmp(const char * s1, const char * s2);
extern int strncmp(const char * s1, const char * s2, size_t n);
extern char * strcpy(char * s1, const char * s2);
extern char * stpcpy(char * s1, const char * s2);
extern char * strncpy(char * s1, const char * s2, size_t n);
extern char * strpcpy(char * s1, const char * s2, size_t n);
extern size_t strlcpy(char * s1, const char * s2, size_t n);
extern char * strstr(const char *in, const char *str);
extern int atoi(const char * str);
extern int ptol(const char * str);
extern size_t strlen(const char * str);
extern char * strcat(char * s1, const char * s2);
extern char * strncat(char * s1, const char * s2, size_t n);
extern char * strdup(const char *s1);
extern void*memset(void * dst, int c, size_t n);
extern void*memcpy(void * dst, const void * src, size_t len);
extern intmemcmp(const void * p1, const void * p2, size_t len);
extern intstrcmp(const char * s1, const char * s2);
extern intstrncmp(const char * s1, const char * s2, size_t n);
extern char*strcpy(char * s1, const char * s2);
extern char*stpcpy(char * s1, const char * s2);
extern char*strncpy(char * s1, const char * s2, size_t n);
extern char*strpcpy(char * s1, const char * s2, size_t n);
extern size_tstrlcpy(char * s1, const char * s2, size_t n);
extern char*strstr(const char *in, const char *str);
extern intatoi(const char * str);
extern intptol(const char * str);
extern size_tstrlen(const char * str);
extern size_tstrlcat(char *, const char *, size_t);
extern char*strcat(char * s1, const char * s2);
extern char*strncat(char * s1, const char * s2, size_t n);
extern char*strdup(const char *s1);
#if STRNCASECMP
extern int strncasecmp(const char *s1, const char *s2, size_t n);
#endif
extern char * strchr(const char *str, int c);
extern char * strbreak(const char *str, char **next, long *len);
extern char *strchr(const char *str, int c);
extern char *strbreak(const char *str, char **next, long *len);
extern uint8_t checksum8( void * start, unsigned int length );
/*
* error.c
*/
extern int errno;
extern char * strerror(int errnum);
extern interrno;
extern char*strerror(int errnum);
/*
* strtol.c
*/
extern long strtol(const char * nptr, char ** endptr, int base);
extern unsigned long strtoul(const char * nptr, char ** endptr, int base);
extern unsigned long long strtouq(const char *nptr, char ** endptr, int base);
extern long strtol(const char *nptr, char **endptr, int base);
extern unsigned long strtoul(const char *nptr, char **endptr, int base);
extern unsigned long long strtouq(const char *nptr, char **endptr, int base);
/*
* prf.c
*/
extern void prf(const char * fmt, va_list ap, int (*putfn_p)(), void * putfn_arg);
extern void prf(const char *fmt, va_list ap, int (*putfn_p)(), void *putfn_arg);
/*
* printf.c
*/
extern int sprintf(char *s, const char * format, ...);
extern int snprintf(char *s, size_t size, const char * format, ...);
extern int slvprintf(char * buffer, int len, const char * fmt, va_list arg);
extern int sprintf(char *s, const char *format, ...);
extern int snprintf(char *s, size_t size, const char *format, ...);
extern int slvprintf(char *buffer, int len, const char *fmt, va_list arg);
/*
* zalloc.c
*/
#define malloc(size) safe_malloc(size, __FILE__, __LINE__)
extern void malloc_init(char * start, int size, int nodes, void (*malloc_error)(char *, size_t, const char *, int));
extern void * safe_malloc(size_t size,const char *file, int line);
extern void free(void * start);
extern void * realloc(void * ptr, size_t size);
extern voidmalloc_init(char *start, int size, int nodes, void (*malloc_error)(char *, size_t, const char *, int));
extern void*safe_malloc(size_t size,const char *file, int line);
extern voidfree(void *start);
extern void*realloc(void *ptr, size_t size);
/*
* getsegbyname.c
*/
extern struct segment_command * getsegbynamefromheader(struct mach_header * mhp, char * segname);
extern struct segment_command *getsegbynamefromheader(struct mach_header *mhp, char *segname);
/*
* interrupts.c
branches/ErmaC/Enoch/i386/libsa/string.c
2525
2626
2727
28
28
2929
30
31
32
33
30
31
32
33
3434
35
35
3636
3737
3838
3939
4040
41
42
43
44
41
42
43
44
4545
46
46
4747
4848
4949
......
5353
5454
5555
56
56
5757
5858
5959
6060
61
61
6262
6363
6464
6565
6666
67
68
69
70
71
72
73
74
75
76
67
68
69
70
71
72
73
74
75
76
7777
78
78
7979
8080
8181
8282
83
84
85
86
87
88
89
90
91
92
83
84
85
86
87
88
89
90
91
92
9393
9494
9595
9696
97
98
99
100
101
102
103
104
105
106
107
97
98
99
100
101
102
103
104
105
106
107
108108
109109
110110
111111
112
113
114
115
116
117
118
119
120
121
122
112
113
114
115
116
117
118
119
120
121
122
123123
124124
125125
......
178178
179179
180180
181
182
181
183182
184183
185184
......
187186
188187
189188
190
191
189
192190
193191
194192
......
197195
198196
199197
200
201
198
202199
203200
204201
......
210207
211208
212209
213
214
210
215211
216212
217213
......
222218
223219
224220
225
226
221
227222
228223
229224
......
246241
247242
248243
249
250
244
251245
252246
253247
......
259253
260254
261255
262
263
256
264257
265258
266259
......
271264
272265
273266
267
268
269
270
271
272
273
274
275
276
277
278
279
274280
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
275309
276310
277311
......
308342
309343
310344
311
345
312346
313347
314348
......
318352
319353
320354
321
322
323
355
356
357
324358
325359
326360
......
358392
359393
360394
361
395
362396
363397
364398
365399
366400
367
401
402
368403
369404
370405
#include "libsa.h"
void * memset(void * dst, int val, size_t len)
void *memset(void * dst, int val, size_t len)
{
asm volatile ( "rep; stosb"
: "=c" (len), "=D" (dst)
: "0" (len), "1" (dst), "a" (val)
: "memory" );
asm volatile ( "rep; stosb"
: "=c" (len), "=D" (dst)
: "0" (len), "1" (dst), "a" (val)
: "memory" );
return dst;
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" );
asm volatile ( "rep; movsb"
: "=c" (len), "=D" (dst), "=S" (src)
: "0" (len), "1" (dst), "2" (src)
: "memory" );
return dst;
return dst;
}
void bcopy(const void * src, void * dst, size_t len)
void bzero(void * dst, size_t len)
{
memset(dst, 0, len);
memset(dst, 0, len);
}
void __bzero(void * dst, size_t len)
{
memset(dst, 0, 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"
"rep; movsl \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" );
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"
: "=D" (dst)
: "c" (len), "D" (dst), "S" (src)
: "memory", "%edx" );
return dst;
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" );
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" );
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" );
}
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" );
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
return 0;
}
char *
strcpy(char * s1, const char * s2)
char *strcpy(char *s1, const char *s2)
{
register char *ret = s1;
while ((*s1++ = *s2++))
return ret;
}
char *
stpcpy(char * s1, const char * s2)
char *stpcpy(char *s1, const char *s2)
{
while ((*s1++ = *s2++))
{
return --s1;
}
char *
strncpy(char * s1, const char * s2, size_t n)
char *strncpy(char *s1, const char *s2, size_t n)
{
register char *ret = s1;
while (n && (*s1++ = *s2++))
return ret;
}
char *
stpncpy(char * s1, const char * s2, size_t n)
char *stpncpy(char *s1, const char *s2, size_t n)
{
while (n && (*s1++ = *s2++))
--n;
return s1;
}
char *
strstr(const char *in, const char *str)
char *strstr(const char *in, const char *str)
{
char c;
size_t len;
return (char *) (in - 1);
}
int
ptol(const char *str)
int ptol(const char *str)
{
register int c = *str;
return c;
}
int
atoi(const char *str)
int atoi(const char *str)
{
register int sum = 0;
while (*str == ' ' || *str == '\t')
}
return sum;
}
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t strlcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
{
d++;
}
dlen = d - dst;
n = siz - dlen;
if (n == 0)
{
return(dlen + strlen(s));
}
while (*s != '\0')
{
if (n != 1)
{
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
char *strncat(char *s1, const char *s2, size_t n)
{
register char *ret = s1;
}
#endif
char* strchr(const char *str, int c)
char *strchr(const char *str, int c)
{
do
{
while(*(str++));
return 0;
}
char* strbreak(const char *str, char **next, long *len)
}
char *strbreak(const char *str, char **next, long *len)
{
char *start = (char*)str, *end;
bool quoted = false;
}
/* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */
uint8_t checksum8( void * start, unsigned int length )
uint8_t checksum8( void *start, unsigned int length )
{
uint8_t csum = 0;
uint8_t * cp = (uint8_t *) start;
unsigned int i;
for ( i = 0; i < length; i++) {
for ( i = 0; i < length; i++)
{
csum += *cp++;
}
return csum;

Archive Download the corresponding diff file

Revision: 2586