summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorPetr Uzel2012-05-15 10:49:03 +0200
committerKarel Zak2012-05-15 11:32:53 +0200
commite872e32064b9ade685417f6a36af08dc2baa8694 (patch)
tree1057c8638d5b6d9a94363c73aed5bbc7550b6641 /include
parentlibuuid: move read_all to include/all-io.h (diff)
downloadkernel-qcow2-util-linux-e872e32064b9ade685417f6a36af08dc2baa8694.tar.gz
kernel-qcow2-util-linux-e872e32064b9ade685417f6a36af08dc2baa8694.tar.xz
kernel-qcow2-util-linux-e872e32064b9ade685417f6a36af08dc2baa8694.zip
include: move get_terminal_width() to ttyutils.h
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
Diffstat (limited to 'include')
-rw-r--r--include/ttyutils.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/include/ttyutils.h b/include/ttyutils.h
index f638aa0d9..15809e85a 100644
--- a/include/ttyutils.h
+++ b/include/ttyutils.h
@@ -1,7 +1,11 @@
#ifndef UTIL_LINUX_TTYUTILS_H
#define UTIL_LINUX_TTYUTILS_H
+#include <stdlib.h>
#include <termios.h>
+#ifdef HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#endif
#define UL_TTY_KEEPCFLAGS (1 << 1)
#define UL_TTY_UTF8 (1 << 2)
@@ -74,4 +78,29 @@ static inline void reset_virtual_console(struct termios *tp, int flags)
tp->c_cc[VEOL2] = _POSIX_VDISABLE;
}
+static inline int get_terminal_width(void)
+{
+#ifdef TIOCGSIZE
+ struct ttysize t_win;
+#endif
+#ifdef TIOCGWINSZ
+ struct winsize w_win;
+#endif
+ const char *cp;
+
+#ifdef TIOCGSIZE
+ if (ioctl (0, TIOCGSIZE, &t_win) == 0)
+ return t_win.ts_cols;
+#endif
+#ifdef TIOCGWINSZ
+ if (ioctl (0, TIOCGWINSZ, &w_win) == 0)
+ return w_win.ws_col;
+#endif
+ cp = getenv("COLUMNS");
+ if (cp)
+ return strtol(cp, NULL, 10);
+ return 0;
+}
+
+
#endif /* UTIL_LINUX_TTYUTILS_H */