-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.c
More file actions
111 lines (91 loc) · 2.3 KB
/
Lab2.c
File metadata and controls
111 lines (91 loc) · 2.3 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#define maxbuf 256
int FolderCount;
int FileCount;
int StartLooking(char *folder, char *filename) {
DIR *cur;
struct dirent *el;
cur = opendir(folder);
//Open current folder
if (!(cur))
{
fprintf(stderr, "%s: %s\n", folder, strerror(errno));
return 1;
}
//Read all element in current directory
while (NULL != (el = readdir(cur))) {
//Check if our elememt is not our or parent folder
if (0 != strcmp(".", el->d_name) && 0 != strcmp("..", el->d_name)) {
//Now check if file is a folder //S_ISDIR(x)
if (DT_DIR == el->d_type) {
FolderCount++;
char *name;
name=(char*)malloc(PATH_MAX);
strcpy(name,folder);
StartLooking(strcat(strcat(name,"/"), el->d_name), filename);
free(name);
}
else if (DT_REG == el->d_type){
FileCount++;
//printf("else\n");
//We found our buddy
if (0 == strcmp(filename, el->d_name)) {
char *name;
name=(char*)malloc(NAME_MAX);
strcpy(name,folder);
printf("!FOUND_FILE! %s/%s\n", folder, el->d_name);
Info(strcat(strcat(name,"/"), el->d_name));
free(name);
}
}
}
}
closedir(cur);
}
int Info (char *nameoffile)
{
struct stat stat1;
stat(nameoffile, &stat1);
//date
struct tm *tm_ptr;
time_t Mytime;
char s1[40]={ 0 };
Mytime = stat1.st_ctime;
tm_ptr = gmtime(&Mytime);
strftime(s1, 80,"%d.%m.%Y %H:%M:%S ",tm_ptr);
printf("\tДата создания: %s \n\tРазмер: %ld байт \n\tПрава доступа: %o\n\tНомер дескриптора: %lu\n",s1,stat1.st_size,stat1.st_mode,stat1.st_ino);
printf("\n");
}
int main(int argc, char *argv[], char *envp[]) {
char folder[maxbuf], path[maxbuf];
printf("\n");
//Check for 2 input parameters
if (argc < 3) {
printf("Not enough actual parameters\n");
return 0;
}
else {
strcpy(folder, argv[1]);
DIR *dir;
if (NULL == (dir = opendir(folder))) {
printf("This folder doesn't exist\n");
return 0;
}
else {
printf("folder else\n");
closedir(dir);
}
}
StartLooking(argv[1], argv[2]);
printf("We looked %d folder and %d file except root folder\n", FolderCount, FileCount);
return 0;
}