llvm.org GIT mirror llvm / d7be663
Always check to see if raw_fd_ostream's file descriptor is attached to a terminal, not just when it's STDOUT_FILENO. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79066 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
1 changed file(s) with 16 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
7171
7272 void raw_ostream::SetBuffered() {
7373 // Ask the subclass to determine an appropriate buffer size.
74 SetBufferSize(preferred_buffer_size());
74 if (size_t Size = preferred_buffer_size())
75 SetBufferSize(Size);
76 else
77 // It may return 0, meaning this stream should be unbuffered.
78 SetUnbuffered();
7579 }
7680
7781 void raw_ostream::SetBufferSize(size_t Size) {
314318 if (Binary)
315319 sys::Program::ChangeStdoutToBinary();
316320 ShouldClose = false;
317 // Mimic stdout by defaulting to unbuffered if the output device
318 // is a terminal.
319 if (sys::Process::StandardOutIsDisplayed())
320 SetUnbuffered();
321321 return;
322322 }
323323
374374 #if !defined(_MSC_VER) // Windows reportedly doesn't have st_blksize.
375375 assert(FD >= 0 && "File not yet open!");
376376 struct stat statbuf;
377 if (fstat(FD, &statbuf) == 0)
377 if (fstat(FD, &statbuf) == 0) {
378 // If this is a terminal, don't use buffering. Line buffering
379 // would be a more traditional thing to do, but it's not worth
380 // the complexity.
381 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
382 return 0;
383 // Return the preferred block size.
378384 return statbuf.st_blksize;
385 }
379386 error_detected();
380387 #endif
381388 return raw_ostream::preferred_buffer_size();
415422 //===----------------------------------------------------------------------===//
416423
417424 // Set buffer settings to model stdout and stderr behavior.
418 // raw_ostream doesn't support line buffering, so set standard output to be
419 // unbuffered when the output device is a terminal. Set standard error to
420 // be unbuffered.
421 raw_stdout_ostream::raw_stdout_ostream()
422 : raw_fd_ostream(STDOUT_FILENO, false,
423 sys::Process::StandardOutIsDisplayed()) {}
424 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
425 // Set standard error to be unbuffered by default.
426 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
427 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
425428 true) {}
426429
427430 // An out of line virtual method to provide a home for the class vtable.