-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontents.h
117 lines (86 loc) · 2.22 KB
/
contents.h
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
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
const char *get_filename_ext(const char *filename) {
const char *dot = strrchr(filename, '.');
if(!dot || dot == filename)
return "";
return dot + 1;
}
void dirwalk(char *dir,int depth)
{
DIR *dp;
char str[30];
char name[30];
struct dirent *entry;
struct stat statbuf;
int count = 0;
double c_count = 0;
double java_count = 0;
double sh_count = 0;
double cpp_count = 0;
double php_count = 0;
double html_count = 0;
double css_count = 0;
double xml_count = 0;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"Cannot open directory: %s\n",dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* Found a directory ,but ignore . and .. */
if(strcmp(".",entry->d_name) ==0 || strcmp("..",entry->d_name) == 0)
continue;
printf("\n%*s %s/\n",depth,"",entry->d_name);
/*funtion is called recursively at a new indent level */
dirwalk(entry->d_name,depth + 4);
}
else if(S_ISREG(statbuf.st_mode)) {
printf("%*s %s \n",depth,"",entry->d_name);
}
count++;
const char *s = get_filename_ext(entry->d_name);
if (s[0]=='c' || s[0]=='h')
c_count++;
if (strcmp(s, "java") == 0)
java_count++;
if (strcmp(s, "sh") == 0)
sh_count++;
if (strcmp(s, "cpp") == 0)
cpp_count++;
if (strcmp(s, "php") == 0)
php_count++;
if (strcmp(s, "css") == 0)
css_count++;
if (strcmp(s, "html") == 0)
html_count++;
if (strcmp(s, "xml") == 0)
xml_count++;
}
printf("\n THE LANGUAGES USED ARE:\n");
if(c_count)
printf(" C: %lf%%", c_count*100/count);
if(java_count)
printf(" JAVA: %lf%%", java_count*100/count);
if(cpp_count)
printf(" C++: %lf%%", cpp_count*100/count);
if(sh_count)
printf(" SHELL: %lf%%", sh_count*100/count);
if(php_count)
printf(" PHP: %lf%%", php_count*100/count);
if(css_count)
printf(" CSS: %lf%%", css_count*100/count);
if(html_count)
printf(" HTML: %lf%%", html_count*100/count);
if(xml_count)
printf(" XML: %lf%%", xml_count*100/count);
printf("\n\n");
chdir("..");
closedir(dp);
}