forked from yellowman/nsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.c
122 lines (117 loc) · 3.19 KB
/
version.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
/*
* Copyright (c) 2002 Chris Cappuccio <[email protected]>
*
* 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 <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include "externs.h"
int
version(int argc, char **argv)
{
char cpubuf[1024];
char kernver[1024];
struct timeval tv, boottime;
struct utsname un;
size_t len;
time_t c;
uint64_t physmem;
int mib[5], pntd, weeks, days, hours, mins;
mib[0] = CTL_HW;
mib[1] = HW_PHYSMEM64;
len = sizeof(physmem);
if (sysctl(mib, 2, &physmem, &len, NULL, 0) == -1) {
printf("%% HW_PHYSMEM: %s\n", strerror(errno));
return(1);
}
mib[0] = CTL_HW;
mib[1] = HW_MODEL;
len = sizeof(cpubuf);
if (sysctl(mib, 2, &cpubuf, &len, NULL, 0) == -1) {
printf("%% HW_MODEL: %s\n", strerror(errno));
return(1);
}
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
len = sizeof(boottime);
if (sysctl(mib, 2, &boottime, &len, NULL, 0) == -1) {
printf("%% KERN_BOOTTIME: %s\n", strerror(errno));
return(1);
}
mib[0] = CTL_KERN;
mib[1] = KERN_VERSION;
len = sizeof(kernver);
if (sysctl(mib, 2, &kernver, &len, NULL, 0) == -1) {
printf("%% KERN_VERSION: %s\n", strerror(errno));
return(1);
}
mib[0] = CTL_NET;
mib[1] = PF_INET;
mib[2] = IPPROTO_IP;
mib[3] = IPCTL_IFQUEUE;
mib[4] = IFQCTL_DROPS;
if (uname(&un)) {
printf("%% uname: %s\n", strerror(errno));
return(1);
}
gettimeofday(&tv, (struct timezone *)0);
c = difftime(tv.tv_sec, boottime.tv_sec);
printf("%% NSH v%s\n", vers);
printf("Compiled %s by %s@%s\n", compiled, compiledby, compilehost);
printf("uptime: ");
pntd = 0;
weeks = c / (7 * 24 * 60 * 60);
c %= (7 * 24 * 60 * 60);
days = c / (24 * 60 * 60);
c %= (24 * 60 * 60);
hours = c / (60 * 60);
c %= (60 * 60);
mins = c / 60;
c %= 60;
if (weeks) {
printf("%d week%s", weeks, weeks == 1 ? "" : "s");
pntd = 1;
}
if (days) {
printf("%s%d day%s", pntd ? ", " : "", days,
days == 1 ? "" : "s");
pntd = 1;
}
if (hours) {
printf("%s%d hour%s", pntd ? ", " : "", hours,
hours == 1 ? "" : "s");
pntd = 1;
}
if (mins) {
printf("%s%d minute%s", pntd ? ", " : "", mins,
mins == 1 ? "" : "s");
pntd = 1;
}
if (!pntd)
printf("%d second%s", (int)c, c == 1 ? "" : "s");
printf("\n");
printf("system: %s/%s version %s\n", un.sysname, un.machine,
un.release);
printf("cpu: %s\n", cpubuf);
printf("memory: %sB\n", format_k(physmem / 1024));
printf("kernel: %s", kernver);
return(0);
}