util.h revision 15ae422b7471cf6f41ccf450243d8afd8ea0a054
afe3ab588a6b2992efe5a9b22ed038545ba3cdbfLennart Poettering/*-*- Mode: C; c-basic-offset: 8 -*-*/
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers This file is part of systemd.
8adaf7bd23baa6e2cd99e9e88e55d0f5f5db29a2Richard Maw Copyright 2010 Lennart Poettering
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek under the terms of the GNU General Public License as published by
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2 of the License, or
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek (at your option) any later version.
3e495a6651609d0a45b62aab5c3ed5a3b40e11abZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
3e495a6651609d0a45b62aab5c3ed5a3b40e11abZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f85857df75cfedbc0d10b8ca2400188dc8f4c22eLennart Poettering General Public License for more details.
bafb15bab99887d1b6b8a35136531bac6c3876a6Lennart Poettering You should have received a copy of the GNU General Public License
83a1ff25e5228b0a5b2cc942fd4f964d10bb73b0Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
3d39e6e5d475461c20d0300da80ff8cae5780ccfLennart Poettering/* What is interpreted as whitespace? */
f506d09f714ce7c405d27cdf5939e1fdc3ed1a07Lennart Poetteringusec_t timespec_load(const struct timespec *ts);
f506d09f714ce7c405d27cdf5939e1fdc3ed1a07Lennart Poetteringstruct timespec *timespec_store(struct timespec *ts, usec_t u);
bd098bceddfe70dc995c0c4f7bac07475fc0fc41Lennart Poetteringusec_t timeval_load(const struct timeval *tv);
bd098bceddfe70dc995c0c4f7bac07475fc0fc41Lennart Poetteringstruct timeval *timeval_store(struct timeval *tv, usec_t u);
de7399eb7427da46c5cd355181080274f69567a2Lennart Poetteringbool streq_ptr(const char *a, const char *b);
de7399eb7427da46c5cd355181080274f69567a2Lennart Poettering#define new(t, n) ((t*) malloc(sizeof(t)*(n)))
0d4c4b7141c494fd8db273cf637a17af53674b2fLennart Poettering#define new0(t, n) ((t*) calloc((n), sizeof(t)))
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poetteringstatic inline const char* yes_no(bool b) {
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poetteringstatic inline const char* strempty(const char *s) {
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poettering return s ? s : "";
e6a26d8c972d45a0927ad0b7c654c830daa2243eLennart Poetteringstatic inline const char* strnull(const char *s) {
1361205099406d2a19d64547448638a6b665af81Lennart Poettering return s ? s : "(null)";
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poetteringstatic inline const char *strna(const char *s) {
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering return s ? s : "n/a";
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poetteringstatic inline bool is_path_absolute(const char *p) {
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering return *p == '/';
203e81db24ccb9b4dcb0b1bad0ba554116267d20Lennart Poetteringbool endswith(const char *s, const char *postfix);
203e81db24ccb9b4dcb0b1bad0ba554116267d20Lennart Poetteringbool startswith(const char *s, const char *prefix);
63432f5d9570b76a8efe82702d69611c20645530Lennart Poetteringbool first_word(const char *s, const char *word);
8aa203819fc7f2a840191f8d9d0e65566c0ce98eLennart Poetteringint parse_boolean(const char *v);
b18d23d7ac6a53d52b99dbf0b2048d5a946a2e28Lennart Poetteringint safe_atou(const char *s, unsigned *ret_u);
449a22a58f0fa55d9d0fd2eb597272446e032acdLennart Poetteringint safe_atolu(const char *s, unsigned long *ret_u);
449a22a58f0fa55d9d0fd2eb597272446e032acdLennart Poetteringint safe_atoli(const char *s, long int *ret_i);
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poetteringint safe_atollu(const char *s, unsigned long long *ret_u);
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poetteringint safe_atolli(const char *s, long long int *ret_i);
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poetteringchar *split(const char *c, size_t *l, const char *separator, char **state);
2f653bded321fc2271edcda43d54fcc3e6c20dc9Lennart Poetteringchar *split_quoted(const char *c, size_t *l, char **state);
2f653bded321fc2271edcda43d54fcc3e6c20dc9Lennart Poettering#define FOREACH_WORD(word, length, s, state) \
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering#define FOREACH_WORD_QUOTED(word, length, s, state) \
2bc39683c0cada86c9dc39e5f3d0ea475cf12e57Lennart Poettering for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
2bc39683c0cada86c9dc39e5f3d0ea475cf12e57Lennart Poetteringchar **split_path_and_make_absolute(const char *p);
17fe56148c44dfa5583a8643c1918fd6eccf2aeeLennart Poetteringpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
c14db9b32ab90738973071d31f259d1a457d7b4aLennart Poetteringint write_one_line_file(const char *fn, const char *line);
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poetteringint read_one_line_file(const char *fn, char **line);
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poetteringchar *strappend(const char *s, const char *suffix);
82e6c50c473f4be8df77c7a510577f1975eedddbLennart Poetteringint readlink_malloc(const char *p, char **r);
d2f81fb00cc3c49e21b31000ba7d37b81a260257Lennart Poetteringchar *file_name_from_path(const char *p);
d2f81fb00cc3c49e21b31000ba7d37b81a260257Lennart Poetteringbool is_path(const char *p);
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poetteringbool path_is_absolute(const char *p);
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poetteringchar *path_make_absolute(const char *p, const char *prefix);
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poetteringchar *path_make_absolute_cwd(const char *p);
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poetteringchar **strv_path_make_absolute_cwd(char **l);
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poetteringchar *delete_chars(char *s, const char *bad);
e3286870fdf20c3c93e944b24fd9af53620f7dbaLennart Poetteringchar *file_in_same_dir(const char *path, const char *filename);
e3286870fdf20c3c93e944b24fd9af53620f7dbaLennart Poetteringint mkdir_parents(const char *path, mode_t mode);
e3286870fdf20c3c93e944b24fd9af53620f7dbaLennart Poetteringint mkdir_p(const char *path, mode_t mode);
8514b67754c5ff7fa628929b3d27131010c21842Lennart Poetteringint get_process_name(pid_t pid, char **name);
7e27f3121e5a10629302b5221eb21345f832724aLennart Poetteringchar *cescape(const char *s);
7e27f3121e5a10629302b5221eb21345f832724aLennart Poetteringchar *cunescape(const char *s);
758c4d7a391c0e024737053c815bf3924653b8c5Lennart Poetteringbool path_startswith(const char *path, const char *prefix);
821cc13ddae40fb7608458b44aaa7a3fd33d56d9Lennart Poetteringbool path_equal(const char *a, const char *b);
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poetteringchar *xescape(const char *s, const char *bad);
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poetteringchar *bus_path_escape(const char *s);
25e773eeb4f853804e1bf0dbd9a184f23e9b2a97Kay Sieverschar *bus_path_unescape(const char *s);
3bcde97e8502c48b53f7420e2433ca68e601662dLennart Poetteringbool chars_intersect(const char *a, const char *b);
279f036675536d55c901562b49f9df146af1a0e3Lennart Poetteringchar *format_timestamp(char *buf, size_t l, usec_t t);
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek const char *name##_to_string(type i) { \
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
42aeb14a4a0fa7d43da96a8ed0fb0e180a2dd5c8Lennart Poettering type name##_from_string(const char *s) { \
e25b5a8d7380a86ea717b4038c45a8f882b1a2aeDavid Herrmann unsigned u = 0; \
14a32924c9b46817c92ae11c1147a59dcb62012bLennart Poettering for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
563b1bdc09efe0cf94dd3f514f30376ca854c1ceLennart Poettering if (safe_atou(s, &u) >= 0 && \
6bd7941ece602ae9962a103c8d65ecda7d642391Tom Gundersen struct __useless_struct_to_allow_trailing_semicolon__
9d6db739ce1eaa3eace21801fd606d523b73c8f4Lennart Poetteringint close_all_fds(const int except[], unsigned n_except);
ccc3e8a104b8ccabb2cde99cc18ed5ac5a8ad883Lennart Poetteringint read_one_char(FILE *f, char *ret, bool *need_nl);
0f47ed0a052c0da743404f23ac3532aaabd23655Lennart Poetteringint ask(char *ret, const char *replies, const char *text, ...);
e25b5a8d7380a86ea717b4038c45a8f882b1a2aeDavid Herrmannint open_terminal(const char *name, int mode);
e25b5a8d7380a86ea717b4038c45a8f882b1a2aeDavid Herrmannint acquire_terminal(const char *name, bool fail, bool force);
a01647e53727107d82382bc5c9d98c894e8f386cLennart Poetteringssize_t loop_read(int fd, void *buf, size_t nbytes);
a940778fb1dd16479f455bab3ac6cbdbc5b06165Lennart Poetteringint path_is_mount_point(const char *path);
37efac5ddb21fd91ed420c070ed07f375e78b3b9Lennart Poetteringconst char *ioprio_class_to_string(int i);
37efac5ddb21fd91ed420c070ed07f375e78b3b9Lennart Poetteringint ioprio_class_from_string(const char *s);
7348b3adb324614132cf376f478e883bd7de28f1Lennart Poetteringconst char *sigchld_code_to_string(int i);
e107ed185ef08945102834234a05ec51bb438685Lennart Poetteringint sigchld_code_from_string(const char *s);
81429136905a6204875174b60a179333b7f3c9e4Kay Sieversconst char *log_facility_to_string(int i);
81429136905a6204875174b60a179333b7f3c9e4Kay Sieversint log_facility_from_string(const char *s);
f598ac3e28b729dd0b1d0a881df3e16465687a2bLennart Poetteringconst char *log_level_to_string(int i);
11fb37f16ed99c1603c9d770b60ce4953b96a58dLennart Poetteringint log_level_from_string(const char *s);
edb2935c5c5b95c42b8679086f60da5eafad74cbLennart Poetteringconst char *sched_policy_to_string(int i);
6a3f892a23db71544d0439355f96c44350dafa8fLennart Poetteringint sched_policy_from_string(const char *s);
2a781fc9bd33982c81e5ff75974a442a33d4f167Lennart Poetteringconst char *rlimit_to_string(int i);
6a3f892a23db71544d0439355f96c44350dafa8fLennart Poetteringint rlimit_from_string(const char *s);