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
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
int numOfEntry(char *path)
{
int count = 0;
DIR *dir = opendir(path);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
count++;
}
closedir(dir);
return count;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Pass the path as an argument to the program");
exit(1);
}
char *path = argv[1];
DIR *dir = opendir(path);
if (dir == NULL)
{
printf("The path passed is invalid");
return -1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
if (entry->d_type == DT_DIR)
{
char next[strlen(path) + strlen(entry->d_name) + 2];
next[0] = '\0';
strcat(next, path);
strcat(next, "/");
strcat(next, entry->d_name);
printf("Directory: %s\n\tEntries: %d\n", entry->d_name, numOfEntry(next));
}
else if (entry->d_type == DT_REG)
{
struct stat *buf = (struct stat *)malloc(sizeof(struct stat));
stat(entry->d_name, buf);
printf("Regular File: %s\n\tOwner: %d\n\tSize: %f\n\tInode: %llu\n",
entry->d_name, buf->st_uid, (double)buf->st_size, buf->st_ino);
free(buf);
}
else
{
printf("File: %s\n\tType:%hhu\n", entry->d_name, entry->d_type);
}
}
closedir(dir);
return 0;
}
|