From c6f04a9c5d33d16623f1c20f0987dd3a992379e7 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sat, 19 Sep 2020 20:22:34 +0200 Subject: [PATCH 1/4] Move xAsprintf, xSnprintf and xStrdup to StringUtils.h --- AffinityPanel.c | 9 +++++--- AvailableColumnsPanel.c | 11 ++++----- AvailableMetersPanel.c | 1 + CPUMeter.c | 1 + ColorsPanel.c | 1 + DiskIOMeter.c | 1 + DisplayOptionsPanel.c | 7 +++--- FunctionBar.c | 1 + LoadAverageMeter.c | 1 + SignalsPanel.c | 12 +++++----- StringUtils.c | 45 +++++++++++++++++++++++++++++++++---- StringUtils.h | 11 +++++++++ TasksMeter.c | 3 ++- UptimeMeter.c | 4 +++- UsersTable.c | 8 ++++--- XAlloc.c | 34 ---------------------------- XAlloc.h | 8 ------- htop.c | 1 + linux/IOPriorityPanel.c | 2 ++ linux/LinuxProcess.c | 3 ++- linux/PressureStallMeter.c | 9 ++++---- zfs/ZfsCompressedArcMeter.c | 1 + 22 files changed, 100 insertions(+), 74 deletions(-) diff --git a/AffinityPanel.c b/AffinityPanel.c index 0a9648829..0027d4a43 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 "StringUtils.h" +#include "Vector.h" + #ifdef HAVE_LIBHWLOC #include #endif diff --git a/AvailableColumnsPanel.c b/AvailableColumnsPanel.c index dcfbb3e27..a2a6aba67 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 "StringUtils.h" + static const char* const AvailableColumnsFunctions[] = {" ", " ", " ", " ", "Add ", " ", " ", " ", " ", "Done ", NULL}; diff --git a/AvailableMetersPanel.c b/AvailableMetersPanel.c index b51a9e9f4..b90912bde 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 "StringUtils.h" #include #include diff --git a/CPUMeter.c b/CPUMeter.c index 446c04d20..c3133a2cd 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 "StringUtils.h" #include #include diff --git a/ColorsPanel.c b/ColorsPanel.c index 917be98cf..45745a734 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 "StringUtils.h" #include #include diff --git a/DiskIOMeter.c b/DiskIOMeter.c index 8393af705..34b0f081d 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 "StringUtils.h" static const int DiskIOMeter_attributes[] = { diff --git a/DisplayOptionsPanel.c b/DisplayOptionsPanel.c index 1aede655f..d65348877 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 "StringUtils.h" + static const char* const DisplayOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL}; diff --git a/FunctionBar.c b/FunctionBar.c index c67e613c3..3e00b87e8 100644 --- a/FunctionBar.c +++ b/FunctionBar.c @@ -8,6 +8,7 @@ in the source distribution for its full text. #include "FunctionBar.h" #include "CRT.h" #include "RichString.h" +#include "StringUtils.h" #include "XAlloc.h" #include diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index 208b7e95b..507d3dc38 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 "StringUtils.h" static const int LoadAverageMeter_attributes[] = { diff --git a/SignalsPanel.c b/SignalsPanel.c index 933a21ba9..e15e6c8d2 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 "StringUtils.h" Panel* SignalsPanel_new() { diff --git a/StringUtils.c b/StringUtils.c index f74566f88..4eb769680 100644 --- a/StringUtils.c +++ b/StringUtils.c @@ -5,14 +5,17 @@ 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 "StringUtils.h" + +#include +#include #include #include -#include + +#include "XAlloc.h" + char* String_cat(const char* s1, const char* s2) { int l1 = strlen(s1); @@ -140,3 +143,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/StringUtils.h index 936617ffb..5a1af9ad9 100644 --- a/StringUtils.h +++ b/StringUtils.h @@ -9,6 +9,9 @@ in the source distribution for its full text. #include +#include "Macros.h" + + #define String_startsWith(s, match) (strncmp((s),(match),strlen(match)) == 0) #define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL) @@ -31,4 +34,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/TasksMeter.c b/TasksMeter.c index fb3767c55..ef7e49642 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 "StringUtils.h" static const int TasksMeter_attributes[] = { diff --git a/UptimeMeter.c b/UptimeMeter.c index a11951869..c8e511f1a 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 "StringUtils.h" static const int UptimeMeter_attributes[] = { diff --git a/UsersTable.c b/UsersTable.c index 722dd6e14..208fc8d84 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,9 @@ in the source distribution for its full text. #include #include +#include "StringUtils.h" +#include "XAlloc.h" + UsersTable* UsersTable_new() { UsersTable* this; diff --git a/XAlloc.c b/XAlloc.c index c832d087c..815cf47f9 100644 --- a/XAlloc.c +++ b/XAlloc.c @@ -40,37 +40,3 @@ void* xRealloc(void* ptr, size_t size) { } 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 index 97c155197..98a422fc2 100644 --- a/XAlloc.h +++ b/XAlloc.h @@ -19,12 +19,4 @@ 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/htop.c b/htop.c index 8c813564f..9ce966f74 100644 --- a/htop.c +++ b/htop.c @@ -16,6 +16,7 @@ in the source distribution for its full text. #include "ProcessList.h" #include "ScreenManager.h" #include "Settings.h" +#include "StringUtils.h" #include "UsersTable.h" #include "Platform.h" diff --git a/linux/IOPriorityPanel.c b/linux/IOPriorityPanel.c index a12b8065b..ce7f01e44 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 "StringUtils.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..9e3c2f89f 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 "StringUtils.h" #include #include diff --git a/linux/PressureStallMeter.c b/linux/PressureStallMeter.c index 307e397db..518e66c8d 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 "StringUtils.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..def88d020 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 "StringUtils.h" #include #include From 872e542f4eca52ce2198ba3fc30a51bc5d672dae Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Wed, 14 Oct 2020 20:21:09 +0200 Subject: [PATCH 2/4] Rename StringUtils.[ch] to XUtils.[ch] --- Action.c | 2 +- AffinityPanel.c | 2 +- AvailableColumnsPanel.c | 2 +- AvailableMetersPanel.c | 2 +- BatteryMeter.c | 2 +- CPUMeter.c | 2 +- CRT.c | 2 +- ColorsPanel.c | 2 +- ColumnsPanel.c | 2 +- CommandScreen.c | 2 +- DiskIOMeter.c | 2 +- DisplayOptionsPanel.c | 2 +- EnvScreen.c | 2 +- FunctionBar.c | 2 +- Header.c | 2 +- IncSet.c | 3 ++- InfoScreen.c | 2 +- ListItem.c | 2 +- LoadAverageMeter.c | 2 +- Makefile.am | 8 ++++---- Meter.c | 2 +- OpenFilesScreen.c | 2 +- Panel.c | 2 +- Process.c | 2 +- ProcessList.c | 4 ++-- Settings.c | 6 +++--- SignalsPanel.c | 2 +- TasksMeter.c | 2 +- TraceScreen.c | 2 +- UptimeMeter.c | 2 +- UsersTable.c | 2 +- StringUtils.c => XUtils.c | 2 +- StringUtils.h => XUtils.h | 4 ++-- htop.c | 2 +- linux/Battery.c | 9 +++++---- linux/IOPriorityPanel.c | 2 +- linux/LinuxProcess.c | 3 ++- linux/LinuxProcessList.c | 3 ++- linux/Platform.c | 2 +- linux/PressureStallMeter.c | 2 +- zfs/ZfsCompressedArcMeter.c | 2 +- 41 files changed, 55 insertions(+), 51 deletions(-) rename StringUtils.c => XUtils.c (99%) rename StringUtils.h => XUtils.h (95%) 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 0027d4a43..ab17b420f 100644 --- a/AffinityPanel.c +++ b/AffinityPanel.c @@ -13,8 +13,8 @@ in the source distribution for its full text. #include #include "CRT.h" -#include "StringUtils.h" #include "Vector.h" +#include "XUtils.h" #ifdef HAVE_LIBHWLOC #include diff --git a/AvailableColumnsPanel.c b/AvailableColumnsPanel.c index a2a6aba67..32bb470e9 100644 --- a/AvailableColumnsPanel.c +++ b/AvailableColumnsPanel.c @@ -15,7 +15,7 @@ in the source distribution for its full text. #include "ColumnsPanel.h" #include "Header.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" static const char* const AvailableColumnsFunctions[] = {" ", " ", " ", " ", "Add ", " ", " ", " ", " ", "Done ", NULL}; diff --git a/AvailableMetersPanel.c b/AvailableMetersPanel.c index b90912bde..faa69a8f0 100644 --- a/AvailableMetersPanel.c +++ b/AvailableMetersPanel.c @@ -12,7 +12,7 @@ in the source distribution for its full text. #include "Header.h" #include "ListItem.h" #include "Platform.h" -#include "StringUtils.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 c3133a2cd..7e70f4549 100644 --- a/CPUMeter.c +++ b/CPUMeter.c @@ -10,7 +10,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Settings.h" #include "Platform.h" -#include "StringUtils.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 45745a734..5b189ea3d 100644 --- a/ColorsPanel.c +++ b/ColorsPanel.c @@ -9,7 +9,7 @@ in the source distribution for its full text. #include "CRT.h" #include "CheckItem.h" -#include "StringUtils.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 34b0f081d..d882b4333 100644 --- a/DiskIOMeter.c +++ b/DiskIOMeter.c @@ -11,7 +11,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" static const int DiskIOMeter_attributes[] = { diff --git a/DisplayOptionsPanel.c b/DisplayOptionsPanel.c index d65348877..d667ab7fa 100644 --- a/DisplayOptionsPanel.c +++ b/DisplayOptionsPanel.c @@ -13,7 +13,7 @@ in the source distribution for its full text. #include "CheckItem.h" #include "CRT.h" -#include "StringUtils.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 3e00b87e8..4e9d30cda 100644 --- a/FunctionBar.c +++ b/FunctionBar.c @@ -8,8 +8,8 @@ in the source distribution for its full text. #include "FunctionBar.h" #include "CRT.h" #include "RichString.h" -#include "StringUtils.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 507d3dc38..c52bffc1d 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -9,7 +9,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" static const int LoadAverageMeter_attributes[] = { diff --git a/Makefile.am b/Makefile.am index 600f2afc1..0c3b68c01 100644 --- a/Makefile.am +++ b/Makefile.am @@ -56,14 +56,14 @@ myhtopsources = \ ScreenManager.c \ Settings.c \ SignalsPanel.c \ - StringUtils.c \ SwapMeter.c \ TasksMeter.c \ TraceScreen.c \ UptimeMeter.c \ UsersTable.c \ Vector.c \ - XAlloc.c + XAlloc.c \ + XUtils.c myhtopheaders = \ Action.h \ @@ -108,14 +108,14 @@ myhtopheaders = \ ScreenManager.h \ Settings.h \ SignalsPanel.h \ - StringUtils.h \ SwapMeter.h \ TasksMeter.h \ TraceScreen.h \ UptimeMeter.h \ UsersTable.h \ Vector.h \ - XAlloc.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/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/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 e15e6c8d2..0ebe16d33 100644 --- a/SignalsPanel.c +++ b/SignalsPanel.c @@ -16,7 +16,7 @@ in the source distribution for its full text. #include "ListItem.h" #include "Platform.h" #include "RichString.h" -#include "StringUtils.h" +#include "XUtils.h" Panel* SignalsPanel_new() { diff --git a/TasksMeter.c b/TasksMeter.c index ef7e49642..a13766ed0 100644 --- a/TasksMeter.c +++ b/TasksMeter.c @@ -9,7 +9,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" -#include "StringUtils.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 c8e511f1a..1011aa108 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -9,7 +9,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" static const int UptimeMeter_attributes[] = { diff --git a/UsersTable.c b/UsersTable.c index 208fc8d84..e37c90382 100644 --- a/UsersTable.c +++ b/UsersTable.c @@ -17,8 +17,8 @@ in the source distribution for its full text. #include #include -#include "StringUtils.h" #include "XAlloc.h" +#include "XUtils.h" UsersTable* UsersTable_new() { diff --git a/StringUtils.c b/XUtils.c similarity index 99% rename from StringUtils.c rename to XUtils.c index 4eb769680..9228c5e83 100644 --- a/StringUtils.c +++ b/XUtils.c @@ -7,7 +7,7 @@ in the source distribution for its full text. #include "config.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include diff --git a/StringUtils.h b/XUtils.h similarity index 95% rename from StringUtils.h rename to XUtils.h index 5a1af9ad9..4880d1a15 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 diff --git a/htop.c b/htop.c index 9ce966f74..39aeec1a7 100644 --- a/htop.c +++ b/htop.c @@ -16,9 +16,9 @@ in the source distribution for its full text. #include "ProcessList.h" #include "ScreenManager.h" #include "Settings.h" -#include "StringUtils.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 ce7f01e44..cc1b3916f 100644 --- a/linux/IOPriorityPanel.c +++ b/linux/IOPriorityPanel.c @@ -7,7 +7,7 @@ in the source distribution for its full text. #include "IOPriorityPanel.h" -#include "StringUtils.h" +#include "XUtils.h" Panel* IOPriorityPanel_new(IOPriority currPrio) { diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 9e3c2f89f..75900b5a2 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -11,7 +11,7 @@ in the source distribution for its full text. #include "LinuxProcess.h" #include "CRT.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include @@ -20,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 518e66c8d..4733415fe 100644 --- a/linux/PressureStallMeter.c +++ b/linux/PressureStallMeter.c @@ -12,7 +12,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" static const int PressureStallMeter_attributes[] = { diff --git a/zfs/ZfsCompressedArcMeter.c b/zfs/ZfsCompressedArcMeter.c index def88d020..0153fccbd 100644 --- a/zfs/ZfsCompressedArcMeter.c +++ b/zfs/ZfsCompressedArcMeter.c @@ -10,7 +10,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Platform.h" -#include "StringUtils.h" +#include "XUtils.h" #include #include From 5e4b1826168b74d8b5e71227ded12980efd5a243 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Thu, 15 Oct 2020 00:56:22 +0200 Subject: [PATCH 3/4] Combine XAlloc.[ch] into XUtils.[ch] --- FunctionBar.c | 1 - Hashtable.c | 2 +- Makefile.am | 2 -- Object.h | 3 ++- RichString.c | 3 ++- UsersTable.c | 1 - XAlloc.c | 42 ------------------------------------------ XAlloc.h | 22 ---------------------- XUtils.c | 31 ++++++++++++++++++++++++++++++- XUtils.h | 11 +++++++++++ 10 files changed, 46 insertions(+), 72 deletions(-) delete mode 100644 XAlloc.c delete mode 100644 XAlloc.h diff --git a/FunctionBar.c b/FunctionBar.c index 4e9d30cda..50bae0b14 100644 --- a/FunctionBar.c +++ b/FunctionBar.c @@ -8,7 +8,6 @@ in the source distribution for its full text. #include "FunctionBar.h" #include "CRT.h" #include "RichString.h" -#include "XAlloc.h" #include "XUtils.h" #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/Makefile.am b/Makefile.am index 0c3b68c01..fd02bb3e1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,7 +62,6 @@ myhtopsources = \ UptimeMeter.c \ UsersTable.c \ Vector.c \ - XAlloc.c \ XUtils.c myhtopheaders = \ @@ -114,7 +113,6 @@ myhtopheaders = \ UptimeMeter.h \ UsersTable.h \ Vector.h \ - XAlloc.h \ XUtils.h # Linux 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/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/UsersTable.c b/UsersTable.c index e37c90382..41e52d3f3 100644 --- a/UsersTable.c +++ b/UsersTable.c @@ -17,7 +17,6 @@ in the source distribution for its full text. #include #include -#include "XAlloc.h" #include "XUtils.h" diff --git a/XAlloc.c b/XAlloc.c deleted file mode 100644 index 815cf47f9..000000000 --- a/XAlloc.c +++ /dev/null @@ -1,42 +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; -} diff --git a/XAlloc.h b/XAlloc.h deleted file mode 100644 index 98a422fc2..000000000 --- a/XAlloc.h +++ /dev/null @@ -1,22 +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); - -#endif diff --git a/XUtils.c b/XUtils.c index 9228c5e83..f654b4bca 100644 --- a/XUtils.c +++ b/XUtils.c @@ -14,9 +14,38 @@ in the source distribution for its full text. #include #include -#include "XAlloc.h" +#include "CRT.h" +void fail() { + CRT_done(); + 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; +} + char* String_cat(const char* s1, const char* s2) { int l1 = strlen(s1); int l2 = strlen(s2); diff --git a/XUtils.h b/XUtils.h index 4880d1a15..742b8e9eb 100644 --- a/XUtils.h +++ b/XUtils.h @@ -7,11 +7,22 @@ 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) From c138d148974ab383044d1eec9922127faeb020f0 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Thu, 15 Oct 2020 07:38:28 +0200 Subject: [PATCH 4/4] Release old memory on error Avoids leaking memory upon realloc failure. --- XUtils.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/XUtils.c b/XUtils.c index f654b4bca..4fccec3ab 100644 --- a/XUtils.c +++ b/XUtils.c @@ -13,6 +13,7 @@ in the source distribution for its full text. #include #include #include +#include #include "CRT.h" @@ -20,6 +21,8 @@ in the source distribution for its full text. void fail() { CRT_done(); abort(); + + _exit(1); // Should never reach here } void* xMalloc(size_t size) { @@ -39,8 +42,13 @@ void* xCalloc(size_t nmemb, size_t size) { } void* xRealloc(void* ptr, size_t size) { - void* data = realloc(ptr, size); - if (!data && size > 0) { + 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;