-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar.c
56 lines (50 loc) · 1.27 KB
/
tar.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "tarHeader.h"
void printCreateMessage(struct header h, char* target){
int i;
printf("Creating Archive %s containing files: ", target);
for(i=0;i<h.fileNum;i++){
printf("%s ", h.fileNames[i]);
}
printf("\n");
}
int create(char *target, char** fileNames, int num){
int status, i;
FILE* output = 0x0;
struct header *h = buildHeader(fileNames,num);
if(0x0 == h){
fprintf(stderr, "Couldn't open input files for reading\n");
status = 1;
goto CLEANUP;
}
output = fopen(target, "w");
if(0x0 == output){
fprintf(stderr, "Could open %s for writing\n", target);
status = 1;
goto CLEANUP;
}
printCreateMessage(*h, target);
fwrite("TAR",1,3,output);
fwrite(&h->fileNum,sizeof(int),1,output);
for(i=0;i<h->fileNum;i++){
int len = strlen(h->fileNames[i]);
fwrite(&len,sizeof(int),1,output);
fwrite(h->fileNames[i],1,len,output);
}
fclose(output);
CLEANUP:
freeHeader(&h);
return status;
}
int main(int argc, char** argv){
if(argv[1][0] == 'c'){
return create(argv[2], argv+3, argc-3);
}else{
return 0; //extract();
}
}