]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/LPdir_unix.c
2 * Copyright 2004-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * This file is dual-licensed and is also available under the following
14 * Copyright (c) 2004, 2018, Richard Levitte <richard@levitte.org>
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
43 #include <sys/types.h>
54 * The POSIX macro for the maximum number of characters in a file path is
55 * NAME_MAX. However, some operating systems use PATH_MAX instead.
56 * Therefore, it seems natural to first check for PATH_MAX and use that, and
57 * if it doesn't exist, use NAME_MAX.
60 # define LP_ENTRY_SIZE PATH_MAX
61 #elif defined(NAME_MAX)
62 # define LP_ENTRY_SIZE NAME_MAX
66 * Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
67 * exist. It's also possible that NAME_MAX exists but is define to a very
68 * small value (HP-UX offers 14), so we need to check if we got a result, and
69 * if it meets a minimum standard, and create or change it if not.
71 #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
73 # define LP_ENTRY_SIZE 255
76 struct LP_dir_context_st
{
78 char entry_name
[LP_ENTRY_SIZE
+ 1];
80 int expect_file_generations
;
81 char previous_entry_name
[LP_ENTRY_SIZE
+ 1];
85 const char *LP_find_file(LP_DIR_CTX
**ctx
, const char *directory
)
87 struct dirent
*direntry
= NULL
;
89 if (ctx
== NULL
|| directory
== NULL
) {
96 *ctx
= malloc(sizeof(**ctx
));
101 memset(*ctx
, 0, sizeof(**ctx
));
105 char c
= directory
[strlen(directory
) - 1];
107 if (c
== ']' || c
== '>' || c
== ':')
108 (*ctx
)->expect_file_generations
= 1;
112 (*ctx
)->dir
= opendir(directory
);
113 if ((*ctx
)->dir
== NULL
) {
114 int save_errno
= errno
; /* Probably not needed, but I'm paranoid */
123 strncpy((*ctx
)->previous_entry_name
, (*ctx
)->entry_name
,
124 sizeof((*ctx
)->previous_entry_name
));
129 direntry
= readdir((*ctx
)->dir
);
130 if (direntry
== NULL
) {
134 OPENSSL_strlcpy((*ctx
)->entry_name
, direntry
->d_name
,
135 sizeof((*ctx
)->entry_name
));
137 if ((*ctx
)->expect_file_generations
) {
138 char *p
= (*ctx
)->entry_name
+ strlen((*ctx
)->entry_name
);
140 while (p
> (*ctx
)->entry_name
&& isdigit((unsigned char)p
[-1]))
142 if (p
> (*ctx
)->entry_name
&& p
[-1] == ';')
144 if (OPENSSL_strcasecmp((*ctx
)->entry_name
,
145 (*ctx
)->previous_entry_name
) == 0)
149 return (*ctx
)->entry_name
;
152 int LP_find_file_end(LP_DIR_CTX
**ctx
)
154 if (ctx
!= NULL
&& *ctx
!= NULL
) {
155 int ret
= closedir((*ctx
)->dir
);