-
Notifications
You must be signed in to change notification settings - Fork 6
/
bsdinfo.h
166 lines (143 loc) · 4.45 KB
/
bsdinfo.h
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
/*-
* Copyright (c) 2012, Jakub Szafrański <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#define O_RDONLY 0x0000
#define _POSIX2_LINE_MAX 2048
#define _PATH_DEVNULL "/dev/null"
int psysctl(char *name)
{
char buf[256];
size_t size = sizeof(buf);
if (sysctlbyname(name, &buf, &size, NULL, 0) != 0) {
return (1);
}
printf("%s", buf);
return (0);
}
int printval(char *val, char *name)
{
printf("\033[1;31m%s:\033[0;0m ", val);
psysctl(name);
}
int printuptime()
{
struct timespec tp;
time_t uptime;
int days, hrs, i, mins, secs;
char buf[256];
if (clock_gettime(CLOCK_UPTIME, &tp) != -1) {
uptime = tp.tv_sec;
if (uptime > 60)
uptime += 30;
days = uptime / 86400;
uptime %= 86400;
hrs = uptime / 3600;
uptime %= 3600;
mins = uptime / 60;
secs = uptime % 60;
(void)printf("\033[1;31mUptime: \033[0;0m");
if (days > 0)
(void)printf("%d day%s ", days, days > 1 ? "s" : "");
if (hrs > 0 && mins > 0)
(void)printf("%d:%02d", hrs, mins);
else if (hrs == 0 && mins > 0)
(void)printf("0:%02d", mins);
else
(void)printf("0:00");
}
}
int printshell()
{
char* shell = getenv("SHELL");
if (shell != NULL) {
int slen = strlen(shell);
for (slen; slen > 0; --slen) if (shell[slen] == '/') break;
slen = slen+1;
while(shell[slen] != '\0') {
printf("%c", shell[slen]);
++slen;
}
if (slen == 1) printf("%s", shell);
}
return (0);
}
int printmem()
{
printf("\033[1;31mRAM:\033[0;0m ");
long int ram_free = 0;
size_t size_ram_free;
size_ram_free = sizeof(ram_free);
sysctlbyname("vm.stats.vm.v_free_count", &ram_free, &size_ram_free, NULL, 0);
long int ram_pagesize = 0;
size_t size_ram_pagesize;
size_ram_pagesize = sizeof(ram_pagesize);
sysctlbyname("hw.pagesize", &ram_pagesize, &size_ram_pagesize, NULL, 0);
ram_free = ram_free*ram_pagesize;
ram_free = ram_free/1024/1024;
long int ram_size = 0;
size_t size_ram_size;
size_ram_size = sizeof(ram_size);
sysctlbyname("hw.physmem", &ram_size, &size_ram_size, NULL, 0);
ram_size = ram_size/1024/1024;
ram_free = ram_size - ram_free;
printf("%dM / ", ram_free);
printf("%dM", ram_size);
return (0);
}
int printrmws(char *str) {
short int space = 0;
int i = 0;
int s = strlen(str);
for (i; i < s; ++i) {
if (str[i] != ' ') space = 0;
if (space == 0) printf("%c", str[i]);
if (str[i] == ' ') space = 1;
else space = 0;
}
return (0);
}
int printcpu()
{
printf("\033[1;31mCPU:\033[0;0m ");
char buf[256];
size_t size = sizeof(buf);
if (sysctlbyname("hw.model", &buf, &size, NULL, 0) != 0) {
return (1);
}
printrmws(buf);
return (0);
}
int printbootmethod()
{
printf("\033[1;31mBootmethod:\033[0;0m ");
char buf[256];
size_t size = sizeof(buf);
if (sysctlbyname("machdep.bootmethod", &buf, &size, NULL, 0) != 0) {
return (1);
}
printrmws(buf);
return (0);
}