source: trunk/libs/newlib/src/newlib/libc/string/strcpy.c @ 559

Last change on this file since 559 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.9 KB
Line 
1/*
2FUNCTION
3        <<strcpy>>---copy string
4
5INDEX
6        strcpy
7
8SYNOPSIS
9        #include <string.h>
10        char *strcpy(char *<[dst]>, const char *<[src]>);
11
12DESCRIPTION
13        <<strcpy>> copies the string pointed to by <[src]>
14        (including the terminating null character) to the array
15        pointed to by <[dst]>.
16
17RETURNS
18        This function returns the initial value of <[dst]>.
19
20PORTABILITY
21<<strcpy>> is ANSI C.
22
23<<strcpy>> requires no supporting OS subroutines.
24
25QUICKREF
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
54char*
55strcpy (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}
Note: See TracBrowser for help on using the repository browser.