X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=2f0aba88ae6e1c443f54b1d821ed25d73649a069;hp=527a5800fec407d6cc096f66d22dc22cbfa6d7cf;hb=a9e12476ed32256690eb801099c41526834b6390;hpb=8481248b9fbddc6d5e6ff26eb23505ef13dc85f7 diff --git a/src/shared/util.c b/src/shared/util.c index 527a5800f..2f0aba88a 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -73,6 +73,7 @@ int saved_argc = 0; char **saved_argv = NULL; static volatile unsigned cached_columns = 0; +static volatile unsigned cached_lines = 0; size_t page_size(void) { static __thread size_t pgsz = 0; @@ -194,7 +195,7 @@ struct timeval *timeval_store(struct timeval *tv, usec_t u) { return tv; } -bool endswith(const char *s, const char *postfix) { +char* endswith(const char *s, const char *postfix) { size_t sl, pl; assert(s); @@ -204,12 +205,15 @@ bool endswith(const char *s, const char *postfix) { pl = strlen(postfix); if (pl == 0) - return true; + return (char*) s + sl; if (sl < pl) - return false; + return NULL; + + if (memcmp(s + sl - pl, postfix, pl) != 0) + return NULL; - return memcmp(s + sl - pl, postfix, pl) == 0; + return (char*) s + sl - pl; } bool startswith(const char *s, const char *prefix) { @@ -3812,20 +3816,6 @@ unsigned columns(void) { return c; } -/* intended to be used as a SIGWINCH sighandler */ -void columns_cache_reset(int signum) { - cached_columns = 0; -} - -bool on_tty(void) { - static int cached_on_tty = -1; - - if (_unlikely_(cached_on_tty < 0)) - cached_on_tty = isatty(STDOUT_FILENO) > 0; - - return cached_on_tty; -} - int fd_lines(int fd) { struct winsize ws; zero(ws); @@ -3840,23 +3830,40 @@ int fd_lines(int fd) { } unsigned lines(void) { - static __thread int parsed_lines = 0; const char *e; + unsigned l; - if (_likely_(parsed_lines > 0)) - return parsed_lines; + if (_likely_(cached_lines > 0)) + return cached_lines; + l = 0; e = getenv("LINES"); if (e) - parsed_lines = atoi(e); + safe_atou(e, &l); - if (parsed_lines <= 0) - parsed_lines = fd_lines(STDOUT_FILENO); + if (l <= 0) + l = fd_lines(STDOUT_FILENO); - if (parsed_lines <= 0) - parsed_lines = 25; + if (l <= 0) + l = 24; - return parsed_lines; + cached_lines = l; + return cached_lines; +} + +/* intended to be used as a SIGWINCH sighandler */ +void columns_lines_cache_reset(int signum) { + cached_columns = 0; + cached_lines = 0; +} + +bool on_tty(void) { + static int cached_on_tty = -1; + + if (_unlikely_(cached_on_tty < 0)) + cached_on_tty = isatty(STDOUT_FILENO) > 0; + + return cached_on_tty; } int running_in_chroot(void) { @@ -6062,3 +6069,27 @@ finish: return 0; } + +/* hey glibc, APIs with callbacks without a user pointer are so useless */ +void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, + int (*compar) (const void *, const void *, void *), + void *arg) { + size_t l, u, idx; + const void *p; + int comparison; + + l = 0; + u = nmemb; + while (l < u) { + idx = (l + u) / 2; + p = (void *)(((const char *) base) + (idx * size)); + comparison = compar(key, p, arg); + if (comparison < 0) + u = idx; + else if (comparison > 0) + l = idx + 1; + else + return (void *)p; + } + return NULL; +}