1 /*
2 * linelength.c: find the terminal line length
3 * Preferences: 1. MANWIDTH, 2. COLUMNS, 3. ioctl, 4. 80
4 *
5 * Originally adapted from Andries Brouwer's man implementation, also
6 * released under the GPL: authors believed to include Martin Schulze and
7 * Jon Tombs, dated 1995/09/02.
8 *
9 * Changes for man-db copyright (C) 2001, 2003, 2007 Colin Watson.
10 *
11 * This file is part of man-db.
12 *
13 * man-db is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * man-db is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with man-db; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <termios.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40
41 #include "linelength.h"
42
43 #ifndef _PATH_TTY
44 # define _PATH_TTY "/dev/tty"
45 #endif /* _PATH_TTY */
46
47 static int line_length = -1;
48
49 int get_line_length (void)
50 {
51 const char *columns;
52 int width;
53 #ifdef TIOCGWINSZ
54 int dev_tty, tty_fd = -1;
55 #endif
56
57 if (line_length != -1)
58 return line_length;
59
60 line_length = 80;
61
62 columns = getenv ("MANWIDTH");
63 if (columns != NULL) {
64 width = atoi (columns);
65 if (width > 0)
66 return line_length = width;
67 }
68
69 columns = getenv ("COLUMNS");
70 if (columns != NULL) {
71 width = atoi (columns);
72 if (width > 0)
73 return line_length = width;
74 }
75
76 #ifdef TIOCGWINSZ
77 /* Original TIOCGWINSZ approach was from Jon Tombs.
78 * We don't require both stdin and stdout to be a tty, and line
79 * length is primarily a property of output. However, if it happens
80 * that stdin is connected to a terminal but stdout isn't, then that
81 * may well be because the user is trying something like
82 * 'MAN_KEEP_STDERR=1 man foo >/dev/null' to see just the error
83 * messages, so use the window size from stdin as a fallback.
84 * In some cases we may have neither (e.g. if man is running inside
85 * lesspipe); /dev/tty should be a reliable way to get to the
86 * current tty if it exists.
87 */
88 dev_tty = open (_PATH_TTY, O_RDONLY);
89 if (dev_tty >= 0)
90 tty_fd = dev_tty;
91 else if (isatty (STDOUT_FILENO))
92 tty_fd = STDOUT_FILENO;
93 else if (isatty (STDIN_FILENO))
94 tty_fd = STDIN_FILENO;
95 if (tty_fd >= 0) {
96 int ret;
97 struct winsize wsz;
98
99 ret = ioctl (tty_fd, TIOCGWINSZ, &wsz);
100 if (dev_tty >= 0)
101 close (dev_tty);
102 if (ret)
103 perror ("TIOCGWINSZ failed");
104 else if (wsz.ws_col)
105 return line_length = wsz.ws_col;
106 }
107 #endif
108
109 return line_length = 80;
110 }