LPdir_unix.c 4.9 KB

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