为什么打印帮助信息时此代码仅使用一半的屏幕?

时间:2014-07-25 14:28:38

标签: c linux terminal

由于某些奇怪的原因,此代码在打印帮助消息时不会使用全屏。为什么会发生这种情况,我该如何解决呢。

示例:没有黑客COLUMNS=80

Usage: watchdogd [OPTION]
A watchdog daemon for linux.

  -F, --foreground  run in foreground mode
  -b, --softboot    ignore file open errors
  -s, --sync        sync file-systems regularly
  -f, --force       force a ping interval or timeout 
                      even if the ping interval is less
                      than the timeout
  -c, --config-file path to configuration file
  -V, --version     print version info
  -h, --help        this help

示例:使用hack COLUMNS=80

Usage: watchdogd [OPTION]
A watchdog daemon for linux.

  -F, --foreground  run in foreground mode
  -b, --softboot    ignore file open errors
  -s, --sync        sync file-systems regularly
  -f, --force       force a ping interval or timeout even if the ping interval
                      is less than the timeout
  -c, --config-file path to configuration file
  -V, --version     print version info
  -h, --help        this help

代码:

int GetConsoleColumns(void)
{
    struct winsize w = { 0 };
    if (ioctl(0, TIOCGWINSZ, &w) < 0) {
        return 80;
    }

    return w.ws_col;
}

    static int PrintHelp(void)
    {
    //Emulate the gnu --help output.
        static const char *const help[][2] = {
            {"Usage: " PACKAGE_NAME " [OPTION]", ""},
            {"A watchdog daemon for linux.", ""},
            {"", ""},
            {"  -F, --foreground", "run in foreground mode"},
            {"  -b, --softboot", "ignore file open errors"},
            {"  -s, --sync", "sync file-systems regularly"},
            {"  -f, --force",
             "force a ping interval or timeout even if the ping interval is less than the timeout"},
            {"  -c, --config-file", "path to configuration file"},
            {"  -V, --version", "print version info"},
            {"  -h, --help", "this help"},
            {NULL, NULL}
        };

        for (int i = 0; help[i][0] != NULL; i += 1) {
            long col = GetConsoleColumns();
            if (col >= 80) { //Without this bit of code the program uses a litte more than half of the screen.
                col += col / 2;
            }
            long len = 0;
            len += printf("%-20s", help[i][0]);

            char *ptr = strndup(help[i][1], strlen(help[i][1]));

            if (ptr == NULL) {
                perror(PACKAGE_NAME);
                return -1;
            }

            char *save = NULL;
            char *tmp = strtok_r(ptr, " ", &save);

            while (tmp != NULL) {
                len += strlen(tmp);
                if (len > col) {
                    len = 0;
                    printf("%s", "\n");
                    len = printf("                      ");
                    len += printf("%s", tmp);
                    tmp = strtok_r(NULL, " ", &save);
                    if (tmp != NULL) {
                        len += printf(" ");
                    }
                } else {
                    len += printf("%s", tmp);
                    tmp = strtok_r(NULL, " ", &save);
                    if (tmp != NULL) {
                        len += printf(" ");
                    }
                }
            }

            free(ptr);
            printf("\n");
        }





        return 0;
    }

0 个答案:

没有答案