😄
This commit is contained in:
112
A2/fhistogram.c
112
A2/fhistogram.c
@@ -21,73 +21,73 @@
|
||||
int global_histogram[8] = { 0 };
|
||||
|
||||
int fhistogram(char const *path) {
|
||||
FILE *f = fopen(path, "r");
|
||||
FILE *f = fopen(path, "r");
|
||||
|
||||
int local_histogram[8] = { 0 };
|
||||
int local_histogram[8] = { 0 };
|
||||
|
||||
if (f == NULL) {
|
||||
fflush(stdout);
|
||||
warn("failed to open %s", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
|
||||
char c;
|
||||
while (fread(&c, sizeof(c), 1, f) == 1) {
|
||||
i++;
|
||||
update_histogram(local_histogram, c);
|
||||
if ((i % 100000) == 0) {
|
||||
merge_histogram(local_histogram, global_histogram);
|
||||
print_histogram(global_histogram);
|
||||
if (f == NULL) {
|
||||
fflush(stdout);
|
||||
warn("failed to open %s", path);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
int i = 0;
|
||||
|
||||
merge_histogram(local_histogram, global_histogram);
|
||||
print_histogram(global_histogram);
|
||||
char c;
|
||||
while (fread(&c, sizeof(c), 1, f) == 1) {
|
||||
i++;
|
||||
update_histogram(local_histogram, c);
|
||||
if ((i % 100000) == 0) {
|
||||
merge_histogram(local_histogram, global_histogram);
|
||||
print_histogram(global_histogram);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
fclose(f);
|
||||
|
||||
merge_histogram(local_histogram, global_histogram);
|
||||
print_histogram(global_histogram);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char * const *argv) {
|
||||
if (argc < 2) {
|
||||
err(1, "usage: paths...");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char * const *paths = &argv[1];
|
||||
|
||||
// FTS_LOGICAL = follow symbolic links
|
||||
// FTS_NOCHDIR = do not change the working directory of the process
|
||||
//
|
||||
// (These are not particularly important distinctions for our simple
|
||||
// uses.)
|
||||
int fts_options = FTS_LOGICAL | FTS_NOCHDIR;
|
||||
|
||||
FTS *ftsp;
|
||||
if ((ftsp = fts_open(paths, fts_options, NULL)) == NULL) {
|
||||
err(1, "fts_open() failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
FTSENT *p;
|
||||
while ((p = fts_read(ftsp)) != NULL) {
|
||||
switch (p->fts_info) {
|
||||
case FTS_D:
|
||||
break;
|
||||
case FTS_F:
|
||||
fhistogram(p->fts_path);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (argc < 2) {
|
||||
err(1, "usage: paths...");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fts_close(ftsp);
|
||||
char * const *paths = &argv[1];
|
||||
|
||||
move_lines(9);
|
||||
// FTS_LOGICAL = follow symbolic links
|
||||
// FTS_NOCHDIR = do not change the working directory of the process
|
||||
//
|
||||
// (These are not particularly important distinctions for our simple
|
||||
// uses.)
|
||||
int fts_options = FTS_LOGICAL | FTS_NOCHDIR;
|
||||
|
||||
return 0;
|
||||
FTS *ftsp;
|
||||
if ((ftsp = fts_open(paths, fts_options, NULL)) == NULL) {
|
||||
err(1, "fts_open() failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
FTSENT *p;
|
||||
while ((p = fts_read(ftsp)) != NULL) {
|
||||
switch (p->fts_info) {
|
||||
case FTS_D:
|
||||
break;
|
||||
case FTS_F:
|
||||
fhistogram(p->fts_path);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fts_close(ftsp);
|
||||
|
||||
move_lines(9);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user