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
|
From ede695c97af5565a1257b2fc204fb97f2aead55e Mon Sep 17 00:00:00 2001
From: Jari Aalto <jari.aalto@cante.net>
Date: Sat, 19 Dec 2009 19:59:11 +0200
Subject: [PATCH] arch/GNU/: add GNU/Hurd compile files
Forwarded: https://github.com/lindes/ttyload/pull/6
Signed-off-by: Jari Aalto <jari.aalto@cante.net>
---
arch/GNU/getload.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++
arch/GNU/terminfo.c | 28 ++++++++++++++++++++++
2 files changed, 92 insertions(+)
create mode 100644 arch/GNU/getload.c
create mode 100644 arch/GNU/terminfo.c
--- /dev/null
+++ b/arch/GNU/getload.c
@@ -0,0 +1,64 @@
+#define LOADFILE "/proc/loadavg"
+
+/* What the contents of the proc file look like, as an example:
+ * 0.00 0.00 0.00 1/59 10941
+ */
+
+#include "ttyload.h"
+
+#include <stdio.h> /* for perror */
+#include <stdlib.h> /* for exit() */
+#include <unistd.h> /* for sleep() */
+
+void getload(load_list *loadavgs)
+{
+ float theload[3];
+ FILE *loadfile;
+ int ret;
+
+ /* just for sanity: */
+ if(!loadavgs)
+ {
+ fprintf(stderr, "getload called without loadavgs pointer.\n");
+ exit(2);
+ }
+
+ if(!(loadfile = fopen(LOADFILE, "r")))
+ {
+ perror("Open of " LOADFILE " failed");
+ exit(1);
+ }
+
+ ret = fscanf(loadfile, "%f %f %f ", &(theload[0]), &(theload[1]), &(theload[2]));
+
+ if(fclose(loadfile) != 0)
+ {
+ perror("Close of " LOADFILE " failed?!");
+ exit(1);
+ }
+
+ /* so the caller _can_ (maybe not _will_) know how we did */
+ loadavgs->numloads = ret;
+ loadavgs->one_minute = theload[0] * 1024;
+ loadavgs->five_minute = theload[1] * 1024;
+ loadavgs->fifteen_minute = theload[2] * 1024;
+
+ /* a little warning thing... should probably be re-written
+ * for the Linux/fscanf stuff, this is a hold-over from the
+ * solaris stuff. */
+ switch(ret)
+ {
+ case 2:
+ fprintf(stderr, "15 minute Load average is unreliable.\n");
+ sleep(5);
+ break;
+ case 1:
+ fprintf(stderr, "5 and 15 minute load averages are unreliable.\n");
+ sleep(5);
+ break;
+ case 0:
+ fprintf(stderr, "Sorry, couldn't get any load "
+ "averages. This is, therefore, pointless.\n");
+ exit(1);
+ }
+}
--- /dev/null
+++ b/arch/GNU/terminfo.c
@@ -0,0 +1,28 @@
+/*
+ * arch/Linux/terminfo.c -- routines for getting terminal
+ * information on Linux machines.
+ *
+ * Copyright 2001, David Lindes. All rights reserved.
+ */
+
+#include <sys/ioctl.h> /* for ioctl() */
+
+/* globals */
+extern int rows;
+extern int cols;
+
+void gettermsize()
+{
+ struct winsize info;
+
+ /* try to get data via IOCTL: */
+ if (ioctl(1 /* stdout */, TIOCGWINSZ, &info) != -1)
+ {
+ /* if successful, and the data seems sane, set the
+ * program's globals: */
+ if(info.ws_col > 0)
+ cols = info.ws_col;
+ if(info.ws_row > 0)
+ rows = info.ws_row;
+ }
+}
|