All the output assumes the terminal width is 80 cols min, check the terminal size in order to detect a smaller terminal and prevent to have a nested output.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- idlestat.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
diff --git a/idlestat.c b/idlestat.c index a0ef2dd..245be9e 100644 --- a/idlestat.c +++ b/idlestat.c @@ -34,6 +34,7 @@ #include <sched.h> #include <string.h> #include <float.h> +#include <sys/ioctl.h> #include <sys/time.h> #include <sys/types.h> #include <sys/resource.h> @@ -1380,6 +1381,23 @@ static int execute(int argc, char *argv[], char *const envp[], return -1; }
+static int check_window_size(void) +{ + struct winsize winsize; + + /* Output is redirected */ + if (!isatty(STDOUT_FILENO)) + return 0; + + /* Get terminal window size */ + ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize); + + if (winsize.ws_col >= 80) + return 0; + + return -1; +} + int main(int argc, char *argv[], char *const envp[]) { struct cpuidle_datas *datas; @@ -1397,6 +1415,12 @@ int main(int argc, char *argv[], char *const envp[]) return -1; }
+ if (check_window_size()) { + fprintf(stderr, "The terminal must be at least " + "80 columns wide\n"); + return -1; + } + /* init cpu topoinfo */ init_cpu_topo_info();