View Single Post
Old 2012-05-30, 20:22   #11
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

1004610 Posts
Default

Quote:
Originally Posted by Dubslow View Post
But you don't know for sure without further code if some part of src got clobbered.
src wouldn't get clobbered; dest simply wouldn't have the full string.

Code:
int strcpy_s(
   char *dest,
   int n,
   const char *src
)
{
   if (!dest || !src) {
      return 1;   // Stupid human...
   }

   if (strlen(src) > n-1) {
      return 2;   // dest too small;  Stupid human...
   }

   strncpy(dest, src, n);

   return 0;
}
chalsall is offline   Reply With Quote