rename sys to environment
and change the environment defenition files to be in environment dir
This commit is contained in:
parent
8b60cdc81b
commit
5b0dcb23ea
103
environment/lin.cpp
Normal file
103
environment/lin.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
#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();
|
||||
}
|
108
environment/win.cpp
Normal file
108
environment/win.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include "environment.hpp"
|
||||
#include "func.hpp"
|
||||
|
||||
#include <shlobj.h>
|
||||
#include <Windows.h>
|
||||
|
||||
bool put_data_on_clipboard(const char *text)
|
||||
{
|
||||
size_t len = strlen(text);
|
||||
if (len == 0)
|
||||
{
|
||||
printf_s("Text is empty\n");
|
||||
return false;
|
||||
}
|
||||
// Open the clipboard
|
||||
if (!OpenClipboard(nullptr))
|
||||
{
|
||||
printf_s("Failed to open clipboard");
|
||||
return false;
|
||||
}
|
||||
// Allocate global memory for the text
|
||||
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1);
|
||||
if (!hMem)
|
||||
{
|
||||
CloseClipboard();
|
||||
printf_s("Failed to allocate memory for text");
|
||||
return false;
|
||||
}
|
||||
// Copy the text to the allocated memory
|
||||
char *memData = static_cast<char *>(GlobalLock(hMem));
|
||||
if (!memData)
|
||||
{
|
||||
CloseClipboard();
|
||||
printf_s("Failed to lock memory for text");
|
||||
return false;
|
||||
}
|
||||
strcpy_s(memData, strlen(text) + 1, text);
|
||||
GlobalUnlock(hMem);
|
||||
// Set the data to the clipboard
|
||||
EmptyClipboard();
|
||||
SetClipboardData(CF_TEXT, hMem);
|
||||
// Clean up and close the clipboard
|
||||
CloseClipboard();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string get_user_password()
|
||||
{
|
||||
|
||||
HANDLE hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
||||
DWORD mode = 0;
|
||||
|
||||
// Create a restore point Mode
|
||||
// is know 503
|
||||
GetConsoleMode(hStdInput, &mode);
|
||||
|
||||
// Enable echo input
|
||||
// set to 499
|
||||
SetConsoleMode(hStdInput, mode & (~ENABLE_ECHO_INPUT));
|
||||
|
||||
// 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 mode
|
||||
SetConsoleMode(hStdInput, mode);
|
||||
|
||||
return ipt;
|
||||
}
|
||||
|
||||
std::optional<std::string> get_save_path()
|
||||
{
|
||||
namespace fs = std::filesystem;
|
||||
const char *path = std::getenv("PROGRAMDATA");
|
||||
if (path == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
fs::path userDirectory(path);
|
||||
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();
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
|
||||
#include "arg_func.hpp"
|
||||
#include "func.hpp"
|
||||
#include "sys.hpp"
|
||||
#include "environment.hpp"
|
||||
#include "func.hpp"
|
||||
#include "buffer.hpp"
|
||||
#include "aes256.hpp"
|
||||
|
12
source/environment.cpp
Normal file
12
source/environment.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
#define GLOB_IMPLEMENTATION
|
||||
#include "glob.hpp"
|
||||
|
||||
#define CRYPTORAND_IMPLEMENTATION
|
||||
#include "cryptorand.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "../environment/win.cpp"
|
||||
#else
|
||||
#include "../environment/lin.cpp"
|
||||
#endif
|
@ -2,11 +2,7 @@
|
||||
|
||||
#include "func.hpp"
|
||||
#include "buffer.hpp"
|
||||
|
||||
#define GLOB_IMPLEMENTATION
|
||||
#include "glob.hpp"
|
||||
|
||||
#define CRYPTORAND_IMPLEMENTATION
|
||||
#include "cryptorand.hpp"
|
||||
|
||||
int find_logininfo_in_buffer(Buffer &buffer, const char *label)
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
|
||||
#include "sys.hpp"
|
||||
#include "environment.hpp"
|
||||
#include "buffer.hpp"
|
||||
#include "aes256.hpp"
|
||||
#include "func.hpp"
|
||||
|
211
source/sys.cpp
211
source/sys.cpp
@ -1,211 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include "sys.hpp"
|
||||
#include "func.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <shlobj.h>
|
||||
#include <Windows.h>
|
||||
|
||||
bool put_data_on_clipboard(const char *text)
|
||||
{
|
||||
size_t len = strlen(text);
|
||||
if (len == 0)
|
||||
{
|
||||
printf_s("Text is empty\n");
|
||||
return false;
|
||||
}
|
||||
// Open the clipboard
|
||||
if (!OpenClipboard(nullptr))
|
||||
{
|
||||
printf_s("Failed to open clipboard");
|
||||
return false;
|
||||
}
|
||||
// Allocate global memory for the text
|
||||
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1);
|
||||
if (!hMem)
|
||||
{
|
||||
CloseClipboard();
|
||||
printf_s("Failed to allocate memory for text");
|
||||
return false;
|
||||
}
|
||||
// Copy the text to the allocated memory
|
||||
char *memData = static_cast<char *>(GlobalLock(hMem));
|
||||
if (!memData)
|
||||
{
|
||||
CloseClipboard();
|
||||
printf_s("Failed to lock memory for text");
|
||||
return false;
|
||||
}
|
||||
strcpy_s(memData, strlen(text) + 1, text);
|
||||
GlobalUnlock(hMem);
|
||||
// Set the data to the clipboard
|
||||
EmptyClipboard();
|
||||
SetClipboardData(CF_TEXT, hMem);
|
||||
// Clean up and close the clipboard
|
||||
CloseClipboard();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string get_user_password()
|
||||
{
|
||||
|
||||
HANDLE hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
||||
DWORD mode = 0;
|
||||
|
||||
// Create a restore point Mode
|
||||
// is know 503
|
||||
GetConsoleMode(hStdInput, &mode);
|
||||
|
||||
// Enable echo input
|
||||
// set to 499
|
||||
SetConsoleMode(hStdInput, mode & (~ENABLE_ECHO_INPUT));
|
||||
|
||||
// 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 mode
|
||||
SetConsoleMode(hStdInput, mode);
|
||||
|
||||
return ipt;
|
||||
}
|
||||
|
||||
std::optional<std::string> get_save_path()
|
||||
{
|
||||
namespace fs = std::filesystem;
|
||||
const char *path = std::getenv("PROGRAMDATA");
|
||||
if (path == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
fs::path userDirectory(path);
|
||||
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();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user