aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2020-04-18 10:40:05 +1000
committerSteve Bennett <steveb@workware.net.au>2020-05-04 21:57:37 +1000
commit6b7b6b8c9fb6c203e99847a1114825504599b9f9 (patch)
tree7d925901ff77722e73d2432bdfacb8fced239c28
parentda82368c816c8d06f425aa3f25a2a918fdba1df1 (diff)
downloadjimtcl-6b7b6b8c9fb6c203e99847a1114825504599b9f9.zip
jimtcl-6b7b6b8c9fb6c203e99847a1114825504599b9f9.tar.gz
jimtcl-6b7b6b8c9fb6c203e99847a1114825504599b9f9.tar.bz2
file: rootname, dirname fixes to match Tcl
Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r--jim-file.c14
-rw-r--r--tests/file.test11
2 files changed, 16 insertions, 9 deletions
diff --git a/jim-file.c b/jim-file.c
index 24f39e5..49bd331 100644
--- a/jim-file.c
+++ b/jim-file.c
@@ -254,11 +254,13 @@ static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
const char *path = Jim_String(objPtr);
const char *p = strrchr(path, '/');
- if (!p && path[0] == '.' && path[1] == '.' && path[2] == '\0') {
- Jim_SetResultString(interp, "..", -1);
- } else if (!p) {
+ if (!p) {
Jim_SetResultString(interp, ".", -1);
}
+ else if (p[1] == 0) {
+ /* Trailing slash so do nothing */
+ Jim_SetResult(interp, objPtr);
+ }
else if (p == path) {
Jim_SetResultString(interp, "/", -1);
}
@@ -306,18 +308,16 @@ static int file_cmd_split(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
- Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]);
- const char *path = Jim_String(objPtr);
+ const char *path = Jim_String(argv[0]);
const char *lastSlash = strrchr(path, '/');
const char *p = strrchr(path, '.');
if (p == NULL || (lastSlash != NULL && lastSlash > p)) {
- Jim_SetResult(interp, objPtr);
+ Jim_SetResult(interp, argv[0]);
}
else {
Jim_SetResultString(interp, path, p - path);
}
- Jim_DecrRefCount(interp, objPtr);
return JIM_OK;
}
diff --git a/tests/file.test b/tests/file.test
index 049469d..f279206 100644
--- a/tests/file.test
+++ b/tests/file.test
@@ -122,9 +122,8 @@ test dirname-1.4 "Trailing slash" {
} {.}
test dirname-1.5 ".." {
- # Should be . to match Tcl
file dirname ..
-} {..}
+} {.}
test dirname-1.6 "abc/.." {
file dirname abc/..
@@ -210,6 +209,14 @@ test rootname-1.6 {file rootname odd cases} -body {
file rootname abc/def.c/ghi
} -result {abc/def.c/ghi}
+test rootname-1.7 {file rootname odd cases} -body {
+ file rootname abc/def.c/
+} -result {abc/def.c/}
+
+test rootname-1.8 {file rootname odd cases} -body {
+ file rootname abc/def.c//
+} -result {abc/def.c//}
+
test readable-1.1 {file readable} {
file readable [info script]
} {1}