2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2015-08-19 16:58:29 +00:00
|
|
|
htop - StringUtils.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2021-09-22 09:33:00 +00:00
|
|
|
Released under the GNU GPLv2+, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2020-09-19 18:22:34 +00:00
|
|
|
|
2020-10-19 10:05:06 +00:00
|
|
|
#include <assert.h>
|
2020-11-25 11:42:36 +00:00
|
|
|
#include <errno.h>
|
2020-12-06 14:22:41 +00:00
|
|
|
#include <fcntl.h>
|
2020-09-19 18:22:34 +00:00
|
|
|
#include <stdarg.h>
|
2020-12-23 20:52:40 +00:00
|
|
|
#include <stdint.h>
|
2020-09-19 18:22:34 +00:00
|
|
|
#include <stdlib.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <string.h>
|
2020-10-15 05:38:28 +00:00
|
|
|
#include <unistd.h>
|
2020-09-19 18:22:34 +00:00
|
|
|
|
2020-10-14 22:56:22 +00:00
|
|
|
#include "CRT.h"
|
2020-09-19 18:22:34 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-10-14 22:56:22 +00:00
|
|
|
void fail() {
|
|
|
|
CRT_done();
|
|
|
|
abort();
|
2020-10-15 05:38:28 +00:00
|
|
|
|
|
|
|
_exit(1); // Should never reach here
|
2020-10-14 22:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* xMalloc(size_t size) {
|
2020-10-05 10:49:01 +00:00
|
|
|
assert(size > 0);
|
2020-10-14 22:56:22 +00:00
|
|
|
void* data = malloc(size);
|
2020-10-05 10:49:01 +00:00
|
|
|
if (!data) {
|
2020-10-14 22:56:22 +00:00
|
|
|
fail();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-12-23 20:52:40 +00:00
|
|
|
void* xMallocArray(size_t nmemb, size_t size) {
|
|
|
|
assert(nmemb > 0);
|
|
|
|
assert(size > 0);
|
|
|
|
if (SIZE_MAX / nmemb < size) {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
return xMalloc(nmemb * size);
|
|
|
|
}
|
|
|
|
|
2020-10-14 22:56:22 +00:00
|
|
|
void* xCalloc(size_t nmemb, size_t size) {
|
2020-10-05 10:49:01 +00:00
|
|
|
assert(nmemb > 0);
|
|
|
|
assert(size > 0);
|
2020-12-23 20:52:40 +00:00
|
|
|
if (SIZE_MAX / nmemb < size) {
|
|
|
|
fail();
|
|
|
|
}
|
2020-10-14 22:56:22 +00:00
|
|
|
void* data = calloc(nmemb, size);
|
2020-10-05 10:49:01 +00:00
|
|
|
if (!data) {
|
2020-10-14 22:56:22 +00:00
|
|
|
fail();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* xRealloc(void* ptr, size_t size) {
|
2020-10-05 10:49:01 +00:00
|
|
|
assert(size > 0);
|
2020-10-15 05:38:28 +00:00
|
|
|
void* data = realloc(ptr, size); // deepcode ignore MemoryLeakOnRealloc: this goes to fail()
|
|
|
|
if (!data) {
|
|
|
|
free(ptr);
|
2020-10-14 22:56:22 +00:00
|
|
|
fail();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-12-23 20:52:40 +00:00
|
|
|
void* xReallocArray(void* ptr, size_t nmemb, size_t size) {
|
|
|
|
assert(nmemb > 0);
|
|
|
|
assert(size > 0);
|
|
|
|
if (SIZE_MAX / nmemb < size) {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
return xRealloc(ptr, nmemb * size);
|
|
|
|
}
|
|
|
|
|
2021-09-10 13:09:42 +00:00
|
|
|
void* xReallocArrayZero(void* ptr, size_t prevmemb, size_t newmemb, size_t size) {
|
|
|
|
assert((ptr == NULL) == (prevmemb == 0));
|
|
|
|
|
|
|
|
if (prevmemb == newmemb) {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* ret = xReallocArray(ptr, newmemb, size);
|
|
|
|
|
|
|
|
if (newmemb > prevmemb) {
|
|
|
|
memset((unsigned char*)ret + prevmemb * size, '\0', (newmemb - prevmemb) * size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-25 15:24:24 +00:00
|
|
|
inline bool String_contains_i(const char* s1, const char* s2, bool multi) {
|
|
|
|
// we have a multi-string search term, handle as special case for performance reasons
|
|
|
|
if (multi && strstr(s2, "|")) {
|
|
|
|
size_t nNeedles;
|
|
|
|
char** needles = String_split(s2, '|', &nNeedles);
|
|
|
|
for (size_t i = 0; i < nNeedles; i++) {
|
|
|
|
if (strcasestr(s1, needles[i]) != NULL) {
|
|
|
|
String_freeArray(needles);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String_freeArray(needles);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return strcasestr(s1, s2) != NULL;
|
|
|
|
}
|
2021-08-24 15:27:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
char* String_cat(const char* s1, const char* s2) {
|
2020-10-03 19:20:43 +00:00
|
|
|
const size_t l1 = strlen(s1);
|
|
|
|
const size_t l2 = strlen(s2);
|
2022-04-30 12:51:10 +00:00
|
|
|
if (SIZE_MAX - l1 <= l2) {
|
|
|
|
fail();
|
|
|
|
}
|
2016-02-02 14:53:02 +00:00
|
|
|
char* out = xMalloc(l1 + l2 + 1);
|
2020-08-19 23:03:45 +00:00
|
|
|
memcpy(out, s1, l1);
|
2020-10-31 22:28:02 +00:00
|
|
|
memcpy(out + l1, s2, l2);
|
2020-08-19 23:03:45 +00:00
|
|
|
out[l1 + l2] = '\0';
|
2006-03-04 18:16:49 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
char* String_trim(const char* in) {
|
2006-03-04 18:16:49 +00:00
|
|
|
while (in[0] == ' ' || in[0] == '\t' || in[0] == '\n') {
|
|
|
|
in++;
|
|
|
|
}
|
2020-10-03 19:20:43 +00:00
|
|
|
|
|
|
|
size_t len = strlen(in);
|
2020-10-31 22:28:02 +00:00
|
|
|
while (len > 0 && (in[len - 1] == ' ' || in[len - 1] == '\t' || in[len - 1] == '\n')) {
|
2006-03-04 18:16:49 +00:00
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:20:43 +00:00
|
|
|
return xStrndup(in, len);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:20:43 +00:00
|
|
|
char** String_split(const char* s, char sep, size_t* n) {
|
2022-01-11 17:55:41 +00:00
|
|
|
const size_t rate = 10;
|
2016-02-02 14:53:02 +00:00
|
|
|
char** out = xCalloc(rate, sizeof(char*));
|
2020-10-03 19:20:43 +00:00
|
|
|
size_t ctr = 0;
|
2022-01-11 17:55:41 +00:00
|
|
|
size_t blocks = rate;
|
2020-10-03 19:20:43 +00:00
|
|
|
const char* where;
|
2006-03-04 18:16:49 +00:00
|
|
|
while ((where = strchr(s, sep)) != NULL) {
|
2020-10-03 19:20:43 +00:00
|
|
|
size_t size = (size_t)(where - s);
|
|
|
|
out[ctr] = xStrndup(s, size);
|
2006-03-04 18:16:49 +00:00
|
|
|
ctr++;
|
|
|
|
if (ctr == blocks) {
|
|
|
|
blocks += rate;
|
2016-05-30 15:22:33 +00:00
|
|
|
out = (char**) xRealloc(out, sizeof(char*) * blocks);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
s += size + 1;
|
|
|
|
}
|
|
|
|
if (s[0] != '\0') {
|
2020-08-20 19:34:28 +00:00
|
|
|
out[ctr] = xStrdup(s);
|
2006-03-04 18:16:49 +00:00
|
|
|
ctr++;
|
|
|
|
}
|
2016-05-30 15:22:33 +00:00
|
|
|
out = xRealloc(out, sizeof(char*) * (ctr + 1));
|
2006-03-04 18:16:49 +00:00
|
|
|
out[ctr] = NULL;
|
2020-10-03 19:20:43 +00:00
|
|
|
|
|
|
|
if (n)
|
|
|
|
*n = ctr;
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String_freeArray(char** s) {
|
2016-08-24 21:12:35 +00:00
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-03 19:20:43 +00:00
|
|
|
for (size_t i = 0; s[i] != NULL; i++) {
|
2006-03-04 18:16:49 +00:00
|
|
|
free(s[i]);
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2016-06-19 21:55:35 +00:00
|
|
|
char* String_readLine(FILE* fd) {
|
2022-01-11 17:55:41 +00:00
|
|
|
const size_t step = 1024;
|
|
|
|
size_t bufSize = step;
|
2016-06-19 21:55:35 +00:00
|
|
|
char* buffer = xMalloc(step + 1);
|
|
|
|
char* at = buffer;
|
|
|
|
for (;;) {
|
2021-01-05 22:42:55 +00:00
|
|
|
const char* ok = fgets(at, step + 1, fd);
|
2016-06-19 21:55:35 +00:00
|
|
|
if (!ok) {
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char* newLine = strrchr(at, '\n');
|
|
|
|
if (newLine) {
|
|
|
|
*newLine = '\0';
|
|
|
|
return buffer;
|
|
|
|
} else {
|
|
|
|
if (feof(fd)) {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bufSize += step;
|
|
|
|
buffer = xRealloc(buffer, bufSize + 1);
|
|
|
|
at = buffer + bufSize - step;
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 18:22:34 +00:00
|
|
|
|
2021-07-14 17:24:18 +00:00
|
|
|
size_t String_safeStrncpy(char* restrict dest, const char* restrict src, size_t size) {
|
2021-01-05 13:44:09 +00:00
|
|
|
assert(size > 0);
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (; i < size - 1 && src[i]; i++)
|
|
|
|
dest[i] = src[i];
|
|
|
|
|
|
|
|
dest[i] = '\0';
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:22:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-24 17:14:56 +00:00
|
|
|
int xSnprintf(char* buf, size_t len, const char* fmt, ...) {
|
2020-09-19 18:22:34 +00:00
|
|
|
va_list vl;
|
|
|
|
va_start(vl, fmt);
|
|
|
|
int n = vsnprintf(buf, len, fmt, vl);
|
|
|
|
va_end(vl);
|
|
|
|
|
2020-11-24 17:14:56 +00:00
|
|
|
if (n < 0 || (size_t)n >= len) {
|
2020-09-19 18:22:34 +00:00
|
|
|
fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* xStrdup(const char* str) {
|
|
|
|
char* data = strdup(str);
|
|
|
|
if (!data) {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2020-10-03 19:20:43 +00:00
|
|
|
|
2021-01-05 13:47:49 +00:00
|
|
|
void free_and_xStrdup(char** ptr, const char* str) {
|
|
|
|
if (*ptr && String_eq(*ptr, str))
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(*ptr);
|
|
|
|
*ptr = xStrdup(str);
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:20:43 +00:00
|
|
|
char* xStrndup(const char* str, size_t len) {
|
|
|
|
char* data = strndup(str, len);
|
|
|
|
if (!data) {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2020-11-25 11:42:36 +00:00
|
|
|
|
|
|
|
static ssize_t readfd_internal(int fd, void* buffer, size_t count) {
|
|
|
|
if (!count) {
|
|
|
|
close(fd);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t alreadyRead = 0;
|
|
|
|
count--; // reserve one for null-terminator
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ssize_t res = read(fd, buffer, count);
|
|
|
|
if (res == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res > 0) {
|
|
|
|
buffer = ((char*)buffer) + res;
|
|
|
|
count -= (size_t)res;
|
|
|
|
alreadyRead += res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0 || res == 0) {
|
|
|
|
close(fd);
|
|
|
|
*((char*)buffer) = '\0';
|
|
|
|
return alreadyRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t xReadfile(const char* pathname, void* buffer, size_t count) {
|
|
|
|
int fd = open(pathname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return readfd_internal(fd, buffer, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t xReadfileat(openat_arg_t dirfd, const char* pathname, void* buffer, size_t count) {
|
|
|
|
int fd = Compat_openat(dirfd, pathname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return readfd_internal(fd, buffer, count);
|
|
|
|
}
|