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 <<strcpy>>---copy string
20
21 INDEX
22 strcpy
23
24 SYNOPSIS
25 #include <string.h>
26 char *strcpy(char *<[dst]>, const char *<[src]>);
27
28 DESCRIPTION
29 <<strcpy>> copies the string pointed to by <[src]>
30 (including the terminating null character) to the array
31 pointed to by <[dst]>.
32
33 RETURNS
34 This function returns the initial value of <[dst]>.
35
36 PORTABILITY
37 <<strcpy>> is ANSI C.
38
39 <<strcpy>> requires no supporting OS subroutines.
40
41 QUICKREF
42 strcpy ansi pure
43 */
44
45 #include <string.h>
46 #include <limits.h>
47 #include <stdint.h>
48
49 /*SUPPRESS 560*/
50 /*SUPPRESS 530*/
51
52 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
53 #define UNALIGNED(X, Y) \
54 (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
55
56 #if LONG_MAX == 2147483647L
57 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
58 #else
59 #if LONG_MAX == 9223372036854775807L
60 /* Nonzero if X (a long int) contains a NULL byte. */
61 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
62 #else
63 #error long int is not a 32bit or 64bit type.
64 #endif
65 #endif
66
67 #ifndef DETECTNULL
68 #error long int is not a 32bit or 64bit byte
69 #endif
70
71 #undef strcpy
72
73 char*
strcpy(char * dst0,const char * src0)74 strcpy (char *dst0,
75 const char *src0)
76 {
77 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
78 char *s = dst0;
79
80 while ((*dst0++ = *src0++))
81 ;
82
83 return s;
84 #else
85 char *dst = dst0;
86 const char *src = src0;
87 long *aligned_dst;
88 const long *aligned_src;
89
90 /* If SRC or DEST is unaligned, then copy bytes. */
91 if (!UNALIGNED (src, dst))
92 {
93 aligned_dst = (long*)dst;
94 aligned_src = (long*)src;
95
96 /* SRC and DEST are both "long int" aligned, try to do "long int"
97 sized copies. */
98 while (!DETECTNULL(*aligned_src))
99 {
100 *aligned_dst++ = *aligned_src++;
101 }
102
103 dst = (char*)aligned_dst;
104 src = (char*)aligned_src;
105 }
106
107 while ((*dst++ = *src++))
108 ;
109 return dst0;
110 #endif /* not PREFER_SIZE_OVER_SPEED */
111 }
112