-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
137 lines (116 loc) · 3.19 KB
/
main.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<stdio.h>
#include<getopt.h>
#include<string.h>
#include "includes/db.h"
#include "includes/modes.h"
void print_invalid();
void print_help ();
void interactive ();
/*
* the print_help function act as the help section for the user.
*
* */
void print_help () {
printf("Usage: secrets <action> [options]\n\n"
"Actions:\n\tsave|s:to save the given credentials.\n"
"\tget|g: to retrieve a specific account/domain's password based on the given username or tag.\n"
"\tdelete|d: to delete a credential of a given username or name value.\n"
"\tinteractive|i: to start an interactive session.\n\n"
"Options:\n"
"\t-h | --help: help.\n"
"\t-u: username or email.\n"
"\t-p: password.\n"
"\t-t: name of the account or domain of the given credentials.\n\n"
"NOTE:\n\tThis program only checks for the first letter in the action word.\n"
"\tFor example, if save is the action the it only check for the first letter 's'.\n");
}
/*
* this function is amied to provied an interactive functionality for this program.
*
* BUT IS IS IT COMPLETED.
*
* */
void interactive () {
puts("welcome to secrets!\n");
puts("Please select any action to continue.");
puts("[1] Save New Password.\n[2] Get Saved Password.\n[3]. Delete Password.\n[4] Exit.\n\n");
printf("Enter your choice: ");
int choice=0;
scanf("%d",&choice);
switch(choice) {
case 1:
puts("Saving...");
break;
case 2:
puts("Retrieving...");
break;
case 3:
puts("Deleting...\n");
break;
case 4:
default:
puts("Exiting...");
break;
}
return;
}
/*
* parse_mode() prases the first command line argument and comapres it with valid avaliable modes
* it returns a mode from the list modes defined in the enum Mode
* it returns invalid mode when the given modevalue does not match any valid mode.
*/
Mode parse_mode(const char *modevalue) {
if (strncmp(modevalue, "-h", (size_t)2) == 0 || strncmp(modevalue, "--help", (size_t)6) == 0) {
return Help;
}
if (strncmp(modevalue, "save", (size_t)4) == 0) {
return Save;
}
if (strncmp(modevalue, "get", (size_t)3) == 0) {
return Get;
}
if (strncmp(modevalue, "delete", (size_t)6) == 0) {
return Delete;
}
if (strncmp(modevalue, "save", (size_t)4) == 0) {
return Save;
}
if (strncmp(modevalue, "shell", (size_t)5) == 0 || strncmp(modevalue, "cli", (size_t)3) == 0) {
return Interactive;
}
return Invalid;
}
//declare print_invalid function to use if given arguments are invalid!
void print_invalid (int *ret_code){
*ret_code = -1;
printf("Invalid use of program! use -h or --help for help\n");
}
/*
* This is the main function, what else it may do? as intended it only calls few function to make this program work.
*
* */
int main (int argc, char **argv) {
if(prepare_env() == 1)
return 1;
int ret_code = 0;
if(argc <= 1) {
print_invalid(&ret_code);
return ret_code;
}
Mode mode = parse_mode(argv[1]);
if (mode != Help && argc != 3) {
print_invalid(&ret_code);
return ret_code;
}
switch(mode) {
case Help:
print_help();
return 0;
case Invalid:
print_invalid(&ret_code);
return ret_code;
}
set_mode(mode);
process_mode(argv[2]);
return ret_code;
}