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