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