if (rows <= 0)
rows = 25;
- path_columns = columns_uncached() - 42;
+ path_columns = columns() - 42;
if (path_columns < 10)
path_columns = 10;
goto finish;
}
+ signal(SIGWINCH, columns_cache_reset);
+
while (!quit) {
Hashmap *c;
usec_t t;
int saved_argc = 0;
char **saved_argv = NULL;
-static int parsed_columns = 0;
+static volatile unsigned cached_columns = 0;
size_t page_size(void) {
static __thread size_t pgsz = 0;
return ws.ws_col;
}
-static unsigned columns_cached(bool cached) {
- static __thread int env_columns = -1;
+unsigned columns(void) {
const char *e;
+ unsigned c;
- if (_likely_(parsed_columns > 0 && cached))
- return parsed_columns;
-
- if (_unlikely_(env_columns == -1)) {
- e = getenv("COLUMNS");
- if (e)
- env_columns = atoi(e);
- else
- env_columns = 0;
- }
-
- if (env_columns > 0) {
- parsed_columns = env_columns;
- return parsed_columns;
- }
+ if (_likely_(cached_columns > 0))
+ return cached_columns;
- if (parsed_columns <= 0 || !cached)
- parsed_columns = fd_columns(STDOUT_FILENO);
-
- if (parsed_columns <= 0)
- parsed_columns = 80;
+ c = 0;
+ e = getenv("COLUMNS");
+ if (e)
+ safe_atou(e, &c);
- return parsed_columns;
-}
+ if (c <= 0)
+ c = fd_columns(STDOUT_FILENO);
-unsigned columns(void) {
- return columns_cached(true);
-}
+ if (c <= 0)
+ c = 80;
-unsigned columns_uncached(void) {
- return columns_cached(false);
+ cached_columns = c;
+ return c;
}
/* intended to be used as a SIGWINCH sighandler */
void columns_cache_reset(int signum) {
- parsed_columns = 0;
+ cached_columns = 0;
}
int fd_lines(int fd) {