X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Fshared%2Futil.h;h=09e556d0114b7da5756823727e767955fa37637a;hb=51d122af23533b0b8318911c4fc8b128ad8eafb7;hp=63f4e3dff2730db346979442061c4e9f48a299b5;hpb=df89481a35d4d7d58c7563f3082f67c218891375;p=elogind.git diff --git a/src/shared/util.h b/src/shared/util.h index 63f4e3dff..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" @@ -253,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) \ @@ -403,6 +405,7 @@ static inline const char *ansi_highlight_off(void) { 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); @@ -447,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); @@ -577,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) @@ -584,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)) @@ -763,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); + } +}