-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.c
95 lines (74 loc) · 1.96 KB
/
code.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
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdlib.h>
// recursive function to traverse through directories
void traverseDir(const char *path, short iteration);
// used for concatenating two directory names
char* concat(const char *s1, const char *s2);
// converts bits into human readable format
void printSize(double sizeInBytes);
static const char* sizes[] = {"bytes", "KB", "MB", "GB"};
// detect files with size greater than threshold
long m_threshold;
// for not to get lost in chained folders
// since it searches in a very DFS way
short m_iteration;
int main(int argc, char *argv[])
{
const char *path = argv[1];
m_threshold = strtol(argv[2], NULL, 10);
m_iteration = strtol(argv[3], NULL, 10);
traverseDir(path, 0);
return 0;
}
void traverseDir(const char *path, short iteration)
{
struct dirent *dp;
struct stat file_info;
DIR *dir = opendir(path);
// read twice to escape /. and /..
dp = readdir(dir);
dp = readdir(dir);
while(dp = readdir(dir))
{
stat(concat(concat(path, "/"), dp->d_name), &file_info);
// get into folders those are not so deep
if(S_ISDIR(file_info.st_mode) && iteration<=m_iteration)
{
traverseDir(concat(concat(path, "/"), dp->d_name), iteration+1);
}
else
{
// print once you find a file with gigantic size
if(file_info.st_size >= m_threshold)
{
printf("%s/%s, ", path, dp->d_name);
printSize((double)file_info.st_size);
}
}
}
closedir(dir);
}
char* concat(const char *s1, const char *s2)
{
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
char *result = (char*) malloc(len1 + len2 + 1);
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2 + 1);
return result;
}
void printSize(double sizeInBytes)
{
double result;
short iterator = -1;
while(sizeInBytes > 1 && iterator < 3)
{
result = sizeInBytes;
sizeInBytes /= 1024;
iterator++;
}
printf("%.1f %s\n", result, sizes[iterator]);
}