Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

directory_posix.c 2.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #define _POSIX_C_SOURCE 1 /* for readdir_r */
  27. #include "directory.h"
  28. #if !defined(OPENSSL_WINDOWS)
  29. #include <dirent.h>
  30. #include <errno.h>
  31. struct OPENSSL_dir_context_st {
  32. DIR *dir;
  33. struct dirent dirent;
  34. };
  35. const char *OPENSSL_DIR_read(OPENSSL_DIR_CTX **ctx, const char *directory) {
  36. struct dirent *dirent;
  37. if (ctx == NULL || directory == NULL) {
  38. errno = EINVAL;
  39. return NULL;
  40. }
  41. errno = 0;
  42. if (*ctx == NULL) {
  43. *ctx = malloc(sizeof(OPENSSL_DIR_CTX));
  44. if (*ctx == NULL) {
  45. errno = ENOMEM;
  46. return 0;
  47. }
  48. memset(*ctx, 0, sizeof(OPENSSL_DIR_CTX));
  49. (*ctx)->dir = opendir(directory);
  50. if ((*ctx)->dir == NULL) {
  51. int save_errno = errno; /* Probably not needed, but I'm paranoid */
  52. free(*ctx);
  53. *ctx = NULL;
  54. errno = save_errno;
  55. return 0;
  56. }
  57. }
  58. if (readdir_r((*ctx)->dir, &(*ctx)->dirent, &dirent) != 0 ||
  59. dirent == NULL) {
  60. return 0;
  61. }
  62. return (*ctx)->dirent.d_name;
  63. }
  64. int OPENSSL_DIR_end(OPENSSL_DIR_CTX **ctx) {
  65. if (ctx != NULL && *ctx != NULL) {
  66. int r = closedir((*ctx)->dir);
  67. free(*ctx);
  68. *ctx = NULL;
  69. return r == 0;
  70. }
  71. errno = EINVAL;
  72. return 0;
  73. }
  74. #endif /* !OPENSSL_WINDOWS */