One of the string codes in Linux (Copyright (C) 1991, 1992 Linus Torvalds

xiaoxiao2021-03-06  57

/ ** * strnicmp - 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 ; if (! C1) Break; IF (! C2) Break; IF (C1 == C2) Continue; c1 = tolower (C1); C2 = TOLOWER (C2); IF (C1! = C2) Break;} while (--len);} Return (int) c1 - (int) C2;} / ** * STRCPY - COPY A% NUL TERMINATED STRING * @Dest: where to copy the string to * @src: Where to copy the string from * / char * STRCPY (Char * DEST, Const char * src) {char * tmp = DEST;

While (* DEST = * src )! = '/ 0') / * Nothing * /; returnTMP;} / ** * STRNCPY - COPY a longth-limited,% NUL-TERMINATED STRING * @Dest: Where to copy the string to * @src: Where to copy the string from * @count: The maximum number of bytes to copy * * 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) {if ((* tmp = * src)! = 0) SRC ; TMP ; count-;} / ** * strcat - append one% Nul-Terminated string to another * @Dest: the string to be Appended to * @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;} / ** * strncat - append a length-limited,% NUL-TERMINATED STRING to Another * @Dest: the string to be appended to * @src: the string to append to it * @count: The maximum number 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 = de; if (count) {While (* DEST) DEST ; while (* DEST = * src )! = 0) {if (--count == 0) {* dest = '/ 0'; Break;}}}

Return TMP;} / ** * strlcat - append a length-limited,% NUL-TERMINATED STRING to Another * @dest: The string to be appended to * @src: the string to append to it * @count: The size of THE DESTINATION BUFFER. * / SIZE_T STRLCAT (CHAR * DEST, Const Char * src, size_t count) {size_t dsize = strlen (dest); size_t len ​​= Strlen (src); size_t res = dsize g

/ * This Would Be a bug * / bug_on (dsize> = count);

DEST = dsize; count - = dsize; if (len> = count) len = count-1; Memcpy (DEST, SRC, LEN); DEST [LEN] = 0; Return Res;} Return Dest;} / ** * strlcpy - Copy a% NUL terminated string into a sized buffer * @dest: Where to copy the string to * @src: Where to copy the string from * @size: size of destination buffer * * Compatible with * BSD: the result Is Always a Valid * Nul-Terminated String That Fits In The Buffer (Unless, * of Course, The Buff Size Is Zero). It Does Not Pad * Out The Result Like Strncpy () Does. * / size_t strlcpy (char * DEST , const char * src, size_t size) {size_t ret = strlen (src);

转载请注明原文地址:https://www.9cbs.com/read-82477.html

New Post(0)