diff options
author | Lennart Weller <lhw@ring0.de> | 2017-09-17 22:17:33 +0000 |
---|---|---|
committer | Lennart Weller <lhw@ring0.de> | 2017-09-17 22:17:33 +0000 |
commit | 6aaf5ba7ed0980c14bdc554fc8839a2126455ed5 (patch) | |
tree | 6161925716661486e7f47c479668a9487b039d83 /src/inlined.h | |
parent | New upstream version 1.7.0+dfsg (diff) | |
download | netdata-6aaf5ba7ed0980c14bdc554fc8839a2126455ed5.tar.xz netdata-6aaf5ba7ed0980c14bdc554fc8839a2126455ed5.zip |
New upstream version 1.8.0+dfsgupstream/1.8.0+dfsg
Diffstat (limited to 'src/inlined.h')
-rw-r--r-- | src/inlined.h | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/inlined.h b/src/inlined.h index f1812ba1..9ab2dca7 100644 --- a/src/inlined.h +++ b/src/inlined.h @@ -244,26 +244,48 @@ static inline char *strncpyz(char *dst, const char *src, size_t n) { return p; } -static inline int read_single_number_file(const char *filename, unsigned long long *result) { - char buffer[30 + 1]; - +static inline int read_file(const char *filename, char *buffer, size_t size) { int fd = open(filename, O_RDONLY, 0666); - if(unlikely(fd == -1)) { - *result = 0; + if(unlikely(fd == -1)) return 1; - } - ssize_t r = read(fd, buffer, 30); + ssize_t r = read(fd, buffer, size); if(unlikely(r == -1)) { - *result = 0; close(fd); return 2; } + buffer[r] = '\0'; 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); + if(unlikely(ret)) { + *result = 0; + return ret; + } + buffer[30] = '\0'; *result = str2ull(buffer); return 0; } +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); + if(unlikely(ret)) { + *result = 0; + return ret; + } + + buffer[30] = '\0'; + *result = atoll(buffer); + return 0; +} + #endif //NETDATA_INLINED_H |