bn/asm/x86_64-mont5.pl: fix carry bug in bn_sqrx8x_internal.
[openssl.git] / crypto / LPdir_unix.c
1 /*
2  * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <limits.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #ifndef LPDIR_H
35 # include "LPdir.h"
36 #endif
37
38 /*
39  * The POSIXly macro for the maximum number of characters in a file path is
40  * NAME_MAX.  However, some operating systems use PATH_MAX instead.
41  * Therefore, it seems natural to first check for PATH_MAX and use that, and
42  * if it doesn't exist, use NAME_MAX.
43  */
44 #if defined(PATH_MAX)
45 # define LP_ENTRY_SIZE PATH_MAX
46 #elif defined(NAME_MAX)
47 # define LP_ENTRY_SIZE NAME_MAX
48 #endif
49
50 /*
51  * Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
52  * exist.  It's also possible that NAME_MAX exists but is define to a very
53  * small value (HP-UX offers 14), so we need to check if we got a result, and
54  * if it meets a minimum standard, and create or change it if not.
55  */
56 #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
57 # undef LP_ENTRY_SIZE
58 # define LP_ENTRY_SIZE 255
59 #endif
60
61 struct LP_dir_context_st {
62     DIR *dir;
63     char entry_name[LP_ENTRY_SIZE + 1];
64 };
65
66 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
67 {
68     struct dirent *direntry = NULL;
69
70     if (ctx == NULL || directory == NULL) {
71         errno = EINVAL;
72         return 0;
73     }
74
75     errno = 0;
76     if (*ctx == NULL) {
77         *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
78         if (*ctx == NULL) {
79             errno = ENOMEM;
80             return 0;
81         }
82         memset(*ctx, '\0', sizeof(LP_DIR_CTX));
83
84         (*ctx)->dir = opendir(directory);
85         if ((*ctx)->dir == NULL) {
86             int save_errno = errno; /* Probably not needed, but I'm paranoid */
87             free(*ctx);
88             *ctx = NULL;
89             errno = save_errno;
90             return 0;
91         }
92     }
93
94     direntry = readdir((*ctx)->dir);
95     if (direntry == NULL) {
96         return 0;
97     }
98
99     strncpy((*ctx)->entry_name, direntry->d_name,
100             sizeof((*ctx)->entry_name) - 1);
101     (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
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         int ret = closedir((*ctx)->dir);
109
110         free(*ctx);
111         switch (ret) {
112         case 0:
113             return 1;
114         case -1:
115             return 0;
116         default:
117             break;
118         }
119     }
120     errno = EINVAL;
121     return 0;
122 }