diff options
Diffstat (limited to '')
-rw-r--r-- | src/libnetdata/inlined.h (renamed from libnetdata/inlined.h) | 109 |
1 files changed, 102 insertions, 7 deletions
diff --git a/libnetdata/inlined.h b/src/libnetdata/inlined.h index 535b791e3..9c0d2dd0b 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++; } @@ -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; @@ -597,4 +646,50 @@ static inline char *trim_all(char *buffer) { 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 |