-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
241 lines (195 loc) · 5.75 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
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
#include <stdio.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <stdlib.h>
#define COLOR CYAN
#define NAMECOLOR COLOR
#include "config.h"
#include <dirent.h>
#include "color.h"
struct utsname u;
struct sysinfo sys;
char info[ELEMENTS+1][150];
int i;
void os(char preprint[]) {
FILE *os = fopen("/etc/os-release","r");
char buffer[150];
fscanf(os, "NAME=\"%[^\"]+", buffer);
snprintf(info[i], 149, COLOR"%s "CLOSE"%s %s", preprint, buffer, u.machine);
fclose(os);
i += 1;
}
void host(char preprint[]) {
FILE *host = fopen("/sys/devices/virtual/dmi/id/product_name", "r");
char buffer[150];
fscanf(host, "%s", buffer);
snprintf( info[i], 149, COLOR"%s "CLOSE"%s", preprint, buffer);
fclose(host);
i += 1;
}
void hname() {
char h[50];
char buffer[100];
gethostname(h);
getlogin_r(buffer);
snprintf(info[i], 149, NAMECOLOR"%s"CLOSE"@"NAMECOLOR"%s"CLOSE, buffer, h);
snprintf(info[i+1], strlen(info[i]) +1 - 2*strlen(CLOSE) - 2*strlen(COLOR), "%s", "-----------------------------------------------------------------------------");
i += 2;
}
void kernel( char preprint[]) {
snprintf(info[i], 149, COLOR"%s "CLOSE"%s", preprint, u.release);
i += 1;
}
void get_up(char preprint[]) {
float mins = sys.uptime / 60;
if((int) (mins / 60) == 0) {
snprintf(info[i], 149, COLOR"%s "CLOSE"%d mins", preprint,(int)mins % 60);
} else {
snprintf(info[i], 149, COLOR"%s "CLOSE"%d hours, %d mins",preprint, (int)(mins / 60), (int)mins % 60);
}
i += 1;
}
void get_shell(char preprint[]) {
snprintf(info[i], 149, COLOR"%s "CLOSE"%s",preprint, strrchr(getenv("SHELL"), '/') + 1);
i += 1;
}
void spacing() {
strcpy(info[i],"");
i += 1;
}
void get_term(char preprint[]) {
snprintf(info[i], 149, COLOR"%s "CLOSE"%s",preprint, getenv("TERM"));
i += 1;
}
void get_cpu(char preprint[]) {
FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); /* read from cpu info */
char *cpu_model = malloc(75);
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 */
while (getline(&line, &len, cpufreq) != -1) {
if (sscanf(line, "cpu MHz : %lf", &freq) > 0) break;
}
cpu_freq = (int) freq;
}
free(line);
fclose(cpufreq);
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
snprintf(info[i], 150, COLOR"%s "CLOSE"%s (%d) @ %.*f%s", preprint,cpu_model, num_cores, prec, freq, freq_unit);
free(cpu_model);
i += 1;
}
void get_memory(char preprint[]) {
FILE *mem = fopen("/proc/meminfo","r");
char *line = NULL;
size_t len;
int total_memory, used_memory, total, shared, memfree, buffers, cached, reclaimable;
while(getline(&line, &len, mem) != -1) {
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(mem);
used_memory = (total + shared - memfree - buffers - cached - reclaimable) / 1024;
total_memory = total / 1024;
int percentage = (int) (100 * (used_memory / (double) total_memory));
snprintf( info[i], 149, COLOR"%s "CLOSE"%dMiB / %dMiB (%d%%)", preprint, used_memory, total_memory, percentage);
i += 1;
}
void get_colors() {
char *s = info[i];
for(int i = 0; i < 8; i++) {
sprintf(s, "\e[4%dm ", i);
s += 8;
}
snprintf(s, 5, "\e[0m");
i += 1;
}
void get_colors2() {
char *s = info[i];
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");
i += 1;
}
void get_packages(char preprint[]) {
int num_packages = 0;
DIR * dirp;
struct dirent *entry;
dirp = opendir(PACDIR);
if(dirp == NULL) {
printf("dir doesn't exist");
} else {
while((entry = readdir(dirp)) != NULL) {
if(entry->d_type == DT_DIR) num_packages++;
}
num_packages -= 2; // accounting for . and ..
closedir(dirp);
snprintf(info[i], 150, COLOR"%s "CLOSE"%d",preprint, num_packages);
i += 1;
}
}
void get_wm(char preprint[]) {
snprintf(info[i], 150, COLOR"%s "CLOSE"%s", preprint,getenv("XDG_CURRENT_DESKTOP"));
i += 1;
}
void customPrint(char preprint[], char message[]) {
snprintf(info[i], 150, COLOR"%s "CLOSE"%s", preprint, message);
i += 1;
}
int main() {
uname(&u);
sysinfo(&sys);
order();
int j = 0;
for(; j < ROWS && j < i; j++) {
printf(COLOR "%s " CLOSE , logo[j]);
printf("%s\n", info[j]);
}
if(ROWS == i) {
} else if( ROWS < i) {
for(; j < i; j++) {
printf("%s ", logo[ROWS]);
printf("%s\n", info[j]);
} }
else {
for(; j < ROWS; j++) {
printf(COLOR "%s " CLOSE , logo[j]);
printf("%s\n", info[i]);
}}
printf("\n");
return 0;
}