forked from KolibriOS/kolibrios
libc testsuite + fixes
git-svn-id: svn://kolibrios.org@6433 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
13
programs/develop/ktcc/trunk/libc/string/calloc.c
Normal file
13
programs/develop/ktcc/trunk/libc/string/calloc.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void* calloc (size_t num, size_t size)
|
||||
{
|
||||
size_t bytes = num * size;
|
||||
void *p = malloc(bytes);
|
||||
|
||||
if(p)
|
||||
memset(p, 0, bytes);
|
||||
|
||||
return p;
|
||||
}
|
@@ -1,8 +1,10 @@
|
||||
void* memchr(const void* buf,int c,int count)
|
||||
#include <string.h>
|
||||
|
||||
void* memchr(const void* buf,int c,size_t count)
|
||||
{
|
||||
int i;
|
||||
for (i=0;i<count;i++)
|
||||
if (*(char*)buf==c)
|
||||
if (*(char*)buf==(char)c)
|
||||
return (void*)buf;
|
||||
else
|
||||
buf++;
|
||||
|
@@ -1,12 +1,14 @@
|
||||
#include <string.h>
|
||||
|
||||
typedef unsigned char uc;
|
||||
int memcmp(const void* buf1,const void* buf2,int count)
|
||||
int memcmp(const void* buf1,const void* buf2,size_t count)
|
||||
{
|
||||
int i;
|
||||
for (i=0;i<count;i++)
|
||||
{
|
||||
if (*(uc*)buf1<*(uc*)buf2)
|
||||
return -1;
|
||||
if (*(uc*)buf1>*(uc*)buf2)
|
||||
if (*(uc*)buf1>*(uc*)buf2)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
@@ -7,29 +7,47 @@ public memcpy
|
||||
public memmove
|
||||
|
||||
proc memcpy c, to:dword,from:dword,count:dword
|
||||
|
||||
mov ecx,[count]
|
||||
push esi
|
||||
push edi
|
||||
mov ecx,[count]
|
||||
test ecx,ecx
|
||||
jz no_copy_block
|
||||
|
||||
mov esi,[from]
|
||||
mov esi,[from]
|
||||
mov edi,[to]
|
||||
cld
|
||||
rep movsb
|
||||
no_copy_block:
|
||||
no_copy_block:
|
||||
|
||||
pop edi
|
||||
pop esi
|
||||
mov eax, [to]
|
||||
ret
|
||||
endp
|
||||
|
||||
proc memmove c, to:dword,from:dword,count:dword
|
||||
|
||||
push esi
|
||||
push edi
|
||||
mov ecx,[count]
|
||||
test ecx,ecx
|
||||
jz no_copy_block_
|
||||
|
||||
mov esi,[from]
|
||||
mov edi,[to]
|
||||
cmp esi, edi
|
||||
je no_copy_block_
|
||||
jg copy_
|
||||
add esi, ecx
|
||||
add edi, ecx
|
||||
dec esi
|
||||
dec edi
|
||||
std
|
||||
copy_:
|
||||
rep movsb
|
||||
no_copy_block_:
|
||||
cld
|
||||
no_copy_block_:
|
||||
|
||||
pop edi
|
||||
pop esi
|
||||
mov eax,[to]
|
||||
ret
|
||||
endp
|
||||
endp
|
||||
|
@@ -10,6 +10,6 @@ memset:
|
||||
cld
|
||||
rep stosb
|
||||
.no_set:
|
||||
mov eax, [esp+8]
|
||||
pop edi
|
||||
ret
|
||||
|
@@ -1,8 +1,10 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strcat(char* strDest, const char* strSource)
|
||||
{
|
||||
char* res;
|
||||
res=strDest;
|
||||
while (*strDest) strDest++;
|
||||
while (*strDest++ = *strSource++) ;
|
||||
while ((*strDest++ = *strSource++)) ;
|
||||
return res;
|
||||
}
|
||||
|
@@ -1,10 +1,11 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strchr(const char* string, int c)
|
||||
{
|
||||
while (*string)
|
||||
{
|
||||
if (*string==c)
|
||||
do {
|
||||
if (*string == (char)c)
|
||||
return (char*)string;
|
||||
string++;
|
||||
}
|
||||
return (char*)0;
|
||||
} while (*string++);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
@@ -1,3 +1,5 @@
|
||||
#include <string.h>
|
||||
|
||||
int strcmp(const char* string1, const char* string2)
|
||||
{
|
||||
while (1)
|
||||
|
@@ -1,7 +1,9 @@
|
||||
char* strcpy(char* strDest,char* strSource)
|
||||
#include <string.h>
|
||||
|
||||
char* strcpy(char* strDest,const char* strSource)
|
||||
{
|
||||
char* res;
|
||||
res=strDest;
|
||||
while(*strDest++ = *strSource++) ;
|
||||
return res;
|
||||
}
|
||||
while((*strDest++ = *strSource++)) ;
|
||||
return res;
|
||||
}
|
||||
|
@@ -1,9 +1,11 @@
|
||||
int strcspn(const char* string, const char* strCharSet)
|
||||
#include <string.h>
|
||||
|
||||
size_t strcspn(const char* string, const char* strCharSet)
|
||||
{
|
||||
const char* temp;
|
||||
int i;
|
||||
i=0;
|
||||
while(1)
|
||||
while(*string)
|
||||
{
|
||||
temp=strCharSet;
|
||||
while (*temp!='\0')
|
||||
@@ -14,4 +16,5 @@ int strcspn(const char* string, const char* strCharSet)
|
||||
}
|
||||
i++;string++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@ char* strdup(const char* str)
|
||||
int len;
|
||||
len=strlen(str)+1;
|
||||
res=malloc(len);
|
||||
memcpy(res,str,len);
|
||||
if(res)
|
||||
memcpy(res,str,len);
|
||||
return res;
|
||||
}
|
||||
|
@@ -1,4 +1,59 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strerror(int err)
|
||||
{
|
||||
return (char*)0;
|
||||
char *msg;
|
||||
switch(err)
|
||||
{
|
||||
case 0:
|
||||
msg = "success";
|
||||
break;
|
||||
case -1:
|
||||
msg = "end of file";
|
||||
break;
|
||||
case -2:
|
||||
msg = "function is not supported for the given file system";
|
||||
break;
|
||||
case -3:
|
||||
msg = "unknown file system";
|
||||
break;
|
||||
case -5:
|
||||
msg = "file not found";
|
||||
break;
|
||||
case -6:
|
||||
msg = "end of file, EOF";
|
||||
break;
|
||||
case -7:
|
||||
msg = "pointer lies outside of application memory";
|
||||
break;
|
||||
case -8:
|
||||
msg = "disk is full";
|
||||
break;
|
||||
case -9:
|
||||
msg = "file system error";
|
||||
break;
|
||||
case -10:
|
||||
msg = "access denied";
|
||||
break;
|
||||
case -11:
|
||||
msg = "device error";
|
||||
break;
|
||||
case -12:
|
||||
msg = "file system requires more memory";
|
||||
break;
|
||||
case -30:
|
||||
msg = "not enough memory";
|
||||
break;
|
||||
case -31:
|
||||
msg = "file is not executable";
|
||||
break;
|
||||
case -32:
|
||||
msg = "too many processes";
|
||||
break;
|
||||
default:
|
||||
msg = "unknown error";
|
||||
break;
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
@@ -1,3 +1,5 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strpbrk(const char* string, const char* strCharSet)
|
||||
{
|
||||
const char* temp;
|
||||
|
@@ -1,13 +1,15 @@
|
||||
char* strncat(char* strDest,const char* strSource,int count)
|
||||
#include <string.h>
|
||||
|
||||
char* strncat(char* strDest,const char* strSource,size_t count)
|
||||
{
|
||||
char* res;
|
||||
res=strDest;
|
||||
while (*strDest++) ;
|
||||
while(count-->0)
|
||||
while (*strDest) strDest++;
|
||||
while(count-- > 0)
|
||||
{
|
||||
if(*strDest++ = *strSource++) continue;
|
||||
if((*strDest++ = *strSource++)) continue;
|
||||
return(res);
|
||||
}
|
||||
*strDest = 0;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,14 @@
|
||||
int strncmp(const char* string1, const char* string2, int count)
|
||||
#include <string.h>
|
||||
|
||||
int strncmp(const char* string1, const char* string2, size_t count)
|
||||
{
|
||||
while(count>0 && *string1==*string2)
|
||||
while(count>0 && (*string1==*string2))
|
||||
{
|
||||
if (*string1) return 0;
|
||||
if ('\0' == *string1) return 0;
|
||||
++string1;
|
||||
++string2;
|
||||
--count;
|
||||
}
|
||||
if(count) return (*string1 - *string2);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,6 @@
|
||||
char* strncpy(char* strDest,const char* strSource,int count)
|
||||
#include <string.h>
|
||||
|
||||
char* strncpy(char* strDest,const char* strSource,size_t count)
|
||||
{
|
||||
char* res;
|
||||
res=strDest;
|
||||
@@ -11,4 +13,4 @@ char* strncpy(char* strDest,const char* strSource,int count)
|
||||
count--;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@@ -1,3 +1,5 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strrchr(const char* s,int c)
|
||||
{
|
||||
char* res;
|
||||
|
@@ -1,12 +1,14 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strrev(char *p)
|
||||
{
|
||||
char *q = p, *res = p, z;
|
||||
while(q && *q) ++q; /* find eos */
|
||||
for(--q; p < q; ++p, --q)
|
||||
{
|
||||
for(--q; p < q; ++p, --q)
|
||||
{
|
||||
z = *p;
|
||||
*p = *q;
|
||||
*q = z;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,6 @@
|
||||
int strspn(const char* string,const char* strCharSet)
|
||||
#include <string.h>
|
||||
|
||||
size_t strspn(const char* string,const char* strCharSet)
|
||||
{
|
||||
int i;
|
||||
const char* temp;
|
||||
@@ -6,14 +8,15 @@ int strspn(const char* string,const char* strCharSet)
|
||||
while (*string!='\0')
|
||||
{
|
||||
temp=strCharSet;
|
||||
while (temp!='\0')
|
||||
while (*temp!='\0')
|
||||
{
|
||||
if (*temp==*string)
|
||||
break;
|
||||
}
|
||||
if (temp=='\0')
|
||||
temp++;
|
||||
}
|
||||
if (*temp=='\0')
|
||||
break;
|
||||
*string++;
|
||||
string++;
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#include<string.h>
|
||||
#include <string.h>
|
||||
|
||||
char* strstr(const char* s, const char* find)
|
||||
{
|
||||
|
@@ -1,14 +1,25 @@
|
||||
#include "string.h"
|
||||
#include <string.h>
|
||||
|
||||
char* strtok(char* s,const char* delim)
|
||||
// non reentrant
|
||||
{
|
||||
char* res;
|
||||
if (*s=='\0')
|
||||
return (char*)0;
|
||||
s+=strspn(s,delim);
|
||||
if (*s=='\0')
|
||||
return (char*)0;
|
||||
res=s;
|
||||
s+=strcspn(s,delim);
|
||||
*s=='\0';
|
||||
static char* savep;
|
||||
char* res;
|
||||
|
||||
if(s)
|
||||
savep = NULL;
|
||||
else
|
||||
s = savep;
|
||||
|
||||
if (*s == '\0')
|
||||
return NULL;
|
||||
s += strspn(s, delim);
|
||||
if (*s == '\0')
|
||||
return NULL;
|
||||
res = s;
|
||||
s += strcspn(s, delim);
|
||||
savep = s + 1;
|
||||
*s = '\0';
|
||||
return res;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user