]> git.ipfire.org Git - thirdparty/systemd.git/blame - util.h
Merge remote branch 'kay/master'
[thirdparty/systemd.git] / util.h
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3#ifndef fooutilhfoo
4#define fooutilhfoo
5
6#include <inttypes.h>
7#include <time.h>
8#include <sys/time.h>
9#include <stdbool.h>
5cb5a6ff 10#include <stdlib.h>
60918275
LP
11
12typedef uint64_t usec_t;
13
034c6ed7
LP
14#define MSEC_PER_SEC 1000ULL
15#define USEC_PER_SEC 1000000ULL
16#define USEC_PER_MSEC 1000ULL
17#define NSEC_PER_SEC 1000000000ULL
18#define NSEC_PER_MSEC 1000000ULL
60918275
LP
19#define NSEC_PER_USEC 1000ULL
20
44d8db9e 21/* What is interpreted as whitespace? */
4a72ff34 22#define WHITESPACE " \t\n\r"
44d8db9e 23
60918275
LP
24usec_t now(clockid_t clock);
25
26usec_t timespec_load(const struct timespec *ts);
27struct timespec *timespec_store(struct timespec *ts, usec_t u);
28
29usec_t timeval_load(const struct timeval *tv);
30struct timeval *timeval_store(struct timeval *tv, usec_t u);
31
32#define streq(a,b) (strcmp((a),(b)) == 0)
33
34#define new(t, n) ((t*) malloc(sizeof(t)*(n)))
35
36#define new0(t, n) ((t*) calloc((n), sizeof(t)))
37
38#define malloc0(n) (calloc((n), 1))
39
40static inline const char* yes_no(bool b) {
41 return b ? "yes" : "no";
42}
43
44static inline const char* strempty(const char *s) {
45 return s ? s : "";
46}
47
48static inline const char* strnull(const char *s) {
49 return s ? s : "(null)";
50}
51
04fd6fe4
LP
52static inline const char *strna(const char *s) {
53 return s ? s : "n/a";
54}
55
56static inline bool is_path_absolute(const char *p) {
57 return *p == '/';
58}
59
60918275
LP
60bool endswith(const char *s, const char *postfix);
61bool startswith(const char *s, const char *prefix);
62
79d6d816
LP
63bool first_word(const char *s, const char *word);
64
42f4e3c4 65int close_nointr(int fd);
85f136b5 66void close_nointr_nofail(int fd);
60918275 67
85261803
LP
68int parse_boolean(const char *v);
69
70int safe_atou(const char *s, unsigned *ret_u);
71int safe_atoi(const char *s, int *ret_i);
72
034c6ed7
LP
73int safe_atolu(const char *s, unsigned long *ret_u);
74int safe_atoli(const char *s, long int *ret_i);
75
76int safe_atollu(const char *s, unsigned long long *ret_u);
77int safe_atolli(const char *s, long long int *ret_i);
78
a41e8209 79char *split_spaces(const char *c, size_t *l, char **state);
034c6ed7 80char *split_quoted(const char *c, size_t *l, char **state);
a41e8209 81
034c6ed7 82#define FOREACH_WORD(word, length, s, state) \
a41e8209
LP
83 for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
84
034c6ed7
LP
85#define FOREACH_WORD_QUOTED(word, length, s, state) \
86 for ((state) = NULL, (word) = split_quoted((s), &(l), &(state)); (word); (word) = split_quoted((s), &(l), &(state)))
87
034c6ed7
LP
88pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
89
90int write_one_line_file(const char *fn, const char *line);
91int read_one_line_file(const char *fn, char **line);
92
44d8db9e
LP
93char *strappend(const char *s, const char *suffix);
94
87f0e418
LP
95int readlink_malloc(const char *p, char **r);
96
97char *file_name_from_path(const char *p);
0301abf4
LP
98bool is_path(const char *p);
99
100bool path_is_absolute(const char *p);
101char *path_make_absolute(const char *p, const char *prefix);
87f0e418 102
2a987ee8
LP
103int reset_all_signal_handlers(void);
104
4a72ff34
LP
105char *strstrip(char *s);
106char *file_in_same_dir(const char *path, const char *filename);
107
fb624d04 108char hexchar(int x);
4fe88d28
LP
109int unhexchar(char c);
110char octchar(int x);
111int unoctchar(char c);
112
113char *cescape(const char *s);
114char *cunescape(const char *s);
115
116char *path_kill_slashes(char *path);
117
118bool path_startswith(const char *path, const char *prefix);
119
120char *ascii_strlower(char *path);
121
122char *xescape(const char *s, const char *bad);
fb624d04 123
ea430986
LP
124char *bus_path_escape(const char *s);
125char *bus_path_unescape(const char *s);
126
1dccbe19
LP
127#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
128 const char *name##_to_string(type i) { \
129 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
130 return NULL; \
131 return name##_table[i]; \
132 } \
133 type name##_from_string(const char *s) { \
134 type i; \
135 assert(s); \
136 for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
137 if (streq(name##_table[i], s)) \
138 return i; \
139 return (type) -1; \
140 } \
141 struct __useless_struct_to_allow_trailing_semicolon__
142
143
144const char *ioprio_class_to_string(int i);
145int ioprio_class_from_string(const char *s);
146
147const char *sigchld_code_to_string(int i);
148int sigchld_code_from_string(const char *s);
149
150const char *log_facility_to_string(int i);
151int log_facility_from_string(const char *s);
152
153const char *log_level_to_string(int i);
154int log_level_from_string(const char *s);
155
156const char *sched_policy_to_string(int i);
157int sched_policy_from_string(const char *s);
158
159const char *rlimit_to_string(int i);
160int rlimit_from_string(const char *s);
161
60918275 162#endif