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;
return tv;
}
-bool endswith(const char *s, const char *postfix) {
+char* endswith(const char *s, const char *postfix) {
size_t sl, pl;
assert(s);
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) {
}
int ask(char *ret, const char *replies, const char *text, ...) {
- bool on_tty;
assert(ret);
assert(replies);
assert(text);
- on_tty = isatty(STDOUT_FILENO);
-
for (;;) {
va_list ap;
char c;
int r;
bool need_nl = true;
- if (on_tty)
+ if (on_tty())
fputs(ANSI_HIGHLIGHT_ON, stdout);
va_start(ap, text);
vprintf(text, ap);
va_end(ap);
- if (on_tty)
+ if (on_tty())
fputs(ANSI_HIGHLIGHT_OFF, stdout);
fflush(stdout);
return c;
}
-/* intended to be used as a SIGWINCH sighandler */
-void columns_cache_reset(int signum) {
- cached_columns = 0;
-}
-
int fd_lines(int fd) {
struct winsize ws;
zero(ws);
}
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 (l <= 0)
+ l = fd_lines(STDOUT_FILENO);
+
+ if (l <= 0)
+ l = 24;
+
+ 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;
+}
- if (parsed_lines <= 0)
- parsed_lines = fd_lines(STDOUT_FILENO);
+bool on_tty(void) {
+ static int cached_on_tty = -1;
- if (parsed_lines <= 0)
- parsed_lines = 25;
+ if (_unlikely_(cached_on_tty < 0))
+ cached_on_tty = isatty(STDOUT_FILENO) > 0;
- return parsed_lines;
+ return cached_on_tty;
}
int running_in_chroot(void) {