+#endif
+}
+
+Path relative(const Path &path, const Path &base)
+{
+ Path::Iterator i=path.begin();
+ Path::Iterator j=base.begin();
+ for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
+
+ Path result;
+ for(; j!=base.end(); ++j)
+ result/="..";
+ for(; i!=path.end(); ++i)
+ result/=*i;
+
+ return result;
+}
+
+/**
+Extracts the basename from the given path. Same thing as Path::Path(p)[-1],
+but faster.
+*/
+string basename(const std::string &p)
+{
+ unsigned slash=p.rfind(DIRCHAR);
+ if(slash==string::npos)
+ return p;
+ else
+ return p.substr(slash+1);
+}
+
+int stat(const Path &fn, struct stat &st)
+{
+ return ::stat(fn.str().c_str(), &st);
+}
+
+struct stat stat(const Path &fn)
+{
+ struct stat st;
+ if(stat(fn, st)==-1)
+ throw SystemError("stat failed", errno);
+ return st;
+}
+
+Path getcwd()
+{
+ char buf[1024];
+ return ::getcwd(buf, sizeof(buf));
+}
+
+Path get_home_dir()
+{
+#ifndef WIN32
+ const char *home=getenv("HOME");
+ if(home)
+ return home;
+ return ".";
+#else
+ return ".";
+#endif
+}
+
+void chdir(const Path &path)
+{
+ if(::chdir(path.str().c_str())==-1)
+ throw SystemError("chdir failed", errno);