summaryrefslogtreecommitdiff
path: root/src/utils.h
blob: 32b4c89654594563f627fe0ef6c9f932cc446682 (plain)
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
#ifndef __UTILS_H
#define __UTILS_H

#include <stdarg.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BIT(nr)			(1UL << (nr))

#define ARRAY_SIZE(x)		(sizeof(x) / sizeof(*(x)))
#define __STRINGIFY(x...)	#x
#define STRINGIFY(x...)		__STRINGIFY(x)

enum {
	false = 0,
	true = 1,
};

char *basename(const char *path);
int tokcmp(const char *cmd, const char *pattern);
char *strrstrip(char *str);

ssize_t safe_read(int fd, void *buf, size_t count);
ssize_t safe_readn(int fd, void *buf, size_t len);
ssize_t safe_write(int fd, const void *buf, size_t count);
ssize_t full_write(int fd, const void *buf, size_t len);

enum {
	USER_YESNO_NO = 0,
	USER_YESNO_YES = 1,
};

int user_yesno(int def, const char *fmt, ...);
int systemf(const char *fmt, ...);
FILE *popenf(const char *mode, const char *fmt, ...);
char *shell_path_expand(const char *path);

int device_lock(const char *path);
int device_unlock(int lock);

int indexOfChar(const char *array, int len, char character);

#define YAML_INDENT	2

#define indentf(n, format, args...) \
	printf("%*s" format , n, "" ,  ## args)

#endif /* ~__UTILS_H */