diff options
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/src/util.cc b/src/util.cc index 299fc942f2ef..8c397aace8c8 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1,47 +1,55 @@ +#include <iostream> + #include "util.hh" string thisSystem = SYSTEM; -string nixHomeDir = "/nix"; -string nixHomeDirEnvVar = "NIX"; +SysError::SysError(string msg) +{ + char * sysMsg = strerror(errno); + err = msg + ": " + sysMsg; +} + -string absPath(string filename, string dir) +string absPath(string path, string dir) { - if (filename[0] != '/') { + if (path[0] != '/') { if (dir == "") { char buf[PATH_MAX]; if (!getcwd(buf, sizeof(buf))) - throw Error("cannot get cwd"); + throw SysError("cannot get cwd"); dir = buf; } - filename = dir + "/" + filename; + path = dir + "/" + path; /* !!! canonicalise */ char resolved[PATH_MAX]; - if (!realpath(filename.c_str(), resolved)) - throw Error("cannot canonicalise path " + filename); - filename = resolved; + if (!realpath(path.c_str(), resolved)) + throw SysError("cannot canonicalise path " + path); + path = resolved; } - return filename; + return path; +} + + +string dirOf(string path) +{ + unsigned int pos = path.rfind('/'); + if (pos == string::npos) throw Error("invalid file name: " + path); + return string(path, 0, pos); } -/* Return the directory part of the given path, i.e., everything - before the final `/'. */ -string dirOf(string s) +string baseNameOf(string path) { - unsigned int pos = s.rfind('/'); - if (pos == string::npos) throw Error("invalid file name"); - return string(s, 0, pos); + unsigned int pos = path.rfind('/'); + if (pos == string::npos) throw Error("invalid file name: " + path); + return string(path, pos + 1); } -/* Return the base name of the given path, i.e., everything following - the final `/'. */ -string baseNameOf(string s) +void debug(string s) { - unsigned int pos = s.rfind('/'); - if (pos == string::npos) throw Error("invalid file name"); - return string(s, pos + 1); + cerr << "debug: " << s << endl; } |