File: basics.h

package info (click to toggle)
account-utils 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 836 kB
  • sloc: ansic: 8,789; xml: 1,584; sh: 77; makefile: 10
file content (82 lines) | stat: -rw-r--r-- 2,549 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//SPDX-License-Identifier: LGPL-2.1-or-later

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>

#define _unused_ __attribute__((unused))
#define _pure_ __attribute__((__pure__))
#define _const_ __attribute__((__const__))
#define _sentinel_ __attribute__((__sentinel__))

/* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time
 * resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
#define TAKE_GENERIC(var, type, nullvalue)                       \
        ({                                                       \
                type *_pvar_ = &(var);                           \
                type _var_ = *_pvar_;                            \
                type _nullvalue_ = nullvalue;                    \
                *_pvar_ = _nullvalue_;                           \
                _var_;                                           \
        })
#define TAKE_PTR_TYPE(ptr, type) TAKE_GENERIC(ptr, type, NULL)
#define TAKE_PTR(ptr) TAKE_PTR_TYPE(ptr, typeof(ptr))
#define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)

#define mfree(memory)                           \
        ({                                      \
                free(memory);                   \
                (typeof(memory)) NULL;          \
        })

static inline void freep(void *p) {
        *(void**)p = mfree(*(void**) p);
}

static inline void closep(int *fd) {
  if (*fd)
        close(*fd);
}

static inline void fclosep(FILE **f) {
  if (*f)
        fclose(*f);
}

#define _cleanup_(x) __attribute__((__cleanup__(x)))
#define _cleanup_close_ _cleanup_(closep)
#define _cleanup_fclose_ _cleanup_(fclosep)
#define _cleanup_free_ _cleanup_(freep)


/* from string-util-fundamental.h */

#define WHITESPACE          " \t\n\r"

#define streq(a,b) (strcmp((a),(b)) == 0)
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)

static inline const char *strempty(const char *s) {
        return s ?:"";
}

static inline const char *strna(const char *s) {
        return s ?: "n/a";
}

static inline const char *stroom(const char *s) {
        return s ?: "Out of memory";
}

extern char *startswith(const char *s, const char *prefix) _pure_;
extern char *endswith(const char *s, const char *suffix) _pure_;

static inline bool isempty(const char *a) {
        return !a || a[0] == '\0';
}