[libposix-development] [PATCH] strncpy implementation and tests
Status: Pre-Alpha
Brought to you by:
hdante
From: Tordek <ke...@gm...> - 2009-07-01 00:02:22
|
Signed-off-by: Guillermo O. Freschi <to...@to...> --- CMakeLists.txt | 4 ++++ mandatory/include/string.h | 1 + mandatory/string.c | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 0 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6be902..435d8d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,15 +85,19 @@ add_executable(printf_scanner tests/internal/printf_scanner.c ${PROJECT_BINARY_DIR}/${runtime_file_name}) add_executable(printf_parser tests/internal/printf_parser.c ${PROJECT_BINARY_DIR}/${runtime_file_name}) +add_executable(strncpy tests/internal/strncpy.c + ${PROJECT_BINARY_DIR}/${runtime_file_name}) add_test(puts_hello puts_hello) add_test(hello hello) add_test(printf_scanner printf_scanner) add_test(printf_parser printf_parser) +add_test(strncpy strncpy) target_link_libraries(posix ${LIBPOSIX_COMPILER_STYLE}) target_link_libraries(hello posix) target_link_libraries(puts_hello posix) target_link_libraries(printf_scanner posix) target_link_libraries(printf_parser posix) +target_link_libraries(strncpy posix) install(TARGETS posix DESTINATION ${LIBPOSIX_LIBRARY_DIRECTORY}) install(DIRECTORY ${mandatory_include_path}/ DESTINATION ${LIBPOSIX_INCLUDE_DIRECTORY}) diff --git a/mandatory/include/string.h b/mandatory/include/string.h index 0c9d517..fe690bd 100644 --- a/mandatory/include/string.h +++ b/mandatory/include/string.h @@ -41,5 +41,6 @@ char *strcat(char *restrict, const char *restrict); char *strchr(const char *, int); int strcmp(const char *, const char *); char *strcpy(char *restrict, const char *restrict); +char *strncpy(char *restrict s1, const char *restrict s2, size_t n); #endif /* _STRING_H_DEFINED_ */ diff --git a/mandatory/string.c b/mandatory/string.c index db91b92..fad1f1e 100644 --- a/mandatory/string.c +++ b/mandatory/string.c @@ -77,6 +77,26 @@ char *strcat(char *restrict dst, const char *restrict src) return dst; } +char *strncpy(char *restrict s1, const char *restrict s2, size_t size) { + char *d = (unsigned char *) s1; + char *s = (unsigned char *) s2; + + while(size--) { + *d++ = *s++; + + if(*s == '\0') + break; + } + + size++; + + while(size--) { + *d++ = '\0'; + } + + return s1; +} + void *memset(void *ptr, int value, size_t size) { char *p = (char *)ptr; -- 1.6.3.3 -- Guillermo O. «Tordek» Freschi. Programador, Escritor, Genio Maligno. http://tordek.com.ar :: http://twitter.com/tordek http://www.arcanopedia.com.ar - Juegos de Rol en Argentina |