- struct stat statbuf;
- size_t sum;
-
- if ((lstat(filename, &statbuf)) != 0) {
- fprintf(stdout, "du: %s: %s\n", filename, strerror(errno));
- return 0;
- }
- sum = statbuf.st_blocks;
-
- if (S_ISDIR(statbuf.st_mode)) {
- DIR *dir;
- struct dirent *entry;
-
- dir = opendir(filename);
- if (!dir) { return 0; }
- while ((entry = readdir(dir))) {
- char newfile[512];
- char *name = entry->d_name;
-
- if ( (strcmp(name, "..") == 0)
- || (strcmp(name, ".") == 0))
- { continue; }
-
- sprintf(newfile, "%s/%s", filename, name);
- sum += du(newfile);
- }
- closedir(dir);
- print(sum, filename);
- }
- return sum;
+ struct stat statbuf;
+ long sum;
+
+ if ((lstat(filename, &statbuf)) != 0) {
+ bb_perror_msg("%s", filename);
+ status = EXIT_FAILURE;
+ return 0;
+ }
+
+ if (one_file_system) {
+ if (du_depth == 0) {
+ dir_dev = statbuf.st_dev;
+ } else if (dir_dev != statbuf.st_dev) {
+ return 0;
+ }
+ }
+
+ sum = statbuf.st_blocks;
+
+ if (S_ISLNK(statbuf.st_mode)) {
+ if (slink_depth > du_depth) { /* -H or -L */
+ if ((stat(filename, &statbuf)) != 0) {
+ bb_perror_msg("%s", filename);
+ status = EXIT_FAILURE;
+ return 0;
+ }
+ sum = statbuf.st_blocks;
+ if (slink_depth == 1) {
+ slink_depth = INT_MAX; /* Convert -H to -L. */
+ }
+ }
+ }
+
+ if (statbuf.st_nlink > count_hardlinks) {
+ /* Add files/directories with links only once */
+ if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
+ return 0;
+ }
+ add_to_ino_dev_hashtable(&statbuf, NULL);
+ }
+
+ if (S_ISDIR(statbuf.st_mode)) {
+ DIR *dir;
+ struct dirent *entry;
+ char *newfile;
+
+ dir = bb_opendir(filename);
+ if (!dir) {
+ status = EXIT_FAILURE;
+ return sum;
+ }
+
+ newfile = last_char_is(filename, '/');
+ if (newfile)
+ *newfile = '\0';
+
+ while ((entry = readdir(dir))) {
+ char *name = entry->d_name;
+
+ newfile = concat_subpath_file(filename, name);
+ if(newfile == NULL)
+ continue;
+ ++du_depth;
+ sum += du(newfile);
+ --du_depth;
+ free(newfile);
+ }
+ closedir(dir);
+ } else if (du_depth > print_files) {
+ return sum;
+ }
+ if (du_depth <= max_print_depth) {
+ print(sum, filename);
+ }
+ return sum;