-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirdump.cpp
145 lines (128 loc) · 3.21 KB
/
dirdump.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Longfilename DIR
// stack-checking off
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include "lfn.cpp"
#include <math.h>
dword totbytes,totfiles,totdirs,lines;
byte locdate;
void DumpMaskedDirCD(const struct DPB *b,struct longdirentry *pDir,char *mask,char *path)
{
struct FATfile f;
size_t bufsize;
void *buffer;
FILE *dump;
if (pDir==NULL) return;
bufsize=b->sectors_per_cluster*b->bytes_per_sector;
buffer=malloc(bufsize);
dump=fopen("\\dir.dmp","w");
f.cluster=pDir->start_cluster;
f.sector=firstSectorOfCluster(b,f.cluster);
dword pos=0;
while (pos < pDir->length) {
readCDSectors(b,f.sector,b->sectors_per_cluster, buffer);
fwrite(buffer, bufsize, 1, dump);
pos += bufsize;
}
fclose(dump);
free(buffer);
}
void DumpMaskedDir(const struct DPB *b,struct longdirentry *pDir,char *mask,char *path)
{
if (b->CD==1) {
DumpMaskedDirCD(b, pDir, mask, path);
return;
};
struct FATfile f;
size_t bufsize;
void *buffer;
FILE *dump;
if (pDir==NULL) return;
bufsize=b->sectors_per_cluster*b->bytes_per_sector;
buffer=malloc(bufsize);
dump=fopen("\\dir.dmp","w");
f.cluster=pDir->start_cluster;
f.sector=firstSectorOfCluster(b,f.cluster);
int eof=0;
while (eof==0) {
readSector(b->drive,f.sector,b->sectors_per_cluster, buffer);
fwrite(buffer, bufsize, 1, dump);
eof=getNextCluster(b, &f);
}
fclose(dump);
free(buffer);
}
void main(int argc, char *argv[], char *envp)
{
struct DPB Disk;
struct longdirentry f;
char drv,path[MAX_PATH_SIZE],mask[MAX_PATH_SIZE],*temp,x[MAX_PATH_SIZE];
int no;
unsigned int slash,len;
temp=NULL;
no=1;
useVMCache=0;
initLFNLib();
drv=drive_of_path(argv[no]);
if (makeDPB(drv,Disk)==1) exit(1); //emergency stop
path[0]='\0';
mask[0]='\0';
x[0]='\0';
if (argv[no]!=NULL)
{
slash=strlast(argv[no],'\\');
if (slash==0xffff) slash=strlast(argv[no],':'); //if no backslash we take the colon
temp=seekPath(&Disk,argv[no],x,f,0);
if (temp==NULL)
{
#ifdef LANG_EN
printf("File not found.\n");
#endif
#ifdef LANG_DE
printf("Datei nicht gefunden.\n");
#endif
return;
}
if (slash!=strlen(argv[no])-1) //we have no trailing backslash
{
if (slash!=0xffff) strcpy(mask,argv[no]+slash+1);
else strcpy(mask,argv[no]); //no backslash at all
len=strlen(mask);
if (strcspn(mask,"*?")==len) //we have no wildcards in the mask
{
if ((f.atrib & F_DIR)!=0) //whole thing is an explicit directory, f is ok
{
mask[0]='\0';
}
else {
//else it is an explicit file, we keep the mask and make f the current dir
temp=seekPath(&Disk,NULL,x,f,1); //current dir
if (temp==NULL) return;
}
}
else {
//else it is a file search mask
if (slash!=0xffff) {
if ((slash==2) && (argv[no][1]==':')) strncat(path,argv[no],3);
else strncat(path,argv[no],slash);
}
if (temp!=NULL) free(temp);
temp=seekPath(&Disk,path,x,f,0);
}
}
}
else
{
temp=seekPath(&Disk,NULL,x,f,1); //current dir
if (temp==NULL) return;
}
totbytes=0;
totfiles=0;
totdirs=0;
DumpMaskedDir(&Disk,&f,mask,x);
free(temp);
flushCache();
printf("Directory %s dumped to \\dir.dmp", f.name);
}