diff --git a/Action.c b/Action.c index 6d7782ca3..f53acf7b6 100644 --- a/Action.c +++ b/Action.c @@ -19,9 +19,9 @@ in the source distribution for its full text. #include "Process.h" #include "ScreenManager.h" #include "SignalsPanel.h" -#include "StringUtils.h" #include "TraceScreen.h" #include "Platform.h" +#include "XUtils.h" #include #include diff --git a/AffinityPanel.c b/AffinityPanel.c index 0a9648829..ab17b420f 100644 --- a/AffinityPanel.c +++ b/AffinityPanel.c @@ -5,14 +5,17 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "AffinityPanel.h" -#include "CRT.h" +#include "config.h" -#include "Vector.h" +#include "AffinityPanel.h" #include #include +#include "CRT.h" +#include "Vector.h" +#include "XUtils.h" + #ifdef HAVE_LIBHWLOC #include #endif diff --git a/AvailableColumnsPanel.c b/AvailableColumnsPanel.c index dcfbb3e27..32bb470e9 100644 --- a/AvailableColumnsPanel.c +++ b/AvailableColumnsPanel.c @@ -6,16 +6,17 @@ in the source distribution for its full text. */ #include "AvailableColumnsPanel.h" -#include "Platform.h" - -#include "Header.h" -#include "ColumnsPanel.h" #include -#include #include +#include #include +#include "ColumnsPanel.h" +#include "Header.h" +#include "Platform.h" +#include "XUtils.h" + static const char* const AvailableColumnsFunctions[] = {" ", " ", " ", " ", "Add ", " ", " ", " ", " ", "Done ", NULL}; diff --git a/AvailableMetersPanel.c b/AvailableMetersPanel.c index b51a9e9f4..faa69a8f0 100644 --- a/AvailableMetersPanel.c +++ b/AvailableMetersPanel.c @@ -12,6 +12,7 @@ in the source distribution for its full text. #include "Header.h" #include "ListItem.h" #include "Platform.h" +#include "XUtils.h" #include #include diff --git a/BatteryMeter.c b/BatteryMeter.c index 060b125cc..22a63c7a1 100644 --- a/BatteryMeter.c +++ b/BatteryMeter.c @@ -12,8 +12,8 @@ This meter written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com). #include "Battery.h" #include "ProcessList.h" #include "CRT.h" -#include "StringUtils.h" #include "Platform.h" +#include "XUtils.h" #include #include diff --git a/CPUMeter.c b/CPUMeter.c index 446c04d20..7e70f4549 100644 --- a/CPUMeter.c +++ b/CPUMeter.c @@ -10,6 +10,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Settings.h" #include "Platform.h" +#include "XUtils.h" #include #include diff --git a/CRT.c b/CRT.c index 06ce04512..2f9eb2b6f 100644 --- a/CRT.c +++ b/CRT.c @@ -8,8 +8,8 @@ in the source distribution for its full text. #include "config.h" #include "CRT.h" -#include "StringUtils.h" #include "RichString.h" +#include "XUtils.h" #include #include diff --git a/ColorsPanel.c b/ColorsPanel.c index 917be98cf..5b189ea3d 100644 --- a/ColorsPanel.c +++ b/ColorsPanel.c @@ -9,6 +9,7 @@ in the source distribution for its full text. #include "CRT.h" #include "CheckItem.h" +#include "XUtils.h" #include #include diff --git a/ColumnsPanel.c b/ColumnsPanel.c index e4fe4fc1f..a0e9fd31c 100644 --- a/ColumnsPanel.c +++ b/ColumnsPanel.c @@ -8,7 +8,7 @@ in the source distribution for its full text. #include "ColumnsPanel.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" #include "ListItem.h" #include "CRT.h" diff --git a/CommandScreen.c b/CommandScreen.c index 58ed88404..df039645d 100644 --- a/CommandScreen.c +++ b/CommandScreen.c @@ -5,7 +5,7 @@ #include "IncSet.h" #include "ListItem.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include diff --git a/DiskIOMeter.c b/DiskIOMeter.c index 8393af705..d882b4333 100644 --- a/DiskIOMeter.c +++ b/DiskIOMeter.c @@ -11,6 +11,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" +#include "XUtils.h" static const int DiskIOMeter_attributes[] = { diff --git a/DisplayOptionsPanel.c b/DisplayOptionsPanel.c index 1aede655f..d667ab7fa 100644 --- a/DisplayOptionsPanel.c +++ b/DisplayOptionsPanel.c @@ -7,13 +7,14 @@ in the source distribution for its full text. #include "DisplayOptionsPanel.h" -#include "CheckItem.h" -#include "CRT.h" - #include #include #include +#include "CheckItem.h" +#include "CRT.h" +#include "XUtils.h" + static const char* const DisplayOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL}; diff --git a/EnvScreen.c b/EnvScreen.c index 1f107457e..10060ec7e 100644 --- a/EnvScreen.c +++ b/EnvScreen.c @@ -5,7 +5,7 @@ #include "IncSet.h" #include "ListItem.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include diff --git a/FunctionBar.c b/FunctionBar.c index c67e613c3..50bae0b14 100644 --- a/FunctionBar.c +++ b/FunctionBar.c @@ -8,7 +8,7 @@ in the source distribution for its full text. #include "FunctionBar.h" #include "CRT.h" #include "RichString.h" -#include "XAlloc.h" +#include "XUtils.h" #include #include diff --git a/Hashtable.c b/Hashtable.c index 383b34a88..a086227c8 100644 --- a/Hashtable.c +++ b/Hashtable.c @@ -6,7 +6,7 @@ in the source distribution for its full text. */ #include "Hashtable.h" -#include "XAlloc.h" +#include "XUtils.h" #include #include diff --git a/Header.c b/Header.c index e5a64db19..f52333703 100644 --- a/Header.c +++ b/Header.c @@ -8,8 +8,8 @@ in the source distribution for its full text. #include "Header.h" #include "CRT.h" -#include "StringUtils.h" #include "Platform.h" +#include "XUtils.h" #include #include diff --git a/IncSet.c b/IncSet.c index 9dda3ce1e..3534f8577 100644 --- a/IncSet.c +++ b/IncSet.c @@ -6,9 +6,10 @@ in the source distribution for its full text. */ #include "IncSet.h" -#include "StringUtils.h" #include "ListItem.h" #include "CRT.h" +#include "XUtils.h" + #include #include diff --git a/InfoScreen.c b/InfoScreen.c index ecde6d91e..ed890f7d8 100644 --- a/InfoScreen.c +++ b/InfoScreen.c @@ -6,7 +6,7 @@ #include "IncSet.h" #include "ListItem.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include diff --git a/ListItem.c b/ListItem.c index d9d36f89c..c4479d73f 100644 --- a/ListItem.c +++ b/ListItem.c @@ -8,8 +8,8 @@ in the source distribution for its full text. #include "ListItem.h" #include "CRT.h" -#include "StringUtils.h" #include "RichString.h" +#include "XUtils.h" #include #include diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index 208b7e95b..c52bffc1d 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -9,6 +9,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" +#include "XUtils.h" static const int LoadAverageMeter_attributes[] = { diff --git a/Makefile.am b/Makefile.am index 600f2afc1..fd02bb3e1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -56,14 +56,13 @@ myhtopsources = \ ScreenManager.c \ Settings.c \ SignalsPanel.c \ - StringUtils.c \ SwapMeter.c \ TasksMeter.c \ TraceScreen.c \ UptimeMeter.c \ UsersTable.c \ Vector.c \ - XAlloc.c + XUtils.c myhtopheaders = \ Action.h \ @@ -108,14 +107,13 @@ myhtopheaders = \ ScreenManager.h \ Settings.h \ SignalsPanel.h \ - StringUtils.h \ SwapMeter.h \ TasksMeter.h \ TraceScreen.h \ UptimeMeter.h \ UsersTable.h \ Vector.h \ - XAlloc.h + XUtils.h # Linux # ----- diff --git a/Meter.c b/Meter.c index 3edfb2331..154a5a642 100644 --- a/Meter.c +++ b/Meter.c @@ -10,8 +10,8 @@ in the source distribution for its full text. #include "RichString.h" #include "Object.h" #include "CRT.h" -#include "StringUtils.h" #include "Settings.h" +#include "XUtils.h" #include #include diff --git a/Object.h b/Object.h index f601ddf05..6c31fa347 100644 --- a/Object.h +++ b/Object.h @@ -9,8 +9,9 @@ in the source distribution for its full text. */ #include "RichString.h" -#include "XAlloc.h" #include "Macros.h" +#include "XUtils.h" + typedef struct Object_ Object; diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index 1973cc45c..8e099bbe4 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -10,8 +10,8 @@ in the source distribution for its full text. #include "CRT.h" #include "ProcessList.h" #include "IncSet.h" -#include "StringUtils.h" #include "FunctionBar.h" +#include "XUtils.h" #include #include diff --git a/Panel.c b/Panel.c index 06a15c6b1..76e45d6ee 100644 --- a/Panel.c +++ b/Panel.c @@ -10,7 +10,7 @@ in the source distribution for its full text. #include "CRT.h" #include "RichString.h" #include "ListItem.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include diff --git a/Process.c b/Process.c index 43a4c47e4..147caee6b 100644 --- a/Process.c +++ b/Process.c @@ -12,9 +12,9 @@ in the source distribution for its full text. #include "config.h" #include "CRT.h" -#include "StringUtils.h" #include "RichString.h" #include "Platform.h" +#include "XUtils.h" #include #include diff --git a/ProcessList.c b/ProcessList.c index 1b364cfcf..b74454319 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -6,10 +6,10 @@ in the source distribution for its full text. */ #include "ProcessList.h" -#include "Platform.h" #include "CRT.h" -#include "StringUtils.h" +#include "Platform.h" +#include "XUtils.h" #include #include diff --git a/RichString.c b/RichString.c index 8019135e6..3cb19ae41 100644 --- a/RichString.c +++ b/RichString.c @@ -6,12 +6,13 @@ in the source distribution for its full text. */ #include "RichString.h" -#include "XAlloc.h" #include "Macros.h" +#include "XUtils.h" #include #include + #define charBytes(n) (sizeof(CharType) * (n)) static void RichString_extendLen(RichString* this, int len) { diff --git a/Settings.c b/Settings.c index 7a90ec477..fd623ad8e 100644 --- a/Settings.c +++ b/Settings.c @@ -6,11 +6,11 @@ in the source distribution for its full text. */ #include "Settings.h" -#include "Platform.h" -#include "StringUtils.h" -#include "Vector.h" #include "CRT.h" +#include "Platform.h" +#include "Vector.h" +#include "XUtils.h" #include #include diff --git a/SignalsPanel.c b/SignalsPanel.c index 933a21ba9..0ebe16d33 100644 --- a/SignalsPanel.c +++ b/SignalsPanel.c @@ -7,16 +7,16 @@ in the source distribution for its full text. #include "Panel.h" #include "SignalsPanel.h" -#include "Platform.h" - -#include "ListItem.h" -#include "RichString.h" -#include #include +#include #include +#include -#include +#include "ListItem.h" +#include "Platform.h" +#include "RichString.h" +#include "XUtils.h" Panel* SignalsPanel_new() { diff --git a/TasksMeter.c b/TasksMeter.c index fb3767c55..a13766ed0 100644 --- a/TasksMeter.c +++ b/TasksMeter.c @@ -7,8 +7,9 @@ in the source distribution for its full text. #include "TasksMeter.h" -#include "Platform.h" #include "CRT.h" +#include "Platform.h" +#include "XUtils.h" static const int TasksMeter_attributes[] = { diff --git a/TraceScreen.c b/TraceScreen.c index 2956b5696..314efc93b 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -11,8 +11,8 @@ in the source distribution for its full text. #include "ProcessList.h" #include "ListItem.h" #include "IncSet.h" -#include "StringUtils.h" #include "FunctionBar.h" +#include "XUtils.h" #include #include diff --git a/UptimeMeter.c b/UptimeMeter.c index a11951869..1011aa108 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -6,8 +6,10 @@ in the source distribution for its full text. */ #include "UptimeMeter.h" -#include "Platform.h" + #include "CRT.h" +#include "Platform.h" +#include "XUtils.h" static const int UptimeMeter_attributes[] = { diff --git a/UsersTable.c b/UsersTable.c index 722dd6e14..41e52d3f3 100644 --- a/UsersTable.c +++ b/UsersTable.c @@ -5,11 +5,10 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "UsersTable.h" -#include "XAlloc.h" - #include "config.h" +#include "UsersTable.h" + #include #include #include @@ -18,6 +17,8 @@ in the source distribution for its full text. #include #include +#include "XUtils.h" + UsersTable* UsersTable_new() { UsersTable* this; diff --git a/XAlloc.c b/XAlloc.c deleted file mode 100644 index c832d087c..000000000 --- a/XAlloc.c +++ /dev/null @@ -1,76 +0,0 @@ - -#include "XAlloc.h" -#include "RichString.h" - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#include -#include -#include - - -void fail() { - curs_set(1); - endwin(); - abort(); -} - -void* xMalloc(size_t size) { - void* data = malloc(size); - if (!data && size > 0) { - fail(); - } - return data; -} - -void* xCalloc(size_t nmemb, size_t size) { - void* data = calloc(nmemb, size); - if (!data && nmemb > 0 && size > 0) { - fail(); - } - return data; -} - -void* xRealloc(void* ptr, size_t size) { - void* data = realloc(ptr, size); - if (!data && size > 0) { - fail(); - } - return data; -} - -int xAsprintf(char** strp, const char* fmt, ...) { - va_list vl; - va_start(vl, fmt); - int _r = vasprintf(strp, fmt, vl); - va_end(vl); - - if (_r < 0) { - fail(); - } - - return _r; -} - -int xSnprintf(char* buf, int len, const char* fmt, ...) { - va_list vl; - va_start(vl, fmt); - int _n=vsnprintf(buf, len, fmt, vl); - va_end(vl); - - if (!(_n > -1 && _n < len)) { - fail(); - } - - return _n; -} - -char* xStrdup(const char* str) { - char* data = strdup(str); - if (!data) { - fail(); - } - return data; -} diff --git a/XAlloc.h b/XAlloc.h deleted file mode 100644 index 97c155197..000000000 --- a/XAlloc.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef HEADER_XAlloc -#define HEADER_XAlloc - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#include "Macros.h" - -#include -#include -#include - -void fail(void) ATTR_NORETURN; - -void* xMalloc(size_t size); - -void* xCalloc(size_t nmemb, size_t size); - -void* xRealloc(void* ptr, size_t size); - -ATTR_FORMAT(printf, 2, 3) -int xAsprintf(char **strp, const char* fmt, ...); - -ATTR_FORMAT(printf, 3, 4) -int xSnprintf(char *buf, int len, const char* fmt, ...); - -char* xStrdup(const char* str) ATTR_NONNULL; - -#endif diff --git a/StringUtils.c b/XUtils.c similarity index 71% rename from StringUtils.c rename to XUtils.c index f74566f88..4fccec3ab 100644 --- a/StringUtils.c +++ b/XUtils.c @@ -5,14 +5,54 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "StringUtils.h" -#include "XAlloc.h" - #include "config.h" +#include "XUtils.h" + +#include +#include #include #include -#include +#include + +#include "CRT.h" + + +void fail() { + CRT_done(); + abort(); + + _exit(1); // Should never reach here +} + +void* xMalloc(size_t size) { + void* data = malloc(size); + if (!data && size > 0) { + fail(); + } + return data; +} + +void* xCalloc(size_t nmemb, size_t size) { + void* data = calloc(nmemb, size); + if (!data && nmemb > 0 && size > 0) { + fail(); + } + return data; +} + +void* xRealloc(void* ptr, size_t size) { + if (!size) { + free(ptr); + return NULL; + } + void* data = realloc(ptr, size); // deepcode ignore MemoryLeakOnRealloc: this goes to fail() + if (!data) { + free(ptr); + fail(); + } + return data; +} char* String_cat(const char* s1, const char* s2) { int l1 = strlen(s1); @@ -140,3 +180,37 @@ char* String_readLine(FILE* fd) { at = buffer + bufSize - step; } } + +int xAsprintf(char** strp, const char* fmt, ...) { + va_list vl; + va_start(vl, fmt); + int r = vasprintf(strp, fmt, vl); + va_end(vl); + + if (r < 0 || !*strp) { + fail(); + } + + return r; +} + +int xSnprintf(char* buf, int len, const char* fmt, ...) { + va_list vl; + va_start(vl, fmt); + int n = vsnprintf(buf, len, fmt, vl); + va_end(vl); + + if (n < 0 || n >= len) { + fail(); + } + + return n; +} + +char* xStrdup(const char* str) { + char* data = strdup(str); + if (!data) { + fail(); + } + return data; +} diff --git a/StringUtils.h b/XUtils.h similarity index 63% rename from StringUtils.h rename to XUtils.h index 936617ffb..742b8e9eb 100644 --- a/StringUtils.h +++ b/XUtils.h @@ -1,5 +1,5 @@ -#ifndef HEADER_StringUtils -#define HEADER_StringUtils +#ifndef HEADER_XUtils +#define HEADER_XUtils /* htop - StringUtils.h (C) 2004-2011 Hisham H. Muhammad @@ -7,7 +7,21 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ +#include +#include #include +#include + +#include "Macros.h" + + +void fail(void) ATTR_NORETURN; + +void* xMalloc(size_t size); + +void* xCalloc(size_t nmemb, size_t size); + +void* xRealloc(void* ptr, size_t size); #define String_startsWith(s, match) (strncmp((s),(match),strlen(match)) == 0) #define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL) @@ -31,4 +45,12 @@ char* String_getToken(const char* line, const unsigned short int numMatch); char* String_readLine(FILE* fd); +ATTR_FORMAT(printf, 2, 3) +int xAsprintf(char **strp, const char* fmt, ...); + +ATTR_FORMAT(printf, 3, 4) +int xSnprintf(char *buf, int len, const char* fmt, ...); + +char* xStrdup(const char* str) ATTR_NONNULL; + #endif diff --git a/htop.c b/htop.c index 8c813564f..39aeec1a7 100644 --- a/htop.c +++ b/htop.c @@ -18,6 +18,7 @@ in the source distribution for its full text. #include "Settings.h" #include "UsersTable.h" #include "Platform.h" +#include "XUtils.h" #include #include diff --git a/linux/Battery.c b/linux/Battery.c index 14fcc90b7..faf4064c3 100644 --- a/linux/Battery.c +++ b/linux/Battery.c @@ -7,11 +7,10 @@ in the source distribution for its full text. Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com). */ +#include "config.h" // IWYU pragma: keep + #include "Battery.h" -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif #include #include #include @@ -20,7 +19,9 @@ Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat #include #include #include -#include "StringUtils.h" + +#include "XUtils.h" + #define SYS_POWERSUPPLY_DIR "/sys/class/power_supply" diff --git a/linux/IOPriorityPanel.c b/linux/IOPriorityPanel.c index a12b8065b..cc1b3916f 100644 --- a/linux/IOPriorityPanel.c +++ b/linux/IOPriorityPanel.c @@ -7,6 +7,8 @@ in the source distribution for its full text. #include "IOPriorityPanel.h" +#include "XUtils.h" + Panel* IOPriorityPanel_new(IOPriority currPrio) { Panel* this = Panel_new(1, 1, 1, 1, true, Class(ListItem), FunctionBar_newEnterEsc("Set ", "Cancel ")); diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 68baf96d1..75900b5a2 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -9,8 +9,9 @@ in the source distribution for its full text. #include "Process.h" #include "ProcessList.h" #include "LinuxProcess.h" -#include "Platform.h" #include "CRT.h" +#include "Platform.h" +#include "XUtils.h" #include #include @@ -19,6 +20,7 @@ in the source distribution for its full text. #include #include + /* semi-global */ long long btime; diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 6dcc5e5a9..57fab8423 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -8,7 +8,8 @@ in the source distribution for its full text. #include "LinuxProcessList.h" #include "LinuxProcess.h" #include "CRT.h" -#include "StringUtils.h" +#include "XUtils.h" + #include #include #include diff --git a/linux/Platform.c b/linux/Platform.c index 6a3ffab20..feb6b6cff 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -30,7 +30,7 @@ in the source distribution for its full text. #include "zfs/ZfsCompressedArcMeter.h" #include "LinuxProcess.h" #include "SELinuxMeter.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include diff --git a/linux/PressureStallMeter.c b/linux/PressureStallMeter.c index 307e397db..4733415fe 100644 --- a/linux/PressureStallMeter.c +++ b/linux/PressureStallMeter.c @@ -7,14 +7,13 @@ in the source distribution for its full text. */ #include "PressureStallMeter.h" -#include "Platform.h" -#include "CRT.h" #include -/*{ -#include "Meter.h" -}*/ +#include "CRT.h" +#include "Platform.h" +#include "XUtils.h" + static const int PressureStallMeter_attributes[] = { PRESSURE_STALL_TEN, PRESSURE_STALL_SIXTY, PRESSURE_STALL_THREEHUNDRED diff --git a/zfs/ZfsCompressedArcMeter.c b/zfs/ZfsCompressedArcMeter.c index 472b5b5bd..0153fccbd 100644 --- a/zfs/ZfsCompressedArcMeter.c +++ b/zfs/ZfsCompressedArcMeter.c @@ -10,6 +10,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" +#include "XUtils.h" #include #include