add safestrcpy

This commit is contained in:
rsc 2007-08-08 08:37:22 +00:00
parent cd08e6e065
commit 7e89fb90bd
2 changed files with 17 additions and 0 deletions

1
defs.h
View file

@ -39,6 +39,7 @@ void* memset(void*, int, uint);
int memcmp(const void*, const void*, uint); int memcmp(const void*, const void*, uint);
void* memmove(void*, const void*, uint); void* memmove(void*, const void*, uint);
int strncmp(const char*, const char*, uint); int strncmp(const char*, const char*, uint);
char* safestrcpy(char*, const char*, int);
// syscall.c // syscall.c
void syscall(void); void syscall(void);

View file

@ -57,3 +57,19 @@ strncmp(const char *p, const char *q, uint n)
else else
return (int) ((uchar) *p - (uchar) *q); return (int) ((uchar) *p - (uchar) *q);
} }
// 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;
}