suwako / farfetch アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
farfetch / src / storage.c
3.6 KB、92行 生 Blame 履歴
1#include "storage.h"
2#include "common.h"
3
4#if defined(__HAIKU__)
5#include <stdio.h>
6#endif
7#include <string.h>
8#include <stdlib.h>
9
10const char *display_storage() {
11 const char *excode = run_command_s("zpool list 2>/dev/null || echo $?");
12 if (excode != NULL && (strncmp(excode, "127", strlen("127")) == 0)) {
13 free((void *)excode);
14#if defined(__HAIKU__)
15 long long int usedstore = 0;
16 long long int totalstore = run_command_lld("df -h | "
17 "awk '/^\\/boot / {printf \"%s\", $3}'");
18 long long int freestore = run_command_lld("df -h | "
19 "awk '/^\\/boot / {printf \"%s\", $5}'");
20 const char *totunit = run_command_s("df -h | "
21 "awk '/^\\/boot / {printf \"%s\", $4}'");
22 const char *freunit = run_command_s("df -h | "
23 "awk '/^\\/boot / {printf \"%s\", $6}'");
24 char tu[2] = {'\0'};
25 char uu[2] = {'\0'};
26
27 if (strncmp(totunit, "TiB", 3) == 0) tu[0] = 'T';
28 else if (strncmp(totunit, "GiB", 3) == 0) tu[0] = 'G';
29 else if (strncmp(totunit, "MiB", 3) == 0) tu[0] = 'M';
30 else if (strncmp(totunit, "KiB", 3) == 0) tu[0] = 'K';
31 else tu[0] = 'B';
32 tu[1] = '\0';
33
34 if (strncmp(freunit, "TiB", 3) == 0) uu[0] = 'T';
35 else if (strncmp(freunit, "GiB", 3) == 0) uu[0] = 'G';
36 else if (strncmp(freunit, "MiB", 3) == 0) uu[0] = 'M';
37 else if (strncmp(freunit, "KiB", 3) == 0) uu[0] = 'K';
38 else uu[0] = 'B';
39 uu[1] = '\0';
40
41 if (strncmp(totunit, freunit, 3) != 0) {
42 if (strncmp(totunit, "TiB", 3) == 0 && strncmp(freunit, "GiB", 3) == 0)
43 usedstore = (totalstore - (freestore / 1024));
44 else if (strncmp(totunit, "GiB", 3) == 0 && strncmp(freunit, "MiB", 3) == 0)
45 usedstore = (totalstore - (freestore / 1024));
46 else if (strncmp(totunit, "MiB", 3) == 0 && strncmp(freunit, "KiB", 3) == 0)
47 usedstore = (totalstore - (freestore / 1024));
48 else if (strncmp(totunit, "KiB", 3) == 0 && strncmp(freunit, "B", 3) == 0)
49 usedstore = (totalstore - (freestore / 1024));
50 else if (strncmp(totunit, "TiB", 3) == 0 && strncmp(freunit, "MiB", 3) == 0)
51 usedstore = (totalstore - (freestore / 1024 / 1024));
52 else if (strncmp(totunit, "GiB", 3) == 0 && strncmp(freunit, "KiB", 3) == 0)
53 usedstore = (totalstore - (freestore / 1024 / 1024));
54 else if (strncmp(totunit, "MiB", 3) == 0 && strncmp(freunit, "B", 3) == 0)
55 usedstore = (totalstore - (freestore / 1024 / 1024));
56 else if (strncmp(totunit, "TiB", 3) == 0 && strncmp(freunit, "KiB", 3) == 0)
57 usedstore = (totalstore - (freestore / 1024 / 1024 / 1024));
58 else if (strncmp(totunit, "GiB", 3) == 0 && strncmp(freunit, "B", 3) == 0)
59 usedstore = (totalstore - (freestore / 1024 / 1024 / 1024));
60 else if (strncmp(totunit, "TiB", 3) == 0 && strncmp(freunit, "B", 3) == 0)
61 usedstore = (totalstore - (freestore / 1024 / 1024 / 1024 / 1024));
62 } else {
63 usedstore = (totalstore - freestore);
64 }
65
66 char *cmd = (char *)malloc(128 * sizeof(char));
67 if (!cmd) {
68 return NULL;
69 }
70
71 snprintf(cmd, 128, "/boot: %lld%s / %lld%s", usedstore, uu, totalstore, tu);
72
73 return cmd;
74#else
75 return run_command_s("df -h | "
76 "awk '/^\\/dev\\// {printf \"%s: %s / %s, \", $1, $3, $2}' | "
77 "awk '{sub(/, $/, \"\"); print}'");
78#endif
79 }
80
81 free((void *)excode);
82 const char *test = run_command_s("zpool list 2>/dev/null || echo $?");
83 if (strncmp(test, "1", strlen("1")) == 0) {
84 free((void *)test);
85 return NULL;
86 }
87
88 free((void *)test);
89 return run_command_s("zpool list | "
90 "awk 'NR>1 {printf \"%s: %s / %s, \", $1, $3, $2}' | "
91 "awk '{sub(/, $/, \"\"); print}'");
92}