1 | /* |
---|
2 | FUNCTION |
---|
3 | <<stpcpy>>---copy string returning a pointer to its end |
---|
4 | |
---|
5 | INDEX |
---|
6 | stpcpy |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <string.h> |
---|
10 | char *stpcpy(char *restrict <[dst]>, const char *restrict <[src]>); |
---|
11 | |
---|
12 | DESCRIPTION |
---|
13 | <<stpcpy>> 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 a pointer to the end of the destination string, |
---|
19 | thus pointing to the trailing '\0'. |
---|
20 | |
---|
21 | PORTABILITY |
---|
22 | <<stpcpy>> is a GNU extension, candidate for inclusion into POSIX/SUSv4. |
---|
23 | |
---|
24 | <<stpcpy>> requires no supporting OS subroutines. |
---|
25 | |
---|
26 | QUICKREF |
---|
27 | stpcpy gnu |
---|
28 | */ |
---|
29 | |
---|
30 | #include <string.h> |
---|
31 | #include <limits.h> |
---|
32 | |
---|
33 | /*SUPPRESS 560*/ |
---|
34 | /*SUPPRESS 530*/ |
---|
35 | |
---|
36 | /* Nonzero if either X or Y is not aligned on a "long" boundary. */ |
---|
37 | #define UNALIGNED(X, Y) \ |
---|
38 | (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1))) |
---|
39 | |
---|
40 | #if LONG_MAX == 2147483647L |
---|
41 | #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) |
---|
42 | #else |
---|
43 | #if LONG_MAX == 9223372036854775807L |
---|
44 | /* Nonzero if X (a long int) contains a NULL byte. */ |
---|
45 | #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) |
---|
46 | #else |
---|
47 | #error long int is not a 32bit or 64bit type. |
---|
48 | #endif |
---|
49 | #endif |
---|
50 | |
---|
51 | #ifndef DETECTNULL |
---|
52 | #error long int is not a 32bit or 64bit byte |
---|
53 | #endif |
---|
54 | |
---|
55 | char* |
---|
56 | stpcpy (char *__restrict dst, |
---|
57 | const char *__restrict src) |
---|
58 | { |
---|
59 | #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) |
---|
60 | long *aligned_dst; |
---|
61 | const long *aligned_src; |
---|
62 | |
---|
63 | /* If SRC or DEST is unaligned, then copy bytes. */ |
---|
64 | if (!UNALIGNED (src, dst)) |
---|
65 | { |
---|
66 | aligned_dst = (long*)dst; |
---|
67 | aligned_src = (long*)src; |
---|
68 | |
---|
69 | /* SRC and DEST are both "long int" aligned, try to do "long int" |
---|
70 | sized copies. */ |
---|
71 | while (!DETECTNULL(*aligned_src)) |
---|
72 | { |
---|
73 | *aligned_dst++ = *aligned_src++; |
---|
74 | } |
---|
75 | |
---|
76 | dst = (char*)aligned_dst; |
---|
77 | src = (char*)aligned_src; |
---|
78 | } |
---|
79 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
---|
80 | |
---|
81 | while ((*dst++ = *src++)) |
---|
82 | ; |
---|
83 | return --dst; |
---|
84 | } |
---|