xv6-cs450/string.c

76 lines
1 KiB
C
Raw Normal View History

2006-06-15 18:02:20 +02:00
#include "types.h"
#include "defs.h"
2006-09-06 19:27:19 +02:00
void*
memset(void *dst, int c, uint n)
2006-06-12 17:22:12 +02:00
{
2006-09-06 19:27:19 +02:00
char *d = (char*) dst;
2006-06-12 17:22:12 +02:00
while(n-- > 0)
*d++ = c;
return dst;
}
2006-06-21 03:53:07 +02:00
int
memcmp(const void *v1, const void *v2, uint n)
2006-06-21 03:53:07 +02:00
{
2006-09-06 19:27:19 +02:00
const uchar *s1 = (const uchar*) v1;
const uchar *s2 = (const uchar*) v2;
2006-06-21 03:53:07 +02:00
2006-09-06 19:27:19 +02:00
while(n-- > 0) {
if(*s1 != *s2)
2006-06-21 03:53:07 +02:00
return (int) *s1 - (int) *s2;
s1++, s2++;
}
return 0;
}
2006-09-06 19:27:19 +02:00
void*
memmove(void *dst, const void *src, uint n)
{
const char *s;
char *d;
s = src;
d = dst;
2006-09-06 19:27:19 +02:00
if(s < d && s + n > d) {
s += n;
d += n;
2006-09-06 19:27:19 +02:00
while(n-- > 0)
*--d = *--s;
} else
2006-09-06 19:27:19 +02:00
while(n-- > 0)
*d++ = *s++;
return dst;
}
int
strncmp(const char *p, const char *q, uint n)
{
2006-09-06 19:27:19 +02:00
while(n > 0 && *p && *p == *q)
n--, p++, q++;
2006-09-06 19:27:19 +02:00
if(n == 0)
return 0;
else
return (int) ((uchar) *p - (uchar) *q);
}
2007-08-08 10:37:22 +02:00
// Like strncpy but guaranteed to NUL-terminate.
char*
safestrcpy(char *s, const char *t, int n)
{
char *os;
os = s;
if(n <= 0)
return os;
while(--n > 0 && (*s++ = *t++) != 0)
;
*s = 0;
return os;
}