< prev index next >

src/java.base/unix/native/libjava/Console_md.c

Print this page
rev 51515 : 8209937: Enhance java.io.Console - provide methods to query console width and height
Contributed-by: christoph.langer@sap.com, matthias.baesken@sap.com

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -25,15 +25,38 @@
 
 #include "jni.h"
 #include "jni_util.h"
 #include "jvm.h"
 #include "java_io_Console.h"
-
 #include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/resource.h>
 #include <unistd.h>
 #include <termios.h>
 
+JNIEXPORT jint JNICALL
+Java_java_io_Console_width(JNIEnv *env, jobject this)
+{
+    struct winsize w;
+
+    if (ioctl(0, TIOCGWINSZ, &w) == 0 || ioctl(1, TIOCGWINSZ, &w) == 0 || ioctl(2, TIOCGWINSZ, &w) == 0) {
+        return w.ws_col;
+    }
+    return -1;
+}
+
+JNIEXPORT jint JNICALL
+Java_java_io_Console_height(JNIEnv *env, jobject this)
+{
+    struct winsize w;
+
+    if (ioctl(0, TIOCGWINSZ, &w) == 0 || ioctl(1, TIOCGWINSZ, &w) == 0 || ioctl(2, TIOCGWINSZ, &w) == 0) {
+        return w.ws_row;
+    }
+    return -1;
+}
+
 JNIEXPORT jboolean JNICALL
 Java_java_io_Console_istty(JNIEnv *env, jclass cls)
 {
     return isatty(fileno(stdin)) && isatty(fileno(stdout));
 }
< prev index next >