From 509046a4fe5ee23f46837462b36dae7e52af5d15 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Wed, 17 Aug 2022 11:29:20 +0200 Subject: [PATCH] sar: Add new environment variable S_REPEAT_HEADER (#332) Add new environment variable S_REPEAT_HEADER. This variable will contain the number of lines after which a header has to be displayed by sar when stdout is not a terminal. Signed-off-by: Sebastien GODARD --- common.c | 15 ++++++++++++++- common.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/common.c b/common.c index 94e94b5..b92be22 100644 --- a/common.c +++ b/common.c @@ -637,6 +637,8 @@ int print_gal_header(struct tm *rectime, char *sysname, char *release, /* *************************************************************************** * Get number of rows for current window. + * If stdout is not a terminal then use the value given by environment + * variable S_REPEAT_HEADER if existent. * * RETURNS: * Number of rows. @@ -645,17 +647,28 @@ int print_gal_header(struct tm *rectime, char *sysname, char *release, int get_win_height(void) { struct winsize win; + char *e; /* * This default value will be used whenever STDOUT - * is redirected to a pipe or a file + * is redirected to a pipe or a file and S_REPEAT_HEADER variable is not set */ int rows = 3600 * 24; + /* Get number of lines of current terminal */ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1) { if (win.ws_row > 2) { rows = win.ws_row - 2; } } + /* STDOUT is not a terminal. Look for S_REPEAT_HEADER variable's value instead */ + else if ((e = __getenv(ENV_REPEAT_HEADER)) != NULL) { + if (strspn(e, DIGITS) == strlen(e)) { + int v = atol(e); + if (v > 0) { + rows = v; + } + } + } return rows; } diff --git a/common.h b/common.h index 9aed879..7efc7ca 100644 --- a/common.h +++ b/common.h @@ -108,6 +108,7 @@ #define ENV_TIME_DEFTM "S_TIME_DEF_TIME" #define ENV_COLORS "S_COLORS" #define ENV_COLORS_SGR "S_COLORS_SGR" +#define ENV_REPEAT_HEADER "S_REPEAT_HEADER" #define C_NEVER "never" #define C_ALWAYS "always" -- 2.40.0