-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytar.c
More file actions
93 lines (78 loc) · 2.4 KB
/
Copy pathmytar.c
File metadata and controls
93 lines (78 loc) · 2.4 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "mytar.h"
extern int errno;
int main(int argc, char *argv[]){
char *options;
int num_ops, verboseBool = 0, strictBool = 0, idx = 1, path_idx = 3;
char **paths;
if (argc < 3){
fprintf(stderr, "Usage: mytar [ctxvS]f tarfile [ path [ ... ] ]\n");
exit(EXIT_FAILURE);
}
options = argv[1];
num_ops = strlen(options);
if (num_ops < 2 || num_ops > 4){
fprintf(stderr, "Usage: mytar [ctxvS]f tarfile [ path [ ... ] ]\n");
exit(EXIT_FAILURE);
}
if (options[0] != 'c' && options[0] != 't' && options[0] != 'x'){
fprintf(stderr, "Usage: mytar [ctxvS]f tarfile [ path [ ... ] ]\n");
printf("second argument requires a c, t or x as first char\n");
exit(EXIT_FAILURE);
}
while (options[idx] != 'f'){
/* we hit the end of the second argument without encountering an 'f' */
if (options[idx] == '\0'){
fprintf(stderr, "Usage: mytar [ctxvS]f tarfile [ path [ ... ] ]\n");
printf("f required in second argument\n");
exit(EXIT_FAILURE);
}
else if (options[idx] == 'v'){
verboseBool = 1;
}
else if(options[idx] == 'S'){
strictBool = 1;
}
else{
fprintf(stderr, "Usage: mytar [ctxvS]f tarfile [ path [ ... ] ]\n");
printf("Invalid character in second argument\n");
exit(EXIT_FAILURE);
}
idx++;
}
errno = 0;
paths = malloc(sizeof(char *) * (argc - path_idx));
if(errno) {
perror("Couldn't malloc paths");
exit(errno);
}
idx = 0;
while(path_idx < argc){
paths[idx++] = argv[path_idx++];
}
switch(options[0]){
case 'c':
create_cmd(verboseBool, strictBool, idx, argv[2], paths);
break;
case 't':
if(idx) {
list_cmd(argv[2], paths, idx, verboseBool, strictBool);
}
else {
list_cmd(argv[2], NULL, 0, verboseBool, strictBool);
}
break;
case 'x':
if(idx) {
extract_cmd(argv[2], paths, idx, verboseBool, strictBool);
}
else {
extract_cmd(argv[2], NULL, 0, verboseBool, strictBool);
}
break;
}
return 0;
}