diff util.c @ 1008:51885857c019

Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
author Michael Pavone <pavone@retrodev.com>
date Sun, 01 May 2016 21:39:43 -0700
parents fd7702bcc034
children 4a92889e2889
line wrap: on
line diff
--- a/util.c	Sun May 01 17:43:28 2016 -0700
+++ b/util.c	Sun May 01 21:39:43 2016 -0700
@@ -94,6 +94,26 @@
 	return text+1;
 }
 
+char is_path_sep(char c)
+{
+#ifdef _WIN32
+	if (c == '\\') {
+		return 1;
+	}
+#endif
+	return c == '/';
+}
+
+char is_absolute_path(char *path)
+{
+#ifdef _WIN32
+	if (path[1] == ':' && is_path_sep(path[2]) && isalpha(path[0])) {
+		return 1;
+	}
+#endif
+	return is_path_sep(path[0]);
+}
+
 char * basename_no_extension(char *path)
 {
 	char *lastdot = NULL;
@@ -103,7 +123,7 @@
 	{
 		if (*cur == '.') {
 			lastdot = cur;
-		} else if (*cur == '/') {
+		} else if (is_path_sep(*cur)) {
 			lastslash = cur + 1;
 		}
 	}
@@ -314,7 +334,13 @@
 		return 0;
 	}
 	char *parent = strdup(path);
-	char *sep = strrchr(parent, '/');
+	//Windows technically supports both native and Unix-style path separators
+	//so search for both
+	char *sep = strrchr(parent, '\\');
+	char *osep = strrchr(parent, '/');
+	if (osep && (!sep || osep < sep)) {
+		sep = osep;
+	}
 	if (!sep || sep == parent) {
 		//relative path, but for some reason we failed
 		return 0;
@@ -372,7 +398,7 @@
 		int linksize = strlen(linktext);
 		for(cur = linktext + linksize - 1; cur != linktext; cur--)
 		{
-			if (*cur == '/') {
+			if (is_path_sep(*cur)) {
 				*cur = 0;
 				break;
 			}
@@ -387,7 +413,7 @@
 			int pathsize = strlen(exe_str);
 			for(cur = exe_str + pathsize - 1; cur != exe_str; cur--)
 			{
-				if (*cur == '/') {
+				if (is_path_sep(*cur)) {
 					exe_dir = malloc(cur-exe_str+1);
 					memcpy(exe_dir, exe_str, cur-exe_str);
 					exe_dir[cur-exe_str] = 0;
@@ -533,7 +559,7 @@
 		}
 		return NULL;
 	}
-	char const *pieces[] = {exe_dir, "/", name};
+	char const *pieces[] = {exe_dir, PATH_SEP, name};
 	char *path = alloc_concat_m(3, pieces);
 	FILE *f = fopen(path, "rb");
 	free(path);