Modify libgen tc to avoid constant string modification
#include <libgen.h>
#include "tc_internal.h"
+#define MAX_PATH_LEN 18
+
/**
* @fn :tc_libc_libgen_basename
* @brief :The basename command strips any "path" name components from a filename, leaving you with a "pure" filename.
*/
static void tc_libc_libgen_basename(void)
{
- char *path;
+ char path[MAX_PATH_LEN + 1] = { '\0' };
char *ret_chk;
- path = NULL;
- ret_chk = basename(path);
- TC_ASSERT_EQ("basename", strncmp(ret_chk, ".", strlen(".")), 0);
-
- path = "\0";
ret_chk = basename(path);
TC_ASSERT_EQ("basename", strncmp(ret_chk, ".", strlen(".")), 0);
- path = "/";
+ strncpy(path, "/", MAX_PATH_LEN);
ret_chk = basename(path);
TC_ASSERT_EQ("basename", strncmp(ret_chk, "/", strlen("/")), 0);
- path = "//";
+ strncpy(path, "//", MAX_PATH_LEN);
ret_chk = basename(path);
TC_ASSERT_EQ("basename", strncmp(ret_chk, "/", strlen("/")), 0);
- path = "/dirname/basename";
+ strncpy(path, "/dirname/basename", MAX_PATH_LEN);
ret_chk = basename(path);
TC_ASSERT_EQ("basename", strncmp(ret_chk, "basename", strlen("basename")), 0);
- path = "basename";
+ strncpy(path, "basename", MAX_PATH_LEN);
ret_chk = basename(path);
TC_ASSERT_EQ("basename", strncmp(ret_chk, "basename", strlen("basename")), 0);
@@ -81,36 +78,32 @@ static void tc_libc_libgen_basename(void)
* @Postonditions :None
* @return :void
*/
+
static void tc_libc_libgen_dirname(void)
{
- char *path;
+ char path[MAX_PATH_LEN + 1] = { '\0' };
char *ret_chk;
- path = NULL;
- ret_chk = dirname(path);
- TC_ASSERT_EQ("dirname", strncmp(ret_chk, ".", strlen(".")), 0);
-
- path = "\0";
ret_chk = dirname(path);
TC_ASSERT_EQ("dirname", strncmp(ret_chk, ".", strlen(".")), 0);
- path = "/";
+ strncpy(path, "/", MAX_PATH_LEN);
ret_chk = dirname(path);
TC_ASSERT_EQ("dirname", strncmp(ret_chk, "/", strlen("/")), 0);
- path = "//";
+ strncpy(path, "//", MAX_PATH_LEN);
ret_chk = dirname(path);
TC_ASSERT_EQ("dirname", strncmp(ret_chk, "/", strlen("/")), 0);
- path = "/dirname/basename";
+ strncpy(path, "/dirname/basename", MAX_PATH_LEN);
ret_chk = dirname(path);
TC_ASSERT_EQ("dirname", strncmp(ret_chk, "/dirname", strlen("/dirname")), 0);
- path = "basename";
+ strncpy(path, "basename", MAX_PATH_LEN);
ret_chk = dirname(path);
TC_ASSERT_EQ("dirname", strncmp(ret_chk, ".", strlen(".")), 0);
- path = "/dirname ";
+ strncpy(path, "/dirname ", MAX_PATH_LEN);
ret_chk = dirname(path);
TC_ASSERT_EQ("dirname", strncmp(ret_chk, "/", strlen("/")), 0);
