]> git.ipfire.org Git - thirdparty/glibc.git/blame - debug/strncpy_chk.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / debug / strncpy_chk.c
CommitLineData
b5cc329c
UD
1/* Copyright (C) 1991, 1997, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
b5cc329c
UD
17
18#include <string.h>
19#include <memcopy.h>
20
21
22char *
23__strncpy_chk (s1, s2, n, s1len)
24 char *s1;
25 const char *s2;
26 size_t n;
27 size_t s1len;
28{
29 reg_char c;
30 char *s = s1;
31
32 if (__builtin_expect (s1len < n, 0))
33 __chk_fail ();
34
35 --s1;
36
37 if (n >= 4)
38 {
39 size_t n4 = n >> 2;
40
41 for (;;)
42 {
43 c = *s2++;
44 *++s1 = c;
45 if (c == '\0')
46 break;
47 c = *s2++;
48 *++s1 = c;
49 if (c == '\0')
50 break;
51 c = *s2++;
52 *++s1 = c;
53 if (c == '\0')
54 break;
55 c = *s2++;
56 *++s1 = c;
57 if (c == '\0')
58 break;
59 if (--n4 == 0)
60 goto last_chars;
61 }
62 n = n - (s1 - s) - 1;
63 if (n == 0)
64 return s;
65 goto zero_fill;
66 }
67
68 last_chars:
69 n &= 3;
70 if (n == 0)
71 return s;
72
73 do
74 {
75 c = *s2++;
76 *++s1 = c;
77 if (--n == 0)
78 return s;
79 }
80 while (c != '\0');
81
82 zero_fill:
83 do
84 *++s1 = '\0';
85 while (--n > 0);
86
87 return s;
88}