util.h revision 59164be40e6b520315d87e1ef16a3be65c854224
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
#include <time.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sched.h>
#include <limits.h>
#include <dirent.h>
#include <sys/resource.h>
#include <stddef.h>
#include "macro.h"
#include "time-util.h"
union dirent_storage {
};
/* What is interpreted as whitespace? */
#define WHITESPACE " \t\n\r"
#define NEWLINE "\n\r"
#define QUOTES "\"\'"
#define COMMENTS "#;\n"
#define FORMAT_BYTES_MAX 8
#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
bool is_efiboot(void);
bool streq_ptr(const char *a, const char *b);
#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
static inline const char* yes_no(bool b) {
return b ? "yes" : "no";
}
static inline const char* strempty(const char *s) {
return s ? s : "";
}
static inline const char* strnull(const char *s) {
return s ? s : "(null)";
}
static inline const char *strna(const char *s) {
return s ? s : "n/a";
}
static inline bool isempty(const char *p) {
return !p || !p[0];
}
char *startswith(const char *s, const char *prefix);
char *startswith_no_case(const char *s, const char *prefix);
bool first_word(const char *s, const char *word);
int close_nointr(int fd);
void close_nointr_nofail(int fd);
int parse_boolean(const char *v);
int safe_atollu(const char *s, unsigned long long *ret_u);
int safe_atolli(const char *s, long long int *ret_i);
#if __WORDSIZE == 32
static inline int safe_atolu(const char *s, unsigned long *ret_u) {
assert_cc(sizeof(unsigned long) == sizeof(unsigned));
}
static inline int safe_atoli(const char *s, long int *ret_u) {
assert_cc(sizeof(long int) == sizeof(int));
}
#else
static inline int safe_atolu(const char *s, unsigned long *ret_u) {
assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
return safe_atollu(s, (unsigned long long*) ret_u);
}
static inline int safe_atoli(const char *s, long int *ret_u) {
assert_cc(sizeof(long int) == sizeof(long long int));
return safe_atolli(s, (long long int*) ret_u);
}
#endif
}
}
return safe_atollu(s, (unsigned long long*) ret_u);
}
return safe_atolli(s, (long long int*) ret_i);
}
for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
int load_env_file(const char *fname, char ***l);
int write_env_file(const char *fname, char **l);
int readlink_malloc(const char *p, char **r);
int readlink_and_make_absolute(const char *p, char **r);
int readlink_and_canonicalize(const char *p, char **r);
int reset_all_signal_handlers(void);
char *strstrip(char *s);
char *delete_chars(char *s, const char *bad);
char *truncate_nl(char *s);
char hexchar(int x);
int unhexchar(char c);
char octchar(int x);
int unoctchar(char c);
char decchar(int x);
int undecchar(char c);
char *cescape(const char *s);
char *cunescape(const char *s);
char *bus_path_escape(const char *s);
char *bus_path_unescape(const char *s);
char *ascii_strlower(char *path);
bool ignore_file(const char *filename);
bool chars_intersect(const char *a, const char *b);
int make_stdio(int fd);
int make_null_stdio(void);
int make_console_stdio(void);
unsigned long long random_ull(void);
/* For basic lookup tables with strictly enumerated entries */
return NULL; \
} \
type i; \
assert(s); \
return i; \
return (type) -1; \
} \
#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
/* For string conversions where numbers are also acceptable */
char *s; \
int r; \
if (i < 0 || i > max) \
return -ERANGE; \
if (!s) \
return log_oom(); \
} else { \
r = asprintf(&s, "%u", i); \
if (r < 0) \
return log_oom(); \
} \
*str = s; \
return 0; \
} \
type i; \
unsigned u = 0; \
assert(s); \
return i; \
return (type) u; \
return (type) -1; \
} \
bool fstype_is_network(const char *fstype);
int reset_terminal(const char *name);
int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm, usec_t timeout);
int release_terminal(void);
int ignore_signals(int sig, ...);
int default_signals(int sig, ...);
int close_pipe(int p[]);
bool is_device_path(const char *path);
int dir_is_empty(const char *path);
bool hostname_is_set(void);
char* gethostname_malloc(void);
char* getlogname_malloc(void);
char* getusername_malloc(void);
int getttyname_malloc(int fd, char **r);
int getttyname_harder(int fd, char **r);
int status_welcome(void);
int fd_columns(int fd);
unsigned columns(void);
unsigned lines(void);
bool on_tty(void);
int running_in_chroot(void);
char *normalize_env_assignment(const char *s);
_noreturn_ void freeze(void);
int null_or_empty_path(const char *fn);
char *fstab_node_to_udev_node(const char *p);
bool tty_is_vc_resolve(const char *tty);
bool tty_is_console(const char *tty);
int vtnr_from_tty(const char *tty);
const char *default_term_for_tty(const char *tty);
bool plymouth_running(void);
bool hostname_is_valid(const char *s);
char* hostname_cleanup(char *s);
char* strshorten(char *s, size_t l);
int terminal_vhangup_fd(int fd);
int terminal_vhangup(const char *name);
int vt_disallocate(const char *name);
bool display_is_local(const char *display);
int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
int glob_exists(const char *path);
char *strjoin(const char *x, ...) _sentinel_;
bool is_main_thread(void);
bool in_charset(const char *s, const char* charset);
int file_is_priv_sticky(const char *p);
int strdup_or_null(const char *a, char **b);
#define NULSTR_FOREACH(i, l) \
for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
#define NULSTR_FOREACH_PAIR(i, j, l) \
for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
int ioprio_class_to_string_alloc(int i, char **s);
int ioprio_class_from_string(const char *s);
const char *sigchld_code_to_string(int i);
int sigchld_code_from_string(const char *s);
int log_facility_unshifted_to_string_alloc(int i, char **s);
int log_facility_unshifted_from_string(const char *s);
int log_level_to_string_alloc(int i, char **s);
int log_level_from_string(const char *s);
int sched_policy_to_string_alloc(int i, char **s);
int sched_policy_from_string(const char *s);
const char *rlimit_to_string(int i);
int rlimit_from_string(const char *s);
int ip_tos_to_string_alloc(int i, char **s);
int ip_tos_from_string(const char *s);
const char *signal_to_string(int i);
int signal_from_string(const char *s);
int signal_from_string_try_harder(const char *s);
extern int saved_argc;
extern char **saved_argv;
bool kexec_loaded(void);
int prot_from_flags(int flags);
int can_sleep_disk(const char *type);
bool is_valid_documentation_url(const char *url);
bool in_initrd(void);
void warn_melody(void);
int get_home_dir(char **ret);
void freep(void *p);
return NULL;
return malloc(a * b);
}
return NULL;
return memdup(p, a * b);
}
bool filename_is_safe(const char *p);
bool string_is_safe(const char *p);
int (*compar) (const void *, const void *, void *),
void *arg);
bool is_locale_utf8(void);
typedef enum DrawSpecialChar {
char *strip_tab_ansi(char **p, size_t *l);
int on_ac_power(void);