LPdir_unix.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. */
  27. #include <stddef.h>
  28. #include <stdlib.h>
  29. #include <limits.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include <dirent.h>
  33. #include <errno.h>
  34. #ifndef LPDIR_H
  35. #include "LPdir.h"
  36. #endif
  37. /* The POSIXly macro for the maximum number of characters in a file path
  38. is NAME_MAX. However, some operating systems use PATH_MAX instead.
  39. Therefore, it seems natural to first check for PATH_MAX and use that,
  40. and if it doesn't exist, use NAME_MAX. */
  41. #if defined(PATH_MAX)
  42. # define LP_ENTRY_SIZE PATH_MAX
  43. #elif defined(NAME_MAX)
  44. # define LP_ENTRY_SIZE NAME_MAX
  45. #endif
  46. /* Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
  47. exist. It's also possible that NAME_MAX exists but is define to a
  48. very small value (HP-UX offers 14), so we need to check if we got a
  49. result, and if it meets a minimum standard, and create or change it
  50. if not. */
  51. #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
  52. # undef LP_ENTRY_SIZE
  53. # define LP_ENTRY_SIZE 255
  54. #endif
  55. struct LP_dir_context_st
  56. {
  57. DIR *dir;
  58. char entry_name[LP_ENTRY_SIZE+1];
  59. };
  60. const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
  61. {
  62. struct dirent *direntry = NULL;
  63. if (ctx == NULL || directory == NULL)
  64. {
  65. errno = EINVAL;
  66. return 0;
  67. }
  68. errno = 0;
  69. if (*ctx == NULL)
  70. {
  71. *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
  72. if (*ctx == NULL)
  73. {
  74. errno = ENOMEM;
  75. return 0;
  76. }
  77. memset(*ctx, '\0', sizeof(LP_DIR_CTX));
  78. (*ctx)->dir = opendir(directory);
  79. if ((*ctx)->dir == NULL)
  80. {
  81. int save_errno = errno; /* Probably not needed, but I'm paranoid */
  82. free(*ctx);
  83. *ctx = NULL;
  84. errno = save_errno;
  85. return 0;
  86. }
  87. }
  88. direntry = readdir((*ctx)->dir);
  89. if (direntry == NULL)
  90. {
  91. return 0;
  92. }
  93. strncpy((*ctx)->entry_name, direntry->d_name, sizeof((*ctx)->entry_name) - 1);
  94. (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
  95. return (*ctx)->entry_name;
  96. }
  97. int LP_find_file_end(LP_DIR_CTX **ctx)
  98. {
  99. if (ctx != NULL && *ctx != NULL)
  100. {
  101. int ret = closedir((*ctx)->dir);
  102. free(*ctx);
  103. switch (ret)
  104. {
  105. case 0:
  106. return 1;
  107. case -1:
  108. return 0;
  109. default:
  110. break;
  111. }
  112. }
  113. errno = EINVAL;
  114. return 0;
  115. }