aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Finck <mail@colinfinck.de>2025-08-06 15:52:31 +0200
committerDavid Gibson <david@gibson.dropbear.id.au>2025-08-07 21:42:42 +1000
commit1c6c51e51b29a1c7204cb0e2085709ae7caa57af (patch)
treed76056125ac787e508e262a67545c5ae896da6fd
parent617f3d9b60f7c52473c52caf7c197718e80c912b (diff)
downloaddtc-1c6c51e51b29a1c7204cb0e2085709ae7caa57af.zip
dtc-1c6c51e51b29a1c7204cb0e2085709ae7caa57af.tar.gz
dtc-1c6c51e51b29a1c7204cb0e2085709ae7caa57af.tar.bz2
Consider drive letters when checking for absolute paths on Windows.
This still requires you to specify paths with forward slashes instead of backslashes on Windows, due to many hardcoded checks for '/'. Fortunately, the Windows user APIs all support forward slashes too. Signed-off-by: Colin Finck <mail@colinfinck.de> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r--srcpos.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/srcpos.c b/srcpos.c
index 5bb57bf..fef892f 100644
--- a/srcpos.c
+++ b/srcpos.c
@@ -89,6 +89,26 @@ static char *shorten_to_initial_path(char *fname)
}
/**
+ * Returns true if the given path is an absolute one.
+ *
+ * On Windows, it either needs to begin with a forward slash or with a drive
+ * letter (e.g. "C:").
+ * On all other operating systems, it must begin with a forward slash to be
+ * considered an absolute path.
+ */
+static bool is_absolute_path(const char *path)
+{
+#ifdef WIN32
+ return (
+ path[0] == '/' ||
+ (((path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z')) && path[1] == ':')
+ );
+#else
+ return (path[0] == '/');
+#endif
+}
+
+/**
* Try to open a file in a given directory.
*
* If the filename is an absolute path, then dirname is ignored. If it is a
@@ -103,7 +123,7 @@ static char *try_open(const char *dirname, const char *fname, FILE **fp)
{
char *fullname;
- if (!dirname || fname[0] == '/')
+ if (!dirname || is_absolute_path(fname))
fullname = xstrdup(fname);
else
fullname = join_path(dirname, fname);