You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

109 lines
3.0 KiB

  1. /* $LP: LPlib/source/LPdir_unix.c,v 1.11 2004/09/23 22:07:22 _cvs_levitte Exp $ */
  2. /*
  3. * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  26. #if !defined(_POSIX_C_SOURCE)
  27. #define _POSIX_C_SOURCE 201409 /* for readdir_r */
  28. #endif
  29. #include "directory.h"
  30. #if !defined(OPENSSL_WINDOWS)
  31. #include <dirent.h>
  32. #include <errno.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #if defined(OPENSSL_PNACL)
  36. /* pnacl doesn't include readdir_r! So we do the best we can. */
  37. int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
  38. errno = 0;
  39. *result = readdir(dirp);
  40. if (*result != NULL) {
  41. return 0;
  42. }
  43. if (errno) {
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. #endif
  49. struct OPENSSL_dir_context_st {
  50. DIR *dir;
  51. struct dirent dirent;
  52. };
  53. const char *OPENSSL_DIR_read(OPENSSL_DIR_CTX **ctx, const char *directory) {
  54. struct dirent *dirent;
  55. if (ctx == NULL || directory == NULL) {
  56. errno = EINVAL;
  57. return NULL;
  58. }
  59. errno = 0;
  60. if (*ctx == NULL) {
  61. *ctx = malloc(sizeof(OPENSSL_DIR_CTX));
  62. if (*ctx == NULL) {
  63. errno = ENOMEM;
  64. return 0;
  65. }
  66. memset(*ctx, 0, sizeof(OPENSSL_DIR_CTX));
  67. (*ctx)->dir = opendir(directory);
  68. if ((*ctx)->dir == NULL) {
  69. int save_errno = errno; /* Probably not needed, but I'm paranoid */
  70. free(*ctx);
  71. *ctx = NULL;
  72. errno = save_errno;
  73. return 0;
  74. }
  75. }
  76. if (readdir_r((*ctx)->dir, &(*ctx)->dirent, &dirent) != 0 ||
  77. dirent == NULL) {
  78. return 0;
  79. }
  80. return (*ctx)->dirent.d_name;
  81. }
  82. int OPENSSL_DIR_end(OPENSSL_DIR_CTX **ctx) {
  83. if (ctx != NULL && *ctx != NULL) {
  84. int r = closedir((*ctx)->dir);
  85. free(*ctx);
  86. *ctx = NULL;
  87. return r == 0;
  88. }
  89. errno = EINVAL;
  90. return 0;
  91. }
  92. #endif /* !OPENSSL_WINDOWS */