LPdir_unix.c 3.8 KB

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