summaryrefslogtreecommitdiffstats
path: root/src/libnetdata/inlined.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/libnetdata/inlined.h (renamed from libnetdata/inlined.h)125
1 files changed, 110 insertions, 15 deletions
diff --git a/libnetdata/inlined.h b/src/libnetdata/inlined.h
index 535b791e3..6b71590c9 100644
--- a/libnetdata/inlined.h
+++ b/src/libnetdata/inlined.h
@@ -204,12 +204,28 @@ static inline long long str2ll(const char *s, char **endptr) {
}
}
+static inline uint32_t str2uint32_hex(const char *src, char **endptr) {
+ uint32_t num = 0;
+ const unsigned char *s = (const unsigned char *)src;
+ unsigned char c;
+
+ while ((c = hex_value_from_ascii[(uint8_t)*s]) != 255) {
+ num = (num << 4) | c;
+ s++;
+ }
+
+ if(endptr)
+ *endptr = (char *)s;
+
+ return num;
+}
+
static inline uint64_t str2uint64_hex(const char *src, char **endptr) {
uint64_t num = 0;
const unsigned char *s = (const unsigned char *)src;
unsigned char c;
- while ((c = hex_value_from_ascii[toupper(*s)]) != 255) {
+ while ((c = hex_value_from_ascii[(uint8_t)*s]) != 255) {
num = (num << 4) | c;
s++;
}
@@ -437,7 +453,7 @@ static inline bool sanitize_command_argument_string(char *dst, const char *src,
if (dst_size < 1)
return false;
- if (iscntrl(*src) || *src == '$') {
+ if (iscntrl((uint8_t)*src) || *src == '$') {
// remove control characters and characters that are expanded by bash
*dst++ = '_';
dst_size--;
@@ -469,16 +485,16 @@ static inline bool sanitize_command_argument_string(char *dst, const char *src,
return true;
}
-static inline int read_file(const char *filename, char *buffer, size_t size) {
+static inline int read_txt_file(const char *filename, char *buffer, size_t size) {
if(unlikely(!size)) return 3;
- int fd = open(filename, O_RDONLY, 0666);
+ int fd = open(filename, O_RDONLY | O_CLOEXEC, 0666);
if(unlikely(fd == -1)) {
buffer[0] = '\0';
return 1;
}
- ssize_t r = read(fd, buffer, size);
+ ssize_t r = read(fd, buffer, size - 1); // leave space of the final zero
if(unlikely(r == -1)) {
buffer[0] = '\0';
close(fd);
@@ -490,10 +506,43 @@ static inline int read_file(const char *filename, char *buffer, size_t size) {
return 0;
}
+static inline int read_proc_cmdline(const char *filename, char *buffer, size_t size) {
+ if (unlikely(!size)) return 3;
+
+ int fd = open(filename, O_RDONLY | O_CLOEXEC, 0666);
+ if (unlikely(fd == -1)) {
+ buffer[0] = '\0';
+ return 1;
+ }
+
+ ssize_t r = read(fd, buffer, size - 1); // Leave space for final null character
+ if (unlikely(r == -1)) {
+ buffer[0] = '\0';
+ close(fd);
+ return 2;
+ }
+
+ if (r > 0) {
+ // Replace null characters with spaces, except for the last one
+ for (ssize_t i = 0; i < r - 1; i++) {
+ if (buffer[i] == '\0') {
+ buffer[i] = ' ';
+ }
+ }
+ buffer[r] = '\0'; // Null-terminate the string
+ }
+ else {
+ buffer[0] = '\0'; // Empty cmdline
+ }
+
+ close(fd);
+ return 0;
+}
+
static inline int read_single_number_file(const char *filename, unsigned long long *result) {
char buffer[30 + 1];
- int ret = read_file(filename, buffer, 30);
+ int ret = read_txt_file(filename, buffer, sizeof(buffer));
if(unlikely(ret)) {
*result = 0;
return ret;
@@ -507,7 +556,7 @@ static inline int read_single_number_file(const char *filename, unsigned long lo
static inline int read_single_signed_number_file(const char *filename, long long *result) {
char buffer[30 + 1];
- int ret = read_file(filename, buffer, 30);
+ int ret = read_txt_file(filename, buffer, sizeof(buffer));
if(unlikely(ret)) {
*result = 0;
return ret;
@@ -521,7 +570,7 @@ static inline int read_single_signed_number_file(const char *filename, long long
static inline int read_single_base64_or_hex_number_file(const char *filename, unsigned long long *result) {
char buffer[30 + 1];
- int ret = read_file(filename, buffer, 30);
+ int ret = read_txt_file(filename, buffer, sizeof(buffer));
if(unlikely(ret)) {
*result = 0;
return ret;
@@ -548,7 +597,7 @@ static inline char *strsep_skip_consecutive_separators(char **ptr, char *s) {
// remove leading and trailing spaces; may return NULL
static inline char *trim(char *s) {
// skip leading spaces
- while (*s && isspace(*s)) s++;
+ while (*s && isspace((uint8_t)*s)) s++;
if (!*s) return NULL;
// skip tailing spaces
@@ -556,7 +605,7 @@ static inline char *trim(char *s) {
ssize_t l = (ssize_t)strlen(s);
if (--l >= 0) {
char *p = s + l;
- while (p > s && isspace(*p)) p--;
+ while (p > s && isspace((uint8_t)*p)) p--;
*++p = '\0';
}
@@ -570,31 +619,77 @@ static inline char *trim_all(char *buffer) {
char *d = buffer, *s = buffer;
// skip spaces
- while(isspace(*s)) s++;
+ while(isspace((uint8_t)*s)) s++;
while(*s) {
// copy the non-space part
- while(*s && !isspace(*s)) *d++ = *s++;
+ while(*s && !isspace((uint8_t)*s)) *d++ = *s++;
// add a space if we have to
- if(*s && isspace(*s)) {
+ if(*s && isspace((uint8_t)*s)) {
*d++ = ' ';
s++;
}
// skip spaces
- while(isspace(*s)) s++;
+ while(isspace((uint8_t)*s)) s++;
}
*d = '\0';
if(d > buffer) {
d--;
- if(isspace(*d)) *d = '\0';
+ if(isspace((uint8_t)*d)) *d = '\0';
}
if(!buffer[0]) return NULL;
return buffer;
}
+static inline bool streq(const char *a, const char *b) {
+ if (a == b)
+ return true;
+
+ if (a == NULL || b == NULL)
+ return false;
+
+ return strcmp(a, b) == 0;
+}
+
+static inline bool strstartswith(const char *string, const char *prefix) {
+ if (string == NULL || prefix == NULL)
+ return false;
+
+ size_t string_len = strlen(string);
+ size_t prefix_len = strlen(prefix);
+
+ if (prefix_len > string_len)
+ return false;
+
+ return strncmp(string, prefix, prefix_len) == 0;
+}
+
+static inline bool strendswith(const char *string, const char *suffix) {
+ if (string == NULL || suffix == NULL)
+ return false;
+
+ size_t string_len = strlen(string);
+ size_t suffix_len = strlen(suffix);
+
+ if (suffix_len > string_len)
+ return false;
+
+ return strcmp(string + string_len - suffix_len, suffix) == 0;
+}
+
+static inline bool strendswith_lengths(const char *string, size_t string_len, const char *suffix, size_t suffix_len) {
+ if (string == NULL || suffix == NULL)
+ return false;
+
+ if (suffix_len > string_len)
+ return false;
+
+ return strcmp(string + string_len - suffix_len, suffix) == 0;
+}
+
#endif //NETDATA_INLINED_H