😄
This commit is contained in:
@@ -19,8 +19,46 @@
|
||||
|
||||
#include "job_queue.h"
|
||||
|
||||
struct job_queue q;
|
||||
|
||||
int fauxgrep_file(char const *needle, char const *path) {
|
||||
FILE *f = fopen(path, "r");
|
||||
|
||||
if (f == NULL) {
|
||||
warn("failed to open %s", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *line = NULL;
|
||||
size_t linelen = 0;
|
||||
int lineno = 1;
|
||||
|
||||
while (getline(&line, &linelen, f) != -1) {
|
||||
if (strstr(line, needle) != NULL) {
|
||||
printf("%s:%d: %s", path, lineno, line);
|
||||
}
|
||||
|
||||
lineno++;
|
||||
}
|
||||
|
||||
free(line);
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* fauxgrep_thread(void* arg) {
|
||||
//printf("thread initialized\n");
|
||||
char* needle = arg;
|
||||
char const* path;
|
||||
while (job_queue_pop(&q, (void**) &path) == 0) {
|
||||
fauxgrep_file(needle, path);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char * const *argv) {
|
||||
if (argc < 2) {
|
||||
if (argc < 3 || (argc == 4 && strcmp(argv[1], "-n") == 0)) {
|
||||
err(1, "usage: [-n INT] STRING paths...");
|
||||
exit(1);
|
||||
}
|
||||
@@ -51,7 +89,15 @@ int main(int argc, char * const *argv) {
|
||||
paths = &argv[2];
|
||||
}
|
||||
|
||||
assert(0); // Initialise the job queue and some worker threads here.
|
||||
//printf("Initializing job queue\n");
|
||||
job_queue_init(&q, 64);
|
||||
pthread_t threads[num_threads];
|
||||
|
||||
for (int i = 0 ; i < num_threads ; i++) {
|
||||
//printf("Initializing thread %i\n", i);
|
||||
pthread_create(&threads[i], NULL, fauxgrep_thread, strdup(needle));
|
||||
}
|
||||
//printf("finished initializing threads\n");
|
||||
|
||||
// FTS_LOGICAL = follow symbolic links
|
||||
// FTS_NOCHDIR = do not change the working directory of the process
|
||||
@@ -72,7 +118,7 @@ int main(int argc, char * const *argv) {
|
||||
case FTS_D:
|
||||
break;
|
||||
case FTS_F:
|
||||
assert(0); // Process the file p->fts_path, somehow.
|
||||
job_queue_push(&q, strdup(p->fts_path));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -81,7 +127,11 @@ int main(int argc, char * const *argv) {
|
||||
|
||||
fts_close(ftsp);
|
||||
|
||||
assert(0); // Shut down the job queue and the worker threads here.
|
||||
job_queue_destroy(&q);
|
||||
|
||||
for (int i = 0 ; i < num_threads ; i++) {
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user