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