From 9e5fa7ae04221ef4d52a3eab51e2a5060ab7ebef Mon Sep 17 00:00:00 2001 From: marschap Date: Sun, 22 May 2005 15:28:04 +0000 Subject: [PATCH] extracted utility functions into util.c, use utility functions where possible, add sample output to each screen function --- clients/lcdproc/Makefile.am | 2 +- clients/lcdproc/batt.c | 7 +- clients/lcdproc/chrono.c | 31 ++++---- clients/lcdproc/cpu.c | 16 +---- clients/lcdproc/disk.c | 53 +++++--------- clients/lcdproc/load.c | 18 +++-- clients/lcdproc/mem.c | 137 ++++++++++++++++-------------------- clients/lcdproc/util.c | 75 ++++++++++++++++++++ clients/lcdproc/util.h | 45 ++++++++++++ 9 files changed, 229 insertions(+), 155 deletions(-) create mode 100644 clients/lcdproc/util.c create mode 100644 clients/lcdproc/util.h diff --git a/clients/lcdproc/Makefile.am b/clients/lcdproc/Makefile.am index c3fbf27..6bf49a9 100644 --- a/clients/lcdproc/Makefile.am +++ b/clients/lcdproc/Makefile.am @@ -1,4 +1,4 @@ bin_PROGRAMS = lcdproc -lcdproc_SOURCES = main.c main.h mode.c mode.h batt.c batt.h chrono.c chrono.h cpu.c cpu.h cpu_smp.c cpu_smp.h disk.c disk.h load.c load.h mem.c mem.h machine.h machine_Linux.c machine_OpenBSD.c machine_FreeBSD.c machine_NetBSD.c machine_SunOS.c +lcdproc_SOURCES = main.c main.h mode.c mode.h batt.c batt.h chrono.c chrono.h cpu.c cpu.h cpu_smp.c cpu_smp.h disk.c disk.h load.c load.h mem.c mem.h machine.h machine_Linux.c machine_OpenBSD.c machine_FreeBSD.c machine_NetBSD.c machine_SunOS.c util.c util.h lcdproc_LDADD = @ldap_libs@ ../../shared/libLCDstuff.a INCLUDES = -I$(top_srcdir) diff --git a/clients/lcdproc/batt.c b/clients/lcdproc/batt.c index e646dc0..8dba98d 100644 --- a/clients/lcdproc/batt.c +++ b/clients/lcdproc/batt.c @@ -91,6 +91,7 @@ battery_screen (int rep, int display) sock_send_string (sock, "widget_add B two string\n"); sock_send_string (sock, "widget_add B three string\n"); sock_send_string (sock, "widget_add B gauge hbar\n"); + sock_send_string (sock, "widget_set B one 1 2 {AC: Unknown}\n"); sock_send_string (sock, "widget_set B two 1 3 {Batt: Unknown}\n"); sprintf(buffer, "widget_set B three 1 4 {E%*sF}\n", gauge_wid, ""); @@ -129,10 +130,8 @@ battery_screen (int rep, int display) } } else { // two-line version of the screen - if (acstat == LCDP_AC_ON) - sprintf(buffer, "widget_set B one 1 2 {AC, Batt: %s}\n", battery_status(battstat)); - else - sprintf(buffer, "widget_set B one 1 2 {Batt: %s}\n", battery_status(battstat)); + sprintf(buffer, "widget_set B one 1 2 {%sBatt: %s}\n", + (acstat == LCDP_AC_ON) ? "AC, " : "", battery_status(battstat)); if (display) sock_send_string(sock, buffer); } diff --git a/clients/lcdproc/chrono.c b/clients/lcdproc/chrono.c index e4ebbda..05e01c3 100644 --- a/clients/lcdproc/chrono.c +++ b/clients/lcdproc/chrono.c @@ -32,6 +32,7 @@ #include "machine.h" #include "chrono.h" + static int TwentyFourHour = 1; // A couple of tables for later use... @@ -120,10 +121,12 @@ time_screen (int rep, int display) sock_send_string (sock, "widget_add T two string\n"); sock_send_string (sock, "widget_add T three string\n"); + // write title bar: OS name, OS version, hostname sprintf (buffer, "widget_set T title {%s %s: %s}\n", get_sysname(), get_sysrelease(), get_hostname()); sock_send_string (sock, buffer); } else { + // write title bar: hostname sprintf (buffer, "widget_set T title {TIME: %s}\n", get_hostname()); sock_send_string (sock, buffer); } @@ -144,15 +147,13 @@ time_screen (int rep, int display) (lcd_wid > 20) ? 2 : 1, (rtime->tm_hour >= 12) ? "pm" : "am"); } - ///////////////////// Write the title bar (os name and version) - if (lcd_hgt >= 4) { strcpy (day, shortdays[rtime->tm_wday]); strcpy (month, shortmonths[rtime->tm_mon]); machine_get_uptime(&uptime, &idle); - /////////////////////// Display the uptime... + // display the uptime... days = (int) uptime / 86400; hour = ((int) uptime % 86400) / 60 / 60; min = (((int) uptime % 86400) % 3600) / 60; @@ -169,7 +170,7 @@ time_screen (int rep, int display) if (display) sock_send_string (sock, buffer); - ////////// display the date + // display the date sprintf (tmp, "%s %s %d, %d", day, month, rtime->tm_mday, rtime->tm_year + 1900); xoffs = (lcd_wid > strlen(tmp)) ? ((lcd_wid - strlen(tmp)) / 2) + 1 : 1; @@ -177,7 +178,7 @@ time_screen (int rep, int display) if (display) sock_send_string (sock, buffer); - /////////////////////// Display the time & idle time... + // display the time & idle time... sprintf (tmp, "%s %3i%% idle", now, (int) idle); xoffs = (lcd_wid > strlen(tmp)) ? ((lcd_wid - strlen(tmp)) / 2) + 1 : 1; sprintf (buffer, "widget_set T three %i 4 {%s}\n", xoffs, tmp); @@ -262,8 +263,7 @@ clock_screen (int rep, int display) (lcd_wid > 20) ? 2 : 1, (rtime->tm_hour >= 12) ? "pm" : "am"); } - if (lcd_hgt >= 4) // 4-line version of the screen - { + if (lcd_hgt >= 4) { // 4-line version of the screen strcpy (day, days[rtime->tm_wday]); strcpy (month, months[rtime->tm_mon]); @@ -279,8 +279,7 @@ clock_screen (int rep, int display) if (display) sock_send_string (sock, buffer); } - else // 2-line version of the screen - { + else { // 2-line version of the screen if (lcd_wid >= 20) // 20+x columns sprintf (tmp, "%d-%02d-%02d %s", rtime->tm_year + 1900, rtime->tm_mon + 1, rtime->tm_mday, now); @@ -376,14 +375,12 @@ uptime_screen (int rep, int display) ////////////////////////////////////////////////////////////////////// // Big Clock Screen displays current time... // -//Curses display is ugly, but should look nice on Matrix Orbital. -// -//+--------------------+ -//|111555 444222 111333| -//|111555 444222 111333| -//|111555 444222 111333| -//|111555 444222 111333| -//+--------------------+ +// +--------------------+ +// | _ _ _ _ | +// | ||_ _||_| _| || +// | ||_| _| | |_ || +// | | +// +--------------------+ // int big_clock_screen (int rep, int display) diff --git a/clients/lcdproc/cpu.c b/clients/lcdproc/cpu.c index 067efce..e35ff19 100644 --- a/clients/lcdproc/cpu.c +++ b/clients/lcdproc/cpu.c @@ -15,21 +15,7 @@ #include "mode.h" #include "machine.h" #include "cpu.h" - - -/** print a percentage value to a given string */ -static char * -sprintf_percent(char *str, double percent) -{ - if (str == NULL) - return NULL; - if (percent > 99.9) - strcpy(str, "100%"); - else - sprintf(str, "%.1f%%", (percent >= 0) ? percent : 0); - - return str; -} +#include "util.h" ////////////////////////////////////////////////////////////////////////// diff --git a/clients/lcdproc/disk.c b/clients/lcdproc/disk.c index b4da894..3b49f5d 100644 --- a/clients/lcdproc/disk.c +++ b/clients/lcdproc/disk.c @@ -16,15 +16,21 @@ #include "mode.h" #include "machine.h" #include "disk.h" +#include "util.h" + /////////////////////////////////////////////////////////////////////////// // Gives disk stats. // -// Stays onscreen until it is done. +// Stays onscreen until it is done; rolls over all mounted file systems +// +--------------------+ +--------------------+ +// |## DISKS: myhost ##@| |## DISKS: myhost ##@| +// |/ 18.3G E-- F| |-local 18.3G E--- F| +// |-local 18.3G E--- F| +--------------------+ +// |/boot 949.6M E- F| +// +--------------------+ // - // TODO: Disk screen! Requires virtual pages in the server, though... - int disk_screen (int rep, int display) { @@ -58,7 +64,6 @@ disk_screen (int rep, int display) sprintf (buffer, "widget_set D title {DISKS: %s}\n", get_hostname()); sock_send_string (sock, buffer); sock_send_string (sock, "widget_add D f frame\n"); - //sock_send_string(sock, "widget_set D f 1 2 20 4 20 3 v 8\n"); sprintf (buffer, "widget_set D f 1 2 %i %i %i %i v 12\n", lcd_wid, lcd_hgt, lcd_wid, lcd_hgt - 1); sock_send_string (sock, buffer); sock_send_string (sock, "widget_add D err1 string\n"); @@ -68,7 +73,6 @@ disk_screen (int rep, int display) } // Grab disk stats on first display, and fill "table". // Get rid of old, unmounted filesystems... - memset (table, 0, sizeof (struct disp) * 256); machine_get_fs(mnt, &count); @@ -82,41 +86,22 @@ disk_screen (int rep, int display) else sprintf (table[i].dev, "%s", mnt[i].mpoint); - //table[i].full = (lcd_cellwid * 4); table[i].full = (huge) (lcd_cellwid * gauge_wid) * (huge) (mnt[i].blocks - mnt[i].bfree) / (huge) mnt[i].blocks; size = (huge) mnt[i].bsize * (huge) mnt[i].blocks; - memset (table[i].cap, 0, 8); - - // Kilobytes - if ((size >= 0) && (size < (huge) 1000 * (huge) 1000)) - sprintf (table[i].cap, "%3.1fk", (double) (size) / 1024.0); - // Megabytes - else if ((size >= (huge) 1000 * (huge) 1000) && (size < (huge) 1000 * (huge) 1000 * (huge) 1000)) - sprintf (table[i].cap, "%3.1fM", (float) (size / (huge) 1024) / 1024.0); - // Gigabytes - else if ((size >= (huge) 1000 * (huge) 1000 * (huge) 1000) && - (size < (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000)) - sprintf (table[i].cap, "%3.1fG", (float) (size / ((huge) 1024 * (huge) 1024)) / 1024.0); - // Terabytes - else if ((size >= (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000) && - (size < (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000)) - sprintf (table[i].cap, "%3.1fT", (float) (size / ((huge) 1024 * (huge) 1024 * (huge) 1024)) / 1024.0); - // PectaBytes -- Yeah! I want some! - else if ((size >= (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000) && - (size < (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000 * (huge) 1000)) - sprintf (table[i].cap, "%3.1fP", (float) (size / ((huge) 1024 * (huge) 1024 * (huge) 1024 * (huge) 1024)) / 1024.0); + memset (table[i].cap, '\0', 8); + sprintf_memory(table[i].cap, (double) size, 1); } } - - if (!count) { + else { sock_send_string (sock, "widget_set D err1 1 2 {Error Retrieving}\n"); sock_send_string (sock, "widget_set D err2 1 3 {Filesystem Stats}\n"); return 0; } + // Display stuff... (show for two seconds, then scroll once per // second, then hold at the end for two seconds) sprintf (buffer, "widget_set D f 1 2 %i %i %i %i v 12\n", lcd_wid, lcd_hgt, lcd_wid, count); @@ -126,8 +111,8 @@ disk_screen (int rep, int display) for (i = 0; i < count; i++) { if (table[i].dev[0] == '\0') continue; - if (i >= num_disks) // Make sure we have enough lines... - { + + if (i >= num_disks) { // Make sure we have enough lines... sprintf (tmp, "widget_add D s%i string -in f\n", i); sock_send_string (sock, tmp); sprintf (tmp, "widget_add D h%i hbar -in f\n", i); @@ -150,7 +135,7 @@ disk_screen (int rep, int display) } // Now remove extra widgets... - for (; i < num_disks; i++) { + for ( ; i < num_disks; i++) { sprintf (tmp, "widget_del D s%i\n", i); sock_send_string (sock, tmp); sprintf (tmp, "widget_del D h%i\n", i); @@ -161,11 +146,5 @@ disk_screen (int rep, int display) #undef huge -/* -// ** FILESYSTEMS ***** -// / 543.2M E----F -// /dos/c 2.1G E----F -// /stuff 4.3G E----F -*/ return 0; } diff --git a/clients/lcdproc/load.c b/clients/lcdproc/load.c index 45d9387..1a9c546 100644 --- a/clients/lcdproc/load.c +++ b/clients/lcdproc/load.c @@ -14,10 +14,17 @@ #include "machine.h" #include "load.h" + /////////////////////////////////////////////////////////////////////////// // Shows a display very similar to "xload"'s histogram. // - +// +--------------------+ +--------------------+ +// |## LOAD 0.44: myh #@| |myhost 0.24 1| +// | 1| | |||||||||||0| +// | |||||| | +--------------------+ +// | |||| |||||||| 0| +// +--------------------+ +// int xload_screen (int rep, int display) { @@ -27,10 +34,9 @@ xload_screen (int rep, int display) double loadmax = 0, factor, x; int status = 0; - if (first) // Only the first time this is ever called... - { + if (first) { // Only the first time this is ever called... first = 0; - memset (loads, 0, sizeof (double) * LCD_MAX_WIDTH); + memset (loads, '\0', sizeof (double) * LCD_MAX_WIDTH); sock_send_string (sock, "screen_add X\n"); sprintf (buffer, "screen_set X -name {X-Load: %s}\n", get_hostname()); @@ -68,9 +74,9 @@ xload_screen (int rep, int display) loadmax = loads[n]; n = (int) loadmax; - if ((double) n < loadmax) { + if ((double) n < loadmax) n++; - } + sprintf (tmp, "widget_set X top %i %i %i\n", lcd_wid, (lcd_hgt <= 2) ? 1 : 2, n); //if(display) sock_send_string(sock, tmp); sock_send_string (sock, tmp); diff --git a/clients/lcdproc/mem.c b/clients/lcdproc/mem.c index 1a02d88..5c88888 100644 --- a/clients/lcdproc/mem.c +++ b/clients/lcdproc/mem.c @@ -15,46 +15,27 @@ #include "mode.h" #include "machine.h" #include "mem.h" +#include "util.h" -static char *sprintmem(char *dest, double value, double roundlimit) -{ - if (dest != NULL) { - static char *units[] = { "", "k", "M", "G", "T", "P", "E", "Z", "Y", NULL }; - int offs = 0; - char *format = "%3.1f%s"; - - if ((roundlimit <= 0.0) || (roundlimit > 1.0)) - roundlimit = 0.5; - - while (units[offs] != NULL > 0) { - if (value <= 1024 * roundlimit) - break; - offs++; - value /= 1024; - } - if (value < 100) - format = "%2.2f%s"; - if (value < 10) - format = "%1.3f%s"; - - sprintf(dest, format, value, units[offs]); - } - return dest; -} - ///////////////////////////////////////////////////////////////////////// // Mem Screen displays info about memory and swap usage... // +// +--------------------+ +--------------------+ +// |## MEM -==- SWAP #@| |M 758.3M [- ] 35.3%@| +// | 758.3M Totl 1.884G | |S 1.884G [ ] 0.1% | +// | 490.8M Free 1.882G | +--------------------+ +// |E--- F E F| +// +--------------------+ +// int mem_screen (int rep, int display) { - meminfo_type mem[2]; static int first = 1; static int which_title = 0; static int gauge_wid = 0; static int gauge_offs = 0; - double value; + meminfo_type mem[2]; if (first) { first = 0; @@ -105,8 +86,6 @@ mem_screen (int rep, int display) } sock_send_string(sock, "widget_add M mem% string\n"); sock_send_string(sock, "widget_add M swap% string\n"); - //sock_send_string(sock, "widget_set M mem% 16 1 { 0.0%}\n"); - //sock_send_string(sock, "widget_set M swap% 16 2 { 0.0%}\n"); } sock_send_string(sock, "widget_add M memtotl string\n"); @@ -118,8 +97,8 @@ mem_screen (int rep, int display) machine_get_meminfo(mem); - // flip the title back and forth... if (lcd_hgt >= 4) { + // flip the title back and forth... (every 4 updates) if (which_title & 4) { sprintf(buffer, "widget_set M title {%s}\n", get_hostname()); sock_send_string(sock, buffer); @@ -128,25 +107,25 @@ mem_screen (int rep, int display) which_title = (which_title + 1) & 7; // Total memory - sprintmem(tmp, mem[0].total * 1024, 1); + sprintf_memory(tmp, mem[0].total * 1024, 1); sprintf(buffer, "widget_set M memtotl 1 2 {%7s}\n", tmp); if (display) sock_send_string(sock, buffer); // Free memory (plus buffers and cache) - sprintmem(tmp, (mem[0].free + mem[0].buffers + mem[0].cache) * 1024, 1); + sprintf_memory(tmp, (mem[0].free + mem[0].buffers + mem[0].cache) * 1024, 1); sprintf(buffer, "widget_set M memused 1 3 {%7s}\n", tmp); if (display) sock_send_string(sock, buffer); // Total swap - sprintmem(tmp, mem[1].total * 1024, 1); + sprintf_memory(tmp, mem[1].total * 1024, 1); sprintf(buffer, "widget_set M swaptotl %i 2 {%7s}\n", lcd_wid - 7, tmp); if (display) sock_send_string(sock, buffer); // Free swap - sprintmem(tmp, mem[1].free * 1024, 1); + sprintf_memory(tmp, mem[1].free * 1024, 1); sprintf(buffer, "widget_set M swapused %i 3 {%7s}\n", lcd_wid - 7, tmp); if (display) sock_send_string(sock, buffer); @@ -154,7 +133,7 @@ mem_screen (int rep, int display) if (gauge_wid > 0) { // Free memory graph if (mem[0].total > 0) { - value = 1.0 - (double) (mem[0].free + mem[0].buffers + mem[0].cache) + double value = 1.0 - (double) (mem[0].free + mem[0].buffers + mem[0].cache) / (double) mem[0].total; // printf(".0f", val) only prints the integer part @@ -166,7 +145,7 @@ mem_screen (int rep, int display) // Free swap graph if (mem[1].total > 0) { - value = 1.0 - ((double) mem[1].free / (double) mem[1].total); + double value = 1.0 - ((double) mem[1].free / (double) mem[1].total); // printf(".0f", val) only prints the integer part sprintf(buffer, "widget_set M swapgauge %i 4 %.0f\n", @@ -178,21 +157,21 @@ mem_screen (int rep, int display) } else { // Total memory - sprintmem(tmp, mem[0].total * 1024, 1); + sprintf_memory(tmp, mem[0].total * 1024, 1); sprintf(buffer, "widget_set M memtotl 3 1 {%6s}\n", tmp); if (display) sock_send_string(sock, buffer); // Total swap - sprintmem(tmp, mem[1].total * 1024, 1); + sprintf_memory(tmp, mem[1].total * 1024, 1); sprintf(buffer, "widget_set M swaptotl 3 2 {%6s}\n", tmp); if (display) sock_send_string(sock, buffer); // Free memory graph - sprintf(tmp, "N/A"); + strcpy(tmp, "N/A"); if (mem[0].total > 0) { - value = 1.0 - (double) (mem[0].free + mem[0].buffers + mem[0].cache) + double value = 1.0 - (double) (mem[0].free + mem[0].buffers + mem[0].cache) / (double) mem[0].total; if (gauge_wid > 0) { @@ -203,18 +182,16 @@ mem_screen (int rep, int display) sock_send_string(sock, buffer); } - value = min(value * 100.0, 100.0); - sprintf(tmp, (value > 99.9) ? "%3.0f%%" : "%4.1f%%", value); + sprintf_percent(tmp, value * 100); } - sprintf(buffer, "widget_set M mem%% %i 1 {%s}\n", lcd_wid - strlen(tmp), tmp); + sprintf(buffer, "widget_set M mem%% %i 1 {%5s}\n", lcd_wid - 5, tmp); if (display) sock_send_string(sock, buffer); - // Free swap graph - sprintf(tmp, "N/A"); + strcpy(tmp, "N/A"); if (mem[1].total > 0) { - value = 1.0 - ((double) mem[1].free / (double) mem[1].total); + double value = 1.0 - ((double) mem[1].free / (double) mem[1].total); if (gauge_wid > 0) { // printf(".0f", val) only prints the integer part @@ -222,18 +199,17 @@ mem_screen (int rep, int display) gauge_offs, lcd_cellwid * gauge_wid * value); if (display) sock_send_string(sock, buffer); - } + } - value = min(value * 100.0, 100.0); - sprintf(tmp, (value > 99.9) ? "%3.0f%%" : "%4.1f%%", value); + sprintf_percent(tmp, value * 100); } - sprintf(buffer, "widget_set M swap%% %i 2 {%s}\n", lcd_wid - strlen(tmp), tmp); + sprintf(buffer, "widget_set M swap%% %i 2 {%5s}\n", lcd_wid - 5, tmp); if (display) sock_send_string(sock, buffer); } return 0; -} // End mem_screen() +} // End mem_screen() static int @@ -241,9 +217,7 @@ sort_procs (void *a, void *b) { procinfo_type *one, *two; - if (!a) - return 0; - if (!b) + if ((a == NULL) || (b == NULL)) return 0; one = (procinfo_type *) a; @@ -253,13 +227,22 @@ sort_procs (void *a, void *b) } +///////////////////////////////////////////////////////////////////////// +// Mem Top Screen displays info about top 5 memory hogs... +// +// +--------------------+ +--------------------+ +// |## TOP MEM: myhos #@| |## TOP MEM: myhos #@| +// |1 110.4M mysqld | |1 110.4M mysqld | +// |2 35.38M konqueror(2| +--------------------+ +// |3 29.21M XFree86 | +// +--------------------+ +// int mem_top_screen (int rep, int display) { - int i; - procinfo_type *p; - LinkedList *procs; static int first = 1; + LinkedList *procs; + int i; if (first) { first = 0; @@ -285,8 +268,8 @@ mem_top_screen (int rep, int display) sock_send_string(sock, "widget_set S 1 1 1 Checking...\n"); } - procs = LL_new (); - if (!procs) { + procs = LL_new(); + if (procs == NULL) { fprintf (stderr, "mem_top_screen: Error allocating list\n"); return -1; } @@ -294,17 +277,22 @@ mem_top_screen (int rep, int display) machine_get_procs(procs); // Now, print some info... - LL_Rewind (procs); - LL_Sort (procs, sort_procs); - LL_Rewind (procs); + LL_Rewind(procs); + LL_Sort(procs, sort_procs); + LL_Rewind(procs); for (i = 1; i <= 5; i++) { - p = LL_Get (procs); - if (p) { - //printf("Mem hog: %s: %ik\n", p->name, p->size); + procinfo_type *p = LL_Get(procs); + + if (p != NULL) { + char mem[10]; + + sprintf_memory(mem, (double) p->totl * 1024, 1); + + //printf("Mem hog: %s: %s\n", p->name, mem); if (p->number > 1) - sprintf(tmp, "%i%6ik %s(%i)", i, p->totl, p->name, p->number); + sprintf(tmp, "%i %5s %s(%i)", i, mem, p->name, p->number); else - sprintf(tmp, "%i%6ik %s", i, p->totl, p->name); + sprintf(tmp, "%i %5s %s", i, mem, p->name); sprintf(buffer, "widget_set S %i 1 %i {%s}\n", i, i, tmp); if (display) sock_send_string(sock, buffer); @@ -320,17 +308,16 @@ mem_top_screen (int rep, int display) } // Now clean it all up... - LL_Rewind (procs); + LL_Rewind(procs); do { - p = (procinfo_type *) LL_Get (procs); - if (p) { + procinfo_type *p = (procinfo_type *) LL_Get(procs); + if (p != NULL) { //printf("Proc: %6ik %s\n", p->size, p->name); - free (p); + free(p); } - } while (LL_Next (procs) == 0); - LL_Destroy (procs); + } while (LL_Next(procs) == 0); + LL_Destroy(procs); return 0; - } diff --git a/clients/lcdproc/util.c b/clients/lcdproc/util.c new file mode 100644 index 0000000..4ee34fb --- /dev/null +++ b/clients/lcdproc/util.c @@ -0,0 +1,75 @@ +/****************************************************************************** +* +* util.c - utility functions to print some numerical values in a nice fashion +* Copyright (C) 2005 Peter Marschall +* +* --- +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* +* --- +* +* $Source$ +* $Revision$ +* Checked in by: $Author$ +* +*******************************************************************************/ + +#include "util.h" + + +/** print a memory value with the correct unit to a given string */ +char * +sprintf_memory(char *dst, double value, double roundlimit) +{ + if (dst != NULL) { + static char *units[] = { "", "k", "M", "G", "T", "P", "E", "Z", "Y", NULL }; + int offs = 0; + char *format = "%.1f%s"; + + if ((roundlimit <= 0.0) || (roundlimit > 1.0)) + roundlimit = 0.5; + + while (units[offs] != NULL > 0) { + if (value <= 1024 * roundlimit) + break; + offs++; + value /= 1024; + } + if (value < 100) + format = "%.2f%s"; + if (value < 10) + format = "%.3f%s"; + + sprintf(dst, format, value, units[offs]); + } + return dst; +} + + +/** print a percentage value to a given string */ +char * +sprintf_percent(char *dst, double percent) +{ + if (dst != NULL) { + if (percent > 99.9) + strcpy(dst, "100%"); + else + sprintf(dst, "%.1f%%", (percent >= 0) ? percent : 0); + } + return dst; +} + +/* EOF */ diff --git a/clients/lcdproc/util.h b/clients/lcdproc/util.h new file mode 100644 index 0000000..3ad1429 --- /dev/null +++ b/clients/lcdproc/util.h @@ -0,0 +1,45 @@ +/****************************************************************************** +* +* util.h - header file for util.c +* Copyright (C) 2005 Peter Marschall +* +* --- +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* +* --- +* +* $Source$ +* $Revision$ +* Checked in by: $Author$ +* +*******************************************************************************/ + + +#ifndef LCDPROC_UTIL_H +# define LCDPROC_UTIL_H + +#include +#include + +/** print a memory value with the correct unit to a given string */ +char *sprintf_memory(char *dst, double value, double roundlimit); + +/** print a percentage value to a given string */ +char *sprintf_percent(char *dst, double percent); + +#endif /* LCDPROC_UTIL_H */ + +/* EOF */