blob: 8d074071ac22c533625cfe19714d7b7d52bca7b5 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (C) 2006-2007 Axis Communications AB
3 *
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5 */
6
7#include <string.h>
8
9
10char *strncpy(char *dest, const char *src, size_t count)
11{
12 char *ret = dest;
13 unsigned long himagic = 0x80808080L;
14 unsigned long lomagic = 0x01010101L;
15
16 while (count && (unsigned long)src & (sizeof src - 1))
17 {
18 count--;
19 if (!(*dest++ = *src++))
20 {
21 goto finalize;
22 }
23 }
24
25 while (count >= sizeof (unsigned long))
26 {
27 unsigned long value = *(unsigned long*)src;
28 unsigned long magic;
29
30 if ((magic = (value - lomagic) & himagic))
31 {
32 if (magic & ~value)
33 {
34 break;
35 }
36 }
37
38 *(unsigned long*)dest = value;
39 dest += sizeof (unsigned long);
40 src += sizeof (unsigned long);
41 count -= sizeof (unsigned long);
42 }
43
44 while (count)
45 {
46 count--;
47 if (!(*dest++ = *src++))
48 break;
49 }
50
51finalize:
52 if (count)
53 {
54 memset(dest, '\0', count);
55 }
56
57 return ret;
58}
59libc_hidden_def(strncpy)