Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / libc / string / strncpy.c
blobbfb51a1390ab3a9a7eede1e461f5e70e3caeb568
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <string.h>
23 char *strncpy (char *s1, const char *s2, size_t n)
25 register char c;
26 char *s = s1;
28 -- s1;
30 if (n >= 4)
32 size_t n4 = n >> 2;
34 for (;;)
36 c = *s2 ++;
37 *++s1 = c;
39 if (c == '\0')
40 break;
42 c = *s2 ++;
44 *++s1 = c;
45 if (c == '\0')
46 break;
48 c = *s2 ++;
49 *++s1 = c;
51 if (c == '\0')
52 break;
54 c = *s2 ++;
55 *++s1 = c;
57 if (c == '\0')
58 break;
60 if (--n4 == 0)
61 goto last_chars;
64 n = n - (s1 - s) - 1;
66 if (n == 0)
67 return s;
69 goto zero_fill;
72 last_chars:
73 n &= 3;
74 if (n == 0)
75 return s;
79 c = *s2 ++;
80 *++s1 = c;
81 if (-- n == 0)
82 return s;
85 while (c != '\0');
87 zero_fill:
89 *++s1 = '\0';
90 while (-- n > 0);
92 return s;