summaryrefslogtreecommitdiffstats
path: root/lib/ttyutils.c
diff options
context:
space:
mode:
authorKarel Zak2012-11-22 14:26:41 +0100
committerKarel Zak2012-11-22 14:26:41 +0100
commit4e76adb0e1aed7b8a094d26adcef793b51ce252b (patch)
tree29730e24ef5102f911a755bee0750b91e40e6071 /lib/ttyutils.c
parentbuild-sys: use libcommon.a for lslocks (diff)
downloadkernel-qcow2-util-linux-4e76adb0e1aed7b8a094d26adcef793b51ce252b.tar.gz
kernel-qcow2-util-linux-4e76adb0e1aed7b8a094d26adcef793b51ce252b.tar.xz
kernel-qcow2-util-linux-4e76adb0e1aed7b8a094d26adcef793b51ce252b.zip
lib/ttyutils: create .c file
Well, now all tty stuff are incline functions in include/ttyutils.h. It's seems more elegant to create regular lib/ttyutils.c for libcommon and write test program. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/ttyutils.c')
-rw-r--r--lib/ttyutils.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/ttyutils.c b/lib/ttyutils.c
new file mode 100644
index 000000000..d37c168ae
--- /dev/null
+++ b/lib/ttyutils.c
@@ -0,0 +1,39 @@
+
+#include <ctype.h>
+
+#include "c.h"
+#include "ttyutils.h"
+
+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) {
+ char *end = NULL;
+ long c;
+
+ errno = 0;
+ c = strtol(cp, &end, 10);
+
+ if (errno == 0 && end && *end == '\0' && end > cp &&
+ c > 0 && c <= INT_MAX)
+ return c;
+ }
+ return 0;
+}
+