LPdir_unix.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <stddef.h>
  27. #include <stdlib.h>
  28. #include <limits.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <dirent.h>
  32. #include <errno.h>
  33. #ifndef LPDIR_H
  34. # include "LPdir.h"
  35. #endif
  36. /*
  37. * The POSIXly macro for the maximum number of characters in a file path is
  38. * NAME_MAX. However, some operating systems use PATH_MAX instead.
  39. * Therefore, it seems natural to first check for PATH_MAX and use that, and
  40. * if it doesn't exist, use NAME_MAX.
  41. */
  42. #if defined(PATH_MAX)
  43. # define LP_ENTRY_SIZE PATH_MAX
  44. #elif defined(NAME_MAX)
  45. # define LP_ENTRY_SIZE NAME_MAX
  46. #endif
  47. /*
  48. * Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
  49. * exist. It's also possible that NAME_MAX exists but is define to a very
  50. * small value (HP-UX offers 14), so we need to check if we got a result, and
  51. * if it meets a minimum standard, and create or change it if not.
  52. */
  53. #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
  54. # undef LP_ENTRY_SIZE
  55. # define LP_ENTRY_SIZE 255
  56. #endif
  57. struct LP_dir_context_st {
  58. DIR *dir;
  59. char entry_name[LP_ENTRY_SIZE + 1];
  60. };
  61. const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
  62. {
  63. struct dirent *direntry = NULL;
  64. if (ctx == NULL || directory == NULL) {
  65. errno = EINVAL;
  66. return 0;
  67. }
  68. errno = 0;
  69. if (*ctx == NULL) {
  70. *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
  71. if (*ctx == NULL) {
  72. errno = ENOMEM;
  73. return 0;
  74. }
  75. memset(*ctx, '\0', sizeof(LP_DIR_CTX));
  76. (*ctx)->dir = opendir(directory);
  77. if ((*ctx)->dir == NULL) {
  78. int save_errno = errno; /* Probably not needed, but I'm paranoid */
  79. free(*ctx);
  80. *ctx = NULL;
  81. errno = save_errno;
  82. return 0;
  83. }
  84. }
  85. direntry = readdir((*ctx)->dir);
  86. if (direntry == NULL) {
  87. return 0;
  88. }
  89. strncpy((*ctx)->entry_name, direntry->d_name,
  90. sizeof((*ctx)->entry_name) - 1);
  91. (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
  92. return (*ctx)->entry_name;
  93. }
  94. int LP_find_file_end(LP_DIR_CTX **ctx)
  95. {
  96. if (ctx != NULL && *ctx != NULL) {
  97. int ret = closedir((*ctx)->dir);
  98. free(*ctx);
  99. switch (ret) {
  100. case 0:
  101. return 1;
  102. case -1:
  103. return 0;
  104. default:
  105. break;
  106. }
  107. }
  108. errno = EINVAL;
  109. return 0;
  110. }