1 #ifndef __HAVE_ARCH_STRCPY 2 /** 3 * strcpy - Copy a %NUL terminated string 4 * @dest: Where to copy the string to 5 * @src: Where to copy the string from 6 */ 7 char *strcpy(char *dest, const char *src) 8 { 9 char *tmp = dest; 10 11 while ((*dest++ = *src++) != '\0') 12 /* nothing */; 13 return tmp; 14 }
1 /** 2 * memcpy - Copy one area of memory to another 3 * @dest: Where to copy to 4 * @src: Where to copy from 5 * @count: The size of the area. 6 * 7 * You should not use this function to access IO space, use memcpy_toio() 8 * or memcpy_fromio() instead. 9 */ 10 void *memcpy(void *dest, const void *src, size_t count) 11 { 12 char *tmp = dest; 13 const char *s = src; 14 15 while (count--) 16 *tmp++ = *s++; 17 return dest; 18 }
strcpy和memcpy主要有如下3方面的區別。
一、複製的內容不一樣。strcpy只能複製字符串,而memcpy能夠複製任意內容,例如字符數組、整型、結構體、類等。
二、複製的方法不一樣。strcpy不須要指定長度,它遇到被複制字符的串結束符"\0"才結束,因此容易溢出。memcpy則是根據其第3個參數決定複製的長度。
三、用途不一樣。一般在複製字符串時用strcpy,而須要複製其餘類型數據時則通常用memcpyc++
1 #ifndef __HAVE_ARCH_STRCMP 2 /** 3 * strcmp - Compare two strings 4 * @cs: One string 5 * @ct: Another string 6 */ 7 8 int strcmp(const char *cs, const char *ct) 9 { 10 unsigned char c1, c2; 11 12 while (1) { 13 c1 = *cs++; 14 c2 = *ct++; 15 if (c1 != c2) 16 return c1 < c2 ? -1 : 1; 17 if (!c1) 18 break; 19 } 20 return 0; 21 }
1 /** 2 * strncmp - Compare two length-limited strings 3 * @cs: One string 4 * @ct: Another string 5 * @count: The maximum number of bytes to compare 6 */ 7 int strncmp(const char *cs, const char *ct, size_t count) 8 { 9 unsigned char c1, c2; 10 11 while (count) { 12 c1 = *cs++; 13 c2 = *ct++; 14 if (c1 != c2) 15 return c1 < c2 ? -1 : 1; 16 if (!c1) 17 break; 18 count--; 19 } 20 return 0; 21 }
1 /** 2 * strlen - Find the length of a string 3 * @s: The string to be sized 4 */ 5 size_t strlen(const char *s) 6 { 7 const char *sc; 8 9 for (sc = s; *sc != '\0'; ++sc) 10 /* nothing */; 11 return sc - s; 12 }
1 /** 2 * memset - Fill a region of memory with the given value 3 * @s: Pointer to the start of the area. 4 * @c: The byte to fill the area with 5 * @count: The size of the area. 6 * 7 * Do not use memset() to access IO space, use memset_io() instead. 8 */ 9 void *memset(void *s, int c, size_t count) 10 { 11 char *xs = s; 12 13 while (count--) 14 *xs++ = c; 15 return s; 16 }
1 /** 2 * memmove - Copy one area of memory to another 3 * @dest: Where to copy to 4 * @src: Where to copy from 5 * @count: The size of the area. 6 * 7 * Unlike memcpy(), memmove() copes with overlapping areas. 8 */ 9 void *memmove(void *dest, const void *src, size_t count) 10 { 11 char *tmp; 12 const char *s; 13 14 if (dest <= src) { 15 tmp = dest; 16 s = src; 17 while (count--) 18 *tmp++ = *s++; 19 } else { 20 tmp = dest; 21 tmp += count; 22 s = src; 23 s += count; 24 while (count--) 25 *--tmp = *--s; 26 } 27 return dest; 28 }
1 /** 2 * memcmp - Compare two areas of memory 3 * @cs: One area of memory 4 * @ct: Another area of memory 5 * @count: The size of the area. 6 */ 7 int memcmp(const void *cs, const void *ct, size_t count) 8 { 9 const unsigned char *su1, *su2; 10 int res = 0; 11 12 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) 13 if ((res = *su1 - *su2) != 0) 14 break; 15 return res; 16 }