ENH: make this work for older versions of OSX
[cmake.git] / Utilities / cmtar / filesystem.c
blob496f81c91abc09193c9a58d71c47b75568b98b5f
4 // First microsoft compilers
6 #include <windows.h>
7 #include <io.h>
8 #include <ctype.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
16 #include <libtarint/filesystem.h>
19 kwDirectory * kwOpenDir(const char* name)
21 // struct _KWDIR ssss;
22 char* buf;
23 size_t n = strlen(name);
24 kwDirectory * dir = (kwDirectory *)malloc(sizeof (kwDirectory));
25 if(dir==NULL)
27 return NULL;
29 dir->EOD=0; //not the end of directory
30 if ( name[n - 1] == '/' )
32 buf = (char*) malloc(n + 1 + 1);
33 // buf = new char[n + 1 + 1];
34 sprintf(buf, "%s*", name);
36 else
38 buf = (char*)malloc(n + 2 + 1);
39 // buf = new char[n + 2 + 1];
40 sprintf(buf, "%s/*", name);
43 // Now put them into the file array
44 dir->SrchHandle = _findfirst(buf, &dir->Entry);
45 free(buf);
47 if ( dir->SrchHandle == -1 )
49 free(dir);
50 return NULL;
52 return dir;
55 kwDirEntry * kwReadDir(kwDirectory * dir)
57 static kwDirEntry entry;
58 if(!dir || dir->EOD ==1)
60 return NULL;
62 strncpy(entry.d_name,dir->Entry.name,TAR_MAXPATHLEN-1);
63 if(_findnext(dir->SrchHandle, &dir->Entry) == -1)
65 dir->EOD=1;
68 // It is both stupid and dangerous to return a pointer to a static like this.
69 // This can only be called by one caller at a time: i.e., it's not thread safe.
70 // On the other hand, it mimics the documented behavior of "readdir" which is
71 // what it's implemented to replace for platforms that do not have readdir.
72 // Memory leaks are also stupid and dangerous... perhaps this is less so.
74 return &entry;
77 int kwCloseDir(kwDirectory * dir)
79 int r=-1;
80 if(dir)
82 r=_findclose(dir->SrchHandle);
83 free(dir);
85 if(r==-1) return 0;
86 return 1;