From 87e586d39b9434d39eed7fb018230c6c3875062b Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Tue, 2 Feb 2016 21:57:53 -0500 Subject: [PATCH] find the home directory --- mpw-shell.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/mpw-shell.cpp b/mpw-shell.cpp index 65c98ff..925b9a1 100644 --- a/mpw-shell.cpp +++ b/mpw-shell.cpp @@ -22,11 +22,39 @@ //#include #include +#include +#include +#include + +//#include + +std::string root() { + + static std::string root; + + if (root.empty()) { + const char *cp = getenv("HOME"); + if (!cp || !*cp) { + auto pw = getpwuid(getuid()); + if (!pw) { + fprintf(stderr,"Unable to determine home directory\n."); + exit(EX_NOUSER); + } + cp = pw->pw_dir; + } + root = cp; + if (root.back() != '/') root.push_back('/'); + root += "mpw/"; + } + return root; +} // should set {MPW}, {MPWVersion}, then execute {MPW}StartUp -void init(Environment &e) { - e.set("status", std::string("0")); - e.set("exit", std::string("1")); // terminate script on error. - e.set("echo", std::string("1")); +void init(Environment &env) { + + env.set("mpw", root()); + env.set("status", std::string("0")); + env.set("exit", std::string("1")); // terminate script on error. + env.set("echo", std::string("1")); } int read_file(phase1 &p, const std::string &file) {