summaryrefslogtreecommitdiff
path: root/src/shared/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/util.c')
-rw-r--r--src/shared/util.c47
1 files changed, 16 insertions, 31 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index 1c97a8a94a..462b541b41 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -72,7 +72,7 @@
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;
@@ -3793,46 +3793,31 @@ int fd_columns(int fd) {
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) {