chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
util: simplify column caching logic
[elogind.git]
/
src
/
shared
/
util.c
diff --git
a/src/shared/util.c
b/src/shared/util.c
index 1e1eb2af5f1bdfc6093b6490c9fa662b91cf1210..462b541b41d6d7560b9ec0b792eb1f801633079b 100644
(file)
--- a/
src/shared/util.c
+++ b/
src/shared/util.c
@@
-72,7
+72,7
@@
int saved_argc = 0;
char **saved_argv = NULL;
int saved_argc = 0;
char **saved_argv = NULL;
-static
int pars
ed_columns = 0;
+static
volatile unsigned cach
ed_columns = 0;
size_t page_size(void) {
static __thread size_t pgsz = 0;
size_t page_size(void) {
static __thread size_t pgsz = 0;
@@
-1959,7
+1959,7
@@
char *format_timestamp(char *buf, size_t l, usec_t t) {
sec = (time_t) (t / USEC_PER_SEC);
sec = (time_t) (t / USEC_PER_SEC);
- if (strftime(buf, l, "%a, %
d %b %Y %H:%M:%S %z
", localtime_r(&sec, &tm)) <= 0)
+ if (strftime(buf, l, "%a, %
Y-%m-%d %H:%M:%S %Z
", localtime_r(&sec, &tm)) <= 0)
return NULL;
return buf;
return NULL;
return buf;
@@
-3793,46
+3793,31
@@
int fd_columns(int fd) {
return ws.ws_col;
}
return ws.ws_col;
}
-static unsigned columns_cached(bool cached) {
- static __thread int env_columns = -1;
+unsigned columns(void) {
const char *e;
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 c
olumns_cached(false)
;
+ cached_columns = c;
+ return c;
}
/* intended to be used as a SIGWINCH sighandler */
void columns_cache_reset(int signum) {
}
/* intended to be used as a SIGWINCH sighandler */
void columns_cache_reset(int signum) {
-
pars
ed_columns = 0;
+
cach
ed_columns = 0;
}
int fd_lines(int fd) {
}
int fd_lines(int fd) {
@@
-5929,7
+5914,7
@@
bool string_is_safe(const char *p) {
assert(p);
for (t = p; *t; t++) {
assert(p);
for (t = p; *t; t++) {
- if (*t < ' ')
+ if (*t
> 0 && *t
< ' ')
return false;
if (strchr("\\\"\'", *t))
return false;
if (strchr("\\\"\'", *t))