filesystem.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // First microsoft compilers
  2. #include <windows.h>
  3. #include <io.h>
  4. #include <ctype.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <libtarint/filesystem.h>
  12. kwDirectory * kwOpenDir(const char* name)
  13. {
  14. // struct _KWDIR ssss;
  15. char* buf;
  16. size_t n = strlen(name);
  17. kwDirectory * dir = (kwDirectory *)malloc(sizeof (kwDirectory));
  18. if(dir==NULL)
  19. {
  20. return NULL;
  21. }
  22. dir->EOD=0; //not the end of directory
  23. if ( name[n - 1] == '/' )
  24. {
  25. buf = (char*) malloc(n + 1 + 1);
  26. // buf = new char[n + 1 + 1];
  27. sprintf(buf, "%s*", name);
  28. }
  29. else
  30. {
  31. buf = (char*)malloc(n + 2 + 1);
  32. // buf = new char[n + 2 + 1];
  33. sprintf(buf, "%s/*", name);
  34. }
  35. // Now put them into the file array
  36. dir->SrchHandle = _findfirst(buf, &dir->Entry);
  37. free(buf);
  38. if ( dir->SrchHandle == -1 )
  39. {
  40. free(dir);
  41. return NULL;
  42. }
  43. return dir;
  44. }
  45. kwDirEntry * kwReadDir(kwDirectory * dir)
  46. {
  47. static kwDirEntry entry;
  48. if(!dir || dir->EOD ==1)
  49. {
  50. return NULL;
  51. }
  52. strncpy(entry.d_name,dir->Entry.name,TAR_MAXPATHLEN-1);
  53. if(_findnext(dir->SrchHandle, &dir->Entry) == -1)
  54. {
  55. dir->EOD=1;
  56. }
  57. // It is both stupid and dangerous to return a pointer to a static like this.
  58. // This can only be called by one caller at a time: i.e., it's not thread safe.
  59. // On the other hand, it mimics the documented behavior of "readdir" which is
  60. // what it's implemented to replace for platforms that do not have readdir.
  61. // Memory leaks are also stupid and dangerous... perhaps this is less so.
  62. //
  63. return &entry;
  64. }
  65. int kwCloseDir(kwDirectory * dir)
  66. {
  67. int r=-1;
  68. if(dir)
  69. {
  70. r=_findclose(dir->SrchHandle);
  71. free(dir);
  72. }
  73. if(r==-1) return 0;
  74. return 1;
  75. }