1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc.  Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 	<<strncpy>>---counted copy string
20 
21 INDEX
22 	strncpy
23 
24 SYNOPSIS
25 	#include <string.h>
26 	char *strncpy(char *restrict <[dst]>, const char *restrict <[src]>,
27                       size_t <[length]>);
28 
29 DESCRIPTION
30 	<<strncpy>> copies not more than <[length]> characters from
31 	the string pointed to by <[src]> (including the terminating
32 	null character) to the array pointed to by <[dst]>.  If the
33 	string pointed to by <[src]> is shorter than <[length]>
34 	characters, null characters are appended to the destination
35 	array until a total of <[length]> characters have been
36 	written.
37 
38 RETURNS
39 	This function returns the initial value of <[dst]>.
40 
41 PORTABILITY
42 <<strncpy>> is ANSI C.
43 
44 <<strncpy>> requires no supporting OS subroutines.
45 
46 QUICKREF
47 	strncpy ansi pure
48 */
49 
50 #include <string.h>
51 #include <limits.h>
52 #include <stdint.h>
53 
54 /*SUPPRESS 560*/
55 /*SUPPRESS 530*/
56 
57 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
58 #define UNALIGNED(X, Y) \
59   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
60 
61 #if LONG_MAX == 2147483647L
62 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
63 #else
64 #if LONG_MAX == 9223372036854775807L
65 /* Nonzero if X (a long int) contains a NULL byte. */
66 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
67 #else
68 #error long int is not a 32bit or 64bit type.
69 #endif
70 #endif
71 
72 #ifndef DETECTNULL
73 #error long int is not a 32bit or 64bit byte
74 #endif
75 
76 #define TOO_SMALL(LEN) ((LEN) < sizeof (long))
77 
78 #undef strncpy
79 
80 char *
strncpy(char * __restrict dst0,const char * __restrict src0,size_t count)81 strncpy (char *__restrict dst0,
82 	const char *__restrict src0,
83 	size_t count)
84 {
85 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) || \
86     defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
87   char *dscan;
88   const char *sscan;
89 
90   dscan = dst0;
91   sscan = src0;
92   while (count > 0)
93     {
94       --count;
95       if ((*dscan++ = *sscan++) == '\0')
96 	break;
97     }
98   while (count-- > 0)
99     *dscan++ = '\0';
100 
101   return dst0;
102 #else
103   char *dst = dst0;
104   const char *src = src0;
105   long *aligned_dst;
106   const long *aligned_src;
107 
108   /* If SRC and DEST is aligned and count large enough, then copy words.  */
109   if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
110     {
111       aligned_dst = (long*)dst;
112       aligned_src = (long*)src;
113 
114       /* SRC and DEST are both "long int" aligned, try to do "long int"
115 	 sized copies.  */
116       while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
117 	{
118 	  count -= sizeof (long int);
119 	  *aligned_dst++ = *aligned_src++;
120 	}
121 
122       dst = (char*)aligned_dst;
123       src = (char*)aligned_src;
124     }
125 
126   while (count > 0)
127     {
128       --count;
129       if ((*dst++ = *src++) == '\0')
130 	break;
131     }
132 
133   while (count-- > 0)
134     *dst++ = '\0';
135 
136   return dst0;
137 #endif /* not PREFER_SIZE_OVER_SPEED */
138 }
139