forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmg.c
71 lines (65 loc) · 1.28 KB
/
mg.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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
char buf[512];
int
main(int argc, char *argv[])
{
int writeFile;
if(argc <= 3){
printf(1,"Need 3 arguments\n");
exit();
}
//open write file
if((writeFile = open(argv[2], O_CREATE|O_RDWR)) < 0){
printf(1, "mg: cannot open %s\n", argv[1]);
exit();
}
//split count
int spCount=0;
for(int i=0;i<strlen(argv[3]);i++)
{
int mul=1;
for(int j=0;j<strlen(argv[3])-i-1;j++)
mul*=10;
spCount+=(argv[3][i]-'0')*mul;
}
//merge
int readFile,n;
for(int i=0;i<spCount;i++)
{
//countLength
int countLen=0;
int tmp=i+1;
while(tmp>0)
{
countLen++;
tmp/=10;
}
//set filename
char fileName[strlen(argv[1])+countLen+1];
strcpy(fileName,argv[1]);
fileName[strlen(argv[1])+countLen]='\0';
tmp=i+1;
for(int j=countLen-1;j>=0;j--)
{
fileName[strlen(argv[1])+j]=(tmp%10)+'0';
tmp/=10;
}
//open write file
if((readFile = open(fileName, O_RDONLY)) < 0)
{
printf(1, "mg: cannot open %s\n", fileName);
exit();
}
//merge file
while((n=read(readFile, buf, sizeof(buf)))>0)
{
write(writeFile,buf,n);
}
close(readFile);
}
close(writeFile);
exit();
}