-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarHeader.c
46 lines (42 loc) · 1007 Bytes
/
tarHeader.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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "tarHeader.h"
struct header* buildHeader(char** fileNames, int num){
struct header *head;
FILE* temp;
int i;
head = malloc(sizeof(struct header));
head->fileNum = num;
head->fileNames = fileNames;
head->filePs = malloc(sizeof(FILE*) * num);
head->fileSizes = malloc(sizeof(int) * num);
for(i=0;i<num;i++){
temp = fopen(fileNames[i], "r");
if(0x0 == temp){
head = 0x0;
break;
}
head->filePs[i] = temp;
head->fileSizes[i] = compFileSize(fileNames[i]);
}
return head;
}
void freeHeader(struct header **headp){
int i;
struct header *head = *headp;
for(i=0;i<head->fileNum;i++){
fclose(head->filePs[i]);
}
free(head->fileSizes);
free(head->filePs);
free(head);
*headp = 0x0;
}
int compFileSize(char* filename){
struct stat sb;
stat(filename, &sb);
return sb.st_size;
}