Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Lucas De Marchi | aac5f45 | 2015-02-10 10:41:47 -0200 | [diff] [blame] | 3 | #include <inttypes.h> |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 4 | #include <limits.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | |
| 11 | #include <shared/macro.h> |
| 12 | |
| 13 | /* string handling functions and memory allocations */ |
| 14 | /* ************************************************************************ */ |
| 15 | #define streq(a, b) (strcmp((a), (b)) == 0) |
| 16 | #define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0) |
Lucas De Marchi | 9a43753 | 2014-10-09 11:03:29 -0300 | [diff] [blame] | 17 | char *strchr_replace(char *s, char c, char r); |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 18 | void *memdup(const void *p, size_t n) __attribute__((nonnull(1))); |
| 19 | |
Lucas De Marchi | 2b0104f | 2014-10-09 00:43:01 -0300 | [diff] [blame] | 20 | /* module-related functions */ |
| 21 | /* ************************************************************************ */ |
Lucas De Marchi | f4e8c16 | 2014-10-09 01:14:16 -0300 | [diff] [blame] | 22 | #define KMOD_EXTENSION_UNCOMPRESSED ".ko" |
| 23 | |
Lucas De Marchi | 2b0104f | 2014-10-09 00:43:01 -0300 | [diff] [blame] | 24 | int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2))); |
Lucas De Marchi | 52c9c99 | 2014-10-09 10:59:08 -0300 | [diff] [blame] | 25 | int underscores(char *s) _must_check_; |
Lucas De Marchi | f4e8c16 | 2014-10-09 01:14:16 -0300 | [diff] [blame] | 26 | char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(1, 2))); |
| 27 | char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(2))); |
| 28 | bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1))); |
Lucas De Marchi | 2b0104f | 2014-10-09 00:43:01 -0300 | [diff] [blame] | 29 | |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 30 | /* read-like and fread-like functions */ |
| 31 | /* ************************************************************************ */ |
| 32 | ssize_t read_str_safe(int fd, char *buf, size_t buflen) _must_check_ __attribute__((nonnull(2))); |
| 33 | ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((nonnull(2))); |
| 34 | int read_str_long(int fd, long *value, int base) _must_check_ __attribute__((nonnull(2))); |
| 35 | int read_str_ulong(int fd, unsigned long *value, int base) _must_check_ __attribute__((nonnull(2))); |
Lucas De Marchi | aafd383 | 2014-10-03 03:25:06 -0300 | [diff] [blame] | 36 | char *freadline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1))); |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 37 | |
| 38 | /* path handling functions */ |
| 39 | /* ************************************************************************ */ |
| 40 | bool path_is_absolute(const char *p) _must_check_ __attribute__((nonnull(1))); |
| 41 | char *path_make_absolute_cwd(const char *p) _must_check_ __attribute__((nonnull(1))); |
| 42 | int mkdir_p(const char *path, int len, mode_t mode); |
| 43 | int mkdir_parents(const char *path, mode_t mode); |
| 44 | unsigned long long stat_mstamp(const struct stat *st); |
| 45 | unsigned long long ts_usec(const struct timespec *ts); |
| 46 | |
| 47 | /* endianess and alignments */ |
| 48 | /* ************************************************************************ */ |
| 49 | #define get_unaligned(ptr) \ |
| 50 | ({ \ |
| 51 | struct __attribute__((packed)) { \ |
| 52 | typeof(*(ptr)) __v; \ |
| 53 | } *__p = (typeof(__p)) (ptr); \ |
| 54 | __p->__v; \ |
| 55 | }) |
| 56 | |
| 57 | #define put_unaligned(val, ptr) \ |
| 58 | do { \ |
| 59 | struct __attribute__((packed)) { \ |
| 60 | typeof(*(ptr)) __v; \ |
| 61 | } *__p = (typeof(__p)) (ptr); \ |
| 62 | __p->__v = (val); \ |
| 63 | } while(0) |
| 64 | |
| 65 | static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u) |
| 66 | { |
| 67 | return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1)); |
| 68 | } |
| 69 | |
| 70 | /* misc */ |
| 71 | /* ************************************************************************ */ |
| 72 | static inline void freep(void *p) { |
| 73 | free(*(void**) p); |
| 74 | } |
| 75 | #define _cleanup_free_ _cleanup_(freep) |
Lucas De Marchi | aac5f45 | 2015-02-10 10:41:47 -0200 | [diff] [blame] | 76 | |
| 77 | static inline bool addu64_overflow(uint64_t a, uint64_t b, uint64_t *res) |
| 78 | { |
| 79 | #if (HAVE___BUILTIN_UADDL_OVERFLOW && HAVE___BUILTIN_UADDLL_OVERFLOW) |
| 80 | #if __SIZEOF_LONG__ == 8 |
| 81 | return __builtin_uaddl_overflow(a, b, res); |
| 82 | #elif __SIZEOF_LONG_LONG__ == 8 |
| 83 | return __builtin_uaddll_overflow(a, b, res); |
| 84 | #else |
| 85 | #error "sizeof(long long) != 8" |
| 86 | #endif |
| 87 | #endif |
| 88 | *res = a + b; |
Lucas De Marchi | a8c73b8 | 2015-03-23 23:47:23 -0300 | [diff] [blame] | 89 | return UINT64_MAX - a < b; |
Lucas De Marchi | aac5f45 | 2015-02-10 10:41:47 -0200 | [diff] [blame] | 90 | } |