Back to index

glibc  2.9
Functions
strncpy_chk.c File Reference
#include <string.h>
#include <memcopy.h>

Go to the source code of this file.

Functions

char * __strncpy_chk (char *s1, const char *s2, size_t n, size_t s1len)

Function Documentation

char* __strncpy_chk ( char *  s1,
const char *  s2,
size_t  n,
size_t  s1len 
)

Definition at line 24 of file strncpy_chk.c.

{
  reg_char c;
  char *s = s1;

  if (__builtin_expect (s1len < n, 0))
    __chk_fail ();

  --s1;

  if (n >= 4)
    {
      size_t n4 = n >> 2;

      for (;;)
       {
         c = *s2++;
         *++s1 = c;
         if (c == '\0')
           break;
         c = *s2++;
         *++s1 = c;
         if (c == '\0')
           break;
         c = *s2++;
         *++s1 = c;
         if (c == '\0')
           break;
         c = *s2++;
         *++s1 = c;
         if (c == '\0')
           break;
         if (--n4 == 0)
           goto last_chars;
       }
      n = n - (s1 - s) - 1;
      if (n == 0)
       return s;
      goto zero_fill;
    }

 last_chars:
  n &= 3;
  if (n == 0)
    return s;

  do
    {
      c = *s2++;
      *++s1 = c;
      if (--n == 0)
       return s;
    }
  while (c != '\0');

 zero_fill:
  do
    *++s1 = '\0';
  while (--n > 0);

  return s;
}

Here is the call graph for this function: