From ce50a35ce9671bdd613b2a05f02ada7eacab2634 Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Wed, 10 Jul 2024 20:48:49 +0300 Subject: [PATCH] Fix for #157 --- MiniScript-cpp/src/ShellIntrinsics.cpp | 6 +++++- MiniScript-cpp/tests/testFileName.ms | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 MiniScript-cpp/tests/testFileName.ms diff --git a/MiniScript-cpp/src/ShellIntrinsics.cpp b/MiniScript-cpp/src/ShellIntrinsics.cpp index 3b1710b..f8e0aec 100644 --- a/MiniScript-cpp/src/ShellIntrinsics.cpp +++ b/MiniScript-cpp/src/ShellIntrinsics.cpp @@ -337,8 +337,12 @@ static IntrinsicResult intrinsic_basename(Context *context, IntrinsicResult part char extBuf[256]; _splitpath_s(pathStr.c_str(), driveBuf, sizeof(driveBuf), nullptr, 0, nameBuf, sizeof(nameBuf), extBuf, sizeof(extBuf)); String result = String(nameBuf) + String(extBuf); - #else + #elif defined(__APPLE__) || defined(__FreeBSD__) String result(basename((char*)pathStr.c_str())); + #else + char *duplicate = strdup((char*)pathStr.c_str()); + String result(basename(duplicate)); + free(duplicate); #endif return IntrinsicResult(result); } diff --git a/MiniScript-cpp/tests/testFileName.ms b/MiniScript-cpp/tests/testFileName.ms new file mode 100644 index 0000000..9ccfb65 --- /dev/null +++ b/MiniScript-cpp/tests/testFileName.ms @@ -0,0 +1,12 @@ +import "qa" + +testDir = "tests/" + +testFileName = function + p = "a/b/" + n = file.name(p) + qa.assertEqual n, "b" + qa.assertEqual p, "a/b/" +end function + +if refEquals(locals, globals) then testFileName