PageRenderTime 80ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/user/lynx/lynx2-8-6.5/lib/dirent.c

https://bitbucket.org/thelearninglabs/uclinux-distro-tll-public
C | 277 lines | 197 code | 45 blank | 35 comment | 72 complexity | 1b1d0366d0cf305f4c07e0b2e210c32f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, Unlicense, GPL-2.0, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0, ISC, MIT, 0BSD, LGPL-2.0
  1. /*
  2. dir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87
  3. */
  4. /* #ifdef WIN32 */
  5. /*
  6. * @(#)dir.c 1.4 87/11/06 Public Domain.
  7. *
  8. * A public domain implementation of BSD directory routines for
  9. * MS-DOS. Written by Michael Rendell ({uunet,utai}michael@garfield),
  10. * August 1897
  11. * Ported to OS/2 by Kai Uwe Rommel
  12. * December 1989, February 1990
  13. * Ported to Windows NT 22 May 91
  14. * other mods Summer '92 brianmo@microsoft.com
  15. * opendirx() was horribly written, very inefficient, and did not take care
  16. * of all cases. It is still not too clean, but it is far more efficient.
  17. * Changes made by Gordon Chaffee (chaffee@bugs-bunny.cs.berkeley.edu)
  18. */
  19. /*Includes:
  20. * crt
  21. */
  22. #include <windows.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys\types.h>
  26. #include <sys\stat.h>
  27. #include "dirent.h"
  28. #define stat _stat
  29. /*
  30. * NT specific
  31. */
  32. #include <stdio.h>
  33. /*
  34. * random typedefs
  35. */
  36. #define HDIR HANDLE
  37. #define HFILE HANDLE
  38. #define PHFILE PHANDLE
  39. /*
  40. * local functions
  41. */
  42. static char *getdirent(char *);
  43. static void free_dircontents(struct _dircontents *);
  44. static HDIR FindHandle;
  45. static WIN32_FIND_DATA FileFindData;
  46. static struct dirent dp;
  47. DIR *opendirx(char *name, char *pattern)
  48. {
  49. struct stat statb;
  50. DIR *dirp;
  51. char c;
  52. char *s;
  53. struct _dircontents *dp;
  54. int len;
  55. int unc;
  56. char path[OFS_MAXPATHNAME];
  57. register char *ip, *op;
  58. for (ip = name, op = path;; op++, ip++) {
  59. *op = *ip;
  60. if (*ip == '\0') {
  61. break;
  62. }
  63. }
  64. len = ip - name;
  65. if (len > 0) {
  66. unc = ((path[0] == '\\' || path[0] == '/') &&
  67. (path[1] == '\\' || path[1] == '/'));
  68. c = path[len - 1];
  69. if (unc) {
  70. if (c != '\\' && c != '/') {
  71. path[len] = '/';
  72. len++;
  73. path[len] = '\0';
  74. }
  75. } else {
  76. if ((c == '\\' || c == '/') && (len > 1)) {
  77. len--;
  78. path[len] = '\0';
  79. if (path[len - 1] == ':') {
  80. path[len] = '/';
  81. len++;
  82. path[len] = '.';
  83. len++;
  84. path[len] = '\0';
  85. }
  86. } else if (c == ':') {
  87. path[len] = '.';
  88. len++;
  89. path[len] = '\0';
  90. }
  91. }
  92. } else {
  93. unc = 0;
  94. path[0] = '.';
  95. path[1] = '\0';
  96. len = 1;
  97. }
  98. if (stat(path, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR) {
  99. return NULL;
  100. }
  101. dirp = malloc(sizeof(DIR));
  102. if (dirp == NULL) {
  103. return dirp;
  104. }
  105. c = path[len - 1];
  106. if (c == '.') {
  107. if (len == 1) {
  108. len--;
  109. } else {
  110. c = path[len - 2];
  111. if (c == '\\' || c == ':') {
  112. len--;
  113. } else {
  114. path[len] = '/';
  115. len++;
  116. }
  117. }
  118. } else if (!unc && ((len != 1) || (c != '\\' && c != '/'))) {
  119. path[len] = '/';
  120. len++;
  121. }
  122. strcpy(path + len, pattern);
  123. dirp->dd_loc = 0;
  124. dirp->dd_contents = dirp->dd_cp = NULL;
  125. if ((s = getdirent(path)) == NULL) {
  126. return dirp;
  127. }
  128. do {
  129. if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
  130. ((dp->_d_entry = malloc(strlen(s) + 1)) == NULL)) {
  131. if (dp)
  132. free(dp);
  133. free_dircontents(dirp->dd_contents);
  134. return NULL;
  135. }
  136. if (dirp->dd_contents)
  137. dirp->dd_cp = dirp->dd_cp->_d_next = dp;
  138. else
  139. dirp->dd_contents = dirp->dd_cp = dp;
  140. strcpy(dp->_d_entry, s);
  141. dp->_d_next = NULL;
  142. }
  143. while ((s = getdirent(NULL)) != NULL);
  144. dirp->dd_cp = dirp->dd_contents;
  145. return dirp;
  146. }
  147. DIR *opendir(char *name)
  148. {
  149. return opendirx(name, "*");
  150. }
  151. void closedir(DIR * dirp)
  152. {
  153. free_dircontents(dirp->dd_contents);
  154. free(dirp);
  155. }
  156. struct dirent *readdir(DIR * dirp)
  157. {
  158. /* static struct dirent dp; */
  159. if (dirp->dd_cp == NULL)
  160. return NULL;
  161. /*strcpy(dp.d_name,dirp->dd_cp->_d_entry); */
  162. dp.d_name = dirp->dd_cp->_d_entry;
  163. dp.d_namlen = dp.d_reclen =
  164. strlen(dp.d_name);
  165. dp.d_ino = dirp->dd_loc + 1; /* fake the inode */
  166. dirp->dd_cp = dirp->dd_cp->_d_next;
  167. dirp->dd_loc++;
  168. return &dp;
  169. }
  170. void seekdir(DIR * dirp, long off)
  171. {
  172. long i = off;
  173. struct _dircontents *dp;
  174. if (off >= 0) {
  175. for (dp = dirp->dd_contents; --i >= 0 && dp; dp = dp->_d_next);
  176. dirp->dd_loc = off - (i + 1);
  177. dirp->dd_cp = dp;
  178. }
  179. }
  180. long telldir(DIR * dirp)
  181. {
  182. return dirp->dd_loc;
  183. }
  184. static void free_dircontents(struct _dircontents *dp)
  185. {
  186. struct _dircontents *odp;
  187. while (dp) {
  188. if (dp->_d_entry)
  189. free(dp->_d_entry);
  190. dp = (odp = dp)->_d_next;
  191. free(odp);
  192. }
  193. }
  194. /* end of "free_dircontents" */
  195. static char *getdirent(char *dir)
  196. {
  197. int got_dirent;
  198. if (dir != NULL) { /* get first entry */
  199. if ((FindHandle = FindFirstFile(dir, &FileFindData))
  200. == (HDIR) 0xffffffff) {
  201. return NULL;
  202. }
  203. got_dirent = 1;
  204. } else /* get next entry */
  205. got_dirent = FindNextFile(FindHandle, &FileFindData);
  206. if (got_dirent)
  207. return FileFindData.cFileName;
  208. else {
  209. FindClose(FindHandle);
  210. return NULL;
  211. }
  212. }
  213. /* end of getdirent() */
  214. struct passwd *_cdecl
  215. getpwnam(char *name)
  216. {
  217. return NULL;
  218. }
  219. struct passwd *_cdecl
  220. getpwuid(int uid)
  221. {
  222. return NULL;
  223. }
  224. int getuid()
  225. {
  226. return 0;
  227. }
  228. void _cdecl
  229. endpwent(void)
  230. {
  231. }
  232. /* #endif */