summaryrefslogtreecommitdiffstats
path: root/mbbsd/osdep.c
diff options
context:
space:
mode:
authorin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-07 23:13:44 +0800
committerin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-07 23:13:44 +0800
commitae31e19f92e717919ac8e3db9039eb38d2b89aae (patch)
treec70164d6a1852344f44b04a653ae2815043512af /mbbsd/osdep.c
downloadpttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.gz
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.bz2
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.lz
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.xz
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.zst
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.zip
Initial revision
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@1 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd/osdep.c')
-rw-r--r--mbbsd/osdep.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/mbbsd/osdep.c b/mbbsd/osdep.c
new file mode 100644
index 00000000..967a4db0
--- /dev/null
+++ b/mbbsd/osdep.c
@@ -0,0 +1,79 @@
+/* $Id: osdep.c,v 1.1 2002/03/07 15:13:48 in2 Exp $ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#if defined(linux)
+int cpuload(char *str) {
+ double l[3] = {-1, -1, -1};
+ FILE *fp;
+
+ if((fp = fopen("/proc/loadavg", "r"))) {
+ if(fscanf(fp, "%lf %lf %lf", &l[0], &l[1], &l[2]) != 3)
+ l[0] = -1;
+ fclose(fp);
+ }
+ if(str) {
+ if(l[0] != -1)
+ sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
+ else
+ strcpy(str, " (unknown) ");
+ }
+ return (int)l[0];
+}
+
+double swapused(long *total, long *used) {
+ double percent = -1;
+ char buf[101];
+ FILE *fp;
+
+ if((fp = fopen("/proc/meminfo","r"))) {
+ while(fgets(buf, 100, fp) && buf[0] != 'S');
+ if(sscanf(buf + 6, "%ld %ld", total, used) == 2)
+ if(*total != 0)
+ percent = (double)*used / (double)*total;
+ fclose(fp);
+ }
+ return percent;
+}
+
+#elif __FreeBSD__ >=4
+
+#include <kvm.h>
+
+int cpuload(char *str) {
+ double l[3] = {-1, -1, -1};
+ if(getloadavg(l, 3) != 3)
+ l[0] = -1;
+
+ if(str) {
+ if(l[0] != -1)
+ sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
+ else
+ strcpy(str, " (unknown) ");
+ }
+ return (int)l[0];
+}
+
+double swapused(long *total, long *used) {
+ double percent = -1;
+ kvm_t *kd;
+ struct kvm_swap swapinfo;
+ int pagesize;
+
+ kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
+ if(kd) {
+ if(kvm_getswapinfo(kd, &swapinfo, 1, 0) == 0) {
+ pagesize = getpagesize();
+ *total = swapinfo.ksw_total * pagesize;
+ *used = swapinfo.ksw_used * pagesize;
+ if(*total != 0)
+ percent = (double)*used / (double)*total;
+ }
+ kvm_close(kd);
+ }
+ return percent;
+}
+#endif