aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/util/string.h1
-rw-r--r--src/path.cc5
-rw-r--r--src/util/prompt.cc5
-rw-r--r--src/util/string.cc5
4 files changed, 10 insertions, 6 deletions
diff --git a/include/util/string.h b/include/util/string.h
index 85aae8d..f5e8ef9 100644
--- a/include/util/string.h
+++ b/include/util/string.h
@@ -12,5 +12,6 @@ namespace ov4
void string2argv(const std::vector<std::string> * const s, char **argv);
void argv2string(std::vector<std::string> *s, const char * const * argv);
+std::string safe_str(const char *s, const char *fallback = "");
}
diff --git a/src/path.cc b/src/path.cc
index 3ab8c0c..103e2c5 100644
--- a/src/path.cc
+++ b/src/path.cc
@@ -31,8 +31,7 @@ string path_unifier(const string &s, bool is_cmd)
// single word, use path
if (is_cmd && (s.find("/") == string::npos))
{
- const char *path_p = getenv("PATH");
- string path = (path_p != nullptr) ? path_p : "";
+ string path = safe_str(getenv("PATH"));
size_t i = 0, j = 0, l = path.size();
while (true)
{
@@ -65,7 +64,7 @@ int set_current_dir(const string &s)
string get_current_dir()
{
char buf[MAXLINE];
- return (string)(getcwd(buf, MAXLINE));
+ return safe_str(getcwd(buf, MAXLINE));
}
diff --git a/src/util/prompt.cc b/src/util/prompt.cc
index 08ff7e4..92ce2ce 100644
--- a/src/util/prompt.cc
+++ b/src/util/prompt.cc
@@ -13,12 +13,11 @@ namespace ov4
{
char hostname[MAXLINE];
gethostname(hostname, MAXLINE);
- char *user = getlogin();
cout
<< make_color((string)name, ov4::color::bright_green)
<< " "
- << make_color((user != nullptr ? user : "ov4"), ov4::color::bright_blue)
+ << make_color(safe_str(getlogin()) , ov4::color::bright_blue)
<< make_color("@", ov4::color::bright_blue)
<< make_color(hostname, ov4::color::bright_blue)
<< ":"
@@ -32,6 +31,6 @@ namespace ov4
string get_prompt()
{
- return (string)((string)(getlogin()) == "root" ? prompt_root : prompt_normal);
+ return (string)(safe_str(getlogin()) == "root" ? prompt_root : prompt_normal);
}
}
diff --git a/src/util/string.cc b/src/util/string.cc
index 7146fe3..cc0de34 100644
--- a/src/util/string.cc
+++ b/src/util/string.cc
@@ -35,4 +35,9 @@ void argv2string(vector<string> *s, const char * const * argv)
}
}
+std::string safe_str(const char *s, const char *fallback)
+{
+ return (s != nullptr) ? s : fallback;
+}
+
}