summaryrefslogtreecommitdiffstats
path: root/Utilities/cmtar/filesystem.c
blob: 1ddd5a52850cad8591bdf65d9f685531a8153fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80



// First microsoft compilers

#include <windows.h>
#include <io.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <libtarint/filesystem.h>


kwDirectory * kwOpenDir(const char* name)
{
//  struct _KWDIR ssss;
  char* buf;
  size_t n = strlen(name);
  kwDirectory * dir = (kwDirectory *)malloc(sizeof (kwDirectory));
  if(dir==NULL)
    {
      return NULL;
    }
  dir->EOD=0; //not the end of directory
  if ( name[n - 1] == '/' ) 
    {
    buf = (char*) malloc(n + 1 + 1);
//    buf = new char[n + 1 + 1];
    sprintf(buf, "%s*", name);
    } 
  else
    {
    buf = (char*)malloc(n + 2 + 1);
//    buf = new char[n + 2 + 1];
    sprintf(buf, "%s/*", name);
    }
  
  // Now put them into the file array
  dir->SrchHandle = _findfirst(buf, &dir->Entry);
  free(buf);
  
  if ( dir->SrchHandle == -1 )
    {
    free(dir);
    return NULL;
    }
  return dir;  
}

kwDirEntry * kwReadDir(kwDirectory * dir)
{
  kwDirEntry * entry;
  if(!dir || dir->EOD ==1)
    {
    return NULL;
    }
  entry = (kwDirEntry*)malloc(sizeof(kwDirEntry));
  strncpy(entry->d_name,dir->Entry.name,TAR_MAXPATHLEN-1);
  if(_findnext(dir->SrchHandle, &dir->Entry) == -1)
    {
      dir->EOD=1;
    }
  return entry;
}
int kwCloseDir(kwDirectory * dir)
{
  int r=-1;
  if(dir)
    {
    r=_findclose(dir->SrchHandle);
    free(dir);
    }
  if(r==-1) return 0;
  return 1;
}