aboutsummaryrefslogtreecommitdiff
path: root/du.c
diff options
context:
space:
mode:
Diffstat (limited to 'du.c')
-rw-r--r--du.c71
1 files changed, 11 insertions, 60 deletions
diff --git a/du.c b/du.c
index 4dc7ea13a..b6ebaca7a 100644
--- a/du.c
+++ b/du.c
@@ -36,16 +36,6 @@
36 36
37typedef void (Display) (long, char *); 37typedef void (Display) (long, char *);
38 38
39typedef struct inode_type {
40 struct inode_type *next;
41 ino_t ino;
42} INODETYPE;
43
44#define HASH_SIZE 311 /* Should be prime */
45#define hash_inode(i) ((i) % HASH_SIZE)
46
47static INODETYPE *inode_hash_list[HASH_SIZE];
48
49static const char du_usage[] = 39static const char du_usage[] =
50 "du [OPTION]... [FILE]...\n\n" 40 "du [OPTION]... [FILE]...\n\n"
51 "Summarize disk space used for each FILE and/or directory.\n" 41 "Summarize disk space used for each FILE and/or directory.\n"
@@ -71,52 +61,6 @@ static void print_summary(long size, char *filename)
71 } 61 }
72} 62}
73 63
74/* Return 1 if inode is in inode hash list, else return 0 */
75static int is_in_list(const ino_t ino)
76{
77 INODETYPE *inode;
78
79 inode = inode_hash_list[hash_inode(ino)];
80 while (inode != NULL) {
81 if (inode->ino == ino)
82 return 1;
83 inode = inode->next;
84 }
85
86 return 0;
87}
88
89/* Add inode to inode hash list */
90static void add_inode(const ino_t ino)
91{
92 int i;
93 INODETYPE *inode;
94
95 i = hash_inode(ino);
96 inode = malloc(sizeof(INODETYPE));
97 if (inode == NULL)
98 fatalError("du: Not enough memory.");
99
100 inode->ino = ino;
101 inode->next = inode_hash_list[i];
102 inode_hash_list[i] = inode;
103}
104
105/* Clear inode hash list */
106static void reset_inode_list(void)
107{
108 int i;
109 INODETYPE *inode;
110
111 for (i = 0; i < HASH_SIZE; i++) {
112 while (inode_hash_list[i] != NULL) {
113 inode = inode_hash_list[i]->next;
114 free(inode_hash_list[i]);
115 inode_hash_list[i] = inode;
116 }
117 }
118}
119
120/* tiny recursive du */ 64/* tiny recursive du */
121static long du(char *filename) 65static long du(char *filename)
122{ 66{
@@ -175,13 +119,13 @@ static long du(char *filename)
175 } 119 }
176 else if (statbuf.st_nlink > 1 && !count_hardlinks) { 120 else if (statbuf.st_nlink > 1 && !count_hardlinks) {
177 /* Add files with hard links only once */ 121 /* Add files with hard links only once */
178 if (is_in_list(statbuf.st_ino)) { 122 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
179 sum = 0L; 123 sum = 0L;
180 if (du_depth == 1) 124 if (du_depth == 1)
181 print(sum, filename); 125 print(sum, filename);
182 } 126 }
183 else { 127 else {
184 add_inode(statbuf.st_ino); 128 add_to_ino_dev_hashtable(&statbuf, NULL);
185 } 129 }
186 } 130 }
187 du_depth--; 131 du_depth--;
@@ -231,11 +175,18 @@ int du_main(int argc, char **argv)
231 if (sum && isDirectory(argv[i], FALSE, NULL)) { 175 if (sum && isDirectory(argv[i], FALSE, NULL)) {
232 print_normal(sum, argv[i]); 176 print_normal(sum, argv[i]);
233 } 177 }
234 reset_inode_list(); 178 reset_ino_dev_hashtable();
235 } 179 }
236 } 180 }
237 181
238 exit(0); 182 exit(0);
239} 183}
240 184
241/* $Id: du.c,v 1.15 2000/02/21 17:27:17 erik Exp $ */ 185/* $Id: du.c,v 1.16 2000/03/04 21:19:32 erik Exp $ */
186/*
187Local Variables:
188c-file-style: "linux"
189c-basic-offset: 4
190tab-width: 4
191End:
192*/