forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-ls.c
236 lines (206 loc) · 5.29 KB
/
cmd-ls.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (c) 2014-2015 LastPass.
*/
#include "cmd.h"
#include "util.h"
#include "config.h"
#include "terminal.h"
#include "kdf.h"
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
static bool long_listing = false;
static bool show_mtime = true;
struct node {
char *name;
struct account *account;
bool shared;
struct node *first_child;
struct node *next_sibling;
};
static char *format_timestamp(char *timestamp, bool utc)
{
char temp[60];
struct tm *ts_tm;
time_t ts_time_t = (time_t) strtoul(timestamp, NULL, 10);
if (ts_time_t == 0)
return xstrdup("");
if (utc)
ts_tm = gmtime(&ts_time_t);
else
ts_tm = localtime(&ts_time_t);
strftime(temp, sizeof(temp), "%Y-%m-%d %H:%M", ts_tm);
return xstrdup(temp);
}
static void insert_node(struct node *head, const char *path, struct account *account)
{
char *slash = NULL;
struct node *child;
while (*path && (slash = strchr(path, '/')) == path)
++path;
if (!path)
return;
if (!slash) {
child = new0(struct node, 1);
child->account = account;
child->shared = !!account->share;
child->name = xstrdup(path);
child->next_sibling = head->first_child;
head->first_child = child;
return;
}
for (child = head->first_child; child; child = child->next_sibling) {
if (!strncmp(child->name, path, slash - path) && strlen(child->name) == (size_t)(slash - path))
break;
}
if (!child) {
child = new0(struct node, 1);
child->shared= !!account->share;
child->name = xstrndup(path, slash - path);
child->next_sibling = head->first_child;
head->first_child = child;
}
insert_node(child, slash + 1, account);
}
static void free_node(struct node *head)
{
if (!head)
return;
for (struct node *node = head, *next_node = NULL; node; node = next_node) {
next_node = node->next_sibling;
free_node(node->first_child);
free(node->name);
free(node);
}
}
static void print_node(struct node *head, int level)
{
struct node *node;
for (node = head; node; node = node->next_sibling) {
if (node->name) {
for (int i = 0; i < level; ++i)
printf(" ");
if (node->account) {
if (long_listing) {
_cleanup_free_ char *timestr;
if (show_mtime)
timestr = format_timestamp(node->account->last_modified_gmt, true);
else
timestr = format_timestamp(node->account->last_touch, false);
terminal_printf(TERMINAL_FG_CYAN "%s ", timestr);
}
terminal_printf(TERMINAL_FG_GREEN TERMINAL_BOLD "%s" TERMINAL_NO_BOLD " [id: %s]" TERMINAL_RESET "\n", node->name, node->account->id);
}
else if (node->shared)
terminal_printf(TERMINAL_FG_CYAN TERMINAL_BOLD "%s" TERMINAL_RESET "\n", node->name);
else
terminal_printf(TERMINAL_FG_BLUE TERMINAL_BOLD "%s" TERMINAL_RESET "\n", node->name);
}
print_node(node->first_child, level + 1);
}
}
static char *get_display_fullname(struct account *account)
{
char *fullname = NULL;
if (account->share || strcmp(account->group, ""))
fullname = xstrdup(account->fullname);
else
xasprintf(&fullname, "(none)/%s", account->fullname);
return fullname;
}
int cmd_ls(int argc, char **argv)
{
unsigned char key[KDF_HASH_LEN];
struct session *session = NULL;
struct blob *blob = NULL;
static struct option long_options[] = {
{"sync", required_argument, NULL, 'S'},
{"color", required_argument, NULL, 'C'},
{"long", no_argument, NULL, 'l'},
{0, 0, 0, 0}
};
int option;
int option_index;
char *group = NULL;
int group_len;
char *sub;
struct node *root;
char *fullname;
enum blobsync sync = BLOB_SYNC_AUTO;
enum color_mode cmode = COLOR_MODE_AUTO;
bool print_tree;
struct account *account;
while ((option = getopt_long(argc, argv, "lmu", long_options, &option_index)) != -1) {
switch (option) {
case 'S':
sync = parse_sync_string(optarg);
break;
case 'C':
cmode = parse_color_mode_string(optarg);
break;
case 'l':
long_listing = true;
break;
case 'm':
show_mtime = true;
break;
case 'u':
show_mtime = false;
break;
case '?':
default:
die_usage(cmd_ls_usage);
}
}
switch (argc - optind) {
case 0:
break;
case 1:
group = argv[optind];
break;
default:
die_usage(cmd_ls_usage);
}
terminal_set_color_mode(cmode);
print_tree = cmode == COLOR_MODE_ALWAYS ||
(cmode == COLOR_MODE_AUTO && isatty(fileno(stdout)));
init_all(sync, key, &session, &blob);
root = new0(struct node, 1);
/* '(none)' group -> search for any without group */
if (group && !strcmp(group, "(none)"))
group = "";
list_for_each_entry(account, &blob->account_head, list) {
if (group) {
sub = strstr(account->group, group);
if (!sub || sub != account->group)
continue;
group_len = strlen(group);
sub += group_len;
if (group[group_len - 1] != '/' && sub[0] != '\0' && sub[0] != '/')
continue;
}
fullname = get_display_fullname(account);
if (print_tree)
insert_node(root, fullname, account);
else {
if (long_listing) {
_cleanup_free_ char *timestr;
if (show_mtime)
timestr = format_timestamp(account->last_modified_gmt, true);
else
timestr = format_timestamp(account->last_touch, false);
printf("%s ", timestr);
}
printf("%s [id: %s]\n", fullname, account->id);
}
free(fullname);
}
if (print_tree)
print_node(root, -1);
free_node(root);
session_free(session);
blob_free(blob);
return 0;
}