-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag.c
108 lines (95 loc) · 2.16 KB
/
tag.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
#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <sys/file.h>
#include <pwd.h>
#include "util.h"
#define KB 1024.0
#define GB (KB*KB)
static inline void
sec_to_format(unsigned int n, char *buf) {
sprintf(buf, "%uh %um %us", n/3600, n%3600/60, n%60);
}
const char *
_username(void)
{
char *s, *s2;
s = xmalloc(LOGIN_NAME_MAX + 1);
if (getlogin_r(s, LOGIN_NAME_MAX) != 0)
{
/* getlogin_r unsuccessful, get user from $USER */
if ((s2 = getenv("USER")))
strncpy(s, s2, LOGIN_NAME_MAX);
}
return s;
}
const char *
_hostname(void)
{
char *s, *s2;
s = xmalloc(HOST_NAME_MAX + 1);
if (gethostname(s, HOST_NAME_MAX) == -1)
{
int fd;
fd = open("/proc/sys/kernel/hostname", O_RDONLY);
read(fd, s, HOST_NAME_MAX);
close(fd);
if ((s2 = strchr(s, '\n')))
*s2 = 0;
}
return s;
}
const char *
_release(void)
{
struct utsname *ver;
ver = xmalloc(sizeof(struct utsname));
uname(ver);
return ver->release;
}
const char *
_shell(void)
{
return getenv("SHELL");
}
const char *
_uptime(void)
{
char *s;
struct sysinfo info;
s = xmalloc(200);
sysinfo(&info);
sec_to_format(info.uptime, s);
return s;
}
const char *
_memory(void)
{
/* struct sysinfo si;
-- sysinfo(&si);
-- snprintf(s, 200, "%.0f/%.0f MB", (float)(si.totalram-si.freeram)/GB, (float)si.totalram/GB);
** mem in sysinfo does not include cached mem, hence is inaccurate */
char *s;
uintmax_t free, total, buffer, cached;
FILE *pmif;
s = xmalloc(200);
pmif = fopen("/proc/meminfo", "r");
if (fscanf(pmif,
"MemTotal: %ju kB\n"
"MemFree: %ju kB\n"
"MemAvailable: %ju kB\n"
"Buffers: %ju kB\n"
"Cached: %ju kB\n",
&total, &free, &buffer, &buffer, &cached) != 5) {
return NULL;
}
fclose(pmif);
snprintf(s, 200, "%.1lf/%.1lf GB", (total - free - buffer - cached)/GB, total/GB);
return s;
}