sys functions working
This commit is contained in:
parent
36de6ec412
commit
43e9aa8553
@ -30,3 +30,7 @@ Flags:
|
||||
- V3 - Add username to LoginInfo
|
||||
- V2 - Remove string size limit
|
||||
- V1 - Working with string size limit of 20 characters
|
||||
|
||||
## Requirements
|
||||
### For linux
|
||||
sudo apt install xclip
|
||||
|
@ -16,7 +16,7 @@ public:
|
||||
Buffer();
|
||||
~Buffer();
|
||||
bool resize(size_t new_size);
|
||||
int add_end(uint8_t *data, size_t data_size);
|
||||
uint32_t add_end(uint8_t *data, size_t data_size);
|
||||
int add_middle(uint8_t *data, size_t data_size, size_t index);
|
||||
|
||||
// Removes data from buffer without checking if it's in the buffer
|
||||
|
2
main.cpp
2
main.cpp
@ -31,7 +31,7 @@ int main(int argc, char **argv)
|
||||
|
||||
args = get_args(argc, argv, &label);
|
||||
if (args == Arg::Error)
|
||||
return 1;
|
||||
return 0;
|
||||
|
||||
save_location_path = get_save_path();
|
||||
if (!save_location_path.has_value())
|
||||
|
@ -38,7 +38,7 @@ bool Buffer::resize(size_t new_size)
|
||||
return true;
|
||||
}
|
||||
|
||||
int Buffer::add_end(uint8_t *data, size_t data_size)
|
||||
uint32_t Buffer::add_end(uint8_t *data, size_t data_size)
|
||||
{
|
||||
if (taken + data_size > size)
|
||||
if (!resize(size + data_size))
|
||||
|
@ -45,9 +45,9 @@ void delete_logininfo_from_buffer(Buffer &buffer, int index_of_pass)
|
||||
|
||||
void add_logininfo_to_buffer(Buffer &buffer, const char *label, const char *username, const char *password)
|
||||
{
|
||||
int label_start = buffer.add_end((uint8_t *)label, strlen(label) + 1);
|
||||
int username_start = buffer.add_end((uint8_t *)username, strlen(username) + 1);
|
||||
int password_start = buffer.add_end((uint8_t *)password, strlen(password) + 1);
|
||||
uint32_t label_start = buffer.add_end((uint8_t *)label, strlen(label) + 1);
|
||||
uint32_t username_start = buffer.add_end((uint8_t *)username, strlen(username) + 1);
|
||||
uint32_t password_start = buffer.add_end((uint8_t *)password, strlen(password) + 1);
|
||||
|
||||
Index *index = (Index *)buffer.buffer;
|
||||
|
||||
|
@ -119,22 +119,101 @@ std::optional<std::string> get_save_path()
|
||||
|
||||
#else
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <filesystem>
|
||||
|
||||
bool put_data_on_clipboard(const char *text)
|
||||
{
|
||||
// TODO
|
||||
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()
|
||||
{
|
||||
// TODO
|
||||
return "123";
|
||||
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()
|
||||
{
|
||||
// TODO
|
||||
return "save.bin";
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
const char *homeDir = getenv("HOME");
|
||||
if (homeDir == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
fs::path userDirectory(homeDir);
|
||||
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