blob: 356089d7fd34a6db3479e3fc2386853b8ea46e8c [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Richard Levittea5829ae2018-03-11 23:48:04 +01002 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
Rich Salz2039c422016-05-17 14:51:34 -04003 *
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/*
Rich Salz8d1598b2017-06-19 22:48:25 -040011 * This file is dual-licensed and is also available under the following
Rich Salz0c3d0d42017-06-15 13:15:26 -040012 * terms:
13 *
Richard Levittea5829ae2018-03-11 23:48:04 +010014 * Copyright (c) 2004, 2018, Richard Levitte <richard@levitte.org>
Richard Levittea2400fc2004-07-10 13:16:02 +000015 * 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.
Matt Caswell0f113f32015-01-22 03:40:55 +000025 *
Richard Levittebb09fd22004-09-23 22:11:39 +000026 * 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.
Richard Levittea2400fc2004-07-10 13:16:02 +000037 */
38
39#include <stddef.h>
40#include <stdlib.h>
Andy Polyakovf1bdf1d2004-07-22 10:53:26 +000041#include <limits.h>
Richard Levittea2400fc2004-07-10 13:16:02 +000042#include <string.h>
43#include <sys/types.h>
44#include <dirent.h>
45#include <errno.h>
46#ifndef LPDIR_H
Matt Caswell0f113f32015-01-22 03:40:55 +000047# include "LPdir.h"
Richard Levittea2400fc2004-07-10 13:16:02 +000048#endif
Richard Levittea5829ae2018-03-11 23:48:04 +010049#ifdef __VMS
50# include <ctype.h>
51#endif
Richard Levittea2400fc2004-07-10 13:16:02 +000052
Matt Caswell0f113f32015-01-22 03:40:55 +000053/*
54 * The POSIXly 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.
58 */
Richard Levittebb09fd22004-09-23 22:11:39 +000059#if defined(PATH_MAX)
60# define LP_ENTRY_SIZE PATH_MAX
61#elif defined(NAME_MAX)
62# define LP_ENTRY_SIZE NAME_MAX
63#endif
64
Matt Caswell0f113f32015-01-22 03:40:55 +000065/*
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.
70 */
Richard Levittebb09fd22004-09-23 22:11:39 +000071#if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
72# undef LP_ENTRY_SIZE
73# define LP_ENTRY_SIZE 255
Andy Polyakovf1bdf1d2004-07-22 10:53:26 +000074#endif
75
Matt Caswell0f113f32015-01-22 03:40:55 +000076struct LP_dir_context_st {
77 DIR *dir;
78 char entry_name[LP_ENTRY_SIZE + 1];
Richard Levittea5829ae2018-03-11 23:48:04 +010079#ifdef __VMS
80 int expect_file_generations;
81 char previous_entry_name[LP_ENTRY_SIZE + 1];
82#endif
Richard Levittea2400fc2004-07-10 13:16:02 +000083};
84
85const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
86{
Matt Caswell0f113f32015-01-22 03:40:55 +000087 struct dirent *direntry = NULL;
Richard Levittea2400fc2004-07-10 13:16:02 +000088
Matt Caswell0f113f32015-01-22 03:40:55 +000089 if (ctx == NULL || directory == NULL) {
90 errno = EINVAL;
91 return 0;
Richard Levittea2400fc2004-07-10 13:16:02 +000092 }
93
Matt Caswell0f113f32015-01-22 03:40:55 +000094 errno = 0;
95 if (*ctx == NULL) {
Rich Salzb4faea52015-05-01 23:10:31 -040096 *ctx = malloc(sizeof(**ctx));
Matt Caswell0f113f32015-01-22 03:40:55 +000097 if (*ctx == NULL) {
98 errno = ENOMEM;
99 return 0;
100 }
Rich Salz16f8d4e2015-05-04 18:00:15 -0400101 memset(*ctx, 0, sizeof(**ctx));
Richard Levittea2400fc2004-07-10 13:16:02 +0000102
Richard Levittea5829ae2018-03-11 23:48:04 +0100103#ifdef __VMS
104 {
105 char c = directory[strlen(directory) - 1];
106
107 if (c == ']' || c == '>' || c == ':')
108 (*ctx)->expect_file_generations = 1;
109 }
110#endif
111
Matt Caswell0f113f32015-01-22 03:40:55 +0000112 (*ctx)->dir = opendir(directory);
113 if ((*ctx)->dir == NULL) {
114 int save_errno = errno; /* Probably not needed, but I'm paranoid */
115 free(*ctx);
116 *ctx = NULL;
117 errno = save_errno;
118 return 0;
119 }
Richard Levittea2400fc2004-07-10 13:16:02 +0000120 }
121
Richard Levittea5829ae2018-03-11 23:48:04 +0100122#ifdef __VMS
123 strncpy((*ctx)->previous_entry_name, (*ctx)->entry_name,
124 sizeof((*ctx)->previous_entry_name));
125
126 again:
127#endif
128
Matt Caswell0f113f32015-01-22 03:40:55 +0000129 direntry = readdir((*ctx)->dir);
130 if (direntry == NULL) {
131 return 0;
Richard Levittea2400fc2004-07-10 13:16:02 +0000132 }
133
Matt Caswell0f113f32015-01-22 03:40:55 +0000134 strncpy((*ctx)->entry_name, direntry->d_name,
135 sizeof((*ctx)->entry_name) - 1);
136 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
Richard Levittea5829ae2018-03-11 23:48:04 +0100137#ifdef __VMS
138 if ((*ctx)->expect_file_generations) {
139 char *p = (*ctx)->entry_name + strlen((*ctx)->entry_name);
140
141 while(p > (*ctx)->entry_name && isdigit(p[-1]))
142 p--;
143 if (p > (*ctx)->entry_name && p[-1] == ';')
144 p[-1] = '\0';
145 if (strcasecmp((*ctx)->entry_name, (*ctx)->previous_entry_name) == 0)
146 goto again;
147 }
148#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000149 return (*ctx)->entry_name;
Richard Levittea2400fc2004-07-10 13:16:02 +0000150}
151
152int LP_find_file_end(LP_DIR_CTX **ctx)
153{
Matt Caswell0f113f32015-01-22 03:40:55 +0000154 if (ctx != NULL && *ctx != NULL) {
155 int ret = closedir((*ctx)->dir);
Richard Levittea2400fc2004-07-10 13:16:02 +0000156
Matt Caswell0f113f32015-01-22 03:40:55 +0000157 free(*ctx);
158 switch (ret) {
159 case 0:
160 return 1;
161 case -1:
162 return 0;
163 default:
164 break;
165 }
Richard Levittea2400fc2004-07-10 13:16:02 +0000166 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000167 errno = EINVAL;
168 return 0;
Richard Levittea2400fc2004-07-10 13:16:02 +0000169}