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=ac999c624cdf4f35009ebb7bd0f30f0da2323d30;hb=51d122af23533b0b8318911c4fc8b128ad8eafb7;hpb=d4ac85c6f6d8547f8b835009ae431438de72df28 diff --git a/src/shared/util.h b/src/shared/util.h index ac999c624..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_; @@ -243,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) \ @@ -374,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); @@ -421,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); @@ -551,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) @@ -558,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)) @@ -737,3 +772,15 @@ 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); + } +}