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