2011. 10. 14. 09:05

/**
 * 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 */;
    return tmp;
}

/**
 * strncpy - Copy a length-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.
 *
 * In the case where the length of @src is less than that of
 * count, the remainder of @dest will be padded with %NUL.
 */
char *strncpy ( char *dest, const char *src, size_t count ) {
    char *tmp = dest;

    while ( count ) {
        if ( ( *dest = *src ) != 0 )
            src++;
        dest++;
        count--;
    }
    return tmp;
}

/**
 * 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' )
        /* nothing */;
    return tmp;
}

/**
 * strcat - 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 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 ) {
        while ( *dest )
            dest++;
        while ( ( *dest++ = *src++ ) != '\0' ) {
            if ( --count == 0 ) {
                *dest = '\0';
                break;
            }
        }
    }
    return tmp;
}

/**
 * strcmp - Compare two strings
 * @cs:	One string
 * @ct:	Another string
 */
int strcmp ( const char *cs, const char *ct ) {
    unsigned char c1, c2;

    do {
        c1 = *cs++;
        c2 = *ct++;
        if ( c1 != c2 )
            return c1 < c2 ? -1 : 1;
    } while ( c1 );
    return 0;
}

/**
 * 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 ) {
    unsigned int c1, c2;
    while ( count-- ) {
        c1 = *cs++;
        c2 = *ct++;
        if ( c1 != c2 )
            return c1 < c2 ? -1 : 1;
        if ( !c1 )
            break;
    }
    return 0;
}

/**
 * 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;
}
Posted by devanix