-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytar.c
145 lines (139 loc) · 3.62 KB
/
mytar.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
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
/*
CPE 357 Asgn4
Author: Dylan Baxter (dybaxter), Van Park (vpark05)
File: mytar.c
Description: This file contains main functionality for home
implement of mytar.c
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#include<getopt.h>
#include<unistd.h>
#include<string.h>
#include<grp.h>
#include<pwd.h>
#include<sys/stat.h>
#include<stdlib.h>
#include"contents.h"
int main(int argc, char* const argv[]){
char* opts;
if(argc >= 2){
opts = argv[1];
}
else{
perror("Usage: tar [txvfS] [dest] [paths to tar]");
exit(EXIT_FAILURE);
}
uint16_t optMask = 0;
char opt;
int i;
for(i=0;i<(int)strlen(opts);i++){
opt = opts[i];
/*Flag create, stop if print or extrac*/
if(opt == 'c'){
optMask = CREATE;
}
/*flag table*/
else if(opt == 't'){
optMask = PRINT;
}
/*Flag Extract*/
else if(opt == 'x'){
optMask = EXTRACT;
}
/*Flag verbose*/
else if(opt == 'v'){
optMask = optMask|VERBOSE;
}
/*flag filename argument*/
else if(opt == 'f'){
optMask = optMask|FILENAME;
}
/*flag strict compilation*/
else if(opt == 'S'){
optMask = optMask|STRICT;
}
/*Reject any other argument*/
else{
perror("Usage: tar [txvfS] [dest] [paths to tar]\n");
exit(EXIT_FAILURE);
}
}
/*Initialize Variables for destination and assign*/
char* dest;
if((optMask & FILENAME) && argc >= 3){
dest = argv[2];
}else{
perror("Absent f not supported");
exit(EXIT_FAILURE);
}
/*Initialize vars for actions*/
char* paths[argc-3];
int pathCount =0;
/*For the remainder of arguments, Perform action*/
if(optMask & CREATE){
if(argc < 4){
perror("Create Usage: tar [txvfS] [dest] [paths to tar]\n");
exit(EXIT_FAILURE);
}
for(i=3;i < argc; i++){
paths[i-3] = argv[i];
pathCount++;
}
createArchive(dest, paths, pathCount, optMask);
}
else if(optMask & PRINT){
/*Print the contents of the tar file*/
if(argc > 3){
for(i=3;i < argc; i++){
paths[i-3] = argv[i];
pathCount++;
}
/*Check if the file is a TAR file*/
if(isTAR(dest, optMask)){
printContents(paths, pathCount, dest, optMask);
}
else{
perror("This is not a TAR file");
exit(EXIT_FAILURE);
}
}
else if(argc == 3){
printContents(paths, pathCount, dest, optMask | ALLFLAG);
}
else{
perror(" Extract Usage: tar [txvfS] [dest] [paths to tar]\n");
exit(EXIT_FAILURE);
}
}
else if(optMask & EXTRACT){
if(argc > 3){
for(i=3;i < argc; i++){
paths[i-3] = argv[i];
pathCount++;
}
/*Check if the file is a TAR file*/
if(isTAR(dest, optMask)){
extract(paths, pathCount, dest, optMask);
}
else{
perror("This is not a TAR file");
exit(EXIT_FAILURE);
}
}
else if(argc == 3){
extract(paths, pathCount, dest, optMask | ALLFLAG);
}
else{
perror(" Extract Usage: tar [txvfS] [dest] [paths to tar]\n");
exit(EXIT_FAILURE);
}
}
else{
perror("You never should have come here\n");
exit(EXIT_FAILURE);
}
return 0;
}