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