fdd955fe3863a6a401ed2dec87f30db1fbd4f051
[oweals/openssl.git] / crypto / LPdir_win.c
1 /* $LP: LPlib/source/LPdir_win.c,v 1.1 2004/06/14 10:07:56 _cvs_levitte Exp $ */
2 /*
3  * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include <windows.h>
28 #ifndef LPDIR_H
29 #include "LPdir.h"
30 #endif
31
32 struct LP_dir_context_st
33 {
34   WIN32_FIND_DATA ctx;
35   HANDLE handle;
36   char entry_name[NAME_MAX+1];
37 };
38
39 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
40 {
41   struct dirent *direntry = NULL;
42
43   if (ctx == NULL || directory == NULL)
44     {
45       errno = EINVAL;
46       return 0;
47     }
48
49   errno = 0;
50   if (*ctx == NULL)
51     {
52       *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
53       if (*ctx == NULL)
54         {
55           errno = ENOMEM;
56           free(*ctx);
57           return 0;
58         }
59       memset(*ctx, '\0', sizeof(LP_DIR_CTX));
60
61 #ifdef LP_SYS_WINCE
62       {
63         WCHAR *wdir = NULL;
64         size_t index = 0;
65
66         wdir = (WCHAR *)malloc((strlen(directory) + 1) * 2);
67         if (wdir == NULL)
68           {
69             errno = ENOMEM;
70             free(*ctx);
71             free(wdir);
72             return 0;
73           }
74
75         for (index = 0; index < strlen(directory) + 1; index++)
76           wdir[index] = (short)directory[index];
77
78         (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
79
80         free(wdir);
81       }
82 #else
83       (*ctx)->handle = FindFirstFile(directory, &(*ctx)->ctx);
84 #endif
85
86       if ((*ctx)->handle == INVALID_HANDLE_VALUE)
87         {
88           free(*ctx);
89           *ctx = NULL;
90           errno = EINVAL;
91           return 0;
92         }
93     }
94   else
95     {
96       if (FindNextFile((*ctx)->handle, (*ctx)->ctx) == FALSE)
97         {
98           return 0;
99         }
100     }
101
102   strncpy((*ctx)->entry_name, (*ctx)->ctx.cFileName,
103           sizeof((*ctx)->entry_name));
104   return (*ctx)->entry_name;
105 }
106
107 int LP_find_file_end(LP_DIR_CTX **ctx)
108 {
109   if (ctx != NULL && *ctx != NULL)
110     {
111       FindClose((*ctx)->handle);
112       free(*ctx);
113       return 1;
114     }
115   errno = EINVAL;
116   return 0;
117 }