-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
58 lines (51 loc) · 1.21 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstdlib>
#include <sstream>
#include "passgen.h"
using namespace std;
std::string exec(const char* cmd);
std::string paste();
std::string copy(const char * new_clipboard);
const string initFilename = "init";
int main(int argc, char *argv[]) {
string input;
if (argc == 2)
input = string(argv[1]);
else { cout << "Usage: " << argv[0] << " <pass_phrase>" << endl;
exit(1);
}
PASSGEN pg;
pg.init(initFilename);
string pass = pg.generate(input, 10);
cout << pass << endl;
//TODO: Make it work only with specific key
//Copy to clipboard
//string exec = "echo \"" + pass + "\" | xclip -selection \"clipboard\"";
//system(exec.c_str());
copy(pass.c_str());
return 0;
}
std::string exec(const char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
{
result += buffer;
}
}
pclose(pipe);
return result;
}
std::string paste()
{
return exec("pbpaste");
}
std::string copy(const char * new_clipboard)
{
std::stringstream cmd;
cmd << "echo \"" << new_clipboard << "\" | pbcopy";
return exec(cmd.str().c_str());
}