blob: ef3881a7bc4f9b4fa7ee30ee111da372dcf4ed3d [file] [log] [blame]
Lucas De Marchi96573a02014-10-03 00:01:35 -03001#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)
16char *strchr_replace(char *s, int c, char r);
17void *memdup(const void *p, size_t n) __attribute__((nonnull(1)));
18
Lucas De Marchi2b0104f2014-10-09 00:43:01 -030019/* module-related functions */
20/* ************************************************************************ */
Lucas De Marchif4e8c162014-10-09 01:14:16 -030021#define KMOD_EXTENSION_UNCOMPRESSED ".ko"
22
Lucas De Marchi2b0104f2014-10-09 00:43:01 -030023int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2)));
Lucas De Marchif4e8c162014-10-09 01:14:16 -030024char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(1, 2)));
25char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(2)));
26bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1)));
Lucas De Marchi2b0104f2014-10-09 00:43:01 -030027
Lucas De Marchi96573a02014-10-03 00:01:35 -030028/* read-like and fread-like functions */
29/* ************************************************************************ */
30ssize_t read_str_safe(int fd, char *buf, size_t buflen) _must_check_ __attribute__((nonnull(2)));
31ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((nonnull(2)));
32int read_str_long(int fd, long *value, int base) _must_check_ __attribute__((nonnull(2)));
33int read_str_ulong(int fd, unsigned long *value, int base) _must_check_ __attribute__((nonnull(2)));
Lucas De Marchiaafd3832014-10-03 03:25:06 -030034char *freadline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1)));
Lucas De Marchi96573a02014-10-03 00:01:35 -030035
36/* path handling functions */
37/* ************************************************************************ */
38bool path_is_absolute(const char *p) _must_check_ __attribute__((nonnull(1)));
39char *path_make_absolute_cwd(const char *p) _must_check_ __attribute__((nonnull(1)));
40int mkdir_p(const char *path, int len, mode_t mode);
41int mkdir_parents(const char *path, mode_t mode);
42unsigned long long stat_mstamp(const struct stat *st);
43unsigned 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) \
56do { \
57 struct __attribute__((packed)) { \
58 typeof(*(ptr)) __v; \
59 } *__p = (typeof(__p)) (ptr); \
60 __p->__v = (val); \
61} while(0)
62
63static _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/* ************************************************************************ */
70static inline void freep(void *p) {
71 free(*(void**) p);
72}
73#define _cleanup_free_ _cleanup_(freep)