Riyyi
3 years ago
6 changed files with 102 additions and 39 deletions
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#include <pwd.h> // getpwnam |
||||
#include <sstream> // istringstream |
||||
#include <unistd.h> // gethostname, getlogin |
||||
|
||||
#include "machine.h" |
||||
#include "util/file.h" |
||||
|
||||
Machine::Machine(s) |
||||
{ |
||||
auto osRelease = Util::File("/etc/os-release"); |
||||
auto stream = std::istringstream(osRelease.data()); |
||||
std::string line; |
||||
while (std::getline(stream, line)) { |
||||
if (line.find("ID=") == 0) { |
||||
m_distroId = line.substr(3); |
||||
} |
||||
if (line.find("ID_LIKE=") == 0) { |
||||
m_distroIdLike = line.substr(8); |
||||
} |
||||
} |
||||
|
||||
char hostname[64] { 0 }; |
||||
if (gethostname(hostname, 64) < 0) { |
||||
perror("\033[31;1mError:\033[0m gethostname"); |
||||
} |
||||
m_hostname = hostname; |
||||
|
||||
// Get the username logged in on the controlling terminal of the process
|
||||
char username[32] { 0 }; |
||||
if (getlogin_r(username, 32) != 0) { |
||||
perror("\033[31;1mError:\033[0m getlogin_r"); |
||||
} |
||||
|
||||
// Get the password database record (/etc/passwd) of the user
|
||||
m_passwd = getpwnam(username); |
||||
if (m_passwd == nullptr) { |
||||
perror("\033[31;1mError:\033[0m getpwnam"); |
||||
} |
||||
} |
||||
|
||||
Machine::~Machine() |
||||
{ |
||||
} |
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#ifndef MACHINE_H |
||||
#define MACHINE_H |
||||
|
||||
#include <cstdint> // uint32_t |
||||
#include <pwd.h> // passwd |
||||
#include <string> |
||||
|
||||
#include "util/singleton.h" |
||||
|
||||
class Machine : public Util::Singleton<Machine> { |
||||
public: |
||||
Machine(s); |
||||
virtual ~Machine(); |
||||
|
||||
const std::string& distroId() { return m_distroId; } |
||||
const std::string& distroIdLike() { return m_distroIdLike; } |
||||
const std::string& hostname() { return m_hostname; } |
||||
|
||||
std::string username() { return m_passwd->pw_name; } |
||||
uint32_t uid() { return m_passwd->pw_uid; } |
||||
uint32_t gid() { return m_passwd->pw_gid; } |
||||
|
||||
private: |
||||
std::string m_distroId; |
||||
std::string m_distroIdLike; |
||||
std::string m_hostname; |
||||
passwd* m_passwd { nullptr }; |
||||
}; |
||||
|
||||
#endif // MACHINE_H
|
Loading…
Reference in new issue