Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6.  
  7. static int path_scan(char *path)
  8. {
  9. DIR *d;
  10. struct dirent *ent;
  11.  
  12. if (!(d = opendir(path)))
  13. return -1;
  14.  
  15. while ((ent = readdir(d))) {
  16. char name[256];
  17. struct stat sbuf;
  18. FILE *f;
  19. int c = 0;
  20.  
  21. sprintf(name, "%s/%s", path, ent->d_name);
  22. if (stat(name, &sbuf)) {
  23. printf("unable to stat %s... continuing\n", name);
  24. continue;
  25. }
  26.  
  27. if (S_ISDIR(sbuf.st_mode)) {
  28. printf("%s is a directory... continuing\n", name);
  29. continue;
  30. }
  31.  
  32. if (S_ISCHR(sbuf.st_mode)) {
  33. printf("%s is a character device... continuing\n",
  34. name);
  35. continue;
  36. }
  37.  
  38. if (S_ISBLK(sbuf.st_mode)) {
  39. printf("%s is a block device... continuing\n", name);
  40. continue;
  41. }
  42.  
  43. if (S_ISFIFO(sbuf.st_mode)) {
  44. printf("%s is a fifo device... continuing\n", name);
  45. continue;
  46. }
  47.  
  48. if (S_ISLNK(sbuf.st_mode)) {
  49. printf("%s is a symbolic link... continuing\n", name);
  50. continue;
  51. }
  52.  
  53. if (S_ISSOCK(sbuf.st_mode)) {
  54. printf("%s is a socket... continuing\n", name);
  55. continue;
  56. }
  57.  
  58. if (!(S_ISREG(sbuf.st_mode))) {
  59. printf("%s is not a regular file... continuing\n",
  60. name);
  61. continue;
  62. }
  63.  
  64. if (!(f = fopen(name, "r"))) {
  65. printf("could not open file %s/%s\n", path, name);
  66. continue;
  67. }
  68.  
  69. if (fread(&c, sizeof(char), 1, f) != 1) {
  70. printf("could not read a character from %s\n", name);
  71. fclose(f);
  72. continue;
  73. }
  74.  
  75. fclose(f);
  76. printf("first character in %s is: %d ", name, c);
  77. if (20 <= c && c <=126)
  78. printf("('%c')", (char)c);
  79. else
  80. printf("(N/A)");
  81. printf("\n");
  82. }
  83.  
  84. return closedir(d);
  85. }
  86.  
  87. int main(int argc, char *argv[])
  88. {
  89. if (argc !=2) {
  90. printf("usage: path <path_name>\n");
  91. return -1;
  92. }
  93. return path_scan(argv[1]);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement