-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
135 lines (108 loc) · 1.93 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include "x_lib.h"
#include "stats.h"
int lpfn_work(char* path, char* name, int type, void* arg)
{
assert((type==0)||(type==1));
//
char* s = (char* )arg;
char buff[1024];
snprintf(buff, 1024, "%s/%s", s, name);
//
if(type == 0)
{
if(mkdir(buff, 0755) == -1)
{
printf("create directory[%s] error.\n", buff);
return -1;
}
if(x_readdir(path, lpfn_work, buff) < 0)
{
printf("open directory[%s] error.\n", path);
}
}
else if(type == 1)
{
struct statistics stats;
if(!stats_initialize(&stats, path, buff))
{
printf("statstic initialize error.\n");
return -1;
}
//
int len;
char* str = x_lastbyte(name, strlen(name), '.', &len);
if((len == 1) && ((strncmp(str, "c", 1) != 0) || (strncmp(str, "h", 1) != 0)))
{
stats_note(&stats);
stats_print(&stats, stdout);
}
else
{
stats_copy(&stats);
}
//
stats_clean(&stats);
}
else
{
assert(0);
}
return 0;
}
int main(int argc, char* argv[])
{
if(argc != 2)
{
printf("Usage: ./note arg [dir or path]\n");
return -1;
}
//
char* s = strdup(argv[1]);
int result;
if((result = x_dirorfile(s)) < 0)
{
printf("[%s] is not directory or file.\n", s);
return -1;
}
//
int len;
char* str = x_lastbyte(s, strlen(s), '/', &len);
if(len <= 0)
{
str = s;
len = strlen(s);
}
time_t now = time(NULL);
char path[1024];
snprintf(path, 1024, "./%lld_%.*s", now, len, str);
//start
if(result == 0/*directory*/)
{
if(mkdir(path, 0755) == -1)
{
printf("create folder[%s] error.\n", path);
return -2;
}
//
if(x_readdir(s, lpfn_work, path) < 0)
{
printf("open directory[%s] error.\n", s);
return -3;
}
}
else if(result == 1/*file*/)
{
struct statistics stats;
if(!stats_initialize(&stats, s, path))
{
return -1;
}
stats_note(&stats);
stats_print(&stats, stdout);
}
return 0;
}