VirtualBox

Changeset 68836 in vbox for trunk/src/VBox/Runtime/r3


Ignore:
Timestamp:
Sep 22, 2017 4:37:46 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
118090
Message:

IPRT: Added RTStrmIsTerminal and RTStrmQueryTerminalWidth.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/stream.cpp

    r62592 r68836  
    6060#ifdef RT_OS_WINDOWS
    6161# include <iprt/win/windows.h>
    62 #endif
    63 #ifndef RT_OS_WINDOWS
     62#else
    6463# include <termios.h>
    6564# include <unistd.h>
     
    553552    return rc;
    554553}
     554
     555
     556RTR3DECL(bool) RTStrmIsTerminal(PRTSTREAM pStream)
     557{
     558    AssertPtrReturn(pStream, false);
     559    AssertReturn(pStream->u32Magic == RTSTREAM_MAGIC, false);
     560
     561    if (pStream->pFile)
     562    {
     563        int fh = fileno(pStream->pFile);
     564        if (isatty(fh))
     565        {
     566#ifdef RT_OS_WINDOWS
     567            DWORD  dwMode;
     568            HANDLE hCon = (HANDLE)_get_osfhandle(fh);
     569            if (GetConsoleMode(hCon, &dwMode))
     570                return true;
     571#else
     572            return true;
     573#endif
     574        }
     575    }
     576    return false;
     577}
     578
     579
     580RTR3DECL(int) RTStrmQueryTerminalWidth(PRTSTREAM pStream, uint32_t *pcchWidth)
     581{
     582    AssertPtrReturn(pcchWidth, VERR_INVALID_HANDLE);
     583    *pcchWidth = 80;
     584
     585    AssertPtrReturn(pStream, VERR_INVALID_HANDLE);
     586    AssertReturn(pStream->u32Magic == RTSTREAM_MAGIC, VERR_INVALID_HANDLE);
     587
     588    if (pStream->pFile)
     589    {
     590        int fh = fileno(pStream->pFile);
     591        if (isatty(fh))
     592        {
     593#ifdef RT_OS_WINDOWS
     594            CONSOLE_SCREEN_BUFFER_INFO Info;
     595            HANDLE hCon = (HANDLE)_get_osfhandle(fh);
     596            RT_ZERO(Info);
     597            if (GetConsoleScreenBufferInfo(hCon, &Info))
     598            {
     599                *pcchWidth = Info.dwSize.X ? Info.dwSize.X : 80;
     600                return VINF_SUCCESS;
     601            }
     602            return RTErrConvertFromWin32(GetLastError());
     603#else
     604            struct winsize Info;
     605            RT_ZERO(Info);
     606            int rc = ioctl(fh, TIOCGWINSZ, &Info);
     607            if (rc >= 0)
     608            {
     609                *pcchWidth = Info.ws_row ? Info.ws_row : 80;
     610                return VINF_SUCCESS;
     611            }
     612            return RTErrConvertFromErrno(errno);
     613#endif
     614        }
     615    }
     616    return VERR_INVALID_FUNCTION;
     617}
     618
    555619
    556620
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette