disk-info/bin/main.cpp

67 lines
1.9 KiB
C++
Raw Normal View History

2023-04-19 21:04:10 +03:00
#include <iostream>
#include <sys/statvfs.h>
#include <mntent.h>
#include <cstdio>
#include <fstream>
template <typename T>
void print_in_color(const T& text, const std::string& color)
{
std::cout << "\033[" << color << "m" << text << "\033[0m";
}
template <typename T>
2023-04-19 21:08:44 +03:00
std::string get_size_string(const T& size)
{
if (size < 1024)
{
2023-04-19 21:04:10 +03:00
return std::to_string(size) + " B";
2023-04-19 21:08:44 +03:00
}
else if (size < 1024 * 1024)
{
2023-04-19 21:04:10 +03:00
return std::to_string(size / 1024) + " KB";
2023-04-19 21:08:44 +03:00
}
else if (size < 1024 * 1024 * 1024)
{
2023-04-19 21:04:10 +03:00
return std::to_string(size / 1024 / 1024) + " MB";
2023-04-19 21:08:44 +03:00
}
else
{
2023-04-19 21:04:10 +03:00
return std::to_string(size / 1024 / 1024 / 1024) + " GB";
}
}
auto main() -> int
{
std::ifstream fs("/etc/mtab");
std::FILE* file = std::fopen("/etc/fstab", "r");
struct mntent* mount_entry = nullptr;
std::cout << "\n\n";
while ((mount_entry = getmntent(file)))
{
struct statvfs stat;
if (statvfs(mount_entry->mnt_dir, &stat) != 0) {
std::cerr << "Ошибка при получении информации о файловой системе: " << mount_entry->mnt_dir << "\n";
continue;
}
2023-04-19 21:08:44 +03:00
unsigned long long total_size = (unsigned long long)stat.f_frsize * stat.f_blocks;
unsigned long long free_size = (unsigned long long)stat.f_frsize * stat.f_bfree;
2023-04-19 21:04:10 +03:00
unsigned long long used_size = total_size - free_size;
std::cout << "Точка монтирования: " << mount_entry->mnt_dir << "\n";
std::cout << "Тип файловой системы: " << mount_entry->mnt_type << "\n";
std::cout << "Общий объем: ";
print_in_color(get_size_string(total_size), "0;33");
std::cout << "\nСвободное место: ";
print_in_color(get_size_string(free_size), "0;32");
std::cout << "\nИспользовано: ";
print_in_color(get_size_string(used_size), "0;35");
std::cout << "\n\n";
}
std::fclose(file);
fs.close();
}