[ace-users] I write a function named nstrsncpy

Douglas C. Schmidt schmidt at dre.vanderbilt.edu
Sun Jun 24 15:02:57 CDT 2007


Hi Kevin,

   Why are you posting this message to the ACE mailing list?  It has
nothing to do with ACE.

Thanks,

        Doug

>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 
>
>


-- 
Dr. Douglas C. Schmidt                       Professor and Associate Chair
Electrical Engineering and Computer Science  TEL: (615) 343-8197
Vanderbilt University                        WEB: www.dre.vanderbilt.edu/~schmidt
Nashville, TN 37203                          NET: d.schmidt at vanderbilt.edu



More information about the Ace-users mailing list