linux函数:解读linux对string.h函数的实现

/*
file:.h
#ndef _LINUX_STRING_H_
# _LINUX_STRING_H_

/* We don't want s.h stuff being user by user stuff by accident */

#def __KERNEL__

# <linux/types.h> /* for size_t */
# <linux/stddef.h> /* for NULL */
# <linux/compiler.h> /* for inline ((always_inline)) */

#def __cplusplus
extern "C" {
#end

extern char * ___strtok;
extern char * strpbrk(const char *,const char *);
extern char * strtok(char *,const char *);
extern char * strsep(char **,const char *);
extern __kernel_size_t strspn(const char *,const char *);


/*
* Include machine specic inline routines
*/
# <asm/.h>

#ndef __HAVE_ARCH_STRCPY
extern char * strcpy(char *,const char *);
#end
#ndef __HAVE_ARCH_STRNCPY
extern char * strncpy(char *,const char *, __kernel_size_t);
#end
#ndef __HAVE_ARCH_STRCAT
extern char * strcat(char *, const char *);
#end
#ndef __HAVE_ARCH_STRNCAT
extern char * strncat(char *, const char *, __kernel_size_t);
#end
#ndef __HAVE_ARCH_STRCMP
extern strcmp(const char *,const char *);
#end
#ndef __HAVE_ARCH_STRNCMP
extern strncmp(const char *,const char *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_STRNICMP
extern strnicmp(const char *, const char *, __kernel_size_t);
#end
#ndef __HAVE_ARCH_STRCHR
extern char * strchr(const char *,);
#end
#ndef __HAVE_ARCH_STRRCHR
extern char * strrchr(const char *,);
#end
#ndef __HAVE_ARCH_STRSTR
extern char * strstr(const char *,const char *);
#end
#ndef __HAVE_ARCH_STRLEN
extern __kernel_size_t strlen(const char *);
#end
#ndef __HAVE_ARCH_STRNLEN
extern __kernel_size_t strnlen(const char *,__kernel_size_t);
#end

#ndef __HAVE_ARCH_MEMSET
extern void * mem(void *,,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMCPY
extern void * memcpy(void *,const void *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMMOVE
extern void * memmove(void *,const void *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMSCAN
extern void * memscan(void *,,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMCMP
extern memcmp(const void *,const void *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMCHR
extern void * memchr(const void *,,__kernel_size_t);
#end

#def __cplusplus
}
#end

#end
#end /* _LINUX_STRING_H_ */
*/
/*
file:.h
#ndef _LINUX_STRING_H_
# _LINUX_STRING_H_

/* We don't want s.h stuff being user by user stuff by accident */

#def __KERNEL__

# <linux/types.h> /* for size_t */
# <linux/stddef.h> /* for NULL */
# <linux/compiler.h> /* for inline ((always_inline)) */

#def __cplusplus
extern "C" {
#end

extern char * ___strtok;
extern char * strpbrk(const char *,const char *);
extern char * strtok(char *,const char *);
extern char * strsep(char **,const char *);
extern __kernel_size_t strspn(const char *,const char *);


/*
* Include machine specic inline routines
*/
# <asm/.h>

#ndef __HAVE_ARCH_STRCPY
extern char * strcpy(char *,const char *);
#end
#ndef __HAVE_ARCH_STRNCPY
extern char * strncpy(char *,const char *, __kernel_size_t);
#end
#ndef __HAVE_ARCH_STRCAT
extern char * strcat(char *, const char *);
#end
#ndef __HAVE_ARCH_STRNCAT
extern char * strncat(char *, const char *, __kernel_size_t);
#end
#ndef __HAVE_ARCH_STRCMP
extern strcmp(const char *,const char *);
#end
#ndef __HAVE_ARCH_STRNCMP
extern strncmp(const char *,const char *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_STRNICMP
extern strnicmp(const char *, const char *, __kernel_size_t);
#end
#ndef __HAVE_ARCH_STRCHR
extern char * strchr(const char *,);
#end
#ndef __HAVE_ARCH_STRRCHR
extern char * strrchr(const char *,);
#end
#ndef __HAVE_ARCH_STRSTR
extern char * strstr(const char *,const char *);
#end
#ndef __HAVE_ARCH_STRLEN
extern __kernel_size_t strlen(const char *);
#end
#ndef __HAVE_ARCH_STRNLEN
extern __kernel_size_t strnlen(const char *,__kernel_size_t);
#end

#ndef __HAVE_ARCH_MEMSET
extern void * mem(void *,,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMCPY
extern void * memcpy(void *,const void *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMMOVE
extern void * memmove(void *,const void *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMSCAN
extern void * memscan(void *,,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMCMP
extern memcmp(const void *,const void *,__kernel_size_t);
#end
#ndef __HAVE_ARCH_MEMCHR
extern void * memchr(const void *,,__kernel_size_t);
#end

#def __cplusplus
}
#end

#end
#end /* _LINUX_STRING_H_ */
*/

实现文件.c

C代码
/*
* linux/lib/.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/

/*
* stupid library routines.. The optimized versions should generally be found
* as inline code in <asm-xx/.h>
*
* These are buggy as well..
*
* * Fri Jun 25 1999, Ingo Oeser <[email protected]>
* - Added strsep which will replace strtok soon (because strsep is
* reentrant and should be faster). Use _disibledevent=> (len) {
do {
c1 = *s1; c2 = *s2;
s1; s2;
//是否已到末尾或两串是否有空串,如果到了末尾或有空串,则比较完毕
(!c1)
;
(!c2)
;
//如果没有,且串相等,则继续比较下个
(c1 c2)
continue;
//如果不相同,则同时转换为小写再进行比较
c1 = tolower(c1);
c2 = tolower(c2);
//如果不相同,则比较完毕,否则继续
(c1 != c2)
;
} while (--len);
}
()c1 - ()c2;
}
#end

char * ___strtok;

#ndef __HAVE_ARCH_STRCPY
/**
* strcpy - Copy a %NUL terminated
* @dest: Where to copy the to
* @src: Where to copy the from
*/
char * strcpy(char * dest,const char *src)
{
char *tmp = dest;

while ((*dest = *src) != '\0')
/* nothing */;
tmp;
}
#end

#ndef __HAVE_ARCH_STRNCPY
/**
* strncpy - Copy a length-limited, %NUL-terminated
* @dest: Where to copy the to
* @src: Where to copy the from
* @count: The maximum number of s to copy
*
* Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
* However, the result is not %NUL-terminated the source exceeds
* @count s.
*/
char * strncpy(char * dest,const char *src,size_t count)
{
char *tmp = dest;

while (count-- && (*dest = *src) != '\0')
/* nothing */;

tmp;
}
#end

#ndef __HAVE_ARCH_STRCAT
/**
* strcat - Append _disibledevent=>
while (*dest)
dest;
while ((*dest = *src) != '\0')
;

tmp;
}
#end

#ndef __HAVE_ARCH_STRNCAT
/**
* strncat - Append a length-limited, %NUL-terminated to another
* @dest: The to be appended to
* @src: The to append to it
* @count: The maximum numbers of s to copy
*
* Note that in contrast to strncpy, strncat ensures the result is
* terminated.
*/
char * strncat(char *dest, const char *src, size_t count)
{
char *tmp = dest;

(count) { //如果追加数不为0
while (*dest) //达到末尾
dest;
while ((*dest = *src)) { //如果没有到源串末尾
(--count 0) { //如果已经追加了count
*dest = '\0';//末尾置null
;//结束循环
}
}
}

tmp;
}
#end

#ndef __HAVE_ARCH_STRCMP
/**
* strcmp - Compare two s
* @cs: _disibledevent=>;
}

__res;
}
#end

#ndef __HAVE_ARCH_STRNCMP
/**
* strncmp - Compare two length-limited s
* @cs: _disibledevent=>
while (count) {
((__res = *cs - *ct) != 0 || !*cs)
;
count--;
}

__res;
}
#end

#ndef __HAVE_ARCH_STRCHR
/**
* strchr - Find the first occurrence of a character in a
* @s: The to be searched
* @c: The character to search for
*/
char * strchr(const char * s, c)
{
for(; *s != (char) c; s)
(*s '\0')
NULL;
(char *) s;
}
#end

#ndef __HAVE_ARCH_STRRCHR
/**
* strrchr - Find the last occurrence of a character in a
* @s: The to be searched
* @c: The character to search for
*/
char * strrchr(const char * s, c)
{
const char *p = s + strlen(s);
do {
(*p (char)c)
(char *)p;
} while (--p >= s);
NULL;
}
#end

#ndef __HAVE_ARCH_STRLEN
/**
* strlen - Find the length of a
* @s: The to be sized
*/
size_t strlen(const char * s)
{
const char *sc;

for (sc = s; *sc != '\0'; sc)
/* nothing */;
sc - s;
}
#end

#ndef __HAVE_ARCH_STRNLEN
/**
* strnlen - Find the length of a length-limited
* @s: The to be sized
* @count: The maximum number of s to search
*/
size_t strnlen(const char * s, size_t count)
{
const char *sc;

for (sc = s; count-- && *sc != '\0'; sc)
/* nothing */;
sc - s;
}
#end

#ndef __HAVE_ARCH_STRSPN
/**
* strspn - Calculate the length of the initial sub of @s which _disibledevent=>
for (p = s; *p != '\0'; p) {
for (a = accept; *a != '\0'; a) {
(*p *a)
;
}
(*a '\0')
count;
count;
}

count;
}
#end

#ndef __HAVE_ARCH_STRPBRK
/**
* strpbrk - Find the first occurrence of a of characters
* @cs: The to be searched
* @ct: The characters to search for
*/
char * strpbrk(const char * cs,const char * ct)
{
const char *sc1,*sc2;

for( sc1 = cs; *sc1 != '\0'; sc1) {
for( sc2 = ct; *sc2 != '\0'; sc2) {
(*sc1 *sc2)
(char *) sc1;
}
}
NULL;
}
#end

#ndef __HAVE_ARCH_STRTOK
/**
* strtok - Split a o tokens
* @s: The to be searched
* @ct: The characters to search for
*
* WARNING: strtok is deprecated, use strsep instead.
*/
char * strtok(char * s,const char * ct)
{
char *sbegin, *send;

sbegin = s ? s : ___strtok;
(!sbegin) {
NULL;
}
sbegin strspn(sbegin,ct);
(*sbegin '\0') {
___strtok = NULL;
( NULL );
}
send = strpbrk( sbegin, ct);
(send && *send != '\0')
*send = '\0';
___strtok = send;
(sbegin);
}
#end

#ndef __HAVE_ARCH_STRSEP
/**
* strsep - Split a o tokens
* @s: The to be searched
* @ct: The characters to search for
*
* strsep updates @s to po after the token, ready for the next call.
*
* It s empty tokens, too, behaving exactly like the libc function
* of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
* Same semantics, slimmer shape. ;)
*/
char * strsep(char **s, const char *ct)
{
char *sbegin = *s, *end;

(sbegin NULL)
NULL;

end = strpbrk(sbegin, ct);
(end)
*end = '\0';
*s = end;

sbegin;
}
#end

#ndef __HAVE_ARCH_MEMSET
/**
* mem - Fill a region of memory with the given value
* @s: Poer to the start of the area.
* @c: The to fill the area with
* @count: The size of the area.
*
* Do not use mem to access IO space, use mem_io instead.
*/
void * mem(void * s, c,size_t count)
{
char *xs = (char *) s;

while (count--)
*xs = c;

s;
}
#end

#ndef __HAVE_ARCH_BCOPY
/**
* bcopy - Copy _disibledevent=>char *dest = destp;

while (count--)
*dest = *src;
}
#end

#ndef __HAVE_ARCH_MEMCPY
/**
* memcpy - Copy _disibledevent=>
while (count--)
*tmp = *s;

dest;
}
#end

#ndef __HAVE_ARCH_MEMMOVE
/**
* memmove - Copy _disibledevent=>tmp = (char *) dest;
s = (char *) src;
while (count--)
*tmp = *s;
}
{
tmp = (char *) dest + count;
s = (char *) src + count;
while (count--)
*--tmp = *--s;
}

dest;
}
#end

#ndef __HAVE_ARCH_MEMCMP
/**
* memcmp - Compare two areas of memory
* @cs: _disibledevent=>
for( su1 = cs, su2 = ct; 0 < count; su1, su2, count--)
((res = *su1 - *su2) != 0)
;
res;
}
#end

#ndef __HAVE_ARCH_MEMSCAN
/**
* memscan - Find a character in an area of memory.
* @addr: The memory area
* @c: The to search for
* @size: The size of the area.
*
* s the address of the first occurrence of @c, or 1 past
* the area @c is not found
*/
void * memscan(void * addr, c, size_t size)
{
unsigned char * p = (unsigned char *) addr;

while (size) {
(*p c)
(void *) p;
p;
size--;
}
(void *) p;
}
#end

#ndef __HAVE_ARCH_STRSTR
/**
* strstr - Find the first sub in a %NUL terminated
* @s1: The to be searched
* @s2: The to search for
*/
char * strstr(const char * s1,const char * s2)
{
l1, l2;

l2 = strlen(s2);
(!l2)
(char *) s1;
l1 = strlen(s1);
while (l1 >= l2) {
l1--;
(!memcmp(s1,s2,l2))
(char *) s1;
s1;
}
NULL;
}
#end

#ndef __HAVE_ARCH_MEMCHR
/**
* memchr - Find a character in an area of memory.
* @s: The memory area
* @c: The to search for
* @n: The size of the area.
*
* s the address of the first occurrence of @c, or %NULL
* @c is not found
*/
void *memchr(const void *s, c, size_t n)
{
const unsigned char *p = s;
while (n-- != 0) {
((unsigned char)c *p) {
(void *)(p-1);
}
}
NULL;
}

#end
Tags:  linuxread函数 linuxsystem函数 linuxc函数 linux函数

延伸阅读

最新评论

发表评论