summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/db/dirent_local.c
blob: 9b6c26cb3407e46dd36346e08af0dee8b664c26a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*									*/
/* dirent_local.c							*/
/*									*/
/*    POSIX directory routines for Windows.				*/
/*									*/
/* Part of the ht://Dig package   <http://www.htdig.org/>		*/
/* Copyright (c) 2003 The ht://Dig Group				*/
/* For copyright details, see the file COPYING in your distribution	*/
/* or the GNU Library General Public License version 2 or later		*/
/* <http://www.gnu.org/copyleft/lgpl.html>				*/
/*									*/
/* $Id: direct_local.c 							*/
/*									*/

/* 
 * POSIX directory routines for Windows.
 *
 * Added by Neal Richter, RightNow Technologies
 * June 2003
 * 
 */

#ifdef _MSC_VER /* _WIN32 */

#include <windows.h>
#include <iostream.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include "dirent_local.h"


/* ----- opendir ------------------------------------- Oct 23, 1999  21:52 ---
 * Open a directory handle 
 */
DIR *
opendir(const char *name)
{
    DIR *dirp;
    char dirpath[256];

    if ((dirp=malloc(sizeof(struct DIRstruct))) == NULL) {
        errno = ENOMEM;
        return(NULL);
    }

    sprintf(dirpath, "%s/*", name);
    if ((dirp->filehand = FindFirstFile(dirpath, &dirp->finddata)) ==
        INVALID_HANDLE_VALUE) {
        errno = ENOENT;
        return(NULL);
    }

    dirp->priv = 1;

    return(dirp);
}


/* ----- readdir ------------------------------------- Oct 23, 1999  21:52 ---
 * Return the next file for a directory handle
 */
struct dirent *
readdir(DIR *dirp)
{
    if (!dirp) {
        errno = EBADF;
        return(NULL);
    }

    if (dirp->priv) {
        /* this is the first time, so return the results of FindFirstFile */
        dirp->priv = 0;
        strcpy(dirp->file.d_name, dirp->finddata.cFileName);
        return(&dirp->file);
    }

    /* this is a subsequent call so get next file */
    if (!FindNextFile(dirp->filehand, &dirp->finddata)) 
        return(NULL);

    strcpy(dirp->file.d_name, dirp->finddata.cFileName);
    return(&dirp->file);
}


/* ----- closedir ------------------------------------ Oct 23, 1999  21:52 ---
 * Close directory handle
 */
int
closedir(DIR *dirp)
{
    if (!dirp) {
        errno = EBADF;
        return(-1);
    }

    FindClose(dirp->filehand);

    free(dirp);

    return(0);
}

#endif /* _MSC_VER (WIN32) */