Add save location in userdir/password_manager

This commit is contained in:
Nikola Petrov 2023-08-28 17:37:57 +02:00
parent 14e2780dbc
commit 02c80c0517
3 changed files with 53 additions and 2 deletions

View File

@ -1,4 +1,6 @@
#pragma once
#include <optional>
bool put_data_on_clipboard(const char* text);
std::string get_user_password();
std::string get_user_password();
std::optional<std::string> get_save_path();

View File

@ -87,12 +87,21 @@ int main()
int main(int argc, char** argv)
{
std::optional<std::string> path = get_save_path();
if (!path.has_value())
{
printf_s("Error opening save location\n");
return 0;
}
char* label = nullptr;
Arg args = get_args(argc, argv, &label);
if (args == Arg::Error) return 1;
Buffer encrypted_buffer;
if (!encrypted_buffer.load_from_file("passwords.bin"))
if (!encrypted_buffer.load_from_file(path.value()))
if (!(args == Arg::Generate || args == Arg::Input)) {
printf_s("No passwords, try generating password\n");
return 1;

View File

@ -1,6 +1,7 @@
#include <Windows.h>
#include <iostream>
#include <string>
#include <shlobj.h>
#include "win.h"
#include "func.h"
@ -66,4 +67,43 @@ std::string get_user_password()
SetConsoleMode(hStdInput, mode);
return ipt;
}
std::optional<std::string> get_save_path()
{
PWSTR path = nullptr;
// Get the user's documents directory
HRESULT result = SHGetKnownFolderPath(FOLDERID_Profile, 0, nullptr, &path);
if (!SUCCEEDED(result)) {
CoTaskMemFree(path); // Free the allocated memory
return{};
}
std::wstring userDirectory(path);
CoTaskMemFree(path); // Free the allocated memory
userDirectory.append(L"\\password_manager");
DWORD attrib = GetFileAttributes(userDirectory.c_str());
//check if it exists
if(!(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY)))
{
if (!CreateDirectory(userDirectory.c_str(), nullptr)) return {};
}
userDirectory.append(L"\\passwords.bin");
int size = WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (size > 0) {
std::string str(size, 0);
WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, &str[0], size, nullptr, nullptr);
return str;
}
return {};
}