From 7e89fb90bd0beb91e5d67af362d6a8a3a1b4c626 Mon Sep 17 00:00:00 2001 From: rsc Date: Wed, 8 Aug 2007 08:37:22 +0000 Subject: add safestrcpy --- string.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'string.c') diff --git a/string.c b/string.c index 540a7d4..dc73266 100644 --- a/string.c +++ b/string.c @@ -57,3 +57,19 @@ strncmp(const char *p, const char *q, uint n) else 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; +} + -- cgit v1.2.3