Imported from LPlib, making sure the entry name (at least on Unix) is
[openssl.git] / crypto / LPdir_win.c
1 /* $LP: LPlib/source/LPdir_win.c,v 1.3 2004/07/19 16:34:54 _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           return 0;
57         }
58       memset(*ctx, '\0', sizeof(LP_DIR_CTX));
59
60 #ifdef LP_SYS_WINCE
61       {
62         WCHAR *wdir = NULL;
63         size_t index = 0;
64
65         wdir = (WCHAR *)malloc((strlen(directory) + 1) * 2);
66         if (wdir == NULL)
67           {
68             errno = ENOMEM;
69             free(*ctx);
70             return 0;
71           }
72
73         for (index = 0; index < strlen(directory) + 1; index++)
74           wdir[index] = (short)directory[index];
75
76         (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
77
78         free(wdir);
79       }
80 #else
81       (*ctx)->handle = FindFirstFile(directory, &(*ctx)->ctx);
82 #endif
83
84       if ((*ctx)->handle == INVALID_HANDLE_VALUE)
85         {
86           free(*ctx);
87           *ctx = NULL;
88           errno = EINVAL;
89           return 0;
90         }
91     }
92   else
93     {
94       if (FindNextFile((*ctx)->handle, (*ctx)->ctx) == FALSE)
95         {
96           return 0;
97         }
98     }
99
100   strncpy((*ctx)->entry_name, (*ctx)->ctx.cFileName,
101           sizeof((*ctx)->entry_name));
102   return (*ctx)->entry_name;
103 }
104
105 int LP_find_file_end(LP_DIR_CTX **ctx)
106 {
107   if (ctx != NULL && *ctx != NULL)
108     {
109       FindClose((*ctx)->handle);
110       free(*ctx);
111       return 1;
112     }
113   errno = EINVAL;
114   return 0;
115 }