lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 3163d0b7005ced0479eb528e34a4a7428e428bf7
parent becac250828d4310614afe74480b9ed2a533c1f6
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 17 May 2017 18:11:10 +0200

Add(Utils): Add file_exist function, and test

Diffstat:
Minc/m_util.h | 15+++++++++++++++
Mtests/test_util.c | 7+++++++
2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/inc/m_util.h b/inc/m_util.h @@ -34,4 +34,19 @@ */ bool recursive_delete(const char *dir); +/*! + * \brief Check if a file is there or not + * + * \param[in] str File path + * + * \return true if the file exist, false otherwise + */ +static inline bool file_exist(const char *str) { + int fd = open(str, O_RDONLY); + bool result = (fd != -1); + + close(fd); + return result; +} + #endif /* M_UTIL */ diff --git a/tests/test_util.c b/tests/test_util.c @@ -48,7 +48,14 @@ TEST(recursive_delete) { return TEST_SUCCESS; } +TEST(file_exist) { + TEST_ASSERT(file_exist("Makefile") == true, "Error on the return"); + TEST_ASSERT(file_exist("Makefile_totally_not_here") == false, "Error on the return"); + return TEST_SUCCESS; +} + void register_util_tests(void) { reg_test("util", init_dir); reg_test("util", recursive_delete); + reg_test("util", file_exist); }