-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicates.c
117 lines (94 loc) · 2.62 KB
/
duplicates.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
// CITS2002 Project 2 2021
// Name(s): Chukwuebuka Anwasi, Charlie Teo
// Student number(s): 22990144 , 22978954
#include "storage.h"
#include "iterate.h"
#include "linkedlist.h"
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int include_hidden = 0;
int hasduplicates = 0;
struct unique* head = (struct unique*)malloc(sizeof(struct unique));
int o; //provides the output for getopt
extern char *optarg;
extern int optind, optopt;
int primaryarg = 0; //for finding the primary argument
while((o = getopt(argc, argv, "aAf:h:lmq")) != -1)
{
if (o == 'a')
include_hidden =1;
if (o == 'A')
return(EXIT_SUCCESS);
if(o == '?')
{
printf("%c is an invalid options\n", optopt );
return(EXIT_FAILURE);
}
}
//stores the path names for all the directories
for (int i = optind; i<argc; i++)
{
if (isDirectoryExists(argv[i]))
{
store_directory(argv[i], include_hidden,head, &hasduplicates);
}
else
{
perror("Error: ");
return(EXIT_FAILURE);
}
}
optind = 1; //this resets getopt
while((o = getopt(argc, argv, "aAf:h:lmq")) != -1)
{
if (primaryarg != 1) //sets the primary argument as the first argument thats succesfully read
{
primaryarg =1;
switch(o)
{
case 'a':
include_hidden =1;
iterate_list(head);
return (EXIT_SUCCESS);
case 'A':
return(EXIT_SUCCESS);
case 'f':
searchfile(optarg,head);//goes through chosen directory and searches for files with same file name
return (EXIT_SUCCESS);
case 'h':
// goes through lists and finds matching hashes
search(optarg,head);
return (EXIT_SUCCESS);
case 'l':
// provides a list of all found duplicates
listdups(head);
return (EXIT_SUCCESS);
case 'm':
return(EXIT_FAILURE);
case 'q':
//simply checks if there are any duplicates or not
if (hasduplicates)
return(EXIT_FAILURE);
else
return(EXIT_SUCCESS);
case':':
if (optopt == 'f' || optopt == 'h')
{
printf("missing argument for %c \n", optopt);
perror("Error: ");
}
//doesn't need to be called under the assumption that directory is the argument
return(EXIT_FAILURE);
case'?':
perror("Error: ");
return(EXIT_FAILURE);
}
}
}
iterate_list(head);
return 0;
}