<commit_before>// bsls_outputredirector.cpp                                          -*-C++-*-
#include <bsls_outputredirector.h>

#include <bsls_ident.h>
BSLS_IDENT("$Id$ $CSID$")

#include <bsls_assert.h>
#include <bsls_bsltestutil.h>   // for testing only

#include <algorithm>

#include <stdlib.h>
#include <string.h>

#include <sys/types.h> // 'struct stat': required on Sun and Windows only

#if defined(BSLS_PLATFORM_OS_WINDOWS)
# include <fcntl.h>     // '_O_BINARY'
# include <io.h>        // '_dup2', '_dup', '_close'
# define snprintf _snprintf
#else
# include <unistd.h>
# include <stdint.h>    // 'SIZE_MAX', cannot include on all Windows platforms
# include <stdlib.h>    // 'mkstemp'
#endif

#ifndef SIZE_MAX
#define SIZE_MAX (static_cast<size_t>(-1))
    // 'SIZE_MAX' is only defined as part of C99, so it may not exist in some
    // pre-C++11 compilers.
#endif

namespace BloombergLP {
namespace bsls {

// PRIVATE MANIPULATORS
void OutputRedirector::cleanup()
{
    disable();
    cleanupFiles();
}

void OutputRedirector::cleanupFiles()
{
#ifdef BSLS_PLATFORM_OS_WINDOWS
    _close(d_duplicatedOriginalFd);
#else
    close(d_duplicatedOriginalFd);
#endif

    d_duplicatedOriginalFd = -1;

    if (d_isFileCreatedFlag) {
        unlink(d_fileName);
        d_isFileCreatedFlag = false;
    }
}

bool OutputRedirector::generateTempFileName()
{
#ifdef BSLS_PLATFORM_OS_WINDOWS
    char tmpPathBuf[k_PATH_BUFFER_SIZE];
    if (! GetTempPathA(k_PATH_BUFFER_SIZE, tmpPathBuf) ||
        ! GetTempFileNameA(tmpPathBuf, "bsls", 0, d_fileName)) {
        return false;                                                 // RETURN
    }
#else
    static const char patternBuf[] = { "/tmp/bslsXXXXXX" };

    std::fill(d_fileName, d_fileName + sizeof(d_fileName), 0xa5);
    ::strncpy(d_fileName, patternBuf, sizeof(d_fileName));
    if ('\0' != d_fileName[sizeof(d_fileName) - 1]) {
        return false;                                                 // RETURN
    }
    if (0 != ::strcmp(d_fileName, patternBuf)) {
        return false;                                                 // RETURN
    }

    const int fd = ::mkstemp(d_fileName);
    if (fd < 0) {
        // 'mkstemp' failed.

        return false;                                                 // RETURN
    }
    int rc = ::close(fd);
    if (0 != rc) {
        return false;                                                 // RETURN
    }
    if ('\0' != d_fileName[sizeof(d_fileName) - 1]) {
        return false;                                                 // RETURN
    }
#endif<commit_msg>bsls_outputredirector: lengthen temp name pattern, doc change<commit_after>// bsls_outputredirector.cpp                                          -*-C++-*-
#include <bsls_outputredirector.h>

#include <bsls_ident.h>
BSLS_IDENT("$Id$ $CSID$")

#include <bsls_assert.h>
#include <bsls_bsltestutil.h>   // for testing only

#include <algorithm>

#include <stdlib.h>
#include <string.h>

#include <sys/types.h> // 'struct stat': required on Sun and Windows only

#if defined(BSLS_PLATFORM_OS_WINDOWS)
# include <fcntl.h>     // '_O_BINARY'
# include <io.h>        // '_dup2', '_dup', '_close'
# define snprintf _snprintf
#else
# include <unistd.h>
# include <stdint.h>    // 'SIZE_MAX', cannot include on all Windows platforms
# include <stdlib.h>    // 'mkstemp'
#endif

#ifndef SIZE_MAX
#define SIZE_MAX (static_cast<size_t>(-1))
    // 'SIZE_MAX' is only defined as part of C99, so it may not exist in some
    // pre-C++11 compilers.
#endif

namespace BloombergLP {
namespace bsls {

// PRIVATE MANIPULATORS
void OutputRedirector::cleanup()
{
    disable();
    cleanupFiles();
}

void OutputRedirector::cleanupFiles()
{
#ifdef BSLS_PLATFORM_OS_WINDOWS
    _close(d_duplicatedOriginalFd);
#else
    close(d_duplicatedOriginalFd);
#endif

    d_duplicatedOriginalFd = -1;

    if (d_isFileCreatedFlag) {
        unlink(d_fileName);
        d_isFileCreatedFlag = false;
    }
}

bool OutputRedirector::generateTempFileName()
{
#ifdef BSLS_PLATFORM_OS_WINDOWS
    char tmpPathBuf[k_PATH_BUFFER_SIZE];
    if (! GetTempPathA(k_PATH_BUFFER_SIZE, tmpPathBuf) ||
        ! GetTempFileNameA(tmpPathBuf, "bsls", 0, d_fileName)) {
        return false;                                                 // RETURN
    }
#else
    static const char patternBuf[] = { "/tmp/bsls_outputredirector_XXXXXX" };

    // Fill 'd_fileName' with garbage.

    std::fill(d_fileName, d_fileName + sizeof(d_fileName), 0xa5);

    // Copy in the pattern.

    ::strncpy(d_fileName, patternBuf, sizeof(d_fileName));
    if ('\0' != d_fileName[sizeof(d_fileName) - 1]) {
        return false;                                                 // RETURN
    }
    if (0 != ::strcmp(d_fileName, patternBuf)) {
        return false;                                                 // RETURN
    }

    const int fd = ::mkstemp(d_fileName);
    if (fd < 0) {
        // 'mkstemp' failed.

        return false;                                                 // RETURN
    }
    int rc = ::close(fd);
    if (0 != rc) {
        return false;                                                 // RETURN
    }
    if ('\0' != d_fileName[sizeof(d_fileName) - 1]) {
        return false;                                                 // RETURN
    }
#endif
{'fraction_non_alphanumeric': 0.12164714771439365, 'fraction_numerical': 0.006422364941443143, 'mean_word_length': 2.0326460481099655, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2260433', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1682, 'n_words': 377}