[ace-users] I write a function named nstrsncpy
=?GB2312?B?wO65+se/?=
ligq at channelsoft.com
Sat Jun 23 02:19:49 CDT 2007
hi, all:
I wrote a function,
unsigned int nstrsncpy(char *dst, const char *src, unsigned int maxlen)
{
unsigned int len = 0;
if (dst==0 || src==0)
return len;
--maxlen;
while (*src && len<maxlen)
{
*dst = *src;
dst++;
src++;
len++;
}
*dst = '\0';
return len;
}
It copy char array 'src' to char array 'dst'. If the length of src is >= maxlen, It just copy maxlen-1 bytes. It will add '\0' to dst at the end, so it's safe for the caller.
At last, it returns copied bytes.
I use this function to replace following:
int len = strlen(src);
if(len >= maxlen-1)
len = maxlen-1;
memcpy(dst, src, len);
dst[len] = '\0';
====>
int len = nstrsncpy(dst, src, sizeof(dst));
Anyone can give me an advise?
thanks a lot.
Kevin Lee
More information about the Ace-users
mailing list