#if defined (WIN32)

#include <process.h>
#include <direct.h>
#include <malloc.h>
#include "winstuff.h"

DIR *opendir ( const char *dirname )
{
    char fullpath[MAX_PATH];
    int iCnt, iTmp = strlen ( dirname );
    DIR *dirstr;

    if ( dirname == NULL || iTmp == 0 )
        return NULL;

    dirstr = ( DIR * ) malloc ( sizeof ( DIR ) );

    if ( dirstr == NULL )
        return NULL;

    strcpy ( fullpath, dirname );

    for ( iCnt = 0; iCnt < iTmp; iCnt++ )
        if ( fullpath[iCnt] == '/' )
            fullpath[iCnt] = '\\';

    if ( fullpath[iTmp - 1] == '\\' )
        strcat ( fullpath, "*.*" );
    else
        strcat ( fullpath, "\\*.*" );

    dirstr->d_firstread = TRUE;
    dirstr->Data =
        FindFirstFile ( ( LPCTSTR ) ( fullpath ), &( dirstr->FindData ) );

    // This may be don't correct...
    if ( dirstr->Data == INVALID_HANDLE_VALUE )
    {
        free ( dirstr );
        return NULL;
    }

    return dirstr;
}

struct dirent *readdir ( DIR * dirstream )
{
    if ( dirstream == NULL )
        return NULL;

    // First read
    if ( dirstream->d_firstread )
        dirstream->d_firstread = FALSE; // We already have readed name
    else
    {
        if ( !FindNextFile ( dirstream->Data, &( dirstream->FindData ) ) )
            return NULL;
    }

    // copy and return
    strcpy ( dirstream->d_name, dirstream->FindData.cFileName );
    return ( struct dirent * ) dirstream;
}

int closedir ( DIR * dirstream )
{
    if ( dirstream == NULL )
        return -1;
    FindClose ( dirstream->Data );
    free ( dirstream );
    return 0;
}

int strcasecmp ( const char *s1, const char *s2 )
{
    register const u_char *us1 = ( const u_char * ) s1, *us2 =
        ( const u_char * ) s2;

    while ( tolower ( *us1 ) == tolower ( *us2++ ) )
        if ( *us1++ == '\0' )
            return ( 0 );
    return ( tolower ( *us1 ) - tolower ( *--us2 ) );
}

int strncasecmp ( const char *s1, const char *s2, register size_t n )
{
    if ( n != 0 )
    {
        register const u_char *us1 = ( const u_char * ) s1, *us2 =
            ( const u_char * ) s2;

        do
        {
            if ( tolower ( *us1 ) != tolower ( *us2++ ) )
                return ( tolower ( *us1 ) - tolower ( *--us2 ) );
            if ( *us1++ == '\0' )
                break;
        }
        while ( --n != 0 );
    }
    return ( 0 );
}

#endif /* WIN32 */