2006-06-15 16:02:20 +00:00
|
|
|
#include "types.h"
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
void*
|
2006-07-17 01:52:13 +00:00
|
|
|
memset(void *dst, int c, uint n)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
2019-05-31 13:45:59 +00:00
|
|
|
char *cdst = (char *) dst;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
cdst[i] = c;
|
|
|
|
}
|
2006-06-12 15:22:12 +00:00
|
|
|
return dst;
|
|
|
|
}
|
2006-06-21 01:53:07 +00:00
|
|
|
|
|
|
|
int
|
2006-07-17 01:52:13 +00:00
|
|
|
memcmp(const void *v1, const void *v2, uint n)
|
2006-06-21 01:53:07 +00:00
|
|
|
{
|
2007-08-10 17:17:42 +00:00
|
|
|
const uchar *s1, *s2;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-10 17:17:42 +00:00
|
|
|
s1 = v1;
|
|
|
|
s2 = v2;
|
2007-08-28 18:32:08 +00:00
|
|
|
while(n-- > 0){
|
2006-09-06 17:27:19 +00:00
|
|
|
if(*s1 != *s2)
|
2007-08-10 17:17:42 +00:00
|
|
|
return *s1 - *s2;
|
2006-06-21 01:53:07 +00:00
|
|
|
s1++, s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-22 01:28:57 +00:00
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
void*
|
2006-07-17 01:52:13 +00:00
|
|
|
memmove(void *dst, const void *src, uint n)
|
2006-06-22 01:28:57 +00:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
char *d;
|
2006-09-06 17:04:06 +00:00
|
|
|
|
2021-09-01 11:29:12 +00:00
|
|
|
if(n == 0)
|
|
|
|
return dst;
|
|
|
|
|
2006-06-22 01:28:57 +00:00
|
|
|
s = src;
|
|
|
|
d = dst;
|
2007-08-28 18:32:08 +00:00
|
|
|
if(s < d && s + n > d){
|
2006-06-22 01:28:57 +00:00
|
|
|
s += n;
|
|
|
|
d += n;
|
2006-09-06 17:27:19 +00:00
|
|
|
while(n-- > 0)
|
2006-06-22 01:28:57 +00:00
|
|
|
*--d = *--s;
|
2007-08-28 18:37:41 +00:00
|
|
|
} else
|
2006-09-06 17:27:19 +00:00
|
|
|
while(n-- > 0)
|
2006-06-22 01:28:57 +00:00
|
|
|
*d++ = *s++;
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
2006-07-05 20:00:14 +00:00
|
|
|
|
2009-10-07 17:06:55 +00:00
|
|
|
// memcpy exists to placate GCC. Use memmove.
|
2009-10-07 16:05:56 +00:00
|
|
|
void*
|
|
|
|
memcpy(void *dst, const void *src, uint n)
|
|
|
|
{
|
|
|
|
return memmove(dst, src, n);
|
|
|
|
}
|
|
|
|
|
2006-07-05 20:00:14 +00:00
|
|
|
int
|
2006-07-17 01:52:13 +00:00
|
|
|
strncmp(const char *p, const char *q, uint n)
|
2006-07-05 20:00:14 +00:00
|
|
|
{
|
2006-09-06 17:27:19 +00:00
|
|
|
while(n > 0 && *p && *p == *q)
|
2006-09-06 17:04:06 +00:00
|
|
|
n--, p++, q++;
|
2006-09-06 17:27:19 +00:00
|
|
|
if(n == 0)
|
2006-09-06 17:04:06 +00:00
|
|
|
return 0;
|
2007-08-24 21:00:02 +00:00
|
|
|
return (uchar)*p - (uchar)*q;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
strncpy(char *s, const char *t, int n)
|
|
|
|
{
|
|
|
|
char *os;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-24 21:00:02 +00:00
|
|
|
os = s;
|
|
|
|
while(n-- > 0 && (*s++ = *t++) != 0)
|
|
|
|
;
|
|
|
|
while(n-- > 0)
|
|
|
|
*s++ = 0;
|
|
|
|
return os;
|
2006-07-05 20:00:14 +00:00
|
|
|
}
|
2007-08-08 08:37:22 +00:00
|
|
|
|
|
|
|
// Like strncpy but guaranteed to NUL-terminate.
|
|
|
|
char*
|
|
|
|
safestrcpy(char *s, const char *t, int n)
|
|
|
|
{
|
|
|
|
char *os;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-08 08:37:22 +00:00
|
|
|
os = s;
|
|
|
|
if(n <= 0)
|
|
|
|
return os;
|
|
|
|
while(--n > 0 && (*s++ = *t++) != 0)
|
|
|
|
;
|
|
|
|
*s = 0;
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2007-08-21 19:22:08 +00:00
|
|
|
int
|
|
|
|
strlen(const char *s)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for(n = 0; s[n]; n++)
|
|
|
|
;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|