How to Implement the string. H function in Linux

Source: Internet
Author: User
Tags strtok

/*
File: String. h
# Ifndef _ linux_string_h _
# DEFINE _ linux_string_h _

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

# Ifdef _ KERNEL __

# Include <Linux/types. h>/* For size_t */
# Include <Linux/stddef. h>/* For null */
# Include <Linux/compiler. h>/* for inline (always_inline ))*/

# Ifdef _ cplusplus
Extern "C "{
# Endif

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 specific inline routines
*/
# Include <ASM/string. h>

# Ifndef _ have_arch_strcpy
Extern char * strcpy (char *, const char *);
# Endif
# Ifndef _ have_arch_strncpy
Extern char * strncpy (char *, const char *, _ kernel_size_t );
# Endif
# Ifndef _ have_arch_strcat
Extern char * strcat (char *, const char *);
# Endif
# Ifndef _ have_arch_strncat
Extern char * strncat (char *, const char *, _ kernel_size_t );
# Endif
# Ifndef _ have_arch_strcmp
Extern int strcmp (const char *, const char *);
# Endif
# Ifndef _ have_arch_strncmp
Extern int strncmp (const char *, const char * ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_strnicmp
Extern int strnicmp (const char *, const char *, _ kernel_size_t );
# Endif
# Ifndef _ have_arch_strchr
Extern char * strchr (const char *, INT );
# Endif
# Ifndef _ have_arch_strrchr
Extern char * strrchr (const char *, INT );
# Endif
# Ifndef _ have_arch_strstr
Extern char * strstr (const char *, const char *);
# Endif
# Ifndef _ have_arch_strlen
Extern _ kernel_size_t strlen (const char *);
# Endif
# Ifndef _ have_arch_strnlen
Extern _ kernel_size_t strnlen (const char * ,__ kernel_size_t );
# Endif

# Ifndef _ have_arch_memset
Extern void * memset (void *, int ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memcpy
Extern void * memcpy (void *, const void * ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memmove
Extern void * memmove (void *, const void * ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memscan
Extern void * memscan (void *, int ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memcmp
Extern int memcmp (const void *, const void * ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memchr
Extern void * memchr (const void *, int ,__ kernel_size_t );
# Endif

# Ifdef _ cplusplus
}
# Endif

# Endif
# Endif/* _ linux_string_h _*/
*/
/*
File: String. h
# Ifndef _ linux_string_h _
# DEFINE _ linux_string_h _

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

# Ifdef _ KERNEL __

# Include <Linux/types. h>/* For size_t */
# Include <Linux/stddef. h>/* For null */
# Include <Linux/compiler. h>/* for inline (always_inline ))*/

# Ifdef _ cplusplus
Extern "C "{
# Endif

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 specific inline routines
*/
# Include <ASM/string. h>

# Ifndef _ have_arch_strcpy
Extern char * strcpy (char *, const char *);
# Endif
# Ifndef _ have_arch_strncpy
Extern char * strncpy (char *, const char *, _ kernel_size_t );
# Endif
# Ifndef _ have_arch_strcat
Extern char * strcat (char *, const char *);
# Endif
# Ifndef _ have_arch_strncat
Extern char * strncat (char *, const char *, _ kernel_size_t );
# Endif
# Ifndef _ have_arch_strcmp
Extern int strcmp (const char *, const char *);
# Endif
# Ifndef _ have_arch_strncmp
Extern int strncmp (const char *, const char * ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_strnicmp
Extern int strnicmp (const char *, const char *, _ kernel_size_t );
# Endif
# Ifndef _ have_arch_strchr
Extern char * strchr (const char *, INT );
# Endif
# Ifndef _ have_arch_strrchr
Extern char * strrchr (const char *, INT );
# Endif
# Ifndef _ have_arch_strstr
Extern char * strstr (const char *, const char *);
# Endif
# Ifndef _ have_arch_strlen
Extern _ kernel_size_t strlen (const char *);
# Endif
# Ifndef _ have_arch_strnlen
Extern _ kernel_size_t strnlen (const char * ,__ kernel_size_t );
# Endif

# Ifndef _ have_arch_memset
Extern void * memset (void *, int ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memcpy
Extern void * memcpy (void *, const void * ,__ kernel_size_t );

 

# Endif
# Ifndef _ have_arch_memmove
Extern void * memmove (void *, const void * ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memscan
Extern void * memscan (void *, int ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memcmp
Extern int memcmp (const void *, const void * ,__ kernel_size_t );
# Endif
# Ifndef _ have_arch_memchr
Extern void * memchr (const void *, int ,__ kernel_size_t );
# Endif

# Ifdef _ cplusplus
}
# Endif

# Endif
# Endif/* _ linux_string_h _*/
*/

Implementation file string. c

C code
/*
* Linux/lib/string. c
*
* Copyright (c) 1991,199 2 Linus Torvalds
*/

/*
* Stupid Library Routines .. the optimized versions shoshould generally be found
* As Inline code in <ASM-XX/string. h>
*
* These are buggy as well ..
*
** Fri Jun 25 1999, Ingo oeser <ioe@informatik.tu-chemnitz.de>
*-Added strsep () which will replace strtok () soon (because strsep () is
* Reentrant and shocould be faster). Use Only strsep () in new code, please.
*/

# Include <Linux/types. h>
# Include <Linux/string. h>
# Include <Linux/ctype. h>

# Ifndef _ have_arch_strnicmp
/**
* Strnicmp-case insensitive (Case Insensitive), length-limited string comparison
* @ S1: one string
* @ S2: The other string
* @ Len: the maximum number of characters to compare
*/
Int strnicmp (const char * S1, const char * S2, size_t Len)
{
/* Yes, Virginia, it had better be unsigned */
Unsigned char C1, C2;

C1 = 0; C2 = 0;
If (LEN ){
Do {
C1 = * S1; C2 = * S2;
S1 ++; S2 ++;
// Whether the string is at the end or whether the two strings are empty. If the string is at the end or is empty, the comparison is complete.
If (! C1)
Break;
If (! C2)
Break;
// If NO, and the string is equal, continue to compare the next character
If (C1 = c2)
Continue;
// If they are different, convert them to lower-case characters for comparison.
C1 = tolower (C1 );
C2 = tolower (C2 );
// If they are not the same, the comparison is complete. Otherwise, continue.
If (C1! = C2)
Break;
} While (-- Len );
}
Return (INT) C1-(INT) C2;
}
# Endif

Char * ___ strtok;

# Ifndef _ have_arch_strcpy
/**
* Strcpy-copy a % NUL terminated string
* @ DEST: Where to copy the string
* @ SRC: Where to copy the string from
*/
Char * strcpy (char * DEST, const char * SRC)
{
Char * TMP = DEST;

While (* DEST ++ = * SRC ++ )! = '/0 ')
/* Nothing */;
Return TMP;
}
# Endif

# Ifndef _ have_arch_strncpy
/**
* Strncpy-copy a length-limited, % NUL-terminated string
* @ DEST: Where to copy the string
* @ SRC: Where to copy the string from
* @ Count: the maximum number of bytes to copy
*
* Note that unlike userspace strncpy, this does not % NUL-pad the buffer.
* However, the result is not % NUL-terminated if the source exceeds
* @ Count bytes.
*/
Char * strncpy (char * DEST, const char * SRC, size_t count)
{
Char * TMP = DEST;

While (count -- & (* DEST ++ = * SRC ++ )! = '/0 ')
/* Nothing */;

Return TMP;
}
# Endif

# Ifndef _ have_arch_strcat
/**
* Strcat-append one % NUL-terminated string to another
* @ DEST: the string to be appended
* @ SRC: the string to append to it
*/
Char * strcat (char * DEST, const char * SRC)
{
Char * TMP = DEST;

While (* DEST)
DeST ++;
While (* DEST ++ = * SRC ++ )! = '/0 ')
;

Return TMP;
}
# Endif

# Ifndef _ have_arch_strncat
/**
* Strncat-append a length-limited, % NUL-terminated string to another
* @ DEST: the string to be appended
* @ SRC: the string to append to it
* @ Count: the maximum numbers of bytes 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;

If (count) {// if the number of appended characters is not 0
While (* DEST) // reaches the end of the string
DeST ++;
While (* DEST ++ = * SRC ++) {// if it does not reach the end of the source string
If (-- count = 0) {// If the Count character has been appended
* DEST = '/0'; // The end is null.
Break; // end Loop
}
}
}

Return TMP;
}
# Endif

# Ifndef _ have_arch_strcmp
/**
* Strcmp-compare two strings
* @ Cs: one string
* @ CT: Another string
*/
Int strcmp (const char * cs, const char * CT)
{
Register signed Char _ res;

While (1 ){
If (_ res = * cs-* CT ++ )! = 0 |! * Cs ++)
Break;
}

Return _ res;
}
# Endif

# Ifndef _ have_arch_strncmp
/**
* Strncmp-compare two length-limited strings
* @ Cs: one string
* @ CT: Another string
* @ Count: the maximum number of bytes to compare

 

*/
Int strncmp (const char * cs, const char * CT, size_t count)
{
Register signed Char _ res = 0;

While (count ){
If (_ res = * cs-* CT ++ )! = 0 |! * Cs ++)
Break;
Count --;
}

Return _ res;
}
# Endif

# Ifndef _ have_arch_strchr
/**
* Strchr-find the first occurrence of a character in a string
* @ S: the string to be searched
* @ C: the character to search
*/
Char * strchr (const char * s, int C)
{
For (; * s! = (Char) C; ++ S)
If (* s = '/0 ')
Return NULL;
Return (char *) S;
}
# Endif

# Ifndef _ have_arch_strrchr
/**
* Strrchr-find the last occurrence of a character in a string
* @ S: the string to be searched
* @ C: the character to search
*/
Char * strrchr (const char * s, int C)
{
Const char * P = S + strlen (s );
Do {
If (* P = (char) C)
Return (char *) P;
} While (-- p> = s );
Return NULL;
}
# Endif

# Ifndef _ have_arch_strlen
/**
* Strlen-find the length of a string
* @ S: the string to be sized
*/
Size_t strlen (const char * s)
{
Const char * SC;

For (SC = s; * SC! = '/0'; ++ SC)
/* Nothing */;
Return SC-S;
}
# Endif

# Ifndef _ have_arch_strnlen
/**
* Strnlen-find the length of a length-limited string
* @ S: the string to be sized
* @ Count: the maximum number of bytes to search
*/
Size_t strnlen (const char * s, size_t count)
{
Const char * SC;

For (SC = s; count -- & * SC! = '/0'; ++ SC)
/* Nothing */;
Return SC-S;
}
# Endif

# Ifndef _ have_arch_strspns
/**
* Strsps-calculate the length of the initial substring of @ s which only
* Contain letters in @ accept
* @ S: the string to be searched
* @ Accept: the string to search
*/
Size_t strspn (const char * s, const char * accept)
{
Const char * P;
Const char *;
Size_t COUNT = 0;

For (P = s; * P! = '/0'; ++ p ){
For (A = accept; *! = '/0'; ++ ){
If (* P = *)
Break;
}
If (* A = '/0 ')
Return count;
++ Count;
}

Return count;
}
# Endif

# Ifndef _ have_arch_strpbrk
/**
* Strpbrk-find the first occurrence of a set of characters
* @ Cs: the string to be searched
* @ CT: The characters to search
*/
Char * strpbrk (const char * cs, const char * CT)
{
Const char * SC1, * SC2;

For (SC1 = cs; * SC1! = '/0'; ++ SC1 ){
For (SC2 = CT; * SC2! = '/0'; ++ SC2 ){
If (* SC1 = * SC2)
Return (char *) SC1;
}
}
Return NULL;
}
# Endif

# Ifndef _ have_arch_strtok
/**
* Strtok-split a string into tokens
* @ S: the string to be searched
* @ CT: The characters to search
*
* Warning: strtok is deprecated, use strsep instead.
*/
Char * strtok (char * s, const char * CT)
{
Char * sbegin, * Send;

Sbegin = s? S: ___ strtok;
If (! Sbegin ){
Return NULL;
}
Sbegin + = strspns (sbegin, CT );
If (* sbegin = '/0 '){
___ Strtok = NULL;
Return (null );
}
Send = strpbrk (sbegin, CT );
If (send & * send! = '/0 ')
* Send ++ = '/0 ';
___ Strtok = Send;
Return (sbegin );
}
# Endif

# Ifndef _ have_arch_strsep
/**
* Strsep-split a string into tokens
* @ S: the string to be searched
* @ CT: The characters to search
*
* Strsep () Updates @ s to point after the token, ready for the next call.
*
* It returns 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;

If (sbegin = NULL)
Return NULL;

End = strpbrk (sbegin, CT );
If (end)
* End ++ = '/0 ';
* S = end;

Return sbegin;
}
# Endif

# Ifndef _ have_arch_memset
/**
* Memset-fill a region of memory with the given value
* @ S: pointer to the start of the area.
* @ C: the byte to fill the area
* @ Count: the size of the area.
*
* Do not use memset () to access Io space, use memset_io () instead.
*/
Void * memset (void * s, int C, size_t count)
{
Char * xs = (char *) S;

While (count --)
* Xs ++ = C;

Return S;
}
# Endif

# Ifndef _ have_arch_bcopy
/**
* Bcopy-copy one area of memory to another
* @ SRC: Where to copy from
* @ DEST: Where to copy
* @ Count: the size of the area.
*
* Note that this is the same as memcpy (), with the arguments reversed.

* Memcpy () is the standard, bcopy () is a legacy BSD function.
*
* You shoshould not use this function to access Io space, use memcpy_toio ()
* Or memcpy_fromio () instead.
*/
Void bcopy (const void * SRCP, void * destp, size_t count)
{
Const char * src = SRCP;
Char * DEST = destp;

While (count --)
* DEST ++ = * SRC ++;
}
# Endif

# Ifndef _ have_arch_memcpy
/**
* Memcpy-copy one area of memory to another
* @ DEST: Where to copy
* @ SRC: Where to copy from
* @ Count: the size of the area.
*
* You shoshould not use this function to access Io space, use memcpy_toio ()
* Or memcpy_fromio () instead.
*/
Void * memcpy (void * DEST, const void * SRC, size_t count)
{
Char * TMP = (char *) DEST, * s = (char *) SRC;

While (count --)
* TMP ++ = * s ++;

Return DEST;
}
# Endif

# Ifndef _ have_arch_memmove
/**
* Memmove-copy one area of memory to another
* @ DEST: Where to copy
* @ SRC: Where to copy from
* @ Count: the size of the area.
*
* Unlike memcpy (), memmove () copes with overlapping areas.
*/
Void * memmove (void * DEST, const void * SRC, size_t count)
{
Char * TMP, * s;

If (DEST <= SRC ){
TMP = (char *) DEST;
S = (char *) SRC;
While (count --)
* TMP ++ = * s ++;
}
Else {
TMP = (char *) DEST + count;
S = (char *) SRC + count;
While (count --)
* -- TMP = * -- S;
}

Return DEST;
}
# Endif

# Ifndef _ have_arch_memcmp
/**
* Memcmp-compare two areas of memory
* @ Cs: one area of memory
* @ CT: Another area of memory
* @ Count: the size of the area.
*/
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;
}
# Endif

# Ifndef _ have_arch_memscan
/**
* Memscan-find a character in an area of memory.
* @ ADDR: the memory area
* @ C: the byte to search
* @ Size: the size of the area.
*
* Returns the address of the first occurrence of @ C, or 1 byte past
* The area if @ C is not found
*/
Void * memscan (void * ADDR, int C, size_t size)
{
Unsigned char * P = (unsigned char *) ADDR;

While (size ){
If (* P = C)
Return (void *) P;
P ++;
Size --;
}
Return (void *) P;
}
# Endif

# Ifndef _ have_arch_strstr
/**
* Strstr-find the first substring in a % NUL terminated string
* @ S1: the string to be searched
* @ S2: the string to search
*/
Char * strstr (const char * S1, const char * S2)
{
Int 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;
}
# Endif

# Ifndef _ have_arch_memchr
/**
* Memchr-find a character in an area of memory.
* @ S: the memory area
* @ C: the byte to search
* @ N: the size of the area.
*
* Returns the address of the first occurrence of @ C, or % null
* If @ C is not found
*/
Void * memchr (const void * s, int C, size_t N)
{
Const unsigned char * P = s;
While (n --! = 0 ){
If (unsigned char) C = * P ++ ){
Return (void *) P-1 );
}
}
Return NULL;
}

# Endif

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.