-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbsdfetch.c
More file actions
303 lines (255 loc) · 7.11 KB
/
bsdfetch.c
File metadata and controls
303 lines (255 loc) · 7.11 KB
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
/*
* Copyright (c) 2022 - 2023 jhx <jhx0x00@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>
#ifdef __OpenBSD__
#include <sys/time.h>
#include <sys/sensors.h>
#include "sysctlbyname.h"
#endif
#include <err.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define VERSION "1.1.1"
#define RED "\033[1;31m" /* Bright red */
#define GREEN "\033[1;32m" /* Bright green */
#define CEND "\033[0m" /* Reset color */
static char buf[BUFSIZ]; /* buffer large enough for everything */
static int color_flag; /* 1 => color; 0 => no color in labels */
/**
* Print field `fld' in colour `clr' followed by formatted string.
*/
static void cpr(char* fld, char* clr, char* fmt, ...) {
va_list ap;
if (color_flag)
printf("%s%s%s: ", clr, fld, CEND);
else
printf("%s: ", fld);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
/**
* Squeeze multiple adjacent blank chars. into a single space.
*/
static void sqz(char* s) {
for (char* p = s; *p; p++) {
size_t n;
if ((n = strspn(p, " \t")) > 0) {
*p = ' ';
memmove(p + 1, p + n, strlen(p + n) + 1);
}
}
}
static void get_shell(void) {
struct passwd* pw;
char* sh, *p;
if ((sh = getenv("SHELL")) == NULL || *sh == '\0') {
if ((pw = getpwuid(getuid())) == NULL)
err(1, "getpwuid() failed");
sh = pw->pw_shell;
}
if ((p = strrchr(sh, '/')) != NULL && *(p+1) != '\0')
sh = ++p;
cpr("Shell", RED, sh);
}
static void get_user(void) {
struct passwd* pw;
char* p;
if ((p = getenv("USER")) == NULL || *p == '\0') {
if ((pw = getpwuid(getuid())) == NULL)
err(1, "getpwuid() failed");
p = pw->pw_name;
}
cpr("User", RED, p);
}
static void get_cpu(void) {
long ncpu, nmax;
size_t sz;
if ((ncpu = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
err(1, "sysconf(_SC_NPROCESSORS_ONLN) failed");
if ((nmax = sysconf(_SC_NPROCESSORS_CONF)) == -1)
err(1, "sysconf(_SC_NPROCESSORS_CONF) failed");
sz = sizeof buf;
if (sysctlbyname("machdep.cpu_brand", buf, &sz, NULL, 0) == -1)
if (sysctlbyname("hw.model", buf, &sz, NULL, 0) == -1)
err(1, "error getting CPU info.");
buf[sz] = '\0';
sqz(buf); /* NetBSD needs this */
cpr("CPU", RED, buf);
cpr("Cores", RED, "%d of %d processors online", ncpu, nmax);
#if defined(__FreeBSD__) || defined(__MidnightBSD__) || defined(__DragonFly__)
#define CELSIUS 273.15
for (int i = 0; i < (int)ncpu; i++) {
int temp = 0;
sz = sizeof temp;
snprintf(buf, sizeof buf, "dev.cpu.%d.temperature", i);
if (sysctlbyname(buf, &temp, &sz, NULL, 0) == -1)
return;
if (color_flag)
printf(GREEN " -> " CEND);
else
printf(" -> ");
snprintf(buf, sizeof buf, "Core [%d]", i + 1);
cpr(buf, RED, "%.1f °C", (temp * 0.1) - CELSIUS);
}
#elif defined(__OpenBSD__)
struct sensor sensors;
int mib[5];
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
mib[2] = 0;
mib[3] = SENSOR_TEMP;
mib[4] = 0;
sz = sizeof sensors;
if (sysctl(mib, 5, &sensors, &sz, NULL, 0) == -1)
return;
cpr("CPU Temp", RED, "%d °C", (int)((float)(sensors.value - 273150000) / 1E6));
#elif defined(__NetBSD__)
const char* const cmd = "/usr/sbin/envstat |"
" awk '/ cpu[0-9]+ temperature: / { print $3 }'";
FILE* f = popen(cmd, "r");
if (f == NULL)
err(1, "popen(%s) failed", cmd);
int i = 0;
while (fgets(buf, sizeof buf, f) != NULL) {
float temp;
if (sscanf(buf, "%f", &temp) != 1)
break;
if (color_flag)
printf(GREEN " -> " CEND);
else
printf(" -> ");
snprintf(buf, sizeof buf, "Core [%d]", ++i);
cpr(buf, RED, "%.1f °C", temp);
}
if (pclose(f) != 0)
err(1, "pclose(%s) failed", cmd);
#endif
}
static void get_loadavg(void) {
double lavg[3] = { 0.0 };
if (getloadavg(lavg, 3) != 3)
err(1, "getloadavg() failed");
cpr("Loadavg", RED, "%.2lf %.2lf %.2lf", lavg[0], lavg[1], lavg[2]);
}
static void get_packages(void) {
#if defined (__OpenBSD__) || defined (__NetBSD__)
const char* const cmd = "/usr/sbin/pkg_info";
#elif defined (__DragonFly__) || defined (__FreeBSD__)
const char* const cmd = "/usr/sbin/pkg info";
#elif defined (__MidnightBSD__)
const char* const cmd = "/usr/sbin/mport list";
#else
#error Unsupported BSD variant
#endif
FILE *f = popen(cmd, "r");
if (f == NULL)
err(1, "popen(%s) failed", cmd);
/* No. of packages == simple line count */
size_t npkg = 0;
while (fgets(buf, sizeof buf, f) != NULL)
if (strchr(buf, '\n') != NULL)
npkg++;
if (pclose(f) != 0)
err(1, "pclose(%s) failed", cmd);
cpr("Packages", RED, "%zu", npkg);
}
static void get_uptime(void) {
long up, days, hours, mins;
struct timeval t;
size_t tsz = sizeof t;
if (sysctlbyname("kern.boottime", &t, &tsz, NULL, 0) == -1)
err(1, "failed to get kern.boottime");
up = (long)(time(NULL) - t.tv_sec + 30);
days = up / 86400;
up %= 86400;
hours = up / 3600;
up %= 3600;
mins = up / 60;
cpr("Uptime", RED, "%dd %dh %dm", days, hours, mins);
}
static void get_memory(void) {
unsigned long long ramsz;
long pagesz, npages;
if ((pagesz = sysconf(_SC_PAGESIZE)) == -1)
err(1, "error getting system page-size");
if ((npages = sysconf(_SC_PHYS_PAGES)) == -1)
err(1, "error getting no. of system pages");
ramsz = (unsigned long long)(pagesz * npages) / (1024*1024);
cpr("RAM", RED, "%llu MB", ramsz);
}
static void get_hostname(void) {
if (gethostname(buf, sizeof buf) == -1)
err(1, "gethostname() failed");
cpr("Host", RED, "%s", buf);
}
static void get_sysinfo(void) {
struct utsname un;
char* p;
if (uname(&un))
err(1, "uname() failed");
cpr("OS", RED, un.sysname);
cpr("Release", RED, un.release);
if ((p = strchr(un.version, ':')) != NULL)
*p = '\0'; /* NetBSD: lop-off build-strings */
cpr("Version", RED, un.version);
cpr("Arch", RED, "%s", un.machine);
}
_Noreturn static void version(void) {
printf("%s - version %s (%s)\n",
getprogname(),
VERSION,
__DATE__
);
exit(EXIT_SUCCESS);
}
_Noreturn static void usage(void) {
printf( "USAGE: %s [-h|-n|-v]\n"
" -h Show this help text\n"
" -n Turn off colors\n"
" -v Show version\n",
getprogname());
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv) {
color_flag = isatty(1);
if (argc == 2) {
if (strcmp(argv[1], "-h") == 0)
usage();
else if (strcmp(argv[1], "-n") == 0)
color_flag = 0;
else if (strcmp(argv[1], "-v") == 0)
version();
}
get_sysinfo();
get_hostname();
get_shell();
get_user();
get_packages();
get_uptime();
get_memory();
get_loadavg();
get_cpu();
return EXIT_SUCCESS;
}