-
Notifications
You must be signed in to change notification settings - Fork 0
/
fat12img.c
102 lines (80 loc) · 1.81 KB
/
fat12img.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "_fat12.h"
void PrintUsage(const char *str1);
int CopyFileFromImage(const PCHAR PathName);
int main(int argc, char *argv[])
{
if((argc == 1) || ((argc == 2) && (!stricmp(argv[1], "-h"))))
{
PrintUsage(argv[0]);
return 0;
}
if(argc == 4 && (!stricmp(argv[1], "-a")))
{
InitFat12FileSystem(argv[2]);
AddFile2Image(argv[3]);
FreeFat12FileSystem();
}
if(argc == 4 && (!stricmp(argv[1], "-c")))
{
InitFat12FileSystem(argv[2]);
CopyFileFromImage(argv[3]);
FreeFat12FileSystem();
}
if(argc == 4 && (!stricmp(argv[1], "-d")))
{
InitFat12FileSystem(argv[2]);
DeleteFileFromImage(argv[3]);
FreeFat12FileSystem();
}
return 0;
}
void PrintUsage(const char *str1)
{
printf("(C) Mumu3w@outlook.com %s (Fat12Image 0.12)\n\n", __DATE__);
printf("Usage: %s <-a | -c | -d | -h> Image [File]\n", str1);
printf(" -a Add file to image\n");
printf(" -c Copy file from image\n");
printf(" -d Delete file from image\n");
printf(" -h Show this usage\n");
}
//
// 从镜像复制文件
int CopyFileFromImage(const PCHAR PathName)
{
FILE *NewFp;
PCHAR FileName;
FHANDLE Fp;
char Buffer[512];
int Bytes;
if(NULL == (NewFp = fopen(PathName, "wb")))
{
fprintf(stderr, "%s Can't open\n", PathName);
exit(EXIT_FAILURE);
}
FileName = strrchr(PathName, SEPARATOR);
FileName = ((NULL == FileName) ? PathName : (FileName + 1));
Fp = FatOpenFile(FileName, 0);
if(!(Fp < 0))
{
while(1)
{
Bytes = FatReadFile(Fp, sizeof(Buffer), Buffer);
if(Bytes == -1)
{
break;
}
fwrite(Buffer, sizeof(char), Bytes, NewFp);
if(Bytes != sizeof(Buffer))
{
break;
}
}
FatCloseFile(Fp);
fclose(NewFp);
return 0;
}
return -1;
}