/usr/src/lib/libc/port/gen/readdir_r.c

https://github.com/buffygb/illumos-gate · C · 202 lines · 126 code · 31 blank · 45 comment · 23 complexity · fbcd33723b38e0a06254cff2ff788cd7 MD5 · raw file

  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  23. * Use is subject to license terms.
  24. */
  25. /* Copyright (c) 1988 AT&T */
  26. /* All Rights Reserved */
  27. #pragma ident "%Z%%M% %I% %E% SMI"
  28. /*
  29. * readdir_r -- C library extension routine
  30. */
  31. #include <sys/feature_tests.h>
  32. #if !defined(_LP64)
  33. #pragma weak _readdir64_r = readdir64_r
  34. #endif
  35. #include "lint.h"
  36. #include "libc.h"
  37. #include <mtlib.h>
  38. #include <unistd.h>
  39. #include <dirent.h>
  40. #include <string.h>
  41. #include <limits.h>
  42. #include <errno.h>
  43. #ifdef _LP64
  44. /*
  45. * POSIX.1c standard version of the thread function readdir_r.
  46. */
  47. int
  48. readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
  49. {
  50. private_DIR *pdirp = (private_DIR *)dirp;
  51. dirent_t *dp; /* -> directory data */
  52. int saveloc = 0;
  53. lmutex_lock(&pdirp->dd_lock);
  54. if (dirp->dd_size != 0) {
  55. dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
  56. saveloc = dirp->dd_loc; /* save for possible EOF */
  57. dirp->dd_loc += (int)dp->d_reclen;
  58. }
  59. if (dirp->dd_loc >= dirp->dd_size)
  60. dirp->dd_loc = dirp->dd_size = 0;
  61. if (dirp->dd_size == 0 && /* refill buffer */
  62. (dirp->dd_size = getdents(dirp->dd_fd,
  63. (dirent_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
  64. if (dirp->dd_size == 0) { /* This means EOF */
  65. dirp->dd_loc = saveloc; /* so save for telldir */
  66. lmutex_unlock(&pdirp->dd_lock);
  67. *result = NULL;
  68. return (0);
  69. }
  70. lmutex_unlock(&pdirp->dd_lock);
  71. *result = NULL;
  72. return (errno); /* error */
  73. }
  74. dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
  75. (void) memcpy(entry, dp, (size_t)dp->d_reclen);
  76. lmutex_unlock(&pdirp->dd_lock);
  77. *result = entry;
  78. return (0);
  79. }
  80. #else /* _LP64 */
  81. /*
  82. * POSIX.1c standard version of the thr function readdir_r.
  83. * Large file version.
  84. */
  85. int
  86. readdir64_r(DIR *dirp, dirent64_t *entry, dirent64_t **result)
  87. {
  88. private_DIR *pdirp = (private_DIR *)(uintptr_t)dirp;
  89. dirent64_t *dp64; /* -> directory data */
  90. int saveloc = 0;
  91. lmutex_lock(&pdirp->dd_lock);
  92. if (dirp->dd_size != 0) {
  93. dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
  94. /* was converted by readdir and needs to be reversed */
  95. if (dp64->d_ino == (ino64_t)-1) {
  96. dirent_t *dp32; /* -> 32 bit directory data */
  97. dp32 = (dirent_t *)(&dp64->d_off);
  98. dp64->d_ino = (ino64_t)dp32->d_ino;
  99. dp64->d_off = (off64_t)dp32->d_off;
  100. dp64->d_reclen = (unsigned short)(dp32->d_reclen +
  101. ((char *)&dp64->d_off - (char *)dp64));
  102. }
  103. saveloc = dirp->dd_loc; /* save for possible EOF */
  104. dirp->dd_loc += (int)dp64->d_reclen;
  105. }
  106. if (dirp->dd_loc >= dirp->dd_size)
  107. dirp->dd_loc = dirp->dd_size = 0;
  108. if (dirp->dd_size == 0 && /* refill buffer */
  109. (dirp->dd_size = getdents64(dirp->dd_fd,
  110. (dirent64_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
  111. if (dirp->dd_size == 0) { /* This means EOF */
  112. dirp->dd_loc = saveloc; /* so save for telldir */
  113. lmutex_unlock(&pdirp->dd_lock);
  114. *result = NULL;
  115. return (0);
  116. }
  117. lmutex_unlock(&pdirp->dd_lock);
  118. *result = NULL;
  119. return (errno); /* error */
  120. }
  121. dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
  122. (void) memcpy(entry, dp64, (size_t)dp64->d_reclen);
  123. *result = entry;
  124. lmutex_unlock(&pdirp->dd_lock);
  125. return (0);
  126. }
  127. /*
  128. * POSIX.1c standard version of the function readdir_r.
  129. * User gets it via static readdir_r from header file.
  130. */
  131. int
  132. __posix_readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
  133. {
  134. int error;
  135. dirent64_t *dp64;
  136. struct {
  137. dirent64_t dirent64;
  138. char chars[MAXNAMLEN];
  139. } buf;
  140. error = readdir64_r(dirp, (dirent64_t *)&buf, &dp64);
  141. if (error != 0 || dp64 == NULL) {
  142. *result = NULL;
  143. return (error);
  144. }
  145. if (dp64->d_ino > SIZE_MAX ||
  146. (uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) {
  147. *result = NULL;
  148. return (EOVERFLOW);
  149. }
  150. entry->d_ino = (ino_t)dp64->d_ino;
  151. entry->d_off = (off_t)dp64->d_off;
  152. entry->d_reclen = (unsigned short)((((char *)entry->d_name -
  153. (char *)entry) + strlen(dp64->d_name) + 1 + 3) & ~3);
  154. (void) strcpy(entry->d_name, dp64->d_name);
  155. *result = entry;
  156. return (0);
  157. }
  158. /*
  159. * POSIX.1c Draft-6 version of the function readdir_r.
  160. * It was implemented by Solaris 2.3.
  161. */
  162. dirent_t *
  163. readdir_r(DIR *dirp, dirent_t *entry)
  164. {
  165. int error;
  166. dirent_t *result;
  167. if ((error = __posix_readdir_r(dirp, entry, &result)) != 0)
  168. errno = error;
  169. return (result);
  170. }
  171. #endif /* _LP64 */