<commit_before>/*

    Implementation of POSIX directory browsing functions and types for Win32.

    Author:  <NAME> (<EMAIL>, <EMAIL>)
    History: Created March 1997. Updated June 2003 and July 2012.
    Rights:  See end of file.

*/
#include <dirent_on_windows.h>
#include <errno.h>
#include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
#include <stdlib.h>
#include <string.h>
#include "vk_loader_platform.h"
#include "loader.h"

#ifdef __cplusplus
extern "C"
{
#endif

typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */

struct DIR
{
    handle_type         handle; /* -1 for failed rewind */
    struct _finddata_t  info;
    struct dirent       result; /* d_name null iff first time */
    char                *name;  /* null-terminated char string */
};

DIR *opendir(const char *name)
{
    DIR *dir = 0;

    if(name && name[0])
    {
        size_t base_length = strlen(name);
        const char *all = /* search pattern must end with suitable wildcard */
            strchr("/\\", name[base_length - 1]) ? "*" : "/*";

        if((dir = (DIR *) loader_tls_heap_alloc(sizeof *dir)) != 0 &&
           (dir->name = (char *) loader_tls_heap_alloc(base_length + strlen(all) + 1)) != 0)
        {
            strcat(strcpy(dir->name, name), all);

            if((dir->handle =
                (handle_type) _findfirst(dir->name, &dir->info)) != -1)
            {
                dir->result.d_name = 0;
            }
            else /* rollback */
            {
                loader_tls_heap_free(dir->name);
                loader_tls_heap_free(dir);
                dir = 0;
            }
        }
        else /* rollback */
        {
            loader_tls_heap_free(dir);
            dir   = 0;
            errno = ENOMEM;
        }
    }
    else
    {
        errno = EINVAL;
    }

    return dir;
}

int closedir(DIR *dir)
{
    int result = -1;

    if(dir)
    {
        if(dir->handle != -1)
        {
            result = _findclose(dir->handle);
        }

        free(dir->name);
        free(dir);<commit_msg>loader: match free to alloc in dirent functions<commit_after>/*

    Implementation of POSIX directory browsing functions and types for Win32.

    Author:  <NAME> (<EMAIL>, <EMAIL>)
    History: Created March 1997. Updated June 2003 and July 2012.
    Rights:  See end of file.

*/
#include <dirent_on_windows.h>
#include <errno.h>
#include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
#include <stdlib.h>
#include <string.h>
#include "vk_loader_platform.h"
#include "loader.h"

#ifdef __cplusplus
extern "C"
{
#endif

typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */

struct DIR
{
    handle_type         handle; /* -1 for failed rewind */
    struct _finddata_t  info;
    struct dirent       result; /* d_name null iff first time */
    char                *name;  /* null-terminated char string */
};

DIR *opendir(const char *name)
{
    DIR *dir = 0;

    if(name && name[0])
    {
        size_t base_length = strlen(name);
        const char *all = /* search pattern must end with suitable wildcard */
            strchr("/\\", name[base_length - 1]) ? "*" : "/*";

        if((dir = (DIR *) loader_tls_heap_alloc(sizeof *dir)) != 0 &&
           (dir->name = (char *) loader_tls_heap_alloc(base_length + strlen(all) + 1)) != 0)
        {
            strcat(strcpy(dir->name, name), all);

            if((dir->handle =
                (handle_type) _findfirst(dir->name, &dir->info)) != -1)
            {
                dir->result.d_name = 0;
            }
            else /* rollback */
            {
                loader_tls_heap_free(dir->name);
                loader_tls_heap_free(dir);
                dir = 0;
            }
        }
        else /* rollback */
        {
            loader_tls_heap_free(dir);
            dir   = 0;
            errno = ENOMEM;
        }
    }
    else
    {
        errno = EINVAL;
    }

    return dir;
}

int closedir(DIR *dir)
{
    int result = -1;

    if(dir)
    {
        if(dir->handle != -1)
        {
            result = _findclose(dir->handle);
        }

        loader_tls_heap_free(dir->name);
        loader_tls_heap_free(dir);
{'fraction_non_alphanumeric': 0.12900943396226416, 'fraction_numerical': 0.014150943396226415, 'mean_word_length': 1.7938076416337285, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5078971', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1339, 'n_words': 340}