X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=09e556d0114b7da5756823727e767955fa37637a;hp=7ef46e8f1ede68c2b38b2b46143550ca7d00176f;hb=51d122af23533b0b8318911c4fc8b128ad8eafb7;hpb=19adb8a3204fefd91411b5f0f350c8bc6bcf75fe diff --git a/src/shared/util.h b/src/shared/util.h index 7ef46e8f1..09e556d01 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -39,6 +39,7 @@ #include #include #include +#include #include "macro.h" #include "time-util.h" @@ -106,9 +107,19 @@ static inline bool isempty(const char *p) { return !p || !p[0]; } +static inline const char *startswith(const char *s, const char *prefix) { + if (strncmp(s, prefix, strlen(prefix)) == 0) + return s + strlen(prefix); + return NULL; +} + +static inline const char *startswith_no_case(const char *s, const char *prefix) { + if (strncasecmp(s, prefix, strlen(prefix)) == 0) + return s + strlen(prefix); + return NULL; +} + char *endswith(const char *s, const char *postfix) _pure_; -char *startswith(const char *s, const char *prefix) _pure_; -char *startswith_no_case(const char *s, const char *prefix) _pure_; bool first_word(const char *s, const char *word) _pure_; @@ -210,6 +221,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char * int get_process_exe(pid_t pid, char **name); int get_process_uid(pid_t pid, uid_t *uid); int get_process_gid(pid_t pid, gid_t *gid); +int get_process_capeff(pid_t pid, char **capeff); char hexchar(int x) _const_; int unhexchar(char c) _const_; @@ -242,6 +254,7 @@ 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 */ #define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ @@ -373,9 +386,26 @@ void columns_lines_cache_reset(int _unused_ signum); bool on_tty(void); +static inline const char *ansi_highlight(void) { + return on_tty() ? ANSI_HIGHLIGHT_ON : ""; +} + +static inline const char *ansi_highlight_red(void) { + return on_tty() ? ANSI_HIGHLIGHT_RED_ON : ""; +} + +static inline const char *ansi_highlight_green(void) { + return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : ""; +} + +static inline const char *ansi_highlight_off(void) { + return on_tty() ? ANSI_HIGHLIGHT_OFF : ""; +} + int running_in_chroot(void); char *ellipsize(const char *s, size_t length, unsigned percent); + /* bytes columns */ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent); int touch(const char *path); @@ -411,7 +441,7 @@ bool nulstr_contains(const char*nulstr, const char *needle); bool plymouth_running(void); bool hostname_is_valid(const char *s) _pure_; -char* hostname_cleanup(char *s); +char* hostname_cleanup(char *s, bool lowercase); char* strshorten(char *s, size_t l); @@ -420,7 +450,7 @@ int terminal_vhangup(const char *name); int vt_disallocate(const char *name); -int copy_file(const char *from, const char *to); +int copy_file(const char *from, const char *to, int flags); int symlink_atomic(const char *from, const char *to); @@ -439,6 +469,7 @@ char* uid_to_name(uid_t uid); char* gid_to_name(gid_t gid); int glob_exists(const char *path); +int glob_extend(char ***strv, const char *path); int dirent_ensure_type(DIR *d, struct dirent *de); @@ -549,6 +580,11 @@ static inline void umaskp(mode_t *u) { umask(*u); } +static inline void endmntentp(FILE **f) { + if (*f) + endmntent(*f); +} + #define _cleanup_free_ _cleanup_(freep) #define _cleanup_fclose_ _cleanup_(fclosep) #define _cleanup_pclose_ _cleanup_(pclosep) @@ -556,6 +592,7 @@ static inline void umaskp(mode_t *u) { #define _cleanup_closedir_ _cleanup_(closedirp) #define _cleanup_umask_ _cleanup_(umaskp) #define _cleanup_globfree_ _cleanup_(globfree) +#define _cleanup_endmntent_ _cleanup_(endmntentp) _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) { if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) @@ -732,3 +769,18 @@ static inline void _reset_locale_(struct _locale_struct_ *s) { _saved_locale_.quit = true) bool id128_is_valid(const char *s) _pure_; +void parse_user_at_host(char *arg, char **user, char **host); + +int split_pair(const char *s, const char *sep, char **l, char **r); + +/** + * Normal qsort requires base to be nonnull. Here were require + * that only if nmemb > 0. + */ +static inline void qsort_safe(void *base, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)) { + if (nmemb) { + assert(base); + qsort(base, nmemb, size, compar); + } +}