- if (!home || g_access(home, W_OK))
- /* Fatal error */
- g_critical("Unable to find a base directory");
-
- /* Build the name of the directory */
- viking_dir = g_build_filename(home, ".viking", NULL);
- if (g_file_test(viking_dir, G_FILE_TEST_EXISTS) == FALSE)
- g_mkdir(viking_dir, 0755);
+/**
+ * Always return the canonical filename in a newly allocated string
+ */
+char *file_realpath_dup ( const char *path )
+{
+ char real[MAXPATHLEN];
+
+ g_return_val_if_fail(path != NULL, NULL);
+
+ if (file_realpath(path, real))
+ return g_strdup(real);
+
+ return g_strdup(path);
+}
+
+/**
+ * Permission granted to use this code after personal correspondance
+ * Slightly reworked for better cross platform use, glibisms, function rename and a compacter format
+ *
+ * FROM http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263
+ */
+
+// GetRelativeFilename(), by Rob Fisher.
+// rfisher@iee.org
+// http://come.to/robfisher
+
+// The number of characters at the start of an absolute filename. e.g. in DOS,
+// absolute filenames start with "X:\" so this value should be 3, in UNIX they start
+// with "\" so this value should be 1.
+#ifdef WINDOWS
+#define ABSOLUTE_NAME_START 3
+#else
+#define ABSOLUTE_NAME_START 1
+#endif
+
+// Given the absolute current directory and an absolute file name, returns a relative file name.
+// For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given,
+// GetRelativeFilename will return ..\whee\text.txt.
+
+const gchar *file_GetRelativeFilename ( gchar *currentDirectory, gchar *absoluteFilename )
+{
+ gint afMarker = 0, rfMarker = 0;
+ gint cdLen = 0, afLen = 0;
+ gint i = 0;
+ gint levels = 0;
+ static gchar relativeFilename[MAXPATHLEN+1];
+
+ cdLen = strlen(currentDirectory);
+ afLen = strlen(absoluteFilename);
+
+ // make sure the names are not too long or too short
+ if (cdLen > MAXPATHLEN || cdLen < ABSOLUTE_NAME_START+1 ||
+ afLen > MAXPATHLEN || afLen < ABSOLUTE_NAME_START+1) {
+ return NULL;
+ }
+
+ // Handle DOS names that are on different drives:
+ if (currentDirectory[0] != absoluteFilename[0]) {
+ // not on the same drive, so only absolute filename will do
+ strcpy(relativeFilename, absoluteFilename);
+ return relativeFilename;