-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
175 lines (153 loc) · 6.27 KB
/
main.c
File metadata and controls
175 lines (153 loc) · 6.27 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "options.h"
#include "createoptionfns.h"
#include "bitio.h"
#include "lz77.h"
#include "bitfilecreation.h"
#include "extract.h"
#include <dirent.h>
#include <errno.h>
//Function to find out whether path entered is a file or folder
int is_regular_file(const char *path){
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
int main(int argc, char *argv[]){
/*If number of arguments less than 3
First arg - ./tar , second arg - to create/extract/list , third arg - file or folder name*/
if(argc < 3){
printf("Please enter a specific option");
return 0;
}
else{
/*-c to create, -x to extract, -t to list, -d to decode a file*/
char *options = getOptions(argc, argv);
if(options){
opts selected_option = get_options(options);
if(selected_option.opts_c && !selected_option.opts_t && !selected_option.opts_x && !selected_option.opts_d){
printf("Create file option activated!\n");
if(is_regular_file(argv[2])){
printf("This is a file\n");
char *filename = getFileNameFromPath(argv[2]);
char *bitfilename;
bitfilename = (char *)malloc(sizeof(char)*20);
int i;
for(i=0; i<strlen(filename);i++){
if(filename[i] != '.')
bitfilename[i] = filename[i];
else
break;
}
//printf("%s\n",filename);
copyingfiles(argv[2], filename);
bitfilecreation(bitfilename, filename);
remove(filename);
}
else{
DIR* dir = opendir(argv[2]);
if (dir) {
/* Directory exists. */
printf("This is a folder\n");
// char *filename = getFileNameFromPath(argv[2]);
// printf("%s\n",filename);
listOFDirectoryFiles(argv[2]);
closedir(dir);
} else if (ENOENT == errno) {
printf("This path does not exist!\n");
return 0;
/* Directory does not exist. */
} else {
/* opendir() failed for some other reason. */
printf("Error!\n");
return 0;
}
}
}
else if(selected_option.opts_t && !selected_option.opts_c && !selected_option.opts_x && !selected_option.opts_d){
printf("List of files in tar file option activated!\n");
if(is_regular_file(argv[2])){
char *tar = getFileNameFromPath(argv[2]);
int i;
for(i = 0; i<strlen(tar);i++){
if(tar[i] == '.'){
break;
}
}
//printf("%ld--%d",strlen(tar),i+6);
if(tar[i-2]!='_'&& tar[i+1]=='t' && tar[i+2]=='a' && tar[i+3]=='r' && (i+4)==strlen(tar)){
printf("This is not a compressed tar file\n");
listofFilesinArchive(tar);
}
else if(tar[i-2]=='_' && tar[i-1]=='c' && tar[i+1]=='t' && tar[i+2]=='a' && tar[i+3]=='r' && (i+4)==strlen(tar)){
printf("This is a compressed tar file\n");
listofFilesinArchive(tar);
}
else{
printf("Please enter a tar file");
return 0;
}
}
else{
printf("This is not a file\n");
}
}
else if(selected_option.opts_x && !selected_option.opts_c && !selected_option.opts_t && !selected_option.opts_d){
printf("Extract files option activated!\n");
if(is_regular_file(argv[2])){
char *tar = getFileNameFromPath(argv[2]);
int i;
for(i = 0; i<strlen(tar);i++){
if(tar[i] == '.'){
break;
}
}
if(tar[i-2]!='_'&& tar[i+1]=='t' && tar[i+2]=='a' && tar[i+3]=='r' && (i+4)==strlen(tar)){
printf("This is not a compressed tar file\n");
extractFilesFromArchive(tar,0);
}
else if(tar[i-2]=='_' && tar[i-1]=='c' && tar[i+1]=='t' && tar[i+2]=='a' && tar[i+3]=='r' && (i+4)==strlen(tar)){
printf("This is a compressed tar file\n");
char **l = extractFilesFromArchive(tar,1);
int i = 0;
while(l[i][0]!='\0'){
decodingfn(l[i]);
//printf("%s--\n",l[i]);
i+=1;
}
}
else{
printf("Please enter a tar file");
return 0;
}
}
else{
printf("This is not a file\n");
}
}
else if(!selected_option.opts_x && !selected_option.opts_c && !selected_option.opts_t && selected_option.opts_d){
printf("Decoding option activated!\n");
if(is_regular_file(argv[2])){
char *d = getFileNameFromPath(argv[2]);
decodingfn(d);
}
else{
printf("No such file exists!");
}
}
else{
printf("Only one option can be specified at a time\n");
}
}
else{
printf("Please specify options!");
return 0;
}
}
return 0;
}