summaryrefslogtreecommitdiff
path: root/src/filename_absolute.cxx
diff options
context:
space:
mode:
authorMichael R Sweet <michael.r.sweet@gmail.com>1998-10-06 18:21:25 +0000
committerMichael R Sweet <michael.r.sweet@gmail.com>1998-10-06 18:21:25 +0000
commitf9039b2ae21988783feae9b362818e7923e82d14 (patch)
tree6d6fe3679d73448758f9794e7d4d4f6b22a4adad /src/filename_absolute.cxx
parent67e89232f9ba067825a158734a09e0fa21aacbe3 (diff)
Initial revision
git-svn-id: file:///fltk/svn/fltk/trunk@2 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/filename_absolute.cxx')
-rw-r--r--src/filename_absolute.cxx67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/filename_absolute.cxx b/src/filename_absolute.cxx
new file mode 100644
index 000000000..d5a937539
--- /dev/null
+++ b/src/filename_absolute.cxx
@@ -0,0 +1,67 @@
+/* expand a file name by prepending current directory, deleting . and
+ .. (not really correct for symbolic links) between the prepended
+ current directory. Use $PWD if it exists.
+ Returns true if any changes were made.
+*/
+
+#include <FL/filename.H>
+#include <stdlib.h>
+#include <string.h>
+#if defined(WIN32) && !defined(CYGNUS)
+# include <direct.h>
+# define getcwd(a,b) _getdcwd(0,a,b)
+#else
+# include <unistd.h>
+# ifdef __EMX__
+# define getcwd _getcwd2
+# endif
+#endif
+
+#if defined(WIN32) || defined(__EMX__)
+inline int isdirsep(char c) {return c=='/' || c=='\\';}
+#else
+#define isdirsep(c) ((c)=='/')
+#endif
+
+int filename_absolute(char *to,const char *from) {
+
+ if (isdirsep(*from) || *from == '|'
+#if defined(WIN32) || defined(__EMX__)
+ || from[1]==':'
+#endif
+ ) {
+ strcpy(to,from);
+ return 0;
+ }
+
+ char *a,temp[FL_PATH_MAX];
+ const char *start = from;
+
+ a = getenv("PWD");
+ if (a) strncpy(temp,a,FL_PATH_MAX);
+ else {a = getcwd(temp,FL_PATH_MAX); if (!a) return 0;}
+#if defined(WIN32) || defined(__EMX__)
+ for (a = temp; *a; a++) if (*a=='\\') *a = '/'; // ha ha
+#else
+ a = temp+strlen(temp);
+#endif
+ if (isdirsep(*(a-1))) a--;
+ /* remove intermediate . and .. names: */
+ while (*start == '.') {
+ if (start[1]=='.' && isdirsep(start[2])) {
+ char *b;
+ for (b = a-1; b >= temp && !isdirsep(*b); b--);
+ if (b < temp) break;
+ a = b;
+ start += 3;
+ } else if (isdirsep(start[1])) {
+ start += 2;
+ } else
+ break;
+ }
+ *a++ = '/';
+ strcpy(a,start);
+ strcpy(to,temp);
+ return 1;
+
+}