From 2dcdd87658cdc38b2df5535cce85976e963fbd30 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 27 Nov 2014 19:34:47 -0200 Subject: [PATCH 1/2] Move UptimeMeter back into cross-platform code. We'll just isolate the platform-dependent bit. --- linux/UptimeMeter.c => UptimeMeter.c | 0 linux/UptimeMeter.h => UptimeMeter.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename linux/UptimeMeter.c => UptimeMeter.c (100%) rename linux/UptimeMeter.h => UptimeMeter.h (100%) diff --git a/linux/UptimeMeter.c b/UptimeMeter.c similarity index 100% rename from linux/UptimeMeter.c rename to UptimeMeter.c diff --git a/linux/UptimeMeter.h b/UptimeMeter.h similarity index 100% rename from linux/UptimeMeter.h rename to UptimeMeter.h From ca03094bb2d60c6ee1558bd2bcfb9038244b38cb Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 27 Nov 2014 19:41:14 -0200 Subject: [PATCH 2/2] Make UptimeMeter cross-platform again. --- Makefile.am | 10 ++++------ UptimeMeter.c | 15 +++++---------- linux/Platform.c | 11 +++++++++++ unsupported/Platform.c | 5 +++++ unsupported/Platform.h | 1 + 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Makefile.am b/Makefile.am index 6374fafa..5ce4c23a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -20,7 +20,7 @@ ClockMeter.c ColorsPanel.c ColumnsPanel.c CPUMeter.c CRT.c \ DisplayOptionsPanel.c FunctionBar.c Hashtable.c Header.c htop.c ListItem.c \ LoadAverageMeter.c MemoryMeter.c Meter.c MetersPanel.c Object.c Panel.c \ BatteryMeter.c Process.c ProcessList.c RichString.c ScreenManager.c Settings.c \ -SignalsPanel.c String.c SwapMeter.c TasksMeter.c TraceScreen.c \ +SignalsPanel.c String.c SwapMeter.c TasksMeter.c UptimeMeter.c TraceScreen.c \ UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.c \ HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c @@ -30,17 +30,15 @@ CPUMeter.h CRT.h DisplayOptionsPanel.h FunctionBar.h \ Hashtable.h Header.h htop.h ListItem.h LoadAverageMeter.h MemoryMeter.h \ BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h \ ScreenManager.h Settings.h SignalsPanel.h String.h SwapMeter.h TasksMeter.h \ -TraceScreen.h UsersTable.h Vector.h Process.h AffinityPanel.h \ +UptimeMeter.h TraceScreen.h UsersTable.h Vector.h Process.h AffinityPanel.h \ HostnameMeter.h OpenFilesScreen.h Affinity.h IncSet.h Action.h if HTOP_LINUX myhtopplatsources = linux/Platform.c linux/IOPriorityPanel.c linux/IOPriority.c \ -linux/LinuxProcess.c linux/LinuxProcessList.c linux/LinuxCRT.c \ -linux/UptimeMeter.c +linux/LinuxProcess.c linux/LinuxProcessList.c linux/LinuxCRT.c myhtopplatheaders = linux/Platform.h linux/IOPriorityPanel.h linux/IOPriority.h \ -linux/LinuxProcess.h linux/LinuxProcessList.h linux/LinuxCRT.h \ -linux/UptimeMeter.h +linux/LinuxProcess.h linux/LinuxProcessList.h linux/LinuxCRT.h endif if HTOP_UNSUPPORTED diff --git a/UptimeMeter.c b/UptimeMeter.c index 9e288171..a033b171 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -6,12 +6,9 @@ in the source distribution for its full text. */ #include "UptimeMeter.h" - -#include "ProcessList.h" +#include "Platform.h" #include "CRT.h" -#include - /*{ #include "Meter.h" }*/ @@ -21,13 +18,11 @@ int UptimeMeter_attributes[] = { }; static void UptimeMeter_setValues(Meter* this, char* buffer, int len) { - double uptime = 0; - FILE* fd = fopen(PROCDIR "/uptime", "r"); - if (fd) { - fscanf(fd, "%64lf", &uptime); - fclose(fd); + int totalseconds = Platform_getUptime(); + if (totalseconds == -1) { + snprintf(buffer, len, "(unknown)"); + return; } - int totalseconds = (int) ceil(uptime); int seconds = totalseconds % 60; int minutes = (totalseconds/60) % 60; int hours = (totalseconds/3600) % 24; diff --git a/linux/Platform.c b/linux/Platform.c index d39e2e96..f6c88170 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -20,6 +20,8 @@ in the source distribution for its full text. #include "ClockMeter.h" #include "HostnameMeter.h" +#include + /*{ #include "Action.h" }*/ @@ -67,3 +69,12 @@ MeterClass* Platform_meterTypes[] = { NULL }; +int Platform_getUptime() { + double uptime = 0; + FILE* fd = fopen(PROCDIR "/uptime", "r"); + if (fd) { + fscanf(fd, "%64lf", &uptime); + fclose(fd); + } + int totalseconds = (int) floor(uptime); +} diff --git a/unsupported/Platform.c b/unsupported/Platform.c index 5e568025..8d4b3a0d 100644 --- a/unsupported/Platform.c +++ b/unsupported/Platform.c @@ -14,6 +14,7 @@ in the source distribution for its full text. #include "BatteryMeter.h" #include "ClockMeter.h" #include "HostnameMeter.h" +#include "UptimeMeter.h" /*{ #include "Action.h" @@ -33,6 +34,7 @@ MeterClass* Platform_meterTypes[] = { &TasksMeter_class, &BatteryMeter_class, &HostnameMeter_class, + &UptimeMeter_class, &AllCPUsMeter_class, &AllCPUs2Meter_class, &LeftCPUsMeter_class, @@ -43,3 +45,6 @@ MeterClass* Platform_meterTypes[] = { NULL }; +int Platform_getUptime() { + return -1; +} diff --git a/unsupported/Platform.h b/unsupported/Platform.h index 1a2b81f0..026c45fb 100644 --- a/unsupported/Platform.h +++ b/unsupported/Platform.h @@ -15,5 +15,6 @@ void Platform_setBindings(Htop_Action* keys); extern MeterClass* Platform_meterTypes[]; +int Platform_getUptime(); #endif