Skip to content

Commit

Permalink
query: system: add initsys_version + some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Aug 23, 2024
1 parent 883d926 commit 3016062
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class System
std::string& os_name();
std::string& os_id();
std::string& os_initsys_name();
std::string& os_initsys_version();
std::string& os_versionid();
std::string& os_version_codename();
long& uptime();
Expand Down
1 change: 0 additions & 1 deletion include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct byte_units_t
float num_bytes;
};

constexpr const char NOCOLOR[] = "\033[0m";
constexpr const char UNKNOWN[] = "(unknown)";

// magic line to be sure that I don't cut the wrong line
Expand Down
4 changes: 2 additions & 2 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
{
std::string pureOutput;
std::string asciiArt_s = parse(line, systemInfo, pureOutput, config, colors, false);
asciiArt_s += config.gui ? "" : NOCOLOR;
asciiArt_s += config.gui ? "" : "\033[0m";

if (config.gui)
{
Expand Down Expand Up @@ -162,7 +162,7 @@ std::vector<std::string> Display::render(const Config& config, const colors_t& c
for (size_t j = 0; j < spaces; j++)
layouts.at(i).insert(origin, " ");

layouts.at(i) += config.gui ? "" : NOCOLOR;
layouts.at(i) += config.gui ? "" : "\033[0m";
}

if (i < asciiArt.size())
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ os
uptime_days : uptime of the system in days (should be used along with others uptime_ members) [2]
hostname : hostname of the OS [mymainPC]
initsys_name : Init system name [systemd]
initsys_version: Init system version [256.5-1-arch]
user
name : name you are currently logged in (not real name) [toni69]
Expand Down
6 changes: 4 additions & 2 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
if (command == "1")
output =
output.replace(dollarSignIndex, (endBracketIndex + 1) - dollarSignIndex,
config.gui ? "</span><span weight='bold'>" : fmt::format("{}\033[1m", NOCOLOR));
config.gui ? "</span><span weight='bold'>" : "\033[0m\033[1m");
else if (command == "0")
output = output.replace(dollarSignIndex, (endBracketIndex + 1) - dollarSignIndex,
config.gui ? "</span><span>" : NOCOLOR);
config.gui ? "</span><span>" : "\033[0m");
else
{
std::string str_clr;
Expand Down Expand Up @@ -521,6 +521,8 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co
case "pkgs"_fnv1a16: SYSINFO_INSERT(query_system.pkgs_installed(config)); break;

case "initsys_name"_fnv1a16: SYSINFO_INSERT(query_system.os_initsys_name()); break;

case "initsys_version"_fnv1a16: SYSINFO_INSERT(query_system.os_initsys_version()); break;

case "hostname"_fnv1a16: SYSINFO_INSERT(query_system.hostname()); break;
}
Expand Down
57 changes: 54 additions & 3 deletions src/query/unix/system.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include <linux/limits.h>
#include <unistd.h>

#include <array>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <filesystem>

#include "config.hpp"
#include "query.hpp"
#include "util.hpp"
#include "switch_fnv1a.hpp"
#include "utils/packages.hpp"

using namespace Query;
Expand Down Expand Up @@ -154,12 +157,10 @@ std::string& System::os_initsys_name()
if (!done)
{
// there's no way PID 1 doesn't exist.
// This will always succeed.
// This will always succeed (because we are on linux)
std::ifstream f_initsys("/proc/1/comm", std::ios::binary);
if (!f_initsys.is_open())
{
die("/proc/1/comm doesn't exist! (what?)");
}

std::string initsys;
std::getline(f_initsys, initsys);
Expand All @@ -179,6 +180,56 @@ std::string& System::os_initsys_name()
return m_system_infos.os_initsys_name;
}

std::string& System::os_initsys_version()
{
static bool done = false;
if (done)
return m_system_infos.os_initsys_version;

std::string path;
char buf[PATH_MAX];
if (realpath(which("init").c_str(), buf))
path = buf;

std::ifstream f(path, std::ios::in);
std::string line;

const std::string& name = str_tolower(this->os_initsys_name());
switch (fnv1a16::hash(name))
{
case "systemd"_fnv1a16:
case "systemctl"_fnv1a16:
{
while (read_binary_file(f, line))
{
if (hasEnding(line, "running in %ssystem mode (%s)"))
{
m_system_infos.os_initsys_version = line.substr("systemd "_len);
m_system_infos.os_initsys_version.erase(m_system_infos.os_initsys_version.find(' '));
break;
}
}
}
break;
case "openrc"_fnv1a16:
{
std::string tmp;
while(read_binary_file(f, line))
{
if (line == "RC_VERSION")
{
m_system_infos.os_initsys_version = tmp;
break;
}
tmp = line;
}
}
break;
}
done = true;
return m_system_infos.os_initsys_version;
}

std::string& System::pkgs_installed(const Config& config)
{
static bool done = false;
Expand Down
1 change: 1 addition & 0 deletions src/query/unix/utils/packages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ std::string get_all_pkgs(const Config& config)
{
case "pacman"_fnv1a16:
pkgs_count.pacman = get_num_count_dir("/var/lib/pacman/local");
pkgs_count.pacman--; // remove ALPM_DB count
ADD_PKGS_COUNT(pacman);
break;

Expand Down
2 changes: 1 addition & 1 deletion src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ bool read_binary_file(std::ifstream& f, std::string& ret)
else
{
if (buffer.length() >= min_string_length)
ret += buffer;
ret = buffer;

return true;
}
Expand Down

0 comments on commit 3016062

Please sign in to comment.