+/* Return how long the file at fd is, if there's any way to determine it. */
+#ifdef UNUSED
+off_t FAST_FUNC fdlength(int fd)
+{
+ off_t bottom = 0, top = 0, pos;
+ long size;
+
+ // If the ioctl works for this, return it.
+
+ if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
+
+ // FIXME: explain why lseek(SEEK_END) is not used here!
+
+ // If not, do a binary search for the last location we can read. (Some
+ // block devices don't do BLKGETSIZE right.)
+
+ do {
+ char temp;
+
+ pos = bottom + (top - bottom) / 2;
+
+ // If we can read from the current location, it's bigger.
+
+ if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
+ if (bottom == top) bottom = top = (top+1) * 2;
+ else bottom = pos;
+
+ // If we can't, it's smaller.
+
+ } else {
+ if (bottom == top) {
+ if (!top) return 0;
+ bottom = top/2;
+ }
+ else top = pos;
+ }
+ } while (bottom + 1 != top);
+
+ return pos + 1;
+}
+#endif
+
+/* It is perfectly ok to pass in a NULL for either width or for
+ * height, in which case that value will not be set. */
+int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
+{
+ struct winsize win = { 0, 0, 0, 0 };
+ int ret = ioctl(fd, TIOCGWINSZ, &win);
+
+ if (height) {
+ if (!win.ws_row) {
+ char *s = getenv("LINES");
+ if (s) win.ws_row = atoi(s);
+ }
+ if (win.ws_row <= 1 || win.ws_row >= 30000)
+ win.ws_row = 24;
+ *height = (int) win.ws_row;
+ }
+
+ if (width) {
+ if (!win.ws_col) {
+ char *s = getenv("COLUMNS");
+ if (s) win.ws_col = atoi(s);
+ }
+ if (win.ws_col <= 1 || win.ws_col >= 30000)
+ win.ws_col = 80;
+ *width = (int) win.ws_col;
+ }
+
+ return ret;
+}