du.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * du - calculate file size usage
  7. *
  8. * TODO: Should use st_blocks, but we don't set that in the kernel yet?
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <dirent.h>
  16. #include <fcntl.h>
  17. #include <sys/stat.h>
  18. #include <sys/ioctl.h>
  19. static int show_total = 0;
  20. static int human = 0;
  21. static int all = 1;
  22. static int is_arg = 0;
  23. static uint64_t count_thing(char * tmp);
  24. static int print_human_readable_size(char * _out, size_t s) {
  25. if (s >= 1<<20) {
  26. size_t t = s / (1 << 20);
  27. return sprintf(_out, "%d.%1dM", (int)t, (int)(s - t * (1 << 20)) / ((1 << 20) / 10));
  28. } else if (s >= 1<<10) {
  29. size_t t = s / (1 << 10);
  30. return sprintf(_out, "%d.%1dK", (int)t, (int)(s - t * (1 << 10)) / ((1 << 10) / 10));
  31. } else {
  32. return sprintf(_out, "%d", (int)s);
  33. }
  34. }
  35. static void print_size(uint64_t size, char * name) {
  36. char sizes[8];
  37. if (!human) {
  38. sprintf(sizes, "%-7llu", size/1024LLU);
  39. } else {
  40. print_human_readable_size(sizes, size);
  41. }
  42. if (strlen(name) > 2 && name[0] == '/' && name[1] == '/') {
  43. name = &name[1];
  44. }
  45. fprintf(stdout, "%7s %s\n", sizes, name);
  46. }
  47. static uint64_t count_directory(char * source) {
  48. DIR * dirp = opendir(source);
  49. if (dirp == NULL) {
  50. //fprintf(stderr, "could not open %s\n", source);
  51. return 0;
  52. }
  53. int was_arg = is_arg;
  54. is_arg = 0;
  55. uint64_t total = 0;
  56. struct dirent * ent = readdir(dirp);
  57. while (ent != NULL) {
  58. if (!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) {
  59. ent = readdir(dirp);
  60. continue;
  61. }
  62. char tmp[strlen(source)+strlen(ent->d_name)+2];
  63. sprintf(tmp, "%s/%s", source, ent->d_name);
  64. total += count_thing(tmp);
  65. ent = readdir(dirp);
  66. }
  67. closedir(dirp);
  68. if (all || was_arg) {
  69. print_size(total, source);
  70. }
  71. return total;
  72. }
  73. static uint64_t count_thing(char * tmp) {
  74. struct stat statbuf;
  75. lstat(tmp,&statbuf);
  76. if (S_ISDIR(statbuf.st_mode)) {
  77. return count_directory(tmp);
  78. } else {
  79. if (is_arg) {
  80. print_size(statbuf.st_size, tmp);
  81. }
  82. return statbuf.st_size;
  83. }
  84. }
  85. int main(int argc, char * argv[]) {
  86. int opt;
  87. while ((opt = getopt(argc, argv, "hsc")) != -1) {
  88. switch (opt) {
  89. case 'h': /* human readable */
  90. human = 1;
  91. break;
  92. case 'c':
  93. show_total = 1;
  94. break;
  95. case 's': /* summary */
  96. all = 0;
  97. break;
  98. default:
  99. fprintf(stderr, "rm: unrecognized option '%c'\n", opt);
  100. break;
  101. }
  102. }
  103. int ret = 0;
  104. uint64_t total = 0;
  105. for (int i = optind; i < argc; ++i) {
  106. is_arg = 1;
  107. total += count_thing(argv[i]);
  108. }
  109. if (show_total) {
  110. print_size(total, "total");
  111. }
  112. return ret;
  113. }