]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/String/Copying.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / StdLib / LibC / String / Copying.c
CommitLineData
2aa62f2b 1/** @file\r
2 Copying Functions for <string.h>.\r
3\r
53e1e5c6 4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
2aa62f2b 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
2aa62f2b 13#include <Uefi.h>\r
14#include <Library/BaseLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16\r
17#include <LibConfig.h>\r
18\r
19#include <stdlib.h>\r
20#include <string.h>\r
21\r
53e1e5c6 22/** Do not define memcpy for IPF+GCC builds.\r
23 For IPF, using a GCC compiler, the memcpy function is converted to\r
24 CopyMem by objcpy during build.\r
25**/\r
26#if !(defined(MDE_CPU_IPF) && defined(__GNUC__))\r
2aa62f2b 27/** The memcpy function copies n characters from the object pointed to by s2\r
28 into the object pointed to by s1.\r
29\r
30 The implementation is reentrant and handles the case where s2 overlaps s1.\r
31\r
32 @return The memcpy function returns the value of s1.\r
33**/\r
34void *\r
35memcpy(void * __restrict s1, const void * __restrict s2, size_t n)\r
36{\r
37 return CopyMem( s1, s2, n);\r
38}\r
53e1e5c6 39#endif /* !(defined(MDE_CPU_IPF) && defined(__GCC)) */\r
2aa62f2b 40\r
41/** The memmove function copies n characters from the object pointed to by s2\r
42 into the object pointed to by s1. Copying takes place as if the n\r
43 characters from the object pointed to by s2 are first copied into a\r
44 temporary array of n characters that does not overlap the objects pointed\r
45 to by s1 and s2, and then the n characters from the temporary array are\r
46 copied into the object pointed to by s1.\r
47\r
48 This is a version of memcpy that is guaranteed to work when s1 and s2\r
49 overlap. Since our implementation of memcpy already handles overlap,\r
50 memmove can be identical to memcpy.\r
51\r
52 @return The memmove function returns the value of s1.\r
53**/\r
54void *\r
55memmove(void *s1, const void *s2, size_t n)\r
56{\r
57 return CopyMem( s1, s2, n);\r
58}\r
59\r
60/** The strcpy function copies the string pointed to by s2 (including the\r
61 terminating null character) into the array pointed to by s1. If copying\r
62 takes place between objects that overlap, the behavior is undefined.\r
63\r
64 @return The strcpy function returns the value of s1.\r
65**/\r
66char *\r
67strcpy(char * __restrict s1, const char * __restrict s2)\r
68{\r
69 //char *s1ret = s1;\r
70\r
71 //while ( *s1++ = *s2++) /* Empty Body */;\r
72 //return(s1ret);\r
73 return AsciiStrCpy( s1, s2);\r
74}\r
75\r
76/** The strncpy function copies not more than n characters (characters that\r
77 follow a null character are not copied) from the array pointed to by s2 to\r
78 the array pointed to by s1. If copying takes place between objects that\r
79 overlap, the behavior is undefined.\r
80\r
81 If the array pointed to by s2 is a string that is shorter than n\r
82 characters, null characters are appended to the copy in the array pointed\r
83 to by s1, until n characters in all have been written.\r
84\r
85 @return The strncpy function returns the value of s1.\r
86**/\r
87char *strncpy(char * __restrict s1, const char * __restrict s2, size_t n)\r
88{\r
89 return AsciiStrnCpy( s1, s2, n);\r
90 //char *dest = s1;\r
91\r
92 //while(n != 0) {\r
93 // --n;\r
94 // if((*dest++ = *s2++) == '\0') break;\r
95 //}\r
96 //while(n != 0) {\r
97 // *dest++ = '\0';\r
98 // --n;\r
99 //}\r
100 //return (s1);\r
101}\r
102\r
103/** The strncpyX function copies not more than n-1 characters (characters that\r
104 follow a null character are not copied) from the array pointed to by s2 to\r
105 the array pointed to by s1. Array s1 is guaranteed to be NULL terminated.\r
106 If copying takes place between objects that overlap,\r
107 the behavior is undefined.\r
108\r
109 strncpyX exists because normal strncpy does not indicate if the copy was\r
110 terminated because of exhausting the buffer or reaching the end of s2.\r
111\r
112 @return The strncpyX function returns 0 if the copy operation was\r
113 terminated because it reached the end of s1. Otherwise,\r
114 a non-zero value is returned indicating how many characters\r
115 remain in s1.\r
116**/\r
117int strncpyX(char * __restrict s1, const char * __restrict s2, size_t n)\r
118{\r
119 int NumLeft;\r
120\r
121 for( ; n != 0; --n) {\r
122 if((*s1++ = *s2++) == '\0') break;\r
123 }\r
124 NumLeft = (int)n;\r
125\r
126 for( --s1; n != 0; --n) {\r
127 *s1++ = '\0';\r
128 }\r
129\r
130 return NumLeft; // Zero if we ran out of buffer ( strlen(s1) < strlen(s2) )\r
131}\r
132\r
133/** NetBSD Compatibility Function strdup creates a duplicate copy of a string. **/\r
134char *\r
135strdup(const char *str)\r
136{\r
137 size_t len;\r
138 char *copy;\r
139\r
140 len = strlen(str) + 1;\r
141 if ((copy = malloc(len)) == NULL)\r
142 return (NULL);\r
143 memcpy(copy, str, len);\r
144 return (copy);\r
145}\r