1 /*
2 Copyright (c) 2007 Corinna Vinschen <corinna@vinschen.de>
3 */
4 /*
5 FUNCTION
6 <<stpncpy>>---counted copy string returning a pointer to its end
7
8 INDEX
9 stpncpy
10
11 SYNOPSIS
12 #include <string.h>
13 char *stpncpy(char *restrict <[dst]>, const char *restrict <[src]>,
14 size_t <[length]>);
15
16 DESCRIPTION
17 <<stpncpy>> copies not more than <[length]> characters from the
18 the string pointed to by <[src]> (including the terminating
19 null character) to the array pointed to by <[dst]>. If the
20 string pointed to by <[src]> is shorter than <[length]>
21 characters, null characters are appended to the destination
22 array until a total of <[length]> characters have been
23 written.
24
25 RETURNS
26 This function returns a pointer to the end of the destination string,
27 thus pointing to the trailing '\0', or, if the destination string is
28 not null-terminated, pointing to dst + n.
29
30 PORTABILITY
31 <<stpncpy>> is a GNU extension, candidate for inclusion into POSIX/SUSv4.
32
33 <<stpncpy>> requires no supporting OS subroutines.
34
35 QUICKREF
36 stpncpy gnu
37 */
38
39 #include <string.h>
40 #include <limits.h>
41 #include <stdint.h>
42
43 /*SUPPRESS 560*/
44 /*SUPPRESS 530*/
45
46 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
47 #define UNALIGNED(X, Y) \
48 (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
49
50 #if LONG_MAX == 2147483647L
51 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
52 #else
53 #if LONG_MAX == 9223372036854775807L
54 /* Nonzero if X (a long int) contains a NULL byte. */
55 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
56 #else
57 #error long int is not a 32bit or 64bit type.
58 #endif
59 #endif
60
61 #ifndef DETECTNULL
62 #error long int is not a 32bit or 64bit byte
63 #endif
64
65 #define TOO_SMALL(LEN) ((LEN) < sizeof (long))
66
67 char *
stpncpy(char * __restrict dst,const char * __restrict src,size_t count)68 stpncpy (char *__restrict dst,
69 const char *__restrict src,
70 size_t count)
71 {
72 char *ret = NULL;
73
74 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && \
75 !defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
76 long *aligned_dst;
77 const long *aligned_src;
78
79 /* If SRC and DEST is aligned and count large enough, then copy words. */
80 if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
81 {
82 aligned_dst = (long*)dst;
83 aligned_src = (long*)src;
84
85 /* SRC and DEST are both "long int" aligned, try to do "long int"
86 sized copies. */
87 while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
88 {
89 count -= sizeof (long int);
90 *aligned_dst++ = *aligned_src++;
91 }
92
93 dst = (char*)aligned_dst;
94 src = (char*)aligned_src;
95 }
96 #endif /* not PREFER_SIZE_OVER_SPEED */
97
98 while (count > 0)
99 {
100 --count;
101 if ((*dst++ = *src++) == '\0')
102 {
103 ret = dst - 1;
104 break;
105 }
106 }
107
108 while (count-- > 0)
109 *dst++ = '\0';
110
111 return ret ? ret : dst;
112 }
113