diff options
Diffstat (limited to 'src/libsystem/libsystem.c')
-rw-r--r-- | src/libsystem/libsystem.c | 424 |
1 files changed, 303 insertions, 121 deletions
diff --git a/src/libsystem/libsystem.c b/src/libsystem/libsystem.c index 2a8aced..c8b7c58 100644 --- a/src/libsystem/libsystem.c +++ b/src/libsystem/libsystem.c @@ -28,17 +28,19 @@ #include <sys/stat.h> #include <fcntl.h> #include <mntent.h> +#include <stdint.h> +#include <inttypes.h> #include "libsystem.h" static int _errno_old; -#define STORE_RESET_ERRNO do { \ +#define STORE_RESET_ERRNO do { \ _errno_old = errno; \ errno = 0; \ } while (0) -#define RESTORE_ERRNO do { \ +#define RESTORE_ERRNO do { \ errno = _errno_old; \ _errno_old = 0; \ } while (0) @@ -81,16 +83,16 @@ char *strnappend(const char *s, const char *suffix, size_t b) { assert(suffix); a = strlen(s); - if (b > ((size_t) -1) - a) + if (b > ((size_t) - 1) - a) return NULL; - r = new(char, a+b+1); + r = new(char, a + b + 1); if (!r) return NULL; memcpy(r, s, a); - memcpy(r+a, suffix, b); - r[a+b] = 0; + memcpy(r + a, suffix, b); + r[a + b] = 0; return r; } @@ -107,7 +109,7 @@ char *strstrip(char *s) { s += strspn(s, WHITESPACE); - for (e = strchr(s, 0); e > s; e --) + for (e = strchr(s, 0); e > s; e--) if (!strchr(WHITESPACE, e[-1])) break; @@ -166,7 +168,7 @@ int strndup_strip(const char *str, size_t len, char **ret) { return 0; } -bool nulstr_contains(const char*nulstr, const char *needle) { +bool nulstr_contains(const char *nulstr, const char *needle) { const char *i; if (!nulstr) @@ -221,7 +223,7 @@ char *path_kill_slashes(char *path) { return path; } -char* endswith(const char *s, const char *postfix) { +char *endswith(const char *s, const char *postfix) { size_t sl, pl; assert(s); @@ -231,7 +233,7 @@ char* endswith(const char *s, const char *postfix) { pl = strlen(postfix); if (pl == 0) - return (char*) s + sl; + return (char *) s + sl; if (sl < pl) return NULL; @@ -239,7 +241,7 @@ char* endswith(const char *s, const char *postfix) { if (memcmp(s + sl - pl, postfix, pl) != 0) return NULL; - return (char*) s + sl - pl; + return (char *) s + sl - pl; } int parse_boolean(const char *v) { @@ -265,7 +267,7 @@ int parse_bytes(const char *b, size_t *s) { return 0; num_l = strspn(b, "0123456789"); - if (num_l < len-1) + if (num_l < len - 1) return -EINVAL; unit_l = strcspn(b, "BKMG"); @@ -306,11 +308,11 @@ int parse_percent(const char *string, size_t *percent) { if (!len) return 0; - if (string[len-1] != '%') + if (string[len - 1] != '%') return -EINVAL; num_len = strspn(string, "0123456789"); - if (num_len < len-1) + if (num_len < len - 1) return -EINVAL; num = strndup(string, num_len); @@ -376,7 +378,7 @@ char *split(const char *c, size_t *l, const char *separator, char **state) { assert(separator); assert(state); - current = *state ? *state : (char*) c; + current = *state ? *state : (char *) c; if (!*current || *c == 0) return NULL; @@ -384,7 +386,7 @@ char *split(const char *c, size_t *l, const char *separator, char **state) { separator_include_quotes = !!strspn(separator, QUOTES); current += strspn(current, separator); - while((s = strcspn(current + *l, separator))) { + while ((s = strcspn(current + *l, separator))) { *l += s; if (separator_include_quotes || quote_complete(current, *l)) @@ -394,7 +396,7 @@ char *split(const char *c, size_t *l, const char *separator, char **state) { *state = current + *l; - return (char *)current; + return (char *) current; } bool is_number(const char *s, int l) { @@ -543,9 +545,8 @@ char *strdup_unquote(const char *str, const char *quotes) { if (l < 2) return strdup(str); - if (strchr(quotes, str[0]) && - str[0] == str[l-1]) - return strndup(str+1, l-2); + if (strchr(quotes, str[0]) && str[0] == str[l - 1]) + return strndup(str + 1, l - 2); return strdup(str); } @@ -559,8 +560,7 @@ int write_str_to_file(FILE *f, const char *str, enum file_write_flags flags) { STORE_RESET_ERRNO; (void) fputs(str, f); - if ((flags & FILE_WRITE_NEWLINE_IF_NOT) && - !endswith(str, "\n")) + if ((flags & FILE_WRITE_NEWLINE_IF_NOT) && !endswith(str, "\n")) (void) fputc('\n', f); if (flags & FILE_WRITE_WITH_FFLUSH) @@ -590,183 +590,365 @@ int write_str_to_path(const char *path, const char *str, enum file_write_flags f return write_str_to_file(f, str, flags); } -int write_int32_to_file(FILE *f, int32_t i, enum file_write_flags flags) { - int r = 0; +int read_one_line_from_file(FILE *f, char **line) { + char t[LINE_MAX], *c; assert(f); + assert(line); STORE_RESET_ERRNO; - (void) fprintf(f, "%d", i); - if (flags & FILE_WRITE_NEWLINE_IF_NOT) - (void) fputc('\n', f); + if (!fgets(t, sizeof(t), f)) { - if (flags & FILE_WRITE_WITH_FFLUSH) - (void) fflush(f); + if (ferror(f)) { + int r; - if (ferror(f)) - r = errno ? -errno : -EIO; + r = errno ? -errno : -EIO; + RESTORE_ERRNO; + return r; + } + + t[0] = 0; + } RESTORE_ERRNO; - return r; + c = strdup(t); + if (!c) + return -ENOMEM; + + *line = truncate_nl(c); + + return 0; } -int write_int32_to_path(const char *path, int32_t i, enum file_write_flags flags) { +int read_one_line_from_path(const char *path, char **line) { _cleanup_fclose_ FILE *f = NULL; assert(path); + assert(line); - if (flags & FILE_WRITE_APPEND) - f = fopen(path, "ae"); - else - f = fopen(path, "we"); + f = fopen(path, "re"); if (!f) return -errno; - return write_int32_to_file(f, i, flags); + return read_one_line_from_file(f, line); } -int write_uint32_to_file(FILE *f, uint32_t u, enum file_write_flags flags) { - int r = 0; +#define DEFINE_WRITE_NUM_TO_FILE(type, format) \ + int write_##type##_to_file(FILE *f, \ + type##_t u, \ + enum file_write_flags flags) { \ + int r = 0; \ + \ + assert(f); \ + \ + STORE_RESET_ERRNO; \ + \ + (void) fprintf(f, format, u); \ + if (flags & FILE_WRITE_NEWLINE_IF_NOT) \ + (void) fputc('\n', f); \ + \ + if (flags & FILE_WRITE_WITH_FFLUSH) \ + (void) fflush(f); \ + \ + if (ferror(f)) \ + r = errno ? -errno : -EIO; \ + \ + RESTORE_ERRNO; \ + \ + return r; \ + } - assert(f); +#define DEFINE_WRITE_NUM_TO_PATH(type) \ + int write_##type##_to_path(const char *path, \ + type##_t u, \ + enum file_write_flags flags) { \ + _cleanup_fclose_ FILE *f = NULL; \ + \ + assert(path); \ + \ + if (flags & FILE_WRITE_APPEND) \ + f = fopen(path, "ae"); \ + else \ + f = fopen(path, "we"); \ + if (!f) \ + return -errno; \ + \ + return write_##type##_to_file(f, u, flags); \ + } - STORE_RESET_ERRNO; +#define DEFINE_WRITE_NUM_DUAL(type, format) \ + DEFINE_WRITE_NUM_TO_FILE(type, format); \ + DEFINE_WRITE_NUM_TO_PATH(type) + +#define DEFINE_READ_NUM_FROM_FILE(type, format) \ + int read_##type##_from_file(FILE *f, type##_t *num) { \ + int r = 0; \ + \ + assert(f); \ + assert(num); \ + \ + STORE_RESET_ERRNO; \ + \ + r = fscanf(f, format, num); \ + if (r == EOF && ferror(f)) \ + r = errno ? -errno : -EOF; \ + \ + RESTORE_ERRNO; \ + \ + return r; \ + } - (void) fprintf(f, "%u", u); - if (flags & FILE_WRITE_NEWLINE_IF_NOT) - (void) fputc('\n', f); +#define DEFINE_READ_NUM_FROM_PATH(type) \ + int read_##type##_from_path(const char *path, type##_t *num) { \ + _cleanup_fclose_ FILE *f = NULL; \ + \ + assert(path); \ + assert(num); \ + \ + f = fopen(path, "re"); \ + if (!f) \ + return -errno; \ + \ + return read_##type##_from_file(f, num); \ + } - if (flags & FILE_WRITE_WITH_FFLUSH) - (void) fflush(f); +#define DEFINE_READ_NUM_DUAL(type, format) \ + DEFINE_READ_NUM_FROM_FILE(type, format); \ + DEFINE_READ_NUM_FROM_PATH(type) - if (ferror(f)) - r = errno ? -errno : -EIO; +#define DEFINE_READ_WRITE_NUM_DUAL(type, r_format, w_format) \ + DEFINE_READ_NUM_DUAL(type, r_format); \ + DEFINE_WRITE_NUM_DUAL(type, w_format) - RESTORE_ERRNO; +DEFINE_READ_WRITE_NUM_DUAL(int32, "%d", "%d"); +DEFINE_READ_WRITE_NUM_DUAL(uint32, "%u", "%u"); +DEFINE_READ_WRITE_NUM_DUAL(int64, "%" SCNd64, "%" PRId64); +DEFINE_READ_WRITE_NUM_DUAL(uint64, "%" SCNu64, "%" PRIu64); - return r; +int write_int_to_file(FILE *f, int num, enum file_write_flags flags) { + + assert(f); + assert(num); + + return write_int32_to_file(f, (int32_t) num, flags); } -int write_uint32_to_path(const char *path, uint32_t u, enum file_write_flags flags) { - _cleanup_fclose_ FILE *f = NULL; +int write_int_to_path(const char *path, int num, enum file_write_flags flags) { assert(path); + assert(num); - if (flags & FILE_WRITE_APPEND) - f = fopen(path, "ae"); - else - f = fopen(path, "we"); - if (!f) - return -errno; + return write_int32_to_path(path, (int32_t) num, flags); +} - return write_uint32_to_file(f, u, flags); +int write_unsigned_int_to_file(FILE *f, unsigned int num, enum file_write_flags flags) { + + assert(f); + assert(num); + + return write_uint32_to_file(f, (uint32_t) num, flags); } -int read_one_line_from_file(FILE *f, char **line) { - char t[LINE_MAX], *c; +int write_unsigned_int_to_path(const char *path, unsigned int num, enum file_write_flags flags) { + + assert(path); + assert(num); + + return write_uint32_to_path(path, (uint32_t) num, flags); +} + +int write_long_int_to_file(FILE *f, long int num, enum file_write_flags flags) { assert(f); - assert(line); + assert(num); - STORE_RESET_ERRNO; +#if __WORDSIZE == 64 + return write_int64_to_file(f, (int64_t) num, flags); +#else + return write_int32_to_file(f, (int32_t) num, flags); +#endif +} - if (!fgets(t, sizeof(t), f)) { +int write_long_int_to_path(const char *path, long int num, enum file_write_flags flags) { - if (ferror(f)) { - int r; + assert(path); + assert(num); - r = errno ? -errno : -EIO; - RESTORE_ERRNO; - return r; - } +#if __WORDSIZE == 64 + return write_int64_to_path(path, (int64_t) num, flags); +#else + return write_int32_to_path(path, (int32_t) num, flags); +#endif +} - t[0] = 0; - } +int write_unsigned_long_int_to_file(FILE *f, unsigned long int num, enum file_write_flags flags) { - RESTORE_ERRNO; + assert(f); + assert(num); - c = strdup(t); - if (!c) - return -ENOMEM; +#if __WORDSIZE == 64 + return write_uint64_to_file(f, (uint64_t) num, flags); +#else + return write_uint32_to_file(f, (uint32_t) num, flags); +#endif +} - *line = truncate_nl(c); +int write_unsigned_long_int_to_path(const char *path, unsigned long int num, enum file_write_flags flags) { - return 0; + assert(path); + assert(num); + +#if __WORDSIZE == 64 + return write_uint64_to_path(path, (uint64_t) num, flags); +#else + return write_uint32_to_path(path, (uint32_t) num, flags); +#endif } -int read_one_line_from_path(const char *path, char **line) { - _cleanup_fclose_ FILE *f = NULL; +int write_long_long_int_to_file(FILE *f, long long int num, enum file_write_flags flags) { + + assert(f); + assert(num); + + return write_int64_to_file(f, (int64_t) num, flags); +} + +int write_long_long_int_to_path(const char *path, long long int num, enum file_write_flags flags) { assert(path); - assert(line); + assert(num); - f = fopen(path, "re"); - if (!f) - return -errno; + return write_int64_to_path(path, (int64_t) num, flags); +} - return read_one_line_from_file(f, line); +int write_unsigned_long_long_int_to_file(FILE *f, unsigned long long int num, enum file_write_flags flags) { + + assert(f); + assert(num); + + return write_uint64_to_file(f, (uint64_t) num, flags); } -int read_int32_from_file(FILE *f, int32_t *i) { - int r = 0; +int write_unsigned_long_long_int_to_path(const char *path, unsigned long long int num, enum file_write_flags flags) { + + assert(path); + assert(num); + + return write_uint64_to_path(path, (uint64_t) num, flags); +} + +int read_int_from_file(FILE *f, int *num) { assert(f); - assert(i); + assert(num); - STORE_RESET_ERRNO; + return read_int32_from_file(f, (int32_t *) num); +} - r = fscanf(f, "%d", i); - if (r == EOF && ferror(f)) - r = errno ? -errno : -EOF; +int read_int_from_path(const char *path, int *num) { - RESTORE_ERRNO; + assert(path); + assert(num); - return r; + return read_int32_from_path(path, (int32_t *) num); } -int read_int32_from_path(const char *path, int32_t *i) { - _cleanup_fclose_ FILE *f = NULL; +int read_unsigned_int_from_file(FILE *f, unsigned int *num) { + + assert(f); + assert(num); + + return read_uint32_from_file(f, (uint32_t *) num); +} + +int read_unsigned_int_from_path(const char *path, unsigned int *num) { assert(path); - assert(i); + assert(num); - f = fopen(path, "re"); - if (!f) - return -errno; + return read_uint32_from_path(path, (uint32_t *) num); +} + +int read_long_int_from_file(FILE *f, long int *num) { + + assert(f); + assert(num); - return read_int32_from_file(f, i); +#if __WORDSIZE == 64 + return read_int64_from_file(f, (int64_t *) num); +#else + return read_int32_from_file(f, (int32_t *) num); +#endif } -int read_uint32_from_file(FILE *f, uint32_t *u) { - int r = 0; +int read_long_int_from_path(const char *path, long int *num) { + + assert(path); + assert(num); + +#if __WORDSIZE == 64 + return read_int64_from_path(path, (int64_t *) num); +#else + return read_int32_from_path(path, (int32_t *) num); +#endif +} + +int read_unsigned_long_int_from_file(FILE *f, unsigned long int *num) { assert(f); - assert(u); + assert(num); - STORE_RESET_ERRNO; +#if __WORDSIZE == 64 + return read_uint64_from_file(f, (uint64_t *) num); +#else + return read_uint32_from_file(f, (uint32_t *) num); +#endif +} - r = fscanf(f, "%u", u); - if (r == EOF && ferror(f)) - r = errno ? -errno : -EOF; +int read_unsigned_long_int_from_path(const char *path, unsigned long int *num) { - RESTORE_ERRNO; + assert(path); + assert(num); - return r; +#if __WORDSIZE == 64 + return read_uint64_from_path(path, (uint64_t *) num); +#else + return read_uint32_from_path(path, (uint32_t *) num); +#endif } -int read_uint32_from_path(const char *path, uint32_t *u) { - _cleanup_fclose_ FILE *f = NULL; +int read_long_long_int_from_file(FILE *f, long long *num) { + + assert(f); + assert(num); + + return read_int64_from_file(f, (int64_t *) num); +} + +int read_long_long_int_from_path(const char *path, long long *num) { assert(path); - assert(u); + assert(num); - f = fopen(path, "re"); - if (!f) - return -errno; + return read_int64_from_path(path, (int64_t *) num); +} + +int read_unsigned_long_long_int_from_file(FILE *f, unsigned long long *num) { + + assert(f); + assert(num); + + return read_uint64_from_file(f, (uint64_t *) num); +} + +int read_unsigned_long_long_int_from_path(const char *path, unsigned long long *num) { + + assert(path); + assert(num); - return read_uint32_from_file(f, u); + return read_uint64_from_path(path, (uint64_t *) num); } int str_to_strv(const char *str, char ***strv, const char *separator) { @@ -782,7 +964,7 @@ int str_to_strv(const char *str, char ***strv, const char *separator) { return -ENOMEM; } - new = (char **)realloc(v, sizeof(char *) * (i + 2)); + new = (char **) realloc(v, sizeof(char *) * (i + 2)); if (!new) { free(p); free(v); @@ -793,7 +975,7 @@ int str_to_strv(const char *str, char ***strv, const char *separator) { v = new; v[i] = p; - v[i+1] = NULL; + v[i + 1] = NULL; i++; } @@ -826,7 +1008,7 @@ int strv_attach(char **first, char **second, char ***strv, bool free_second) { if (second) { n2 = sizeof_strv(second); - new = (char **)realloc(first, sizeof(char *) * (n1 + n2 + 1)); + new = (char **) realloc(first, sizeof(char *) * (n1 + n2 + 1)); if (!new) return -ENOMEM; |