-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleCode.c
More file actions
218 lines (193 loc) · 6.25 KB
/
Copy pathSampleCode.c
File metadata and controls
218 lines (193 loc) · 6.25 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
int fileCount = 0;
int filesSize = 2;
int wordCount = 0;
int arraySize = 16;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct word
{
char * text;
char * file;
};
struct argument
{
struct word ** wordArray;
char file[512];
};
void * fileReading(void * arg)
{
struct argument info = *(struct argument*)arg;
char * fileName = (char*)calloc(strlen(info.file)+1,sizeof(char));
strcpy(fileName, info.file);
struct word ** words = (info.wordArray);
FILE * reader = fopen(fileName, "r");
/*iterate through all the words in the file until the end and add them to
words*/
while(!feof(reader))
{
/*create the buffer for the word being read*/
char buffer[512];
/*read the first word in the input file*/
fscanf(reader, "%s", buffer);
/*doubles words if full*/
if(wordCount == arraySize)
{
pthread_mutex_lock(&mutex);
/*doubles words size*/
*words = (struct word *)realloc(*words,
arraySize * 2 * sizeof(struct word));
/*checks that memory for words realloc is good*/
if(*words == NULL)
{
perror("words realloc failed");
return NULL;
}
arraySize = arraySize*2;
/*write that words memory has doubled*/
printf("THREAD %u: Re-allocated array of %i character pointers.\n",
(unsigned int)pthread_self(), arraySize);
pthread_mutex_unlock(&mutex);
}
pthread_mutex_lock(&mutex);
(*words)[wordCount].text = (char*)calloc(strlen(buffer)+1,
sizeof(char));
if((*words)[wordCount].text == NULL)
{
perror("word text calloc failed");
return NULL;
}
(*words)[wordCount].file = (char*)calloc(strlen(fileName)+1,
sizeof(char));
if((*words)[wordCount].file == NULL)
{
perror("word file calloc failed");
return NULL;
}
strcpy((*words)[wordCount].text,buffer);
strcpy((*words)[wordCount].file,fileName);
printf("THREAD %u: Added %s at index %i\n", (unsigned int)pthread_self(),
buffer, wordCount);
wordCount++;
pthread_mutex_unlock(&mutex);
}
free(fileName);
fclose(reader);
pthread_exit(NULL);
return NULL;
}
int main(int argc, char * argv[])
{
if(argc != 3)
{
perror("Invalid Arguments\nUSAGE: ./a.out <directory> <substring>\n");
return EXIT_FAILURE;
}
char* d = argv[1];
DIR * dir = opendir(d); /* open given working directory */
if ( dir == NULL )
{
perror( "opendir() failed" );
return EXIT_FAILURE;
}
chdir(d);
struct dirent * file;
struct dirent ** files = (struct dirent **)calloc(filesSize,
sizeof(struct dirent *));
if(files == NULL)
{
perror("files calloc failed");
return EXIT_FAILURE;
}
while((file = readdir(dir)) != NULL)
{
char * name = file->d_name;
int name_len = strlen(name);
char ext[5];
strncpy(ext, name+(name_len-4), 4);
ext[4] = '\0';
if (strcmp(ext,".txt") == 0)
{
if(fileCount == filesSize)
{
filesSize = filesSize *2;
files = (struct dirent **)realloc(files,filesSize*sizeof(struct
dirent *));
if(files == NULL)
{
perror("files realloc failed");
return EXIT_FAILURE;
}
}
files[fileCount] = file;
fileCount++;
}
}
if(fileCount == 0)
{
perror("No .txt files found in directory");
return EXIT_FAILURE;
}
/*create array to hold words*/
struct word * words = (struct word *)calloc(arraySize, sizeof(struct word));
/*checks that memory for words is good*/
if(words == NULL)
{
perror("words calloc failed");
return EXIT_FAILURE;
}
/*write that the initial words is created*/
printf("Allocated initial array of %i word pointers.\n", arraySize);
////////////////////////////////////////////////////////////////////////////
////////////////////////Start of For Threads Code///////////////////////////
////////////////////////////////////////////////////////////////////////////
pthread_t tid[fileCount];
struct argument passer[fileCount];
int n;
for(n = 0; n < fileCount; n++)
{
passer[n].wordArray = &words;
strcpy(passer[n].file, files[n]->d_name);
int rc = pthread_create( &tid[n], NULL, fileReading, (void*)&passer[n] );
if ( rc != 0 ) {
perror( "Could not create the thread" );
}
}
for(n = 0; n < fileCount; n++)
{
pthread_join(tid[n], NULL);
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////End of For Threads Code///////////////////////////
////////////////////////////////////////////////////////////////////////////
/*write that the file has been fully read and words containing substring
are following*/
/*string to be checked for in words*/
char * substring = argv[2];
printf("MAIN THREAD: All done (successfully read %i words from %i files).\n",
wordCount, fileCount);
printf("MAIN THREAD: Words containing substring \"%s\" are:\n", substring);
/*loop through all words*/
for(n = 0; n < wordCount; n++)
{
/*create a pointer for where substring is in word*/
char * substrCheck = strstr(words[n].text, substring);
/*if the substring is found anywhere print the word to output file*/
if(substrCheck != NULL)
{
printf("MAIN THREAD: %s: %s\n", words[n].text, words[n].file);
}
free(words[n].text);
free(words[n].file);
}
free(words);
free(files);
closedir(dir);
return EXIT_SUCCESS;
}