1 /*
2 *
3 * Conky, a system monitor, based on torsmo
4 *
5 * Any original torsmo code is licensed under the BSD license
6 *
7 * All code written since the fork of torsmo is licensed under the GPL
8 *
9 * Please see COPYING for details
10 *
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
13 * (see AUTHORS)
14 * All rights reserved.
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30 #include <inttypes.h>
31 #include <time.h>
32 #include "config.h"
33 #include "conky.h"
34 #include "text_object.h"
35
36 /* check for OS and include appropriate headers */
37 #if defined(__linux__)
38 #include "linux.h"
39 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
40 #include "freebsd.h"
41 #elif defined(__DragonFly__)
42 #include "dragonfly.h"
43 #elif defined(__OpenBSD__)
44 #include "openbsd.h"
45 #elif defined(__sun)
46 #include "solaris.h"
47 #elif defined(__HAIKU__)
48 #include "haiku.h"
49 #elif defined(__APPLE__) && defined(__MACH__)
50 #include "darwin.h"
51 #endif
52
53 struct _entropy {
54 _entropy() = default;
55 unsigned int avail{0};
56 unsigned int poolsize{0};
57 };
58
59 static _entropy entropy;
60
update_entropy()61 int update_entropy() {
62 get_entropy_avail(&entropy.avail);
63 get_entropy_poolsize(&entropy.poolsize);
64 return 0;
65 }
66
print_entropy_avail(struct text_object * obj,char * p,unsigned int p_max_size)67 void print_entropy_avail(struct text_object *obj, char *p,
68 unsigned int p_max_size) {
69 (void)obj;
70 snprintf(p, p_max_size, "%u", entropy.avail);
71 }
72
entropy_percentage(struct text_object * obj)73 uint8_t entropy_percentage(struct text_object *obj) {
74 (void)obj;
75 return round_to_positive_int(static_cast<double>(entropy.avail) * 100.0 /
76 static_cast<double>(entropy.poolsize));
77 }
78
print_entropy_poolsize(struct text_object * obj,char * p,unsigned int p_max_size)79 void print_entropy_poolsize(struct text_object *obj, char *p,
80 unsigned int p_max_size) {
81 (void)obj;
82 snprintf(p, p_max_size, "%u", entropy.poolsize);
83 }
84
entropy_barval(struct text_object * obj)85 double entropy_barval(struct text_object *obj) {
86 (void)obj;
87
88 return static_cast<double>(entropy.avail) / entropy.poolsize;
89 }
90
print_password(struct text_object * obj,char * p,unsigned int p_max_size)91 void print_password(struct text_object *obj, char *p, unsigned int p_max_size) {
92 time_t t;
93 static const char letters[] =
94 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*("
95 ")_";
96 static const int len = static_cast<int>(sizeof(letters)) - 1;
97 uintmax_t x = strtoumax(obj->data.s, (char **)NULL, 10);
98 uintmax_t z = 0;
99
100 if (-1 == (t = time(NULL))) { return; }
101 srandom(static_cast<unsigned int>(t));
102
103 for (; z < x && p_max_size - 1 > z; z++) { *p++ = letters[random() % len]; }
104 *p = '\0';
105 }
106