linux内核字符串函数的实现
来源:互联网 发布:mac终端默认路径 编辑:程序博客网 时间:2024/06/10 05:50
公司笔试和面试中常要你编写strcpy/strcmp/memcpy/memcmp/strstr/atoi 等等这样的基础函数的实现。
我想在这儿总结下:(如下代码都是来自最新Linux kernel的库函数)
---------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
我想在这儿总结下:(如下代码都是来自最新Linux kernel的库函数)
---------------------------------------------------------------------------------------
- //strcpy in linux kernel linux-3.3/linux/lib/string.c
- char *strcpy(char *dest, const char *src) {
- char *tmp = dest; //很多时候可以在这儿对传入参数进行有效检查。内核里面没有这样做。
- while ((*dest++ = *src++) != '\0')
- /* nothing */;
- return tmp;
- }
- int strcmp(const char *cs, const char *ct)
- {
- unsigned char c1, c2;
-
- while (1) {
- c1 = *cs++;
- c2 = *ct++;
- if (c1 != c2)
- return c1 < c2 ? -1 : 1;
- if (!c1)
- break;
- }
- return 0;
- }
- void *memcpy(void *dest, const void *src, size_t count)
- {
- char *tmp = dest;
- const char *s = src;
-
- while (count--)
- *tmp++ = *s++;
- return dest;
- }
- int memcmp(const void *cs, const void *ct, size_t count) {
- const unsigned char *su1, *su2;
- int res = 0;
-
- for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
- if ((res = *su1 - *su2) != 0)
- break;
- return res;
- }
- char *strstr(const char *s1, const char *s2)
- {
- size_t l1, l2;
-
- l2 = strlen(s2);
- if (!l2)
- return (char *)s1;
- l1 = strlen(s1);
- while (l1 >= l2) {
- l1--;
- if (!memcmp(s1, s2, l2))
- return (char *)s1;
- s1++;
- }
- return NULL;
- }
0 0
- linux内核字符串函数的实现
- linux内核字符串函数的实现
- linux字符串函数的实现(内核源代码)
- linux内核字符串转换函数 -- linux内核
- Linux内核字符串转换函数
- Linux内核使用的字符串转整形函数kstrtouint
- Linux内核中提供的一些字符串转换函数
- Linux中常用字符串函数的实现
- linux c语言字符串函数的实现
- Linux内核中的字符串处理函数
- Linux内核中的字符串处理函数
- linux内核中字符串转换函数 simple_strtoul
- Linux内核中的字符串处理函数
- linux内核函数schedule()实现进程的调度
- linux内核函数schedule()实现进程的调度
- linux内核函数schedule()实现进程的调度
- Linux 内核模块实现udp发送字符串
- Linux 内核可用的字符串分割函数 strsep (String Split Function strsep in Linux Kernel)
- 80386ASM程序设计基础(十):段描述符,段选择子(zz)
- WebService环境搭建
- 的
- Struts文件上传
- 在firefox的搜索栏中 添加百度搜索栏
- linux内核字符串函数的实现
- Problem 2150 Fire Game
- [Spring]梦创俱乐部网站后台开发笔记
- 动态规划最长公共子序列求解以及内存优化
- android中的Fragment
- 关于对新装Linux的固态硬盘(SSD)做优化配置
- 新辰:一位老SEOer给新手的忠告 人脉才是重中之重!
- begin
- android基于xmpp的即时通讯应用