This repository was archived by the owner on Jun 13, 2024. It is now read-only.
forked from magnus-ISU/paleofetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
614 lines (512 loc) · 16 KB
/
functions.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
static char *get_title(void *arg) {
(void)arg;
// reduce the maximum size for these, so that we don't over-fill the title string
char hostname[BUF_SIZE / 3];
status = gethostname(hostname, BUF_SIZE / 3);
halt_and_catch_fire("unable to retrieve host name");
char username[BUF_SIZE / 3];
status = getlogin_r(username, BUF_SIZE / 3);
if (getlogin_r(username, BUF_SIZE / 3) != 0) {
FILE *proc = popen("echo ${USER:-$(id -un || printf %s \"${HOME/*\/}\")}", "r");
fscanf(proc, "%s", &username);
}
title_length = strlen(hostname) + strlen(username) + 1;
char *title = safe_malloc(BUF_SIZE);
snprintf(title, BUF_SIZE, COLOR "%s\e[0m@" COLOR "%s", username, hostname);
return title;
}
static char *get_bar(void *arg) {
intptr_t bar_length = (intptr_t) arg;
if (!bar_length)
bar_length = title_length;
char *bar = safe_malloc(BUF_SIZE);
char *s = bar;
for (intptr_t i = 0; i < bar_length; i++)
*(s++) = '-';
*s = '\0';
return bar;
}
static char *get_os(void *arg) {
(void)arg;
char *os = safe_malloc(BUF_SIZE), *name = safe_malloc(BUF_SIZE), *line = NULL;
size_t len;
FILE *os_release = fopen("/etc/os-release", "r");
if (os_release == NULL) {
status = -1;
halt_and_catch_fire("unable to open /etc/os-release");
}
while (getline(&line, &len, os_release) != -1) {
if (sscanf(line, "NAME=\"%[^\"]+", name) > 0)
break;
}
free(line);
fclose(os_release);
snprintf(os, BUF_SIZE, "%s %s", name, uname_info.machine);
free(name);
return os;
}
static char *get_kernel(void *arg) {
(void)arg;
char *kernel = safe_malloc(BUF_SIZE);
strncpy(kernel, uname_info.release, BUF_SIZE);
return kernel;
}
static char *get_host(void *arg) {
(void)arg;
char *host = safe_malloc(BUF_SIZE), buffer[BUF_SIZE / 2];
FILE *product_name, *product_version, *model;
if ((product_name = fopen("/sys/devices/virtual/dmi/id/product_name", "r")) != NULL) {
if ((product_version = fopen("/sys/devices/virtual/dmi/id/product_version", "r")) != NULL) {
fread(host, 1, BUF_SIZE / 2, product_name);
remove_newline(host);
strcat(host, " ");
fread(buffer, 1, BUF_SIZE / 2, product_version);
remove_newline(buffer);
strcat(host, buffer);
fclose(product_version);
} else {
#ifndef HOST_SIMPLE
fclose(product_name);
goto model_fallback;
#endif
}
fclose(product_name);
return host;
}
model_fallback:
if ((model = fopen("/sys/firmware/devicetree/base/model", "r")) != NULL) {
fread(host, 1, BUF_SIZE, model);
remove_newline(host);
return host;
}
status = -1;
halt_and_catch_fire("unable to get host");
return NULL;
}
static char *get_uptime(void *arg) {
(void)arg;
long seconds = my_sysinfo.uptime;
struct {
char *name;
int secs;
} units[] = {
{"day", 60 * 60 * 24},
{"hour", 60 * 60},
{"min", 60},
};
int n, len = 0;
char *uptime = safe_malloc(BUF_SIZE);
for (int i = 0; i < 3; ++i) {
if ((n = seconds / units[i].secs) || i == 2) /* always print minutes */
len += snprintf(uptime + len, BUF_SIZE - len, "%d %s%s, ", n, units[i].name, n != 1 ? "s" : "");
seconds %= units[i].secs;
}
// null-terminate at the trailing comma
uptime[len - 2] = '\0';
return uptime;
}
// returns "<Battery Percentage>% [<Charging | Discharging | Unknown>]"
// Credit: allisio - https://gist.github.com/allisio/1e850b93c81150124c2634716fbc4815
static char *get_battery_percentage(void *arg) {
(void)arg;
int battery_capacity;
FILE *capacity_file, *status_file;
char battery_status[12] = "Unknown";
if ((capacity_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) == NULL) {
status = ENOENT;
halt_and_catch_fire("Unable to get battery information");
}
fscanf(capacity_file, "%d", &battery_capacity);
fclose(capacity_file);
if ((status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) {
fscanf(status_file, "%s", battery_status);
fclose(status_file);
}
// max length of resulting string is 19
// one byte for padding incase there is a newline
// 100% [Discharging]
// 1234567890123456789
char *battery = safe_malloc(20);
snprintf(battery, 20, "%d%% [%s]", battery_capacity, battery_status);
return battery;
}
static char *get_packages(const char *dirname, const char *pacname, int num_extraneous) {
int num_packages = 0;
DIR *dirp;
struct dirent *entry;
dirp = opendir(dirname);
if (dirp == NULL) {
status = -1;
halt_and_catch_fire("You may not have %s installed", dirname);
}
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_DIR)
num_packages++;
}
num_packages -= (2 + num_extraneous); // accounting for . and ..
status = closedir(dirp);
char *packages = safe_malloc(BUF_SIZE);
snprintf(packages, BUF_SIZE, "%d (%s)", num_packages, pacname);
return packages;
}
static char *get_packages_pacman(void *arg) { (void)arg; return get_packages(PACMAN"/var/lib/pacman/local", "pacman", 0); }
static char *get_shell(void *arg) {
(void)arg;
char *shell_path = getenv("SHELL");
char *shell_name = strrchr(shell_path, '/');
char *shell = safe_malloc(strlen(shell_path) + 1);
if (shell_name == NULL) /* if $SHELL doesn't have a '/' */
strcpy(shell, shell_path); /* copy the whole thing over */
else
strcpy(shell, shell_name + 1); /* o/w copy past the last '/' */
return shell;
}
static char *get_resolution(void *arg) {
(void)arg;
int screen, width, height;
char *resolution = safe_malloc(BUF_SIZE + 1);
resolution[BUF_SIZE] = 0;
if (display != NULL) {
screen = DefaultScreen(display);
width = DisplayWidth(display, screen);
height = DisplayHeight(display, screen);
snprintf(resolution, BUF_SIZE, "%dx%d", width, height);
} else {
DIR *dir;
struct dirent *entry;
FILE *modes;
char *line = NULL;
size_t len;
char modes_file_name[BUF_SIZE * 2];
char dir_name[] = "/sys/class/drm";
/* preload resolution with empty string, in case we cant find a resolution through parsing */
strcpy(resolution, "");
dir = opendir(dir_name);
if (dir == NULL) {
status = -1;
halt_and_catch_fire("Could not open /sys/class/drm to determine resolution in tty mode.");
}
/* parse through all directories and look for a non empty modes file */
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_LNK) {
snprintf(modes_file_name, BUF_SIZE * 2 + 9, "%s/%s/modes", dir_name, entry->d_name);
modes = fopen(modes_file_name, "r");
if (modes != NULL) {
if (getline(&line, &len, modes) != -1) {
strncpy(resolution, line, BUF_SIZE);
remove_newline(resolution);
free(line);
fclose(modes);
break;
}
fclose(modes);
}
}
}
closedir(dir);
}
return resolution;
}
static char *get_terminal(void *arg) {
(void)arg;
unsigned char *prop;
char *terminal = safe_malloc(BUF_SIZE+1);
terminal[BUF_SIZE] = 0;
/* check if xserver is running or if we are running in a straight tty */
if (display != NULL) {
unsigned long _, // not unused, but we don't need the results
window = RootWindow(display, XDefaultScreen(display));
Atom a, active = XInternAtom(display, "_NET_ACTIVE_WINDOW", True), class = XInternAtom(display, "WM_CLASS", True);
#define GetProp(property) XGetWindowProperty(display, window, property, 0, 64, 0, 0, &a, (int *)&_, &_, &_, &prop);
GetProp(active);
window = (prop[3] << 24) + (prop[2] << 16) + (prop[1] << 8) + prop[0];
free(prop);
if (!window)
goto terminal_fallback;
GetProp(class);
#undef GetProp
snprintf(terminal, BUF_SIZE, "%s", prop);
free(prop);
} else {
terminal_fallback:
strncpy(terminal, getenv("TERM"), BUF_SIZE); /* fallback to old method */
/* in tty, $TERM is simply returned as "linux"; in this case get actual tty name */
if (strcmp(terminal, "linux") == 0) {
strncpy(terminal, ttyname(STDIN_FILENO), BUF_SIZE);
}
}
return terminal;
}
static char *get_cpu(void *arg) {
(void)arg;
FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); /* read from cpu info */
if (cpuinfo == NULL) {
status = -1;
halt_and_catch_fire("Unable to open cpuinfo");
}
char *cpu_model = safe_malloc(BUF_SIZE / 2);
char *line = NULL;
size_t len; /* unused */
int num_cores = 0, cpu_freq, prec = 3;
double freq;
char freq_unit[] = "GHz";
/* read the model name into cpu_model, and increment num_cores every time model name is found */
while (getline(&line, &len, cpuinfo) != -1) {
num_cores += sscanf(line, "model name : %[^\n@]", cpu_model);
}
free(line);
fclose(cpuinfo);
FILE *cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
line = NULL;
if (cpufreq != NULL) {
if (getline(&line, &len, cpufreq) != -1) {
sscanf(line, "%d", &cpu_freq);
cpu_freq /= 1000; // convert kHz to MHz
} else {
fclose(cpufreq);
free(line);
goto cpufreq_fallback;
}
} else {
cpufreq_fallback:
cpufreq = fopen("/proc/cpuinfo", "r"); /* read from cpu info */
if (cpufreq == NULL) {
status = -1;
halt_and_catch_fire("Unable to open cpuinfo");
}
while (getline(&line, &len, cpufreq) != -1) {
if (sscanf(line, "cpu MHz : %lf", &freq) > 0)
break;
}
cpu_freq = (int)freq;
}
free(line);
fclose(cpufreq);
if (cpu_freq < 1000) {
freq = (double)cpu_freq;
freq_unit[0] = 'M'; // make MHz from GHz
prec = 0; // show frequency as integer value
} else {
freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double
while (cpu_freq % 10 == 0) {
--prec;
cpu_freq /= 10;
}
if (prec == 0)
prec = 1; // we don't want zero decimal places
}
/* remove unneeded information */
for (int i = 0; i < COUNT(cpu_config); ++i) {
if (cpu_config[i].repl_str == NULL) {
remove_substring(cpu_model, cpu_config[i].substring, cpu_config[i].length);
} else {
replace_substring(cpu_model, cpu_config[i].substring, cpu_config[i].repl_str, cpu_config[i].length, cpu_config[i].repl_len);
}
}
char *cpu = safe_malloc(BUF_SIZE);
snprintf(cpu, BUF_SIZE, "%s (%d) @ %.*f%s", cpu_model, num_cores, prec, freq, freq_unit);
free(cpu_model);
truncate_spaces(cpu);
if (num_cores == 0)
*cpu = '\0';
return cpu;
}
static char *get_gpu(void *arg) {
// inspired by https://github.com/pciutils/pciutils/edit/master/example.c
/* it seems that pci_lookup_name needs to be given a buffer, but I can't for the life of my figure out what its for */
char buffer[BUF_SIZE];
char *device_class, *gpu = safe_malloc(BUF_SIZE + 1);
gpu[BUF_SIZE] = 0;
struct pci_access *pacc;
struct pci_dev *dev;
intptr_t index = (intptr_t)arg;
intptr_t gpu_index = 0;
bool found = false;
pacc = pci_alloc();
pci_init(pacc);
pci_scan_bus(pacc);
dev = pacc->devices;
while (dev != NULL) {
pci_fill_info(dev, PCI_FILL_IDENT);
device_class = pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_CLASS, dev->device_class);
if (strcmp("VGA compatible controller", device_class) == 0 || strcmp("3D controller", device_class) == 0) {
if (gpu_index == index) {
strncpy(gpu, pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_DEVICE | PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id), BUF_SIZE);
found = true;
break;
} else {
gpu_index++;
}
}
dev = dev->next;
}
if (found == false)
*gpu = '\0'; // empty string, so it will not be printed
pci_cleanup(pacc);
/* remove unneeded information */
for (int i = 0; i < COUNT(gpu_config); ++i) {
if (gpu_config[i].repl_str == NULL) {
remove_substring(gpu, gpu_config[i].substring, gpu_config[i].length);
} else {
replace_substring(gpu, gpu_config[i].substring, gpu_config[i].repl_str, gpu_config[i].length, gpu_config[i].repl_len);
}
}
truncate_spaces(gpu);
return gpu;
}
static char *get_memory(void *arg) {
(void) arg;
int total_memory, used_memory;
int total, shared, memfree, buffers, cached, reclaimable;
FILE *meminfo = fopen("/proc/meminfo", "r"); /* get infomation from meminfo */
if (meminfo == NULL) {
status = -1;
halt_and_catch_fire("Unable to open meminfo");
}
/* We parse through all lines of meminfo and scan for the information we need */
char *line = NULL; // allocation handled automatically by getline()
size_t len; /* unused */
/* parse until EOF */
while (getline(&line, &len, meminfo) != -1) {
/* if sscanf doesn't find a match, pointer is untouched */
sscanf(line, "MemTotal: %d", &total);
sscanf(line, "Shmem: %d", &shared);
sscanf(line, "MemFree: %d", &memfree);
sscanf(line, "Buffers: %d", &buffers);
sscanf(line, "Cached: %d", &cached);
sscanf(line, "SReclaimable: %d", &reclaimable);
}
free(line);
fclose(meminfo);
/* use same calculation as neofetch */
used_memory = (total + shared - memfree - buffers - cached - reclaimable) / 1024;
total_memory = total / 1024;
int percentage = (int)(100 * (used_memory / (double)total_memory));
char *memory = safe_malloc(BUF_SIZE);
snprintf(memory, BUF_SIZE, "%dMiB / %dMiB (%d%%)", used_memory, total_memory, percentage);
return memory;
}
static char *get_disk_usage(void *arg) {
char *folder = arg;
char *disk_usage = safe_malloc(BUF_SIZE);
long total, used, free;
int percentage;
status = statvfs(folder, &file_stats);
halt_and_catch_fire("Error getting disk usage for %s", folder);
total = file_stats.f_blocks * file_stats.f_frsize;
free = file_stats.f_bfree * file_stats.f_frsize;
used = total - free;
percentage = (used / (double)total) * 100;
#define TO_GB(A) ((A) / (1024.0 * 1024 * 1024))
snprintf(disk_usage, BUF_SIZE, "%.1fGiB / %.1fGiB (%d%%)", TO_GB(used), TO_GB(total), percentage);
#undef TO_GB
return disk_usage;
}
static char *get_colors1(void *arg) {
(void) arg;
char *colors1 = safe_malloc(BUF_SIZE);
char *s = colors1;
for (int i = 0; i < 8; i++) {
sprintf(s, "\e[4%dm ", i);
s += 8;
}
snprintf(s, 5, "\e[0m");
return colors1;
}
static char *get_colors2(void *arg) {
(void) arg;
char *colors2 = safe_malloc(BUF_SIZE);
char *s = colors2;
for (int i = 8; i < 16; i++) {
sprintf(s, "\e[48;5;%dm ", i);
s += 12 + (i >= 10 ? 1 : 0);
}
snprintf(s, 5, "\e[0m");
return colors2;
}
static char *spacer(void *arg) {
(void) arg;
return safe_calloc(1, 1); // freeable, null-terminated string of length 1
}
static char *run_shell_cmd(void *arg) {
char *cmd = arg;
size_t output_len = 256;
char *output = safe_malloc(output_len + 1);
int pipefds[2];
safe_pipe(pipefds);
int pid = fork();
if (pid < 0)
halt_and_catch_fire("Unable to fork to run command '%s'", cmd);
if (pid == 0) {
close(pipefds[0]); //close read end of pipe
dup2(pipefds[1], STDOUT_FILENO); //Output goes through the pipe
int err = execl("/bin/sh", "/bin/sh", "-c", cmd, (char*) 0);
(void)err;
halt_and_catch_fire("Unable to run command '%s' with '/bin/sh -c'", cmd);
}
close(pipefds[1]); //close write end of pipe
size_t i;
for (i = 0; i < output_len;) {
int new_bytes = read(pipefds[0], output + i, output_len - i);
if (!new_bytes) //we reached the end of output from the shell
break;
i += new_bytes;
}
output[i] = 0;
remove_newline(output);
return output;
}
static char *get_date(void *arg) {
(void) arg;
time_t t = time(0);
struct tm *tm = localtime(&t);
char *retval = safe_malloc(72);
sprintf(retval, "%d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
return retval;
}
static char *get_env(void *arg) {
char *env = arg;
env = getenv(env);
if (!env)
return safe_calloc(1, 1);
char *retval = malloc(strlen(env) + 1);
strcpy(env, retval);
remove_newline(retval);
return retval;
}
char *gtk_settings_file_name() {
char *config_dir = getenv("XDG_CONFIG_HOME");
if (config_dir) {
return sallocf("%s/gtk-3.0/settings.ini", config_dir);
}
return sallocf("%s/.config/gtk-3.0/settings.ini", getenv("HOME"));
}
static char *get_gtk_option(void *arg) {
char *optname = arg;
char *conffile = gtk_settings_file_name();
FILE *f = fopen(conffile, "r");
char *line = 0;
size_t max = 0;
while (!feof(f)) {
getline(&line, &max, f);
size_t i;
for (i = 0; optname[i]; i++) {
if (!line[i])
break;
if (line[i] != optname[i])
break;
}
if (!optname[i] && line[i] == '=') {
remove_newline(line + i + 1);
char *retval = safe_strdup(line + i + 1);
free(line);
free(conffile);
fclose(f);
return retval;
}
}
free(line);
free(conffile);
fclose(f);
return safe_calloc(1, 1);
}