htop/XUtils.c

263 lines
5.2 KiB
C
Raw Permalink Normal View History

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
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.
*/
#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"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
2006-03-04 18:16:49 +00:00
#include <string.h>
#include <unistd.h>
2020-10-14 22:56:22 +00:00
#include "CRT.h"
2006-03-04 18:16:49 +00:00
2020-10-14 22:56:22 +00:00
void fail() {
CRT_done();
abort();
_exit(1); // Should never reach here
2020-10-14 22:56:22 +00:00
}
void* xMalloc(size_t size) {
assert(size > 0);
2020-10-14 22:56:22 +00:00
void* data = malloc(size);
if (!data) {
2020-10-14 22:56:22 +00:00
fail();
}
return data;
}
void* xCalloc(size_t nmemb, size_t size) {
assert(nmemb > 0);
assert(size > 0);
2020-10-14 22:56:22 +00:00
void* data = calloc(nmemb, size);
if (!data) {
2020-10-14 22:56:22 +00:00
fail();
}
return data;
}
void* xRealloc(void* ptr, size_t size) {
assert(size > 0);
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;
}
2010-02-25 01:43:18 +00:00
char* String_cat(const char* s1, const char* s2) {
const size_t l1 = strlen(s1);
const size_t l2 = strlen(s2);
2016-02-02 14:53:02 +00:00
char* out = xMalloc(l1 + l2 + 1);
memcpy(out, s1, l1);
2020-10-31 22:28:02 +00:00
memcpy(out + l1, s2, l2);
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++;
}
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--;
}
return xStrndup(in, len);
2006-03-04 18:16:49 +00:00
}
char** String_split(const char* s, char sep, size_t* n) {
const unsigned int rate = 10;
2016-02-02 14:53:02 +00:00
char** out = xCalloc(rate, sizeof(char*));
size_t ctr = 0;
unsigned int blocks = rate;
const char* where;
2006-03-04 18:16:49 +00:00
while ((where = strchr(s, sep)) != NULL) {
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;
out = (char**) xRealloc(out, sizeof(char*) * blocks);
2006-03-04 18:16:49 +00:00
}
s += size + 1;
}
if (s[0] != '\0') {
out[ctr] = xStrdup(s);
2006-03-04 18:16:49 +00:00
ctr++;
}
out = xRealloc(out, sizeof(char*) * (ctr + 1));
2006-03-04 18:16:49 +00:00
out[ctr] = NULL;
if (n)
*n = ctr;
2006-03-04 18:16:49 +00:00
return out;
}
void String_freeArray(char** s) {
if (!s) {
return;
}
for (size_t i = 0; s[i] != NULL; i++) {
2006-03-04 18:16:49 +00:00
free(s[i]);
}
free(s);
}
char* String_getToken(const char* line, const unsigned short int numMatch) {
const size_t len = strlen(line);
char inWord = 0;
unsigned short int count = 0;
char match[50];
size_t foundCount = 0;
for (size_t i = 0; i < len; i++) {
char lastState = inWord;
2020-10-31 22:28:02 +00:00
inWord = line[i] == ' ' ? 0 : 1;
if (lastState == 0 && inWord == 1)
count++;
2019-10-31 16:39:12 +00:00
2020-10-31 22:28:02 +00:00
if (inWord == 1) {
if (count == numMatch && line[i] != ' ' && line[i] != '\0' && line[i] != '\n' && line[i] != (char)EOF) {
match[foundCount] = line[i];
foundCount++;
}
}
}
match[foundCount] = '\0';
2020-09-28 10:17:52 +00:00
return xStrdup(match);
}
char* String_readLine(FILE* fd) {
const unsigned int step = 1024;
unsigned int bufSize = step;
char* buffer = xMalloc(step + 1);
char* at = buffer;
for (;;) {
char* ok = fgets(at, step + 1, fd);
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;
}
}
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;
}
char* xStrndup(const char* str, size_t len) {
char* data = strndup(str, len);
if (!data) {
fail();
}
return data;
}
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);
}