summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorReckordp <[email protected]>2020-03-08 05:32:27 +0700
committerReckordp <[email protected]>2020-03-08 05:32:27 +0700
commit6ee24fe1bc9c5a43704cbf0d24aa5c931b91a3a3 (patch)
tree28043034fda8230c31f637195d5b14f7575f8d3e
parent5a4b9fd8772a7bd34c8866716a7622237fed5e7b (diff)
downloadmruby-6ee24fe1bc9c5a43704cbf0d24aa5c931b91a3a3.tar.gz
mruby-6ee24fe1bc9c5a43704cbf0d24aa5c931b91a3a3.zip
Evaluate all type path
-rw-r--r--mrbgems/mruby-io/src/file.c54
1 files changed, 44 insertions, 10 deletions
diff --git a/mrbgems/mruby-io/src/file.c b/mrbgems/mruby-io/src/file.c
index ffce0ddcb..25b1dd6e1 100644
--- a/mrbgems/mruby-io/src/file.c
+++ b/mrbgems/mruby-io/src/file.c
@@ -48,6 +48,7 @@
#if defined(_WIN32) || defined(_WIN64)
#define PATH_SEPARATOR ";"
#define FILE_ALT_SEPARATOR "\\"
+ #define VOLUME_SEPARATOR ":"
#else
#define PATH_SEPARATOR ":"
#endif
@@ -279,18 +280,51 @@ mrb_file__getwd(mrb_state *mrb, mrb_value klass)
return path;
}
+#ifdef _WIN32
+#define IS_FILESEP(x) (x == FILE_SEPARATOR || x == FILE_ALT_SEPARATOR)
+#define IS_VOLSEP(x) (x == VOLUME_SEPARATOR)
+#define IS_DEVICEID(x) (x == '.' || x == '?')
+
+static int
+is_absolute_traditional_path(const char *path, int len)
+{
+ if (len < 3) return 0;
+ if (IS_FILESEP(path[0])) return 1;
+ return (ISALPHA(path[0]) && IS_VOLSEP(path[1]) && IS_FILESEP(path[2]));
+}
+
+static int
+is_aboslute_unc_path(const char *path, int len) {
+ if (len < 2) return 0;
+ return (IS_FILESEP(path[0]) && IS_FILESEP(path[1]));
+}
+
+static int
+is_absolute_device_path(const char *path, int len) {
+ if (len < 4) return 0;
+ return (is_aboslute_unc_path(path, len) && IS_DEVICEID(path[2]) && IS_FILESEP(path[3]));
+}
+
static int
-mrb_file_is_absolute_path(const char *path)
+mrb_file_is_absolute_path(const char *path, int len)
{
-#ifdef _WIN32
-#define IS_PATHSEP(x) (x == '/' || x == '\\')
- if (isalpha(path[0]))
- return (strlen(path) > 2 && path[1] == ':' && IS_PATHSEP(path[2]));
- else
- return (IS_PATHSEP(path[0]) && IS_PATHSEP(path[1]));
-#undef IS_PATHSEP
+ return (
+ is_absolute_traditional_path(path, len) ||
+ is_aboslute_unc_path(path, len) ||
+ is_absolute_device_path(path, len)
+ );
+
+#undef IS_FILESEP
+#undef IS_VOLSEP
+#undef IS_DEVICEID
+
#else
- return (path[0] == '/');
+static int
+mrb_file_is_absolute_path(const char *path)
+{
+#define IS_FILESEP(x) (x == FILE_SEPARATOR)
+ return IS_FILESEP(path[0]);
+#undef IS_FILESEP
#endif
}
@@ -335,7 +369,7 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
if (home == NULL) {
return mrb_nil_value();
}
- if (!mrb_file_is_absolute_path(home)) {
+ if (!mrb_file_is_absolute_path(home, strlen(home))) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
}
} else {