diff --git a/AffinityPanel.c b/AffinityPanel.c index 0a964882..0027d4a4 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 dcfbb3e2..a2a6aba6 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 b51a9e9f..b90912bd 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 446c04d2..c3133a2c 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 917be98c..45745a73 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 8393af70..34b0f081 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 1aede655..d6534887 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 c67e613c..3e00b87e 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 208b7e95..507d3dc3 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 933a21ba..e15e6c8d 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 +#include +#include +#include #include "ListItem.h" +#include "Platform.h" #include "RichString.h" - -#include -#include -#include - -#include +#include "StringUtils.h" Panel* SignalsPanel_new() { diff --git a/StringUtils.c b/StringUtils.c index f74566f8..4eb76968 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 936617ff..5a1af9ad 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 fb3767c5..ef7e4964 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 a1195186..c8e511f1 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 722dd6e1..208fc8d8 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 c832d087..815cf47f 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 97c15519..98a422fc 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 8c813564..9ce966f7 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 a12b8065..ce7f01e4 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 68baf96d..9e3c2f89 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 307e397d..518e66c8 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 472b5b5b..def88d02 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