-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdu.c
More file actions
75 lines (73 loc) · 1.39 KB
/
du.c
File metadata and controls
75 lines (73 loc) · 1.39 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
/*************************************************************************
> File Name: du.c
> Author: jianghechao
> Mail: [email protected]
> Created Time: Thu 03 Apr 2014 02:46:51 PM CST
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
#define MAX_LEN 1024
#define MAX_PATH 1024
void show_Error(char *str)
{
perror(str);
}
int print(char *filename,off_t st_size,mode_t mode,int n)
{
if(S_ISDIR(mode))
{
printf("%d\t%s\n",n,filename);
}
else
n = st_size+n;
return n;
}
int statis(char *path)
{
char tempPath[MAX_PATH];
struct DIR *dir;
struct dirent *pdirent = NULL;
struct stat st;
int nret ;
int n = 0;
dir = opendir(path);
nret = stat(path,&st);
if(nret==-1)
{
show_Error("stat Error");
return n;
}
if(dir!=NULL)
{
while((pdirent = readdir(dir))!=NULL)
{
if(strcmp(".",pdirent->d_name)==0
||strcmp("..",pdirent->d_name)==0)
{
if(strcmp(".",pdirent->d_name)==0)
n+=st.st_size;
continue;
}
sprintf(tempPath,"%s/%s",path,pdirent->d_name);
n+=statis(tempPath);
}
closedir(dir);
}
return print(path,st.st_size,st.st_mode,n);
}
int main(int argc,char **argv)
{
if(argc!=1)
{
show_Error("请不要输入多余的参数...\n");
return 1;
}
statis(".");
return 0;
}