]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/String/Copying.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / String / Copying.c
1 /** @file
2 Copying Functions for <string.h>.
3
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 //#include <sys/EfiCdefs.h>
14
15 #include <Uefi.h>
16 #include <Library/BaseLib.h>
17 #include <Library/BaseMemoryLib.h>
18
19 #include <LibConfig.h>
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 /** The memcpy function copies n characters from the object pointed to by s2
25 into the object pointed to by s1.
26
27 The implementation is reentrant and handles the case where s2 overlaps s1.
28
29 @return The memcpy function returns the value of s1.
30 **/
31 void *
32 memcpy(void * __restrict s1, const void * __restrict s2, size_t n)
33 {
34 return CopyMem( s1, s2, n);
35 }
36
37 /** The memmove function copies n characters from the object pointed to by s2
38 into the object pointed to by s1. Copying takes place as if the n
39 characters from the object pointed to by s2 are first copied into a
40 temporary array of n characters that does not overlap the objects pointed
41 to by s1 and s2, and then the n characters from the temporary array are
42 copied into the object pointed to by s1.
43
44 This is a version of memcpy that is guaranteed to work when s1 and s2
45 overlap. Since our implementation of memcpy already handles overlap,
46 memmove can be identical to memcpy.
47
48 @return The memmove function returns the value of s1.
49 **/
50 void *
51 memmove(void *s1, const void *s2, size_t n)
52 {
53 return CopyMem( s1, s2, n);
54 }
55
56 /** The strcpy function copies the string pointed to by s2 (including the
57 terminating null character) into the array pointed to by s1. If copying
58 takes place between objects that overlap, the behavior is undefined.
59
60 @return The strcpy function returns the value of s1.
61 **/
62 char *
63 strcpy(char * __restrict s1, const char * __restrict s2)
64 {
65 //char *s1ret = s1;
66
67 //while ( *s1++ = *s2++) /* Empty Body */;
68 //return(s1ret);
69 return AsciiStrCpy( s1, s2);
70 }
71
72 /** The strncpy function copies not more than n characters (characters that
73 follow a null character are not copied) from the array pointed to by s2 to
74 the array pointed to by s1. If copying takes place between objects that
75 overlap, the behavior is undefined.
76
77 If the array pointed to by s2 is a string that is shorter than n
78 characters, null characters are appended to the copy in the array pointed
79 to by s1, until n characters in all have been written.
80
81 @return The strncpy function returns the value of s1.
82 **/
83 char *strncpy(char * __restrict s1, const char * __restrict s2, size_t n)
84 {
85 return AsciiStrnCpy( s1, s2, n);
86 //char *dest = s1;
87
88 //while(n != 0) {
89 // --n;
90 // if((*dest++ = *s2++) == '\0') break;
91 //}
92 //while(n != 0) {
93 // *dest++ = '\0';
94 // --n;
95 //}
96 //return (s1);
97 }
98
99 /** The strncpyX function copies not more than n-1 characters (characters that
100 follow a null character are not copied) from the array pointed to by s2 to
101 the array pointed to by s1. Array s1 is guaranteed to be NULL terminated.
102 If copying takes place between objects that overlap,
103 the behavior is undefined.
104
105 strncpyX exists because normal strncpy does not indicate if the copy was
106 terminated because of exhausting the buffer or reaching the end of s2.
107
108 @return The strncpyX function returns 0 if the copy operation was
109 terminated because it reached the end of s1. Otherwise,
110 a non-zero value is returned indicating how many characters
111 remain in s1.
112 **/
113 int strncpyX(char * __restrict s1, const char * __restrict s2, size_t n)
114 {
115 int NumLeft;
116
117 for( ; n != 0; --n) {
118 if((*s1++ = *s2++) == '\0') break;
119 }
120 NumLeft = (int)n;
121
122 for( --s1; n != 0; --n) {
123 *s1++ = '\0';
124 }
125
126 return NumLeft; // Zero if we ran out of buffer ( strlen(s1) < strlen(s2) )
127 }
128
129 /** NetBSD Compatibility Function strdup creates a duplicate copy of a string. **/
130 char *
131 strdup(const char *str)
132 {
133 size_t len;
134 char *copy;
135
136 len = strlen(str) + 1;
137 if ((copy = malloc(len)) == NULL)
138 return (NULL);
139 memcpy(copy, str, len);
140 return (copy);
141 }