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   char *dscan;
87   const char *sscan;
88 
89   dscan = dst0;
90   sscan = src0;
91   while (count > 0)
92     {
93       --count;
94       if ((*dscan++ = *sscan++) == '\0')
95 	break;
96     }
97   while (count-- > 0)
98     *dscan++ = '\0';
99 
100   return dst0;
101 #else
102   char *dst = dst0;
103   const char *src = src0;
104   long *aligned_dst;
105   const long *aligned_src;
106 
107   /* If SRC and DEST is aligned and count large enough, then copy words.  */
108   if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
109     {
110       aligned_dst = (long*)dst;
111       aligned_src = (long*)src;
112 
113       /* SRC and DEST are both "long int" aligned, try to do "long int"
114 	 sized copies.  */
115       while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
116 	{
117 	  count -= sizeof (long int);
118 	  *aligned_dst++ = *aligned_src++;
119 	}
120 
121       dst = (char*)aligned_dst;
122       src = (char*)aligned_src;
123     }
124 
125   while (count > 0)
126     {
127       --count;
128       if ((*dst++ = *src++) == '\0')
129 	break;
130     }
131 
132   while (count-- > 0)
133     *dst++ = '\0';
134 
135   return dst0;
136 #endif /* not PREFER_SIZE_OVER_SPEED */
137 }
138