-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfsizes.c
67 lines (55 loc) · 1.21 KB
/
fsizes.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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#define LEN(arr) (sizeof(arr)) / (sizeof(arr[0]))
enum {BINS = 5};
enum {TRUE, FALSE};
void print_hor_hist (const int, const int []);
int main (void)
{
struct dirent *de;
struct stat finfo;
DIR *dp = opendir ("./");
const unsigned long bin_size = 1024;
int bins[BINS];
int i;
int bin_pos;
for (i = 0; i != BINS; ++i)
{
bins[i] = 0;
}
if (NULL == dp)
{
perror ("opendir");
return -1;
}
while ((de = readdir (dp)))
{
if (strcmp (".", de->d_name) != 0 && strcmp ("..", de->d_name) != 0)
{
stat (de->d_name, &finfo);
bin_pos = ((long)finfo.st_size / bin_size);
/* printf ("%s: %ld bytes goes in bin %d\n", de->d_name, (long)finfo.st_size, (bin_pos > BINS - 1 ? BINS - 1 : bin_pos)); */
i = (bin_pos > BINS - 1 ? BINS - 1 : bin_pos);
++bins[i];
}
}
print_hor_hist (LEN (bins), bins);
closedir (dp);
return 0;
}
void print_hor_hist (const int size, const int arr[])
{
int i, j;
for (i = 0; i != size; ++i)
{
printf ("%4d: ", i);
for (j = 0; j != arr[i]; ++j)
{
putchar ('*');
}
putchar ('\n');
}
}