diff options
author | Daniel Lange <DLange@git.local> | 2024-01-10 12:40:37 +0100 |
---|---|---|
committer | Daniel Lange <DLange@git.local> | 2024-01-10 12:40:37 +0100 |
commit | 7271b076b82785ffca73ee9e4ae84cabb77018ee (patch) | |
tree | e8270dd60ec096bee8157dbadf029e15ed584592 /XUtils.c | |
parent | f288666edc9180a2e81e6655951878124f321df6 (diff) | |
download | debian_htop-upstream.tar.gz debian_htop-upstream.tar.bz2 debian_htop-upstream.zip |
New upstream version 3.3.0upstream/3.3.0upstream
Diffstat (limited to 'XUtils.c')
-rw-r--r-- | XUtils.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -12,6 +12,7 @@ in the source distribution for its full text. #include <assert.h> #include <errno.h> #include <fcntl.h> +#include <math.h> #include <stdarg.h> #include <stdint.h> #include <stdlib.h> @@ -19,6 +20,7 @@ in the source distribution for its full text. #include <unistd.h> #include "CRT.h" +#include "Macros.h" void fail(void) { @@ -265,6 +267,7 @@ char* xStrndup(const char* str, size_t len) { return data; } +ATTR_ACCESS3_W(2, 3) static ssize_t readfd_internal(int fd, void* buffer, size_t count) { if (!count) { close(fd); @@ -281,10 +284,13 @@ static ssize_t readfd_internal(int fd, void* buffer, size_t count) { continue; close(fd); + *((char*)buffer) = '\0'; return -errno; } if (res > 0) { + assert((size_t)res <= count); + buffer = ((char*)buffer) + res; count -= (size_t)res; alreadyRead += res; @@ -336,3 +342,25 @@ ssize_t full_write(int fd, const void* buf, size_t count) { return written; } + +/* Compares floating point values for ordering data entries. In this function, + NaN is considered "less than" any other floating point value (regardless of + sign), and two NaNs are considered "equal" regardless of payload. */ +int compareRealNumbers(double a, double b) { + int result = isgreater(a, b) - isgreater(b, a); + if (result) + return result; + return !isNaN(a) - !isNaN(b); +} + +/* Computes the sum of all positive floating point values in an array. + NaN values in the array are skipped. The returned sum will always be + nonnegative. */ +double sumPositiveValues(const double* array, size_t count) { + double sum = 0.0; + for (size_t i = 0; i < count; i++) { + if (isPositive(array[i])) + sum += array[i]; + } + return sum; +} |