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; }
'예제 코드 & 코드 조각' 카테고리의 다른 글
재시작 라이브러리 (restart library) (0) | 2011.09.03 |
---|---|
showtimes - exit핸들러 등록하여 프로그램 CPU 사용률 출력 (0) | 2011.09.03 |
keeplog - 리스트 자료 구조를 사용하여 커맨드 실행 기록 저장 (0) | 2011.09.02 |
wordaverage - 한 줄 평균 단어 구하기 (0) | 2011.09.02 |
makeargv - 인자 배열 만들기 (0) | 2011.09.02 |