]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-util.h
libkmod-index.c: Fix error message
[thirdparty/kmod.git] / libkmod / libkmod-util.h
1 #pragma once
2 #include "macro.h"
3
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
12 char *getline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1)));
13 #define streq(a, b) (strcmp((a), (b)) == 0)
14 #define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0)
15 void *memdup(const void *p, size_t n) __attribute__((nonnull(1)));
16
17 ssize_t read_str_safe(int fd, char *buf, size_t buflen) _must_check_ __attribute__((nonnull(2)));
18 ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((nonnull(2)));
19 int read_str_long(int fd, long *value, int base) _must_check_ __attribute__((nonnull(2)));
20 int read_str_ulong(int fd, unsigned long *value, int base) _must_check_ __attribute__((nonnull(2)));
21 char *strchr_replace(char *s, int c, char r);
22 bool path_is_absolute(const char *p) _must_check_ __attribute__((nonnull(1)));
23 char *path_make_absolute_cwd(const char *p) _must_check_ __attribute__((nonnull(1)));
24 int mkdir_p(const char *path, int len, mode_t mode);
25 int mkdir_parents(const char *path, mode_t mode);
26 int alias_normalize(const char *alias, char buf[PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2)));
27 char *modname_normalize(const char *modname, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(1, 2)));
28 char *path_to_modname(const char *path, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(2)));
29
30 extern const struct kmod_ext {
31 const char *ext;
32 size_t len;
33 } kmod_exts[];
34 #define KMOD_EXT_UNC 0
35 bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1)));
36
37 unsigned long long stat_mstamp(const struct stat *st);
38 unsigned long long ts_usec(const struct timespec *ts);
39
40 #define get_unaligned(ptr) \
41 ({ \
42 struct __attribute__((packed)) { \
43 typeof(*(ptr)) __v; \
44 } *__p = (typeof(__p)) (ptr); \
45 __p->__v; \
46 })
47
48 #define put_unaligned(val, ptr) \
49 do { \
50 struct __attribute__((packed)) { \
51 typeof(*(ptr)) __v; \
52 } *__p = (typeof(__p)) (ptr); \
53 __p->__v = (val); \
54 } while(0)
55
56 static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u)
57 {
58 return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
59 }
60
61 static inline void freep(void *p) {
62 free(*(void**) p);
63 }
64
65 #define _cleanup_free_ _cleanup_(freep)