.
[glibc.git] / sysdeps / generic / strncpy.c
blob460e0baca496a349977b6c7365b81a6260a96107
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <string.h>
21 #include <memcopy.h>
23 char *
24 DEFUN(strncpy, (s1, s2, n), char *s1 AND CONST char *s2 AND size_t n)
26 reg_char c;
27 char *s = s1;
29 --s1;
31 if (n >= 4)
33 size_t n4 = n >> 2;
35 for (;;)
37 c = *s2++;
38 *++s1 = c;
39 if (c == '\0')
40 break;
41 c = *s2++;
42 *++s1 = c;
43 if (c == '\0')
44 break;
45 c = *s2++;
46 *++s1 = c;
47 if (c == '\0')
48 break;
49 c = *s2++;
50 *++s1 = c;
51 if (c == '\0')
52 break;
53 if (--n4 == 0)
54 goto last_chars;
56 n = n - (s1 - s) - 1;
57 if (n == 0)
58 return s;
59 goto zero_fill;
62 last_chars:
63 n &= 3;
64 if (n == 0)
65 return s;
69 c = *s2++;
70 *++s1 = c;
71 if (--n == 0)
72 return s;
74 while (c != '\0');
76 zero_fill:
78 *++s1 = '\0';
79 while (--n > 0);
81 return s;