C - Modify the fsize program to print the other information contained in the inode entry

本是古典 何须时尚 2022-11-16 15:12 202阅读 0赞

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

  1. /*
  2. * dirent.h - by FreeMan
  3. */
  4. #define NAME_MAX 14 /* longest filename component; */
  5. /* system-dependent */
  6. typedef struct { /* portable directory entry */
  7. long ino; /* inode number */
  8. char name[NAME_MAX + 1]; /* name + '\0' terminator */
  9. } Dirent;
  10. typedef struct { /* minimal DIR: no buffering, etc. */
  11. int fd; /* file descriptor for the directory */
  12. Dirent d; /* the directory entry */
  13. } DIR;
  14. DIR *opendir(char *dirname);
  15. Dirent *readdir(DIR *dfd);
  16. void closedir(DIR *dfd);
  17. /*
  18. * Modify the fsize program to print the other information contained in the inode entry.
  19. *
  20. * FSize.c - by FreeMan
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <io.h>
  25. #include <fcntl.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include "dirent.h"
  29. #include <string.h>
  30. #include <time.h>
  31. void fsize(char *);
  32. int main(int argc, char *argv[])
  33. {
  34. if (argc == 1)
  35. {
  36. fsize(".");
  37. }
  38. else
  39. {
  40. while (--argc > 0)
  41. {
  42. fsize(*++argv);
  43. }
  44. }
  45. return 0;
  46. }
  47. void dirwalk(char *, void (*fcn)(char *));
  48. void fsize(char *name)
  49. {
  50. struct stat stbuf;
  51. if (stat(name, &stbuf) == -1)
  52. {
  53. fprintf(stderr, "fsize: can't access %s\n", name);
  54. return;
  55. }
  56. if ((stbuf.st_mode & _S_IFMT) == _S_IFDIR)
  57. {
  58. dirwalk(name, fsize);
  59. }
  60. printf("%8ld %s\n", stbuf.st_size, name);
  61. }
  62. #define MAX_PATH 1024
  63. void dirwalk(char *dir, void (*fcn)(char *))
  64. {
  65. char name[MAX_PATH];
  66. Dirent *dp;
  67. DIR *dfd;
  68. if ((dfd = opendir(dir)) == NULL)
  69. {
  70. fprintf(stderr, "dirwalk: can't open %s\n", dir);
  71. return;
  72. }
  73. while ((dp = readdir(dfd)) != NULL)
  74. {
  75. if (strcmp(dp->name, ".") == 0 || strcmp(dp->name, "..") == 0)
  76. {
  77. continue;
  78. }
  79. if (strlen(dir) + strlen(dp->name) + 2 > sizeof(name))
  80. {
  81. fprintf(stderr, "dirwalkd: name %s/%s too long\n", dir, dp->name);
  82. }
  83. else
  84. {
  85. sprintf(name, "%s/%s", dir, dp->name);
  86. (*fcn)(name);
  87. }
  88. }
  89. closedir(dfd);
  90. }
  91. #ifndef DIRSIZ
  92. #define DIRSIZ 14
  93. #endif
  94. struct direct
  95. {
  96. ino_t d_ino;
  97. char d_name[DIRSIZ];
  98. };
  99. int fstat(int fd, struct stat *);
  100. DIR *opendir(char *dirname)
  101. {
  102. int fd;
  103. struct stat stbuf;
  104. DIR *dp;
  105. if ((fd = open(dirname, O_RDONLY, 0)) == -1 ||
  106. fstat(fd, &stbuf) == -1 ||
  107. (stbuf.st_mode & _S_IFMT) != _S_IFDIR ||
  108. (dp = (DIR *)malloc(sizeof(DIR))) == NULL)
  109. {
  110. return NULL;
  111. }
  112. dp->fd = fd;
  113. return dp;
  114. }
  115. void closedir(DIR *dp)
  116. {
  117. if (dp)
  118. {
  119. close(dp->fd);
  120. free(dp);
  121. }
  122. }
  123. Dirent *readdir(DIR *dp)
  124. {
  125. struct direct dirbuf;
  126. static Dirent d;
  127. while (read(dp->fd, (char *)&dirbuf, sizeof(dirbuf)) == sizeof(dirbuf))
  128. {
  129. if (dirbuf.d_ino == 0)
  130. {
  131. continue;
  132. }
  133. d.ino = dirbuf.d_ino;
  134. strncpy(d.name, dirbuf.d_name, DIRSIZ);
  135. d.name[DIRSIZ] = '\0';
  136. return &d;
  137. }
  138. return NULL;
  139. }

发表评论

表情:
评论列表 (有 0 条评论,202人围观)

还没有评论,来说两句吧...

相关阅读