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   long *aligned_dst;
76   const long *aligned_src;
77 
78   /* If SRC and DEST is aligned and count large enough, then copy words.  */
79   if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
80     {
81       aligned_dst = (long*)dst;
82       aligned_src = (long*)src;
83 
84       /* SRC and DEST are both "long int" aligned, try to do "long int"
85 	 sized copies.  */
86       while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
87 	{
88 	  count -= sizeof (long int);
89 	  *aligned_dst++ = *aligned_src++;
90 	}
91 
92       dst = (char*)aligned_dst;
93       src = (char*)aligned_src;
94     }
95 #endif /* not PREFER_SIZE_OVER_SPEED */
96 
97   while (count > 0)
98     {
99       --count;
100       if ((*dst++ = *src++) == '\0')
101 	{
102 	  ret = dst - 1;
103 	  break;
104 	}
105     }
106 
107   while (count-- > 0)
108     *dst++ = '\0';
109 
110   return ret ? ret : dst;
111 }
112