diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-03 05:18:46 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-03 05:18:46 +0000 |
| commit | 065ca31d261e55b26bcd4ddc5877b8a0ac9e77b7 (patch) | |
| tree | e13a728c0fa0295ee84014ac5759b6aecd897276 /src/util | |
| parent | 7b27a3efde5d19c592ce674da26fdf0a6ef628a9 (diff) | |
safe_str
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/prompt.cc | 5 | ||||
| -rw-r--r-- | src/util/string.cc | 5 |
2 files changed, 7 insertions, 3 deletions
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; +} + } |
