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