Skip to content

Commit 08e098f

Browse files
Sirraidefschlimb
authored andcommitted
[LLVM] [Support] Query the terminal width using ioctl() (llvm#143514)
On unix systems, we were trying to determine the terminal width using the `COULMNS` environment variable. Unfortunately, `COLUMNS` is not exported by all shells and thus not available on some systems. We were previously using `ioctl()` for this; fall back to doing so if `COLUMNS` does not exist or does not store a positive integer. This essentially reverts a3eb3d3 and parts of https://reviews.llvm.org/D61326. For more information, see llvm#139499. Fixes llvm#139499.
1 parent 73d01bb commit 08e098f

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

llvm/cmake/config-ix.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ if (ANDROID OR CYGWIN OR CMAKE_SYSTEM_NAME MATCHES "AIX|DragonFly|FreeBSD|Haiku|
1919
set(HAVE_SYS_MMAN_H 1)
2020
set(HAVE_SYSEXITS_H 1)
2121
set(HAVE_UNISTD_H 1)
22+
set(HAVE_SYS_IOCTL_H 1)
2223
elseif (APPLE)
2324
set(HAVE_MACH_MACH_H 1)
2425
set(HAVE_MALLOC_MALLOC_H 1)
2526
set(HAVE_PTHREAD_H 1)
2627
set(HAVE_SYS_MMAN_H 1)
2728
set(HAVE_SYSEXITS_H 1)
2829
set(HAVE_UNISTD_H 1)
30+
set(HAVE_SYS_IOCTL_H 1)
2931
elseif (WIN32)
3032
set(HAVE_MACH_MACH_H 0)
3133
set(HAVE_MALLOC_MALLOC_H 0)
3234
set(HAVE_PTHREAD_H 0)
3335
set(HAVE_SYS_MMAN_H 0)
3436
set(HAVE_SYSEXITS_H 0)
3537
set(HAVE_UNISTD_H 0)
38+
set(HAVE_SYS_IOCTL_H 0)
3639
elseif (ZOS)
3740
# Confirmed in
3841
# https://github.com/llvm/llvm-project/pull/104706#issuecomment-2297109613
@@ -42,6 +45,7 @@ elseif (ZOS)
4245
set(HAVE_SYS_MMAN_H 1)
4346
set(HAVE_SYSEXITS_H 0)
4447
set(HAVE_UNISTD_H 1)
48+
set(HAVE_SYS_IOCTL_H 1)
4549
else()
4650
# Other platforms that we don't promise support for.
4751
check_include_file(mach/mach.h HAVE_MACH_MACH_H)
@@ -50,6 +54,7 @@ else()
5054
check_include_file(sys/mman.h HAVE_SYS_MMAN_H)
5155
check_include_file(sysexits.h HAVE_SYSEXITS_H)
5256
check_include_file(unistd.h HAVE_UNISTD_H)
57+
check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
5358
endif()
5459

5560
if( UNIX AND NOT (APPLE OR BEOS OR HAIKU) )

llvm/include/llvm/Config/config.h.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
/* Define to 1 if you have the <sys/mman.h> header file. */
165165
#cmakedefine HAVE_SYS_MMAN_H ${HAVE_SYS_MMAN_H}
166166

167+
/* Define to 1 if you have the <sys/ioctl.h> header file. */
168+
#cmakedefine HAVE_SYS_IOCTL_H ${HAVE_SYS_IOCTL_H}
169+
167170
/* Define to 1 if stat struct has st_mtimespec member .*/
168171
#cmakedefine HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC ${HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC}
169172

llvm/lib/Support/Unix/Process.inc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#ifdef HAVE_GETAUXVAL
3535
#include <sys/auxv.h>
3636
#endif
37+
#ifdef HAVE_SYS_IOCTL_H
38+
#include <sys/ioctl.h>
39+
#endif
3740

3841
//===----------------------------------------------------------------------===//
3942
//=== WARNING: Implementation here must contain only generic UNIX code that
@@ -304,31 +307,40 @@ bool Process::FileDescriptorIsDisplayed(int fd) {
304307
#endif
305308
}
306309

307-
static unsigned getColumns() {
310+
static unsigned getColumns(int FileID) {
308311
// If COLUMNS is defined in the environment, wrap to that many columns.
312+
// This matches GCC.
309313
if (const char *ColumnsStr = std::getenv("COLUMNS")) {
310314
int Columns = std::atoi(ColumnsStr);
311315
if (Columns > 0)
312316
return Columns;
313317
}
314318

315-
// We used to call ioctl TIOCGWINSZ to determine the width. It is considered
316-
// unuseful.
317-
return 0;
319+
// Some shells do not export COLUMNS; query the column count via ioctl()
320+
// instead if it isn't available.
321+
unsigned Columns = 0;
322+
323+
#ifdef HAVE_SYS_IOCTL_H
324+
struct winsize ws;
325+
if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
326+
Columns = ws.ws_col;
327+
#endif
328+
329+
return Columns;
318330
}
319331

320332
unsigned Process::StandardOutColumns() {
321333
if (!StandardOutIsDisplayed())
322334
return 0;
323335

324-
return getColumns();
336+
return getColumns(0);
325337
}
326338

327339
unsigned Process::StandardErrColumns() {
328340
if (!StandardErrIsDisplayed())
329341
return 0;
330342

331-
return getColumns();
343+
return getColumns(1);
332344
}
333345

334346
static bool terminalHasColors() {

0 commit comments

Comments
 (0)