Nikola Petrov 5b0dcb23ea rename sys to environment
and change the environment defenition files
to be in environment dir
2024-07-22 10:43:35 +02:00

103 lines
1.9 KiB
C++

#include <iostream>
#include <string>
#include <filesystem>
#include "environment.hpp"
#include "func.hpp"
#include <cstring>
#include <iostream>
#include <termios.h>
#include <unistd.h>
bool put_data_on_clipboard(const char *text)
{
size_t len = strlen(text);
if (len == 0)
{
printf("Text is empty\n");
return false;
}
// Open a pipe to xclip command
FILE *pipe = popen("xclip -selection clipboard", "w");
if (!pipe)
{
printf("Failed to open pipe to xclip\n");
return false;
}
// Write text to xclip
if (fwrite(text, 1, len, pipe) != len)
{
printf("Failed to write text to xclip\n");
pclose(pipe);
return false;
}
// Close the pipe
pclose(pipe);
return true;
}
std::string get_user_password()
{
struct termios oldt, newt;
// Save the current terminal attributes
tcgetattr(STDIN_FILENO, &oldt);
// Copy the old settings to the new settings
newt = oldt;
// Disable echo input
newt.c_lflag &= ~ECHO;
// Apply the new settings
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
// Take input
std::string ipt;
std::getline(std::cin, ipt);
// Otherwise next cout will print into the same line
std::cout << std::endl;
// Restore the terminal attributes
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ipt;
}
std::optional<std::string> get_save_path()
{
namespace fs = std::filesystem;
const char *homeDir = getenv("HOME");
if (homeDir == nullptr)
{
return {};
}
fs::path userDirectory(homeDir);
userDirectory /= ".config";
userDirectory /= "password_manager";
if (!fs::exists(userDirectory))
{
// Directory doesn't exist, create it
if (!fs::create_directory(userDirectory))
{
return {};
}
}
else if (!fs::is_directory(userDirectory))
{
// Path exists but is not a directory
return {};
}
userDirectory /= "save.txt";
return userDirectory.string();
}