strcat,strcpy,strcmp,strstr,memset,memcpy,strsep在内核中的实现

时间:2022-09-04 03:46:11
以下代码来自linux-2.6.38
1、strcat 在lib/string.c中实现。
 1 /**
2 * strcat - Append one %NUL-terminated string to another
3 * @dest: The string to be appended to
4 * @src: The string to append to it
5 */
6
7 char *strcat(char *dest, const char *src)
8 {
9 char *tmp = dest;
10
11 while (*dest)
12 dest++;
13 while ((*dest++ = *src++) != '\0')
14 ;
15 return tmp;
16 }

2、strcpy
在lib/string.c中实现。
 1 /**
2 * strcpy - Copy a %NUL terminated string
3 * @dest: Where to copy the string to
4 * @src: Where to copy the string from
5 */
6 char *strcpy(char *dest, const char *src)
7 {
8 char *tmp = dest;
9
10 while ((*dest++ = *src++) != '\0')
11 /* nothing */;
12 return tmp;
13 }
3、strcmp
在lib/string.c中实现。
 1 /** 2  * strcmp - Compare two strings 3  * @cs: One string 4  * @ct: Another string 5  */ 6 int strcmp(const char *cs, const char *ct) 7 { 8     unsigned char c1, c2; 9 10     while (1) {11         c1 = *cs++;12         c2 = *ct++;13         if (c1 != c2)14             return c1 < c2 ? -1 : 1;15         if (!c1)16             break;17  }18     return 0;19 }
4、strstr
在lib/string.c中实现。
 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 }13 /**14  * memcmp - Compare two areas of memory15  * @cs: One area of memory16  * @ct: Another area of memory17  * @count: The size of the area.18  */19 int memcmp(const void *cs, const void *ct, size_t count)20 {21     const unsigned char *su1, *su2;22     int res = 0;23 24     for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)25         if ((res = *su1 - *su2) != 0)26             break;27     return res;28 }29 /**30  * strstr - Find the first substring in a %NUL terminated string31  * @s1: The string to be searched32  * @s2: The string to search for33  */34 char *strstr(const char *s1, const char *s2)35 {36  size_t l1, l2;37 38     l2 = strlen(s2);39     if (!l2)40         return (char *)s1;41     l1 = strlen(s1);42     while (l1 >= l2) {43         l1--;44         if (!memcmp(s1, s2, l2))45             return (char *)s1;46         s1++;47  }48     return NULL;49 }
5、strnstr
在lib/string.c中实现。
 1 /** 2  * strnstr - Find the first substring in a length-limited string 3  * @s1: The string to be searched 4  * @s2: The string to search for 5  * @len: the maximum number of characters to search 6  */ 7 char *strnstr(const char *s1, const char *s2, size_t len) 8 { 9  size_t l2;10 11     l2 = strlen(s2);12     if (!l2)13         return (char *)s1;14     while (len >= l2) {15         len--;16         if (!memcmp(s1, s2, l2))17             return (char *)s1;18         s1++;19  }20     return NULL;21 }
6、memset
在lib/string.c中实现。
 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 }
7、memcpy
在lib/string.c中实现。
 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 }
8、memmove
在lib/string.c中实现。关于memcpy和memmove的区别下面会进行分析。
 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 }
9、memchr
在lib/string.c中实现。
 1 /** 2  * memchr - Find a character in an area of memory. 3  * @s: The memory area 4  * @c: The byte to search for 5  * @n: The size of the area. 6  * 7  * returns the address of the first occurrence of @c, or %NULL 8  * if @c is not found 9  */10 void *memchr(const void *s, int c, size_t n)11 {12     const unsigned char *p = s;13     while (n-- != 0) {14             if ((unsigned char)c == *p++) {15             return (void *)(p - 1);16  }17  }18     return NULL;19 }

10、strsep
在lib/string.c中实现。
 1 /** 2  * strpbrk - Find the first occurrence of a set of characters 3  * @cs: The string to be searched 4  * @ct: The characters to search for 5  */ 6 char *strpbrk(const char *cs, const char *ct) 7 { 8     const char *sc1, *sc2; 9 10     for (sc1 = cs; *sc1 != '\0'; ++sc1) {11         for (sc2 = ct; *sc2 != '\0'; ++sc2) {12             if (*sc1 == *sc2)13                 return (char *)sc1;14  }15  }16     return NULL;17 }18 19 /**20  * strsep - Split a string into tokens21  * @s: The string to be searched22  * @ct: The characters to search for23  *24  * strsep() updates @s to point after the token, ready for the next call.25  *26  * It returns empty tokens, too, behaving exactly like the libc function27  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.28  * Same semantics, slimmer shape. ;)29  */30 char *strsep(char **s, const char *ct)31 {32     char *sbegin = *s;33     char *end;34 35     if (sbegin == NULL)36         return NULL;37 38     end = strpbrk(sbegin, ct);39     if (end)40         *end++ = '\0';41     *s = end;42     return sbegin;43 }


【转载】memmove和memcopy的区别

memcpy和memmove()都是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下:
void
*memcpy(void *dst, const void *src, size_t count);

void *memmove(void
*dst, const void *src, size_t count);


他们的作用是一样的,唯一的区别是,当内存发生局部重叠的时候,memmove保证拷贝的结果是正确的,memcpy不保证拷贝的结果的正确。
strcat,strcpy,strcmp,strstr,memset,memcpy,strsep在内核中的实现
第一种情况下,拷贝重叠的区域不会出现问题,内容均可以正确的被拷贝。
第二种情况下,问题出现在
右边的两个字节,这两个字节的原来的内容首先就被覆盖了而且没有保存。所以接下来拷贝的时候,拷贝的是已经被覆盖的内容,显然这是有问题的。
实际上,
memcpy只是memmove的一个子集