+namespace
+{
+
+/**
+Helper function to determine the location of the program's executable. Caches
+the last result to cut down filesystem access with repeated calls.
+*/
+const Path &get_bin_dir(const Path &argv0)
+{
+ static Path last_argv0;
+ static Path bin_dir;
+
+ if(!(argv0==last_argv0))
+ {
+ Path exe;
+ if(argv0.size()==1)
+ {
+ const char *path=getenv("PATH");
+ vector<string> dirs=split(path, ':');
+ for(vector<string>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
+ if(exists(Path(*i)/argv0))
+ {
+ exe=realpath(Path(*i)/argv0);
+ break;
+ }
+ }
+ else
+ exe=realpath(argv0);
+
+ last_argv0=argv0;
+ bin_dir=dirname(exe);
+ }
+
+ return bin_dir;
+}
+
+}
+