util.h revision 613b411c947635136637f8cdd66b94512f761eab
/*-*- 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 <alloca.h>
#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 <unistd.h>
#include <locale.h>
#include <mntent.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 "#;"
#define FORMAT_BYTES_MAX 8
#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
#define ANSI_RED_ON "\x1B[31m"
#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
#define ANSI_GREEN_ON "\x1B[32m"
#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
#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];
}
static inline const char *startswith(const char *s, const char *prefix) {
return NULL;
}
static inline const char *startswith_no_case(const char *s, const char *prefix) {
return NULL;
}
int close_nointr(int fd);
void close_nointr_nofail(int fd);
int parse_boolean(const char *v) _pure_;
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 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 *cescape(const char *s);
char *cunescape(const char *s);
char *ascii_strlower(char *path);
bool chars_intersect(const char *a, const char *b) _pure_;
int make_stdio(int fd);
int make_null_stdio(void);
int make_console_stdio(void);
unsigned long long random_ull(void);
unsigned random_u(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);
char* dirname_malloc(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_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
int status_welcome(void);
int fd_columns(int fd);
unsigned columns(void);
unsigned lines(void);
bool on_tty(void);
static inline const char *ansi_highlight(void) {
}
static inline const char *ansi_highlight_red(void) {
}
static inline const char *ansi_highlight_green(void) {
}
static inline const char *ansi_highlight_yellow(void) {
}
static inline const char *ansi_highlight_off(void) {
}
int running_in_chroot(void);
/* bytes columns */
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);
char *resolve_dev_console(char **active);
bool tty_is_vc_resolve(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) _pure_;
char* hostname_cleanup(char *s, bool lowercase);
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);
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);
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) _const_;
int sigchld_code_from_string(const char *s) _pure_;
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) _const_;
int rlimit_from_string(const char *s) _pure_;
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) _const_;
int signal_from_string(const char *s) _pure_;
int signal_from_string_try_harder(const char *s);
extern int saved_argc;
extern char **saved_argv;
bool kexec_loaded(void);
bool in_initrd(void);
void warn_melody(void);
int get_home_dir(char **ret);
static inline void freep(void *p) {
free(*(void**) p);
}
if (*p) \
func(*p); \
} \
if (*fd >= 0)
}
umask(*u);
}
static inline void close_pipep(int (*p)[2]) {
close_pipe(*p);
}
return NULL;
return malloc(a * b);
}
return NULL;
return memdup(p, a * b);
}
bool filename_is_safe(const char *p) _pure_;
bool path_is_safe(const char *p) _pure_;
bool string_is_safe(const char *p) _pure_;
bool string_has_cc(const char *p) _pure_;
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);
for (;;) \
if (ferror(f)) { \
on_error; \
} \
break; \
} else
if (!de) { \
if (errno > 0) { \
on_error; \
} \
break; \
continue; \
else
memset(s, c, n);
return (uint8_t*)s + n;
}
char *strextend(char **x, ...) _sentinel_;
char *strrep(const char *s, unsigned n);
static inline void _reset_errno_(int *saved_errno) {
errno = *saved_errno;
}
struct _umask_struct_ {
bool quit;
};
static inline void _reset_umask_(struct _umask_struct_ *s) {
};
#define RUN_WITH_UMASK(mask) \
!_saved_umask_.quit ; \
_saved_umask_.quit = true)
}
static inline bool logind_running(void) {
}
#define DECIMAL_STR_WIDTH(x) \
({ \
unsigned ans = 1; \
while (_x_ /= 10) \
ans++; \
ans; \
})
int unlink_noerrno(const char *path);
#define alloca0(n) \
({ \
char *_new_; \
})
#define strappenda(a, b) \
({ \
char *_c_; \
_c_; \
})
({ \
char *_r_; \
_r_; \
})
struct _locale_struct_ {
bool quit;
};
static inline void _reset_locale_(struct _locale_struct_ *s) {
if (s->saved_locale != (locale_t) 0)
uselocale(s->saved_locale);
if (s->new_locale != (locale_t) 0)
freelocale(s->new_locale);
}
for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \
({ \
if (!_saved_locale_.quit) { \
} \
!_saved_locale_.quit; }) ; \
_saved_locale_.quit = true)
bool id128_is_valid(const char *s) _pure_;
int split_pair(const char *s, const char *sep, char **l, char **r);
int shall_restore_state(void);
/**
* Normal qsort requires base to be nonnull. Here were require
* that only if nmemb > 0.
*/
int (*compar)(const void *, const void *)) {
if (nmemb) {
}
}
int proc_cmdline(char **ret);