Ignore:
Timestamp:
Mar 24, 2018, 4:32:26 PM (7 years ago)
Author:
bird
Message:

kmk,lib,kWorker: Console output on windows cleanups.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/maybe_con_write.c

    r3065 r3188  
    55
    66/*
    7  * Copyright (c) 2016 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
     7 * Copyright (c) 2016-2018 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
    88 *
    99 * Permission is hereby granted, free of charge, to any person obtaining a
     
    3333*   Header Files                                                                                                                 *
    3434*********************************************************************************************************************************/
     35#include "console.h"
    3536#ifdef KBUILD_OS_WINDOWS
    3637# include <windows.h>
     
    3839#include <errno.h>
    3940#ifdef _MSC_VER
    40 # include <io.h>
    4141# include <conio.h>
    42 typedef intptr_t ssize_t;
    4342typedef unsigned int to_write_t;
    4443#else
    45 # include <unistd.h>
    4644typedef size_t to_write_t;
    4745#endif
     
    5654 * @param   cbToWrite           How much to write.
    5755 */
    58 ssize_t maybe_con_write(int fd, void *pvBuf, size_t cbToWrite)
     56ssize_t maybe_con_write(int fd, void const *pvBuf, size_t cbToWrite)
    5957{
    6058    ssize_t cbWritten;
     59
    6160#ifdef KBUILD_OS_WINDOWS
    6261    /*
     
    6463     * call WriteConsoleW directly.
    6564     */
    66     if (cbToWrite > 0 && isatty(fd))
     65    if (cbToWrite > 0)
    6766    {
    6867        HANDLE hCon = (HANDLE)_get_osfhandle(fd);
     
    7069            && hCon != NULL)
    7170        {
    72             size_t   cwcTmp  = cbToWrite * 2 + 16;
    73             wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
    74             if (pawcTmp)
     71            if (is_console_handle((intptr_t)hCon))
    7572            {
    76                 int           cwcToWrite;
    77                 static UINT s_uConsoleCp = 0;
    78                 if (s_uConsoleCp == 0)
    79                     s_uConsoleCp = GetConsoleCP();
     73                size_t   cwcTmp  = cbToWrite * 2 + 16;
     74                wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
     75                if (pawcTmp)
     76                {
     77                    int           cwcToWrite;
     78                    static UINT s_uConsoleCp = 0;
     79                    if (s_uConsoleCp == 0)
     80                        s_uConsoleCp = GetConsoleCP();
    8081
    81                 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, pawcTmp, (int)(cwcTmp - 1));
    82                 if (cwcToWrite > 0)
    83                 {
    84                     /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
    85                        sources indicates _cputws will do the right thing we want.  */
    86                     pawcTmp[cwcToWrite] = '\0';
    87                     if (_cputws(pawcTmp) >= 0)
    88                         return cbToWrite;
    89                     return -1;
     82                    cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite,
     83                                                     pawcTmp, (int)(cwcTmp - 1));
     84                    if (cwcToWrite > 0)
     85                    {
     86                        /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
     87                           sources indicates _cputws will do the right thing.  */
     88                        pawcTmp[cwcToWrite] = '\0';
     89                        if (_cputws(pawcTmp) >= 0)
     90                            return cbToWrite;
     91                        return -1;
     92                    }
    9093                }
    9194            }
     
    115118    return cbWritten;
    116119}
     120
Note: See TracChangeset for help on using the changeset viewer.