如何从Linux获取Windows大小 [英] How to get windows size from Linux

查看:74
本文介绍了如何从Linux获取Windows大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人.我还是编程新手.我真的需要一些关于我面临的问题的帮助.所以,这里的情况是我试图在终端大小小于80x24时显示警告.作为记录,我的操作系统是Window,但是我使用的是虚拟机来运行Linux,因为所有文件都在Linux中.当我使用终端运行文件时,警告显示正确.但是问题是当我尝试使用PuTTY从Windows运行文件时.该警告未出现.我确定是因为我使用的功能只能读取Linux环境,而不能读取Windows.任何人都可以帮助我或为我指明如何使其能够获得窗户尺寸的方向.文件应全部保留在Linux中.我正在使用C.

everyone. I am still new to programming. I really need some help about the issues that I'm facing. So, the situation here is I'm trying to display a warning when the terminal size is below 80x24. For the record, my Os is Window, but I'm using a virtual machine to run Linux because all the files are in Linux. When i run the file using terminal, the warning display correctly. But the problem is when i try to run the file from Windows using PuTTY. The warning did not appear. I'm sure its because the function that I'm using can only read the Linux environment and not the Windows. Can anyone help me or point me to a direction on how to make it capable of getting the dimension of windows. The files should all remain in Linux. I am using C.

这里只是显示有关显示警告和获取尺寸的代码的一部分.

Here are just some part of the code to show about displaying warning and getting dimension.

//This is to display warning


int display_warning()
{
 CDKSCREEN *cdkscreen = 0;
 WINDOW *cursesWin    = 0;
 char *mesg[5];
 char *buttons[] = {"[ OK ]"};
 CDKDIALOG *confirm;

 cursesWin = initscr();
 cdkscreen = initCDKScreen (cursesWin);

 initCDKColor();

 mesg[0] = "</2>"The size of Window must be at least 80x24.";
 confirm = newCDKDialog(cdkscreen, CENTER, CENTER, mesg, 1, buttons, A_REVERSE, TRUE,TRUE, FALSE);
 setCDKDialogBackgroundColor(confirm, "</2>");
 injectCDKDialog(confirm,TAB);
 activateCDKDialog(confirm,0);

 if (confirm -> exitType == vNORMAL){
 destroyCDKDialog (confirm);
 destroyCDKScreen (cdkscreen);
 endCDK();
 }
 return 0;
}



//This is to get the dimension

int get_terminal_size()
{
 int cols;
 int lines;

 #ifdef TIOCGSIZE
 struct ttysize ts;
 ioctl(0,TIOCGSIZE, &ts);
 lines = ts.ts_linesl;
 cols = ts.ts_cols;

 #elif defined(TIOCGWINSZ)
 struct winsize ts;
 ioctl(0, TIOCGWINSZ, &ts);
 lines = ts.ws_row;
 cols = ts.ws_col;

 #endif

 if((lines <= 23)||(cols <= 79)){
 display_warning();
 }

 return 0;
}

//then there will be the main function that i think is not necessary to put the code here.

所有评论和帮助都非常感谢.我是编程的初学者,所以如果有一些我不知道的基本知识,请原谅.

All comment and help are very appreciated. I am a beginner in programming, so please excuse me if there are some basic things that i dont know.

Fikrie

推荐答案

此问题本身与PuTTY无关,而与SSH客户端和伪终端通常无关.

The issue has nothing to do with PuTTY per se, and everything to do with SSH clients and pseudoterminals in general.

为避免此问题,请将您的PuTTY配置为使用伪终端.(在TTY面板中,有一个不分配伪终端" 复选框.确保未选中它.)

To avoid this issue, configure your PuTTY to use a pseudoterminal. (In the TTY panel, there is a "Don't allocate a pseudoterminal" checkbox. Make sure it is not checked.)

对于 ssh ,您需要使用 -t 选项来告知 ssh 使用伪终端.

With ssh, you need to use the -t option to tell ssh to use a pseudoterminal.

这是一个简单的示例程序,您可以在Linux中使用它来获取终端大小.它不需要诅咒:

Here is a simple example program you can use in Linux to obtain the terminal size. It does not require curses:

#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdio.h>

static int get_size(const int fd, int *const rows, int *const cols)
{
    struct winsize sz;
    int            result;

    do {
        result = ioctl(fd, TIOCGWINSZ, &sz);
    } while (result == -1 && errno == EINTR);
    if (result == -1)
        return errno;

    if (rows)
        *rows = sz.ws_row;

    if (cols)
        *cols = sz.ws_col;

    return 0;
}

int main(void)
{
    int rows, cols;

    if (!get_size(STDIN_FILENO,  &rows, &cols) ||
        !get_size(STDOUT_FILENO, &rows, &cols) ||
        !get_size(STDERR_FILENO, &rows, &cols))
        printf("%d rows, %d columns\n", rows, cols);
    else
        fprintf(stderr, "Terminal size is unknown.\n");
    return 0;
}

使用 TIOCGWINSZ TTY ioctl获取实际信息.

The actual information is obtained using the TIOCGWINSZ TTY ioctl.

伪终端的大小实际上是由内核维护的.如果没有伪终端,则只有标准流,就没有行和列;如果没有伪终端,则没有行和列.在那种情况下,这只是一条小溪尤其是,甚至 tput行 tput cols 都将失败.

The pseudoterminal size is actually maintained by the kernel. If there is no pseudoterminal, just standard streams, there are no rows and columns; it's just a stream in that case. In particular, even tput lines and tput cols will fail then.

如果没有伪终端,许多交互式命令行程序将拒绝运行.例如, top 将报告类似未设置TERM环境变量" "top:tty失败获取" 之类的内容.其他人可以工作,只是不能交互;他们只会输出一次,但就好像终端无限高和无限宽.

Many interactive command-line programs will refuse to work if there is no pseudoterminal. For example, top will report something like "TERM environment variable not set" or "top: failed tty get". Others will work, just not interactively; they'll output once only, but as if the terminal was infinitely tall and infinitely wide.

总而言之,您的应用程序应该识别它是在伪终端(已知终端大小,可能支持curses等)中运行,还是在流模式下(通过SSH或PuTTY,有意没有伪终端)运行,或者也许仅仅是因为输入和输出都直接指向文件或类似文件).

In summary, your application should recognize whether it is running in a pseudoterminal (with terminal size known, curses support possible, and so on), or in stream mode (via SSH or PuTTY, deliberately without a pseudoterminal -- or perhaps just because inputs and outputs are all directed to/from files or some such).

这篇关于如何从Linux获取Windows大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆