-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cpp
More file actions
118 lines (102 loc) · 2.9 KB
/
User.cpp
File metadata and controls
118 lines (102 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// User.cpp for rush in /home/erwan/Code/teck/piscine/cpp_gkrellm
//
// Made by erwan
// Login <erwan.simon@epitech.eu>
//
// Started on Sat Jan 21 14:22:34 2017 erwan
// Last update Sat Jan 21 21:01:47 2017 antoine
//
#include <sys/utsname.h>
#include <iostream>
#include <fstream>
#include <string>
#include "User.hpp"
#include "Infos.hpp"
User::User()
{
}
User::~User()
{
}
User& User::operator=(User const &other)
{
this->_machineName = other._machineName;
this->_userName = other._userName;
this->_opSys = other._opSys;
this->_kernel = other._kernel;
this->_date = other._date;
this->_time = other._time;
return (*this);
}
void User::setMachineName(std::string machineName) {this->_machineName = machineName;}
void User::setUserName(std::string userName) {this->_userName = userName;}
void User::setOpSys(std::string opSys) {this->_opSys = opSys;}
void User::setKernel(std::string kernel) {this->_kernel = kernel;}
void User::setDate(std::string date) {this->_date = date;}
void User::setTime(std::string time) {this->_time = time;}
std::string User::getMachineName() const {return (this->_machineName);}
std::string User::getUserName() const {return (this->_userName);}
std::string User::getOpSys() const {return (this->_opSys);}
std::string User::getKernel() const {return (this->_kernel);}
std::string User::getDate() const {return (this->_date);}
std::string User::getTime() const {return (this->_time);}
void init_User(Infos &_info)
{
sys_get_hostname(_info);
// sys_get_username(_info);
sys_get_kernel(_info);
sys_get_time(_info);
}
void sys_get_hostname(Infos &_info)
{
std::string _src = "/proc/sys/kernel/hostname";
std::ifstream _file(_src.c_str(), std::ios::in);
std::string _hostname = "ERROR";;
if (_file)
{
if (!_file.eof())
std::getline(_file, _hostname);
}
_file.close();
_info._user.setMachineName(_hostname);
}
// void sys_get_username(Infos &_info)
// {
// _info._user.setUserName("UserName");
// }
void sys_get_kernel(Infos &_info)
{
struct utsname unameData;
std::string k_name = "ERROR";
std::string k_vers = "ERROR";
if(uname(&unameData) != -1)
{
k_name = unameData.sysname;
k_vers = unameData.release;
}
_info._user.setOpSys(k_name);
_info._user.setKernel(k_vers);
}
void sys_get_time(Infos &_info)
{
std::string _src = "/proc/driver/rtc";
std::ifstream _file(_src.c_str(), std::ios::in);
std::string _line;
std::string _time = "ERROR";
std::string _day = "ERROR";
if (_file)
{
while(!_file.eof())
{
std::getline(_file, _line);
if (_line.find("rtc_time") != _line.npos)
_time = _line.substr (11,19);
if (_line.find("rtc_date") != _line.npos)
_day = _line.substr (11,21);
}
}
_file.close();
_info._user.setTime(_time);
_info._user.setDate(_day);
}