1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 1994-2009 Red Hat, Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * 3. Neither the name of the copyright holder nor the names of its 17 * contributors may be used to endorse or promote products derived from this 18 * software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 FUNCTION 35 <<strcpy>>---copy string 36 37 INDEX 38 strcpy 39 40 ANSI_SYNOPSIS 41 #include <string.h> 42 char *strcpy(char *<[dst]>, const char *<[src]>); 43 44 TRAD_SYNOPSIS 45 #include <string.h> 46 char *strcpy(<[dst]>, <[src]>) 47 char *<[dst]>; 48 char *<[src]>; 49 50 DESCRIPTION 51 <<strcpy>> copies the string pointed to by <[src]> 52 (including the terminating null character) to the array 53 pointed to by <[dst]>. 54 55 RETURNS 56 This function returns the initial value of <[dst]>. 57 58 PORTABILITY 59 <<strcpy>> is ANSI C. 60 61 <<strcpy>> requires no supporting OS subroutines. 62 63 QUICKREF 64 strcpy ansi pure 65 */ 66 67 #include "_ansi.h" 68 #include <string.h> 69 #include <limits.h> 70 71 /*SUPPRESS 560*/ 72 /*SUPPRESS 530*/ 73 74 /* Nonzero if either X or Y is not aligned on a "long" boundary. */ 75 #define UNALIGNED(X, Y) \ 76 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1))) 77 78 #if LONG_MAX == 2147483647L 79 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) 80 #else 81 #if LONG_MAX == 9223372036854775807L 82 /* Nonzero if X (a long int) contains a NULL byte. */ 83 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) 84 #else 85 #error long int is not a 32bit or 64bit type. 86 #endif 87 #endif 88 89 #ifndef DETECTNULL 90 #error long int is not a 32bit or 64bit byte 91 #endif 92 93 char* 94 _DEFUN (strcpy, (dst0, src0), 95 char *dst0 _AND 96 _CONST char *src0) 97 { 98 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) 99 char *s = dst0; 100 101 while (*dst0++ = *src0++) 102 ; 103 104 return s; 105 #else 106 char *dst = dst0; 107 _CONST char *src = src0; 108 long *aligned_dst; 109 _CONST long *aligned_src; 110 111 /* If SRC or DEST is unaligned, then copy bytes. */ 112 if (!UNALIGNED (src, dst)) 113 { 114 aligned_dst = (long*)dst; 115 aligned_src = (long*)src; 116 117 /* SRC and DEST are both "long int" aligned, try to do "long int" 118 sized copies. */ 119 while (!DETECTNULL(*aligned_src)) 120 { 121 *aligned_dst++ = *aligned_src++; 122 } 123 124 dst = (char*)aligned_dst; 125 src = (char*)aligned_src; 126 } 127 128 while ((*dst++ = *src++)) 129 ; 130 return dst0; 131 #endif /* not PREFER_SIZE_OVER_SPEED */ 132 } 133