strcpy函数

strcpy函数的源码:
#include <stddef.h>
#include <string.h>
#include <memcopy.h>
#include <bp-checks.h>

#undef strcpy

/* Copy SRC to DEST.  */
char *
strcpy (dest, src)
     char *dest;
     const char *src;
{
  reg_char c;
  char *__unbounded s = (char *__unbounded) CHECK_BOUNDS_LOW (src);
  const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1;
  size_t n;

  do
    {
      c = *s++;
      s[off] = c;
    }
  while (c != '\0');

  n = s - src;
  (void) CHECK_BOUNDS_HIGH (src + n);
  (void) CHECK_BOUNDS_HIGH (dest + n);

  return dest;
}
libc_hidden_builtin_def (strcpy)


需要理解的地方:
1、头文件<stddef.h>、<string.h>、<memcopy.h>和<bp-checks.h>为什么被引用?
2、reg_char、ptrdiff_t、size_t和char *__unbounded,以及CHECK_BOUNDS_LOW和CHECK_BOUNDS_HIGH 的含义?
3、const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1;
为什么要-1?
4、c = *s++;
   s[off] = c;
这2行的含义是什么?
5、n的类型是size_t,下面这样写的作用?
  n = s - src;
  (void) CHECK_BOUNDS_HIGH (src + n);
  (void) CHECK_BOUNDS_HIGH (dest + n);




你可能感兴趣的:(RCP)